blob: ca2f51be8ef960f12ec22f8ebe9f63e7a9c9183f [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"
Imre Deakdb18b6a2016-03-24 12:41:40 +020039#include "intel_dsi.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070040#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080041#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080042#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010043#include <drm/drm_dp_helper.h>
44#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070045#include <drm/drm_plane_helper.h>
46#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080047#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080048#include <linux/reservation.h>
49#include <linux/dma-buf.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080050
Matt Roper465c1202014-05-29 08:06:54 -070051/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010052static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010053 DRM_FORMAT_C8,
54 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070055 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010056 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070057};
58
59/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010060static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010061 DRM_FORMAT_C8,
62 DRM_FORMAT_RGB565,
63 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070064 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010065 DRM_FORMAT_XRGB2101010,
66 DRM_FORMAT_XBGR2101010,
67};
68
69static const uint32_t skl_primary_formats[] = {
70 DRM_FORMAT_C8,
71 DRM_FORMAT_RGB565,
72 DRM_FORMAT_XRGB8888,
73 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010074 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070075 DRM_FORMAT_ABGR8888,
76 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070077 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053078 DRM_FORMAT_YUYV,
79 DRM_FORMAT_YVYU,
80 DRM_FORMAT_UYVY,
81 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070082};
83
Matt Roper3d7d6512014-06-10 08:28:13 -070084/* Cursor formats */
85static const uint32_t intel_cursor_formats[] = {
86 DRM_FORMAT_ARGB8888,
87};
88
Jesse Barnesf1f644d2013-06-27 00:39:25 +030089static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020090 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030091static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020092 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030093
Jesse Barneseb1bfe82014-02-12 12:26:25 -080094static int intel_framebuffer_init(struct drm_device *dev,
95 struct intel_framebuffer *ifb,
96 struct drm_mode_fb_cmd2 *mode_cmd,
97 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020098static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
99static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +0200100static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200101static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700102 struct intel_link_m_n *m_n,
103 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200104static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200105static void haswell_set_pipeconf(struct drm_crtc *crtc);
Jani Nikula391bf042016-03-18 17:05:40 +0200106static void haswell_set_pipemisc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200107static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200108 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200109static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200110 const struct intel_crtc_state *pipe_config);
Maarten Lankhorst613d2b22015-07-21 13:28:58 +0200111static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
112static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700113static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
114 struct intel_crtc_state *crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200115static void skylake_pfit_enable(struct intel_crtc *crtc);
116static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
117static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200118static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200119static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Ville Syrjälä4e5ca602016-05-11 22:44:44 +0300120static int ilk_max_pixel_rate(struct drm_atomic_state *state);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100121
Ma Lingd4906092009-03-18 20:13:27 +0800122struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300123 struct {
124 int min, max;
125 } dot, vco, n, m, m1, m2, p, p1;
126
127 struct {
128 int dot_limit;
129 int p2_slow, p2_fast;
130 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800131};
Jesse Barnes79e53942008-11-07 14:24:08 -0800132
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300133/* returns HPLL frequency in kHz */
134static int valleyview_get_vco(struct drm_i915_private *dev_priv)
135{
136 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
137
138 /* Obtain SKU information */
139 mutex_lock(&dev_priv->sb_lock);
140 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
141 CCK_FUSE_HPLL_FREQ_MASK;
142 mutex_unlock(&dev_priv->sb_lock);
143
144 return vco_freq[hpll_freq] * 1000;
145}
146
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200147int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
148 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300149{
150 u32 val;
151 int divider;
152
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300153 mutex_lock(&dev_priv->sb_lock);
154 val = vlv_cck_read(dev_priv, reg);
155 mutex_unlock(&dev_priv->sb_lock);
156
157 divider = val & CCK_FREQUENCY_VALUES;
158
159 WARN((val & CCK_FREQUENCY_STATUS) !=
160 (divider << CCK_FREQUENCY_STATUS_SHIFT),
161 "%s change in progress\n", name);
162
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200163 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
164}
165
166static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
167 const char *name, u32 reg)
168{
169 if (dev_priv->hpll_freq == 0)
170 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
171
172 return vlv_get_cck_clock(dev_priv, name, reg,
173 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300174}
175
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200176static int
177intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200178{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200179 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200180}
181
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200182static int
183intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300184{
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300185 /* RAWCLK_FREQ_VLV register updated from power well code */
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200186 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
187 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200188}
189
190static int
191intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
192{
Jani Nikula79e50a42015-08-26 10:58:20 +0300193 uint32_t clkcfg;
194
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200195 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300196 clkcfg = I915_READ(CLKCFG);
197 switch (clkcfg & CLKCFG_FSB_MASK) {
198 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200199 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300200 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200201 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300202 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200203 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300204 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200205 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300206 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200207 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300208 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200209 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300210 /* these two are just a guess; one of them might be right */
211 case CLKCFG_FSB_1600:
212 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200213 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300214 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200215 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300216 }
217}
218
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300219void intel_update_rawclk(struct drm_i915_private *dev_priv)
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200220{
221 if (HAS_PCH_SPLIT(dev_priv))
222 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
223 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
224 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
225 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
226 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
227 else
228 return; /* no rawclk on other platforms, or no need to know it */
229
230 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
231}
232
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300233static void intel_update_czclk(struct drm_i915_private *dev_priv)
234{
Wayne Boyer666a4532015-12-09 12:29:35 -0800235 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300236 return;
237
238 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
239 CCK_CZ_CLOCK_CONTROL);
240
241 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
242}
243
Chris Wilson021357a2010-09-07 20:54:59 +0100244static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200245intel_fdi_link_freq(struct drm_i915_private *dev_priv,
246 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100247{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200248 if (HAS_DDI(dev_priv))
249 return pipe_config->port_clock; /* SPLL */
250 else if (IS_GEN5(dev_priv))
251 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200252 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200253 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100254}
255
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300256static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400257 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200258 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200259 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400260 .m = { .min = 96, .max = 140 },
261 .m1 = { .min = 18, .max = 26 },
262 .m2 = { .min = 6, .max = 16 },
263 .p = { .min = 4, .max = 128 },
264 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700265 .p2 = { .dot_limit = 165000,
266 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700267};
268
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300269static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200270 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200271 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200272 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200273 .m = { .min = 96, .max = 140 },
274 .m1 = { .min = 18, .max = 26 },
275 .m2 = { .min = 6, .max = 16 },
276 .p = { .min = 4, .max = 128 },
277 .p1 = { .min = 2, .max = 33 },
278 .p2 = { .dot_limit = 165000,
279 .p2_slow = 4, .p2_fast = 4 },
280};
281
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300282static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400283 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200284 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200285 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400286 .m = { .min = 96, .max = 140 },
287 .m1 = { .min = 18, .max = 26 },
288 .m2 = { .min = 6, .max = 16 },
289 .p = { .min = 4, .max = 128 },
290 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700291 .p2 = { .dot_limit = 165000,
292 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700293};
Eric Anholt273e27c2011-03-30 13:01:10 -0700294
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300295static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400296 .dot = { .min = 20000, .max = 400000 },
297 .vco = { .min = 1400000, .max = 2800000 },
298 .n = { .min = 1, .max = 6 },
299 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100300 .m1 = { .min = 8, .max = 18 },
301 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400302 .p = { .min = 5, .max = 80 },
303 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700304 .p2 = { .dot_limit = 200000,
305 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700306};
307
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300308static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400309 .dot = { .min = 20000, .max = 400000 },
310 .vco = { .min = 1400000, .max = 2800000 },
311 .n = { .min = 1, .max = 6 },
312 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100313 .m1 = { .min = 8, .max = 18 },
314 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400315 .p = { .min = 7, .max = 98 },
316 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700317 .p2 = { .dot_limit = 112000,
318 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700319};
320
Eric Anholt273e27c2011-03-30 13:01:10 -0700321
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300322static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700323 .dot = { .min = 25000, .max = 270000 },
324 .vco = { .min = 1750000, .max = 3500000},
325 .n = { .min = 1, .max = 4 },
326 .m = { .min = 104, .max = 138 },
327 .m1 = { .min = 17, .max = 23 },
328 .m2 = { .min = 5, .max = 11 },
329 .p = { .min = 10, .max = 30 },
330 .p1 = { .min = 1, .max = 3},
331 .p2 = { .dot_limit = 270000,
332 .p2_slow = 10,
333 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800334 },
Keith Packarde4b36692009-06-05 19:22:17 -0700335};
336
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300337static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700338 .dot = { .min = 22000, .max = 400000 },
339 .vco = { .min = 1750000, .max = 3500000},
340 .n = { .min = 1, .max = 4 },
341 .m = { .min = 104, .max = 138 },
342 .m1 = { .min = 16, .max = 23 },
343 .m2 = { .min = 5, .max = 11 },
344 .p = { .min = 5, .max = 80 },
345 .p1 = { .min = 1, .max = 8},
346 .p2 = { .dot_limit = 165000,
347 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700348};
349
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300350static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700351 .dot = { .min = 20000, .max = 115000 },
352 .vco = { .min = 1750000, .max = 3500000 },
353 .n = { .min = 1, .max = 3 },
354 .m = { .min = 104, .max = 138 },
355 .m1 = { .min = 17, .max = 23 },
356 .m2 = { .min = 5, .max = 11 },
357 .p = { .min = 28, .max = 112 },
358 .p1 = { .min = 2, .max = 8 },
359 .p2 = { .dot_limit = 0,
360 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800361 },
Keith Packarde4b36692009-06-05 19:22:17 -0700362};
363
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300364static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700365 .dot = { .min = 80000, .max = 224000 },
366 .vco = { .min = 1750000, .max = 3500000 },
367 .n = { .min = 1, .max = 3 },
368 .m = { .min = 104, .max = 138 },
369 .m1 = { .min = 17, .max = 23 },
370 .m2 = { .min = 5, .max = 11 },
371 .p = { .min = 14, .max = 42 },
372 .p1 = { .min = 2, .max = 6 },
373 .p2 = { .dot_limit = 0,
374 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800375 },
Keith Packarde4b36692009-06-05 19:22:17 -0700376};
377
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300378static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400379 .dot = { .min = 20000, .max = 400000},
380 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700381 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400382 .n = { .min = 3, .max = 6 },
383 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700384 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400385 .m1 = { .min = 0, .max = 0 },
386 .m2 = { .min = 0, .max = 254 },
387 .p = { .min = 5, .max = 80 },
388 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700389 .p2 = { .dot_limit = 200000,
390 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700391};
392
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300393static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400394 .dot = { .min = 20000, .max = 400000 },
395 .vco = { .min = 1700000, .max = 3500000 },
396 .n = { .min = 3, .max = 6 },
397 .m = { .min = 2, .max = 256 },
398 .m1 = { .min = 0, .max = 0 },
399 .m2 = { .min = 0, .max = 254 },
400 .p = { .min = 7, .max = 112 },
401 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700402 .p2 = { .dot_limit = 112000,
403 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700404};
405
Eric Anholt273e27c2011-03-30 13:01:10 -0700406/* Ironlake / Sandybridge
407 *
408 * We calculate clock using (register_value + 2) for N/M1/M2, so here
409 * the range value for them is (actual_value - 2).
410 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300411static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700412 .dot = { .min = 25000, .max = 350000 },
413 .vco = { .min = 1760000, .max = 3510000 },
414 .n = { .min = 1, .max = 5 },
415 .m = { .min = 79, .max = 127 },
416 .m1 = { .min = 12, .max = 22 },
417 .m2 = { .min = 5, .max = 9 },
418 .p = { .min = 5, .max = 80 },
419 .p1 = { .min = 1, .max = 8 },
420 .p2 = { .dot_limit = 225000,
421 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700422};
423
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300424static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700425 .dot = { .min = 25000, .max = 350000 },
426 .vco = { .min = 1760000, .max = 3510000 },
427 .n = { .min = 1, .max = 3 },
428 .m = { .min = 79, .max = 118 },
429 .m1 = { .min = 12, .max = 22 },
430 .m2 = { .min = 5, .max = 9 },
431 .p = { .min = 28, .max = 112 },
432 .p1 = { .min = 2, .max = 8 },
433 .p2 = { .dot_limit = 225000,
434 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800435};
436
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300437static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700438 .dot = { .min = 25000, .max = 350000 },
439 .vco = { .min = 1760000, .max = 3510000 },
440 .n = { .min = 1, .max = 3 },
441 .m = { .min = 79, .max = 127 },
442 .m1 = { .min = 12, .max = 22 },
443 .m2 = { .min = 5, .max = 9 },
444 .p = { .min = 14, .max = 56 },
445 .p1 = { .min = 2, .max = 8 },
446 .p2 = { .dot_limit = 225000,
447 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800448};
449
Eric Anholt273e27c2011-03-30 13:01:10 -0700450/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300451static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700452 .dot = { .min = 25000, .max = 350000 },
453 .vco = { .min = 1760000, .max = 3510000 },
454 .n = { .min = 1, .max = 2 },
455 .m = { .min = 79, .max = 126 },
456 .m1 = { .min = 12, .max = 22 },
457 .m2 = { .min = 5, .max = 9 },
458 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400459 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700460 .p2 = { .dot_limit = 225000,
461 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800462};
463
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300464static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700465 .dot = { .min = 25000, .max = 350000 },
466 .vco = { .min = 1760000, .max = 3510000 },
467 .n = { .min = 1, .max = 3 },
468 .m = { .min = 79, .max = 126 },
469 .m1 = { .min = 12, .max = 22 },
470 .m2 = { .min = 5, .max = 9 },
471 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400472 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700473 .p2 = { .dot_limit = 225000,
474 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800475};
476
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300477static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300478 /*
479 * These are the data rate limits (measured in fast clocks)
480 * since those are the strictest limits we have. The fast
481 * clock and actual rate limits are more relaxed, so checking
482 * them would make no difference.
483 */
484 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200485 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700486 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700487 .m1 = { .min = 2, .max = 3 },
488 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300489 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300490 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700491};
492
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300493static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300494 /*
495 * These are the data rate limits (measured in fast clocks)
496 * since those are the strictest limits we have. The fast
497 * clock and actual rate limits are more relaxed, so checking
498 * them would make no difference.
499 */
500 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200501 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300502 .n = { .min = 1, .max = 1 },
503 .m1 = { .min = 2, .max = 2 },
504 .m2 = { .min = 24 << 22, .max = 175 << 22 },
505 .p1 = { .min = 2, .max = 4 },
506 .p2 = { .p2_slow = 1, .p2_fast = 14 },
507};
508
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300509static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200510 /* FIXME: find real dot limits */
511 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530512 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200513 .n = { .min = 1, .max = 1 },
514 .m1 = { .min = 2, .max = 2 },
515 /* FIXME: find real m2 limits */
516 .m2 = { .min = 2 << 22, .max = 255 << 22 },
517 .p1 = { .min = 2, .max = 4 },
518 .p2 = { .p2_slow = 1, .p2_fast = 20 },
519};
520
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200521static bool
522needs_modeset(struct drm_crtc_state *state)
523{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200524 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200525}
526
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300527/**
528 * Returns whether any output on the specified pipe is of the specified type
529 */
Damien Lespiau40935612014-10-29 11:16:59 +0000530bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300531{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300532 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300533 struct intel_encoder *encoder;
534
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300535 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300536 if (encoder->type == type)
537 return true;
538
539 return false;
540}
541
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200542/**
543 * Returns whether any output on the specified pipe will have the specified
544 * type after a staged modeset is complete, i.e., the same as
545 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
546 * encoder->crtc.
547 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200548static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
549 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200550{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200551 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300552 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200553 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200554 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200555 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200556
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300557 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200558 if (connector_state->crtc != crtc_state->base.crtc)
559 continue;
560
561 num_connectors++;
562
563 encoder = to_intel_encoder(connector_state->best_encoder);
564 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200565 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200566 }
567
568 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200569
570 return false;
571}
572
Imre Deakdccbea32015-06-22 23:35:51 +0300573/*
574 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
575 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
576 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
577 * The helpers' return value is the rate of the clock that is fed to the
578 * display engine's pipe which can be the above fast dot clock rate or a
579 * divided-down version of it.
580 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500581/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300582static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800583{
Shaohua Li21778322009-02-23 15:19:16 +0800584 clock->m = clock->m2 + 2;
585 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200586 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300587 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300588 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
589 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300590
591 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800592}
593
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200594static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
595{
596 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
597}
598
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300599static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800600{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200601 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800602 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200603 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300604 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300605 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
606 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300607
608 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800609}
610
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300611static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300612{
613 clock->m = clock->m1 * clock->m2;
614 clock->p = clock->p1 * clock->p2;
615 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300616 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300617 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
618 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300619
620 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300621}
622
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300623int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300624{
625 clock->m = clock->m1 * clock->m2;
626 clock->p = clock->p1 * clock->p2;
627 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300628 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300629 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
630 clock->n << 22);
631 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300632
633 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300634}
635
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800636#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800637/**
638 * Returns whether the given set of divisors are valid for a given refclk with
639 * the given connectors.
640 */
641
Chris Wilson1b894b52010-12-14 20:04:54 +0000642static bool intel_PLL_is_valid(struct drm_device *dev,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300643 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300644 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800645{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300646 if (clock->n < limit->n.min || limit->n.max < clock->n)
647 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800648 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400649 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800650 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400651 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800652 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400653 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300654
Wayne Boyer666a4532015-12-09 12:29:35 -0800655 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
656 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300657 if (clock->m1 <= clock->m2)
658 INTELPllInvalid("m1 <= m2\n");
659
Wayne Boyer666a4532015-12-09 12:29:35 -0800660 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300661 if (clock->p < limit->p.min || limit->p.max < clock->p)
662 INTELPllInvalid("p out of range\n");
663 if (clock->m < limit->m.min || limit->m.max < clock->m)
664 INTELPllInvalid("m out of range\n");
665 }
666
Jesse Barnes79e53942008-11-07 14:24:08 -0800667 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400668 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800669 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
670 * connector, etc., rather than just a single range.
671 */
672 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400673 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800674
675 return true;
676}
677
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300678static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300679i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300680 const struct intel_crtc_state *crtc_state,
681 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800682{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300683 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800684
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200685 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800686 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100687 * For LVDS just rely on its current settings for dual-channel.
688 * We haven't figured out how to reliably set up different
689 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800690 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100691 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300692 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800693 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300694 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800695 } else {
696 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300697 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800698 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300699 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800700 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300701}
702
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200703/*
704 * Returns a set of divisors for the desired target clock with the given
705 * refclk, or FALSE. The returned values represent the clock equation:
706 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
707 *
708 * Target and reference clocks are specified in kHz.
709 *
710 * If match_clock is provided, then best_clock P divider must match the P
711 * divider from @match_clock used for LVDS downclocking.
712 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300713static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300714i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300715 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300716 int target, int refclk, struct dpll *match_clock,
717 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300718{
719 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300720 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300721 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800722
Akshay Joshi0206e352011-08-16 15:34:10 -0400723 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800724
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300725 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
726
Zhao Yakui42158662009-11-20 11:24:18 +0800727 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
728 clock.m1++) {
729 for (clock.m2 = limit->m2.min;
730 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200731 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800732 break;
733 for (clock.n = limit->n.min;
734 clock.n <= limit->n.max; clock.n++) {
735 for (clock.p1 = limit->p1.min;
736 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800737 int this_err;
738
Imre Deakdccbea32015-06-22 23:35:51 +0300739 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000740 if (!intel_PLL_is_valid(dev, limit,
741 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800742 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800743 if (match_clock &&
744 clock.p != match_clock->p)
745 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800746
747 this_err = abs(clock.dot - target);
748 if (this_err < err) {
749 *best_clock = clock;
750 err = this_err;
751 }
752 }
753 }
754 }
755 }
756
757 return (err != target);
758}
759
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200760/*
761 * Returns a set of divisors for the desired target clock with the given
762 * refclk, or FALSE. The returned values represent the clock equation:
763 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
764 *
765 * Target and reference clocks are specified in kHz.
766 *
767 * If match_clock is provided, then best_clock P divider must match the P
768 * divider from @match_clock used for LVDS downclocking.
769 */
Ma Lingd4906092009-03-18 20:13:27 +0800770static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300771pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200772 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300773 int target, int refclk, struct dpll *match_clock,
774 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200775{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300776 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300777 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200778 int err = target;
779
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200780 memset(best_clock, 0, sizeof(*best_clock));
781
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300782 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
783
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200784 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
785 clock.m1++) {
786 for (clock.m2 = limit->m2.min;
787 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200788 for (clock.n = limit->n.min;
789 clock.n <= limit->n.max; clock.n++) {
790 for (clock.p1 = limit->p1.min;
791 clock.p1 <= limit->p1.max; clock.p1++) {
792 int this_err;
793
Imre Deakdccbea32015-06-22 23:35:51 +0300794 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800795 if (!intel_PLL_is_valid(dev, limit,
796 &clock))
797 continue;
798 if (match_clock &&
799 clock.p != match_clock->p)
800 continue;
801
802 this_err = abs(clock.dot - target);
803 if (this_err < err) {
804 *best_clock = clock;
805 err = this_err;
806 }
807 }
808 }
809 }
810 }
811
812 return (err != target);
813}
814
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200815/*
816 * Returns a set of divisors for the desired target clock with the given
817 * refclk, or FALSE. The returned values represent the clock equation:
818 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200819 *
820 * Target and reference clocks are specified in kHz.
821 *
822 * If match_clock is provided, then best_clock P divider must match the P
823 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200824 */
Ma Lingd4906092009-03-18 20:13:27 +0800825static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300826g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200827 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300828 int target, int refclk, struct dpll *match_clock,
829 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800830{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300831 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300832 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800833 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300834 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400835 /* approximately equals target * 0.00585 */
836 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800837
838 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300839
840 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
841
Ma Lingd4906092009-03-18 20:13:27 +0800842 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200843 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800844 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200845 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800846 for (clock.m1 = limit->m1.max;
847 clock.m1 >= limit->m1.min; clock.m1--) {
848 for (clock.m2 = limit->m2.max;
849 clock.m2 >= limit->m2.min; clock.m2--) {
850 for (clock.p1 = limit->p1.max;
851 clock.p1 >= limit->p1.min; clock.p1--) {
852 int this_err;
853
Imre Deakdccbea32015-06-22 23:35:51 +0300854 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000855 if (!intel_PLL_is_valid(dev, limit,
856 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800857 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000858
859 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800860 if (this_err < err_most) {
861 *best_clock = clock;
862 err_most = this_err;
863 max_n = clock.n;
864 found = true;
865 }
866 }
867 }
868 }
869 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800870 return found;
871}
Ma Lingd4906092009-03-18 20:13:27 +0800872
Imre Deakd5dd62b2015-03-17 11:40:03 +0200873/*
874 * Check if the calculated PLL configuration is more optimal compared to the
875 * best configuration and error found so far. Return the calculated error.
876 */
877static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300878 const struct dpll *calculated_clock,
879 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200880 unsigned int best_error_ppm,
881 unsigned int *error_ppm)
882{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200883 /*
884 * For CHV ignore the error and consider only the P value.
885 * Prefer a bigger P value based on HW requirements.
886 */
887 if (IS_CHERRYVIEW(dev)) {
888 *error_ppm = 0;
889
890 return calculated_clock->p > best_clock->p;
891 }
892
Imre Deak24be4e42015-03-17 11:40:04 +0200893 if (WARN_ON_ONCE(!target_freq))
894 return false;
895
Imre Deakd5dd62b2015-03-17 11:40:03 +0200896 *error_ppm = div_u64(1000000ULL *
897 abs(target_freq - calculated_clock->dot),
898 target_freq);
899 /*
900 * Prefer a better P value over a better (smaller) error if the error
901 * is small. Ensure this preference for future configurations too by
902 * setting the error to 0.
903 */
904 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
905 *error_ppm = 0;
906
907 return true;
908 }
909
910 return *error_ppm + 10 < best_error_ppm;
911}
912
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200913/*
914 * Returns a set of divisors for the desired target clock with the given
915 * refclk, or FALSE. The returned values represent the clock equation:
916 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
917 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800918static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300919vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200920 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300921 int target, int refclk, struct dpll *match_clock,
922 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700923{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200924 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300925 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300926 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300927 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300928 /* min update 19.2 MHz */
929 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300930 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700931
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300932 target *= 5; /* fast clock */
933
934 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700935
936 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300937 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300938 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300939 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300940 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300941 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700942 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300943 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200944 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300945
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300946 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
947 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300948
Imre Deakdccbea32015-06-22 23:35:51 +0300949 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300950
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300951 if (!intel_PLL_is_valid(dev, limit,
952 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300953 continue;
954
Imre Deakd5dd62b2015-03-17 11:40:03 +0200955 if (!vlv_PLL_is_optimal(dev, target,
956 &clock,
957 best_clock,
958 bestppm, &ppm))
959 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300960
Imre Deakd5dd62b2015-03-17 11:40:03 +0200961 *best_clock = clock;
962 bestppm = ppm;
963 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700964 }
965 }
966 }
967 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700968
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300969 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700970}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700971
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200972/*
973 * Returns a set of divisors for the desired target clock with the given
974 * refclk, or FALSE. The returned values represent the clock equation:
975 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
976 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300977static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300978chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200979 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300980 int target, int refclk, struct dpll *match_clock,
981 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300982{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200983 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300984 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200985 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300986 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300987 uint64_t m2;
988 int found = false;
989
990 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200991 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300992
993 /*
994 * Based on hardware doc, the n always set to 1, and m1 always
995 * set to 2. If requires to support 200Mhz refclk, we need to
996 * revisit this because n may not 1 anymore.
997 */
998 clock.n = 1, clock.m1 = 2;
999 target *= 5; /* fast clock */
1000
1001 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1002 for (clock.p2 = limit->p2.p2_fast;
1003 clock.p2 >= limit->p2.p2_slow;
1004 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001005 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001006
1007 clock.p = clock.p1 * clock.p2;
1008
1009 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1010 clock.n) << 22, refclk * clock.m1);
1011
1012 if (m2 > INT_MAX/clock.m1)
1013 continue;
1014
1015 clock.m2 = m2;
1016
Imre Deakdccbea32015-06-22 23:35:51 +03001017 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001018
1019 if (!intel_PLL_is_valid(dev, limit, &clock))
1020 continue;
1021
Imre Deak9ca3ba02015-03-17 11:40:05 +02001022 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1023 best_error_ppm, &error_ppm))
1024 continue;
1025
1026 *best_clock = clock;
1027 best_error_ppm = error_ppm;
1028 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001029 }
1030 }
1031
1032 return found;
1033}
1034
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001035bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03001036 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001037{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001038 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03001039 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001040
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001041 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001042 target_clock, refclk, NULL, best_clock);
1043}
1044
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001045bool intel_crtc_active(struct drm_crtc *crtc)
1046{
1047 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1048
1049 /* Be paranoid as we can arrive here with only partial
1050 * state retrieved from the hardware during setup.
1051 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001052 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001053 * as Haswell has gained clock readout/fastboot support.
1054 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001055 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001056 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001057 *
1058 * FIXME: The intel_crtc->active here should be switched to
1059 * crtc->state->active once we have proper CRTC states wired up
1060 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001061 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001062 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001063 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001064}
1065
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001066enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1067 enum pipe pipe)
1068{
1069 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1070 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1071
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001072 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001073}
1074
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001075static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1076{
1077 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001078 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001079 u32 line1, line2;
1080 u32 line_mask;
1081
1082 if (IS_GEN2(dev))
1083 line_mask = DSL_LINEMASK_GEN2;
1084 else
1085 line_mask = DSL_LINEMASK_GEN3;
1086
1087 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001088 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001089 line2 = I915_READ(reg) & line_mask;
1090
1091 return line1 == line2;
1092}
1093
Keith Packardab7ad7f2010-10-03 00:33:06 -07001094/*
1095 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001096 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001097 *
1098 * After disabling a pipe, we can't wait for vblank in the usual way,
1099 * spinning on the vblank interrupt status bit, since we won't actually
1100 * see an interrupt when the pipe is disabled.
1101 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001102 * On Gen4 and above:
1103 * wait for the pipe register state bit to turn off
1104 *
1105 * Otherwise:
1106 * wait for the display line value to settle (it usually
1107 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001108 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001109 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001110static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001111{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001112 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001113 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001114 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001115 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001116
Keith Packardab7ad7f2010-10-03 00:33:06 -07001117 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001118 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001119
Keith Packardab7ad7f2010-10-03 00:33:06 -07001120 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001121 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1122 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001123 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001124 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001125 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001126 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001127 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001128 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001129}
1130
Jesse Barnesb24e7172011-01-04 15:09:30 -08001131/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001132void assert_pll(struct drm_i915_private *dev_priv,
1133 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001134{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001135 u32 val;
1136 bool cur_state;
1137
Ville Syrjälä649636e2015-09-22 19:50:01 +03001138 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001139 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001140 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001141 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001142 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001143}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001144
Jani Nikula23538ef2013-08-27 15:12:22 +03001145/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001146void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001147{
1148 u32 val;
1149 bool cur_state;
1150
Ville Syrjäläa5805162015-05-26 20:42:30 +03001151 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001152 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001153 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001154
1155 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001156 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001157 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001158 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001159}
Jani Nikula23538ef2013-08-27 15:12:22 +03001160
Jesse Barnes040484a2011-01-03 12:14:26 -08001161static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1162 enum pipe pipe, bool state)
1163{
Jesse Barnes040484a2011-01-03 12:14:26 -08001164 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001165 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1166 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001167
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001168 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001169 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001170 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001171 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001172 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001173 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001174 cur_state = !!(val & FDI_TX_ENABLE);
1175 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001176 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001177 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001178 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001179}
1180#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1181#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1182
1183static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1184 enum pipe pipe, bool state)
1185{
Jesse Barnes040484a2011-01-03 12:14:26 -08001186 u32 val;
1187 bool cur_state;
1188
Ville Syrjälä649636e2015-09-22 19:50:01 +03001189 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001190 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001191 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001192 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001193 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001194}
1195#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1196#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1197
1198static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1199 enum pipe pipe)
1200{
Jesse Barnes040484a2011-01-03 12:14:26 -08001201 u32 val;
1202
1203 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001204 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001205 return;
1206
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001207 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001208 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001209 return;
1210
Ville Syrjälä649636e2015-09-22 19:50:01 +03001211 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001212 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
Jesse Barnes040484a2011-01-03 12:14:26 -08001213}
1214
Daniel Vetter55607e82013-06-16 21:42:39 +02001215void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1216 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001217{
Jesse Barnes040484a2011-01-03 12:14:26 -08001218 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001219 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001220
Ville Syrjälä649636e2015-09-22 19:50:01 +03001221 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001222 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001223 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001224 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001225 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001226}
1227
Daniel Vetterb680c372014-09-19 18:27:27 +02001228void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1229 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001230{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001231 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001232 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001233 u32 val;
1234 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001235 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001236
Jani Nikulabedd4db2014-08-22 15:04:13 +03001237 if (WARN_ON(HAS_DDI(dev)))
1238 return;
1239
1240 if (HAS_PCH_SPLIT(dev)) {
1241 u32 port_sel;
1242
Jesse Barnesea0760c2011-01-04 15:09:32 -08001243 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001244 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1245
1246 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1247 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1248 panel_pipe = PIPE_B;
1249 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001250 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001251 /* presumably write lock depends on pipe, not port select */
1252 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1253 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001254 } else {
1255 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001256 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1257 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001258 }
1259
1260 val = I915_READ(pp_reg);
1261 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001262 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001263 locked = false;
1264
Rob Clarke2c719b2014-12-15 13:56:32 -05001265 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001266 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001267 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001268}
1269
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001270static void assert_cursor(struct drm_i915_private *dev_priv,
1271 enum pipe pipe, bool state)
1272{
1273 struct drm_device *dev = dev_priv->dev;
1274 bool cur_state;
1275
Paulo Zanonid9d82082014-02-27 16:30:56 -03001276 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001277 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001278 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001279 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001280
Rob Clarke2c719b2014-12-15 13:56:32 -05001281 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001282 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001283 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001284}
1285#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1286#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1287
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001288void assert_pipe(struct drm_i915_private *dev_priv,
1289 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001290{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001291 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001292 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1293 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001294 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001295
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001296 /* if we need the pipe quirk it must be always on */
1297 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1298 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001299 state = true;
1300
Imre Deak4feed0e2016-02-12 18:55:14 +02001301 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1302 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001303 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001304 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001305
1306 intel_display_power_put(dev_priv, power_domain);
1307 } else {
1308 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001309 }
1310
Rob Clarke2c719b2014-12-15 13:56:32 -05001311 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001312 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001313 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001314}
1315
Chris Wilson931872f2012-01-16 23:01:13 +00001316static void assert_plane(struct drm_i915_private *dev_priv,
1317 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001318{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001319 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001320 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001321
Ville Syrjälä649636e2015-09-22 19:50:01 +03001322 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001323 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001324 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001325 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001326 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001327}
1328
Chris Wilson931872f2012-01-16 23:01:13 +00001329#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1330#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1331
Jesse Barnesb24e7172011-01-04 15:09:30 -08001332static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1333 enum pipe pipe)
1334{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001335 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001336 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001337
Ville Syrjälä653e1022013-06-04 13:49:05 +03001338 /* Primary planes are fixed to pipes on gen4+ */
1339 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001340 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001341 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001342 "plane %c assertion failure, should be disabled but not\n",
1343 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001344 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001345 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001346
Jesse Barnesb24e7172011-01-04 15:09:30 -08001347 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001348 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001349 u32 val = I915_READ(DSPCNTR(i));
1350 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001351 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001352 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001353 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1354 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001355 }
1356}
1357
Jesse Barnes19332d72013-03-28 09:55:38 -07001358static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1359 enum pipe pipe)
1360{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001361 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001362 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001363
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001364 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001365 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001366 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001367 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001368 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1369 sprite, pipe_name(pipe));
1370 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001371 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001372 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001373 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001374 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001375 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001376 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001377 }
1378 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001379 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001380 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001381 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001382 plane_name(pipe), pipe_name(pipe));
1383 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001384 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001385 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001386 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1387 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001388 }
1389}
1390
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001391static void assert_vblank_disabled(struct drm_crtc *crtc)
1392{
Rob Clarke2c719b2014-12-15 13:56:32 -05001393 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001394 drm_crtc_vblank_put(crtc);
1395}
1396
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001397void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1398 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001399{
Jesse Barnes92f25842011-01-04 15:09:34 -08001400 u32 val;
1401 bool enabled;
1402
Ville Syrjälä649636e2015-09-22 19:50:01 +03001403 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001404 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001405 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001406 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1407 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001408}
1409
Keith Packard4e634382011-08-06 10:39:45 -07001410static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1411 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001412{
1413 if ((val & DP_PORT_EN) == 0)
1414 return false;
1415
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001416 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001417 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001418 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1419 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001420 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001421 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1422 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001423 } else {
1424 if ((val & DP_PIPE_MASK) != (pipe << 30))
1425 return false;
1426 }
1427 return true;
1428}
1429
Keith Packard1519b992011-08-06 10:35:34 -07001430static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1431 enum pipe pipe, u32 val)
1432{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001433 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001434 return false;
1435
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001436 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001437 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001438 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001439 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001440 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1441 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001442 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001443 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001444 return false;
1445 }
1446 return true;
1447}
1448
1449static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1450 enum pipe pipe, u32 val)
1451{
1452 if ((val & LVDS_PORT_EN) == 0)
1453 return false;
1454
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001455 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001456 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1457 return false;
1458 } else {
1459 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1460 return false;
1461 }
1462 return true;
1463}
1464
1465static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1466 enum pipe pipe, u32 val)
1467{
1468 if ((val & ADPA_DAC_ENABLE) == 0)
1469 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001470 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001471 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1472 return false;
1473 } else {
1474 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1475 return false;
1476 }
1477 return true;
1478}
1479
Jesse Barnes291906f2011-02-02 12:28:03 -08001480static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001481 enum pipe pipe, i915_reg_t reg,
1482 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001483{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001484 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001485 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001486 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001487 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001488
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001489 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001490 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001491 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001492}
1493
1494static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001495 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001496{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001497 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001498 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001499 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001500 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001501
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001502 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001503 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001504 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001505}
1506
1507static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1508 enum pipe pipe)
1509{
Jesse Barnes291906f2011-02-02 12:28:03 -08001510 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001511
Keith Packardf0575e92011-07-25 22:12:43 -07001512 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1513 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1514 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001515
Ville Syrjälä649636e2015-09-22 19:50:01 +03001516 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001517 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001518 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001519 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001520
Ville Syrjälä649636e2015-09-22 19:50:01 +03001521 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001522 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001523 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001524 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001525
Paulo Zanonie2debe92013-02-18 19:00:27 -03001526 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1527 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1528 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001529}
1530
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001531static void _vlv_enable_pll(struct intel_crtc *crtc,
1532 const struct intel_crtc_state *pipe_config)
1533{
1534 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1535 enum pipe pipe = crtc->pipe;
1536
1537 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1538 POSTING_READ(DPLL(pipe));
1539 udelay(150);
1540
1541 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1542 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1543}
1544
Ville Syrjäläd288f652014-10-28 13:20:22 +02001545static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001546 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001547{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001548 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001549 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001550
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001551 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001552
Daniel Vetter87442f72013-06-06 00:52:17 +02001553 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001554 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001555
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001556 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1557 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001558
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001559 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1560 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001561}
1562
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001563
1564static void _chv_enable_pll(struct intel_crtc *crtc,
1565 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001566{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001567 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001568 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001569 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001570 u32 tmp;
1571
Ville Syrjäläa5805162015-05-26 20:42:30 +03001572 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001573
1574 /* Enable back the 10bit clock to display controller */
1575 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1576 tmp |= DPIO_DCLKP_EN;
1577 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1578
Ville Syrjälä54433e92015-05-26 20:42:31 +03001579 mutex_unlock(&dev_priv->sb_lock);
1580
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001581 /*
1582 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1583 */
1584 udelay(1);
1585
1586 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001587 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001588
1589 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001590 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001591 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001592}
1593
1594static void chv_enable_pll(struct intel_crtc *crtc,
1595 const struct intel_crtc_state *pipe_config)
1596{
1597 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1598 enum pipe pipe = crtc->pipe;
1599
1600 assert_pipe_disabled(dev_priv, pipe);
1601
1602 /* PLL is protected by panel, make sure we can write it */
1603 assert_panel_unlocked(dev_priv, pipe);
1604
1605 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1606 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001607
Ville Syrjäläc2317752016-03-15 16:39:56 +02001608 if (pipe != PIPE_A) {
1609 /*
1610 * WaPixelRepeatModeFixForC0:chv
1611 *
1612 * DPLLCMD is AWOL. Use chicken bits to propagate
1613 * the value from DPLLBMD to either pipe B or C.
1614 */
1615 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1616 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1617 I915_WRITE(CBR4_VLV, 0);
1618 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1619
1620 /*
1621 * DPLLB VGA mode also seems to cause problems.
1622 * We should always have it disabled.
1623 */
1624 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1625 } else {
1626 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1627 POSTING_READ(DPLL_MD(pipe));
1628 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001629}
1630
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001631static int intel_num_dvo_pipes(struct drm_device *dev)
1632{
1633 struct intel_crtc *crtc;
1634 int count = 0;
1635
1636 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001637 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001638 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001639
1640 return count;
1641}
1642
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001643static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001644{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001645 struct drm_device *dev = crtc->base.dev;
1646 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001647 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001648 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001649
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001650 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001651
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001652 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001653 if (IS_MOBILE(dev) && !IS_I830(dev))
1654 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001655
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001656 /* Enable DVO 2x clock on both PLLs if necessary */
1657 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1658 /*
1659 * It appears to be important that we don't enable this
1660 * for the current pipe before otherwise configuring the
1661 * PLL. No idea how this should be handled if multiple
1662 * DVO outputs are enabled simultaneosly.
1663 */
1664 dpll |= DPLL_DVO_2X_MODE;
1665 I915_WRITE(DPLL(!crtc->pipe),
1666 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1667 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001668
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001669 /*
1670 * Apparently we need to have VGA mode enabled prior to changing
1671 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1672 * dividers, even though the register value does change.
1673 */
1674 I915_WRITE(reg, 0);
1675
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001676 I915_WRITE(reg, dpll);
1677
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001678 /* Wait for the clocks to stabilize. */
1679 POSTING_READ(reg);
1680 udelay(150);
1681
1682 if (INTEL_INFO(dev)->gen >= 4) {
1683 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001684 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001685 } else {
1686 /* The pixel multiplier can only be updated once the
1687 * DPLL is enabled and the clocks are stable.
1688 *
1689 * So write it again.
1690 */
1691 I915_WRITE(reg, dpll);
1692 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001693
1694 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001695 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001696 POSTING_READ(reg);
1697 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001698 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001699 POSTING_READ(reg);
1700 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001701 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001702 POSTING_READ(reg);
1703 udelay(150); /* wait for warmup */
1704}
1705
1706/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001707 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001708 * @dev_priv: i915 private structure
1709 * @pipe: pipe PLL to disable
1710 *
1711 * Disable the PLL for @pipe, making sure the pipe is off first.
1712 *
1713 * Note! This is for pre-ILK only.
1714 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001715static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001716{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001717 struct drm_device *dev = crtc->base.dev;
1718 struct drm_i915_private *dev_priv = dev->dev_private;
1719 enum pipe pipe = crtc->pipe;
1720
1721 /* Disable DVO 2x clock on both PLLs if necessary */
1722 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001723 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001724 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001725 I915_WRITE(DPLL(PIPE_B),
1726 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1727 I915_WRITE(DPLL(PIPE_A),
1728 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1729 }
1730
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001731 /* Don't disable pipe or pipe PLLs if needed */
1732 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1733 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001734 return;
1735
1736 /* Make sure the pipe isn't still relying on us */
1737 assert_pipe_disabled(dev_priv, pipe);
1738
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001739 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001740 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001741}
1742
Jesse Barnesf6071162013-10-01 10:41:38 -07001743static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1744{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001745 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001746
1747 /* Make sure the pipe isn't still relying on us */
1748 assert_pipe_disabled(dev_priv, pipe);
1749
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001750 val = DPLL_INTEGRATED_REF_CLK_VLV |
1751 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1752 if (pipe != PIPE_A)
1753 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1754
Jesse Barnesf6071162013-10-01 10:41:38 -07001755 I915_WRITE(DPLL(pipe), val);
1756 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001757}
1758
1759static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1760{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001761 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001762 u32 val;
1763
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001764 /* Make sure the pipe isn't still relying on us */
1765 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001766
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001767 val = DPLL_SSC_REF_CLK_CHV |
1768 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001769 if (pipe != PIPE_A)
1770 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001771
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001772 I915_WRITE(DPLL(pipe), val);
1773 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001774
Ville Syrjäläa5805162015-05-26 20:42:30 +03001775 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001776
1777 /* Disable 10bit clock to display controller */
1778 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1779 val &= ~DPIO_DCLKP_EN;
1780 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1781
Ville Syrjäläa5805162015-05-26 20:42:30 +03001782 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001783}
1784
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001785void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001786 struct intel_digital_port *dport,
1787 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001788{
1789 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001790 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001791
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001792 switch (dport->port) {
1793 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001794 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001795 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001796 break;
1797 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001798 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001799 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001800 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001801 break;
1802 case PORT_D:
1803 port_mask = DPLL_PORTD_READY_MASK;
1804 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001805 break;
1806 default:
1807 BUG();
1808 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001809
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001810 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1811 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1812 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001813}
1814
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001815static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1816 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001817{
Daniel Vetter23670b322012-11-01 09:15:30 +01001818 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001819 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001821 i915_reg_t reg;
1822 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001823
Jesse Barnes040484a2011-01-03 12:14:26 -08001824 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001825 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001826
1827 /* FDI must be feeding us bits for PCH ports */
1828 assert_fdi_tx_enabled(dev_priv, pipe);
1829 assert_fdi_rx_enabled(dev_priv, pipe);
1830
Daniel Vetter23670b322012-11-01 09:15:30 +01001831 if (HAS_PCH_CPT(dev)) {
1832 /* Workaround: Set the timing override bit before enabling the
1833 * pch transcoder. */
1834 reg = TRANS_CHICKEN2(pipe);
1835 val = I915_READ(reg);
1836 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1837 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001838 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001839
Daniel Vetterab9412b2013-05-03 11:49:46 +02001840 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001841 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001842 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001843
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001844 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001845 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001846 * Make the BPC in transcoder be consistent with
1847 * that in pipeconf reg. For HDMI we must use 8bpc
1848 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001849 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001850 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001851 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1852 val |= PIPECONF_8BPC;
1853 else
1854 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001855 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001856
1857 val &= ~TRANS_INTERLACE_MASK;
1858 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001859 if (HAS_PCH_IBX(dev_priv) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001860 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001861 val |= TRANS_LEGACY_INTERLACED_ILK;
1862 else
1863 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001864 else
1865 val |= TRANS_PROGRESSIVE;
1866
Jesse Barnes040484a2011-01-03 12:14:26 -08001867 I915_WRITE(reg, val | TRANS_ENABLE);
1868 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001869 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001870}
1871
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001872static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001873 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001874{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001875 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001876
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001877 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001878 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001879 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001880
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001881 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001882 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001883 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001884 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001885
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001886 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001887 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001888
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001889 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1890 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001891 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001892 else
1893 val |= TRANS_PROGRESSIVE;
1894
Daniel Vetterab9412b2013-05-03 11:49:46 +02001895 I915_WRITE(LPT_TRANSCONF, val);
1896 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001897 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001898}
1899
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001900static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1901 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001902{
Daniel Vetter23670b322012-11-01 09:15:30 +01001903 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001904 i915_reg_t reg;
1905 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001906
1907 /* FDI relies on the transcoder */
1908 assert_fdi_tx_disabled(dev_priv, pipe);
1909 assert_fdi_rx_disabled(dev_priv, pipe);
1910
Jesse Barnes291906f2011-02-02 12:28:03 -08001911 /* Ports must be off as well */
1912 assert_pch_ports_disabled(dev_priv, pipe);
1913
Daniel Vetterab9412b2013-05-03 11:49:46 +02001914 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001915 val = I915_READ(reg);
1916 val &= ~TRANS_ENABLE;
1917 I915_WRITE(reg, val);
1918 /* wait for PCH transcoder off, transcoder state */
1919 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001920 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001921
Ville Syrjäläc4656132015-10-29 21:25:56 +02001922 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001923 /* Workaround: Clear the timing override chicken bit again. */
1924 reg = TRANS_CHICKEN2(pipe);
1925 val = I915_READ(reg);
1926 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1927 I915_WRITE(reg, val);
1928 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001929}
1930
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001931static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001932{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001933 u32 val;
1934
Daniel Vetterab9412b2013-05-03 11:49:46 +02001935 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001936 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001937 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001938 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001939 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001940 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001941
1942 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001943 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001944 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001945 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001946}
1947
1948/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001949 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001950 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001951 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001952 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001953 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001954 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001955static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001956{
Paulo Zanoni03722642014-01-17 13:51:09 -02001957 struct drm_device *dev = crtc->base.dev;
1958 struct drm_i915_private *dev_priv = dev->dev_private;
1959 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001960 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001961 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001962 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001963 u32 val;
1964
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001965 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1966
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001967 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001968 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001969 assert_sprites_disabled(dev_priv, pipe);
1970
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001971 if (HAS_PCH_LPT(dev_priv))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001972 pch_transcoder = TRANSCODER_A;
1973 else
1974 pch_transcoder = pipe;
1975
Jesse Barnesb24e7172011-01-04 15:09:30 -08001976 /*
1977 * A pipe without a PLL won't actually be able to drive bits from
1978 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1979 * need the check.
1980 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001981 if (HAS_GMCH_DISPLAY(dev_priv))
Jani Nikulaa65347b2015-11-27 12:21:46 +02001982 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03001983 assert_dsi_pll_enabled(dev_priv);
1984 else
1985 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001986 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001987 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001988 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001989 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001990 assert_fdi_tx_pll_enabled(dev_priv,
1991 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001992 }
1993 /* FIXME: assert CPU port conditions for SNB+ */
1994 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001995
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001996 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001997 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001998 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001999 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2000 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002001 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002002 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002003
2004 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002005 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02002006
2007 /*
2008 * Until the pipe starts DSL will read as 0, which would cause
2009 * an apparent vblank timestamp jump, which messes up also the
2010 * frame count when it's derived from the timestamps. So let's
2011 * wait for the pipe to start properly before we call
2012 * drm_crtc_vblank_on()
2013 */
2014 if (dev->max_vblank_count == 0 &&
2015 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2016 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002017}
2018
2019/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002020 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002021 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002022 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002023 * Disable the pipe of @crtc, making sure that various hardware
2024 * specific requirements are met, if applicable, e.g. plane
2025 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002026 *
2027 * Will wait until the pipe has shut down before returning.
2028 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002029static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002030{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002031 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002032 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002033 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002034 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002035 u32 val;
2036
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002037 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2038
Jesse Barnesb24e7172011-01-04 15:09:30 -08002039 /*
2040 * Make sure planes won't keep trying to pump pixels to us,
2041 * or we might hang the display.
2042 */
2043 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002044 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002045 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002046
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002047 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002048 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002049 if ((val & PIPECONF_ENABLE) == 0)
2050 return;
2051
Ville Syrjälä67adc642014-08-15 01:21:57 +03002052 /*
2053 * Double wide has implications for planes
2054 * so best keep it disabled when not needed.
2055 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002056 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002057 val &= ~PIPECONF_DOUBLE_WIDE;
2058
2059 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002060 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2061 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002062 val &= ~PIPECONF_ENABLE;
2063
2064 I915_WRITE(reg, val);
2065 if ((val & PIPECONF_ENABLE) == 0)
2066 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002067}
2068
Chris Wilson693db182013-03-05 14:52:39 +00002069static bool need_vtd_wa(struct drm_device *dev)
2070{
2071#ifdef CONFIG_INTEL_IOMMU
2072 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2073 return true;
2074#endif
2075 return false;
2076}
2077
Ville Syrjälä832be822016-01-12 21:08:33 +02002078static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2079{
2080 return IS_GEN2(dev_priv) ? 2048 : 4096;
2081}
2082
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002083static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2084 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002085{
2086 switch (fb_modifier) {
2087 case DRM_FORMAT_MOD_NONE:
2088 return cpp;
2089 case I915_FORMAT_MOD_X_TILED:
2090 if (IS_GEN2(dev_priv))
2091 return 128;
2092 else
2093 return 512;
2094 case I915_FORMAT_MOD_Y_TILED:
2095 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2096 return 128;
2097 else
2098 return 512;
2099 case I915_FORMAT_MOD_Yf_TILED:
2100 switch (cpp) {
2101 case 1:
2102 return 64;
2103 case 2:
2104 case 4:
2105 return 128;
2106 case 8:
2107 case 16:
2108 return 256;
2109 default:
2110 MISSING_CASE(cpp);
2111 return cpp;
2112 }
2113 break;
2114 default:
2115 MISSING_CASE(fb_modifier);
2116 return cpp;
2117 }
2118}
2119
Ville Syrjälä832be822016-01-12 21:08:33 +02002120unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2121 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002122{
Ville Syrjälä832be822016-01-12 21:08:33 +02002123 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2124 return 1;
2125 else
2126 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002127 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002128}
2129
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002130/* Return the tile dimensions in pixel units */
2131static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2132 unsigned int *tile_width,
2133 unsigned int *tile_height,
2134 uint64_t fb_modifier,
2135 unsigned int cpp)
2136{
2137 unsigned int tile_width_bytes =
2138 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2139
2140 *tile_width = tile_width_bytes / cpp;
2141 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2142}
2143
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002144unsigned int
2145intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002146 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002147{
Ville Syrjälä832be822016-01-12 21:08:33 +02002148 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2149 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2150
2151 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002152}
2153
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002154unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2155{
2156 unsigned int size = 0;
2157 int i;
2158
2159 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2160 size += rot_info->plane[i].width * rot_info->plane[i].height;
2161
2162 return size;
2163}
2164
Daniel Vetter75c82a52015-10-14 16:51:04 +02002165static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002166intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2167 const struct drm_framebuffer *fb,
2168 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002169{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002170 if (intel_rotation_90_or_270(rotation)) {
2171 *view = i915_ggtt_view_rotated;
2172 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2173 } else {
2174 *view = i915_ggtt_view_normal;
2175 }
2176}
2177
2178static void
2179intel_fill_fb_info(struct drm_i915_private *dev_priv,
2180 struct drm_framebuffer *fb)
2181{
2182 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002183 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002184
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002185 tile_size = intel_tile_size(dev_priv);
2186
2187 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002188 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2189 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002190
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002191 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2192 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002193
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002194 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002195 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002196 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2197 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002198
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002199 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002200 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2201 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002202 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002203}
2204
Ville Syrjälä603525d2016-01-12 21:08:37 +02002205static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002206{
2207 if (INTEL_INFO(dev_priv)->gen >= 9)
2208 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002209 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002210 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002211 return 128 * 1024;
2212 else if (INTEL_INFO(dev_priv)->gen >= 4)
2213 return 4 * 1024;
2214 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002215 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002216}
2217
Ville Syrjälä603525d2016-01-12 21:08:37 +02002218static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2219 uint64_t fb_modifier)
2220{
2221 switch (fb_modifier) {
2222 case DRM_FORMAT_MOD_NONE:
2223 return intel_linear_alignment(dev_priv);
2224 case I915_FORMAT_MOD_X_TILED:
2225 if (INTEL_INFO(dev_priv)->gen >= 9)
2226 return 256 * 1024;
2227 return 0;
2228 case I915_FORMAT_MOD_Y_TILED:
2229 case I915_FORMAT_MOD_Yf_TILED:
2230 return 1 * 1024 * 1024;
2231 default:
2232 MISSING_CASE(fb_modifier);
2233 return 0;
2234 }
2235}
2236
Chris Wilson127bd2a2010-07-23 23:32:05 +01002237int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002238intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2239 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002240{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002241 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002242 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002243 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002244 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002245 u32 alignment;
2246 int ret;
2247
Matt Roperebcdd392014-07-09 16:22:11 -07002248 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2249
Ville Syrjälä603525d2016-01-12 21:08:37 +02002250 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002251
Ville Syrjälä3465c582016-02-15 22:54:43 +02002252 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002253
Chris Wilson693db182013-03-05 14:52:39 +00002254 /* Note that the w/a also requires 64 PTE of padding following the
2255 * bo. We currently fill all unused PTE with the shadow page and so
2256 * we should always have valid PTE following the scanout preventing
2257 * the VT-d warning.
2258 */
2259 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2260 alignment = 256 * 1024;
2261
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002262 /*
2263 * Global gtt pte registers are special registers which actually forward
2264 * writes to a chunk of system memory. Which means that there is no risk
2265 * that the register values disappear as soon as we call
2266 * intel_runtime_pm_put(), so it is correct to wrap only the
2267 * pin/unpin/fence and not more.
2268 */
2269 intel_runtime_pm_get(dev_priv);
2270
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002271 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2272 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002273 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002274 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002275
2276 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2277 * fence, whereas 965+ only requires a fence if using
2278 * framebuffer compression. For simplicity, we always install
2279 * a fence as the cost is not that onerous.
2280 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002281 if (view.type == I915_GGTT_VIEW_NORMAL) {
2282 ret = i915_gem_object_get_fence(obj);
2283 if (ret == -EDEADLK) {
2284 /*
2285 * -EDEADLK means there are no free fences
2286 * no pending flips.
2287 *
2288 * This is propagated to atomic, but it uses
2289 * -EDEADLK to force a locking recovery, so
2290 * change the returned error to -EBUSY.
2291 */
2292 ret = -EBUSY;
2293 goto err_unpin;
2294 } else if (ret)
2295 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002296
Vivek Kasireddy98072162015-10-29 18:54:38 -07002297 i915_gem_object_pin_fence(obj);
2298 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002299
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002300 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002301 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002302
2303err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002304 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002305err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002306 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002307 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002308}
2309
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +01002310void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002311{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002312 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002313 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002314
Matt Roperebcdd392014-07-09 16:22:11 -07002315 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2316
Ville Syrjälä3465c582016-02-15 22:54:43 +02002317 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002318
Vivek Kasireddy98072162015-10-29 18:54:38 -07002319 if (view.type == I915_GGTT_VIEW_NORMAL)
2320 i915_gem_object_unpin_fence(obj);
2321
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002322 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002323}
2324
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002325/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002326 * Adjust the tile offset by moving the difference into
2327 * the x/y offsets.
2328 *
2329 * Input tile dimensions and pitch must already be
2330 * rotated to match x and y, and in pixel units.
2331 */
2332static u32 intel_adjust_tile_offset(int *x, int *y,
2333 unsigned int tile_width,
2334 unsigned int tile_height,
2335 unsigned int tile_size,
2336 unsigned int pitch_tiles,
2337 u32 old_offset,
2338 u32 new_offset)
2339{
2340 unsigned int tiles;
2341
2342 WARN_ON(old_offset & (tile_size - 1));
2343 WARN_ON(new_offset & (tile_size - 1));
2344 WARN_ON(new_offset > old_offset);
2345
2346 tiles = (old_offset - new_offset) / tile_size;
2347
2348 *y += tiles / pitch_tiles * tile_height;
2349 *x += tiles % pitch_tiles * tile_width;
2350
2351 return new_offset;
2352}
2353
2354/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002355 * Computes the linear offset to the base tile and adjusts
2356 * x, y. bytes per pixel is assumed to be a power-of-two.
2357 *
2358 * In the 90/270 rotated case, x and y are assumed
2359 * to be already rotated to match the rotated GTT view, and
2360 * pitch is the tile_height aligned framebuffer height.
2361 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002362u32 intel_compute_tile_offset(int *x, int *y,
2363 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002364 unsigned int pitch,
2365 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002366{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002367 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2368 uint64_t fb_modifier = fb->modifier[plane];
2369 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002370 u32 offset, offset_aligned, alignment;
2371
2372 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2373 if (alignment)
2374 alignment--;
2375
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002376 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002377 unsigned int tile_size, tile_width, tile_height;
2378 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002379
Ville Syrjäläd8433102016-01-12 21:08:35 +02002380 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002381 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2382 fb_modifier, cpp);
2383
2384 if (intel_rotation_90_or_270(rotation)) {
2385 pitch_tiles = pitch / tile_height;
2386 swap(tile_width, tile_height);
2387 } else {
2388 pitch_tiles = pitch / (tile_width * cpp);
2389 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002390
Ville Syrjäläd8433102016-01-12 21:08:35 +02002391 tile_rows = *y / tile_height;
2392 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002393
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002394 tiles = *x / tile_width;
2395 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002396
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002397 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2398 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002399
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002400 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2401 tile_size, pitch_tiles,
2402 offset, offset_aligned);
2403 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002404 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002405 offset_aligned = offset & ~alignment;
2406
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002407 *y = (offset & alignment) / pitch;
2408 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002409 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002410
2411 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002412}
2413
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002414static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002415{
2416 switch (format) {
2417 case DISPPLANE_8BPP:
2418 return DRM_FORMAT_C8;
2419 case DISPPLANE_BGRX555:
2420 return DRM_FORMAT_XRGB1555;
2421 case DISPPLANE_BGRX565:
2422 return DRM_FORMAT_RGB565;
2423 default:
2424 case DISPPLANE_BGRX888:
2425 return DRM_FORMAT_XRGB8888;
2426 case DISPPLANE_RGBX888:
2427 return DRM_FORMAT_XBGR8888;
2428 case DISPPLANE_BGRX101010:
2429 return DRM_FORMAT_XRGB2101010;
2430 case DISPPLANE_RGBX101010:
2431 return DRM_FORMAT_XBGR2101010;
2432 }
2433}
2434
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002435static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2436{
2437 switch (format) {
2438 case PLANE_CTL_FORMAT_RGB_565:
2439 return DRM_FORMAT_RGB565;
2440 default:
2441 case PLANE_CTL_FORMAT_XRGB_8888:
2442 if (rgb_order) {
2443 if (alpha)
2444 return DRM_FORMAT_ABGR8888;
2445 else
2446 return DRM_FORMAT_XBGR8888;
2447 } else {
2448 if (alpha)
2449 return DRM_FORMAT_ARGB8888;
2450 else
2451 return DRM_FORMAT_XRGB8888;
2452 }
2453 case PLANE_CTL_FORMAT_XRGB_2101010:
2454 if (rgb_order)
2455 return DRM_FORMAT_XBGR2101010;
2456 else
2457 return DRM_FORMAT_XRGB2101010;
2458 }
2459}
2460
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002461static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002462intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2463 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002464{
2465 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002466 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002467 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002468 struct drm_i915_gem_object *obj = NULL;
2469 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002470 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002471 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2472 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2473 PAGE_SIZE);
2474
2475 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002476
Chris Wilsonff2652e2014-03-10 08:07:02 +00002477 if (plane_config->size == 0)
2478 return false;
2479
Paulo Zanoni3badb492015-09-23 12:52:23 -03002480 /* If the FB is too big, just don't use it since fbdev is not very
2481 * important and we should probably use that space with FBC or other
2482 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002483 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002484 return false;
2485
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002486 mutex_lock(&dev->struct_mutex);
2487
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002488 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2489 base_aligned,
2490 base_aligned,
2491 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002492 if (!obj) {
2493 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002494 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002495 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002496
Damien Lespiau49af4492015-01-20 12:51:44 +00002497 obj->tiling_mode = plane_config->tiling;
2498 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002499 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002500
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002501 mode_cmd.pixel_format = fb->pixel_format;
2502 mode_cmd.width = fb->width;
2503 mode_cmd.height = fb->height;
2504 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002505 mode_cmd.modifier[0] = fb->modifier[0];
2506 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002507
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002508 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002509 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002510 DRM_DEBUG_KMS("intel fb init failed\n");
2511 goto out_unref_obj;
2512 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002513
Jesse Barnes46f297f2014-03-07 08:57:48 -08002514 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002515
Daniel Vetterf6936e22015-03-26 12:17:05 +01002516 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002517 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002518
2519out_unref_obj:
2520 drm_gem_object_unreference(&obj->base);
2521 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002522 return false;
2523}
2524
Matt Roperafd65eb2015-02-03 13:10:04 -08002525/* Update plane->state->fb to match plane->fb after driver-internal updates */
2526static void
2527update_state_fb(struct drm_plane *plane)
2528{
2529 if (plane->fb == plane->state->fb)
2530 return;
2531
2532 if (plane->state->fb)
2533 drm_framebuffer_unreference(plane->state->fb);
2534 plane->state->fb = plane->fb;
2535 if (plane->state->fb)
2536 drm_framebuffer_reference(plane->state->fb);
2537}
2538
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002539static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002540intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2541 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002542{
2543 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002544 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002545 struct drm_crtc *c;
2546 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002547 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002548 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002549 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002550 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2551 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002552 struct intel_plane_state *intel_state =
2553 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002554 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002555
Damien Lespiau2d140302015-02-05 17:22:18 +00002556 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002557 return;
2558
Daniel Vetterf6936e22015-03-26 12:17:05 +01002559 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002560 fb = &plane_config->fb->base;
2561 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002562 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002563
Damien Lespiau2d140302015-02-05 17:22:18 +00002564 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002565
2566 /*
2567 * Failed to alloc the obj, check to see if we should share
2568 * an fb with another CRTC instead
2569 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002570 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002571 i = to_intel_crtc(c);
2572
2573 if (c == &intel_crtc->base)
2574 continue;
2575
Matt Roper2ff8fde2014-07-08 07:50:07 -07002576 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002577 continue;
2578
Daniel Vetter88595ac2015-03-26 12:42:24 +01002579 fb = c->primary->fb;
2580 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002581 continue;
2582
Daniel Vetter88595ac2015-03-26 12:42:24 +01002583 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002584 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002585 drm_framebuffer_reference(fb);
2586 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002587 }
2588 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002589
Matt Roper200757f2015-12-03 11:37:36 -08002590 /*
2591 * We've failed to reconstruct the BIOS FB. Current display state
2592 * indicates that the primary plane is visible, but has a NULL FB,
2593 * which will lead to problems later if we don't fix it up. The
2594 * simplest solution is to just disable the primary plane now and
2595 * pretend the BIOS never had it enabled.
2596 */
2597 to_intel_plane_state(plane_state)->visible = false;
2598 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002599 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002600 intel_plane->disable_plane(primary, &intel_crtc->base);
2601
Daniel Vetter88595ac2015-03-26 12:42:24 +01002602 return;
2603
2604valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002605 plane_state->src_x = 0;
2606 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002607 plane_state->src_w = fb->width << 16;
2608 plane_state->src_h = fb->height << 16;
2609
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002610 plane_state->crtc_x = 0;
2611 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002612 plane_state->crtc_w = fb->width;
2613 plane_state->crtc_h = fb->height;
2614
Matt Roper0a8d8a82015-12-03 11:37:38 -08002615 intel_state->src.x1 = plane_state->src_x;
2616 intel_state->src.y1 = plane_state->src_y;
2617 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2618 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2619 intel_state->dst.x1 = plane_state->crtc_x;
2620 intel_state->dst.y1 = plane_state->crtc_y;
2621 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2622 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2623
Daniel Vetter88595ac2015-03-26 12:42:24 +01002624 obj = intel_fb_obj(fb);
2625 if (obj->tiling_mode != I915_TILING_NONE)
2626 dev_priv->preserve_bios_swizzle = true;
2627
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002628 drm_framebuffer_reference(fb);
2629 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002630 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002631 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002632 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002633}
2634
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002635static void i9xx_update_primary_plane(struct drm_plane *primary,
2636 const struct intel_crtc_state *crtc_state,
2637 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002638{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002639 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002640 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002641 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2642 struct drm_framebuffer *fb = plane_state->base.fb;
2643 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002644 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002645 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002646 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002647 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002648 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002649 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002650 int x = plane_state->src.x1 >> 16;
2651 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002652
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002653 dspcntr = DISPPLANE_GAMMA_ENABLE;
2654
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002655 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002656
2657 if (INTEL_INFO(dev)->gen < 4) {
2658 if (intel_crtc->pipe == PIPE_B)
2659 dspcntr |= DISPPLANE_SEL_PIPE_B;
2660
2661 /* pipesrc and dspsize control the size that is scaled from,
2662 * which should always be the user's requested size.
2663 */
2664 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002665 ((crtc_state->pipe_src_h - 1) << 16) |
2666 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002667 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002668 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2669 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002670 ((crtc_state->pipe_src_h - 1) << 16) |
2671 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002672 I915_WRITE(PRIMPOS(plane), 0);
2673 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002674 }
2675
Ville Syrjälä57779d02012-10-31 17:50:14 +02002676 switch (fb->pixel_format) {
2677 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002678 dspcntr |= DISPPLANE_8BPP;
2679 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002680 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002681 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002682 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002683 case DRM_FORMAT_RGB565:
2684 dspcntr |= DISPPLANE_BGRX565;
2685 break;
2686 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002687 dspcntr |= DISPPLANE_BGRX888;
2688 break;
2689 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002690 dspcntr |= DISPPLANE_RGBX888;
2691 break;
2692 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002693 dspcntr |= DISPPLANE_BGRX101010;
2694 break;
2695 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002696 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002697 break;
2698 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002699 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002700 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002701
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002702 if (INTEL_INFO(dev)->gen >= 4 &&
2703 obj->tiling_mode != I915_TILING_NONE)
2704 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002705
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002706 if (IS_G4X(dev))
2707 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2708
Ville Syrjäläac484962016-01-20 21:05:26 +02002709 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002710
Daniel Vetterc2c75132012-07-05 12:17:30 +02002711 if (INTEL_INFO(dev)->gen >= 4) {
2712 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002713 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002714 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002715 linear_offset -= intel_crtc->dspaddr_offset;
2716 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002717 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002718 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002719
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002720 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302721 dspcntr |= DISPPLANE_ROTATE_180;
2722
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002723 x += (crtc_state->pipe_src_w - 1);
2724 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302725
2726 /* Finding the last pixel of the last line of the display
2727 data and adding to linear_offset*/
2728 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002729 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002730 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302731 }
2732
Paulo Zanoni2db33662015-09-14 15:20:03 -03002733 intel_crtc->adjusted_x = x;
2734 intel_crtc->adjusted_y = y;
2735
Sonika Jindal48404c12014-08-22 14:06:04 +05302736 I915_WRITE(reg, dspcntr);
2737
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002738 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002739 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002740 I915_WRITE(DSPSURF(plane),
2741 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002742 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002743 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002744 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002745 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002746 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002747}
2748
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002749static void i9xx_disable_primary_plane(struct drm_plane *primary,
2750 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002751{
2752 struct drm_device *dev = crtc->dev;
2753 struct drm_i915_private *dev_priv = dev->dev_private;
2754 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002755 int plane = intel_crtc->plane;
2756
2757 I915_WRITE(DSPCNTR(plane), 0);
2758 if (INTEL_INFO(dev_priv)->gen >= 4)
2759 I915_WRITE(DSPSURF(plane), 0);
2760 else
2761 I915_WRITE(DSPADDR(plane), 0);
2762 POSTING_READ(DSPCNTR(plane));
2763}
2764
2765static void ironlake_update_primary_plane(struct drm_plane *primary,
2766 const struct intel_crtc_state *crtc_state,
2767 const struct intel_plane_state *plane_state)
2768{
2769 struct drm_device *dev = primary->dev;
2770 struct drm_i915_private *dev_priv = dev->dev_private;
2771 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2772 struct drm_framebuffer *fb = plane_state->base.fb;
2773 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002774 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002775 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002776 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002777 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002778 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002779 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002780 int x = plane_state->src.x1 >> 16;
2781 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002782
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002783 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002784 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002785
2786 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2787 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2788
Ville Syrjälä57779d02012-10-31 17:50:14 +02002789 switch (fb->pixel_format) {
2790 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002791 dspcntr |= DISPPLANE_8BPP;
2792 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002793 case DRM_FORMAT_RGB565:
2794 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002795 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002796 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002797 dspcntr |= DISPPLANE_BGRX888;
2798 break;
2799 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002800 dspcntr |= DISPPLANE_RGBX888;
2801 break;
2802 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002803 dspcntr |= DISPPLANE_BGRX101010;
2804 break;
2805 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002806 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002807 break;
2808 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002809 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002810 }
2811
2812 if (obj->tiling_mode != I915_TILING_NONE)
2813 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002814
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002815 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002816 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002817
Ville Syrjäläac484962016-01-20 21:05:26 +02002818 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002819 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002820 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002821 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002822 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002823 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302824 dspcntr |= DISPPLANE_ROTATE_180;
2825
2826 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002827 x += (crtc_state->pipe_src_w - 1);
2828 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302829
2830 /* Finding the last pixel of the last line of the display
2831 data and adding to linear_offset*/
2832 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002833 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002834 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302835 }
2836 }
2837
Paulo Zanoni2db33662015-09-14 15:20:03 -03002838 intel_crtc->adjusted_x = x;
2839 intel_crtc->adjusted_y = y;
2840
Sonika Jindal48404c12014-08-22 14:06:04 +05302841 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002842
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002843 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002844 I915_WRITE(DSPSURF(plane),
2845 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002846 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002847 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2848 } else {
2849 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2850 I915_WRITE(DSPLINOFF(plane), linear_offset);
2851 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002852 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002853}
2854
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002855u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2856 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002857{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002858 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2859 return 64;
2860 } else {
2861 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002862
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002863 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002864 }
2865}
2866
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002867u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2868 struct drm_i915_gem_object *obj,
2869 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002870{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002871 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002872 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002873 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002874
Ville Syrjäläe7941292016-01-19 18:23:17 +02002875 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002876 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002877
Daniel Vetterce7f1722015-10-14 16:51:06 +02002878 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002879 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002880 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002881 return -1;
2882
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002883 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002884
2885 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002886 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002887 PAGE_SIZE;
2888 }
2889
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002890 WARN_ON(upper_32_bits(offset));
2891
2892 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002893}
2894
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002895static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2896{
2897 struct drm_device *dev = intel_crtc->base.dev;
2898 struct drm_i915_private *dev_priv = dev->dev_private;
2899
2900 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2901 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2902 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002903}
2904
Chandra Kondurua1b22782015-04-07 15:28:45 -07002905/*
2906 * This function detaches (aka. unbinds) unused scalers in hardware
2907 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002908static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002909{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002910 struct intel_crtc_scaler_state *scaler_state;
2911 int i;
2912
Chandra Kondurua1b22782015-04-07 15:28:45 -07002913 scaler_state = &intel_crtc->config->scaler_state;
2914
2915 /* loop through and disable scalers that aren't in use */
2916 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002917 if (!scaler_state->scalers[i].in_use)
2918 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002919 }
2920}
2921
Chandra Konduru6156a452015-04-27 13:48:39 -07002922u32 skl_plane_ctl_format(uint32_t pixel_format)
2923{
Chandra Konduru6156a452015-04-27 13:48:39 -07002924 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002925 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002926 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002927 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002928 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002929 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002930 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002931 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002932 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002933 /*
2934 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2935 * to be already pre-multiplied. We need to add a knob (or a different
2936 * DRM_FORMAT) for user-space to configure that.
2937 */
2938 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002939 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002940 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002941 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002942 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002943 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002944 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002945 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002946 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002947 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002948 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002949 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002951 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002952 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002953 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002955 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002956 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002957 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002959
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002960 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002961}
2962
2963u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2964{
Chandra Konduru6156a452015-04-27 13:48:39 -07002965 switch (fb_modifier) {
2966 case DRM_FORMAT_MOD_NONE:
2967 break;
2968 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002969 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002970 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002971 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002972 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002973 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002974 default:
2975 MISSING_CASE(fb_modifier);
2976 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002977
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002978 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002979}
2980
2981u32 skl_plane_ctl_rotation(unsigned int rotation)
2982{
Chandra Konduru6156a452015-04-27 13:48:39 -07002983 switch (rotation) {
2984 case BIT(DRM_ROTATE_0):
2985 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302986 /*
2987 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2988 * while i915 HW rotation is clockwise, thats why this swapping.
2989 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002990 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302991 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002992 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002993 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002994 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302995 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002996 default:
2997 MISSING_CASE(rotation);
2998 }
2999
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003000 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003001}
3002
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003003static void skylake_update_primary_plane(struct drm_plane *plane,
3004 const struct intel_crtc_state *crtc_state,
3005 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003006{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003007 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003008 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003009 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3010 struct drm_framebuffer *fb = plane_state->base.fb;
3011 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003012 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303013 u32 plane_ctl, stride_div, stride;
3014 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003015 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303016 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003017 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003018 int scaler_id = plane_state->scaler_id;
3019 int src_x = plane_state->src.x1 >> 16;
3020 int src_y = plane_state->src.y1 >> 16;
3021 int src_w = drm_rect_width(&plane_state->src) >> 16;
3022 int src_h = drm_rect_height(&plane_state->src) >> 16;
3023 int dst_x = plane_state->dst.x1;
3024 int dst_y = plane_state->dst.y1;
3025 int dst_w = drm_rect_width(&plane_state->dst);
3026 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003027
3028 plane_ctl = PLANE_CTL_ENABLE |
3029 PLANE_CTL_PIPE_GAMMA_ENABLE |
3030 PLANE_CTL_PIPE_CSC_ENABLE;
3031
Chandra Konduru6156a452015-04-27 13:48:39 -07003032 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3033 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003034 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003035 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003036
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003037 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003038 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003039 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303040
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003041 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003042
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303043 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003044 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3045
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303046 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003047 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303048 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003049 x_offset = stride * tile_height - src_y - src_h;
3050 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003051 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303052 } else {
3053 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003054 x_offset = src_x;
3055 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003056 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303057 }
3058 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003059
Paulo Zanoni2db33662015-09-14 15:20:03 -03003060 intel_crtc->adjusted_x = x_offset;
3061 intel_crtc->adjusted_y = y_offset;
3062
Damien Lespiau70d21f02013-07-03 21:06:04 +01003063 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303064 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3065 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3066 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003067
3068 if (scaler_id >= 0) {
3069 uint32_t ps_ctrl = 0;
3070
3071 WARN_ON(!dst_w || !dst_h);
3072 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3073 crtc_state->scaler_state.scalers[scaler_id].mode;
3074 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3075 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3076 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3077 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3078 I915_WRITE(PLANE_POS(pipe, 0), 0);
3079 } else {
3080 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3081 }
3082
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003083 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003084
3085 POSTING_READ(PLANE_SURF(pipe, 0));
3086}
3087
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003088static void skylake_disable_primary_plane(struct drm_plane *primary,
3089 struct drm_crtc *crtc)
3090{
3091 struct drm_device *dev = crtc->dev;
3092 struct drm_i915_private *dev_priv = dev->dev_private;
3093 int pipe = to_intel_crtc(crtc)->pipe;
3094
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003095 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3096 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3097 POSTING_READ(PLANE_SURF(pipe, 0));
3098}
3099
Jesse Barnes17638cd2011-06-24 12:19:23 -07003100/* Assume fb object is pinned & idle & fenced and just update base pointers */
3101static int
3102intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3103 int x, int y, enum mode_set_atomic state)
3104{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003105 /* Support for kgdboc is disabled, this needs a major rework. */
3106 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003107
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003108 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003109}
3110
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01003111static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003112{
Maarten Lankhorstef583192016-05-17 15:07:46 +02003113 struct intel_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003114
Maarten Lankhorst5251f042016-05-17 15:07:47 +02003115 for_each_intel_crtc(dev_priv->dev, crtc)
Maarten Lankhorstef583192016-05-17 15:07:46 +02003116 intel_finish_page_flip(dev_priv, crtc->pipe);
Ville Syrjälä75147472014-11-24 18:28:11 +02003117}
3118
3119static void intel_update_primary_planes(struct drm_device *dev)
3120{
Ville Syrjälä75147472014-11-24 18:28:11 +02003121 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003122
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003123 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003124 struct intel_plane *plane = to_intel_plane(crtc->primary);
3125 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003126
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003127 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003128 plane_state = to_intel_plane_state(plane->base.state);
3129
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003130 if (plane_state->visible)
3131 plane->update_plane(&plane->base,
3132 to_intel_crtc_state(crtc->state),
3133 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003134
3135 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003136 }
3137}
3138
Chris Wilsonc0336662016-05-06 15:40:21 +01003139void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003140{
3141 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003142 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003143 return;
3144
3145 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003146 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003147 return;
3148
Chris Wilsonc0336662016-05-06 15:40:21 +01003149 drm_modeset_lock_all(dev_priv->dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003150 /*
3151 * Disabling the crtcs gracefully seems nicer. Also the
3152 * g33 docs say we should at least disable all the planes.
3153 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003154 intel_display_suspend(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003155}
3156
Chris Wilsonc0336662016-05-06 15:40:21 +01003157void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003158{
Ville Syrjälä75147472014-11-24 18:28:11 +02003159 /*
3160 * Flips in the rings will be nuked by the reset,
3161 * so complete all pending flips so that user space
3162 * will get its events and not get stuck.
3163 */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01003164 intel_complete_page_flips(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003165
3166 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003167 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003168 return;
3169
3170 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003171 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
Ville Syrjälä75147472014-11-24 18:28:11 +02003172 /*
3173 * Flips in the rings have been nuked by the reset,
3174 * so update the base address of all primary
3175 * planes to the the last fb to make sure we're
3176 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003177 *
3178 * FIXME: Atomic will make this obsolete since we won't schedule
3179 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003180 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003181 intel_update_primary_planes(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003182 return;
3183 }
3184
3185 /*
3186 * The display has been reset as well,
3187 * so need a full re-initialization.
3188 */
3189 intel_runtime_pm_disable_interrupts(dev_priv);
3190 intel_runtime_pm_enable_interrupts(dev_priv);
3191
Chris Wilsonc0336662016-05-06 15:40:21 +01003192 intel_modeset_init_hw(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003193
3194 spin_lock_irq(&dev_priv->irq_lock);
3195 if (dev_priv->display.hpd_irq_setup)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01003196 dev_priv->display.hpd_irq_setup(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003197 spin_unlock_irq(&dev_priv->irq_lock);
3198
Chris Wilsonc0336662016-05-06 15:40:21 +01003199 intel_display_resume(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003200
3201 intel_hpd_init(dev_priv);
3202
Chris Wilsonc0336662016-05-06 15:40:21 +01003203 drm_modeset_unlock_all(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003204}
3205
Chris Wilson7d5e3792014-03-04 13:15:08 +00003206static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3207{
3208 struct drm_device *dev = crtc->dev;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003209 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonc19ae982016-04-13 17:35:03 +01003210 unsigned reset_counter;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003211 bool pending;
3212
Chris Wilson7f1847e2016-04-13 17:35:04 +01003213 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3214 if (intel_crtc->reset_counter != reset_counter)
Chris Wilson7d5e3792014-03-04 13:15:08 +00003215 return false;
3216
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003217 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003218 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003219 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003220
3221 return pending;
3222}
3223
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003224static void intel_update_pipe_config(struct intel_crtc *crtc,
3225 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003226{
3227 struct drm_device *dev = crtc->base.dev;
3228 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003229 struct intel_crtc_state *pipe_config =
3230 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003231
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003232 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3233 crtc->base.mode = crtc->base.state->mode;
3234
3235 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3236 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3237 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003238
3239 /*
3240 * Update pipe size and adjust fitter if needed: the reason for this is
3241 * that in compute_mode_changes we check the native mode (not the pfit
3242 * mode) to see if we can flip rather than do a full mode set. In the
3243 * fastboot case, we'll flip, but if we don't update the pipesrc and
3244 * pfit state, we'll end up with a big fb scanned out into the wrong
3245 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003246 */
3247
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003248 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003249 ((pipe_config->pipe_src_w - 1) << 16) |
3250 (pipe_config->pipe_src_h - 1));
3251
3252 /* on skylake this is done by detaching scalers */
3253 if (INTEL_INFO(dev)->gen >= 9) {
3254 skl_detach_scalers(crtc);
3255
3256 if (pipe_config->pch_pfit.enabled)
3257 skylake_pfit_enable(crtc);
3258 } else if (HAS_PCH_SPLIT(dev)) {
3259 if (pipe_config->pch_pfit.enabled)
3260 ironlake_pfit_enable(crtc);
3261 else if (old_crtc_state->pch_pfit.enabled)
3262 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003263 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003264}
3265
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003266static void intel_fdi_normal_train(struct drm_crtc *crtc)
3267{
3268 struct drm_device *dev = crtc->dev;
3269 struct drm_i915_private *dev_priv = dev->dev_private;
3270 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3271 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003272 i915_reg_t reg;
3273 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003274
3275 /* enable normal train */
3276 reg = FDI_TX_CTL(pipe);
3277 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003278 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003279 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3280 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003281 } else {
3282 temp &= ~FDI_LINK_TRAIN_NONE;
3283 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003284 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003285 I915_WRITE(reg, temp);
3286
3287 reg = FDI_RX_CTL(pipe);
3288 temp = I915_READ(reg);
3289 if (HAS_PCH_CPT(dev)) {
3290 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3291 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3292 } else {
3293 temp &= ~FDI_LINK_TRAIN_NONE;
3294 temp |= FDI_LINK_TRAIN_NONE;
3295 }
3296 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3297
3298 /* wait one idle pattern time */
3299 POSTING_READ(reg);
3300 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003301
3302 /* IVB wants error correction enabled */
3303 if (IS_IVYBRIDGE(dev))
3304 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3305 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003306}
3307
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003308/* The FDI link training functions for ILK/Ibexpeak. */
3309static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3310{
3311 struct drm_device *dev = crtc->dev;
3312 struct drm_i915_private *dev_priv = dev->dev_private;
3313 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3314 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003315 i915_reg_t reg;
3316 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003317
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003318 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003319 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003320
Adam Jacksone1a44742010-06-25 15:32:14 -04003321 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3322 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003323 reg = FDI_RX_IMR(pipe);
3324 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003325 temp &= ~FDI_RX_SYMBOL_LOCK;
3326 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003327 I915_WRITE(reg, temp);
3328 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003329 udelay(150);
3330
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003331 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003332 reg = FDI_TX_CTL(pipe);
3333 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003334 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003335 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003336 temp &= ~FDI_LINK_TRAIN_NONE;
3337 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003338 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003339
Chris Wilson5eddb702010-09-11 13:48:45 +01003340 reg = FDI_RX_CTL(pipe);
3341 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003342 temp &= ~FDI_LINK_TRAIN_NONE;
3343 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003344 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3345
3346 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003347 udelay(150);
3348
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003349 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003350 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3351 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3352 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003353
Chris Wilson5eddb702010-09-11 13:48:45 +01003354 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003355 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003356 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003357 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3358
3359 if ((temp & FDI_RX_BIT_LOCK)) {
3360 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003361 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003362 break;
3363 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003364 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003365 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003366 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003367
3368 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003369 reg = FDI_TX_CTL(pipe);
3370 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003371 temp &= ~FDI_LINK_TRAIN_NONE;
3372 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003373 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003374
Chris Wilson5eddb702010-09-11 13:48:45 +01003375 reg = FDI_RX_CTL(pipe);
3376 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003377 temp &= ~FDI_LINK_TRAIN_NONE;
3378 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003379 I915_WRITE(reg, temp);
3380
3381 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003382 udelay(150);
3383
Chris Wilson5eddb702010-09-11 13:48:45 +01003384 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003385 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003386 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003387 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3388
3389 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003390 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003391 DRM_DEBUG_KMS("FDI train 2 done.\n");
3392 break;
3393 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003394 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003395 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003396 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003397
3398 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003399
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003400}
3401
Akshay Joshi0206e352011-08-16 15:34:10 -04003402static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003403 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3404 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3405 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3406 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3407};
3408
3409/* The FDI link training functions for SNB/Cougarpoint. */
3410static void gen6_fdi_link_train(struct drm_crtc *crtc)
3411{
3412 struct drm_device *dev = crtc->dev;
3413 struct drm_i915_private *dev_priv = dev->dev_private;
3414 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3415 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003416 i915_reg_t reg;
3417 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003418
Adam Jacksone1a44742010-06-25 15:32:14 -04003419 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3420 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003421 reg = FDI_RX_IMR(pipe);
3422 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003423 temp &= ~FDI_RX_SYMBOL_LOCK;
3424 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003425 I915_WRITE(reg, temp);
3426
3427 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003428 udelay(150);
3429
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003430 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003431 reg = FDI_TX_CTL(pipe);
3432 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003433 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003434 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003435 temp &= ~FDI_LINK_TRAIN_NONE;
3436 temp |= FDI_LINK_TRAIN_PATTERN_1;
3437 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3438 /* SNB-B */
3439 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003440 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003441
Daniel Vetterd74cf322012-10-26 10:58:13 +02003442 I915_WRITE(FDI_RX_MISC(pipe),
3443 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3444
Chris Wilson5eddb702010-09-11 13:48:45 +01003445 reg = FDI_RX_CTL(pipe);
3446 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003447 if (HAS_PCH_CPT(dev)) {
3448 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3449 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3450 } else {
3451 temp &= ~FDI_LINK_TRAIN_NONE;
3452 temp |= FDI_LINK_TRAIN_PATTERN_1;
3453 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003454 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3455
3456 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003457 udelay(150);
3458
Akshay Joshi0206e352011-08-16 15:34:10 -04003459 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003460 reg = FDI_TX_CTL(pipe);
3461 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003462 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3463 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003464 I915_WRITE(reg, temp);
3465
3466 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003467 udelay(500);
3468
Sean Paulfa37d392012-03-02 12:53:39 -05003469 for (retry = 0; retry < 5; retry++) {
3470 reg = FDI_RX_IIR(pipe);
3471 temp = I915_READ(reg);
3472 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3473 if (temp & FDI_RX_BIT_LOCK) {
3474 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3475 DRM_DEBUG_KMS("FDI train 1 done.\n");
3476 break;
3477 }
3478 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003479 }
Sean Paulfa37d392012-03-02 12:53:39 -05003480 if (retry < 5)
3481 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003482 }
3483 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003484 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003485
3486 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003487 reg = FDI_TX_CTL(pipe);
3488 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003489 temp &= ~FDI_LINK_TRAIN_NONE;
3490 temp |= FDI_LINK_TRAIN_PATTERN_2;
3491 if (IS_GEN6(dev)) {
3492 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3493 /* SNB-B */
3494 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3495 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003496 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003497
Chris Wilson5eddb702010-09-11 13:48:45 +01003498 reg = FDI_RX_CTL(pipe);
3499 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003500 if (HAS_PCH_CPT(dev)) {
3501 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3502 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3503 } else {
3504 temp &= ~FDI_LINK_TRAIN_NONE;
3505 temp |= FDI_LINK_TRAIN_PATTERN_2;
3506 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003507 I915_WRITE(reg, temp);
3508
3509 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003510 udelay(150);
3511
Akshay Joshi0206e352011-08-16 15:34:10 -04003512 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003513 reg = FDI_TX_CTL(pipe);
3514 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003515 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3516 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003517 I915_WRITE(reg, temp);
3518
3519 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003520 udelay(500);
3521
Sean Paulfa37d392012-03-02 12:53:39 -05003522 for (retry = 0; retry < 5; retry++) {
3523 reg = FDI_RX_IIR(pipe);
3524 temp = I915_READ(reg);
3525 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3526 if (temp & FDI_RX_SYMBOL_LOCK) {
3527 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3528 DRM_DEBUG_KMS("FDI train 2 done.\n");
3529 break;
3530 }
3531 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003532 }
Sean Paulfa37d392012-03-02 12:53:39 -05003533 if (retry < 5)
3534 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003535 }
3536 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003537 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003538
3539 DRM_DEBUG_KMS("FDI train done.\n");
3540}
3541
Jesse Barnes357555c2011-04-28 15:09:55 -07003542/* Manual link training for Ivy Bridge A0 parts */
3543static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3544{
3545 struct drm_device *dev = crtc->dev;
3546 struct drm_i915_private *dev_priv = dev->dev_private;
3547 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3548 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003549 i915_reg_t reg;
3550 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003551
3552 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3553 for train result */
3554 reg = FDI_RX_IMR(pipe);
3555 temp = I915_READ(reg);
3556 temp &= ~FDI_RX_SYMBOL_LOCK;
3557 temp &= ~FDI_RX_BIT_LOCK;
3558 I915_WRITE(reg, temp);
3559
3560 POSTING_READ(reg);
3561 udelay(150);
3562
Daniel Vetter01a415f2012-10-27 15:58:40 +02003563 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3564 I915_READ(FDI_RX_IIR(pipe)));
3565
Jesse Barnes139ccd32013-08-19 11:04:55 -07003566 /* Try each vswing and preemphasis setting twice before moving on */
3567 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3568 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003569 reg = FDI_TX_CTL(pipe);
3570 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003571 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3572 temp &= ~FDI_TX_ENABLE;
3573 I915_WRITE(reg, temp);
3574
3575 reg = FDI_RX_CTL(pipe);
3576 temp = I915_READ(reg);
3577 temp &= ~FDI_LINK_TRAIN_AUTO;
3578 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3579 temp &= ~FDI_RX_ENABLE;
3580 I915_WRITE(reg, temp);
3581
3582 /* enable CPU FDI TX and PCH FDI RX */
3583 reg = FDI_TX_CTL(pipe);
3584 temp = I915_READ(reg);
3585 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003586 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003587 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003588 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003589 temp |= snb_b_fdi_train_param[j/2];
3590 temp |= FDI_COMPOSITE_SYNC;
3591 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3592
3593 I915_WRITE(FDI_RX_MISC(pipe),
3594 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3595
3596 reg = FDI_RX_CTL(pipe);
3597 temp = I915_READ(reg);
3598 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3599 temp |= FDI_COMPOSITE_SYNC;
3600 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3601
3602 POSTING_READ(reg);
3603 udelay(1); /* should be 0.5us */
3604
3605 for (i = 0; i < 4; i++) {
3606 reg = FDI_RX_IIR(pipe);
3607 temp = I915_READ(reg);
3608 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3609
3610 if (temp & FDI_RX_BIT_LOCK ||
3611 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3612 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3613 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3614 i);
3615 break;
3616 }
3617 udelay(1); /* should be 0.5us */
3618 }
3619 if (i == 4) {
3620 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3621 continue;
3622 }
3623
3624 /* Train 2 */
3625 reg = FDI_TX_CTL(pipe);
3626 temp = I915_READ(reg);
3627 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3628 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3629 I915_WRITE(reg, temp);
3630
3631 reg = FDI_RX_CTL(pipe);
3632 temp = I915_READ(reg);
3633 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3634 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003635 I915_WRITE(reg, temp);
3636
3637 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003638 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003639
Jesse Barnes139ccd32013-08-19 11:04:55 -07003640 for (i = 0; i < 4; i++) {
3641 reg = FDI_RX_IIR(pipe);
3642 temp = I915_READ(reg);
3643 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003644
Jesse Barnes139ccd32013-08-19 11:04:55 -07003645 if (temp & FDI_RX_SYMBOL_LOCK ||
3646 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3647 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3648 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3649 i);
3650 goto train_done;
3651 }
3652 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003653 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003654 if (i == 4)
3655 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003656 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003657
Jesse Barnes139ccd32013-08-19 11:04:55 -07003658train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003659 DRM_DEBUG_KMS("FDI train done.\n");
3660}
3661
Daniel Vetter88cefb62012-08-12 19:27:14 +02003662static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003663{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003664 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003665 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003666 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003667 i915_reg_t reg;
3668 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003669
Jesse Barnes0e23b992010-09-10 11:10:00 -07003670 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003671 reg = FDI_RX_CTL(pipe);
3672 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003673 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003674 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003675 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003676 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3677
3678 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003679 udelay(200);
3680
3681 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003682 temp = I915_READ(reg);
3683 I915_WRITE(reg, temp | FDI_PCDCLK);
3684
3685 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003686 udelay(200);
3687
Paulo Zanoni20749732012-11-23 15:30:38 -02003688 /* Enable CPU FDI TX PLL, always on for Ironlake */
3689 reg = FDI_TX_CTL(pipe);
3690 temp = I915_READ(reg);
3691 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3692 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003693
Paulo Zanoni20749732012-11-23 15:30:38 -02003694 POSTING_READ(reg);
3695 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003696 }
3697}
3698
Daniel Vetter88cefb62012-08-12 19:27:14 +02003699static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3700{
3701 struct drm_device *dev = intel_crtc->base.dev;
3702 struct drm_i915_private *dev_priv = dev->dev_private;
3703 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003704 i915_reg_t reg;
3705 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003706
3707 /* Switch from PCDclk to Rawclk */
3708 reg = FDI_RX_CTL(pipe);
3709 temp = I915_READ(reg);
3710 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3711
3712 /* Disable CPU FDI TX PLL */
3713 reg = FDI_TX_CTL(pipe);
3714 temp = I915_READ(reg);
3715 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3716
3717 POSTING_READ(reg);
3718 udelay(100);
3719
3720 reg = FDI_RX_CTL(pipe);
3721 temp = I915_READ(reg);
3722 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3723
3724 /* Wait for the clocks to turn off. */
3725 POSTING_READ(reg);
3726 udelay(100);
3727}
3728
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003729static void ironlake_fdi_disable(struct drm_crtc *crtc)
3730{
3731 struct drm_device *dev = crtc->dev;
3732 struct drm_i915_private *dev_priv = dev->dev_private;
3733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3734 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003735 i915_reg_t reg;
3736 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003737
3738 /* disable CPU FDI tx and PCH FDI rx */
3739 reg = FDI_TX_CTL(pipe);
3740 temp = I915_READ(reg);
3741 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3742 POSTING_READ(reg);
3743
3744 reg = FDI_RX_CTL(pipe);
3745 temp = I915_READ(reg);
3746 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003747 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003748 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3749
3750 POSTING_READ(reg);
3751 udelay(100);
3752
3753 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003754 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003755 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003756
3757 /* still set train pattern 1 */
3758 reg = FDI_TX_CTL(pipe);
3759 temp = I915_READ(reg);
3760 temp &= ~FDI_LINK_TRAIN_NONE;
3761 temp |= FDI_LINK_TRAIN_PATTERN_1;
3762 I915_WRITE(reg, temp);
3763
3764 reg = FDI_RX_CTL(pipe);
3765 temp = I915_READ(reg);
3766 if (HAS_PCH_CPT(dev)) {
3767 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3768 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3769 } else {
3770 temp &= ~FDI_LINK_TRAIN_NONE;
3771 temp |= FDI_LINK_TRAIN_PATTERN_1;
3772 }
3773 /* BPC in FDI rx is consistent with that in PIPECONF */
3774 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003775 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003776 I915_WRITE(reg, temp);
3777
3778 POSTING_READ(reg);
3779 udelay(100);
3780}
3781
Chris Wilson5dce5b932014-01-20 10:17:36 +00003782bool intel_has_pending_fb_unpin(struct drm_device *dev)
3783{
3784 struct intel_crtc *crtc;
3785
3786 /* Note that we don't need to be called with mode_config.lock here
3787 * as our list of CRTC objects is static for the lifetime of the
3788 * device and so cannot disappear as we iterate. Similarly, we can
3789 * happily treat the predicates as racy, atomic checks as userspace
3790 * cannot claim and pin a new fb without at least acquring the
3791 * struct_mutex and so serialising with us.
3792 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003793 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003794 if (atomic_read(&crtc->unpin_work_count) == 0)
3795 continue;
3796
3797 if (crtc->unpin_work)
3798 intel_wait_for_vblank(dev, crtc->pipe);
3799
3800 return true;
3801 }
3802
3803 return false;
3804}
3805
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003806static void page_flip_completed(struct intel_crtc *intel_crtc)
3807{
3808 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3809 struct intel_unpin_work *work = intel_crtc->unpin_work;
3810
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003811 intel_crtc->unpin_work = NULL;
3812
3813 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07003814 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003815
3816 drm_crtc_vblank_put(&intel_crtc->base);
3817
3818 wake_up_all(&dev_priv->pending_flip_queue);
3819 queue_work(dev_priv->wq, &work->work);
3820
3821 trace_i915_flip_complete(intel_crtc->plane,
3822 work->pending_flip_obj);
3823}
3824
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003825static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003826{
Chris Wilson0f911282012-04-17 10:05:38 +01003827 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003828 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003829 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003830
Daniel Vetter2c10d572012-12-20 21:24:07 +01003831 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003832
3833 ret = wait_event_interruptible_timeout(
3834 dev_priv->pending_flip_queue,
3835 !intel_crtc_has_pending_flip(crtc),
3836 60*HZ);
3837
3838 if (ret < 0)
3839 return ret;
3840
3841 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01003842 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003843
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003844 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003845 if (intel_crtc->unpin_work) {
3846 WARN_ONCE(1, "Removing stuck page flip\n");
3847 page_flip_completed(intel_crtc);
3848 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003849 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003850 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003851
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003852 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003853}
3854
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003855static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3856{
3857 u32 temp;
3858
3859 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3860
3861 mutex_lock(&dev_priv->sb_lock);
3862
3863 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3864 temp |= SBI_SSCCTL_DISABLE;
3865 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3866
3867 mutex_unlock(&dev_priv->sb_lock);
3868}
3869
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003870/* Program iCLKIP clock to the desired frequency */
3871static void lpt_program_iclkip(struct drm_crtc *crtc)
3872{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003873 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003874 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003875 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3876 u32 temp;
3877
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003878 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003879
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003880 /* The iCLK virtual clock root frequency is in MHz,
3881 * but the adjusted_mode->crtc_clock in in KHz. To get the
3882 * divisors, it is necessary to divide one by another, so we
3883 * convert the virtual clock precision to KHz here for higher
3884 * precision.
3885 */
3886 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003887 u32 iclk_virtual_root_freq = 172800 * 1000;
3888 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003889 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003890
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003891 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3892 clock << auxdiv);
3893 divsel = (desired_divisor / iclk_pi_range) - 2;
3894 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003895
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003896 /*
3897 * Near 20MHz is a corner case which is
3898 * out of range for the 7-bit divisor
3899 */
3900 if (divsel <= 0x7f)
3901 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003902 }
3903
3904 /* This should not happen with any sane values */
3905 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3906 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3907 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3908 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3909
3910 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 +03003911 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003912 auxdiv,
3913 divsel,
3914 phasedir,
3915 phaseinc);
3916
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003917 mutex_lock(&dev_priv->sb_lock);
3918
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003919 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003920 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003921 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3922 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3923 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3924 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3925 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3926 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003927 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003928
3929 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003930 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003931 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3932 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003933 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003934
3935 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003936 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003937 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003938 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003939
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003940 mutex_unlock(&dev_priv->sb_lock);
3941
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003942 /* Wait for initialization time */
3943 udelay(24);
3944
3945 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3946}
3947
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003948int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3949{
3950 u32 divsel, phaseinc, auxdiv;
3951 u32 iclk_virtual_root_freq = 172800 * 1000;
3952 u32 iclk_pi_range = 64;
3953 u32 desired_divisor;
3954 u32 temp;
3955
3956 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3957 return 0;
3958
3959 mutex_lock(&dev_priv->sb_lock);
3960
3961 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3962 if (temp & SBI_SSCCTL_DISABLE) {
3963 mutex_unlock(&dev_priv->sb_lock);
3964 return 0;
3965 }
3966
3967 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3968 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3969 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3970 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3971 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3972
3973 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3974 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3975 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3976
3977 mutex_unlock(&dev_priv->sb_lock);
3978
3979 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3980
3981 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3982 desired_divisor << auxdiv);
3983}
3984
Daniel Vetter275f01b22013-05-03 11:49:47 +02003985static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3986 enum pipe pch_transcoder)
3987{
3988 struct drm_device *dev = crtc->base.dev;
3989 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003990 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003991
3992 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3993 I915_READ(HTOTAL(cpu_transcoder)));
3994 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3995 I915_READ(HBLANK(cpu_transcoder)));
3996 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3997 I915_READ(HSYNC(cpu_transcoder)));
3998
3999 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4000 I915_READ(VTOTAL(cpu_transcoder)));
4001 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4002 I915_READ(VBLANK(cpu_transcoder)));
4003 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4004 I915_READ(VSYNC(cpu_transcoder)));
4005 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4006 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4007}
4008
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004009static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004010{
4011 struct drm_i915_private *dev_priv = dev->dev_private;
4012 uint32_t temp;
4013
4014 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004015 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004016 return;
4017
4018 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4019 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4020
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004021 temp &= ~FDI_BC_BIFURCATION_SELECT;
4022 if (enable)
4023 temp |= FDI_BC_BIFURCATION_SELECT;
4024
4025 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004026 I915_WRITE(SOUTH_CHICKEN1, temp);
4027 POSTING_READ(SOUTH_CHICKEN1);
4028}
4029
4030static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4031{
4032 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004033
4034 switch (intel_crtc->pipe) {
4035 case PIPE_A:
4036 break;
4037 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004038 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004039 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004040 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004041 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004042
4043 break;
4044 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004045 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004046
4047 break;
4048 default:
4049 BUG();
4050 }
4051}
4052
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004053/* Return which DP Port should be selected for Transcoder DP control */
4054static enum port
4055intel_trans_dp_port_sel(struct drm_crtc *crtc)
4056{
4057 struct drm_device *dev = crtc->dev;
4058 struct intel_encoder *encoder;
4059
4060 for_each_encoder_on_crtc(dev, crtc, encoder) {
4061 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4062 encoder->type == INTEL_OUTPUT_EDP)
4063 return enc_to_dig_port(&encoder->base)->port;
4064 }
4065
4066 return -1;
4067}
4068
Jesse Barnesf67a5592011-01-05 10:31:48 -08004069/*
4070 * Enable PCH resources required for PCH ports:
4071 * - PCH PLLs
4072 * - FDI training & RX/TX
4073 * - update transcoder timings
4074 * - DP transcoding bits
4075 * - transcoder
4076 */
4077static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004078{
4079 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004080 struct drm_i915_private *dev_priv = dev->dev_private;
4081 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4082 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004083 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004084
Daniel Vetterab9412b2013-05-03 11:49:46 +02004085 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004086
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004087 if (IS_IVYBRIDGE(dev))
4088 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4089
Daniel Vettercd986ab2012-10-26 10:58:12 +02004090 /* Write the TU size bits before fdi link training, so that error
4091 * detection works. */
4092 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4093 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4094
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004095 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004096 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004097
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004098 /* We need to program the right clock selection before writing the pixel
4099 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004100 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004101 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004102
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004103 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004104 temp |= TRANS_DPLL_ENABLE(pipe);
4105 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004106 if (intel_crtc->config->shared_dpll ==
4107 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004108 temp |= sel;
4109 else
4110 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004111 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004112 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004113
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004114 /* XXX: pch pll's can be enabled any time before we enable the PCH
4115 * transcoder, and we actually should do this to not upset any PCH
4116 * transcoder that already use the clock when we share it.
4117 *
4118 * Note that enable_shared_dpll tries to do the right thing, but
4119 * get_shared_dpll unconditionally resets the pll - we need that to have
4120 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004121 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004122
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004123 /* set transcoder timing, panel must allow it */
4124 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004125 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004126
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004127 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004128
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004129 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004130 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004131 const struct drm_display_mode *adjusted_mode =
4132 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004133 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004134 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004135 temp = I915_READ(reg);
4136 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004137 TRANS_DP_SYNC_MASK |
4138 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004139 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004140 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004141
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004142 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004143 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004144 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004145 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004146
4147 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004148 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004149 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004150 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004151 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004152 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004153 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004154 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004155 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004156 break;
4157 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004158 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004159 }
4160
Chris Wilson5eddb702010-09-11 13:48:45 +01004161 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004162 }
4163
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004164 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004165}
4166
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004167static void lpt_pch_enable(struct drm_crtc *crtc)
4168{
4169 struct drm_device *dev = crtc->dev;
4170 struct drm_i915_private *dev_priv = dev->dev_private;
4171 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004172 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004173
Daniel Vetterab9412b2013-05-03 11:49:46 +02004174 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004175
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004176 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004177
Paulo Zanoni0540e482012-10-31 18:12:40 -02004178 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004179 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004180
Paulo Zanoni937bb612012-10-31 18:12:47 -02004181 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004182}
4183
Daniel Vettera1520312013-05-03 11:49:50 +02004184static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004185{
4186 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004187 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004188 u32 temp;
4189
4190 temp = I915_READ(dslreg);
4191 udelay(500);
4192 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004193 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004194 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004195 }
4196}
4197
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004198static int
4199skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4200 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4201 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004202{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004203 struct intel_crtc_scaler_state *scaler_state =
4204 &crtc_state->scaler_state;
4205 struct intel_crtc *intel_crtc =
4206 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004207 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004208
4209 need_scaling = intel_rotation_90_or_270(rotation) ?
4210 (src_h != dst_w || src_w != dst_h):
4211 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004212
4213 /*
4214 * if plane is being disabled or scaler is no more required or force detach
4215 * - free scaler binded to this plane/crtc
4216 * - in order to do this, update crtc->scaler_usage
4217 *
4218 * Here scaler state in crtc_state is set free so that
4219 * scaler can be assigned to other user. Actual register
4220 * update to free the scaler is done in plane/panel-fit programming.
4221 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4222 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004223 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004224 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004225 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004226 scaler_state->scalers[*scaler_id].in_use = 0;
4227
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004228 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4229 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4230 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004231 scaler_state->scaler_users);
4232 *scaler_id = -1;
4233 }
4234 return 0;
4235 }
4236
4237 /* range checks */
4238 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4239 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4240
4241 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4242 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004243 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004244 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004245 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004246 return -EINVAL;
4247 }
4248
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004249 /* mark this plane as a scaler user in crtc_state */
4250 scaler_state->scaler_users |= (1 << scaler_user);
4251 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4252 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4253 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4254 scaler_state->scaler_users);
4255
4256 return 0;
4257}
4258
4259/**
4260 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4261 *
4262 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004263 *
4264 * Return
4265 * 0 - scaler_usage updated successfully
4266 * error - requested scaling cannot be supported or other error condition
4267 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004268int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004269{
4270 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004271 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004272
4273 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4274 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4275
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004276 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004277 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004278 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004279 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004280}
4281
4282/**
4283 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4284 *
4285 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004286 * @plane_state: atomic plane state to update
4287 *
4288 * Return
4289 * 0 - scaler_usage updated successfully
4290 * error - requested scaling cannot be supported or other error condition
4291 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004292static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4293 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004294{
4295
4296 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004297 struct intel_plane *intel_plane =
4298 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004299 struct drm_framebuffer *fb = plane_state->base.fb;
4300 int ret;
4301
4302 bool force_detach = !fb || !plane_state->visible;
4303
4304 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4305 intel_plane->base.base.id, intel_crtc->pipe,
4306 drm_plane_index(&intel_plane->base));
4307
4308 ret = skl_update_scaler(crtc_state, force_detach,
4309 drm_plane_index(&intel_plane->base),
4310 &plane_state->scaler_id,
4311 plane_state->base.rotation,
4312 drm_rect_width(&plane_state->src) >> 16,
4313 drm_rect_height(&plane_state->src) >> 16,
4314 drm_rect_width(&plane_state->dst),
4315 drm_rect_height(&plane_state->dst));
4316
4317 if (ret || plane_state->scaler_id < 0)
4318 return ret;
4319
Chandra Kondurua1b22782015-04-07 15:28:45 -07004320 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004321 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004322 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004323 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004324 return -EINVAL;
4325 }
4326
4327 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004328 switch (fb->pixel_format) {
4329 case DRM_FORMAT_RGB565:
4330 case DRM_FORMAT_XBGR8888:
4331 case DRM_FORMAT_XRGB8888:
4332 case DRM_FORMAT_ABGR8888:
4333 case DRM_FORMAT_ARGB8888:
4334 case DRM_FORMAT_XRGB2101010:
4335 case DRM_FORMAT_XBGR2101010:
4336 case DRM_FORMAT_YUYV:
4337 case DRM_FORMAT_YVYU:
4338 case DRM_FORMAT_UYVY:
4339 case DRM_FORMAT_VYUY:
4340 break;
4341 default:
4342 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4343 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4344 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004345 }
4346
Chandra Kondurua1b22782015-04-07 15:28:45 -07004347 return 0;
4348}
4349
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004350static void skylake_scaler_disable(struct intel_crtc *crtc)
4351{
4352 int i;
4353
4354 for (i = 0; i < crtc->num_scalers; i++)
4355 skl_detach_scaler(crtc, i);
4356}
4357
4358static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004359{
4360 struct drm_device *dev = crtc->base.dev;
4361 struct drm_i915_private *dev_priv = dev->dev_private;
4362 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004363 struct intel_crtc_scaler_state *scaler_state =
4364 &crtc->config->scaler_state;
4365
4366 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4367
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004368 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004369 int id;
4370
4371 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4372 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4373 return;
4374 }
4375
4376 id = scaler_state->scaler_id;
4377 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4378 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4379 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4380 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4381
4382 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004383 }
4384}
4385
Jesse Barnesb074cec2013-04-25 12:55:02 -07004386static void ironlake_pfit_enable(struct intel_crtc *crtc)
4387{
4388 struct drm_device *dev = crtc->base.dev;
4389 struct drm_i915_private *dev_priv = dev->dev_private;
4390 int pipe = crtc->pipe;
4391
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004392 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004393 /* Force use of hard-coded filter coefficients
4394 * as some pre-programmed values are broken,
4395 * e.g. x201.
4396 */
4397 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4398 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4399 PF_PIPE_SEL_IVB(pipe));
4400 else
4401 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004402 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4403 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004404 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004405}
4406
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004407void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004408{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004409 struct drm_device *dev = crtc->base.dev;
4410 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004411
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004412 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004413 return;
4414
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004415 /*
4416 * We can only enable IPS after we enable a plane and wait for a vblank
4417 * This function is called from post_plane_update, which is run after
4418 * a vblank wait.
4419 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004420
Paulo Zanonid77e4532013-09-24 13:52:55 -03004421 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004422 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004423 mutex_lock(&dev_priv->rps.hw_lock);
4424 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4425 mutex_unlock(&dev_priv->rps.hw_lock);
4426 /* Quoting Art Runyan: "its not safe to expect any particular
4427 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004428 * mailbox." Moreover, the mailbox may return a bogus state,
4429 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004430 */
4431 } else {
4432 I915_WRITE(IPS_CTL, IPS_ENABLE);
4433 /* The bit only becomes 1 in the next vblank, so this wait here
4434 * is essentially intel_wait_for_vblank. If we don't have this
4435 * and don't wait for vblanks until the end of crtc_enable, then
4436 * the HW state readout code will complain that the expected
4437 * IPS_CTL value is not the one we read. */
4438 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4439 DRM_ERROR("Timed out waiting for IPS enable\n");
4440 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004441}
4442
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004443void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004444{
4445 struct drm_device *dev = crtc->base.dev;
4446 struct drm_i915_private *dev_priv = dev->dev_private;
4447
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004448 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004449 return;
4450
4451 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004452 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004453 mutex_lock(&dev_priv->rps.hw_lock);
4454 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4455 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004456 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4457 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4458 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004459 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004460 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004461 POSTING_READ(IPS_CTL);
4462 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004463
4464 /* We need to wait for a vblank before we can disable the plane. */
4465 intel_wait_for_vblank(dev, crtc->pipe);
4466}
4467
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004468static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004469{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004470 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004471 struct drm_device *dev = intel_crtc->base.dev;
4472 struct drm_i915_private *dev_priv = dev->dev_private;
4473
4474 mutex_lock(&dev->struct_mutex);
4475 dev_priv->mm.interruptible = false;
4476 (void) intel_overlay_switch_off(intel_crtc->overlay);
4477 dev_priv->mm.interruptible = true;
4478 mutex_unlock(&dev->struct_mutex);
4479 }
4480
4481 /* Let userspace switch the overlay on again. In most cases userspace
4482 * has to recompute where to put it anyway.
4483 */
4484}
4485
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004486/**
4487 * intel_post_enable_primary - Perform operations after enabling primary plane
4488 * @crtc: the CRTC whose primary plane was just enabled
4489 *
4490 * Performs potentially sleeping operations that must be done after the primary
4491 * plane is enabled, such as updating FBC and IPS. Note that this may be
4492 * called due to an explicit primary plane update, or due to an implicit
4493 * re-enable that is caused when a sprite plane is updated to no longer
4494 * completely hide the primary plane.
4495 */
4496static void
4497intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004498{
4499 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004500 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004501 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4502 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004503
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004504 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004505 * FIXME IPS should be fine as long as one plane is
4506 * enabled, but in practice it seems to have problems
4507 * when going from primary only to sprite only and vice
4508 * versa.
4509 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004510 hsw_enable_ips(intel_crtc);
4511
Daniel Vetterf99d7062014-06-19 16:01:59 +02004512 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004513 * Gen2 reports pipe underruns whenever all planes are disabled.
4514 * So don't enable underrun reporting before at least some planes
4515 * are enabled.
4516 * FIXME: Need to fix the logic to work when we turn off all planes
4517 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004518 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004519 if (IS_GEN2(dev))
4520 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4521
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004522 /* Underruns don't always raise interrupts, so check manually. */
4523 intel_check_cpu_fifo_underruns(dev_priv);
4524 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004525}
4526
Ville Syrjälä2622a082016-03-09 19:07:26 +02004527/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004528static void
4529intel_pre_disable_primary(struct drm_crtc *crtc)
4530{
4531 struct drm_device *dev = crtc->dev;
4532 struct drm_i915_private *dev_priv = dev->dev_private;
4533 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4534 int pipe = intel_crtc->pipe;
4535
4536 /*
4537 * Gen2 reports pipe underruns whenever all planes are disabled.
4538 * So diasble underrun reporting before all the planes get disabled.
4539 * FIXME: Need to fix the logic to work when we turn off all planes
4540 * but leave the pipe running.
4541 */
4542 if (IS_GEN2(dev))
4543 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4544
4545 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004546 * FIXME IPS should be fine as long as one plane is
4547 * enabled, but in practice it seems to have problems
4548 * when going from primary only to sprite only and vice
4549 * versa.
4550 */
4551 hsw_disable_ips(intel_crtc);
4552}
4553
4554/* FIXME get rid of this and use pre_plane_update */
4555static void
4556intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4557{
4558 struct drm_device *dev = crtc->dev;
4559 struct drm_i915_private *dev_priv = dev->dev_private;
4560 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4561 int pipe = intel_crtc->pipe;
4562
4563 intel_pre_disable_primary(crtc);
4564
4565 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004566 * Vblank time updates from the shadow to live plane control register
4567 * are blocked if the memory self-refresh mode is active at that
4568 * moment. So to make sure the plane gets truly disabled, disable
4569 * first the self-refresh mode. The self-refresh enable bit in turn
4570 * will be checked/applied by the HW only at the next frame start
4571 * event which is after the vblank start event, so we need to have a
4572 * wait-for-vblank between disabling the plane and the pipe.
4573 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004574 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004575 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004576 dev_priv->wm.vlv.cxsr = false;
4577 intel_wait_for_vblank(dev, pipe);
4578 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004579}
4580
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004581static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004582{
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004583 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4584 struct drm_atomic_state *old_state = old_crtc_state->base.state;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004585 struct intel_crtc_state *pipe_config =
4586 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004587 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004588 struct drm_plane *primary = crtc->base.primary;
4589 struct drm_plane_state *old_pri_state =
4590 drm_atomic_get_existing_plane_state(old_state, primary);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004591
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004592 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004593
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004594 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004595
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004596 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004597 intel_update_watermarks(&crtc->base);
4598
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004599 if (old_pri_state) {
4600 struct intel_plane_state *primary_state =
4601 to_intel_plane_state(primary->state);
4602 struct intel_plane_state *old_primary_state =
4603 to_intel_plane_state(old_pri_state);
4604
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004605 intel_fbc_post_update(crtc);
4606
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004607 if (primary_state->visible &&
4608 (needs_modeset(&pipe_config->base) ||
4609 !old_primary_state->visible))
4610 intel_post_enable_primary(&crtc->base);
4611 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004612}
4613
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004614static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004615{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004616 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004617 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004618 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004619 struct intel_crtc_state *pipe_config =
4620 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004621 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4622 struct drm_plane *primary = crtc->base.primary;
4623 struct drm_plane_state *old_pri_state =
4624 drm_atomic_get_existing_plane_state(old_state, primary);
4625 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004626
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004627 if (old_pri_state) {
4628 struct intel_plane_state *primary_state =
4629 to_intel_plane_state(primary->state);
4630 struct intel_plane_state *old_primary_state =
4631 to_intel_plane_state(old_pri_state);
4632
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004633 intel_fbc_pre_update(crtc);
4634
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004635 if (old_primary_state->visible &&
4636 (modeset || !primary_state->visible))
4637 intel_pre_disable_primary(&crtc->base);
4638 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004639
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004640 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004641 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004642
Ville Syrjälä2622a082016-03-09 19:07:26 +02004643 /*
4644 * Vblank time updates from the shadow to live plane control register
4645 * are blocked if the memory self-refresh mode is active at that
4646 * moment. So to make sure the plane gets truly disabled, disable
4647 * first the self-refresh mode. The self-refresh enable bit in turn
4648 * will be checked/applied by the HW only at the next frame start
4649 * event which is after the vblank start event, so we need to have a
4650 * wait-for-vblank between disabling the plane and the pipe.
4651 */
4652 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004653 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004654 dev_priv->wm.vlv.cxsr = false;
4655 intel_wait_for_vblank(dev, crtc->pipe);
4656 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004657 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004658
Matt Ropered4a6a72016-02-23 17:20:13 -08004659 /*
4660 * IVB workaround: must disable low power watermarks for at least
4661 * one frame before enabling scaling. LP watermarks can be re-enabled
4662 * when scaling is disabled.
4663 *
4664 * WaCxSRDisabledForSpriteScaling:ivb
4665 */
4666 if (pipe_config->disable_lp_wm) {
4667 ilk_disable_lp_wm(dev);
4668 intel_wait_for_vblank(dev, crtc->pipe);
4669 }
4670
4671 /*
4672 * If we're doing a modeset, we're done. No need to do any pre-vblank
4673 * watermark programming here.
4674 */
4675 if (needs_modeset(&pipe_config->base))
4676 return;
4677
4678 /*
4679 * For platforms that support atomic watermarks, program the
4680 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4681 * will be the intermediate values that are safe for both pre- and
4682 * post- vblank; when vblank happens, the 'active' values will be set
4683 * to the final 'target' values and we'll do this again to get the
4684 * optimal watermarks. For gen9+ platforms, the values we program here
4685 * will be the final target values which will get automatically latched
4686 * at vblank time; no further programming will be necessary.
4687 *
4688 * If a platform hasn't been transitioned to atomic watermarks yet,
4689 * we'll continue to update watermarks the old way, if flags tell
4690 * us to.
4691 */
4692 if (dev_priv->display.initial_watermarks != NULL)
4693 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004694 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004695 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004696}
4697
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004698static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004699{
4700 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004701 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004702 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004703 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004704
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004705 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004706
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004707 drm_for_each_plane_mask(p, dev, plane_mask)
4708 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004709
Daniel Vetterf99d7062014-06-19 16:01:59 +02004710 /*
4711 * FIXME: Once we grow proper nuclear flip support out of this we need
4712 * to compute the mask of flip planes precisely. For the time being
4713 * consider this a flip to a NULL plane.
4714 */
4715 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004716}
4717
Jesse Barnesf67a5592011-01-05 10:31:48 -08004718static void ironlake_crtc_enable(struct drm_crtc *crtc)
4719{
4720 struct drm_device *dev = crtc->dev;
4721 struct drm_i915_private *dev_priv = dev->dev_private;
4722 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004723 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004724 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004725 struct intel_crtc_state *pipe_config =
4726 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004727
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004728 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004729 return;
4730
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004731 /*
4732 * Sometimes spurious CPU pipe underruns happen during FDI
4733 * training, at least with VGA+HDMI cloning. Suppress them.
4734 *
4735 * On ILK we get an occasional spurious CPU pipe underruns
4736 * between eDP port A enable and vdd enable. Also PCH port
4737 * enable seems to result in the occasional CPU pipe underrun.
4738 *
4739 * Spurious PCH underruns also occur during PCH enabling.
4740 */
4741 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4742 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004743 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004744 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4745
4746 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004747 intel_prepare_shared_dpll(intel_crtc);
4748
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004749 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304750 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004751
4752 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004753 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004754
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004755 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004756 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004757 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004758 }
4759
4760 ironlake_set_pipeconf(crtc);
4761
Jesse Barnesf67a5592011-01-05 10:31:48 -08004762 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004763
Daniel Vetterf6736a12013-06-05 13:34:30 +02004764 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004765 if (encoder->pre_enable)
4766 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004767
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004768 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004769 /* Note: FDI PLL enabling _must_ be done before we enable the
4770 * cpu pipes, hence this is separate from all the other fdi/pch
4771 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004772 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004773 } else {
4774 assert_fdi_tx_disabled(dev_priv, pipe);
4775 assert_fdi_rx_disabled(dev_priv, pipe);
4776 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004777
Jesse Barnesb074cec2013-04-25 12:55:02 -07004778 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004779
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004780 /*
4781 * On ILK+ LUT must be loaded before the pipe is running but with
4782 * clocks enabled
4783 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004784 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004785
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004786 if (dev_priv->display.initial_watermarks != NULL)
4787 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004788 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004789
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004790 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004791 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004792
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004793 assert_vblank_disabled(crtc);
4794 drm_crtc_vblank_on(crtc);
4795
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004796 for_each_encoder_on_crtc(dev, crtc, encoder)
4797 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004798
4799 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004800 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004801
4802 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4803 if (intel_crtc->config->has_pch_encoder)
4804 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004805 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004806 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004807}
4808
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004809/* IPS only exists on ULT machines and is tied to pipe A. */
4810static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4811{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004812 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004813}
4814
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004815static void haswell_crtc_enable(struct drm_crtc *crtc)
4816{
4817 struct drm_device *dev = crtc->dev;
4818 struct drm_i915_private *dev_priv = dev->dev_private;
4819 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4820 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004821 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004822 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004823 struct intel_crtc_state *pipe_config =
4824 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004825
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004826 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004827 return;
4828
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004829 if (intel_crtc->config->has_pch_encoder)
4830 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4831 false);
4832
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004833 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004834 intel_enable_shared_dpll(intel_crtc);
4835
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004836 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304837 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004838
Jani Nikula4d1de972016-03-18 17:05:42 +02004839 if (!intel_crtc->config->has_dsi_encoder)
4840 intel_set_pipe_timings(intel_crtc);
4841
Jani Nikulabc58be62016-03-18 17:05:39 +02004842 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004843
Jani Nikula4d1de972016-03-18 17:05:42 +02004844 if (cpu_transcoder != TRANSCODER_EDP &&
4845 !transcoder_is_dsi(cpu_transcoder)) {
4846 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004847 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004848 }
4849
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004850 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004851 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004852 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004853 }
4854
Jani Nikula4d1de972016-03-18 17:05:42 +02004855 if (!intel_crtc->config->has_dsi_encoder)
4856 haswell_set_pipeconf(crtc);
4857
Jani Nikula391bf042016-03-18 17:05:40 +02004858 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004859
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004860 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02004861
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004862 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004863
Daniel Vetter6b698512015-11-28 11:05:39 +01004864 if (intel_crtc->config->has_pch_encoder)
4865 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4866 else
4867 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4868
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304869 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004870 if (encoder->pre_enable)
4871 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304872 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004873
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004874 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004875 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004876
Jani Nikulaa65347b2015-11-27 12:21:46 +02004877 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304878 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004879
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004880 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004881 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004882 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004883 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004884
4885 /*
4886 * On ILK+ LUT must be loaded before the pipe is running but with
4887 * clocks enabled
4888 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004889 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004890
Paulo Zanoni1f544382012-10-24 11:32:00 -02004891 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004892 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304893 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004894
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004895 if (dev_priv->display.initial_watermarks != NULL)
4896 dev_priv->display.initial_watermarks(pipe_config);
4897 else
4898 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004899
4900 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4901 if (!intel_crtc->config->has_dsi_encoder)
4902 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004903
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004904 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004905 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004906
Jani Nikulaa65347b2015-11-27 12:21:46 +02004907 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004908 intel_ddi_set_vc_payload_alloc(crtc, true);
4909
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004910 assert_vblank_disabled(crtc);
4911 drm_crtc_vblank_on(crtc);
4912
Jani Nikula8807e552013-08-30 19:40:32 +03004913 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004914 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004915 intel_opregion_notify_encoder(encoder, true);
4916 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004917
Daniel Vetter6b698512015-11-28 11:05:39 +01004918 if (intel_crtc->config->has_pch_encoder) {
4919 intel_wait_for_vblank(dev, pipe);
4920 intel_wait_for_vblank(dev, pipe);
4921 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004922 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4923 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004924 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004925
Paulo Zanonie4916942013-09-20 16:21:19 -03004926 /* If we change the relative order between pipe/planes enabling, we need
4927 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004928 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4929 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4930 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4931 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4932 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004933}
4934
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004935static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004936{
4937 struct drm_device *dev = crtc->base.dev;
4938 struct drm_i915_private *dev_priv = dev->dev_private;
4939 int pipe = crtc->pipe;
4940
4941 /* To avoid upsetting the power well on haswell only disable the pfit if
4942 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004943 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004944 I915_WRITE(PF_CTL(pipe), 0);
4945 I915_WRITE(PF_WIN_POS(pipe), 0);
4946 I915_WRITE(PF_WIN_SZ(pipe), 0);
4947 }
4948}
4949
Jesse Barnes6be4a602010-09-10 10:26:01 -07004950static void ironlake_crtc_disable(struct drm_crtc *crtc)
4951{
4952 struct drm_device *dev = crtc->dev;
4953 struct drm_i915_private *dev_priv = dev->dev_private;
4954 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004955 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004956 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004957
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004958 /*
4959 * Sometimes spurious CPU pipe underruns happen when the
4960 * pipe is already disabled, but FDI RX/TX is still enabled.
4961 * Happens at least with VGA+HDMI cloning. Suppress them.
4962 */
4963 if (intel_crtc->config->has_pch_encoder) {
4964 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004965 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004966 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004967
Daniel Vetterea9d7582012-07-10 10:42:52 +02004968 for_each_encoder_on_crtc(dev, crtc, encoder)
4969 encoder->disable(encoder);
4970
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004971 drm_crtc_vblank_off(crtc);
4972 assert_vblank_disabled(crtc);
4973
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004974 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004975
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004976 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004977
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004978 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004979 ironlake_fdi_disable(crtc);
4980
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004981 for_each_encoder_on_crtc(dev, crtc, encoder)
4982 if (encoder->post_disable)
4983 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004984
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004985 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004986 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004987
Daniel Vetterd925c592013-06-05 13:34:04 +02004988 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004989 i915_reg_t reg;
4990 u32 temp;
4991
Daniel Vetterd925c592013-06-05 13:34:04 +02004992 /* disable TRANS_DP_CTL */
4993 reg = TRANS_DP_CTL(pipe);
4994 temp = I915_READ(reg);
4995 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4996 TRANS_DP_PORT_SEL_MASK);
4997 temp |= TRANS_DP_PORT_SEL_NONE;
4998 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004999
Daniel Vetterd925c592013-06-05 13:34:04 +02005000 /* disable DPLL_SEL */
5001 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005002 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005003 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005004 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005005
Daniel Vetterd925c592013-06-05 13:34:04 +02005006 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005007 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005008
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005009 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005010 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005011}
5012
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005013static void haswell_crtc_disable(struct drm_crtc *crtc)
5014{
5015 struct drm_device *dev = crtc->dev;
5016 struct drm_i915_private *dev_priv = dev->dev_private;
5017 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5018 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005019 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005020
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005021 if (intel_crtc->config->has_pch_encoder)
5022 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5023 false);
5024
Jani Nikula8807e552013-08-30 19:40:32 +03005025 for_each_encoder_on_crtc(dev, crtc, encoder) {
5026 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005027 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005028 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005029
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005030 drm_crtc_vblank_off(crtc);
5031 assert_vblank_disabled(crtc);
5032
Jani Nikula4d1de972016-03-18 17:05:42 +02005033 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5034 if (!intel_crtc->config->has_dsi_encoder)
5035 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005036
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005037 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005038 intel_ddi_set_vc_payload_alloc(crtc, false);
5039
Jani Nikulaa65347b2015-11-27 12:21:46 +02005040 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305041 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005042
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005043 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005044 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005045 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005046 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005047
Jani Nikulaa65347b2015-11-27 12:21:46 +02005048 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305049 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005050
Imre Deak97b040a2014-06-25 22:01:50 +03005051 for_each_encoder_on_crtc(dev, crtc, encoder)
5052 if (encoder->post_disable)
5053 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005054
Ville Syrjälä92966a32015-12-08 16:05:48 +02005055 if (intel_crtc->config->has_pch_encoder) {
5056 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005057 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005058 intel_ddi_fdi_disable(crtc);
5059
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005060 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5061 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005062 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005063}
5064
Jesse Barnes2dd24552013-04-25 12:55:01 -07005065static void i9xx_pfit_enable(struct intel_crtc *crtc)
5066{
5067 struct drm_device *dev = crtc->base.dev;
5068 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005069 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005070
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005071 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005072 return;
5073
Daniel Vetterc0b03412013-05-28 12:05:54 +02005074 /*
5075 * The panel fitter should only be adjusted whilst the pipe is disabled,
5076 * according to register description and PRM.
5077 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005078 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5079 assert_pipe_disabled(dev_priv, crtc->pipe);
5080
Jesse Barnesb074cec2013-04-25 12:55:02 -07005081 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5082 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005083
5084 /* Border color in case we don't scale up to the full screen. Black by
5085 * default, change to something else for debugging. */
5086 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005087}
5088
Dave Airlied05410f2014-06-05 13:22:59 +10005089static enum intel_display_power_domain port_to_power_domain(enum port port)
5090{
5091 switch (port) {
5092 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005093 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005094 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005095 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005096 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005097 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005098 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005099 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005100 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005101 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005102 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005103 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005104 return POWER_DOMAIN_PORT_OTHER;
5105 }
5106}
5107
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005108static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5109{
5110 switch (port) {
5111 case PORT_A:
5112 return POWER_DOMAIN_AUX_A;
5113 case PORT_B:
5114 return POWER_DOMAIN_AUX_B;
5115 case PORT_C:
5116 return POWER_DOMAIN_AUX_C;
5117 case PORT_D:
5118 return POWER_DOMAIN_AUX_D;
5119 case PORT_E:
5120 /* FIXME: Check VBT for actual wiring of PORT E */
5121 return POWER_DOMAIN_AUX_D;
5122 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005123 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005124 return POWER_DOMAIN_AUX_A;
5125 }
5126}
5127
Imre Deak319be8a2014-03-04 19:22:57 +02005128enum intel_display_power_domain
5129intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005130{
Imre Deak319be8a2014-03-04 19:22:57 +02005131 struct drm_device *dev = intel_encoder->base.dev;
5132 struct intel_digital_port *intel_dig_port;
5133
5134 switch (intel_encoder->type) {
5135 case INTEL_OUTPUT_UNKNOWN:
5136 /* Only DDI platforms should ever use this output type */
5137 WARN_ON_ONCE(!HAS_DDI(dev));
5138 case INTEL_OUTPUT_DISPLAYPORT:
5139 case INTEL_OUTPUT_HDMI:
5140 case INTEL_OUTPUT_EDP:
5141 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005142 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005143 case INTEL_OUTPUT_DP_MST:
5144 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5145 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005146 case INTEL_OUTPUT_ANALOG:
5147 return POWER_DOMAIN_PORT_CRT;
5148 case INTEL_OUTPUT_DSI:
5149 return POWER_DOMAIN_PORT_DSI;
5150 default:
5151 return POWER_DOMAIN_PORT_OTHER;
5152 }
5153}
5154
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005155enum intel_display_power_domain
5156intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5157{
5158 struct drm_device *dev = intel_encoder->base.dev;
5159 struct intel_digital_port *intel_dig_port;
5160
5161 switch (intel_encoder->type) {
5162 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005163 case INTEL_OUTPUT_HDMI:
5164 /*
5165 * Only DDI platforms should ever use these output types.
5166 * We can get here after the HDMI detect code has already set
5167 * the type of the shared encoder. Since we can't be sure
5168 * what's the status of the given connectors, play safe and
5169 * run the DP detection too.
5170 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005171 WARN_ON_ONCE(!HAS_DDI(dev));
5172 case INTEL_OUTPUT_DISPLAYPORT:
5173 case INTEL_OUTPUT_EDP:
5174 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5175 return port_to_aux_power_domain(intel_dig_port->port);
5176 case INTEL_OUTPUT_DP_MST:
5177 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5178 return port_to_aux_power_domain(intel_dig_port->port);
5179 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005180 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005181 return POWER_DOMAIN_AUX_A;
5182 }
5183}
5184
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005185static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5186 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005187{
5188 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005189 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005190 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5191 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005192 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005193 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005194
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005195 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005196 return 0;
5197
Imre Deak77d22dc2014-03-05 16:20:52 +02005198 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5199 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005200 if (crtc_state->pch_pfit.enabled ||
5201 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005202 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5203
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005204 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5205 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5206
Imre Deak319be8a2014-03-04 19:22:57 +02005207 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005208 }
Imre Deak319be8a2014-03-04 19:22:57 +02005209
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005210 if (crtc_state->shared_dpll)
5211 mask |= BIT(POWER_DOMAIN_PLLS);
5212
Imre Deak77d22dc2014-03-05 16:20:52 +02005213 return mask;
5214}
5215
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005216static unsigned long
5217modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5218 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005219{
5220 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5221 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5222 enum intel_display_power_domain domain;
5223 unsigned long domains, new_domains, old_domains;
5224
5225 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005226 intel_crtc->enabled_power_domains = new_domains =
5227 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005228
5229 domains = new_domains & ~old_domains;
5230
5231 for_each_power_domain(domain, domains)
5232 intel_display_power_get(dev_priv, domain);
5233
5234 return old_domains & ~new_domains;
5235}
5236
5237static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5238 unsigned long domains)
5239{
5240 enum intel_display_power_domain domain;
5241
5242 for_each_power_domain(domain, domains)
5243 intel_display_power_put(dev_priv, domain);
5244}
5245
Mika Kaholaadafdc62015-08-18 14:36:59 +03005246static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5247{
5248 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5249
5250 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5251 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5252 return max_cdclk_freq;
5253 else if (IS_CHERRYVIEW(dev_priv))
5254 return max_cdclk_freq*95/100;
5255 else if (INTEL_INFO(dev_priv)->gen < 4)
5256 return 2*max_cdclk_freq*90/100;
5257 else
5258 return max_cdclk_freq*90/100;
5259}
5260
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005261static void intel_update_max_cdclk(struct drm_device *dev)
5262{
5263 struct drm_i915_private *dev_priv = dev->dev_private;
5264
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005265 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005266 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5267
5268 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5269 dev_priv->max_cdclk_freq = 675000;
5270 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5271 dev_priv->max_cdclk_freq = 540000;
5272 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5273 dev_priv->max_cdclk_freq = 450000;
5274 else
5275 dev_priv->max_cdclk_freq = 337500;
Matt Roper281c1142016-04-05 14:37:19 -07005276 } else if (IS_BROXTON(dev)) {
5277 dev_priv->max_cdclk_freq = 624000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005278 } else if (IS_BROADWELL(dev)) {
5279 /*
5280 * FIXME with extra cooling we can allow
5281 * 540 MHz for ULX and 675 Mhz for ULT.
5282 * How can we know if extra cooling is
5283 * available? PCI ID, VTB, something else?
5284 */
5285 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5286 dev_priv->max_cdclk_freq = 450000;
5287 else if (IS_BDW_ULX(dev))
5288 dev_priv->max_cdclk_freq = 450000;
5289 else if (IS_BDW_ULT(dev))
5290 dev_priv->max_cdclk_freq = 540000;
5291 else
5292 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005293 } else if (IS_CHERRYVIEW(dev)) {
5294 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005295 } else if (IS_VALLEYVIEW(dev)) {
5296 dev_priv->max_cdclk_freq = 400000;
5297 } else {
5298 /* otherwise assume cdclk is fixed */
5299 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5300 }
5301
Mika Kaholaadafdc62015-08-18 14:36:59 +03005302 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5303
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005304 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5305 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005306
5307 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5308 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005309}
5310
5311static void intel_update_cdclk(struct drm_device *dev)
5312{
5313 struct drm_i915_private *dev_priv = dev->dev_private;
5314
5315 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5316 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5317 dev_priv->cdclk_freq);
5318
5319 /*
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005320 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5321 * Programmng [sic] note: bit[9:2] should be programmed to the number
5322 * of cdclk that generates 4MHz reference clock freq which is used to
5323 * generate GMBus clock. This will vary with the cdclk freq.
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005324 */
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005325 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005326 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005327
5328 if (dev_priv->max_cdclk_freq == 0)
5329 intel_update_max_cdclk(dev);
5330}
5331
Ville Syrjälä92891e42016-05-11 22:44:45 +03005332/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5333static int skl_cdclk_decimal(int cdclk)
5334{
5335 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5336}
5337
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005338static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305339{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305340 uint32_t divider;
5341 uint32_t ratio;
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005342 uint32_t current_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305343 int ret;
5344
5345 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005346 switch (cdclk) {
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305347 case 144000:
5348 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5349 ratio = BXT_DE_PLL_RATIO(60);
5350 break;
5351 case 288000:
5352 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5353 ratio = BXT_DE_PLL_RATIO(60);
5354 break;
5355 case 384000:
5356 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5357 ratio = BXT_DE_PLL_RATIO(60);
5358 break;
5359 case 576000:
5360 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5361 ratio = BXT_DE_PLL_RATIO(60);
5362 break;
5363 case 624000:
5364 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5365 ratio = BXT_DE_PLL_RATIO(65);
5366 break;
5367 case 19200:
5368 /*
5369 * Bypass frequency with DE PLL disabled. Init ratio, divider
5370 * to suppress GCC warning.
5371 */
5372 ratio = 0;
5373 divider = 0;
5374 break;
5375 default:
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005376 DRM_ERROR("unsupported CDCLK freq %d", cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305377
5378 return;
5379 }
5380
5381 mutex_lock(&dev_priv->rps.hw_lock);
5382 /* Inform power controller of upcoming frequency change */
5383 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5384 0x80000000);
5385 mutex_unlock(&dev_priv->rps.hw_lock);
5386
5387 if (ret) {
5388 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005389 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305390 return;
5391 }
5392
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005393 current_cdclk = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305394 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005395 current_cdclk = current_cdclk * 500 + 1000;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305396
5397 /*
5398 * DE PLL has to be disabled when
5399 * - setting to 19.2MHz (bypass, PLL isn't used)
5400 * - before setting to 624MHz (PLL needs toggling)
5401 * - before setting to any frequency from 624MHz (PLL needs toggling)
5402 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005403 if (cdclk == 19200 || cdclk == 624000 ||
5404 current_cdclk == 624000) {
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305405 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5406 /* Timeout 200us */
5407 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5408 1))
5409 DRM_ERROR("timout waiting for DE PLL unlock\n");
5410 }
5411
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005412 if (cdclk != 19200) {
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305413 uint32_t val;
5414
5415 val = I915_READ(BXT_DE_PLL_CTL);
5416 val &= ~BXT_DE_PLL_RATIO_MASK;
5417 val |= ratio;
5418 I915_WRITE(BXT_DE_PLL_CTL, val);
5419
5420 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5421 /* Timeout 200us */
5422 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5423 DRM_ERROR("timeout waiting for DE PLL lock\n");
5424
Ville Syrjäläb8e75702016-05-11 22:44:52 +03005425 val = divider | skl_cdclk_decimal(cdclk);
Ville Syrjälä7fe62752016-05-11 22:44:51 +03005426 /*
5427 * FIXME if only the cd2x divider needs changing, it could be done
5428 * without shutting off the pipe (if only one pipe is active).
5429 */
5430 val |= BXT_CDCLK_CD2X_PIPE_NONE;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305431 /*
5432 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5433 * enable otherwise.
5434 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005435 if (cdclk >= 500000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305436 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305437 I915_WRITE(CDCLK_CTL, val);
5438 }
5439
5440 mutex_lock(&dev_priv->rps.hw_lock);
5441 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005442 DIV_ROUND_UP(cdclk, 25000));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305443 mutex_unlock(&dev_priv->rps.hw_lock);
5444
5445 if (ret) {
5446 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005447 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305448 return;
5449 }
5450
Imre Deakc6c46962016-04-01 16:02:40 +03005451 intel_update_cdclk(dev_priv->dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305452}
5453
Imre Deakc2e001e2016-04-01 16:02:43 +03005454static bool broxton_cdclk_is_enabled(struct drm_i915_private *dev_priv)
5455{
5456 if (!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE))
5457 return false;
5458
5459 /* TODO: Check for a valid CDCLK rate */
5460
5461 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_REQUEST)) {
5462 DRM_DEBUG_DRIVER("CDCLK enabled, but DBUF power not requested\n");
5463
5464 return false;
5465 }
5466
5467 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE)) {
5468 DRM_DEBUG_DRIVER("CDCLK enabled, but DBUF power hasn't settled\n");
5469
5470 return false;
5471 }
5472
5473 return true;
5474}
5475
Imre Deakadc7f042016-04-04 17:27:10 +03005476bool broxton_cdclk_verify_state(struct drm_i915_private *dev_priv)
5477{
5478 return broxton_cdclk_is_enabled(dev_priv);
5479}
5480
Imre Deakc6c46962016-04-01 16:02:40 +03005481void broxton_init_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305482{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305483 /* check if cd clock is enabled */
Imre Deakc2e001e2016-04-01 16:02:43 +03005484 if (broxton_cdclk_is_enabled(dev_priv)) {
5485 DRM_DEBUG_KMS("CDCLK already enabled, won't reprogram it\n");
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305486 return;
5487 }
5488
Imre Deakc2e001e2016-04-01 16:02:43 +03005489 DRM_DEBUG_KMS("CDCLK not enabled, enabling it\n");
5490
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305491 /*
5492 * FIXME:
5493 * - The initial CDCLK needs to be read from VBT.
5494 * Need to make this change after VBT has changes for BXT.
5495 * - check if setting the max (or any) cdclk freq is really necessary
5496 * here, it belongs to modeset time
5497 */
Imre Deakc6c46962016-04-01 16:02:40 +03005498 broxton_set_cdclk(dev_priv, 624000);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305499
5500 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005501 POSTING_READ(DBUF_CTL);
5502
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305503 udelay(10);
5504
5505 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5506 DRM_ERROR("DBuf power enable timeout!\n");
5507}
5508
Imre Deakc6c46962016-04-01 16:02:40 +03005509void broxton_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305510{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305511 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005512 POSTING_READ(DBUF_CTL);
5513
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305514 udelay(10);
5515
5516 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5517 DRM_ERROR("DBuf power disable timeout!\n");
5518
5519 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
Imre Deakc6c46962016-04-01 16:02:40 +03005520 broxton_set_cdclk(dev_priv, 19200);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305521}
5522
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005523static const struct skl_cdclk_entry {
5524 unsigned int freq;
5525 unsigned int vco;
5526} skl_cdclk_frequencies[] = {
5527 { .freq = 308570, .vco = 8640 },
5528 { .freq = 337500, .vco = 8100 },
5529 { .freq = 432000, .vco = 8640 },
5530 { .freq = 450000, .vco = 8100 },
5531 { .freq = 540000, .vco = 8100 },
5532 { .freq = 617140, .vco = 8640 },
5533 { .freq = 675000, .vco = 8100 },
5534};
5535
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005536static unsigned int skl_cdclk_get_vco(unsigned int freq)
5537{
5538 unsigned int i;
5539
5540 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5541 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5542
5543 if (e->freq == freq)
5544 return e->vco;
5545 }
5546
5547 return 8100;
5548}
5549
5550static void
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005551skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005552{
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005553 int min_cdclk;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005554 u32 val;
5555
5556 /* select the minimum CDCLK before enabling DPLL 0 */
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005557 if (vco == 8640)
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005558 min_cdclk = 308570;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005559 else
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005560 min_cdclk = 337500;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005561
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005562 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005563 I915_WRITE(CDCLK_CTL, val);
5564 POSTING_READ(CDCLK_CTL);
5565
5566 /*
5567 * We always enable DPLL0 with the lowest link rate possible, but still
5568 * taking into account the VCO required to operate the eDP panel at the
5569 * desired frequency. The usual DP link rates operate with a VCO of
5570 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5571 * The modeset code is responsible for the selection of the exact link
5572 * rate later on, with the constraint of choosing a frequency that
5573 * works with required_vco.
5574 */
5575 val = I915_READ(DPLL_CTRL1);
5576
5577 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5578 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5579 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005580 if (vco == 8640)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005581 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5582 SKL_DPLL0);
5583 else
5584 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5585 SKL_DPLL0);
5586
5587 I915_WRITE(DPLL_CTRL1, val);
5588 POSTING_READ(DPLL_CTRL1);
5589
5590 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5591
5592 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5593 DRM_ERROR("DPLL0 not locked\n");
5594}
5595
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005596static void
5597skl_dpll0_disable(struct drm_i915_private *dev_priv)
5598{
5599 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5600 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5601 DRM_ERROR("Couldn't disable DPLL0\n");
5602}
5603
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005604static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5605{
5606 int ret;
5607 u32 val;
5608
5609 /* inform PCU we want to change CDCLK */
5610 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5611 mutex_lock(&dev_priv->rps.hw_lock);
5612 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5613 mutex_unlock(&dev_priv->rps.hw_lock);
5614
5615 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5616}
5617
5618static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5619{
5620 unsigned int i;
5621
5622 for (i = 0; i < 15; i++) {
5623 if (skl_cdclk_pcu_ready(dev_priv))
5624 return true;
5625 udelay(10);
5626 }
5627
5628 return false;
5629}
5630
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005631static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005632{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005633 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005634 u32 freq_select, pcu_ack;
5635
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005636 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005637
5638 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5639 DRM_ERROR("failed to inform PCU about cdclk change\n");
5640 return;
5641 }
5642
5643 /* set CDCLK_CTL */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005644 switch (cdclk) {
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005645 case 450000:
5646 case 432000:
5647 freq_select = CDCLK_FREQ_450_432;
5648 pcu_ack = 1;
5649 break;
5650 case 540000:
5651 freq_select = CDCLK_FREQ_540;
5652 pcu_ack = 2;
5653 break;
5654 case 308570:
5655 case 337500:
5656 default:
5657 freq_select = CDCLK_FREQ_337_308;
5658 pcu_ack = 0;
5659 break;
5660 case 617140:
5661 case 675000:
5662 freq_select = CDCLK_FREQ_675_617;
5663 pcu_ack = 3;
5664 break;
5665 }
5666
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005667 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005668 POSTING_READ(CDCLK_CTL);
5669
5670 /* inform PCU of the change */
5671 mutex_lock(&dev_priv->rps.hw_lock);
5672 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5673 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005674
5675 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005676}
5677
5678void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5679{
5680 /* disable DBUF power */
5681 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5682 POSTING_READ(DBUF_CTL);
5683
5684 udelay(10);
5685
5686 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5687 DRM_ERROR("DBuf power disable timeout\n");
5688
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005689 skl_dpll0_disable(dev_priv);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005690}
5691
5692void skl_init_cdclk(struct drm_i915_private *dev_priv)
5693{
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005694 unsigned int vco;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005695
Gary Wang39d9b852015-08-28 16:40:34 +08005696 /* DPLL0 not enabled (happens on early BIOS versions) */
5697 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5698 /* enable DPLL0 */
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005699 vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5700 skl_dpll0_enable(dev_priv, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005701 }
5702
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005703 /* set CDCLK to the frequency the BIOS chose */
5704 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5705
5706 /* enable DBUF power */
5707 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5708 POSTING_READ(DBUF_CTL);
5709
5710 udelay(10);
5711
5712 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5713 DRM_ERROR("DBuf power enable timeout\n");
5714}
5715
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305716int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5717{
5718 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5719 uint32_t cdctl = I915_READ(CDCLK_CTL);
5720 int freq = dev_priv->skl_boot_cdclk;
5721
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305722 /*
5723 * check if the pre-os intialized the display
5724 * There is SWF18 scratchpad register defined which is set by the
5725 * pre-os which can be used by the OS drivers to check the status
5726 */
5727 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5728 goto sanitize;
5729
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305730 /* Is PLL enabled and locked ? */
5731 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5732 goto sanitize;
5733
5734 /* DPLL okay; verify the cdclock
5735 *
5736 * Noticed in some instances that the freq selection is correct but
5737 * decimal part is programmed wrong from BIOS where pre-os does not
5738 * enable display. Verify the same as well.
5739 */
5740 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5741 /* All well; nothing to sanitize */
5742 return false;
5743sanitize:
5744 /*
5745 * As of now initialize with max cdclk till
5746 * we get dynamic cdclk support
5747 * */
5748 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5749 skl_init_cdclk(dev_priv);
5750
5751 /* we did have to sanitize */
5752 return true;
5753}
5754
Jesse Barnes30a970c2013-11-04 13:48:12 -08005755/* Adjust CDclk dividers to allow high res or save power if possible */
5756static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5757{
5758 struct drm_i915_private *dev_priv = dev->dev_private;
5759 u32 val, cmd;
5760
Vandana Kannan164dfd22014-11-24 13:37:41 +05305761 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5762 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005763
Ville Syrjälädfcab172014-06-13 13:37:47 +03005764 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005765 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005766 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005767 cmd = 1;
5768 else
5769 cmd = 0;
5770
5771 mutex_lock(&dev_priv->rps.hw_lock);
5772 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5773 val &= ~DSPFREQGUAR_MASK;
5774 val |= (cmd << DSPFREQGUAR_SHIFT);
5775 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5776 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5777 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5778 50)) {
5779 DRM_ERROR("timed out waiting for CDclk change\n");
5780 }
5781 mutex_unlock(&dev_priv->rps.hw_lock);
5782
Ville Syrjälä54433e92015-05-26 20:42:31 +03005783 mutex_lock(&dev_priv->sb_lock);
5784
Ville Syrjälädfcab172014-06-13 13:37:47 +03005785 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005786 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005787
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005788 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005789
Jesse Barnes30a970c2013-11-04 13:48:12 -08005790 /* adjust cdclk divider */
5791 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005792 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005793 val |= divider;
5794 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005795
5796 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005797 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005798 50))
5799 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005800 }
5801
Jesse Barnes30a970c2013-11-04 13:48:12 -08005802 /* adjust self-refresh exit latency value */
5803 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5804 val &= ~0x7f;
5805
5806 /*
5807 * For high bandwidth configs, we set a higher latency in the bunit
5808 * so that the core display fetch happens in time to avoid underruns.
5809 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005810 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005811 val |= 4500 / 250; /* 4.5 usec */
5812 else
5813 val |= 3000 / 250; /* 3.0 usec */
5814 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005815
Ville Syrjäläa5805162015-05-26 20:42:30 +03005816 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005817
Ville Syrjäläb6283052015-06-03 15:45:07 +03005818 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005819}
5820
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005821static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5822{
5823 struct drm_i915_private *dev_priv = dev->dev_private;
5824 u32 val, cmd;
5825
Vandana Kannan164dfd22014-11-24 13:37:41 +05305826 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5827 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005828
5829 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005830 case 333333:
5831 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005832 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005833 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005834 break;
5835 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005836 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005837 return;
5838 }
5839
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005840 /*
5841 * Specs are full of misinformation, but testing on actual
5842 * hardware has shown that we just need to write the desired
5843 * CCK divider into the Punit register.
5844 */
5845 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5846
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005847 mutex_lock(&dev_priv->rps.hw_lock);
5848 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5849 val &= ~DSPFREQGUAR_MASK_CHV;
5850 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5851 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5852 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5853 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5854 50)) {
5855 DRM_ERROR("timed out waiting for CDclk change\n");
5856 }
5857 mutex_unlock(&dev_priv->rps.hw_lock);
5858
Ville Syrjäläb6283052015-06-03 15:45:07 +03005859 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005860}
5861
Jesse Barnes30a970c2013-11-04 13:48:12 -08005862static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5863 int max_pixclk)
5864{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005865 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005866 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005867
Jesse Barnes30a970c2013-11-04 13:48:12 -08005868 /*
5869 * Really only a few cases to deal with, as only 4 CDclks are supported:
5870 * 200MHz
5871 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005872 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005873 * 400MHz (VLV only)
5874 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5875 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005876 *
5877 * We seem to get an unstable or solid color picture at 200MHz.
5878 * Not sure what's wrong. For now use 200MHz only when all pipes
5879 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005880 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005881 if (!IS_CHERRYVIEW(dev_priv) &&
5882 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005883 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005884 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005885 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005886 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005887 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005888 else
5889 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005890}
5891
Ville Syrjäläc44deb62016-05-11 22:44:43 +03005892static int broxton_calc_cdclk(int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005893{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305894 /*
5895 * FIXME:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305896 * - set 19.2MHz bypass frequency if there are no active pipes
5897 */
Ville Syrjälä760e1472016-05-11 22:44:46 +03005898 if (max_pixclk > 576000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305899 return 624000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005900 else if (max_pixclk > 384000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305901 return 576000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005902 else if (max_pixclk > 288000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305903 return 384000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005904 else if (max_pixclk > 144000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305905 return 288000;
5906 else
5907 return 144000;
5908}
5909
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005910/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005911static int intel_mode_max_pixclk(struct drm_device *dev,
5912 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005913{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005914 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5915 struct drm_i915_private *dev_priv = dev->dev_private;
5916 struct drm_crtc *crtc;
5917 struct drm_crtc_state *crtc_state;
5918 unsigned max_pixclk = 0, i;
5919 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005920
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005921 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5922 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005923
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005924 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5925 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005926
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005927 if (crtc_state->enable)
5928 pixclk = crtc_state->adjusted_mode.crtc_clock;
5929
5930 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005931 }
5932
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005933 for_each_pipe(dev_priv, pipe)
5934 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5935
Jesse Barnes30a970c2013-11-04 13:48:12 -08005936 return max_pixclk;
5937}
5938
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005939static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005940{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005941 struct drm_device *dev = state->dev;
5942 struct drm_i915_private *dev_priv = dev->dev_private;
5943 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005944 struct intel_atomic_state *intel_state =
5945 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005946
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005947 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005948 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305949
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005950 if (!intel_state->active_crtcs)
5951 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5952
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005953 return 0;
5954}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005955
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005956static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5957{
Ville Syrjälä4e5ca602016-05-11 22:44:44 +03005958 int max_pixclk = ilk_max_pixel_rate(state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005959 struct intel_atomic_state *intel_state =
5960 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005961
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005962 intel_state->cdclk = intel_state->dev_cdclk =
Ville Syrjäläc44deb62016-05-11 22:44:43 +03005963 broxton_calc_cdclk(max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005964
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005965 if (!intel_state->active_crtcs)
Ville Syrjäläc44deb62016-05-11 22:44:43 +03005966 intel_state->dev_cdclk = broxton_calc_cdclk(0);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005967
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005968 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005969}
5970
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005971static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5972{
5973 unsigned int credits, default_credits;
5974
5975 if (IS_CHERRYVIEW(dev_priv))
5976 default_credits = PFI_CREDIT(12);
5977 else
5978 default_credits = PFI_CREDIT(8);
5979
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005980 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005981 /* CHV suggested value is 31 or 63 */
5982 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005983 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005984 else
5985 credits = PFI_CREDIT(15);
5986 } else {
5987 credits = default_credits;
5988 }
5989
5990 /*
5991 * WA - write default credits before re-programming
5992 * FIXME: should we also set the resend bit here?
5993 */
5994 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5995 default_credits);
5996
5997 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5998 credits | PFI_CREDIT_RESEND);
5999
6000 /*
6001 * FIXME is this guaranteed to clear
6002 * immediately or should we poll for it?
6003 */
6004 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6005}
6006
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006007static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006008{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006009 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006010 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006011 struct intel_atomic_state *old_intel_state =
6012 to_intel_atomic_state(old_state);
6013 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006014
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006015 /*
6016 * FIXME: We can end up here with all power domains off, yet
6017 * with a CDCLK frequency other than the minimum. To account
6018 * for this take the PIPE-A power domain, which covers the HW
6019 * blocks needed for the following programming. This can be
6020 * removed once it's guaranteed that we get here either with
6021 * the minimum CDCLK set, or the required power domains
6022 * enabled.
6023 */
6024 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006025
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006026 if (IS_CHERRYVIEW(dev))
6027 cherryview_set_cdclk(dev, req_cdclk);
6028 else
6029 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006030
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006031 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006032
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006033 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006034}
6035
Jesse Barnes89b667f2013-04-18 14:51:36 -07006036static void valleyview_crtc_enable(struct drm_crtc *crtc)
6037{
6038 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006039 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006040 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6041 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006042 struct intel_crtc_state *pipe_config =
6043 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006044 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006045
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006046 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006047 return;
6048
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006049 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306050 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006051
6052 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006053 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006054
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006055 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6056 struct drm_i915_private *dev_priv = dev->dev_private;
6057
6058 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6059 I915_WRITE(CHV_CANVAS(pipe), 0);
6060 }
6061
Daniel Vetter5b18e572014-04-24 23:55:06 +02006062 i9xx_set_pipeconf(intel_crtc);
6063
Jesse Barnes89b667f2013-04-18 14:51:36 -07006064 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006065
Daniel Vettera72e4c92014-09-30 10:56:47 +02006066 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006067
Jesse Barnes89b667f2013-04-18 14:51:36 -07006068 for_each_encoder_on_crtc(dev, crtc, encoder)
6069 if (encoder->pre_pll_enable)
6070 encoder->pre_pll_enable(encoder);
6071
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006072 if (IS_CHERRYVIEW(dev)) {
6073 chv_prepare_pll(intel_crtc, intel_crtc->config);
6074 chv_enable_pll(intel_crtc, intel_crtc->config);
6075 } else {
6076 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6077 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006078 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006079
6080 for_each_encoder_on_crtc(dev, crtc, encoder)
6081 if (encoder->pre_enable)
6082 encoder->pre_enable(encoder);
6083
Jesse Barnes2dd24552013-04-25 12:55:01 -07006084 i9xx_pfit_enable(intel_crtc);
6085
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006086 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006087
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006088 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006089 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006090
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006091 assert_vblank_disabled(crtc);
6092 drm_crtc_vblank_on(crtc);
6093
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006094 for_each_encoder_on_crtc(dev, crtc, encoder)
6095 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006096}
6097
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006098static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6099{
6100 struct drm_device *dev = crtc->base.dev;
6101 struct drm_i915_private *dev_priv = dev->dev_private;
6102
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006103 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6104 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006105}
6106
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006107static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006108{
6109 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006110 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006111 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006112 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006113 struct intel_crtc_state *pipe_config =
6114 to_intel_crtc_state(crtc->state);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006115 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006116
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006117 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006118 return;
6119
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006120 i9xx_set_pll_dividers(intel_crtc);
6121
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006122 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306123 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006124
6125 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006126 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006127
Daniel Vetter5b18e572014-04-24 23:55:06 +02006128 i9xx_set_pipeconf(intel_crtc);
6129
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006130 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006131
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006132 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006133 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006134
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006135 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006136 if (encoder->pre_enable)
6137 encoder->pre_enable(encoder);
6138
Daniel Vetterf6736a12013-06-05 13:34:30 +02006139 i9xx_enable_pll(intel_crtc);
6140
Jesse Barnes2dd24552013-04-25 12:55:01 -07006141 i9xx_pfit_enable(intel_crtc);
6142
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006143 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006144
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006145 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006146 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006147
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006148 assert_vblank_disabled(crtc);
6149 drm_crtc_vblank_on(crtc);
6150
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006151 for_each_encoder_on_crtc(dev, crtc, encoder)
6152 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006153}
6154
Daniel Vetter87476d62013-04-11 16:29:06 +02006155static void i9xx_pfit_disable(struct intel_crtc *crtc)
6156{
6157 struct drm_device *dev = crtc->base.dev;
6158 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006159
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006160 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006161 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006162
6163 assert_pipe_disabled(dev_priv, crtc->pipe);
6164
Daniel Vetter328d8e82013-05-08 10:36:31 +02006165 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6166 I915_READ(PFIT_CONTROL));
6167 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006168}
6169
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006170static void i9xx_crtc_disable(struct drm_crtc *crtc)
6171{
6172 struct drm_device *dev = crtc->dev;
6173 struct drm_i915_private *dev_priv = dev->dev_private;
6174 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006175 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006176 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006177
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006178 /*
6179 * On gen2 planes are double buffered but the pipe isn't, so we must
6180 * wait for planes to fully turn off before disabling the pipe.
6181 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006182 if (IS_GEN2(dev))
6183 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006184
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006185 for_each_encoder_on_crtc(dev, crtc, encoder)
6186 encoder->disable(encoder);
6187
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006188 drm_crtc_vblank_off(crtc);
6189 assert_vblank_disabled(crtc);
6190
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006191 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006192
Daniel Vetter87476d62013-04-11 16:29:06 +02006193 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006194
Jesse Barnes89b667f2013-04-18 14:51:36 -07006195 for_each_encoder_on_crtc(dev, crtc, encoder)
6196 if (encoder->post_disable)
6197 encoder->post_disable(encoder);
6198
Jani Nikulaa65347b2015-11-27 12:21:46 +02006199 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006200 if (IS_CHERRYVIEW(dev))
6201 chv_disable_pll(dev_priv, pipe);
6202 else if (IS_VALLEYVIEW(dev))
6203 vlv_disable_pll(dev_priv, pipe);
6204 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006205 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006206 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006207
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006208 for_each_encoder_on_crtc(dev, crtc, encoder)
6209 if (encoder->post_pll_disable)
6210 encoder->post_pll_disable(encoder);
6211
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006212 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006213 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006214}
6215
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006216static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006217{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006218 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006219 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006220 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006221 enum intel_display_power_domain domain;
6222 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006223
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006224 if (!intel_crtc->active)
6225 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006226
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006227 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006228 WARN_ON(intel_crtc->unpin_work);
6229
Ville Syrjälä2622a082016-03-09 19:07:26 +02006230 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006231
6232 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6233 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006234 }
6235
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006236 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006237
6238 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6239 crtc->base.id);
6240
6241 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6242 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006243 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006244 crtc->enabled = false;
6245 crtc->state->connector_mask = 0;
6246 crtc->state->encoder_mask = 0;
6247
6248 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6249 encoder->base.crtc = NULL;
6250
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006251 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006252 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006253 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006254
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006255 domains = intel_crtc->enabled_power_domains;
6256 for_each_power_domain(domain, domains)
6257 intel_display_power_put(dev_priv, domain);
6258 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006259
6260 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6261 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006262}
6263
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006264/*
6265 * turn all crtc's off, but do not adjust state
6266 * This has to be paired with a call to intel_modeset_setup_hw_state.
6267 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006268int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006269{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006270 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006271 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006272 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006273
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006274 state = drm_atomic_helper_suspend(dev);
6275 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006276 if (ret)
6277 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006278 else
6279 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006280 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006281}
6282
Chris Wilsonea5b2132010-08-04 13:50:23 +01006283void intel_encoder_destroy(struct drm_encoder *encoder)
6284{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006285 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006286
Chris Wilsonea5b2132010-08-04 13:50:23 +01006287 drm_encoder_cleanup(encoder);
6288 kfree(intel_encoder);
6289}
6290
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006291/* Cross check the actual hw state with our own modeset state tracking (and it's
6292 * internal consistency). */
Maarten Lankhorstc0ead702016-03-30 10:00:05 +02006293static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006294{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006295 struct drm_crtc *crtc = connector->base.state->crtc;
6296
6297 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6298 connector->base.base.id,
6299 connector->base.name);
6300
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006301 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006302 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006303 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006304
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006305 I915_STATE_WARN(!crtc,
6306 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006307
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006308 if (!crtc)
6309 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006310
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006311 I915_STATE_WARN(!crtc->state->active,
6312 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006313
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006314 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006315 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006316
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006317 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006318 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006319
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006320 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006321 "attached encoder crtc differs from connector crtc\n");
6322 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006323 I915_STATE_WARN(crtc && crtc->state->active,
6324 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006325 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6326 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006327 }
6328}
6329
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006330int intel_connector_init(struct intel_connector *connector)
6331{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006332 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006333
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006334 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006335 return -ENOMEM;
6336
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006337 return 0;
6338}
6339
6340struct intel_connector *intel_connector_alloc(void)
6341{
6342 struct intel_connector *connector;
6343
6344 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6345 if (!connector)
6346 return NULL;
6347
6348 if (intel_connector_init(connector) < 0) {
6349 kfree(connector);
6350 return NULL;
6351 }
6352
6353 return connector;
6354}
6355
Daniel Vetterf0947c32012-07-02 13:10:34 +02006356/* Simple connector->get_hw_state implementation for encoders that support only
6357 * one connector and no cloning and hence the encoder state determines the state
6358 * of the connector. */
6359bool intel_connector_get_hw_state(struct intel_connector *connector)
6360{
Daniel Vetter24929352012-07-02 20:28:59 +02006361 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006362 struct intel_encoder *encoder = connector->encoder;
6363
6364 return encoder->get_hw_state(encoder, &pipe);
6365}
6366
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006367static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006368{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006369 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6370 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006371
6372 return 0;
6373}
6374
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006375static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006376 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006377{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006378 struct drm_atomic_state *state = pipe_config->base.state;
6379 struct intel_crtc *other_crtc;
6380 struct intel_crtc_state *other_crtc_state;
6381
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006382 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6383 pipe_name(pipe), pipe_config->fdi_lanes);
6384 if (pipe_config->fdi_lanes > 4) {
6385 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6386 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006387 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006388 }
6389
Paulo Zanonibafb6552013-11-02 21:07:44 -07006390 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006391 if (pipe_config->fdi_lanes > 2) {
6392 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6393 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006394 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006395 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006396 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006397 }
6398 }
6399
6400 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006401 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006402
6403 /* Ivybridge 3 pipe is really complicated */
6404 switch (pipe) {
6405 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006406 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006407 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006408 if (pipe_config->fdi_lanes <= 2)
6409 return 0;
6410
6411 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6412 other_crtc_state =
6413 intel_atomic_get_crtc_state(state, other_crtc);
6414 if (IS_ERR(other_crtc_state))
6415 return PTR_ERR(other_crtc_state);
6416
6417 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006418 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6419 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006420 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006421 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006422 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006423 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006424 if (pipe_config->fdi_lanes > 2) {
6425 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6426 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006427 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006428 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006429
6430 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6431 other_crtc_state =
6432 intel_atomic_get_crtc_state(state, other_crtc);
6433 if (IS_ERR(other_crtc_state))
6434 return PTR_ERR(other_crtc_state);
6435
6436 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006437 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006438 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006439 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006440 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006441 default:
6442 BUG();
6443 }
6444}
6445
Daniel Vettere29c22c2013-02-21 00:00:16 +01006446#define RETRY 1
6447static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006448 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006449{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006450 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006451 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006452 int lane, link_bw, fdi_dotclock, ret;
6453 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006454
Daniel Vettere29c22c2013-02-21 00:00:16 +01006455retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006456 /* FDI is a binary signal running at ~2.7GHz, encoding
6457 * each output octet as 10 bits. The actual frequency
6458 * is stored as a divider into a 100MHz clock, and the
6459 * mode pixel clock is stored in units of 1KHz.
6460 * Hence the bw of each lane in terms of the mode signal
6461 * is:
6462 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006463 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006464
Damien Lespiau241bfc32013-09-25 16:45:37 +01006465 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006466
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006467 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006468 pipe_config->pipe_bpp);
6469
6470 pipe_config->fdi_lanes = lane;
6471
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006472 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006473 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006474
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006475 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006476 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006477 pipe_config->pipe_bpp -= 2*3;
6478 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6479 pipe_config->pipe_bpp);
6480 needs_recompute = true;
6481 pipe_config->bw_constrained = true;
6482
6483 goto retry;
6484 }
6485
6486 if (needs_recompute)
6487 return RETRY;
6488
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006489 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006490}
6491
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006492static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6493 struct intel_crtc_state *pipe_config)
6494{
6495 if (pipe_config->pipe_bpp > 24)
6496 return false;
6497
6498 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006499 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006500 return true;
6501
6502 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006503 * We compare against max which means we must take
6504 * the increased cdclk requirement into account when
6505 * calculating the new cdclk.
6506 *
6507 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006508 */
6509 return ilk_pipe_pixel_rate(pipe_config) <=
6510 dev_priv->max_cdclk_freq * 95 / 100;
6511}
6512
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006513static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006514 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006515{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006516 struct drm_device *dev = crtc->base.dev;
6517 struct drm_i915_private *dev_priv = dev->dev_private;
6518
Jani Nikulad330a952014-01-21 11:24:25 +02006519 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006520 hsw_crtc_supports_ips(crtc) &&
6521 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006522}
6523
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006524static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6525{
6526 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6527
6528 /* GDG double wide on either pipe, otherwise pipe A only */
6529 return INTEL_INFO(dev_priv)->gen < 4 &&
6530 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6531}
6532
Daniel Vettera43f6e02013-06-07 23:10:32 +02006533static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006534 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006535{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006536 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006537 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006538 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006539
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006540 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006541 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006542 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006543
6544 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006545 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006546 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006547 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006548 if (intel_crtc_supports_double_wide(crtc) &&
6549 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006550 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006551 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006552 }
6553
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006554 if (adjusted_mode->crtc_clock > clock_limit) {
6555 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6556 adjusted_mode->crtc_clock, clock_limit,
6557 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006558 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006559 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006560 }
Chris Wilson89749352010-09-12 18:25:19 +01006561
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006562 /*
6563 * Pipe horizontal size must be even in:
6564 * - DVO ganged mode
6565 * - LVDS dual channel mode
6566 * - Double wide pipe
6567 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006568 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006569 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6570 pipe_config->pipe_src_w &= ~1;
6571
Damien Lespiau8693a822013-05-03 18:48:11 +01006572 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6573 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006574 */
6575 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006576 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006577 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006578
Damien Lespiauf5adf942013-06-24 18:29:34 +01006579 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006580 hsw_compute_ips_config(crtc, pipe_config);
6581
Daniel Vetter877d48d2013-04-19 11:24:43 +02006582 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006583 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006584
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006585 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006586}
6587
Ville Syrjälä1652d192015-03-31 14:12:01 +03006588static int skylake_get_display_clock_speed(struct drm_device *dev)
6589{
6590 struct drm_i915_private *dev_priv = to_i915(dev);
6591 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6592 uint32_t cdctl = I915_READ(CDCLK_CTL);
6593 uint32_t linkrate;
6594
Damien Lespiau414355a2015-06-04 18:21:31 +01006595 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006596 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006597
6598 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6599 return 540000;
6600
6601 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006602 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006603
Damien Lespiau71cd8422015-04-30 16:39:17 +01006604 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6605 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006606 /* vco 8640 */
6607 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6608 case CDCLK_FREQ_450_432:
6609 return 432000;
6610 case CDCLK_FREQ_337_308:
6611 return 308570;
6612 case CDCLK_FREQ_675_617:
6613 return 617140;
6614 default:
6615 WARN(1, "Unknown cd freq selection\n");
6616 }
6617 } else {
6618 /* vco 8100 */
6619 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6620 case CDCLK_FREQ_450_432:
6621 return 450000;
6622 case CDCLK_FREQ_337_308:
6623 return 337500;
6624 case CDCLK_FREQ_675_617:
6625 return 675000;
6626 default:
6627 WARN(1, "Unknown cd freq selection\n");
6628 }
6629 }
6630
6631 /* error case, do as if DPLL0 isn't enabled */
6632 return 24000;
6633}
6634
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006635static int broxton_get_display_clock_speed(struct drm_device *dev)
6636{
6637 struct drm_i915_private *dev_priv = to_i915(dev);
6638 uint32_t cdctl = I915_READ(CDCLK_CTL);
6639 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6640 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6641 int cdclk;
6642
6643 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6644 return 19200;
6645
6646 cdclk = 19200 * pll_ratio / 2;
6647
6648 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6649 case BXT_CDCLK_CD2X_DIV_SEL_1:
6650 return cdclk; /* 576MHz or 624MHz */
6651 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6652 return cdclk * 2 / 3; /* 384MHz */
6653 case BXT_CDCLK_CD2X_DIV_SEL_2:
6654 return cdclk / 2; /* 288MHz */
6655 case BXT_CDCLK_CD2X_DIV_SEL_4:
6656 return cdclk / 4; /* 144MHz */
6657 }
6658
6659 /* error case, do as if DE PLL isn't enabled */
6660 return 19200;
6661}
6662
Ville Syrjälä1652d192015-03-31 14:12:01 +03006663static int broadwell_get_display_clock_speed(struct drm_device *dev)
6664{
6665 struct drm_i915_private *dev_priv = dev->dev_private;
6666 uint32_t lcpll = I915_READ(LCPLL_CTL);
6667 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6668
6669 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6670 return 800000;
6671 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6672 return 450000;
6673 else if (freq == LCPLL_CLK_FREQ_450)
6674 return 450000;
6675 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6676 return 540000;
6677 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6678 return 337500;
6679 else
6680 return 675000;
6681}
6682
6683static int haswell_get_display_clock_speed(struct drm_device *dev)
6684{
6685 struct drm_i915_private *dev_priv = dev->dev_private;
6686 uint32_t lcpll = I915_READ(LCPLL_CTL);
6687 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6688
6689 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6690 return 800000;
6691 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6692 return 450000;
6693 else if (freq == LCPLL_CLK_FREQ_450)
6694 return 450000;
6695 else if (IS_HSW_ULT(dev))
6696 return 337500;
6697 else
6698 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006699}
6700
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006701static int valleyview_get_display_clock_speed(struct drm_device *dev)
6702{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006703 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6704 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006705}
6706
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006707static int ilk_get_display_clock_speed(struct drm_device *dev)
6708{
6709 return 450000;
6710}
6711
Jesse Barnese70236a2009-09-21 10:42:27 -07006712static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006713{
Jesse Barnese70236a2009-09-21 10:42:27 -07006714 return 400000;
6715}
Jesse Barnes79e53942008-11-07 14:24:08 -08006716
Jesse Barnese70236a2009-09-21 10:42:27 -07006717static int i915_get_display_clock_speed(struct drm_device *dev)
6718{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006719 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006720}
Jesse Barnes79e53942008-11-07 14:24:08 -08006721
Jesse Barnese70236a2009-09-21 10:42:27 -07006722static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6723{
6724 return 200000;
6725}
Jesse Barnes79e53942008-11-07 14:24:08 -08006726
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006727static int pnv_get_display_clock_speed(struct drm_device *dev)
6728{
6729 u16 gcfgc = 0;
6730
6731 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6732
6733 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6734 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006735 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006736 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006737 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006738 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006739 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006740 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6741 return 200000;
6742 default:
6743 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6744 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006745 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006746 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006747 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006748 }
6749}
6750
Jesse Barnese70236a2009-09-21 10:42:27 -07006751static int i915gm_get_display_clock_speed(struct drm_device *dev)
6752{
6753 u16 gcfgc = 0;
6754
6755 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6756
6757 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006758 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006759 else {
6760 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6761 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006762 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006763 default:
6764 case GC_DISPLAY_CLOCK_190_200_MHZ:
6765 return 190000;
6766 }
6767 }
6768}
Jesse Barnes79e53942008-11-07 14:24:08 -08006769
Jesse Barnese70236a2009-09-21 10:42:27 -07006770static int i865_get_display_clock_speed(struct drm_device *dev)
6771{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006772 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006773}
6774
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006775static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006776{
6777 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006778
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006779 /*
6780 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6781 * encoding is different :(
6782 * FIXME is this the right way to detect 852GM/852GMV?
6783 */
6784 if (dev->pdev->revision == 0x1)
6785 return 133333;
6786
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006787 pci_bus_read_config_word(dev->pdev->bus,
6788 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6789
Jesse Barnese70236a2009-09-21 10:42:27 -07006790 /* Assume that the hardware is in the high speed state. This
6791 * should be the default.
6792 */
6793 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6794 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006795 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006796 case GC_CLOCK_100_200:
6797 return 200000;
6798 case GC_CLOCK_166_250:
6799 return 250000;
6800 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006801 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006802 case GC_CLOCK_133_266:
6803 case GC_CLOCK_133_266_2:
6804 case GC_CLOCK_166_266:
6805 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006806 }
6807
6808 /* Shouldn't happen */
6809 return 0;
6810}
6811
6812static int i830_get_display_clock_speed(struct drm_device *dev)
6813{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006814 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006815}
6816
Ville Syrjälä34edce22015-05-22 11:22:33 +03006817static unsigned int intel_hpll_vco(struct drm_device *dev)
6818{
6819 struct drm_i915_private *dev_priv = dev->dev_private;
6820 static const unsigned int blb_vco[8] = {
6821 [0] = 3200000,
6822 [1] = 4000000,
6823 [2] = 5333333,
6824 [3] = 4800000,
6825 [4] = 6400000,
6826 };
6827 static const unsigned int pnv_vco[8] = {
6828 [0] = 3200000,
6829 [1] = 4000000,
6830 [2] = 5333333,
6831 [3] = 4800000,
6832 [4] = 2666667,
6833 };
6834 static const unsigned int cl_vco[8] = {
6835 [0] = 3200000,
6836 [1] = 4000000,
6837 [2] = 5333333,
6838 [3] = 6400000,
6839 [4] = 3333333,
6840 [5] = 3566667,
6841 [6] = 4266667,
6842 };
6843 static const unsigned int elk_vco[8] = {
6844 [0] = 3200000,
6845 [1] = 4000000,
6846 [2] = 5333333,
6847 [3] = 4800000,
6848 };
6849 static const unsigned int ctg_vco[8] = {
6850 [0] = 3200000,
6851 [1] = 4000000,
6852 [2] = 5333333,
6853 [3] = 6400000,
6854 [4] = 2666667,
6855 [5] = 4266667,
6856 };
6857 const unsigned int *vco_table;
6858 unsigned int vco;
6859 uint8_t tmp = 0;
6860
6861 /* FIXME other chipsets? */
6862 if (IS_GM45(dev))
6863 vco_table = ctg_vco;
6864 else if (IS_G4X(dev))
6865 vco_table = elk_vco;
6866 else if (IS_CRESTLINE(dev))
6867 vco_table = cl_vco;
6868 else if (IS_PINEVIEW(dev))
6869 vco_table = pnv_vco;
6870 else if (IS_G33(dev))
6871 vco_table = blb_vco;
6872 else
6873 return 0;
6874
6875 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6876
6877 vco = vco_table[tmp & 0x7];
6878 if (vco == 0)
6879 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6880 else
6881 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6882
6883 return vco;
6884}
6885
6886static int gm45_get_display_clock_speed(struct drm_device *dev)
6887{
6888 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6889 uint16_t tmp = 0;
6890
6891 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6892
6893 cdclk_sel = (tmp >> 12) & 0x1;
6894
6895 switch (vco) {
6896 case 2666667:
6897 case 4000000:
6898 case 5333333:
6899 return cdclk_sel ? 333333 : 222222;
6900 case 3200000:
6901 return cdclk_sel ? 320000 : 228571;
6902 default:
6903 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6904 return 222222;
6905 }
6906}
6907
6908static int i965gm_get_display_clock_speed(struct drm_device *dev)
6909{
6910 static const uint8_t div_3200[] = { 16, 10, 8 };
6911 static const uint8_t div_4000[] = { 20, 12, 10 };
6912 static const uint8_t div_5333[] = { 24, 16, 14 };
6913 const uint8_t *div_table;
6914 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6915 uint16_t tmp = 0;
6916
6917 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6918
6919 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6920
6921 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6922 goto fail;
6923
6924 switch (vco) {
6925 case 3200000:
6926 div_table = div_3200;
6927 break;
6928 case 4000000:
6929 div_table = div_4000;
6930 break;
6931 case 5333333:
6932 div_table = div_5333;
6933 break;
6934 default:
6935 goto fail;
6936 }
6937
6938 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6939
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006940fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006941 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6942 return 200000;
6943}
6944
6945static int g33_get_display_clock_speed(struct drm_device *dev)
6946{
6947 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6948 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6949 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6950 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6951 const uint8_t *div_table;
6952 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6953 uint16_t tmp = 0;
6954
6955 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6956
6957 cdclk_sel = (tmp >> 4) & 0x7;
6958
6959 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6960 goto fail;
6961
6962 switch (vco) {
6963 case 3200000:
6964 div_table = div_3200;
6965 break;
6966 case 4000000:
6967 div_table = div_4000;
6968 break;
6969 case 4800000:
6970 div_table = div_4800;
6971 break;
6972 case 5333333:
6973 div_table = div_5333;
6974 break;
6975 default:
6976 goto fail;
6977 }
6978
6979 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6980
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006981fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006982 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6983 return 190476;
6984}
6985
Zhenyu Wang2c072452009-06-05 15:38:42 +08006986static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006987intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006988{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006989 while (*num > DATA_LINK_M_N_MASK ||
6990 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006991 *num >>= 1;
6992 *den >>= 1;
6993 }
6994}
6995
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006996static void compute_m_n(unsigned int m, unsigned int n,
6997 uint32_t *ret_m, uint32_t *ret_n)
6998{
6999 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7000 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7001 intel_reduce_m_n_ratio(ret_m, ret_n);
7002}
7003
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007004void
7005intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7006 int pixel_clock, int link_clock,
7007 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007008{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007009 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007010
7011 compute_m_n(bits_per_pixel * pixel_clock,
7012 link_clock * nlanes * 8,
7013 &m_n->gmch_m, &m_n->gmch_n);
7014
7015 compute_m_n(pixel_clock, link_clock,
7016 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007017}
7018
Chris Wilsona7615032011-01-12 17:04:08 +00007019static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7020{
Jani Nikulad330a952014-01-21 11:24:25 +02007021 if (i915.panel_use_ssc >= 0)
7022 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007023 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007024 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007025}
7026
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007027static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007028{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007029 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007030}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007031
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007032static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7033{
7034 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007035}
7036
Daniel Vetterf47709a2013-03-28 10:42:02 +01007037static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007038 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007039 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08007040{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007041 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007042 u32 fp, fp2 = 0;
7043
7044 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007045 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007046 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007047 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007048 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007049 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007050 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007051 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007052 }
7053
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007054 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007055
Daniel Vetterf47709a2013-03-28 10:42:02 +01007056 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007057 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007058 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007059 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007060 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007061 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007062 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007063 }
7064}
7065
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007066static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7067 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007068{
7069 u32 reg_val;
7070
7071 /*
7072 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7073 * and set it to a reasonable value instead.
7074 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007075 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007076 reg_val &= 0xffffff00;
7077 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007078 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007079
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007080 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007081 reg_val &= 0x8cffffff;
7082 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007083 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007084
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007085 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007086 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007087 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007088
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007089 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007090 reg_val &= 0x00ffffff;
7091 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007092 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007093}
7094
Daniel Vetterb5518422013-05-03 11:49:48 +02007095static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7096 struct intel_link_m_n *m_n)
7097{
7098 struct drm_device *dev = crtc->base.dev;
7099 struct drm_i915_private *dev_priv = dev->dev_private;
7100 int pipe = crtc->pipe;
7101
Daniel Vettere3b95f12013-05-03 11:49:49 +02007102 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7103 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7104 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7105 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007106}
7107
7108static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007109 struct intel_link_m_n *m_n,
7110 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007111{
7112 struct drm_device *dev = crtc->base.dev;
7113 struct drm_i915_private *dev_priv = dev->dev_private;
7114 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007115 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007116
7117 if (INTEL_INFO(dev)->gen >= 5) {
7118 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7119 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7120 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7121 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007122 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7123 * for gen < 8) and if DRRS is supported (to make sure the
7124 * registers are not unnecessarily accessed).
7125 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307126 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007127 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007128 I915_WRITE(PIPE_DATA_M2(transcoder),
7129 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7130 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7131 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7132 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7133 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007134 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007135 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7136 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7137 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7138 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007139 }
7140}
7141
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307142void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007143{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307144 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7145
7146 if (m_n == M1_N1) {
7147 dp_m_n = &crtc->config->dp_m_n;
7148 dp_m2_n2 = &crtc->config->dp_m2_n2;
7149 } else if (m_n == M2_N2) {
7150
7151 /*
7152 * M2_N2 registers are not supported. Hence m2_n2 divider value
7153 * needs to be programmed into M1_N1.
7154 */
7155 dp_m_n = &crtc->config->dp_m2_n2;
7156 } else {
7157 DRM_ERROR("Unsupported divider value\n");
7158 return;
7159 }
7160
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007161 if (crtc->config->has_pch_encoder)
7162 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007163 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307164 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007165}
7166
Daniel Vetter251ac862015-06-18 10:30:24 +02007167static void vlv_compute_dpll(struct intel_crtc *crtc,
7168 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007169{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007170 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007171 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007172 if (crtc->pipe != PIPE_A)
7173 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007174
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007175 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjälä187a1c02016-04-18 20:34:04 +03007176 if (!pipe_config->has_dsi_encoder)
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007177 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7178 DPLL_EXT_BUFFER_ENABLE_VLV;
7179
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007180 pipe_config->dpll_hw_state.dpll_md =
7181 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7182}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007183
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007184static void chv_compute_dpll(struct intel_crtc *crtc,
7185 struct intel_crtc_state *pipe_config)
7186{
7187 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007188 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007189 if (crtc->pipe != PIPE_A)
7190 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7191
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007192 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjälä187a1c02016-04-18 20:34:04 +03007193 if (!pipe_config->has_dsi_encoder)
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007194 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7195
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007196 pipe_config->dpll_hw_state.dpll_md =
7197 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007198}
7199
Ville Syrjäläd288f652014-10-28 13:20:22 +02007200static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007201 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007202{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007203 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007204 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007205 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007206 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007207 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007208 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007209
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007210 /* Enable Refclk */
7211 I915_WRITE(DPLL(pipe),
7212 pipe_config->dpll_hw_state.dpll &
7213 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7214
7215 /* No need to actually set up the DPLL with DSI */
7216 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7217 return;
7218
Ville Syrjäläa5805162015-05-26 20:42:30 +03007219 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007220
Ville Syrjäläd288f652014-10-28 13:20:22 +02007221 bestn = pipe_config->dpll.n;
7222 bestm1 = pipe_config->dpll.m1;
7223 bestm2 = pipe_config->dpll.m2;
7224 bestp1 = pipe_config->dpll.p1;
7225 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007226
Jesse Barnes89b667f2013-04-18 14:51:36 -07007227 /* See eDP HDMI DPIO driver vbios notes doc */
7228
7229 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007230 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007231 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007232
7233 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007234 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007235
7236 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007237 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007238 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007239 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007240
7241 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007242 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007243
7244 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007245 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7246 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7247 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007248 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007249
7250 /*
7251 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7252 * but we don't support that).
7253 * Note: don't use the DAC post divider as it seems unstable.
7254 */
7255 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007256 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007257
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007258 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007259 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007260
Jesse Barnes89b667f2013-04-18 14:51:36 -07007261 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007262 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007263 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7264 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007265 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007266 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007267 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007268 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007269 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007270
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007271 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007272 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007273 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007274 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007275 0x0df40000);
7276 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007277 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007278 0x0df70000);
7279 } else { /* HDMI or VGA */
7280 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007281 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007282 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007283 0x0df70000);
7284 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007285 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007286 0x0df40000);
7287 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007288
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007289 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007290 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007291 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7292 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007293 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007294 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007295
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007296 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007297 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007298}
7299
Ville Syrjäläd288f652014-10-28 13:20:22 +02007300static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007301 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007302{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007303 struct drm_device *dev = crtc->base.dev;
7304 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007305 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007306 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307307 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007308 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307309 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307310 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007311
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007312 /* Enable Refclk and SSC */
7313 I915_WRITE(DPLL(pipe),
7314 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7315
7316 /* No need to actually set up the DPLL with DSI */
7317 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7318 return;
7319
Ville Syrjäläd288f652014-10-28 13:20:22 +02007320 bestn = pipe_config->dpll.n;
7321 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7322 bestm1 = pipe_config->dpll.m1;
7323 bestm2 = pipe_config->dpll.m2 >> 22;
7324 bestp1 = pipe_config->dpll.p1;
7325 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307326 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307327 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307328 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007329
Ville Syrjäläa5805162015-05-26 20:42:30 +03007330 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007331
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007332 /* p1 and p2 divider */
7333 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7334 5 << DPIO_CHV_S1_DIV_SHIFT |
7335 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7336 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7337 1 << DPIO_CHV_K_DIV_SHIFT);
7338
7339 /* Feedback post-divider - m2 */
7340 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7341
7342 /* Feedback refclk divider - n and m1 */
7343 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7344 DPIO_CHV_M1_DIV_BY_2 |
7345 1 << DPIO_CHV_N_DIV_SHIFT);
7346
7347 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007348 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007349
7350 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307351 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7352 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7353 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7354 if (bestm2_frac)
7355 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7356 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007357
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307358 /* Program digital lock detect threshold */
7359 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7360 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7361 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7362 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7363 if (!bestm2_frac)
7364 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7365 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7366
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007367 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307368 if (vco == 5400000) {
7369 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7370 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7371 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7372 tribuf_calcntr = 0x9;
7373 } else if (vco <= 6200000) {
7374 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7375 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7376 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7377 tribuf_calcntr = 0x9;
7378 } else if (vco <= 6480000) {
7379 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7380 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7381 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7382 tribuf_calcntr = 0x8;
7383 } else {
7384 /* Not supported. Apply the same limits as in the max case */
7385 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7386 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7387 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7388 tribuf_calcntr = 0;
7389 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007390 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7391
Ville Syrjälä968040b2015-03-11 22:52:08 +02007392 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307393 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7394 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7395 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7396
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007397 /* AFC Recal */
7398 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7399 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7400 DPIO_AFC_RECAL);
7401
Ville Syrjäläa5805162015-05-26 20:42:30 +03007402 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007403}
7404
Ville Syrjäläd288f652014-10-28 13:20:22 +02007405/**
7406 * vlv_force_pll_on - forcibly enable just the PLL
7407 * @dev_priv: i915 private structure
7408 * @pipe: pipe PLL to enable
7409 * @dpll: PLL configuration
7410 *
7411 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7412 * in cases where we need the PLL enabled even when @pipe is not going to
7413 * be enabled.
7414 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007415int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7416 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007417{
7418 struct intel_crtc *crtc =
7419 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007420 struct intel_crtc_state *pipe_config;
7421
7422 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7423 if (!pipe_config)
7424 return -ENOMEM;
7425
7426 pipe_config->base.crtc = &crtc->base;
7427 pipe_config->pixel_multiplier = 1;
7428 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007429
7430 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007431 chv_compute_dpll(crtc, pipe_config);
7432 chv_prepare_pll(crtc, pipe_config);
7433 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007434 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007435 vlv_compute_dpll(crtc, pipe_config);
7436 vlv_prepare_pll(crtc, pipe_config);
7437 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007438 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007439
7440 kfree(pipe_config);
7441
7442 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007443}
7444
7445/**
7446 * vlv_force_pll_off - forcibly disable just the PLL
7447 * @dev_priv: i915 private structure
7448 * @pipe: pipe PLL to disable
7449 *
7450 * Disable the PLL for @pipe. To be used in cases where we need
7451 * the PLL enabled even when @pipe is not going to be enabled.
7452 */
7453void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7454{
7455 if (IS_CHERRYVIEW(dev))
7456 chv_disable_pll(to_i915(dev), pipe);
7457 else
7458 vlv_disable_pll(to_i915(dev), pipe);
7459}
7460
Daniel Vetter251ac862015-06-18 10:30:24 +02007461static void i9xx_compute_dpll(struct intel_crtc *crtc,
7462 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007463 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007464{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007465 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007466 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007467 u32 dpll;
7468 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007469 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007470
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007471 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307472
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007473 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7474 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007475
7476 dpll = DPLL_VGA_MODE_DIS;
7477
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007478 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007479 dpll |= DPLLB_MODE_LVDS;
7480 else
7481 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007482
Daniel Vetteref1b4602013-06-01 17:17:04 +02007483 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007484 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007485 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007486 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007487
7488 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007489 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007490
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007491 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007492 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007493
7494 /* compute bitmask from p1 value */
7495 if (IS_PINEVIEW(dev))
7496 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7497 else {
7498 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7499 if (IS_G4X(dev) && reduced_clock)
7500 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7501 }
7502 switch (clock->p2) {
7503 case 5:
7504 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7505 break;
7506 case 7:
7507 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7508 break;
7509 case 10:
7510 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7511 break;
7512 case 14:
7513 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7514 break;
7515 }
7516 if (INTEL_INFO(dev)->gen >= 4)
7517 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7518
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007519 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007520 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007521 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007522 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007523 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7524 else
7525 dpll |= PLL_REF_INPUT_DREFCLK;
7526
7527 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007528 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007529
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007530 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007531 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007532 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007533 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007534 }
7535}
7536
Daniel Vetter251ac862015-06-18 10:30:24 +02007537static void i8xx_compute_dpll(struct intel_crtc *crtc,
7538 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007539 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007540{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007541 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007542 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007543 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007544 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007545
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007546 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307547
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007548 dpll = DPLL_VGA_MODE_DIS;
7549
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007550 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007551 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7552 } else {
7553 if (clock->p1 == 2)
7554 dpll |= PLL_P1_DIVIDE_BY_TWO;
7555 else
7556 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7557 if (clock->p2 == 4)
7558 dpll |= PLL_P2_DIVIDE_BY_4;
7559 }
7560
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007561 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007562 dpll |= DPLL_DVO_2X_MODE;
7563
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007564 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007565 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007566 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7567 else
7568 dpll |= PLL_REF_INPUT_DREFCLK;
7569
7570 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007571 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007572}
7573
Daniel Vetter8a654f32013-06-01 17:16:22 +02007574static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007575{
7576 struct drm_device *dev = intel_crtc->base.dev;
7577 struct drm_i915_private *dev_priv = dev->dev_private;
7578 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007579 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007580 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007581 uint32_t crtc_vtotal, crtc_vblank_end;
7582 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007583
7584 /* We need to be careful not to changed the adjusted mode, for otherwise
7585 * the hw state checker will get angry at the mismatch. */
7586 crtc_vtotal = adjusted_mode->crtc_vtotal;
7587 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007588
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007589 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007590 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007591 crtc_vtotal -= 1;
7592 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007593
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007594 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007595 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7596 else
7597 vsyncshift = adjusted_mode->crtc_hsync_start -
7598 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007599 if (vsyncshift < 0)
7600 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007601 }
7602
7603 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007604 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007605
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007606 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007607 (adjusted_mode->crtc_hdisplay - 1) |
7608 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007609 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007610 (adjusted_mode->crtc_hblank_start - 1) |
7611 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007612 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007613 (adjusted_mode->crtc_hsync_start - 1) |
7614 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7615
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007616 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007617 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007618 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007619 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007620 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007621 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007622 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007623 (adjusted_mode->crtc_vsync_start - 1) |
7624 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7625
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007626 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7627 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7628 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7629 * bits. */
7630 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7631 (pipe == PIPE_B || pipe == PIPE_C))
7632 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7633
Jani Nikulabc58be62016-03-18 17:05:39 +02007634}
7635
7636static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7637{
7638 struct drm_device *dev = intel_crtc->base.dev;
7639 struct drm_i915_private *dev_priv = dev->dev_private;
7640 enum pipe pipe = intel_crtc->pipe;
7641
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007642 /* pipesrc controls the size that is scaled from, which should
7643 * always be the user's requested size.
7644 */
7645 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007646 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7647 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007648}
7649
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007650static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007651 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007652{
7653 struct drm_device *dev = crtc->base.dev;
7654 struct drm_i915_private *dev_priv = dev->dev_private;
7655 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7656 uint32_t tmp;
7657
7658 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007659 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7660 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007661 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007662 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7663 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007664 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007665 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7666 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007667
7668 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007669 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7670 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007671 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007672 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7673 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007674 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007675 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7676 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007677
7678 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007679 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7680 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7681 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007682 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007683}
7684
7685static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7686 struct intel_crtc_state *pipe_config)
7687{
7688 struct drm_device *dev = crtc->base.dev;
7689 struct drm_i915_private *dev_priv = dev->dev_private;
7690 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007691
7692 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007693 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7694 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7695
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007696 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7697 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007698}
7699
Daniel Vetterf6a83282014-02-11 15:28:57 -08007700void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007701 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007702{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007703 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7704 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7705 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7706 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007707
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007708 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7709 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7710 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7711 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007712
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007713 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007714 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007715
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007716 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7717 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007718
7719 mode->hsync = drm_mode_hsync(mode);
7720 mode->vrefresh = drm_mode_vrefresh(mode);
7721 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007722}
7723
Daniel Vetter84b046f2013-02-19 18:48:54 +01007724static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7725{
7726 struct drm_device *dev = intel_crtc->base.dev;
7727 struct drm_i915_private *dev_priv = dev->dev_private;
7728 uint32_t pipeconf;
7729
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007730 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007731
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007732 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7733 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7734 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007735
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007736 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007737 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007738
Daniel Vetterff9ce462013-04-24 14:57:17 +02007739 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007740 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007741 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007742 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007743 pipeconf |= PIPECONF_DITHER_EN |
7744 PIPECONF_DITHER_TYPE_SP;
7745
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007746 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007747 case 18:
7748 pipeconf |= PIPECONF_6BPC;
7749 break;
7750 case 24:
7751 pipeconf |= PIPECONF_8BPC;
7752 break;
7753 case 30:
7754 pipeconf |= PIPECONF_10BPC;
7755 break;
7756 default:
7757 /* Case prevented by intel_choose_pipe_bpp_dither. */
7758 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007759 }
7760 }
7761
7762 if (HAS_PIPE_CXSR(dev)) {
7763 if (intel_crtc->lowfreq_avail) {
7764 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7765 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7766 } else {
7767 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007768 }
7769 }
7770
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007771 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007772 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007773 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007774 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7775 else
7776 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7777 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007778 pipeconf |= PIPECONF_PROGRESSIVE;
7779
Wayne Boyer666a4532015-12-09 12:29:35 -08007780 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7781 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007782 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007783
Daniel Vetter84b046f2013-02-19 18:48:54 +01007784 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7785 POSTING_READ(PIPECONF(intel_crtc->pipe));
7786}
7787
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007788static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7789 struct intel_crtc_state *crtc_state)
7790{
7791 struct drm_device *dev = crtc->base.dev;
7792 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007793 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007794 int refclk = 48000;
7795
7796 memset(&crtc_state->dpll_hw_state, 0,
7797 sizeof(crtc_state->dpll_hw_state));
7798
7799 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7800 if (intel_panel_use_ssc(dev_priv)) {
7801 refclk = dev_priv->vbt.lvds_ssc_freq;
7802 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7803 }
7804
7805 limit = &intel_limits_i8xx_lvds;
7806 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7807 limit = &intel_limits_i8xx_dvo;
7808 } else {
7809 limit = &intel_limits_i8xx_dac;
7810 }
7811
7812 if (!crtc_state->clock_set &&
7813 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7814 refclk, NULL, &crtc_state->dpll)) {
7815 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7816 return -EINVAL;
7817 }
7818
7819 i8xx_compute_dpll(crtc, crtc_state, NULL);
7820
7821 return 0;
7822}
7823
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007824static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7825 struct intel_crtc_state *crtc_state)
7826{
7827 struct drm_device *dev = crtc->base.dev;
7828 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007829 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007830 int refclk = 96000;
7831
7832 memset(&crtc_state->dpll_hw_state, 0,
7833 sizeof(crtc_state->dpll_hw_state));
7834
7835 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7836 if (intel_panel_use_ssc(dev_priv)) {
7837 refclk = dev_priv->vbt.lvds_ssc_freq;
7838 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7839 }
7840
7841 if (intel_is_dual_link_lvds(dev))
7842 limit = &intel_limits_g4x_dual_channel_lvds;
7843 else
7844 limit = &intel_limits_g4x_single_channel_lvds;
7845 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7846 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7847 limit = &intel_limits_g4x_hdmi;
7848 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7849 limit = &intel_limits_g4x_sdvo;
7850 } else {
7851 /* The option is for other outputs */
7852 limit = &intel_limits_i9xx_sdvo;
7853 }
7854
7855 if (!crtc_state->clock_set &&
7856 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7857 refclk, NULL, &crtc_state->dpll)) {
7858 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7859 return -EINVAL;
7860 }
7861
7862 i9xx_compute_dpll(crtc, crtc_state, NULL);
7863
7864 return 0;
7865}
7866
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007867static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7868 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007869{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007870 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007871 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007872 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007873 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007874
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007875 memset(&crtc_state->dpll_hw_state, 0,
7876 sizeof(crtc_state->dpll_hw_state));
7877
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007878 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7879 if (intel_panel_use_ssc(dev_priv)) {
7880 refclk = dev_priv->vbt.lvds_ssc_freq;
7881 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7882 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007883
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007884 limit = &intel_limits_pineview_lvds;
7885 } else {
7886 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007887 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007888
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007889 if (!crtc_state->clock_set &&
7890 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7891 refclk, NULL, &crtc_state->dpll)) {
7892 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7893 return -EINVAL;
7894 }
7895
7896 i9xx_compute_dpll(crtc, crtc_state, NULL);
7897
7898 return 0;
7899}
7900
7901static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7902 struct intel_crtc_state *crtc_state)
7903{
7904 struct drm_device *dev = crtc->base.dev;
7905 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007906 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007907 int refclk = 96000;
7908
7909 memset(&crtc_state->dpll_hw_state, 0,
7910 sizeof(crtc_state->dpll_hw_state));
7911
7912 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7913 if (intel_panel_use_ssc(dev_priv)) {
7914 refclk = dev_priv->vbt.lvds_ssc_freq;
7915 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007916 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007917
7918 limit = &intel_limits_i9xx_lvds;
7919 } else {
7920 limit = &intel_limits_i9xx_sdvo;
7921 }
7922
7923 if (!crtc_state->clock_set &&
7924 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7925 refclk, NULL, &crtc_state->dpll)) {
7926 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7927 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007928 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007929
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007930 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007931
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007932 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007933}
7934
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007935static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7936 struct intel_crtc_state *crtc_state)
7937{
7938 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007939 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007940
7941 memset(&crtc_state->dpll_hw_state, 0,
7942 sizeof(crtc_state->dpll_hw_state));
7943
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007944 if (!crtc_state->clock_set &&
7945 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7946 refclk, NULL, &crtc_state->dpll)) {
7947 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7948 return -EINVAL;
7949 }
7950
7951 chv_compute_dpll(crtc, crtc_state);
7952
7953 return 0;
7954}
7955
7956static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7957 struct intel_crtc_state *crtc_state)
7958{
7959 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007960 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007961
7962 memset(&crtc_state->dpll_hw_state, 0,
7963 sizeof(crtc_state->dpll_hw_state));
7964
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007965 if (!crtc_state->clock_set &&
7966 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7967 refclk, NULL, &crtc_state->dpll)) {
7968 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7969 return -EINVAL;
7970 }
7971
7972 vlv_compute_dpll(crtc, crtc_state);
7973
7974 return 0;
7975}
7976
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007977static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007978 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007979{
7980 struct drm_device *dev = crtc->base.dev;
7981 struct drm_i915_private *dev_priv = dev->dev_private;
7982 uint32_t tmp;
7983
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007984 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7985 return;
7986
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007987 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007988 if (!(tmp & PFIT_ENABLE))
7989 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007990
Daniel Vetter06922822013-07-11 13:35:40 +02007991 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007992 if (INTEL_INFO(dev)->gen < 4) {
7993 if (crtc->pipe != PIPE_B)
7994 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007995 } else {
7996 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7997 return;
7998 }
7999
Daniel Vetter06922822013-07-11 13:35:40 +02008000 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008001 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008002}
8003
Jesse Barnesacbec812013-09-20 11:29:32 -07008004static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008005 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008006{
8007 struct drm_device *dev = crtc->base.dev;
8008 struct drm_i915_private *dev_priv = dev->dev_private;
8009 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008010 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07008011 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008012 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008013
Ville Syrjäläb5219732016-03-15 16:40:01 +02008014 /* In case of DSI, DPLL will not be used */
8015 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308016 return;
8017
Ville Syrjäläa5805162015-05-26 20:42:30 +03008018 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008019 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008020 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008021
8022 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8023 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8024 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8025 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8026 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8027
Imre Deakdccbea32015-06-22 23:35:51 +03008028 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008029}
8030
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008031static void
8032i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8033 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008034{
8035 struct drm_device *dev = crtc->base.dev;
8036 struct drm_i915_private *dev_priv = dev->dev_private;
8037 u32 val, base, offset;
8038 int pipe = crtc->pipe, plane = crtc->plane;
8039 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008040 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008041 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008042 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008043
Damien Lespiau42a7b082015-02-05 19:35:13 +00008044 val = I915_READ(DSPCNTR(plane));
8045 if (!(val & DISPLAY_PLANE_ENABLE))
8046 return;
8047
Damien Lespiaud9806c92015-01-21 14:07:19 +00008048 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008049 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008050 DRM_DEBUG_KMS("failed to alloc fb\n");
8051 return;
8052 }
8053
Damien Lespiau1b842c82015-01-21 13:50:54 +00008054 fb = &intel_fb->base;
8055
Daniel Vetter18c52472015-02-10 17:16:09 +00008056 if (INTEL_INFO(dev)->gen >= 4) {
8057 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008058 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008059 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8060 }
8061 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008062
8063 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008064 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008065 fb->pixel_format = fourcc;
8066 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008067
8068 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008069 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008070 offset = I915_READ(DSPTILEOFF(plane));
8071 else
8072 offset = I915_READ(DSPLINOFF(plane));
8073 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8074 } else {
8075 base = I915_READ(DSPADDR(plane));
8076 }
8077 plane_config->base = base;
8078
8079 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008080 fb->width = ((val >> 16) & 0xfff) + 1;
8081 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008082
8083 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008084 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008085
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008086 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008087 fb->pixel_format,
8088 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008089
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008090 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008091
Damien Lespiau2844a922015-01-20 12:51:48 +00008092 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8093 pipe_name(pipe), plane, fb->width, fb->height,
8094 fb->bits_per_pixel, base, fb->pitches[0],
8095 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008096
Damien Lespiau2d140302015-02-05 17:22:18 +00008097 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008098}
8099
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008100static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008101 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008102{
8103 struct drm_device *dev = crtc->base.dev;
8104 struct drm_i915_private *dev_priv = dev->dev_private;
8105 int pipe = pipe_config->cpu_transcoder;
8106 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008107 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008108 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008109 int refclk = 100000;
8110
Ville Syrjäläb5219732016-03-15 16:40:01 +02008111 /* In case of DSI, DPLL will not be used */
8112 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8113 return;
8114
Ville Syrjäläa5805162015-05-26 20:42:30 +03008115 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008116 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8117 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8118 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8119 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008120 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008121 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008122
8123 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008124 clock.m2 = (pll_dw0 & 0xff) << 22;
8125 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8126 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008127 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8128 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8129 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8130
Imre Deakdccbea32015-06-22 23:35:51 +03008131 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008132}
8133
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008134static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008135 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008136{
8137 struct drm_device *dev = crtc->base.dev;
8138 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008139 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008140 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008141 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008142
Imre Deak17290502016-02-12 18:55:11 +02008143 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8144 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008145 return false;
8146
Daniel Vettere143a212013-07-04 12:01:15 +02008147 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008148 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008149
Imre Deak17290502016-02-12 18:55:11 +02008150 ret = false;
8151
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008152 tmp = I915_READ(PIPECONF(crtc->pipe));
8153 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008154 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008155
Wayne Boyer666a4532015-12-09 12:29:35 -08008156 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008157 switch (tmp & PIPECONF_BPC_MASK) {
8158 case PIPECONF_6BPC:
8159 pipe_config->pipe_bpp = 18;
8160 break;
8161 case PIPECONF_8BPC:
8162 pipe_config->pipe_bpp = 24;
8163 break;
8164 case PIPECONF_10BPC:
8165 pipe_config->pipe_bpp = 30;
8166 break;
8167 default:
8168 break;
8169 }
8170 }
8171
Wayne Boyer666a4532015-12-09 12:29:35 -08008172 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8173 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008174 pipe_config->limited_color_range = true;
8175
Ville Syrjälä282740f2013-09-04 18:30:03 +03008176 if (INTEL_INFO(dev)->gen < 4)
8177 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8178
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008179 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008180 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008181
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008182 i9xx_get_pfit_config(crtc, pipe_config);
8183
Daniel Vetter6c49f242013-06-06 12:45:25 +02008184 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008185 /* No way to read it out on pipes B and C */
8186 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8187 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8188 else
8189 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008190 pipe_config->pixel_multiplier =
8191 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8192 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008193 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008194 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8195 tmp = I915_READ(DPLL(crtc->pipe));
8196 pipe_config->pixel_multiplier =
8197 ((tmp & SDVO_MULTIPLIER_MASK)
8198 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8199 } else {
8200 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8201 * port and will be fixed up in the encoder->get_config
8202 * function. */
8203 pipe_config->pixel_multiplier = 1;
8204 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008205 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008206 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008207 /*
8208 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8209 * on 830. Filter it out here so that we don't
8210 * report errors due to that.
8211 */
8212 if (IS_I830(dev))
8213 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8214
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008215 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8216 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008217 } else {
8218 /* Mask out read-only status bits. */
8219 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8220 DPLL_PORTC_READY_MASK |
8221 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008222 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008223
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008224 if (IS_CHERRYVIEW(dev))
8225 chv_crtc_clock_get(crtc, pipe_config);
8226 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008227 vlv_crtc_clock_get(crtc, pipe_config);
8228 else
8229 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008230
Ville Syrjälä0f646142015-08-26 19:39:18 +03008231 /*
8232 * Normally the dotclock is filled in by the encoder .get_config()
8233 * but in case the pipe is enabled w/o any ports we need a sane
8234 * default.
8235 */
8236 pipe_config->base.adjusted_mode.crtc_clock =
8237 pipe_config->port_clock / pipe_config->pixel_multiplier;
8238
Imre Deak17290502016-02-12 18:55:11 +02008239 ret = true;
8240
8241out:
8242 intel_display_power_put(dev_priv, power_domain);
8243
8244 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008245}
8246
Paulo Zanonidde86e22012-12-01 12:04:25 -02008247static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008248{
8249 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008250 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008251 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008252 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008253 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008254 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008255 bool has_ck505 = false;
8256 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008257
8258 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008259 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008260 switch (encoder->type) {
8261 case INTEL_OUTPUT_LVDS:
8262 has_panel = true;
8263 has_lvds = true;
8264 break;
8265 case INTEL_OUTPUT_EDP:
8266 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008267 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008268 has_cpu_edp = true;
8269 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008270 default:
8271 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008272 }
8273 }
8274
Keith Packard99eb6a02011-09-26 14:29:12 -07008275 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008276 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008277 can_ssc = has_ck505;
8278 } else {
8279 has_ck505 = false;
8280 can_ssc = true;
8281 }
8282
Imre Deak2de69052013-05-08 13:14:04 +03008283 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8284 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008285
8286 /* Ironlake: try to setup display ref clock before DPLL
8287 * enabling. This is only under driver's control after
8288 * PCH B stepping, previous chipset stepping should be
8289 * ignoring this setting.
8290 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008291 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008292
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008293 /* As we must carefully and slowly disable/enable each source in turn,
8294 * compute the final state we want first and check if we need to
8295 * make any changes at all.
8296 */
8297 final = val;
8298 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008299 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008300 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008301 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008302 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8303
8304 final &= ~DREF_SSC_SOURCE_MASK;
8305 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8306 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008307
Keith Packard199e5d72011-09-22 12:01:57 -07008308 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008309 final |= DREF_SSC_SOURCE_ENABLE;
8310
8311 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8312 final |= DREF_SSC1_ENABLE;
8313
8314 if (has_cpu_edp) {
8315 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8316 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8317 else
8318 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8319 } else
8320 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8321 } else {
8322 final |= DREF_SSC_SOURCE_DISABLE;
8323 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8324 }
8325
8326 if (final == val)
8327 return;
8328
8329 /* Always enable nonspread source */
8330 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8331
8332 if (has_ck505)
8333 val |= DREF_NONSPREAD_CK505_ENABLE;
8334 else
8335 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8336
8337 if (has_panel) {
8338 val &= ~DREF_SSC_SOURCE_MASK;
8339 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008340
Keith Packard199e5d72011-09-22 12:01:57 -07008341 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008342 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008343 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008344 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008345 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008346 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008347
8348 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008349 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008350 POSTING_READ(PCH_DREF_CONTROL);
8351 udelay(200);
8352
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008353 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008354
8355 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008356 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008357 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008358 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008359 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008360 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008361 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008362 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008363 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008364
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008365 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008366 POSTING_READ(PCH_DREF_CONTROL);
8367 udelay(200);
8368 } else {
8369 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8370
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008371 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008372
8373 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008374 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008375
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008376 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008377 POSTING_READ(PCH_DREF_CONTROL);
8378 udelay(200);
8379
8380 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008381 val &= ~DREF_SSC_SOURCE_MASK;
8382 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008383
8384 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008385 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008386
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008387 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008388 POSTING_READ(PCH_DREF_CONTROL);
8389 udelay(200);
8390 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008391
8392 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008393}
8394
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008395static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008396{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008397 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008398
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008399 tmp = I915_READ(SOUTH_CHICKEN2);
8400 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8401 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008402
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008403 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8404 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8405 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008406
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008407 tmp = I915_READ(SOUTH_CHICKEN2);
8408 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8409 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008410
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008411 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8412 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8413 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008414}
8415
8416/* WaMPhyProgramming:hsw */
8417static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8418{
8419 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008420
8421 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8422 tmp &= ~(0xFF << 24);
8423 tmp |= (0x12 << 24);
8424 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8425
Paulo Zanonidde86e22012-12-01 12:04:25 -02008426 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8427 tmp |= (1 << 11);
8428 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8429
8430 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8431 tmp |= (1 << 11);
8432 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8433
Paulo Zanonidde86e22012-12-01 12:04:25 -02008434 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8435 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8436 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8437
8438 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8439 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8440 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8441
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008442 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8443 tmp &= ~(7 << 13);
8444 tmp |= (5 << 13);
8445 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008446
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008447 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8448 tmp &= ~(7 << 13);
8449 tmp |= (5 << 13);
8450 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008451
8452 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8453 tmp &= ~0xFF;
8454 tmp |= 0x1C;
8455 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8456
8457 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8458 tmp &= ~0xFF;
8459 tmp |= 0x1C;
8460 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8461
8462 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8463 tmp &= ~(0xFF << 16);
8464 tmp |= (0x1C << 16);
8465 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8466
8467 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8468 tmp &= ~(0xFF << 16);
8469 tmp |= (0x1C << 16);
8470 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8471
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008472 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8473 tmp |= (1 << 27);
8474 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008475
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008476 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8477 tmp |= (1 << 27);
8478 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008479
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008480 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8481 tmp &= ~(0xF << 28);
8482 tmp |= (4 << 28);
8483 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008484
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008485 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8486 tmp &= ~(0xF << 28);
8487 tmp |= (4 << 28);
8488 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008489}
8490
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008491/* Implements 3 different sequences from BSpec chapter "Display iCLK
8492 * Programming" based on the parameters passed:
8493 * - Sequence to enable CLKOUT_DP
8494 * - Sequence to enable CLKOUT_DP without spread
8495 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8496 */
8497static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8498 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008499{
8500 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008501 uint32_t reg, tmp;
8502
8503 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8504 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008505 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008506 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008507
Ville Syrjäläa5805162015-05-26 20:42:30 +03008508 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008509
8510 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8511 tmp &= ~SBI_SSCCTL_DISABLE;
8512 tmp |= SBI_SSCCTL_PATHALT;
8513 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8514
8515 udelay(24);
8516
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008517 if (with_spread) {
8518 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8519 tmp &= ~SBI_SSCCTL_PATHALT;
8520 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008521
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008522 if (with_fdi) {
8523 lpt_reset_fdi_mphy(dev_priv);
8524 lpt_program_fdi_mphy(dev_priv);
8525 }
8526 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008527
Ville Syrjäläc2699522015-08-27 23:55:59 +03008528 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008529 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8530 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8531 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008532
Ville Syrjäläa5805162015-05-26 20:42:30 +03008533 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008534}
8535
Paulo Zanoni47701c32013-07-23 11:19:25 -03008536/* Sequence to disable CLKOUT_DP */
8537static void lpt_disable_clkout_dp(struct drm_device *dev)
8538{
8539 struct drm_i915_private *dev_priv = dev->dev_private;
8540 uint32_t reg, tmp;
8541
Ville Syrjäläa5805162015-05-26 20:42:30 +03008542 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008543
Ville Syrjäläc2699522015-08-27 23:55:59 +03008544 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008545 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8546 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8547 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8548
8549 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8550 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8551 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8552 tmp |= SBI_SSCCTL_PATHALT;
8553 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8554 udelay(32);
8555 }
8556 tmp |= SBI_SSCCTL_DISABLE;
8557 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8558 }
8559
Ville Syrjäläa5805162015-05-26 20:42:30 +03008560 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008561}
8562
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008563#define BEND_IDX(steps) ((50 + (steps)) / 5)
8564
8565static const uint16_t sscdivintphase[] = {
8566 [BEND_IDX( 50)] = 0x3B23,
8567 [BEND_IDX( 45)] = 0x3B23,
8568 [BEND_IDX( 40)] = 0x3C23,
8569 [BEND_IDX( 35)] = 0x3C23,
8570 [BEND_IDX( 30)] = 0x3D23,
8571 [BEND_IDX( 25)] = 0x3D23,
8572 [BEND_IDX( 20)] = 0x3E23,
8573 [BEND_IDX( 15)] = 0x3E23,
8574 [BEND_IDX( 10)] = 0x3F23,
8575 [BEND_IDX( 5)] = 0x3F23,
8576 [BEND_IDX( 0)] = 0x0025,
8577 [BEND_IDX( -5)] = 0x0025,
8578 [BEND_IDX(-10)] = 0x0125,
8579 [BEND_IDX(-15)] = 0x0125,
8580 [BEND_IDX(-20)] = 0x0225,
8581 [BEND_IDX(-25)] = 0x0225,
8582 [BEND_IDX(-30)] = 0x0325,
8583 [BEND_IDX(-35)] = 0x0325,
8584 [BEND_IDX(-40)] = 0x0425,
8585 [BEND_IDX(-45)] = 0x0425,
8586 [BEND_IDX(-50)] = 0x0525,
8587};
8588
8589/*
8590 * Bend CLKOUT_DP
8591 * steps -50 to 50 inclusive, in steps of 5
8592 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8593 * change in clock period = -(steps / 10) * 5.787 ps
8594 */
8595static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8596{
8597 uint32_t tmp;
8598 int idx = BEND_IDX(steps);
8599
8600 if (WARN_ON(steps % 5 != 0))
8601 return;
8602
8603 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8604 return;
8605
8606 mutex_lock(&dev_priv->sb_lock);
8607
8608 if (steps % 10 != 0)
8609 tmp = 0xAAAAAAAB;
8610 else
8611 tmp = 0x00000000;
8612 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8613
8614 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8615 tmp &= 0xffff0000;
8616 tmp |= sscdivintphase[idx];
8617 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8618
8619 mutex_unlock(&dev_priv->sb_lock);
8620}
8621
8622#undef BEND_IDX
8623
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008624static void lpt_init_pch_refclk(struct drm_device *dev)
8625{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008626 struct intel_encoder *encoder;
8627 bool has_vga = false;
8628
Damien Lespiaub2784e12014-08-05 11:29:37 +01008629 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008630 switch (encoder->type) {
8631 case INTEL_OUTPUT_ANALOG:
8632 has_vga = true;
8633 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008634 default:
8635 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008636 }
8637 }
8638
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008639 if (has_vga) {
8640 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008641 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008642 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008643 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008644 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008645}
8646
Paulo Zanonidde86e22012-12-01 12:04:25 -02008647/*
8648 * Initialize reference clocks when the driver loads
8649 */
8650void intel_init_pch_refclk(struct drm_device *dev)
8651{
8652 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8653 ironlake_init_pch_refclk(dev);
8654 else if (HAS_PCH_LPT(dev))
8655 lpt_init_pch_refclk(dev);
8656}
8657
Daniel Vetter6ff93602013-04-19 11:24:36 +02008658static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008659{
8660 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8661 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8662 int pipe = intel_crtc->pipe;
8663 uint32_t val;
8664
Daniel Vetter78114072013-06-13 00:54:57 +02008665 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008666
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008667 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008668 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008669 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008670 break;
8671 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008672 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008673 break;
8674 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008675 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008676 break;
8677 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008678 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008679 break;
8680 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008681 /* Case prevented by intel_choose_pipe_bpp_dither. */
8682 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008683 }
8684
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008685 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008686 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8687
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008688 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008689 val |= PIPECONF_INTERLACED_ILK;
8690 else
8691 val |= PIPECONF_PROGRESSIVE;
8692
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008693 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008694 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008695
Paulo Zanonic8203562012-09-12 10:06:29 -03008696 I915_WRITE(PIPECONF(pipe), val);
8697 POSTING_READ(PIPECONF(pipe));
8698}
8699
Daniel Vetter6ff93602013-04-19 11:24:36 +02008700static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008701{
Jani Nikula391bf042016-03-18 17:05:40 +02008702 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008703 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008704 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008705 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008706
Jani Nikula391bf042016-03-18 17:05:40 +02008707 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008708 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8709
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008710 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008711 val |= PIPECONF_INTERLACED_ILK;
8712 else
8713 val |= PIPECONF_PROGRESSIVE;
8714
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008715 I915_WRITE(PIPECONF(cpu_transcoder), val);
8716 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008717}
8718
Jani Nikula391bf042016-03-18 17:05:40 +02008719static void haswell_set_pipemisc(struct drm_crtc *crtc)
8720{
8721 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8722 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8723
8724 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8725 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008726
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008727 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008728 case 18:
8729 val |= PIPEMISC_DITHER_6_BPC;
8730 break;
8731 case 24:
8732 val |= PIPEMISC_DITHER_8_BPC;
8733 break;
8734 case 30:
8735 val |= PIPEMISC_DITHER_10_BPC;
8736 break;
8737 case 36:
8738 val |= PIPEMISC_DITHER_12_BPC;
8739 break;
8740 default:
8741 /* Case prevented by pipe_config_set_bpp. */
8742 BUG();
8743 }
8744
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008745 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008746 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8747
Jani Nikula391bf042016-03-18 17:05:40 +02008748 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008749 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008750}
8751
Paulo Zanonid4b19312012-11-29 11:29:32 -02008752int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8753{
8754 /*
8755 * Account for spread spectrum to avoid
8756 * oversubscribing the link. Max center spread
8757 * is 2.5%; use 5% for safety's sake.
8758 */
8759 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008760 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008761}
8762
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008763static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008764{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008765 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008766}
8767
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008768static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8769 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008770 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008771{
8772 struct drm_crtc *crtc = &intel_crtc->base;
8773 struct drm_device *dev = crtc->dev;
8774 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008775 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008776 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008777 struct drm_connector_state *connector_state;
8778 struct intel_encoder *encoder;
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008779 u32 dpll, fp, fp2;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008780 int factor, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008781 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008782
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008783 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008784 if (connector_state->crtc != crtc_state->base.crtc)
8785 continue;
8786
8787 encoder = to_intel_encoder(connector_state->best_encoder);
8788
8789 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008790 case INTEL_OUTPUT_LVDS:
8791 is_lvds = true;
8792 break;
8793 case INTEL_OUTPUT_SDVO:
8794 case INTEL_OUTPUT_HDMI:
8795 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008796 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008797 default:
8798 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008799 }
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008800 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008801
Chris Wilsonc1858122010-12-03 21:35:48 +00008802 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008803 factor = 21;
8804 if (is_lvds) {
8805 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008806 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008807 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008808 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008809 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008810 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008811
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008812 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008813
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008814 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8815 fp |= FP_CB_TUNE;
8816
8817 if (reduced_clock) {
8818 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8819
8820 if (reduced_clock->m < factor * reduced_clock->n)
8821 fp2 |= FP_CB_TUNE;
8822 } else {
8823 fp2 = fp;
8824 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008825
Chris Wilson5eddb702010-09-11 13:48:45 +01008826 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008827
Eric Anholta07d6782011-03-30 13:01:08 -07008828 if (is_lvds)
8829 dpll |= DPLLB_MODE_LVDS;
8830 else
8831 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008832
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008833 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008834 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008835
8836 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008837 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008838 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008839 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008840
Eric Anholta07d6782011-03-30 13:01:08 -07008841 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008842 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008843 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008844 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008845
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008846 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008847 case 5:
8848 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8849 break;
8850 case 7:
8851 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8852 break;
8853 case 10:
8854 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8855 break;
8856 case 14:
8857 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8858 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008859 }
8860
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008861 if (is_lvds && intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008862 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008863 else
8864 dpll |= PLL_REF_INPUT_DREFCLK;
8865
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008866 dpll |= DPLL_VCO_ENABLE;
8867
8868 crtc_state->dpll_hw_state.dpll = dpll;
8869 crtc_state->dpll_hw_state.fp0 = fp;
8870 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008871}
8872
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008873static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8874 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008875{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008876 struct drm_device *dev = crtc->base.dev;
8877 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008878 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008879 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008880 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008881 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008882 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008883
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008884 memset(&crtc_state->dpll_hw_state, 0,
8885 sizeof(crtc_state->dpll_hw_state));
8886
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008887 crtc->lowfreq_avail = false;
8888
8889 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8890 if (!crtc_state->has_pch_encoder)
8891 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008892
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008893 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8894 if (intel_panel_use_ssc(dev_priv)) {
8895 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8896 dev_priv->vbt.lvds_ssc_freq);
8897 refclk = dev_priv->vbt.lvds_ssc_freq;
8898 }
8899
8900 if (intel_is_dual_link_lvds(dev)) {
8901 if (refclk == 100000)
8902 limit = &intel_limits_ironlake_dual_lvds_100m;
8903 else
8904 limit = &intel_limits_ironlake_dual_lvds;
8905 } else {
8906 if (refclk == 100000)
8907 limit = &intel_limits_ironlake_single_lvds_100m;
8908 else
8909 limit = &intel_limits_ironlake_single_lvds;
8910 }
8911 } else {
8912 limit = &intel_limits_ironlake_dac;
8913 }
8914
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008915 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008916 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8917 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008918 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8919 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008920 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008921
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008922 ironlake_compute_dpll(crtc, crtc_state,
8923 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008924
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008925 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8926 if (pll == NULL) {
8927 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8928 pipe_name(crtc->pipe));
8929 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008930 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008931
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008932 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8933 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008934 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02008935
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008936 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008937}
8938
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008939static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8940 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008941{
8942 struct drm_device *dev = crtc->base.dev;
8943 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008944 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008945
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008946 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8947 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8948 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8949 & ~TU_SIZE_MASK;
8950 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8951 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8952 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8953}
8954
8955static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8956 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008957 struct intel_link_m_n *m_n,
8958 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008959{
8960 struct drm_device *dev = crtc->base.dev;
8961 struct drm_i915_private *dev_priv = dev->dev_private;
8962 enum pipe pipe = crtc->pipe;
8963
8964 if (INTEL_INFO(dev)->gen >= 5) {
8965 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8966 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8967 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8968 & ~TU_SIZE_MASK;
8969 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8970 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8971 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008972 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8973 * gen < 8) and if DRRS is supported (to make sure the
8974 * registers are not unnecessarily read).
8975 */
8976 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008977 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008978 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8979 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8980 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8981 & ~TU_SIZE_MASK;
8982 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8983 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8984 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8985 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008986 } else {
8987 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8988 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8989 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8990 & ~TU_SIZE_MASK;
8991 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8992 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8993 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8994 }
8995}
8996
8997void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008998 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008999{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009000 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009001 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9002 else
9003 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009004 &pipe_config->dp_m_n,
9005 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009006}
9007
Daniel Vetter72419202013-04-04 13:28:53 +02009008static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009009 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009010{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009011 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009012 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009013}
9014
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009015static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009016 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009017{
9018 struct drm_device *dev = crtc->base.dev;
9019 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009020 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9021 uint32_t ps_ctrl = 0;
9022 int id = -1;
9023 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009024
Chandra Kondurua1b22782015-04-07 15:28:45 -07009025 /* find scaler attached to this pipe */
9026 for (i = 0; i < crtc->num_scalers; i++) {
9027 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9028 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9029 id = i;
9030 pipe_config->pch_pfit.enabled = true;
9031 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9032 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9033 break;
9034 }
9035 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009036
Chandra Kondurua1b22782015-04-07 15:28:45 -07009037 scaler_state->scaler_id = id;
9038 if (id >= 0) {
9039 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9040 } else {
9041 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009042 }
9043}
9044
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009045static void
9046skylake_get_initial_plane_config(struct intel_crtc *crtc,
9047 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009048{
9049 struct drm_device *dev = crtc->base.dev;
9050 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009051 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009052 int pipe = crtc->pipe;
9053 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009054 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009055 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009056 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009057
Damien Lespiaud9806c92015-01-21 14:07:19 +00009058 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009059 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009060 DRM_DEBUG_KMS("failed to alloc fb\n");
9061 return;
9062 }
9063
Damien Lespiau1b842c82015-01-21 13:50:54 +00009064 fb = &intel_fb->base;
9065
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009066 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009067 if (!(val & PLANE_CTL_ENABLE))
9068 goto error;
9069
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009070 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9071 fourcc = skl_format_to_fourcc(pixel_format,
9072 val & PLANE_CTL_ORDER_RGBX,
9073 val & PLANE_CTL_ALPHA_MASK);
9074 fb->pixel_format = fourcc;
9075 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9076
Damien Lespiau40f46282015-02-27 11:15:21 +00009077 tiling = val & PLANE_CTL_TILED_MASK;
9078 switch (tiling) {
9079 case PLANE_CTL_TILED_LINEAR:
9080 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9081 break;
9082 case PLANE_CTL_TILED_X:
9083 plane_config->tiling = I915_TILING_X;
9084 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9085 break;
9086 case PLANE_CTL_TILED_Y:
9087 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9088 break;
9089 case PLANE_CTL_TILED_YF:
9090 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9091 break;
9092 default:
9093 MISSING_CASE(tiling);
9094 goto error;
9095 }
9096
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009097 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9098 plane_config->base = base;
9099
9100 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9101
9102 val = I915_READ(PLANE_SIZE(pipe, 0));
9103 fb->height = ((val >> 16) & 0xfff) + 1;
9104 fb->width = ((val >> 0) & 0x1fff) + 1;
9105
9106 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009107 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009108 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009109 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9110
9111 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009112 fb->pixel_format,
9113 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009114
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009115 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009116
9117 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9118 pipe_name(pipe), fb->width, fb->height,
9119 fb->bits_per_pixel, base, fb->pitches[0],
9120 plane_config->size);
9121
Damien Lespiau2d140302015-02-05 17:22:18 +00009122 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009123 return;
9124
9125error:
9126 kfree(fb);
9127}
9128
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009129static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009130 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009131{
9132 struct drm_device *dev = crtc->base.dev;
9133 struct drm_i915_private *dev_priv = dev->dev_private;
9134 uint32_t tmp;
9135
9136 tmp = I915_READ(PF_CTL(crtc->pipe));
9137
9138 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009139 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009140 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9141 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009142
9143 /* We currently do not free assignements of panel fitters on
9144 * ivb/hsw (since we don't use the higher upscaling modes which
9145 * differentiates them) so just WARN about this case for now. */
9146 if (IS_GEN7(dev)) {
9147 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9148 PF_PIPE_SEL_IVB(crtc->pipe));
9149 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009150 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009151}
9152
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009153static void
9154ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9155 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009156{
9157 struct drm_device *dev = crtc->base.dev;
9158 struct drm_i915_private *dev_priv = dev->dev_private;
9159 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009160 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009161 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009162 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009163 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009164 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009165
Damien Lespiau42a7b082015-02-05 19:35:13 +00009166 val = I915_READ(DSPCNTR(pipe));
9167 if (!(val & DISPLAY_PLANE_ENABLE))
9168 return;
9169
Damien Lespiaud9806c92015-01-21 14:07:19 +00009170 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009171 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009172 DRM_DEBUG_KMS("failed to alloc fb\n");
9173 return;
9174 }
9175
Damien Lespiau1b842c82015-01-21 13:50:54 +00009176 fb = &intel_fb->base;
9177
Daniel Vetter18c52472015-02-10 17:16:09 +00009178 if (INTEL_INFO(dev)->gen >= 4) {
9179 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009180 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009181 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9182 }
9183 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009184
9185 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009186 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009187 fb->pixel_format = fourcc;
9188 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009189
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009190 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009191 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009192 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009193 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009194 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009195 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009196 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009197 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009198 }
9199 plane_config->base = base;
9200
9201 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009202 fb->width = ((val >> 16) & 0xfff) + 1;
9203 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009204
9205 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009206 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009207
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009208 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009209 fb->pixel_format,
9210 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009211
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009212 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009213
Damien Lespiau2844a922015-01-20 12:51:48 +00009214 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9215 pipe_name(pipe), fb->width, fb->height,
9216 fb->bits_per_pixel, base, fb->pitches[0],
9217 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009218
Damien Lespiau2d140302015-02-05 17:22:18 +00009219 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009220}
9221
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009222static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009223 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009224{
9225 struct drm_device *dev = crtc->base.dev;
9226 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009227 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009228 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009229 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009230
Imre Deak17290502016-02-12 18:55:11 +02009231 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9232 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009233 return false;
9234
Daniel Vettere143a212013-07-04 12:01:15 +02009235 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009236 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009237
Imre Deak17290502016-02-12 18:55:11 +02009238 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009239 tmp = I915_READ(PIPECONF(crtc->pipe));
9240 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009241 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009242
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009243 switch (tmp & PIPECONF_BPC_MASK) {
9244 case PIPECONF_6BPC:
9245 pipe_config->pipe_bpp = 18;
9246 break;
9247 case PIPECONF_8BPC:
9248 pipe_config->pipe_bpp = 24;
9249 break;
9250 case PIPECONF_10BPC:
9251 pipe_config->pipe_bpp = 30;
9252 break;
9253 case PIPECONF_12BPC:
9254 pipe_config->pipe_bpp = 36;
9255 break;
9256 default:
9257 break;
9258 }
9259
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009260 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9261 pipe_config->limited_color_range = true;
9262
Daniel Vetterab9412b2013-05-03 11:49:46 +02009263 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009264 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009265 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009266
Daniel Vetter88adfff2013-03-28 10:42:01 +01009267 pipe_config->has_pch_encoder = true;
9268
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009269 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9270 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9271 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009272
9273 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009274
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009275 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03009276 /*
9277 * The pipe->pch transcoder and pch transcoder->pll
9278 * mapping is fixed.
9279 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009280 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009281 } else {
9282 tmp = I915_READ(PCH_DPLL_SEL);
9283 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009284 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009285 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009286 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009287 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009288
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009289 pipe_config->shared_dpll =
9290 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9291 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009292
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009293 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9294 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009295
9296 tmp = pipe_config->dpll_hw_state.dpll;
9297 pipe_config->pixel_multiplier =
9298 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9299 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009300
9301 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009302 } else {
9303 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009304 }
9305
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009306 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009307 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009308
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009309 ironlake_get_pfit_config(crtc, pipe_config);
9310
Imre Deak17290502016-02-12 18:55:11 +02009311 ret = true;
9312
9313out:
9314 intel_display_power_put(dev_priv, power_domain);
9315
9316 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009317}
9318
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009319static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9320{
9321 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009322 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009323
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009324 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009325 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009326 pipe_name(crtc->pipe));
9327
Rob Clarke2c719b2014-12-15 13:56:32 -05009328 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9329 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009330 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9331 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009332 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9333 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009334 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009335 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009336 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009337 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009338 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009339 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009340 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009341 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009342 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009343
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009344 /*
9345 * In theory we can still leave IRQs enabled, as long as only the HPD
9346 * interrupts remain enabled. We used to check for that, but since it's
9347 * gen-specific and since we only disable LCPLL after we fully disable
9348 * the interrupts, the check below should be enough.
9349 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009350 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009351}
9352
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009353static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9354{
9355 struct drm_device *dev = dev_priv->dev;
9356
9357 if (IS_HASWELL(dev))
9358 return I915_READ(D_COMP_HSW);
9359 else
9360 return I915_READ(D_COMP_BDW);
9361}
9362
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009363static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9364{
9365 struct drm_device *dev = dev_priv->dev;
9366
9367 if (IS_HASWELL(dev)) {
9368 mutex_lock(&dev_priv->rps.hw_lock);
9369 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9370 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009371 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009372 mutex_unlock(&dev_priv->rps.hw_lock);
9373 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009374 I915_WRITE(D_COMP_BDW, val);
9375 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009376 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009377}
9378
9379/*
9380 * This function implements pieces of two sequences from BSpec:
9381 * - Sequence for display software to disable LCPLL
9382 * - Sequence for display software to allow package C8+
9383 * The steps implemented here are just the steps that actually touch the LCPLL
9384 * register. Callers should take care of disabling all the display engine
9385 * functions, doing the mode unset, fixing interrupts, etc.
9386 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009387static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9388 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009389{
9390 uint32_t val;
9391
9392 assert_can_disable_lcpll(dev_priv);
9393
9394 val = I915_READ(LCPLL_CTL);
9395
9396 if (switch_to_fclk) {
9397 val |= LCPLL_CD_SOURCE_FCLK;
9398 I915_WRITE(LCPLL_CTL, val);
9399
9400 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9401 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9402 DRM_ERROR("Switching to FCLK failed\n");
9403
9404 val = I915_READ(LCPLL_CTL);
9405 }
9406
9407 val |= LCPLL_PLL_DISABLE;
9408 I915_WRITE(LCPLL_CTL, val);
9409 POSTING_READ(LCPLL_CTL);
9410
9411 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9412 DRM_ERROR("LCPLL still locked\n");
9413
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009414 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009415 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009416 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009417 ndelay(100);
9418
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009419 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9420 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009421 DRM_ERROR("D_COMP RCOMP still in progress\n");
9422
9423 if (allow_power_down) {
9424 val = I915_READ(LCPLL_CTL);
9425 val |= LCPLL_POWER_DOWN_ALLOW;
9426 I915_WRITE(LCPLL_CTL, val);
9427 POSTING_READ(LCPLL_CTL);
9428 }
9429}
9430
9431/*
9432 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9433 * source.
9434 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009435static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009436{
9437 uint32_t val;
9438
9439 val = I915_READ(LCPLL_CTL);
9440
9441 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9442 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9443 return;
9444
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009445 /*
9446 * Make sure we're not on PC8 state before disabling PC8, otherwise
9447 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009448 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009449 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009450
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009451 if (val & LCPLL_POWER_DOWN_ALLOW) {
9452 val &= ~LCPLL_POWER_DOWN_ALLOW;
9453 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009454 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009455 }
9456
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009457 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009458 val |= D_COMP_COMP_FORCE;
9459 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009460 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009461
9462 val = I915_READ(LCPLL_CTL);
9463 val &= ~LCPLL_PLL_DISABLE;
9464 I915_WRITE(LCPLL_CTL, val);
9465
9466 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9467 DRM_ERROR("LCPLL not locked yet\n");
9468
9469 if (val & LCPLL_CD_SOURCE_FCLK) {
9470 val = I915_READ(LCPLL_CTL);
9471 val &= ~LCPLL_CD_SOURCE_FCLK;
9472 I915_WRITE(LCPLL_CTL, val);
9473
9474 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9475 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9476 DRM_ERROR("Switching back to LCPLL failed\n");
9477 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009478
Mika Kuoppala59bad942015-01-16 11:34:40 +02009479 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009480 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009481}
9482
Paulo Zanoni765dab672014-03-07 20:08:18 -03009483/*
9484 * Package states C8 and deeper are really deep PC states that can only be
9485 * reached when all the devices on the system allow it, so even if the graphics
9486 * device allows PC8+, it doesn't mean the system will actually get to these
9487 * states. Our driver only allows PC8+ when going into runtime PM.
9488 *
9489 * The requirements for PC8+ are that all the outputs are disabled, the power
9490 * well is disabled and most interrupts are disabled, and these are also
9491 * requirements for runtime PM. When these conditions are met, we manually do
9492 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9493 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9494 * hang the machine.
9495 *
9496 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9497 * the state of some registers, so when we come back from PC8+ we need to
9498 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9499 * need to take care of the registers kept by RC6. Notice that this happens even
9500 * if we don't put the device in PCI D3 state (which is what currently happens
9501 * because of the runtime PM support).
9502 *
9503 * For more, read "Display Sequences for Package C8" on the hardware
9504 * documentation.
9505 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009506void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009507{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009508 struct drm_device *dev = dev_priv->dev;
9509 uint32_t val;
9510
Paulo Zanonic67a4702013-08-19 13:18:09 -03009511 DRM_DEBUG_KMS("Enabling package C8+\n");
9512
Ville Syrjäläc2699522015-08-27 23:55:59 +03009513 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009514 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9515 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9516 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9517 }
9518
9519 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009520 hsw_disable_lcpll(dev_priv, true, true);
9521}
9522
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009523void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009524{
9525 struct drm_device *dev = dev_priv->dev;
9526 uint32_t val;
9527
Paulo Zanonic67a4702013-08-19 13:18:09 -03009528 DRM_DEBUG_KMS("Disabling package C8+\n");
9529
9530 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009531 lpt_init_pch_refclk(dev);
9532
Ville Syrjäläc2699522015-08-27 23:55:59 +03009533 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009534 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9535 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9536 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9537 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009538}
9539
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009540static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309541{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009542 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009543 struct intel_atomic_state *old_intel_state =
9544 to_intel_atomic_state(old_state);
9545 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309546
Imre Deakc6c46962016-04-01 16:02:40 +03009547 broxton_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309548}
9549
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009550/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009551static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009552{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009553 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9554 struct drm_i915_private *dev_priv = state->dev->dev_private;
9555 struct drm_crtc *crtc;
9556 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009557 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009558 unsigned max_pixel_rate = 0, i;
9559 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009560
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009561 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9562 sizeof(intel_state->min_pixclk));
9563
9564 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009565 int pixel_rate;
9566
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009567 crtc_state = to_intel_crtc_state(cstate);
9568 if (!crtc_state->base.enable) {
9569 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009570 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009571 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009572
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009573 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009574
9575 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009576 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009577 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9578
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009579 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009580 }
9581
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009582 for_each_pipe(dev_priv, pipe)
9583 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9584
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009585 return max_pixel_rate;
9586}
9587
9588static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9589{
9590 struct drm_i915_private *dev_priv = dev->dev_private;
9591 uint32_t val, data;
9592 int ret;
9593
9594 if (WARN((I915_READ(LCPLL_CTL) &
9595 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9596 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9597 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9598 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9599 "trying to change cdclk frequency with cdclk not enabled\n"))
9600 return;
9601
9602 mutex_lock(&dev_priv->rps.hw_lock);
9603 ret = sandybridge_pcode_write(dev_priv,
9604 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9605 mutex_unlock(&dev_priv->rps.hw_lock);
9606 if (ret) {
9607 DRM_ERROR("failed to inform pcode about cdclk change\n");
9608 return;
9609 }
9610
9611 val = I915_READ(LCPLL_CTL);
9612 val |= LCPLL_CD_SOURCE_FCLK;
9613 I915_WRITE(LCPLL_CTL, val);
9614
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009615 if (wait_for_us(I915_READ(LCPLL_CTL) &
9616 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009617 DRM_ERROR("Switching to FCLK failed\n");
9618
9619 val = I915_READ(LCPLL_CTL);
9620 val &= ~LCPLL_CLK_FREQ_MASK;
9621
9622 switch (cdclk) {
9623 case 450000:
9624 val |= LCPLL_CLK_FREQ_450;
9625 data = 0;
9626 break;
9627 case 540000:
9628 val |= LCPLL_CLK_FREQ_54O_BDW;
9629 data = 1;
9630 break;
9631 case 337500:
9632 val |= LCPLL_CLK_FREQ_337_5_BDW;
9633 data = 2;
9634 break;
9635 case 675000:
9636 val |= LCPLL_CLK_FREQ_675_BDW;
9637 data = 3;
9638 break;
9639 default:
9640 WARN(1, "invalid cdclk frequency\n");
9641 return;
9642 }
9643
9644 I915_WRITE(LCPLL_CTL, val);
9645
9646 val = I915_READ(LCPLL_CTL);
9647 val &= ~LCPLL_CD_SOURCE_FCLK;
9648 I915_WRITE(LCPLL_CTL, val);
9649
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009650 if (wait_for_us((I915_READ(LCPLL_CTL) &
9651 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009652 DRM_ERROR("Switching back to LCPLL failed\n");
9653
9654 mutex_lock(&dev_priv->rps.hw_lock);
9655 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9656 mutex_unlock(&dev_priv->rps.hw_lock);
9657
Ville Syrjälä7f1052a2016-04-26 19:46:32 +03009658 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9659
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009660 intel_update_cdclk(dev);
9661
9662 WARN(cdclk != dev_priv->cdclk_freq,
9663 "cdclk requested %d kHz but got %d kHz\n",
9664 cdclk, dev_priv->cdclk_freq);
9665}
9666
Ville Syrjälä587c7912016-05-11 22:44:41 +03009667static int broadwell_calc_cdclk(int max_pixclk)
9668{
9669 if (max_pixclk > 540000)
9670 return 675000;
9671 else if (max_pixclk > 450000)
9672 return 540000;
9673 else if (max_pixclk > 337500)
9674 return 450000;
9675 else
9676 return 337500;
9677}
9678
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009679static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009680{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009681 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009682 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009683 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009684 int cdclk;
9685
9686 /*
9687 * FIXME should also account for plane ratio
9688 * once 64bpp pixel formats are supported.
9689 */
Ville Syrjälä587c7912016-05-11 22:44:41 +03009690 cdclk = broadwell_calc_cdclk(max_pixclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009691
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009692 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009693 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9694 cdclk, dev_priv->max_cdclk_freq);
9695 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009696 }
9697
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009698 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9699 if (!intel_state->active_crtcs)
Ville Syrjälä587c7912016-05-11 22:44:41 +03009700 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009701
9702 return 0;
9703}
9704
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009705static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009706{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009707 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009708 struct intel_atomic_state *old_intel_state =
9709 to_intel_atomic_state(old_state);
9710 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009711
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009712 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009713}
9714
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009715static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9716 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009717{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009718 struct intel_encoder *intel_encoder =
9719 intel_ddi_get_crtc_new_encoder(crtc_state);
9720
9721 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9722 if (!intel_ddi_pll_select(crtc, crtc_state))
9723 return -EINVAL;
9724 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009725
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009726 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009727
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009728 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009729}
9730
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309731static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9732 enum port port,
9733 struct intel_crtc_state *pipe_config)
9734{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009735 enum intel_dpll_id id;
9736
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309737 switch (port) {
9738 case PORT_A:
9739 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009740 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309741 break;
9742 case PORT_B:
9743 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009744 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309745 break;
9746 case PORT_C:
9747 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009748 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309749 break;
9750 default:
9751 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009752 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309753 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009754
9755 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309756}
9757
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009758static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9759 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009760 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009761{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009762 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009763 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009764
9765 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9766 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9767
9768 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009769 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009770 id = DPLL_ID_SKL_DPLL0;
9771 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009772 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009773 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009774 break;
9775 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009776 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009777 break;
9778 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009779 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009780 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009781 default:
9782 MISSING_CASE(pipe_config->ddi_pll_sel);
9783 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009784 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009785
9786 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009787}
9788
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009789static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9790 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009791 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009792{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009793 enum intel_dpll_id id;
9794
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009795 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9796
9797 switch (pipe_config->ddi_pll_sel) {
9798 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009799 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009800 break;
9801 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009802 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009803 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009804 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009805 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009806 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009807 case PORT_CLK_SEL_LCPLL_810:
9808 id = DPLL_ID_LCPLL_810;
9809 break;
9810 case PORT_CLK_SEL_LCPLL_1350:
9811 id = DPLL_ID_LCPLL_1350;
9812 break;
9813 case PORT_CLK_SEL_LCPLL_2700:
9814 id = DPLL_ID_LCPLL_2700;
9815 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009816 default:
9817 MISSING_CASE(pipe_config->ddi_pll_sel);
9818 /* fall through */
9819 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009820 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009821 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009822
9823 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009824}
9825
Jani Nikulacf304292016-03-18 17:05:41 +02009826static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9827 struct intel_crtc_state *pipe_config,
9828 unsigned long *power_domain_mask)
9829{
9830 struct drm_device *dev = crtc->base.dev;
9831 struct drm_i915_private *dev_priv = dev->dev_private;
9832 enum intel_display_power_domain power_domain;
9833 u32 tmp;
9834
Imre Deakd9a7bc62016-05-12 16:18:50 +03009835 /*
9836 * The pipe->transcoder mapping is fixed with the exception of the eDP
9837 * transcoder handled below.
9838 */
Jani Nikulacf304292016-03-18 17:05:41 +02009839 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9840
9841 /*
9842 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9843 * consistency and less surprising code; it's in always on power).
9844 */
9845 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9846 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9847 enum pipe trans_edp_pipe;
9848 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9849 default:
9850 WARN(1, "unknown pipe linked to edp transcoder\n");
9851 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9852 case TRANS_DDI_EDP_INPUT_A_ON:
9853 trans_edp_pipe = PIPE_A;
9854 break;
9855 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9856 trans_edp_pipe = PIPE_B;
9857 break;
9858 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9859 trans_edp_pipe = PIPE_C;
9860 break;
9861 }
9862
9863 if (trans_edp_pipe == crtc->pipe)
9864 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9865 }
9866
9867 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9868 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9869 return false;
9870 *power_domain_mask |= BIT(power_domain);
9871
9872 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9873
9874 return tmp & PIPECONF_ENABLE;
9875}
9876
Jani Nikula4d1de972016-03-18 17:05:42 +02009877static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9878 struct intel_crtc_state *pipe_config,
9879 unsigned long *power_domain_mask)
9880{
9881 struct drm_device *dev = crtc->base.dev;
9882 struct drm_i915_private *dev_priv = dev->dev_private;
9883 enum intel_display_power_domain power_domain;
9884 enum port port;
9885 enum transcoder cpu_transcoder;
9886 u32 tmp;
9887
9888 pipe_config->has_dsi_encoder = false;
9889
9890 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9891 if (port == PORT_A)
9892 cpu_transcoder = TRANSCODER_DSI_A;
9893 else
9894 cpu_transcoder = TRANSCODER_DSI_C;
9895
9896 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9897 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9898 continue;
9899 *power_domain_mask |= BIT(power_domain);
9900
Imre Deakdb18b6a2016-03-24 12:41:40 +02009901 /*
9902 * The PLL needs to be enabled with a valid divider
9903 * configuration, otherwise accessing DSI registers will hang
9904 * the machine. See BSpec North Display Engine
9905 * registers/MIPI[BXT]. We can break out here early, since we
9906 * need the same DSI PLL to be enabled for both DSI ports.
9907 */
9908 if (!intel_dsi_pll_is_enabled(dev_priv))
9909 break;
9910
Jani Nikula4d1de972016-03-18 17:05:42 +02009911 /* XXX: this works for video mode only */
9912 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9913 if (!(tmp & DPI_ENABLE))
9914 continue;
9915
9916 tmp = I915_READ(MIPI_CTRL(port));
9917 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9918 continue;
9919
9920 pipe_config->cpu_transcoder = cpu_transcoder;
9921 pipe_config->has_dsi_encoder = true;
9922 break;
9923 }
9924
9925 return pipe_config->has_dsi_encoder;
9926}
9927
Daniel Vetter26804af2014-06-25 22:01:55 +03009928static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009929 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009930{
9931 struct drm_device *dev = crtc->base.dev;
9932 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009933 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009934 enum port port;
9935 uint32_t tmp;
9936
9937 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9938
9939 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9940
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07009941 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009942 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309943 else if (IS_BROXTON(dev))
9944 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009945 else
9946 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009947
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009948 pll = pipe_config->shared_dpll;
9949 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009950 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9951 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009952 }
9953
Daniel Vetter26804af2014-06-25 22:01:55 +03009954 /*
9955 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9956 * DDI E. So just check whether this pipe is wired to DDI E and whether
9957 * the PCH transcoder is on.
9958 */
Damien Lespiauca370452013-12-03 13:56:24 +00009959 if (INTEL_INFO(dev)->gen < 9 &&
9960 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009961 pipe_config->has_pch_encoder = true;
9962
9963 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9964 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9965 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9966
9967 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9968 }
9969}
9970
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009971static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009972 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009973{
9974 struct drm_device *dev = crtc->base.dev;
9975 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009976 enum intel_display_power_domain power_domain;
9977 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009978 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009979
Imre Deak17290502016-02-12 18:55:11 +02009980 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9981 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009982 return false;
Imre Deak17290502016-02-12 18:55:11 +02009983 power_domain_mask = BIT(power_domain);
9984
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009985 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009986
Jani Nikulacf304292016-03-18 17:05:41 +02009987 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +02009988
Jani Nikula4d1de972016-03-18 17:05:42 +02009989 if (IS_BROXTON(dev_priv)) {
9990 bxt_get_dsi_transcoder_state(crtc, pipe_config,
9991 &power_domain_mask);
9992 WARN_ON(active && pipe_config->has_dsi_encoder);
9993 if (pipe_config->has_dsi_encoder)
9994 active = true;
9995 }
9996
Jani Nikulacf304292016-03-18 17:05:41 +02009997 if (!active)
Imre Deak17290502016-02-12 18:55:11 +02009998 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009999
Jani Nikula4d1de972016-03-18 17:05:42 +020010000 if (!pipe_config->has_dsi_encoder) {
10001 haswell_get_ddi_port_state(crtc, pipe_config);
10002 intel_get_pipe_timings(crtc, pipe_config);
10003 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010004
Jani Nikulabc58be62016-03-18 17:05:39 +020010005 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010006
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010007 pipe_config->gamma_mode =
10008 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10009
Chandra Kondurua1b22782015-04-07 15:28:45 -070010010 if (INTEL_INFO(dev)->gen >= 9) {
10011 skl_init_scalers(dev, crtc, pipe_config);
10012 }
10013
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010014 if (INTEL_INFO(dev)->gen >= 9) {
10015 pipe_config->scaler_state.scaler_id = -1;
10016 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10017 }
10018
Imre Deak17290502016-02-12 18:55:11 +020010019 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10020 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10021 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010022 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010023 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010024 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010025 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010026 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010027
Jesse Barnese59150d2014-01-07 13:30:45 -080010028 if (IS_HASWELL(dev))
10029 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10030 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010031
Jani Nikula4d1de972016-03-18 17:05:42 +020010032 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10033 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010034 pipe_config->pixel_multiplier =
10035 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10036 } else {
10037 pipe_config->pixel_multiplier = 1;
10038 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010039
Imre Deak17290502016-02-12 18:55:11 +020010040out:
10041 for_each_power_domain(power_domain, power_domain_mask)
10042 intel_display_power_put(dev_priv, power_domain);
10043
Jani Nikulacf304292016-03-18 17:05:41 +020010044 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010045}
10046
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010047static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10048 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010049{
10050 struct drm_device *dev = crtc->dev;
10051 struct drm_i915_private *dev_priv = dev->dev_private;
10052 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010053 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010054
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010055 if (plane_state && plane_state->visible) {
10056 unsigned int width = plane_state->base.crtc_w;
10057 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010058 unsigned int stride = roundup_pow_of_two(width) * 4;
10059
10060 switch (stride) {
10061 default:
10062 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10063 width, stride);
10064 stride = 256;
10065 /* fallthrough */
10066 case 256:
10067 case 512:
10068 case 1024:
10069 case 2048:
10070 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010071 }
10072
Ville Syrjälädc41c152014-08-13 11:57:05 +030010073 cntl |= CURSOR_ENABLE |
10074 CURSOR_GAMMA_ENABLE |
10075 CURSOR_FORMAT_ARGB |
10076 CURSOR_STRIDE(stride);
10077
10078 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010079 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010080
Ville Syrjälädc41c152014-08-13 11:57:05 +030010081 if (intel_crtc->cursor_cntl != 0 &&
10082 (intel_crtc->cursor_base != base ||
10083 intel_crtc->cursor_size != size ||
10084 intel_crtc->cursor_cntl != cntl)) {
10085 /* On these chipsets we can only modify the base/size/stride
10086 * whilst the cursor is disabled.
10087 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010088 I915_WRITE(CURCNTR(PIPE_A), 0);
10089 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010090 intel_crtc->cursor_cntl = 0;
10091 }
10092
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010093 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010094 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010095 intel_crtc->cursor_base = base;
10096 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010097
10098 if (intel_crtc->cursor_size != size) {
10099 I915_WRITE(CURSIZE, size);
10100 intel_crtc->cursor_size = size;
10101 }
10102
Chris Wilson4b0e3332014-05-30 16:35:26 +030010103 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010104 I915_WRITE(CURCNTR(PIPE_A), cntl);
10105 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010106 intel_crtc->cursor_cntl = cntl;
10107 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010108}
10109
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010110static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10111 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010112{
10113 struct drm_device *dev = crtc->dev;
10114 struct drm_i915_private *dev_priv = dev->dev_private;
10115 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10116 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010117 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010118
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010119 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010120 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010121 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010122 case 64:
10123 cntl |= CURSOR_MODE_64_ARGB_AX;
10124 break;
10125 case 128:
10126 cntl |= CURSOR_MODE_128_ARGB_AX;
10127 break;
10128 case 256:
10129 cntl |= CURSOR_MODE_256_ARGB_AX;
10130 break;
10131 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010132 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010133 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010134 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010135 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010136
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010137 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010138 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010139
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010140 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10141 cntl |= CURSOR_ROTATE_180;
10142 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010143
Chris Wilson4b0e3332014-05-30 16:35:26 +030010144 if (intel_crtc->cursor_cntl != cntl) {
10145 I915_WRITE(CURCNTR(pipe), cntl);
10146 POSTING_READ(CURCNTR(pipe));
10147 intel_crtc->cursor_cntl = cntl;
10148 }
10149
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010150 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010151 I915_WRITE(CURBASE(pipe), base);
10152 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010153
10154 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010155}
10156
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010157/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010158static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010159 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010160{
10161 struct drm_device *dev = crtc->dev;
10162 struct drm_i915_private *dev_priv = dev->dev_private;
10163 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10164 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010165 u32 base = intel_crtc->cursor_addr;
10166 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010167
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010168 if (plane_state) {
10169 int x = plane_state->base.crtc_x;
10170 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010171
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010172 if (x < 0) {
10173 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10174 x = -x;
10175 }
10176 pos |= x << CURSOR_X_SHIFT;
10177
10178 if (y < 0) {
10179 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10180 y = -y;
10181 }
10182 pos |= y << CURSOR_Y_SHIFT;
10183
10184 /* ILK+ do this automagically */
10185 if (HAS_GMCH_DISPLAY(dev) &&
10186 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10187 base += (plane_state->base.crtc_h *
10188 plane_state->base.crtc_w - 1) * 4;
10189 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010190 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010191
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010192 I915_WRITE(CURPOS(pipe), pos);
10193
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010194 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010195 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010196 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010197 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010198}
10199
Ville Syrjälädc41c152014-08-13 11:57:05 +030010200static bool cursor_size_ok(struct drm_device *dev,
10201 uint32_t width, uint32_t height)
10202{
10203 if (width == 0 || height == 0)
10204 return false;
10205
10206 /*
10207 * 845g/865g are special in that they are only limited by
10208 * the width of their cursors, the height is arbitrary up to
10209 * the precision of the register. Everything else requires
10210 * square cursors, limited to a few power-of-two sizes.
10211 */
10212 if (IS_845G(dev) || IS_I865G(dev)) {
10213 if ((width & 63) != 0)
10214 return false;
10215
10216 if (width > (IS_845G(dev) ? 64 : 512))
10217 return false;
10218
10219 if (height > 1023)
10220 return false;
10221 } else {
10222 switch (width | height) {
10223 case 256:
10224 case 128:
10225 if (IS_GEN2(dev))
10226 return false;
10227 case 64:
10228 break;
10229 default:
10230 return false;
10231 }
10232 }
10233
10234 return true;
10235}
10236
Jesse Barnes79e53942008-11-07 14:24:08 -080010237/* VESA 640x480x72Hz mode to set on the pipe */
10238static struct drm_display_mode load_detect_mode = {
10239 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10240 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10241};
10242
Daniel Vettera8bb6812014-02-10 18:00:39 +010010243struct drm_framebuffer *
10244__intel_framebuffer_create(struct drm_device *dev,
10245 struct drm_mode_fb_cmd2 *mode_cmd,
10246 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010247{
10248 struct intel_framebuffer *intel_fb;
10249 int ret;
10250
10251 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010252 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010253 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010254
10255 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010256 if (ret)
10257 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010258
10259 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010260
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010261err:
10262 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010263 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010264}
10265
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010266static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010267intel_framebuffer_create(struct drm_device *dev,
10268 struct drm_mode_fb_cmd2 *mode_cmd,
10269 struct drm_i915_gem_object *obj)
10270{
10271 struct drm_framebuffer *fb;
10272 int ret;
10273
10274 ret = i915_mutex_lock_interruptible(dev);
10275 if (ret)
10276 return ERR_PTR(ret);
10277 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10278 mutex_unlock(&dev->struct_mutex);
10279
10280 return fb;
10281}
10282
Chris Wilsond2dff872011-04-19 08:36:26 +010010283static u32
10284intel_framebuffer_pitch_for_width(int width, int bpp)
10285{
10286 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10287 return ALIGN(pitch, 64);
10288}
10289
10290static u32
10291intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10292{
10293 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010294 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010295}
10296
10297static struct drm_framebuffer *
10298intel_framebuffer_create_for_mode(struct drm_device *dev,
10299 struct drm_display_mode *mode,
10300 int depth, int bpp)
10301{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010302 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010303 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010304 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010305
Dave Gordond37cd8a2016-04-22 19:14:32 +010010306 obj = i915_gem_object_create(dev,
Chris Wilsond2dff872011-04-19 08:36:26 +010010307 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +010010308 if (IS_ERR(obj))
10309 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010310
10311 mode_cmd.width = mode->hdisplay;
10312 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010313 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10314 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010315 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010316
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010317 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10318 if (IS_ERR(fb))
10319 drm_gem_object_unreference_unlocked(&obj->base);
10320
10321 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010322}
10323
10324static struct drm_framebuffer *
10325mode_fits_in_fbdev(struct drm_device *dev,
10326 struct drm_display_mode *mode)
10327{
Daniel Vetter06957262015-08-10 13:34:08 +020010328#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010329 struct drm_i915_private *dev_priv = dev->dev_private;
10330 struct drm_i915_gem_object *obj;
10331 struct drm_framebuffer *fb;
10332
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010333 if (!dev_priv->fbdev)
10334 return NULL;
10335
10336 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010337 return NULL;
10338
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010339 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010340 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010341
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010342 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010343 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10344 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010345 return NULL;
10346
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010347 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010348 return NULL;
10349
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010350 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010351 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010352#else
10353 return NULL;
10354#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010355}
10356
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010357static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10358 struct drm_crtc *crtc,
10359 struct drm_display_mode *mode,
10360 struct drm_framebuffer *fb,
10361 int x, int y)
10362{
10363 struct drm_plane_state *plane_state;
10364 int hdisplay, vdisplay;
10365 int ret;
10366
10367 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10368 if (IS_ERR(plane_state))
10369 return PTR_ERR(plane_state);
10370
10371 if (mode)
10372 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10373 else
10374 hdisplay = vdisplay = 0;
10375
10376 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10377 if (ret)
10378 return ret;
10379 drm_atomic_set_fb_for_plane(plane_state, fb);
10380 plane_state->crtc_x = 0;
10381 plane_state->crtc_y = 0;
10382 plane_state->crtc_w = hdisplay;
10383 plane_state->crtc_h = vdisplay;
10384 plane_state->src_x = x << 16;
10385 plane_state->src_y = y << 16;
10386 plane_state->src_w = hdisplay << 16;
10387 plane_state->src_h = vdisplay << 16;
10388
10389 return 0;
10390}
10391
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010392bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010393 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010394 struct intel_load_detect_pipe *old,
10395 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010396{
10397 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010398 struct intel_encoder *intel_encoder =
10399 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010400 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010401 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010402 struct drm_crtc *crtc = NULL;
10403 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010404 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010405 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010406 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010407 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010408 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010409 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010410
Chris Wilsond2dff872011-04-19 08:36:26 +010010411 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010412 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010413 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010414
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010415 old->restore_state = NULL;
10416
Rob Clark51fd3712013-11-19 12:10:12 -050010417retry:
10418 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10419 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010420 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010421
Jesse Barnes79e53942008-11-07 14:24:08 -080010422 /*
10423 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010424 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010425 * - if the connector already has an assigned crtc, use it (but make
10426 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010427 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010428 * - try to find the first unused crtc that can drive this connector,
10429 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010430 */
10431
10432 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010433 if (connector->state->crtc) {
10434 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010435
Rob Clark51fd3712013-11-19 12:10:12 -050010436 ret = drm_modeset_lock(&crtc->mutex, ctx);
10437 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010438 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010439
10440 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010441 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010442 }
10443
10444 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010445 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010446 i++;
10447 if (!(encoder->possible_crtcs & (1 << i)))
10448 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010449
10450 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10451 if (ret)
10452 goto fail;
10453
10454 if (possible_crtc->state->enable) {
10455 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010456 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010457 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010458
10459 crtc = possible_crtc;
10460 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010461 }
10462
10463 /*
10464 * If we didn't find an unused CRTC, don't use any.
10465 */
10466 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010467 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010468 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010469 }
10470
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010471found:
10472 intel_crtc = to_intel_crtc(crtc);
10473
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010474 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10475 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010476 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010477
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010478 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010479 restore_state = drm_atomic_state_alloc(dev);
10480 if (!state || !restore_state) {
10481 ret = -ENOMEM;
10482 goto fail;
10483 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010484
10485 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010486 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010487
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010488 connector_state = drm_atomic_get_connector_state(state, connector);
10489 if (IS_ERR(connector_state)) {
10490 ret = PTR_ERR(connector_state);
10491 goto fail;
10492 }
10493
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010494 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10495 if (ret)
10496 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010497
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010498 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10499 if (IS_ERR(crtc_state)) {
10500 ret = PTR_ERR(crtc_state);
10501 goto fail;
10502 }
10503
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010504 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010505
Chris Wilson64927112011-04-20 07:25:26 +010010506 if (!mode)
10507 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010508
Chris Wilsond2dff872011-04-19 08:36:26 +010010509 /* We need a framebuffer large enough to accommodate all accesses
10510 * that the plane may generate whilst we perform load detection.
10511 * We can not rely on the fbcon either being present (we get called
10512 * during its initialisation to detect all boot displays, or it may
10513 * not even exist) or that it is large enough to satisfy the
10514 * requested mode.
10515 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010516 fb = mode_fits_in_fbdev(dev, mode);
10517 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010518 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010519 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010520 } else
10521 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010522 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010523 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010524 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010525 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010526
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010527 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10528 if (ret)
10529 goto fail;
10530
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010531 drm_framebuffer_unreference(fb);
10532
10533 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10534 if (ret)
10535 goto fail;
10536
10537 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10538 if (!ret)
10539 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10540 if (!ret)
10541 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10542 if (ret) {
10543 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10544 goto fail;
10545 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010546
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010547 ret = drm_atomic_commit(state);
10548 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010549 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010550 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010551 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010552
10553 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010554
Jesse Barnes79e53942008-11-07 14:24:08 -080010555 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010556 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010557 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010558
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010559fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010560 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010561 drm_atomic_state_free(restore_state);
10562 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010563
Rob Clark51fd3712013-11-19 12:10:12 -050010564 if (ret == -EDEADLK) {
10565 drm_modeset_backoff(ctx);
10566 goto retry;
10567 }
10568
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010569 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010570}
10571
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010572void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010573 struct intel_load_detect_pipe *old,
10574 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010575{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010576 struct intel_encoder *intel_encoder =
10577 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010578 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010579 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010580 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010581
Chris Wilsond2dff872011-04-19 08:36:26 +010010582 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010583 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010584 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010585
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010586 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010587 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010588
10589 ret = drm_atomic_commit(state);
10590 if (ret) {
10591 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10592 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010593 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010594}
10595
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010596static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010597 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010598{
10599 struct drm_i915_private *dev_priv = dev->dev_private;
10600 u32 dpll = pipe_config->dpll_hw_state.dpll;
10601
10602 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010603 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010604 else if (HAS_PCH_SPLIT(dev))
10605 return 120000;
10606 else if (!IS_GEN2(dev))
10607 return 96000;
10608 else
10609 return 48000;
10610}
10611
Jesse Barnes79e53942008-11-07 14:24:08 -080010612/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010613static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010614 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010615{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010616 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010617 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010618 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010619 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010620 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030010621 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010622 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010623 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010624
10625 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010626 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010627 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010628 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010629
10630 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010631 if (IS_PINEVIEW(dev)) {
10632 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10633 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010634 } else {
10635 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10636 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10637 }
10638
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010639 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010640 if (IS_PINEVIEW(dev))
10641 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10642 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010643 else
10644 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010645 DPLL_FPA01_P1_POST_DIV_SHIFT);
10646
10647 switch (dpll & DPLL_MODE_MASK) {
10648 case DPLLB_MODE_DAC_SERIAL:
10649 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10650 5 : 10;
10651 break;
10652 case DPLLB_MODE_LVDS:
10653 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10654 7 : 14;
10655 break;
10656 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010657 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010658 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010659 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010660 }
10661
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010662 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010663 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010664 else
Imre Deakdccbea32015-06-22 23:35:51 +030010665 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010666 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010667 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010668 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010669
10670 if (is_lvds) {
10671 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10672 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010673
10674 if (lvds & LVDS_CLKB_POWER_UP)
10675 clock.p2 = 7;
10676 else
10677 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010678 } else {
10679 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10680 clock.p1 = 2;
10681 else {
10682 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10683 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10684 }
10685 if (dpll & PLL_P2_DIVIDE_BY_4)
10686 clock.p2 = 4;
10687 else
10688 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010689 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010690
Imre Deakdccbea32015-06-22 23:35:51 +030010691 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010692 }
10693
Ville Syrjälä18442d02013-09-13 16:00:08 +030010694 /*
10695 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010696 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010697 * encoder's get_config() function.
10698 */
Imre Deakdccbea32015-06-22 23:35:51 +030010699 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010700}
10701
Ville Syrjälä6878da02013-09-13 15:59:11 +030010702int intel_dotclock_calculate(int link_freq,
10703 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010704{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010705 /*
10706 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010707 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010708 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010709 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010710 *
10711 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010712 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010713 */
10714
Ville Syrjälä6878da02013-09-13 15:59:11 +030010715 if (!m_n->link_n)
10716 return 0;
10717
10718 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10719}
10720
Ville Syrjälä18442d02013-09-13 16:00:08 +030010721static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010722 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010723{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010724 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010725
10726 /* read out port_clock from the DPLL */
10727 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010728
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010729 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010730 * In case there is an active pipe without active ports,
10731 * we may need some idea for the dotclock anyway.
10732 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010733 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010734 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010735 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010736 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010737}
10738
10739/** Returns the currently programmed mode of the given pipe. */
10740struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10741 struct drm_crtc *crtc)
10742{
Jesse Barnes548f2452011-02-17 10:40:53 -080010743 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010744 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010745 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010746 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010747 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010748 int htot = I915_READ(HTOTAL(cpu_transcoder));
10749 int hsync = I915_READ(HSYNC(cpu_transcoder));
10750 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10751 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010752 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010753
10754 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10755 if (!mode)
10756 return NULL;
10757
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010758 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10759 if (!pipe_config) {
10760 kfree(mode);
10761 return NULL;
10762 }
10763
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010764 /*
10765 * Construct a pipe_config sufficient for getting the clock info
10766 * back out of crtc_clock_get.
10767 *
10768 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10769 * to use a real value here instead.
10770 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010771 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10772 pipe_config->pixel_multiplier = 1;
10773 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10774 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10775 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10776 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010777
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010778 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010779 mode->hdisplay = (htot & 0xffff) + 1;
10780 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10781 mode->hsync_start = (hsync & 0xffff) + 1;
10782 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10783 mode->vdisplay = (vtot & 0xffff) + 1;
10784 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10785 mode->vsync_start = (vsync & 0xffff) + 1;
10786 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10787
10788 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010789
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010790 kfree(pipe_config);
10791
Jesse Barnes79e53942008-11-07 14:24:08 -080010792 return mode;
10793}
10794
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010795void intel_mark_busy(struct drm_i915_private *dev_priv)
Jesse Barnes652c3932009-08-17 13:31:43 -070010796{
Chris Wilsonf62a0072014-02-21 17:55:39 +000010797 if (dev_priv->mm.busy)
10798 return;
10799
Paulo Zanoni43694d62014-03-07 20:08:08 -030010800 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010801 i915_update_gfx_val(dev_priv);
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010802 if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010803 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010804 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010805}
10806
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010807void intel_mark_idle(struct drm_i915_private *dev_priv)
Chris Wilsonf047e392012-07-21 12:31:41 +010010808{
Chris Wilsonf62a0072014-02-21 17:55:39 +000010809 if (!dev_priv->mm.busy)
10810 return;
10811
10812 dev_priv->mm.busy = false;
10813
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010814 if (INTEL_GEN(dev_priv) >= 6)
10815 gen6_rps_idle(dev_priv);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010816
Paulo Zanoni43694d62014-03-07 20:08:08 -030010817 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010818}
10819
Jesse Barnes79e53942008-11-07 14:24:08 -080010820static void intel_crtc_destroy(struct drm_crtc *crtc)
10821{
10822 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010823 struct drm_device *dev = crtc->dev;
10824 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010825
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010826 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010827 work = intel_crtc->unpin_work;
10828 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010829 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010830
10831 if (work) {
10832 cancel_work_sync(&work->work);
10833 kfree(work);
10834 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010835
10836 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010837
Jesse Barnes79e53942008-11-07 14:24:08 -080010838 kfree(intel_crtc);
10839}
10840
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010841static void intel_unpin_work_fn(struct work_struct *__work)
10842{
10843 struct intel_unpin_work *work =
10844 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010845 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10846 struct drm_device *dev = crtc->base.dev;
10847 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010848
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010849 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020010850 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilson05394f32010-11-08 19:18:58 +000010851 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010852
John Harrisonf06cc1b2014-11-24 18:49:37 +000010853 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010854 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010855 mutex_unlock(&dev->struct_mutex);
10856
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010857 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanoni1eb52232016-01-19 11:35:44 -020010858 intel_fbc_post_update(crtc);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010859 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010860
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010861 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10862 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010863
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010864 kfree(work);
10865}
10866
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010867/* Is 'a' after or equal to 'b'? */
10868static bool g4x_flip_count_after_eq(u32 a, u32 b)
10869{
10870 return !((a - b) & 0x80000000);
10871}
10872
10873static bool page_flip_finished(struct intel_crtc *crtc)
10874{
10875 struct drm_device *dev = crtc->base.dev;
10876 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc19ae982016-04-13 17:35:03 +010010877 unsigned reset_counter;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010878
Maarten Lankhorst5251f042016-05-17 15:07:47 +020010879 /* ensure that the unpin work is consistent wrt ->pending. */
10880 smp_rmb();
10881
Chris Wilsonc19ae982016-04-13 17:35:03 +010010882 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
Chris Wilson7f1847e2016-04-13 17:35:04 +010010883 if (crtc->reset_counter != reset_counter)
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010884 return true;
10885
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010886 /*
10887 * The relevant registers doen't exist on pre-ctg.
10888 * As the flip done interrupt doesn't trigger for mmio
10889 * flips on gmch platforms, a flip count check isn't
10890 * really needed there. But since ctg has the registers,
10891 * include it in the check anyway.
10892 */
10893 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10894 return true;
10895
10896 /*
Maarten Lankhorste8861672016-02-24 11:24:26 +010010897 * BDW signals flip done immediately if the plane
10898 * is disabled, even if the plane enable is already
10899 * armed to occur at the next vblank :(
10900 */
10901
10902 /*
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010903 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10904 * used the same base address. In that case the mmio flip might
10905 * have completed, but the CS hasn't even executed the flip yet.
10906 *
10907 * A flip count check isn't enough as the CS might have updated
10908 * the base address just after start of vblank, but before we
10909 * managed to process the interrupt. This means we'd complete the
10910 * CS flip too soon.
10911 *
10912 * Combining both checks should get us a good enough result. It may
10913 * still happen that the CS flip has been executed, but has not
10914 * yet actually completed. But in case the base address is the same
10915 * anyway, we don't really care.
10916 */
10917 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10918 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030010919 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010920 crtc->unpin_work->flip_count);
10921}
10922
Maarten Lankhorst5251f042016-05-17 15:07:47 +020010923void intel_finish_page_flip(struct drm_i915_private *dev_priv, int pipe)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010924{
Tvrtko Ursulin91d14252016-05-06 14:48:28 +010010925 struct drm_device *dev = dev_priv->dev;
Maarten Lankhorst5251f042016-05-17 15:07:47 +020010926 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10927 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10928 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010929 unsigned long flags;
10930
Maarten Lankhorst5251f042016-05-17 15:07:47 +020010931 /* Ignore early vblank irqs */
10932 if (!crtc)
10933 return;
Daniel Vetterf3260382014-09-15 14:55:23 +020010934
10935 /*
10936 * This is called both by irq handlers and the reset code (to complete
10937 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000010938 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010939 spin_lock_irqsave(&dev->event_lock, flags);
Maarten Lankhorst5251f042016-05-17 15:07:47 +020010940 work = intel_crtc->unpin_work;
10941
10942 if (work != NULL &&
10943 atomic_read(&work->pending) &&
10944 page_flip_finished(intel_crtc))
10945 page_flip_completed(intel_crtc);
10946
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010947 spin_unlock_irqrestore(&dev->event_lock, flags);
10948}
10949
Chris Wilson60426392015-10-10 10:44:32 +010010950static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010951{
10952 /* Ensure that the work item is consistent when activating it ... */
Maarten Lankhorst55d80d22016-05-17 15:07:45 +020010953 smp_mb__before_atomic();
Maarten Lankhorst5251f042016-05-17 15:07:47 +020010954 atomic_set(&work->pending, 1);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010955}
10956
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010957static int intel_gen2_queue_flip(struct drm_device *dev,
10958 struct drm_crtc *crtc,
10959 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010960 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010961 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010962 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010963{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010964 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010965 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010966 u32 flip_mask;
10967 int ret;
10968
John Harrison5fb9de12015-05-29 17:44:07 +010010969 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010970 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010971 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010972
10973 /* Can't queue multiple flips, so wait for the previous
10974 * one to finish before executing the next.
10975 */
10976 if (intel_crtc->plane)
10977 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10978 else
10979 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010980 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
10981 intel_ring_emit(engine, MI_NOOP);
10982 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020010983 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010984 intel_ring_emit(engine, fb->pitches[0]);
10985 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
10986 intel_ring_emit(engine, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010987
Chris Wilson83d40922012-04-17 19:35:53 +010010988 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010989}
10990
10991static int intel_gen3_queue_flip(struct drm_device *dev,
10992 struct drm_crtc *crtc,
10993 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010994 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010995 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010996 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010997{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010998 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010999 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011000 u32 flip_mask;
11001 int ret;
11002
John Harrison5fb9de12015-05-29 17:44:07 +010011003 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011004 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011005 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011006
11007 if (intel_crtc->plane)
11008 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11009 else
11010 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011011 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11012 intel_ring_emit(engine, MI_NOOP);
11013 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011014 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011015 intel_ring_emit(engine, fb->pitches[0]);
11016 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11017 intel_ring_emit(engine, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011018
Chris Wilson83d40922012-04-17 19:35:53 +010011019 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011020}
11021
11022static int intel_gen4_queue_flip(struct drm_device *dev,
11023 struct drm_crtc *crtc,
11024 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011025 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011026 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011027 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011028{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011029 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011030 struct drm_i915_private *dev_priv = dev->dev_private;
11031 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11032 uint32_t pf, pipesrc;
11033 int ret;
11034
John Harrison5fb9de12015-05-29 17:44:07 +010011035 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011036 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011037 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011038
11039 /* i965+ uses the linear or tiled offsets from the
11040 * Display Registers (which do not change across a page-flip)
11041 * so we need only reprogram the base address.
11042 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011043 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011044 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011045 intel_ring_emit(engine, fb->pitches[0]);
11046 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011047 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011048
11049 /* XXX Enabling the panel-fitter across page-flip is so far
11050 * untested on non-native modes, so ignore it for now.
11051 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11052 */
11053 pf = 0;
11054 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011055 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011056
Chris Wilson83d40922012-04-17 19:35:53 +010011057 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011058}
11059
11060static int intel_gen6_queue_flip(struct drm_device *dev,
11061 struct drm_crtc *crtc,
11062 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011063 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011064 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011065 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011066{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011067 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011068 struct drm_i915_private *dev_priv = dev->dev_private;
11069 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11070 uint32_t pf, pipesrc;
11071 int ret;
11072
John Harrison5fb9de12015-05-29 17:44:07 +010011073 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011074 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011075 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011076
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011077 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011078 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011079 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11080 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011081
Chris Wilson99d9acd2012-04-17 20:37:00 +010011082 /* Contrary to the suggestions in the documentation,
11083 * "Enable Panel Fitter" does not seem to be required when page
11084 * flipping with a non-native mode, and worse causes a normal
11085 * modeset to fail.
11086 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11087 */
11088 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011089 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011090 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011091
Chris Wilson83d40922012-04-17 19:35:53 +010011092 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011093}
11094
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011095static int intel_gen7_queue_flip(struct drm_device *dev,
11096 struct drm_crtc *crtc,
11097 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011098 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011099 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011100 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011101{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011102 struct intel_engine_cs *engine = req->engine;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011103 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011104 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011105 int len, ret;
11106
Robin Schroereba905b2014-05-18 02:24:50 +020011107 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011108 case PLANE_A:
11109 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11110 break;
11111 case PLANE_B:
11112 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11113 break;
11114 case PLANE_C:
11115 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11116 break;
11117 default:
11118 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011119 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011120 }
11121
Chris Wilsonffe74d72013-08-26 20:58:12 +010011122 len = 4;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011123 if (engine->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011124 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011125 /*
11126 * On Gen 8, SRM is now taking an extra dword to accommodate
11127 * 48bits addresses, and we need a NOOP for the batch size to
11128 * stay even.
11129 */
11130 if (IS_GEN8(dev))
11131 len += 2;
11132 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011133
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011134 /*
11135 * BSpec MI_DISPLAY_FLIP for IVB:
11136 * "The full packet must be contained within the same cache line."
11137 *
11138 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11139 * cacheline, if we ever start emitting more commands before
11140 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11141 * then do the cacheline alignment, and finally emit the
11142 * MI_DISPLAY_FLIP.
11143 */
John Harrisonbba09b12015-05-29 17:44:06 +010011144 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011145 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011146 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011147
John Harrison5fb9de12015-05-29 17:44:07 +010011148 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011149 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011150 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011151
Chris Wilsonffe74d72013-08-26 20:58:12 +010011152 /* Unmask the flip-done completion message. Note that the bspec says that
11153 * we should do this for both the BCS and RCS, and that we must not unmask
11154 * more than one flip event at any time (or ensure that one flip message
11155 * can be sent by waiting for flip-done prior to queueing new flips).
11156 * Experimentation says that BCS works despite DERRMR masking all
11157 * flip-done completion events and that unmasking all planes at once
11158 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11159 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11160 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011161 if (engine->id == RCS) {
11162 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11163 intel_ring_emit_reg(engine, DERRMR);
11164 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11165 DERRMR_PIPEB_PRI_FLIP_DONE |
11166 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011167 if (IS_GEN8(dev))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011168 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011169 MI_SRM_LRM_GLOBAL_GTT);
11170 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011171 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011172 MI_SRM_LRM_GLOBAL_GTT);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011173 intel_ring_emit_reg(engine, DERRMR);
11174 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011175 if (IS_GEN8(dev)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011176 intel_ring_emit(engine, 0);
11177 intel_ring_emit(engine, MI_NOOP);
Damien Lespiauf4768282014-04-07 20:24:34 +010011178 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011179 }
11180
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011181 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11182 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11183 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11184 intel_ring_emit(engine, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011185
Chris Wilson83d40922012-04-17 19:35:53 +010011186 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011187}
11188
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011189static bool use_mmio_flip(struct intel_engine_cs *engine,
Sourab Gupta84c33a62014-06-02 16:47:17 +053011190 struct drm_i915_gem_object *obj)
11191{
11192 /*
11193 * This is not being used for older platforms, because
11194 * non-availability of flip done interrupt forces us to use
11195 * CS flips. Older platforms derive flip done using some clever
11196 * tricks involving the flip_pending status bits and vblank irqs.
11197 * So using MMIO flips there would disrupt this mechanism.
11198 */
11199
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011200 if (engine == NULL)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011201 return true;
11202
Chris Wilsonc0336662016-05-06 15:40:21 +010011203 if (INTEL_GEN(engine->i915) < 5)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011204 return false;
11205
11206 if (i915.use_mmio_flip < 0)
11207 return false;
11208 else if (i915.use_mmio_flip > 0)
11209 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011210 else if (i915.enable_execlists)
11211 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011212 else if (obj->base.dma_buf &&
11213 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11214 false))
11215 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011216 else
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011217 return engine != i915_gem_request_get_engine(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011218}
11219
Chris Wilson60426392015-10-10 10:44:32 +010011220static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011221 unsigned int rotation,
Chris Wilson60426392015-10-10 10:44:32 +010011222 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011223{
11224 struct drm_device *dev = intel_crtc->base.dev;
11225 struct drm_i915_private *dev_priv = dev->dev_private;
11226 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011227 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011228 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011229
11230 ctl = I915_READ(PLANE_CTL(pipe, 0));
11231 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011232 switch (fb->modifier[0]) {
11233 case DRM_FORMAT_MOD_NONE:
11234 break;
11235 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011236 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011237 break;
11238 case I915_FORMAT_MOD_Y_TILED:
11239 ctl |= PLANE_CTL_TILED_Y;
11240 break;
11241 case I915_FORMAT_MOD_Yf_TILED:
11242 ctl |= PLANE_CTL_TILED_YF;
11243 break;
11244 default:
11245 MISSING_CASE(fb->modifier[0]);
11246 }
Damien Lespiauff944562014-11-20 14:58:16 +000011247
11248 /*
11249 * The stride is either expressed as a multiple of 64 bytes chunks for
11250 * linear buffers or in number of tiles for tiled buffers.
11251 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011252 if (intel_rotation_90_or_270(rotation)) {
11253 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +020011254 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011255 stride = DIV_ROUND_UP(fb->height, tile_height);
11256 } else {
11257 stride = fb->pitches[0] /
Ville Syrjälä7b49f942016-01-12 21:08:32 +020011258 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11259 fb->pixel_format);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011260 }
Damien Lespiauff944562014-11-20 14:58:16 +000011261
11262 /*
11263 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11264 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11265 */
11266 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11267 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11268
Chris Wilson60426392015-10-10 10:44:32 +010011269 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011270 POSTING_READ(PLANE_SURF(pipe, 0));
11271}
11272
Chris Wilson60426392015-10-10 10:44:32 +010011273static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11274 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011275{
11276 struct drm_device *dev = intel_crtc->base.dev;
11277 struct drm_i915_private *dev_priv = dev->dev_private;
11278 struct intel_framebuffer *intel_fb =
11279 to_intel_framebuffer(intel_crtc->base.primary->fb);
11280 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011281 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011282 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011283
Sourab Gupta84c33a62014-06-02 16:47:17 +053011284 dspcntr = I915_READ(reg);
11285
Damien Lespiauc5d97472014-10-25 00:11:11 +010011286 if (obj->tiling_mode != I915_TILING_NONE)
11287 dspcntr |= DISPPLANE_TILED;
11288 else
11289 dspcntr &= ~DISPPLANE_TILED;
11290
Sourab Gupta84c33a62014-06-02 16:47:17 +053011291 I915_WRITE(reg, dspcntr);
11292
Chris Wilson60426392015-10-10 10:44:32 +010011293 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011294 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011295}
11296
11297/*
11298 * XXX: This is the temporary way to update the plane registers until we get
11299 * around to using the usual plane update functions for MMIO flips
11300 */
Chris Wilson60426392015-10-10 10:44:32 +010011301static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011302{
Chris Wilson60426392015-10-10 10:44:32 +010011303 struct intel_crtc *crtc = mmio_flip->crtc;
11304 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011305
Chris Wilson60426392015-10-10 10:44:32 +010011306 spin_lock_irq(&crtc->base.dev->event_lock);
11307 work = crtc->unpin_work;
11308 spin_unlock_irq(&crtc->base.dev->event_lock);
11309 if (work == NULL)
11310 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011311
Chris Wilson60426392015-10-10 10:44:32 +010011312 intel_pipe_update_start(crtc);
11313
11314 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011315 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011316 else
11317 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011318 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011319
Chris Wilson60426392015-10-10 10:44:32 +010011320 intel_pipe_update_end(crtc);
Maarten Lankhorst55d80d22016-05-17 15:07:45 +020011321
11322 intel_mark_page_flip_active(work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011323}
11324
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011325static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011326{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011327 struct intel_mmio_flip *mmio_flip =
11328 container_of(work, struct intel_mmio_flip, work);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011329 struct intel_framebuffer *intel_fb =
11330 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11331 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011332
Chris Wilson60426392015-10-10 10:44:32 +010011333 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011334 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011335 false, NULL,
11336 &mmio_flip->i915->rps.mmioflips));
Chris Wilson73db04c2016-04-28 09:56:55 +010011337 i915_gem_request_unreference(mmio_flip->req);
Chris Wilson60426392015-10-10 10:44:32 +010011338 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011339
Alex Goinsfd8e0582015-11-25 18:43:38 -080011340 /* For framebuffer backed by dmabuf, wait for fence */
11341 if (obj->base.dma_buf)
11342 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11343 false, false,
11344 MAX_SCHEDULE_TIMEOUT) < 0);
11345
Chris Wilson60426392015-10-10 10:44:32 +010011346 intel_do_mmio_flip(mmio_flip);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011347 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011348}
11349
11350static int intel_queue_mmio_flip(struct drm_device *dev,
11351 struct drm_crtc *crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011352 struct drm_i915_gem_object *obj)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011353{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011354 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011355
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011356 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11357 if (mmio_flip == NULL)
11358 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011359
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011360 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011361 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011362 mmio_flip->crtc = to_intel_crtc(crtc);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011363 mmio_flip->rotation = crtc->primary->state->rotation;
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011364
11365 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11366 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011367
Sourab Gupta84c33a62014-06-02 16:47:17 +053011368 return 0;
11369}
11370
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011371static int intel_default_queue_flip(struct drm_device *dev,
11372 struct drm_crtc *crtc,
11373 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011374 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011375 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011376 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011377{
11378 return -ENODEV;
11379}
11380
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011381static bool __intel_pageflip_stall_check(struct drm_device *dev,
11382 struct drm_crtc *crtc)
11383{
11384 struct drm_i915_private *dev_priv = dev->dev_private;
11385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11386 struct intel_unpin_work *work = intel_crtc->unpin_work;
11387 u32 addr;
Maarten Lankhorst55d80d22016-05-17 15:07:45 +020011388 u32 pending;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011389
Maarten Lankhorst55d80d22016-05-17 15:07:45 +020011390 pending = atomic_read(&work->pending);
11391 /* ensure that the unpin work is consistent wrt ->pending. */
11392 smp_rmb();
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011393
Maarten Lankhorst5251f042016-05-17 15:07:47 +020011394 if (!pending)
11395 return false;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011396
11397 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011398 if (work->flip_queued_req &&
11399 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011400 return false;
11401
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011402 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011403 }
11404
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011405 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011406 return false;
11407
11408 /* Potential stall - if we see that the flip has happened,
11409 * assume a missed interrupt. */
11410 if (INTEL_INFO(dev)->gen >= 4)
11411 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11412 else
11413 addr = I915_READ(DSPADDR(intel_crtc->plane));
11414
11415 /* There is a potential issue here with a false positive after a flip
11416 * to the same address. We could address this by checking for a
11417 * non-incrementing frame counter.
11418 */
11419 return addr == work->gtt_offset;
11420}
11421
Tvrtko Ursulin91d14252016-05-06 14:48:28 +010011422void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011423{
Tvrtko Ursulin91d14252016-05-06 14:48:28 +010011424 struct drm_device *dev = dev_priv->dev;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011425 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11426 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011427 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011428
Dave Gordon6c51d462015-03-06 15:34:26 +000011429 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011430
11431 if (crtc == NULL)
11432 return;
11433
Daniel Vetterf3260382014-09-15 14:55:23 +020011434 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011435 work = intel_crtc->unpin_work;
11436 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011437 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011438 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011439 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011440 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011441 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011442 if (work != NULL &&
11443 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +010011444 intel_queue_rps_boost_for_request(work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011445 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011446}
11447
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011448static int intel_crtc_page_flip(struct drm_crtc *crtc,
11449 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011450 struct drm_pending_vblank_event *event,
11451 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011452{
11453 struct drm_device *dev = crtc->dev;
11454 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011455 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011456 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011457 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011458 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011459 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011460 struct intel_unpin_work *work;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011461 struct intel_engine_cs *engine;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011462 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011463 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011464 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011465
Matt Roper2ff8fde2014-07-08 07:50:07 -070011466 /*
11467 * drm_mode_page_flip_ioctl() should already catch this, but double
11468 * check to be safe. In the future we may enable pageflipping from
11469 * a disabled primary plane.
11470 */
11471 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11472 return -EBUSY;
11473
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011474 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011475 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011476 return -EINVAL;
11477
11478 /*
11479 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11480 * Note that pitch changes could also affect these register.
11481 */
11482 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011483 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11484 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011485 return -EINVAL;
11486
Chris Wilsonf900db42014-02-20 09:26:13 +000011487 if (i915_terminally_wedged(&dev_priv->gpu_error))
11488 goto out_hang;
11489
Daniel Vetterb14c5672013-09-19 12:18:32 +020011490 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011491 if (work == NULL)
11492 return -ENOMEM;
11493
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011494 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011495 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011496 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011497 INIT_WORK(&work->work, intel_unpin_work_fn);
11498
Daniel Vetter87b6b102014-05-15 15:33:46 +020011499 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011500 if (ret)
11501 goto free_work;
11502
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011503 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011504 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011505 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011506 /* Before declaring the flip queue wedged, check if
11507 * the hardware completed the operation behind our backs.
11508 */
11509 if (__intel_pageflip_stall_check(dev, crtc)) {
11510 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11511 page_flip_completed(intel_crtc);
11512 } else {
11513 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011514 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011515
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011516 drm_crtc_vblank_put(crtc);
11517 kfree(work);
11518 return -EBUSY;
11519 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011520 }
11521 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011522 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011523
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011524 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11525 flush_workqueue(dev_priv->wq);
11526
Jesse Barnes75dfca82010-02-10 15:09:44 -080011527 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011528 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011529 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011530
Matt Roperf4510a22014-04-01 15:22:40 -070011531 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011532 update_state_fb(crtc->primary);
Paulo Zanonie8216e52016-01-19 11:35:56 -020011533 intel_fbc_pre_update(intel_crtc);
Matt Roper1ed1f962015-01-30 16:22:36 -080011534
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011535 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011536
Chris Wilson89ed88b2015-02-16 14:31:49 +000011537 ret = i915_mutex_lock_interruptible(dev);
11538 if (ret)
11539 goto cleanup;
11540
Chris Wilsonc19ae982016-04-13 17:35:03 +010011541 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
Chris Wilson7f1847e2016-04-13 17:35:04 +010011542 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11543 ret = -EIO;
11544 goto cleanup;
11545 }
11546
11547 atomic_inc(&intel_crtc->unpin_work_count);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011548
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011549 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030011550 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011551
Wayne Boyer666a4532015-12-09 12:29:35 -080011552 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011553 engine = &dev_priv->engine[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011554 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011555 /* vlv: DISPLAY_FLIP fails to change tiling */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011556 engine = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011557 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011558 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011559 } else if (INTEL_INFO(dev)->gen >= 7) {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011560 engine = i915_gem_request_get_engine(obj->last_write_req);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011561 if (engine == NULL || engine->id != RCS)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011562 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011563 } else {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011564 engine = &dev_priv->engine[RCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011565 }
11566
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011567 mmio_flip = use_mmio_flip(engine, obj);
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011568
11569 /* When using CS flips, we want to emit semaphores between rings.
11570 * However, when using mmio flips we will create a task to do the
11571 * synchronisation, so all we want here is to pin the framebuffer
11572 * into the display plane and skip any waits.
11573 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011574 if (!mmio_flip) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011575 ret = i915_gem_object_sync(obj, engine, &request);
Maarten Lankhorst55d80d22016-05-17 15:07:45 +020011576 if (!ret && !request) {
11577 request = i915_gem_request_alloc(engine, NULL);
11578 ret = PTR_ERR_OR_ZERO(request);
11579 }
11580
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011581 if (ret)
11582 goto cleanup_pending;
11583 }
11584
Ville Syrjälä3465c582016-02-15 22:54:43 +020011585 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011586 if (ret)
11587 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011588
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011589 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11590 obj, 0);
11591 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011592
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011593 if (mmio_flip) {
Maarten Lankhorst55d80d22016-05-17 15:07:45 +020011594 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011595
John Harrisonf06cc1b2014-11-24 18:49:37 +000011596 i915_gem_request_assign(&work->flip_queued_req,
11597 obj->last_write_req);
John Harrison6258fbe2015-05-29 17:43:48 +010011598
Maarten Lankhorst55d80d22016-05-17 15:07:45 +020011599 ret = intel_queue_mmio_flip(dev, crtc, obj);
11600 if (ret)
11601 goto cleanup_unpin;
11602 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011603 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011604 page_flip_flags);
11605 if (ret)
11606 goto cleanup_unpin;
11607
John Harrison6258fbe2015-05-29 17:43:48 +010011608 i915_gem_request_assign(&work->flip_queued_req, request);
Maarten Lankhorst55d80d22016-05-17 15:07:45 +020011609
11610 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
11611 intel_mark_page_flip_active(work);
11612
11613 i915_add_request_no_flush(request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011614 }
11615
Maarten Lankhorst55d80d22016-05-17 15:07:45 +020011616 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011617 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011618 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011619
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011620 intel_frontbuffer_flip_prepare(dev,
11621 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011622
Jesse Barnese5510fa2010-07-01 16:48:37 -070011623 trace_i915_flip_request(intel_crtc->plane, obj);
11624
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011625 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011626
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011627cleanup_unpin:
Ville Syrjälä3465c582016-02-15 22:54:43 +020011628 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011629cleanup_pending:
Dave Gordon0aa498d2016-01-28 10:48:09 +000011630 if (!IS_ERR_OR_NULL(request))
Chris Wilsonaa9b7812016-04-13 17:35:15 +010011631 i915_add_request_no_flush(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011632 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011633 mutex_unlock(&dev->struct_mutex);
11634cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011635 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011636 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011637
Chris Wilson89ed88b2015-02-16 14:31:49 +000011638 drm_gem_object_unreference_unlocked(&obj->base);
11639 drm_framebuffer_unreference(work->old_fb);
11640
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011641 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011642 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011643 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011644
Daniel Vetter87b6b102014-05-15 15:33:46 +020011645 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011646free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011647 kfree(work);
11648
Chris Wilsonf900db42014-02-20 09:26:13 +000011649 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011650 struct drm_atomic_state *state;
11651 struct drm_plane_state *plane_state;
11652
Chris Wilsonf900db42014-02-20 09:26:13 +000011653out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011654 state = drm_atomic_state_alloc(dev);
11655 if (!state)
11656 return -ENOMEM;
11657 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11658
11659retry:
11660 plane_state = drm_atomic_get_plane_state(state, primary);
11661 ret = PTR_ERR_OR_ZERO(plane_state);
11662 if (!ret) {
11663 drm_atomic_set_fb_for_plane(plane_state, fb);
11664
11665 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11666 if (!ret)
11667 ret = drm_atomic_commit(state);
11668 }
11669
11670 if (ret == -EDEADLK) {
11671 drm_modeset_backoff(state->acquire_ctx);
11672 drm_atomic_state_clear(state);
11673 goto retry;
11674 }
11675
11676 if (ret)
11677 drm_atomic_state_free(state);
11678
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011679 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011680 spin_lock_irq(&dev->event_lock);
Gustavo Padovan560ce1d2016-04-14 10:48:15 -070011681 drm_crtc_send_vblank_event(crtc, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011682 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011683 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011684 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011685 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011686}
11687
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011688
11689/**
11690 * intel_wm_need_update - Check whether watermarks need updating
11691 * @plane: drm plane
11692 * @state: new plane state
11693 *
11694 * Check current plane state versus the new one to determine whether
11695 * watermarks need to be recalculated.
11696 *
11697 * Returns true or false.
11698 */
11699static bool intel_wm_need_update(struct drm_plane *plane,
11700 struct drm_plane_state *state)
11701{
Matt Roperd21fbe82015-09-24 15:53:12 -070011702 struct intel_plane_state *new = to_intel_plane_state(state);
11703 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11704
11705 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011706 if (new->visible != cur->visible)
11707 return true;
11708
11709 if (!cur->base.fb || !new->base.fb)
11710 return false;
11711
11712 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11713 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011714 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11715 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11716 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11717 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011718 return true;
11719
11720 return false;
11721}
11722
Matt Roperd21fbe82015-09-24 15:53:12 -070011723static bool needs_scaling(struct intel_plane_state *state)
11724{
11725 int src_w = drm_rect_width(&state->src) >> 16;
11726 int src_h = drm_rect_height(&state->src) >> 16;
11727 int dst_w = drm_rect_width(&state->dst);
11728 int dst_h = drm_rect_height(&state->dst);
11729
11730 return (src_w != dst_w || src_h != dst_h);
11731}
11732
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011733int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11734 struct drm_plane_state *plane_state)
11735{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011736 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011737 struct drm_crtc *crtc = crtc_state->crtc;
11738 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11739 struct drm_plane *plane = plane_state->plane;
11740 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011741 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011742 struct intel_plane_state *old_plane_state =
11743 to_intel_plane_state(plane->state);
11744 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011745 bool mode_changed = needs_modeset(crtc_state);
11746 bool was_crtc_enabled = crtc->state->active;
11747 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011748 bool turn_off, turn_on, visible, was_visible;
11749 struct drm_framebuffer *fb = plane_state->fb;
11750
11751 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11752 plane->type != DRM_PLANE_TYPE_CURSOR) {
11753 ret = skl_update_scaler_plane(
11754 to_intel_crtc_state(crtc_state),
11755 to_intel_plane_state(plane_state));
11756 if (ret)
11757 return ret;
11758 }
11759
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011760 was_visible = old_plane_state->visible;
11761 visible = to_intel_plane_state(plane_state)->visible;
11762
11763 if (!was_crtc_enabled && WARN_ON(was_visible))
11764 was_visible = false;
11765
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011766 /*
11767 * Visibility is calculated as if the crtc was on, but
11768 * after scaler setup everything depends on it being off
11769 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030011770 *
11771 * FIXME this is wrong for watermarks. Watermarks should also
11772 * be computed as if the pipe would be active. Perhaps move
11773 * per-plane wm computation to the .check_plane() hook, and
11774 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011775 */
11776 if (!is_crtc_enabled)
11777 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011778
11779 if (!was_visible && !visible)
11780 return 0;
11781
Maarten Lankhorste8861672016-02-24 11:24:26 +010011782 if (fb != old_plane_state->base.fb)
11783 pipe_config->fb_changed = true;
11784
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011785 turn_off = was_visible && (!visible || mode_changed);
11786 turn_on = visible && (!was_visible || mode_changed);
11787
11788 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11789 plane->base.id, fb ? fb->base.id : -1);
11790
11791 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11792 plane->base.id, was_visible, visible,
11793 turn_off, turn_on, mode_changed);
11794
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011795 if (turn_on) {
11796 pipe_config->update_wm_pre = true;
11797
11798 /* must disable cxsr around plane enable/disable */
11799 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11800 pipe_config->disable_cxsr = true;
11801 } else if (turn_off) {
11802 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011803
Ville Syrjälä852eb002015-06-24 22:00:07 +030011804 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011805 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011806 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011807 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011808 /* FIXME bollocks */
11809 pipe_config->update_wm_pre = true;
11810 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011811 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011812
Matt Ropered4a6a72016-02-23 17:20:13 -080011813 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011814 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11815 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011816 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11817
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011818 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011819 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011820
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011821 /*
11822 * WaCxSRDisabledForSpriteScaling:ivb
11823 *
11824 * cstate->update_wm was already set above, so this flag will
11825 * take effect when we commit and program watermarks.
11826 */
11827 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11828 needs_scaling(to_intel_plane_state(plane_state)) &&
11829 !needs_scaling(old_plane_state))
11830 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011831
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011832 return 0;
11833}
11834
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011835static bool encoders_cloneable(const struct intel_encoder *a,
11836 const struct intel_encoder *b)
11837{
11838 /* masks could be asymmetric, so check both ways */
11839 return a == b || (a->cloneable & (1 << b->type) &&
11840 b->cloneable & (1 << a->type));
11841}
11842
11843static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11844 struct intel_crtc *crtc,
11845 struct intel_encoder *encoder)
11846{
11847 struct intel_encoder *source_encoder;
11848 struct drm_connector *connector;
11849 struct drm_connector_state *connector_state;
11850 int i;
11851
11852 for_each_connector_in_state(state, connector, connector_state, i) {
11853 if (connector_state->crtc != &crtc->base)
11854 continue;
11855
11856 source_encoder =
11857 to_intel_encoder(connector_state->best_encoder);
11858 if (!encoders_cloneable(encoder, source_encoder))
11859 return false;
11860 }
11861
11862 return true;
11863}
11864
11865static bool check_encoder_cloning(struct drm_atomic_state *state,
11866 struct intel_crtc *crtc)
11867{
11868 struct intel_encoder *encoder;
11869 struct drm_connector *connector;
11870 struct drm_connector_state *connector_state;
11871 int i;
11872
11873 for_each_connector_in_state(state, connector, connector_state, i) {
11874 if (connector_state->crtc != &crtc->base)
11875 continue;
11876
11877 encoder = to_intel_encoder(connector_state->best_encoder);
11878 if (!check_single_encoder_cloning(state, crtc, encoder))
11879 return false;
11880 }
11881
11882 return true;
11883}
11884
11885static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11886 struct drm_crtc_state *crtc_state)
11887{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011888 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011889 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011890 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011891 struct intel_crtc_state *pipe_config =
11892 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011893 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011894 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011895 bool mode_changed = needs_modeset(crtc_state);
11896
11897 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11898 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11899 return -EINVAL;
11900 }
11901
Ville Syrjälä852eb002015-06-24 22:00:07 +030011902 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011903 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011904
Maarten Lankhorstad421372015-06-15 12:33:42 +020011905 if (mode_changed && crtc_state->enable &&
11906 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011907 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020011908 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11909 pipe_config);
11910 if (ret)
11911 return ret;
11912 }
11913
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000011914 if (crtc_state->color_mgmt_changed) {
11915 ret = intel_color_check(crtc, crtc_state);
11916 if (ret)
11917 return ret;
11918 }
11919
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011920 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011921 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010011922 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080011923 if (ret) {
11924 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070011925 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080011926 }
11927 }
11928
11929 if (dev_priv->display.compute_intermediate_wm &&
11930 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11931 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11932 return 0;
11933
11934 /*
11935 * Calculate 'intermediate' watermarks that satisfy both the
11936 * old state and the new state. We can program these
11937 * immediately.
11938 */
11939 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11940 intel_crtc,
11941 pipe_config);
11942 if (ret) {
11943 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
11944 return ret;
11945 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070011946 } else if (dev_priv->display.compute_intermediate_wm) {
11947 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
11948 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011949 }
11950
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011951 if (INTEL_INFO(dev)->gen >= 9) {
11952 if (mode_changed)
11953 ret = skl_update_scaler_crtc(pipe_config);
11954
11955 if (!ret)
11956 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11957 pipe_config);
11958 }
11959
11960 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011961}
11962
Jani Nikula65b38e02015-04-13 11:26:56 +030011963static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011964 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Matt Roperea2c67b2014-12-23 10:41:52 -080011965 .atomic_begin = intel_begin_crtc_commit,
11966 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011967 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011968};
11969
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011970static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11971{
11972 struct intel_connector *connector;
11973
11974 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020011975 if (connector->base.state->crtc)
11976 drm_connector_unreference(&connector->base);
11977
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011978 if (connector->base.encoder) {
11979 connector->base.state->best_encoder =
11980 connector->base.encoder;
11981 connector->base.state->crtc =
11982 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020011983
11984 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011985 } else {
11986 connector->base.state->best_encoder = NULL;
11987 connector->base.state->crtc = NULL;
11988 }
11989 }
11990}
11991
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011992static void
Robin Schroereba905b2014-05-18 02:24:50 +020011993connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011994 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011995{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011996 int bpp = pipe_config->pipe_bpp;
11997
11998 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11999 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012000 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012001
12002 /* Don't use an invalid EDID bpc value */
12003 if (connector->base.display_info.bpc &&
12004 connector->base.display_info.bpc * 3 < bpp) {
12005 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12006 bpp, connector->base.display_info.bpc*3);
12007 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12008 }
12009
Jani Nikula013dd9e2016-01-13 16:35:20 +020012010 /* Clamp bpp to default limit on screens without EDID 1.4 */
12011 if (connector->base.display_info.bpc == 0) {
12012 int type = connector->base.connector_type;
12013 int clamp_bpp = 24;
12014
12015 /* Fall back to 18 bpp when DP sink capability is unknown. */
12016 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12017 type == DRM_MODE_CONNECTOR_eDP)
12018 clamp_bpp = 18;
12019
12020 if (bpp > clamp_bpp) {
12021 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12022 bpp, clamp_bpp);
12023 pipe_config->pipe_bpp = clamp_bpp;
12024 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012025 }
12026}
12027
12028static int
12029compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012030 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012031{
12032 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012033 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012034 struct drm_connector *connector;
12035 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012036 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012037
Wayne Boyer666a4532015-12-09 12:29:35 -080012038 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012039 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012040 else if (INTEL_INFO(dev)->gen >= 5)
12041 bpp = 12*3;
12042 else
12043 bpp = 8*3;
12044
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012045
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012046 pipe_config->pipe_bpp = bpp;
12047
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012048 state = pipe_config->base.state;
12049
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012050 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012051 for_each_connector_in_state(state, connector, connector_state, i) {
12052 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012053 continue;
12054
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012055 connected_sink_compute_bpp(to_intel_connector(connector),
12056 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012057 }
12058
12059 return bpp;
12060}
12061
Daniel Vetter644db712013-09-19 14:53:58 +020012062static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12063{
12064 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12065 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012066 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012067 mode->crtc_hdisplay, mode->crtc_hsync_start,
12068 mode->crtc_hsync_end, mode->crtc_htotal,
12069 mode->crtc_vdisplay, mode->crtc_vsync_start,
12070 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12071}
12072
Daniel Vetterc0b03412013-05-28 12:05:54 +020012073static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012074 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012075 const char *context)
12076{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012077 struct drm_device *dev = crtc->base.dev;
12078 struct drm_plane *plane;
12079 struct intel_plane *intel_plane;
12080 struct intel_plane_state *state;
12081 struct drm_framebuffer *fb;
12082
12083 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12084 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012085
Jani Nikulada205632016-03-15 21:51:10 +020012086 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012087 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12088 pipe_config->pipe_bpp, pipe_config->dither);
12089 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12090 pipe_config->has_pch_encoder,
12091 pipe_config->fdi_lanes,
12092 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12093 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12094 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012095 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012096 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012097 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012098 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12099 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12100 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012101
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012102 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012103 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012104 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012105 pipe_config->dp_m2_n2.gmch_m,
12106 pipe_config->dp_m2_n2.gmch_n,
12107 pipe_config->dp_m2_n2.link_m,
12108 pipe_config->dp_m2_n2.link_n,
12109 pipe_config->dp_m2_n2.tu);
12110
Daniel Vetter55072d12014-11-20 16:10:28 +010012111 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12112 pipe_config->has_audio,
12113 pipe_config->has_infoframe);
12114
Daniel Vetterc0b03412013-05-28 12:05:54 +020012115 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012116 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012117 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012118 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12119 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012120 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012121 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12122 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012123 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12124 crtc->num_scalers,
12125 pipe_config->scaler_state.scaler_users,
12126 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012127 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12128 pipe_config->gmch_pfit.control,
12129 pipe_config->gmch_pfit.pgm_ratios,
12130 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012131 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012132 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012133 pipe_config->pch_pfit.size,
12134 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012135 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012136 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012137
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012138 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012139 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012140 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012141 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012142 pipe_config->ddi_pll_sel,
12143 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012144 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012145 pipe_config->dpll_hw_state.pll0,
12146 pipe_config->dpll_hw_state.pll1,
12147 pipe_config->dpll_hw_state.pll2,
12148 pipe_config->dpll_hw_state.pll3,
12149 pipe_config->dpll_hw_state.pll6,
12150 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012151 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012152 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012153 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012154 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012155 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12156 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12157 pipe_config->ddi_pll_sel,
12158 pipe_config->dpll_hw_state.ctrl1,
12159 pipe_config->dpll_hw_state.cfgcr1,
12160 pipe_config->dpll_hw_state.cfgcr2);
12161 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012162 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012163 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012164 pipe_config->dpll_hw_state.wrpll,
12165 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012166 } else {
12167 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12168 "fp0: 0x%x, fp1: 0x%x\n",
12169 pipe_config->dpll_hw_state.dpll,
12170 pipe_config->dpll_hw_state.dpll_md,
12171 pipe_config->dpll_hw_state.fp0,
12172 pipe_config->dpll_hw_state.fp1);
12173 }
12174
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012175 DRM_DEBUG_KMS("planes on this crtc\n");
12176 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12177 intel_plane = to_intel_plane(plane);
12178 if (intel_plane->pipe != crtc->pipe)
12179 continue;
12180
12181 state = to_intel_plane_state(plane->state);
12182 fb = state->base.fb;
12183 if (!fb) {
12184 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12185 "disabled, scaler_id = %d\n",
12186 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12187 plane->base.id, intel_plane->pipe,
12188 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12189 drm_plane_index(plane), state->scaler_id);
12190 continue;
12191 }
12192
12193 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12194 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12195 plane->base.id, intel_plane->pipe,
12196 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12197 drm_plane_index(plane));
12198 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12199 fb->base.id, fb->width, fb->height, fb->pixel_format);
12200 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12201 state->scaler_id,
12202 state->src.x1 >> 16, state->src.y1 >> 16,
12203 drm_rect_width(&state->src) >> 16,
12204 drm_rect_height(&state->src) >> 16,
12205 state->dst.x1, state->dst.y1,
12206 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12207 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012208}
12209
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012210static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012211{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012212 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012213 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012214 unsigned int used_ports = 0;
12215
12216 /*
12217 * Walk the connector list instead of the encoder
12218 * list to detect the problem on ddi platforms
12219 * where there's just one encoder per digital port.
12220 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012221 drm_for_each_connector(connector, dev) {
12222 struct drm_connector_state *connector_state;
12223 struct intel_encoder *encoder;
12224
12225 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12226 if (!connector_state)
12227 connector_state = connector->state;
12228
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012229 if (!connector_state->best_encoder)
12230 continue;
12231
12232 encoder = to_intel_encoder(connector_state->best_encoder);
12233
12234 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012235
12236 switch (encoder->type) {
12237 unsigned int port_mask;
12238 case INTEL_OUTPUT_UNKNOWN:
12239 if (WARN_ON(!HAS_DDI(dev)))
12240 break;
12241 case INTEL_OUTPUT_DISPLAYPORT:
12242 case INTEL_OUTPUT_HDMI:
12243 case INTEL_OUTPUT_EDP:
12244 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12245
12246 /* the same port mustn't appear more than once */
12247 if (used_ports & port_mask)
12248 return false;
12249
12250 used_ports |= port_mask;
12251 default:
12252 break;
12253 }
12254 }
12255
12256 return true;
12257}
12258
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012259static void
12260clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12261{
12262 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012263 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012264 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012265 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012266 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012267 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012268
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012269 /* FIXME: before the switch to atomic started, a new pipe_config was
12270 * kzalloc'd. Code that depends on any field being zero should be
12271 * fixed, so that the crtc_state can be safely duplicated. For now,
12272 * only fields that are know to not cause problems are preserved. */
12273
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012274 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012275 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012276 shared_dpll = crtc_state->shared_dpll;
12277 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012278 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012279 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012280
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012281 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012282
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012283 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012284 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012285 crtc_state->shared_dpll = shared_dpll;
12286 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012287 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012288 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012289}
12290
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012291static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012292intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012293 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012294{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012295 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012296 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012297 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012298 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012299 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012300 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012301 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012302
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012303 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012304
Daniel Vettere143a212013-07-04 12:01:15 +020012305 pipe_config->cpu_transcoder =
12306 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012307
Imre Deak2960bc92013-07-30 13:36:32 +030012308 /*
12309 * Sanitize sync polarity flags based on requested ones. If neither
12310 * positive or negative polarity is requested, treat this as meaning
12311 * negative polarity.
12312 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012313 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012314 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012315 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012316
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012317 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012318 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012319 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012320
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012321 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12322 pipe_config);
12323 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012324 goto fail;
12325
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012326 /*
12327 * Determine the real pipe dimensions. Note that stereo modes can
12328 * increase the actual pipe size due to the frame doubling and
12329 * insertion of additional space for blanks between the frame. This
12330 * is stored in the crtc timings. We use the requested mode to do this
12331 * computation to clearly distinguish it from the adjusted mode, which
12332 * can be changed by the connectors in the below retry loop.
12333 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012334 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012335 &pipe_config->pipe_src_w,
12336 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012337
Daniel Vettere29c22c2013-02-21 00:00:16 +010012338encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012339 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012340 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012341 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012342
Daniel Vetter135c81b2013-07-21 21:37:09 +020012343 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012344 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12345 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012346
Daniel Vetter7758a112012-07-08 19:40:39 +020012347 /* Pass our mode to the connectors and the CRTC to give them a chance to
12348 * adjust it according to limitations or connector properties, and also
12349 * a chance to reject the mode entirely.
12350 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012351 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012352 if (connector_state->crtc != crtc)
12353 continue;
12354
12355 encoder = to_intel_encoder(connector_state->best_encoder);
12356
Daniel Vetterefea6e82013-07-21 21:36:59 +020012357 if (!(encoder->compute_config(encoder, pipe_config))) {
12358 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012359 goto fail;
12360 }
12361 }
12362
Daniel Vetterff9a6752013-06-01 17:16:21 +020012363 /* Set default port clock if not overwritten by the encoder. Needs to be
12364 * done afterwards in case the encoder adjusts the mode. */
12365 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012366 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012367 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012368
Daniel Vettera43f6e02013-06-07 23:10:32 +020012369 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012370 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012371 DRM_DEBUG_KMS("CRTC fixup failed\n");
12372 goto fail;
12373 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012374
12375 if (ret == RETRY) {
12376 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12377 ret = -EINVAL;
12378 goto fail;
12379 }
12380
12381 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12382 retry = false;
12383 goto encoder_retry;
12384 }
12385
Daniel Vettere8fa4272015-08-12 11:43:34 +020012386 /* Dithering seems to not pass-through bits correctly when it should, so
12387 * only enable it on 6bpc panels. */
12388 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012389 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012390 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012391
Daniel Vetter7758a112012-07-08 19:40:39 +020012392fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012393 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012394}
12395
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012396static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012397intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012398{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012399 struct drm_crtc *crtc;
12400 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012401 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012402
Ville Syrjälä76688512014-01-10 11:28:06 +020012403 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012404 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012405 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012406
12407 /* Update hwmode for vblank functions */
12408 if (crtc->state->active)
12409 crtc->hwmode = crtc->state->adjusted_mode;
12410 else
12411 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012412
12413 /*
12414 * Update legacy state to satisfy fbc code. This can
12415 * be removed when fbc uses the atomic state.
12416 */
12417 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12418 struct drm_plane_state *plane_state = crtc->primary->state;
12419
12420 crtc->primary->fb = plane_state->fb;
12421 crtc->x = plane_state->src_x >> 16;
12422 crtc->y = plane_state->src_y >> 16;
12423 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012424 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012425}
12426
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012427static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012428{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012429 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012430
12431 if (clock1 == clock2)
12432 return true;
12433
12434 if (!clock1 || !clock2)
12435 return false;
12436
12437 diff = abs(clock1 - clock2);
12438
12439 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12440 return true;
12441
12442 return false;
12443}
12444
Daniel Vetter25c5b262012-07-08 22:08:04 +020012445#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12446 list_for_each_entry((intel_crtc), \
12447 &(dev)->mode_config.crtc_list, \
12448 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012449 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012450
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012451static bool
12452intel_compare_m_n(unsigned int m, unsigned int n,
12453 unsigned int m2, unsigned int n2,
12454 bool exact)
12455{
12456 if (m == m2 && n == n2)
12457 return true;
12458
12459 if (exact || !m || !n || !m2 || !n2)
12460 return false;
12461
12462 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12463
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012464 if (n > n2) {
12465 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012466 m2 <<= 1;
12467 n2 <<= 1;
12468 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012469 } else if (n < n2) {
12470 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012471 m <<= 1;
12472 n <<= 1;
12473 }
12474 }
12475
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012476 if (n != n2)
12477 return false;
12478
12479 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012480}
12481
12482static bool
12483intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12484 struct intel_link_m_n *m2_n2,
12485 bool adjust)
12486{
12487 if (m_n->tu == m2_n2->tu &&
12488 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12489 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12490 intel_compare_m_n(m_n->link_m, m_n->link_n,
12491 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12492 if (adjust)
12493 *m2_n2 = *m_n;
12494
12495 return true;
12496 }
12497
12498 return false;
12499}
12500
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012501static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012502intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012503 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012504 struct intel_crtc_state *pipe_config,
12505 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012506{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012507 bool ret = true;
12508
12509#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12510 do { \
12511 if (!adjust) \
12512 DRM_ERROR(fmt, ##__VA_ARGS__); \
12513 else \
12514 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12515 } while (0)
12516
Daniel Vetter66e985c2013-06-05 13:34:20 +020012517#define PIPE_CONF_CHECK_X(name) \
12518 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012519 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012520 "(expected 0x%08x, found 0x%08x)\n", \
12521 current_config->name, \
12522 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012523 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012524 }
12525
Daniel Vetter08a24032013-04-19 11:25:34 +020012526#define PIPE_CONF_CHECK_I(name) \
12527 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012528 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012529 "(expected %i, found %i)\n", \
12530 current_config->name, \
12531 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012532 ret = false; \
12533 }
12534
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012535#define PIPE_CONF_CHECK_P(name) \
12536 if (current_config->name != pipe_config->name) { \
12537 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12538 "(expected %p, found %p)\n", \
12539 current_config->name, \
12540 pipe_config->name); \
12541 ret = false; \
12542 }
12543
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012544#define PIPE_CONF_CHECK_M_N(name) \
12545 if (!intel_compare_link_m_n(&current_config->name, \
12546 &pipe_config->name,\
12547 adjust)) { \
12548 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12549 "(expected tu %i gmch %i/%i link %i/%i, " \
12550 "found tu %i, gmch %i/%i link %i/%i)\n", \
12551 current_config->name.tu, \
12552 current_config->name.gmch_m, \
12553 current_config->name.gmch_n, \
12554 current_config->name.link_m, \
12555 current_config->name.link_n, \
12556 pipe_config->name.tu, \
12557 pipe_config->name.gmch_m, \
12558 pipe_config->name.gmch_n, \
12559 pipe_config->name.link_m, \
12560 pipe_config->name.link_n); \
12561 ret = false; \
12562 }
12563
Daniel Vetter55c561a2016-03-30 11:34:36 +020012564/* This is required for BDW+ where there is only one set of registers for
12565 * switching between high and low RR.
12566 * This macro can be used whenever a comparison has to be made between one
12567 * hw state and multiple sw state variables.
12568 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012569#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12570 if (!intel_compare_link_m_n(&current_config->name, \
12571 &pipe_config->name, adjust) && \
12572 !intel_compare_link_m_n(&current_config->alt_name, \
12573 &pipe_config->name, adjust)) { \
12574 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12575 "(expected tu %i gmch %i/%i link %i/%i, " \
12576 "or tu %i gmch %i/%i link %i/%i, " \
12577 "found tu %i, gmch %i/%i link %i/%i)\n", \
12578 current_config->name.tu, \
12579 current_config->name.gmch_m, \
12580 current_config->name.gmch_n, \
12581 current_config->name.link_m, \
12582 current_config->name.link_n, \
12583 current_config->alt_name.tu, \
12584 current_config->alt_name.gmch_m, \
12585 current_config->alt_name.gmch_n, \
12586 current_config->alt_name.link_m, \
12587 current_config->alt_name.link_n, \
12588 pipe_config->name.tu, \
12589 pipe_config->name.gmch_m, \
12590 pipe_config->name.gmch_n, \
12591 pipe_config->name.link_m, \
12592 pipe_config->name.link_n); \
12593 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012594 }
12595
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012596#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12597 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012598 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012599 "(expected %i, found %i)\n", \
12600 current_config->name & (mask), \
12601 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012602 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012603 }
12604
Ville Syrjälä5e550652013-09-06 23:29:07 +030012605#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12606 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012607 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012608 "(expected %i, found %i)\n", \
12609 current_config->name, \
12610 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012611 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012612 }
12613
Daniel Vetterbb760062013-06-06 14:55:52 +020012614#define PIPE_CONF_QUIRK(quirk) \
12615 ((current_config->quirks | pipe_config->quirks) & (quirk))
12616
Daniel Vettereccb1402013-05-22 00:50:22 +020012617 PIPE_CONF_CHECK_I(cpu_transcoder);
12618
Daniel Vetter08a24032013-04-19 11:25:34 +020012619 PIPE_CONF_CHECK_I(has_pch_encoder);
12620 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012621 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012622
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012623 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012624 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012625
12626 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012627 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012628
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012629 if (current_config->has_drrs)
12630 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12631 } else
12632 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012633
Jani Nikulaa65347b2015-11-27 12:21:46 +020012634 PIPE_CONF_CHECK_I(has_dsi_encoder);
12635
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012636 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12637 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12638 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12639 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12640 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12641 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012642
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012643 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12644 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12645 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12646 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12647 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12648 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012649
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012650 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012651 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012652 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012653 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012654 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012655 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012656
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012657 PIPE_CONF_CHECK_I(has_audio);
12658
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012659 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012660 DRM_MODE_FLAG_INTERLACE);
12661
Daniel Vetterbb760062013-06-06 14:55:52 +020012662 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012663 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012664 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012665 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012666 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012667 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012668 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012669 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012670 DRM_MODE_FLAG_NVSYNC);
12671 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012672
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012673 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012674 /* pfit ratios are autocomputed by the hw on gen4+ */
12675 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020012676 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012677 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012678
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012679 if (!adjust) {
12680 PIPE_CONF_CHECK_I(pipe_src_w);
12681 PIPE_CONF_CHECK_I(pipe_src_h);
12682
12683 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12684 if (current_config->pch_pfit.enabled) {
12685 PIPE_CONF_CHECK_X(pch_pfit.pos);
12686 PIPE_CONF_CHECK_X(pch_pfit.size);
12687 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012688
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012689 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12690 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012691
Jesse Barnese59150d2014-01-07 13:30:45 -080012692 /* BDW+ don't expose a synchronous way to read the state */
12693 if (IS_HASWELL(dev))
12694 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012695
Ville Syrjälä282740f2013-09-04 18:30:03 +030012696 PIPE_CONF_CHECK_I(double_wide);
12697
Daniel Vetter26804af2014-06-25 22:01:55 +030012698 PIPE_CONF_CHECK_X(ddi_pll_sel);
12699
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012700 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012701 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012702 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012703 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12704 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012705 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012706 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012707 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12708 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12709 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012710
Ville Syrjälä47eacba2016-04-12 22:14:35 +030012711 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12712 PIPE_CONF_CHECK_X(dsi_pll.div);
12713
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012714 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12715 PIPE_CONF_CHECK_I(pipe_bpp);
12716
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012717 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012718 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012719
Daniel Vetter66e985c2013-06-05 13:34:20 +020012720#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012721#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012722#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012723#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012724#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012725#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012726#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012727
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012728 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012729}
12730
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012731static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12732 const struct intel_crtc_state *pipe_config)
12733{
12734 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012735 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012736 &pipe_config->fdi_m_n);
12737 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12738
12739 /*
12740 * FDI already provided one idea for the dotclock.
12741 * Yell if the encoder disagrees.
12742 */
12743 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12744 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12745 fdi_dotclock, dotclock);
12746 }
12747}
12748
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012749static void verify_wm_state(struct drm_crtc *crtc,
12750 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000012751{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012752 struct drm_device *dev = crtc->dev;
Damien Lespiau08db6652014-11-04 17:06:52 +000012753 struct drm_i915_private *dev_priv = dev->dev_private;
12754 struct skl_ddb_allocation hw_ddb, *sw_ddb;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012755 struct skl_ddb_entry *hw_entry, *sw_entry;
12756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12757 const enum pipe pipe = intel_crtc->pipe;
Damien Lespiau08db6652014-11-04 17:06:52 +000012758 int plane;
12759
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012760 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000012761 return;
12762
12763 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12764 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12765
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012766 /* planes */
12767 for_each_plane(dev_priv, pipe, plane) {
12768 hw_entry = &hw_ddb.plane[pipe][plane];
12769 sw_entry = &sw_ddb->plane[pipe][plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000012770
12771 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12772 continue;
12773
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012774 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12775 "(expected (%u,%u), found (%u,%u))\n",
12776 pipe_name(pipe), plane + 1,
12777 sw_entry->start, sw_entry->end,
12778 hw_entry->start, hw_entry->end);
12779 }
12780
12781 /* cursor */
12782 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12783 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
12784
12785 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012786 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12787 "(expected (%u,%u), found (%u,%u))\n",
12788 pipe_name(pipe),
12789 sw_entry->start, sw_entry->end,
12790 hw_entry->start, hw_entry->end);
12791 }
12792}
12793
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012794static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012795verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012796{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012797 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012798
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012799 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012800 struct drm_encoder *encoder = connector->encoder;
12801 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012802
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012803 if (state->crtc != crtc)
12804 continue;
12805
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012806 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012807
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012808 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012809 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012810 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012811}
12812
12813static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012814verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012815{
12816 struct intel_encoder *encoder;
12817 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012818
Damien Lespiaub2784e12014-08-05 11:29:37 +010012819 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012820 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012821 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012822
12823 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12824 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012825 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012826
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012827 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012828 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012829 continue;
12830 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012831
12832 I915_STATE_WARN(connector->base.state->crtc !=
12833 encoder->base.crtc,
12834 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012835 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012836
Rob Clarke2c719b2014-12-15 13:56:32 -050012837 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012838 "encoder's enabled state mismatch "
12839 "(expected %i, found %i)\n",
12840 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012841
12842 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012843 bool active;
12844
12845 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012846 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012847 "encoder detached but still enabled on pipe %c.\n",
12848 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012849 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012850 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012851}
12852
12853static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012854verify_crtc_state(struct drm_crtc *crtc,
12855 struct drm_crtc_state *old_crtc_state,
12856 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012857{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012858 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012859 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012860 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12862 struct intel_crtc_state *pipe_config, *sw_config;
12863 struct drm_atomic_state *old_state;
12864 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012865
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012866 old_state = old_crtc_state->state;
12867 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12868 pipe_config = to_intel_crtc_state(old_crtc_state);
12869 memset(pipe_config, 0, sizeof(*pipe_config));
12870 pipe_config->base.crtc = crtc;
12871 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012872
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012873 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012874
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012875 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012876
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012877 /* hw state is inconsistent with the pipe quirk */
12878 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12879 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12880 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012881
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012882 I915_STATE_WARN(new_crtc_state->active != active,
12883 "crtc active state doesn't match with hw state "
12884 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012885
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012886 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
12887 "transitional active state does not match atomic hw state "
12888 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012889
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012890 for_each_encoder_on_crtc(dev, crtc, encoder) {
12891 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012892
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012893 active = encoder->get_hw_state(encoder, &pipe);
12894 I915_STATE_WARN(active != new_crtc_state->active,
12895 "[ENCODER:%i] active %i with crtc active %i\n",
12896 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012897
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012898 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12899 "Encoder connected to wrong pipe %c\n",
12900 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012901
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012902 if (active)
12903 encoder->get_config(encoder, pipe_config);
12904 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012905
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012906 if (!new_crtc_state->active)
12907 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012908
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012909 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012910
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012911 sw_config = to_intel_crtc_state(crtc->state);
12912 if (!intel_pipe_config_compare(dev, sw_config,
12913 pipe_config, false)) {
12914 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12915 intel_dump_pipe_config(intel_crtc, pipe_config,
12916 "[hw state]");
12917 intel_dump_pipe_config(intel_crtc, sw_config,
12918 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012919 }
12920}
12921
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012922static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012923verify_single_dpll_state(struct drm_i915_private *dev_priv,
12924 struct intel_shared_dpll *pll,
12925 struct drm_crtc *crtc,
12926 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012927{
12928 struct intel_dpll_hw_state dpll_hw_state;
12929 unsigned crtc_mask;
12930 bool active;
12931
12932 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12933
12934 DRM_DEBUG_KMS("%s\n", pll->name);
12935
12936 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
12937
12938 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
12939 I915_STATE_WARN(!pll->on && pll->active_mask,
12940 "pll in active use but not on in sw tracking\n");
12941 I915_STATE_WARN(pll->on && !pll->active_mask,
12942 "pll is on but not used by any active crtc\n");
12943 I915_STATE_WARN(pll->on != active,
12944 "pll on state mismatch (expected %i, found %i)\n",
12945 pll->on, active);
12946 }
12947
12948 if (!crtc) {
12949 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
12950 "more active pll users than references: %x vs %x\n",
12951 pll->active_mask, pll->config.crtc_mask);
12952
12953 return;
12954 }
12955
12956 crtc_mask = 1 << drm_crtc_index(crtc);
12957
12958 if (new_state->active)
12959 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12960 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12961 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12962 else
12963 I915_STATE_WARN(pll->active_mask & crtc_mask,
12964 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12965 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12966
12967 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
12968 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
12969 crtc_mask, pll->config.crtc_mask);
12970
12971 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
12972 &dpll_hw_state,
12973 sizeof(dpll_hw_state)),
12974 "pll hw state mismatch\n");
12975}
12976
12977static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012978verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12979 struct drm_crtc_state *old_crtc_state,
12980 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012981{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012982 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012983 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
12984 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
12985
12986 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012987 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012988
12989 if (old_state->shared_dpll &&
12990 old_state->shared_dpll != new_state->shared_dpll) {
12991 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
12992 struct intel_shared_dpll *pll = old_state->shared_dpll;
12993
12994 I915_STATE_WARN(pll->active_mask & crtc_mask,
12995 "pll active mismatch (didn't expect pipe %c in active mask)\n",
12996 pipe_name(drm_crtc_index(crtc)));
12997 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
12998 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
12999 pipe_name(drm_crtc_index(crtc)));
13000 }
13001}
13002
13003static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013004intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013005 struct drm_crtc_state *old_state,
13006 struct drm_crtc_state *new_state)
13007{
13008 if (!needs_modeset(new_state) &&
13009 !to_intel_crtc_state(new_state)->update_pipe)
13010 return;
13011
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013012 verify_wm_state(crtc, new_state);
13013 verify_connector_state(crtc->dev, crtc);
13014 verify_crtc_state(crtc, old_state, new_state);
13015 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013016}
13017
13018static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013019verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013020{
13021 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013022 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013023
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013024 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013025 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013026}
Daniel Vetter53589012013-06-05 13:34:16 +020013027
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013028static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013029intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013030{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013031 verify_encoder_state(dev);
13032 verify_connector_state(dev, NULL);
13033 verify_disabled_dpll_state(dev);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013034}
13035
Ville Syrjälä80715b22014-05-15 20:23:23 +030013036static void update_scanline_offset(struct intel_crtc *crtc)
13037{
13038 struct drm_device *dev = crtc->base.dev;
13039
13040 /*
13041 * The scanline counter increments at the leading edge of hsync.
13042 *
13043 * On most platforms it starts counting from vtotal-1 on the
13044 * first active line. That means the scanline counter value is
13045 * always one less than what we would expect. Ie. just after
13046 * start of vblank, which also occurs at start of hsync (on the
13047 * last active line), the scanline counter will read vblank_start-1.
13048 *
13049 * On gen2 the scanline counter starts counting from 1 instead
13050 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13051 * to keep the value positive), instead of adding one.
13052 *
13053 * On HSW+ the behaviour of the scanline counter depends on the output
13054 * type. For DP ports it behaves like most other platforms, but on HDMI
13055 * there's an extra 1 line difference. So we need to add two instead of
13056 * one to the value.
13057 */
13058 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013059 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013060 int vtotal;
13061
Ville Syrjälä124abe02015-09-08 13:40:45 +030013062 vtotal = adjusted_mode->crtc_vtotal;
13063 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013064 vtotal /= 2;
13065
13066 crtc->scanline_offset = vtotal - 1;
13067 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013068 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013069 crtc->scanline_offset = 2;
13070 } else
13071 crtc->scanline_offset = 1;
13072}
13073
Maarten Lankhorstad421372015-06-15 12:33:42 +020013074static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013075{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013076 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013077 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013078 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013079 struct drm_crtc *crtc;
13080 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013081 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013082
13083 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013084 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013085
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013086 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013087 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013088 struct intel_shared_dpll *old_dpll =
13089 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013090
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013091 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013092 continue;
13093
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013094 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013095
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013096 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013097 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013098
Maarten Lankhorstad421372015-06-15 12:33:42 +020013099 if (!shared_dpll)
13100 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13101
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013102 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013103 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013104}
13105
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013106/*
13107 * This implements the workaround described in the "notes" section of the mode
13108 * set sequence documentation. When going from no pipes or single pipe to
13109 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13110 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13111 */
13112static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13113{
13114 struct drm_crtc_state *crtc_state;
13115 struct intel_crtc *intel_crtc;
13116 struct drm_crtc *crtc;
13117 struct intel_crtc_state *first_crtc_state = NULL;
13118 struct intel_crtc_state *other_crtc_state = NULL;
13119 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13120 int i;
13121
13122 /* look at all crtc's that are going to be enabled in during modeset */
13123 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13124 intel_crtc = to_intel_crtc(crtc);
13125
13126 if (!crtc_state->active || !needs_modeset(crtc_state))
13127 continue;
13128
13129 if (first_crtc_state) {
13130 other_crtc_state = to_intel_crtc_state(crtc_state);
13131 break;
13132 } else {
13133 first_crtc_state = to_intel_crtc_state(crtc_state);
13134 first_pipe = intel_crtc->pipe;
13135 }
13136 }
13137
13138 /* No workaround needed? */
13139 if (!first_crtc_state)
13140 return 0;
13141
13142 /* w/a possibly needed, check how many crtc's are already enabled. */
13143 for_each_intel_crtc(state->dev, intel_crtc) {
13144 struct intel_crtc_state *pipe_config;
13145
13146 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13147 if (IS_ERR(pipe_config))
13148 return PTR_ERR(pipe_config);
13149
13150 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13151
13152 if (!pipe_config->base.active ||
13153 needs_modeset(&pipe_config->base))
13154 continue;
13155
13156 /* 2 or more enabled crtcs means no need for w/a */
13157 if (enabled_pipe != INVALID_PIPE)
13158 return 0;
13159
13160 enabled_pipe = intel_crtc->pipe;
13161 }
13162
13163 if (enabled_pipe != INVALID_PIPE)
13164 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13165 else if (other_crtc_state)
13166 other_crtc_state->hsw_workaround_pipe = first_pipe;
13167
13168 return 0;
13169}
13170
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013171static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13172{
13173 struct drm_crtc *crtc;
13174 struct drm_crtc_state *crtc_state;
13175 int ret = 0;
13176
13177 /* add all active pipes to the state */
13178 for_each_crtc(state->dev, crtc) {
13179 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13180 if (IS_ERR(crtc_state))
13181 return PTR_ERR(crtc_state);
13182
13183 if (!crtc_state->active || needs_modeset(crtc_state))
13184 continue;
13185
13186 crtc_state->mode_changed = true;
13187
13188 ret = drm_atomic_add_affected_connectors(state, crtc);
13189 if (ret)
13190 break;
13191
13192 ret = drm_atomic_add_affected_planes(state, crtc);
13193 if (ret)
13194 break;
13195 }
13196
13197 return ret;
13198}
13199
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013200static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013201{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013202 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13203 struct drm_i915_private *dev_priv = state->dev->dev_private;
13204 struct drm_crtc *crtc;
13205 struct drm_crtc_state *crtc_state;
13206 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013207
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013208 if (!check_digital_port_conflicts(state)) {
13209 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13210 return -EINVAL;
13211 }
13212
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013213 intel_state->modeset = true;
13214 intel_state->active_crtcs = dev_priv->active_crtcs;
13215
13216 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13217 if (crtc_state->active)
13218 intel_state->active_crtcs |= 1 << i;
13219 else
13220 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070013221
13222 if (crtc_state->active != crtc->state->active)
13223 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013224 }
13225
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013226 /*
13227 * See if the config requires any additional preparation, e.g.
13228 * to adjust global state with pipes off. We need to do this
13229 * here so we can get the modeset_pipe updated config for the new
13230 * mode set on this crtc. For other crtcs we need to use the
13231 * adjusted_mode bits in the crtc directly.
13232 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013233 if (dev_priv->display.modeset_calc_cdclk) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013234 ret = dev_priv->display.modeset_calc_cdclk(state);
13235
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013236 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013237 ret = intel_modeset_all_pipes(state);
13238
13239 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013240 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013241
13242 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13243 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013244 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013245 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013246
Maarten Lankhorstad421372015-06-15 12:33:42 +020013247 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013248
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013249 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013250 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013251
Maarten Lankhorstad421372015-06-15 12:33:42 +020013252 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013253}
13254
Matt Roperaa363132015-09-24 15:53:18 -070013255/*
13256 * Handle calculation of various watermark data at the end of the atomic check
13257 * phase. The code here should be run after the per-crtc and per-plane 'check'
13258 * handlers to ensure that all derived state has been updated.
13259 */
Matt Roper55994c22016-05-12 07:06:08 -070013260static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070013261{
13262 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070013263 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070013264
13265 /* Is there platform-specific watermark information to calculate? */
13266 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070013267 return dev_priv->display.compute_global_watermarks(state);
13268
13269 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070013270}
13271
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013272/**
13273 * intel_atomic_check - validate state object
13274 * @dev: drm device
13275 * @state: state to validate
13276 */
13277static int intel_atomic_check(struct drm_device *dev,
13278 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013279{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013280 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013281 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013282 struct drm_crtc *crtc;
13283 struct drm_crtc_state *crtc_state;
13284 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013285 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013286
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013287 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013288 if (ret)
13289 return ret;
13290
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013291 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013292 struct intel_crtc_state *pipe_config =
13293 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013294
13295 /* Catch I915_MODE_FLAG_INHERITED */
13296 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13297 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013298
Daniel Vetter26495482015-07-15 14:15:52 +020013299 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013300 continue;
13301
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013302 if (!crtc_state->enable) {
13303 any_ms = true;
13304 continue;
13305 }
13306
Daniel Vetter26495482015-07-15 14:15:52 +020013307 /* FIXME: For only active_changed we shouldn't need to do any
13308 * state recomputation at all. */
13309
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013310 ret = drm_atomic_add_affected_connectors(state, crtc);
13311 if (ret)
13312 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013313
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013314 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013315 if (ret) {
13316 intel_dump_pipe_config(to_intel_crtc(crtc),
13317 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013318 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013319 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013320
Jani Nikula73831232015-11-19 10:26:30 +020013321 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013322 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013323 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013324 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013325 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013326 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013327 }
13328
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013329 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020013330 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013331
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013332 ret = drm_atomic_add_affected_planes(state, crtc);
13333 if (ret)
13334 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013335
Daniel Vetter26495482015-07-15 14:15:52 +020013336 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13337 needs_modeset(crtc_state) ?
13338 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013339 }
13340
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013341 if (any_ms) {
13342 ret = intel_modeset_checks(state);
13343
13344 if (ret)
13345 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013346 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013347 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013348
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013349 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013350 if (ret)
13351 return ret;
13352
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013353 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070013354 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013355}
13356
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013357static int intel_atomic_prepare_commit(struct drm_device *dev,
13358 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013359 bool nonblock)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013360{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013361 struct drm_i915_private *dev_priv = dev->dev_private;
13362 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013363 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013364 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013365 struct drm_crtc *crtc;
13366 int i, ret;
13367
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013368 if (nonblock) {
13369 DRM_DEBUG_KMS("i915 does not yet support nonblocking commit\n");
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013370 return -EINVAL;
13371 }
13372
13373 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Chris Wilsonacf4e842016-04-17 20:42:46 +010013374 if (state->legacy_cursor_update)
13375 continue;
13376
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013377 ret = intel_crtc_wait_for_pending_flips(crtc);
13378 if (ret)
13379 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013380
13381 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13382 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013383 }
13384
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013385 ret = mutex_lock_interruptible(&dev->struct_mutex);
13386 if (ret)
13387 return ret;
13388
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013389 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013390 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013391
Dave Airlie21daaee2016-05-05 09:56:30 +100013392 if (!ret && !nonblock) {
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013393 for_each_plane_in_state(state, plane, plane_state, i) {
13394 struct intel_plane_state *intel_plane_state =
13395 to_intel_plane_state(plane_state);
13396
13397 if (!intel_plane_state->wait_req)
13398 continue;
13399
13400 ret = __i915_wait_request(intel_plane_state->wait_req,
Chris Wilson299259a2016-04-13 17:35:06 +010013401 true, NULL, NULL);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013402 if (ret) {
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013403 /* Any hang should be swallowed by the wait */
13404 WARN_ON(ret == -EIO);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013405 mutex_lock(&dev->struct_mutex);
13406 drm_atomic_helper_cleanup_planes(dev, state);
13407 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013408 break;
Chris Wilsonf7e58382016-04-13 17:35:07 +010013409 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013410 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013411 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013412
13413 return ret;
13414}
13415
Maarten Lankhorste8861672016-02-24 11:24:26 +010013416static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13417 struct drm_i915_private *dev_priv,
13418 unsigned crtc_mask)
13419{
13420 unsigned last_vblank_count[I915_MAX_PIPES];
13421 enum pipe pipe;
13422 int ret;
13423
13424 if (!crtc_mask)
13425 return;
13426
13427 for_each_pipe(dev_priv, pipe) {
13428 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13429
13430 if (!((1 << pipe) & crtc_mask))
13431 continue;
13432
13433 ret = drm_crtc_vblank_get(crtc);
13434 if (WARN_ON(ret != 0)) {
13435 crtc_mask &= ~(1 << pipe);
13436 continue;
13437 }
13438
13439 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13440 }
13441
13442 for_each_pipe(dev_priv, pipe) {
13443 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13444 long lret;
13445
13446 if (!((1 << pipe) & crtc_mask))
13447 continue;
13448
13449 lret = wait_event_timeout(dev->vblank[pipe].queue,
13450 last_vblank_count[pipe] !=
13451 drm_crtc_vblank_count(crtc),
13452 msecs_to_jiffies(50));
13453
Ville Syrjälä8a8dae22016-04-18 14:29:32 +030013454 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
Maarten Lankhorste8861672016-02-24 11:24:26 +010013455
13456 drm_crtc_vblank_put(crtc);
13457 }
13458}
13459
13460static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13461{
13462 /* fb updated, need to unpin old fb */
13463 if (crtc_state->fb_changed)
13464 return true;
13465
13466 /* wm changes, need vblank before final wm's */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013467 if (crtc_state->update_wm_post)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013468 return true;
13469
13470 /*
13471 * cxsr is re-enabled after vblank.
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013472 * This is already handled by crtc_state->update_wm_post,
Maarten Lankhorste8861672016-02-24 11:24:26 +010013473 * but added for clarity.
13474 */
13475 if (crtc_state->disable_cxsr)
13476 return true;
13477
13478 return false;
13479}
13480
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013481/**
13482 * intel_atomic_commit - commit validated state object
13483 * @dev: DRM device
13484 * @state: the top-level driver state object
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013485 * @nonblock: nonblocking commit
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013486 *
13487 * This function commits a top-level state object that has been validated
13488 * with drm_atomic_helper_check().
13489 *
13490 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13491 * we can only handle plane-related operations and do not yet support
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013492 * nonblocking commit.
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013493 *
13494 * RETURNS
13495 * Zero for success or -errno.
13496 */
13497static int intel_atomic_commit(struct drm_device *dev,
13498 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013499 bool nonblock)
Daniel Vettera6778b32012-07-02 09:56:42 +020013500{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013501 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013502 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013503 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013504 struct drm_crtc *crtc;
Matt Ropered4a6a72016-02-23 17:20:13 -080013505 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013506 int ret = 0, i;
13507 bool hw_check = intel_state->modeset;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013508 unsigned long put_domains[I915_MAX_PIPES] = {};
Maarten Lankhorste8861672016-02-24 11:24:26 +010013509 unsigned crtc_vblank_mask = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013510
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013511 ret = intel_atomic_prepare_commit(dev, state, nonblock);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013512 if (ret) {
13513 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013514 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013515 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013516
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013517 drm_atomic_helper_swap_state(dev, state);
Matt Roper279e99d2016-05-12 07:06:02 -070013518 dev_priv->wm.distrust_bios_wm = false;
Matt Roper734fa012016-05-12 15:11:40 -070013519 dev_priv->wm.skl_results = intel_state->wm_results;
Maarten Lankhorsta1475e72016-03-14 09:27:53 +010013520 intel_shared_dpll_commit(state);
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013521
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013522 if (intel_state->modeset) {
13523 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13524 sizeof(intel_state->min_pixclk));
13525 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013526 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013527
13528 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013529 }
13530
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013531 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013532 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13533
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013534 if (needs_modeset(crtc->state) ||
13535 to_intel_crtc_state(crtc->state)->update_pipe) {
13536 hw_check = true;
13537
13538 put_domains[to_intel_crtc(crtc)->pipe] =
13539 modeset_get_crtc_power_domains(crtc,
13540 to_intel_crtc_state(crtc->state));
13541 }
13542
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013543 if (!needs_modeset(crtc->state))
13544 continue;
13545
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013546 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013547
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013548 if (old_crtc_state->active) {
13549 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013550 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013551 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013552 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013553 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013554
13555 /*
13556 * Underruns don't always raise
13557 * interrupts, so check manually.
13558 */
13559 intel_check_cpu_fifo_underruns(dev_priv);
13560 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013561
13562 if (!crtc->state->active)
13563 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013564 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013565 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013566
Daniel Vetterea9d7582012-07-10 10:42:52 +020013567 /* Only after disabling all output pipelines that will be changed can we
13568 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013569 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013570
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013571 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013572 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013573
13574 if (dev_priv->display.modeset_commit_cdclk &&
13575 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13576 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013577
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013578 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013579 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013580
Daniel Vettera6778b32012-07-02 09:56:42 +020013581 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013582 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013583 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13584 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorste8861672016-02-24 11:24:26 +010013585 struct intel_crtc_state *pipe_config =
13586 to_intel_crtc_state(crtc->state);
13587 bool update_pipe = !modeset && pipe_config->update_pipe;
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013588
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013589 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013590 update_scanline_offset(to_intel_crtc(crtc));
13591 dev_priv->display.crtc_enable(crtc);
13592 }
13593
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013594 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013595 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013596
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010013597 if (crtc->state->active &&
13598 drm_atomic_get_existing_plane_state(state, crtc->primary))
Paulo Zanoni49227c42016-01-19 11:35:52 -020013599 intel_fbc_enable(intel_crtc);
13600
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013601 if (crtc->state->active &&
13602 (crtc->state->planes_changed || update_pipe))
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013603 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013604
Maarten Lankhorste8861672016-02-24 11:24:26 +010013605 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13606 crtc_vblank_mask |= 1 << i;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013607 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013608
Daniel Vettera6778b32012-07-02 09:56:42 +020013609 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013610
Maarten Lankhorste8861672016-02-24 11:24:26 +010013611 if (!state->legacy_cursor_update)
13612 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013613
Matt Ropered4a6a72016-02-23 17:20:13 -080013614 /*
13615 * Now that the vblank has passed, we can go ahead and program the
13616 * optimal watermarks on platforms that need two-step watermark
13617 * programming.
13618 *
13619 * TODO: Move this (and other cleanup) to an async worker eventually.
13620 */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013621 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Matt Ropered4a6a72016-02-23 17:20:13 -080013622 intel_cstate = to_intel_crtc_state(crtc->state);
13623
13624 if (dev_priv->display.optimize_watermarks)
13625 dev_priv->display.optimize_watermarks(intel_cstate);
13626 }
13627
Matt Roper177246a2016-03-04 15:59:39 -080013628 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13629 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13630
13631 if (put_domains[i])
13632 modeset_put_power_domains(dev_priv, put_domains[i]);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013633
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013634 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
Matt Roper177246a2016-03-04 15:59:39 -080013635 }
13636
13637 if (intel_state->modeset)
13638 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13639
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013640 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013641 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013642 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013643
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013644 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013645
Mika Kuoppala75714942015-12-16 09:26:48 +020013646 /* As one of the primary mmio accessors, KMS has a high likelihood
13647 * of triggering bugs in unclaimed access. After we finish
13648 * modesetting, see if an error has been flagged, and if so
13649 * enable debugging for the next modeset - and hope we catch
13650 * the culprit.
13651 *
13652 * XXX note that we assume display power is on at this point.
13653 * This might hold true now but we need to add pm helper to check
13654 * unclaimed only when the hardware is on, as atomic commits
13655 * can happen also when the device is completely off.
13656 */
13657 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13658
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013659 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013660}
13661
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013662void intel_crtc_restore_mode(struct drm_crtc *crtc)
13663{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013664 struct drm_device *dev = crtc->dev;
13665 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013666 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013667 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013668
13669 state = drm_atomic_state_alloc(dev);
13670 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013671 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013672 crtc->base.id);
13673 return;
13674 }
13675
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013676 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013677
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013678retry:
13679 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13680 ret = PTR_ERR_OR_ZERO(crtc_state);
13681 if (!ret) {
13682 if (!crtc_state->active)
13683 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013684
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013685 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013686 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013687 }
13688
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013689 if (ret == -EDEADLK) {
13690 drm_atomic_state_clear(state);
13691 drm_modeset_backoff(state->acquire_ctx);
13692 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013693 }
13694
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013695 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013696out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013697 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013698}
13699
Daniel Vetter25c5b262012-07-08 22:08:04 +020013700#undef for_each_intel_crtc_masked
13701
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013702static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013703 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013704 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013705 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013706 .destroy = intel_crtc_destroy,
13707 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013708 .atomic_duplicate_state = intel_crtc_duplicate_state,
13709 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013710};
13711
Matt Roper6beb8c232014-12-01 15:40:14 -080013712/**
13713 * intel_prepare_plane_fb - Prepare fb for usage on plane
13714 * @plane: drm plane to prepare for
13715 * @fb: framebuffer to prepare for presentation
13716 *
13717 * Prepares a framebuffer for usage on a display plane. Generally this
13718 * involves pinning the underlying object and updating the frontbuffer tracking
13719 * bits. Some older platforms need special physical address handling for
13720 * cursor planes.
13721 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013722 * Must be called with struct_mutex held.
13723 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013724 * Returns 0 on success, negative error code on failure.
13725 */
13726int
13727intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013728 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013729{
13730 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013731 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013732 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013733 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013734 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013735 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013736
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013737 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013738 return 0;
13739
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013740 if (old_obj) {
13741 struct drm_crtc_state *crtc_state =
13742 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13743
13744 /* Big Hammer, we also need to ensure that any pending
13745 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13746 * current scanout is retired before unpinning the old
13747 * framebuffer. Note that we rely on userspace rendering
13748 * into the buffer attached to the pipe they are waiting
13749 * on. If not, userspace generates a GPU hang with IPEHR
13750 * point to the MI_WAIT_FOR_EVENT.
13751 *
13752 * This should only fail upon a hung GPU, in which case we
13753 * can safely continue.
13754 */
13755 if (needs_modeset(crtc_state))
13756 ret = i915_gem_object_wait_rendering(old_obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013757 if (ret) {
13758 /* GPU hangs should have been swallowed by the wait */
13759 WARN_ON(ret == -EIO);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013760 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013761 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013762 }
13763
Alex Goins3c28ff22015-11-25 18:43:39 -080013764 /* For framebuffer backed by dmabuf, wait for fence */
13765 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013766 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080013767
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013768 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13769 false, true,
13770 MAX_SCHEDULE_TIMEOUT);
13771 if (lret == -ERESTARTSYS)
13772 return lret;
13773
13774 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080013775 }
13776
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013777 if (!obj) {
13778 ret = 0;
13779 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013780 INTEL_INFO(dev)->cursor_needs_physical) {
13781 int align = IS_I830(dev) ? 16 * 1024 : 256;
13782 ret = i915_gem_object_attach_phys(obj, align);
13783 if (ret)
13784 DRM_DEBUG_KMS("failed to attach phys object\n");
13785 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020013786 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080013787 }
13788
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013789 if (ret == 0) {
13790 if (obj) {
13791 struct intel_plane_state *plane_state =
13792 to_intel_plane_state(new_state);
13793
13794 i915_gem_request_assign(&plane_state->wait_req,
13795 obj->last_write_req);
13796 }
13797
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013798 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013799 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013800
Matt Roper6beb8c232014-12-01 15:40:14 -080013801 return ret;
13802}
13803
Matt Roper38f3ce32014-12-02 07:45:25 -080013804/**
13805 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13806 * @plane: drm plane to clean up for
13807 * @fb: old framebuffer that was on plane
13808 *
13809 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013810 *
13811 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013812 */
13813void
13814intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013815 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013816{
13817 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013818 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013819 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013820 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13821 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013822
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013823 old_intel_state = to_intel_plane_state(old_state);
13824
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013825 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013826 return;
13827
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013828 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13829 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020013830 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013831
13832 /* prepare_fb aborted? */
13833 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13834 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13835 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013836
13837 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070013838}
13839
Chandra Konduru6156a452015-04-27 13:48:39 -070013840int
13841skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13842{
13843 int max_scale;
13844 struct drm_device *dev;
13845 struct drm_i915_private *dev_priv;
13846 int crtc_clock, cdclk;
13847
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013848 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013849 return DRM_PLANE_HELPER_NO_SCALING;
13850
13851 dev = intel_crtc->base.dev;
13852 dev_priv = dev->dev_private;
13853 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013854 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013855
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010013856 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013857 return DRM_PLANE_HELPER_NO_SCALING;
13858
13859 /*
13860 * skl max scale is lower of:
13861 * close to 3 but not 3, -1 is for that purpose
13862 * or
13863 * cdclk/crtc_clock
13864 */
13865 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13866
13867 return max_scale;
13868}
13869
Matt Roper465c1202014-05-29 08:06:54 -070013870static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013871intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013872 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013873 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013874{
Matt Roper2b875c22014-12-01 15:40:13 -080013875 struct drm_crtc *crtc = state->base.crtc;
13876 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013877 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013878 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13879 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013880
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013881 if (INTEL_INFO(plane->dev)->gen >= 9) {
13882 /* use scaler when colorkey is not required */
13883 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13884 min_scale = 1;
13885 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13886 }
Sonika Jindald8106362015-04-10 14:37:28 +053013887 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013888 }
Sonika Jindald8106362015-04-10 14:37:28 +053013889
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013890 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13891 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013892 min_scale, max_scale,
13893 can_position, true,
13894 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013895}
13896
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013897static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13898 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013899{
13900 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080013901 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013902 struct intel_crtc_state *old_intel_state =
13903 to_intel_crtc_state(old_crtc_state);
13904 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013905
Matt Roperc34c9ee2014-12-23 10:41:50 -080013906 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020013907 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013908
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013909 if (modeset)
13910 return;
13911
Maarten Lankhorst20a34e72016-03-30 17:16:36 +020013912 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
13913 intel_color_set_csc(crtc->state);
13914 intel_color_load_luts(crtc->state);
13915 }
13916
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013917 if (to_intel_crtc_state(crtc->state)->update_pipe)
13918 intel_update_pipe_config(intel_crtc, old_intel_state);
13919 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020013920 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013921}
13922
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013923static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13924 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013925{
Matt Roper32b7eee2014-12-24 07:59:06 -080013926 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013927
Maarten Lankhorst62852622015-09-23 16:29:38 +020013928 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013929}
13930
Matt Ropercf4c7c12014-12-04 10:27:42 -080013931/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013932 * intel_plane_destroy - destroy a plane
13933 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013934 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013935 * Common destruction function for all types of planes (primary, cursor,
13936 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013937 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013938void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013939{
13940 struct intel_plane *intel_plane = to_intel_plane(plane);
13941 drm_plane_cleanup(plane);
13942 kfree(intel_plane);
13943}
13944
Matt Roper65a3fea2015-01-21 16:35:42 -080013945const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013946 .update_plane = drm_atomic_helper_update_plane,
13947 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013948 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013949 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013950 .atomic_get_property = intel_plane_atomic_get_property,
13951 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013952 .atomic_duplicate_state = intel_plane_duplicate_state,
13953 .atomic_destroy_state = intel_plane_destroy_state,
13954
Matt Roper465c1202014-05-29 08:06:54 -070013955};
13956
13957static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13958 int pipe)
13959{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013960 struct intel_plane *primary = NULL;
13961 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013962 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020013963 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013964 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070013965
13966 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013967 if (!primary)
13968 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070013969
Matt Roper8e7d6882015-01-21 16:35:41 -080013970 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013971 if (!state)
13972 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080013973 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013974
Matt Roper465c1202014-05-29 08:06:54 -070013975 primary->can_scale = false;
13976 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013977 if (INTEL_INFO(dev)->gen >= 9) {
13978 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013979 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013980 }
Matt Roper465c1202014-05-29 08:06:54 -070013981 primary->pipe = pipe;
13982 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013983 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013984 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013985 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13986 primary->plane = !pipe;
13987
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013988 if (INTEL_INFO(dev)->gen >= 9) {
13989 intel_primary_formats = skl_primary_formats;
13990 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013991
13992 primary->update_plane = skylake_update_primary_plane;
13993 primary->disable_plane = skylake_disable_primary_plane;
13994 } else if (HAS_PCH_SPLIT(dev)) {
13995 intel_primary_formats = i965_primary_formats;
13996 num_formats = ARRAY_SIZE(i965_primary_formats);
13997
13998 primary->update_plane = ironlake_update_primary_plane;
13999 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014000 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014001 intel_primary_formats = i965_primary_formats;
14002 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014003
14004 primary->update_plane = i9xx_update_primary_plane;
14005 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014006 } else {
14007 intel_primary_formats = i8xx_primary_formats;
14008 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014009
14010 primary->update_plane = i9xx_update_primary_plane;
14011 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014012 }
14013
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014014 ret = drm_universal_plane_init(dev, &primary->base, 0,
14015 &intel_plane_funcs,
14016 intel_primary_formats, num_formats,
14017 DRM_PLANE_TYPE_PRIMARY, NULL);
14018 if (ret)
14019 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053014020
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014021 if (INTEL_INFO(dev)->gen >= 4)
14022 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014023
Matt Roperea2c67b2014-12-23 10:41:52 -080014024 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14025
Matt Roper465c1202014-05-29 08:06:54 -070014026 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014027
14028fail:
14029 kfree(state);
14030 kfree(primary);
14031
14032 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014033}
14034
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014035void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14036{
14037 if (!dev->mode_config.rotation_property) {
14038 unsigned long flags = BIT(DRM_ROTATE_0) |
14039 BIT(DRM_ROTATE_180);
14040
14041 if (INTEL_INFO(dev)->gen >= 9)
14042 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14043
14044 dev->mode_config.rotation_property =
14045 drm_mode_create_rotation_property(dev, flags);
14046 }
14047 if (dev->mode_config.rotation_property)
14048 drm_object_attach_property(&plane->base.base,
14049 dev->mode_config.rotation_property,
14050 plane->base.state->rotation);
14051}
14052
Matt Roper3d7d6512014-06-10 08:28:13 -070014053static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014054intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014055 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014056 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014057{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014058 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014059 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014060 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014061 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014062 unsigned stride;
14063 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014064
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014065 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14066 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014067 DRM_PLANE_HELPER_NO_SCALING,
14068 DRM_PLANE_HELPER_NO_SCALING,
14069 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014070 if (ret)
14071 return ret;
14072
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014073 /* if we want to turn off the cursor ignore width and height */
14074 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014075 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014076
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014077 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014078 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014079 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14080 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014081 return -EINVAL;
14082 }
14083
Matt Roperea2c67b2014-12-23 10:41:52 -080014084 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14085 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014086 DRM_DEBUG_KMS("buffer is too small\n");
14087 return -ENOMEM;
14088 }
14089
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014090 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014091 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014092 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014093 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014094
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014095 /*
14096 * There's something wrong with the cursor on CHV pipe C.
14097 * If it straddles the left edge of the screen then
14098 * moving it away from the edge or disabling it often
14099 * results in a pipe underrun, and often that can lead to
14100 * dead pipe (constant underrun reported, and it scans
14101 * out just a solid color). To recover from that, the
14102 * display power well must be turned off and on again.
14103 * Refuse the put the cursor into that compromised position.
14104 */
14105 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14106 state->visible && state->base.crtc_x < 0) {
14107 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14108 return -EINVAL;
14109 }
14110
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014111 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014112}
14113
Matt Roperf4a2cf22014-12-01 15:40:12 -080014114static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014115intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014116 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014117{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014118 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14119
14120 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014121 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014122}
14123
14124static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014125intel_update_cursor_plane(struct drm_plane *plane,
14126 const struct intel_crtc_state *crtc_state,
14127 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014128{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014129 struct drm_crtc *crtc = crtc_state->base.crtc;
14130 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014131 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014132 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014133 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014134
Matt Roperf4a2cf22014-12-01 15:40:12 -080014135 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014136 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014137 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014138 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014139 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014140 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014141
Gustavo Padovana912f122014-12-01 15:40:10 -080014142 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014143 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014144}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014145
Matt Roper3d7d6512014-06-10 08:28:13 -070014146static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14147 int pipe)
14148{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014149 struct intel_plane *cursor = NULL;
14150 struct intel_plane_state *state = NULL;
14151 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070014152
14153 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014154 if (!cursor)
14155 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070014156
Matt Roper8e7d6882015-01-21 16:35:41 -080014157 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014158 if (!state)
14159 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014160 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014161
Matt Roper3d7d6512014-06-10 08:28:13 -070014162 cursor->can_scale = false;
14163 cursor->max_downscale = 1;
14164 cursor->pipe = pipe;
14165 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014166 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014167 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014168 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014169 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014170
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014171 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14172 &intel_plane_funcs,
14173 intel_cursor_formats,
14174 ARRAY_SIZE(intel_cursor_formats),
14175 DRM_PLANE_TYPE_CURSOR, NULL);
14176 if (ret)
14177 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014178
14179 if (INTEL_INFO(dev)->gen >= 4) {
14180 if (!dev->mode_config.rotation_property)
14181 dev->mode_config.rotation_property =
14182 drm_mode_create_rotation_property(dev,
14183 BIT(DRM_ROTATE_0) |
14184 BIT(DRM_ROTATE_180));
14185 if (dev->mode_config.rotation_property)
14186 drm_object_attach_property(&cursor->base.base,
14187 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014188 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014189 }
14190
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014191 if (INTEL_INFO(dev)->gen >=9)
14192 state->scaler_id = -1;
14193
Matt Roperea2c67b2014-12-23 10:41:52 -080014194 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14195
Matt Roper3d7d6512014-06-10 08:28:13 -070014196 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014197
14198fail:
14199 kfree(state);
14200 kfree(cursor);
14201
14202 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014203}
14204
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014205static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14206 struct intel_crtc_state *crtc_state)
14207{
14208 int i;
14209 struct intel_scaler *intel_scaler;
14210 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14211
14212 for (i = 0; i < intel_crtc->num_scalers; i++) {
14213 intel_scaler = &scaler_state->scalers[i];
14214 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014215 intel_scaler->mode = PS_SCALER_MODE_DYN;
14216 }
14217
14218 scaler_state->scaler_id = -1;
14219}
14220
Hannes Ederb358d0a2008-12-18 21:18:47 +010014221static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014222{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014223 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014224 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014225 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014226 struct drm_plane *primary = NULL;
14227 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014228 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014229
Daniel Vetter955382f2013-09-19 14:05:45 +020014230 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014231 if (intel_crtc == NULL)
14232 return;
14233
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014234 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14235 if (!crtc_state)
14236 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014237 intel_crtc->config = crtc_state;
14238 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014239 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014240
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014241 /* initialize shared scalers */
14242 if (INTEL_INFO(dev)->gen >= 9) {
14243 if (pipe == PIPE_C)
14244 intel_crtc->num_scalers = 1;
14245 else
14246 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14247
14248 skl_init_scalers(dev, intel_crtc, crtc_state);
14249 }
14250
Matt Roper465c1202014-05-29 08:06:54 -070014251 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014252 if (!primary)
14253 goto fail;
14254
14255 cursor = intel_cursor_plane_create(dev, pipe);
14256 if (!cursor)
14257 goto fail;
14258
Matt Roper465c1202014-05-29 08:06:54 -070014259 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020014260 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070014261 if (ret)
14262 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014263
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014264 /*
14265 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014266 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014267 */
Jesse Barnes80824002009-09-10 15:28:06 -070014268 intel_crtc->pipe = pipe;
14269 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014270 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014271 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014272 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014273 }
14274
Chris Wilson4b0e3332014-05-30 16:35:26 +030014275 intel_crtc->cursor_base = ~0;
14276 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014277 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014278
Ville Syrjälä852eb002015-06-24 22:00:07 +030014279 intel_crtc->wm.cxsr_allowed = true;
14280
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014281 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14282 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14283 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14284 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14285
Jesse Barnes79e53942008-11-07 14:24:08 -080014286 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014287
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014288 intel_color_init(&intel_crtc->base);
14289
Daniel Vetter87b6b102014-05-15 15:33:46 +020014290 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014291 return;
14292
14293fail:
14294 if (primary)
14295 drm_plane_cleanup(primary);
14296 if (cursor)
14297 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014298 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014299 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014300}
14301
Jesse Barnes752aa882013-10-31 18:55:49 +020014302enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14303{
14304 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014305 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014306
Rob Clark51fd3712013-11-19 12:10:12 -050014307 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014308
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014309 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014310 return INVALID_PIPE;
14311
14312 return to_intel_crtc(encoder->crtc)->pipe;
14313}
14314
Carl Worth08d7b3d2009-04-29 14:43:54 -070014315int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014316 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014317{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014318 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014319 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014320 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014321
Rob Clark7707e652014-07-17 23:30:04 -040014322 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014323
Rob Clark7707e652014-07-17 23:30:04 -040014324 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014325 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014326 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014327 }
14328
Rob Clark7707e652014-07-17 23:30:04 -040014329 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014330 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014331
Daniel Vetterc05422d2009-08-11 16:05:30 +020014332 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014333}
14334
Daniel Vetter66a92782012-07-12 20:08:18 +020014335static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014336{
Daniel Vetter66a92782012-07-12 20:08:18 +020014337 struct drm_device *dev = encoder->base.dev;
14338 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014339 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014340 int entry = 0;
14341
Damien Lespiaub2784e12014-08-05 11:29:37 +010014342 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014343 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014344 index_mask |= (1 << entry);
14345
Jesse Barnes79e53942008-11-07 14:24:08 -080014346 entry++;
14347 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014348
Jesse Barnes79e53942008-11-07 14:24:08 -080014349 return index_mask;
14350}
14351
Chris Wilson4d302442010-12-14 19:21:29 +000014352static bool has_edp_a(struct drm_device *dev)
14353{
14354 struct drm_i915_private *dev_priv = dev->dev_private;
14355
14356 if (!IS_MOBILE(dev))
14357 return false;
14358
14359 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14360 return false;
14361
Damien Lespiaue3589902014-02-07 19:12:50 +000014362 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014363 return false;
14364
14365 return true;
14366}
14367
Jesse Barnes84b4e042014-06-25 08:24:29 -070014368static bool intel_crt_present(struct drm_device *dev)
14369{
14370 struct drm_i915_private *dev_priv = dev->dev_private;
14371
Damien Lespiau884497e2013-12-03 13:56:23 +000014372 if (INTEL_INFO(dev)->gen >= 9)
14373 return false;
14374
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014375 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014376 return false;
14377
14378 if (IS_CHERRYVIEW(dev))
14379 return false;
14380
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014381 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14382 return false;
14383
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014384 /* DDI E can't be used if DDI A requires 4 lanes */
14385 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14386 return false;
14387
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014388 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014389 return false;
14390
14391 return true;
14392}
14393
Jesse Barnes79e53942008-11-07 14:24:08 -080014394static void intel_setup_outputs(struct drm_device *dev)
14395{
Eric Anholt725e30a2009-01-22 13:01:02 -080014396 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014397 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014398 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014399
Daniel Vetterc9093352013-06-06 22:22:47 +020014400 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014401
Jesse Barnes84b4e042014-06-25 08:24:29 -070014402 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014403 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014404
Vandana Kannanc776eb22014-08-19 12:05:01 +053014405 if (IS_BROXTON(dev)) {
14406 /*
14407 * FIXME: Broxton doesn't support port detection via the
14408 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14409 * detect the ports.
14410 */
14411 intel_ddi_init(dev, PORT_A);
14412 intel_ddi_init(dev, PORT_B);
14413 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014414
14415 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014416 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014417 int found;
14418
Jesse Barnesde31fac2015-03-06 15:53:32 -080014419 /*
14420 * Haswell uses DDI functions to detect digital outputs.
14421 * On SKL pre-D0 the strap isn't connected, so we assume
14422 * it's there.
14423 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014424 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014425 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014426 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014427 intel_ddi_init(dev, PORT_A);
14428
14429 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14430 * register */
14431 found = I915_READ(SFUSE_STRAP);
14432
14433 if (found & SFUSE_STRAP_DDIB_DETECTED)
14434 intel_ddi_init(dev, PORT_B);
14435 if (found & SFUSE_STRAP_DDIC_DETECTED)
14436 intel_ddi_init(dev, PORT_C);
14437 if (found & SFUSE_STRAP_DDID_DETECTED)
14438 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014439 /*
14440 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14441 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014442 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014443 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14444 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14445 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14446 intel_ddi_init(dev, PORT_E);
14447
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014448 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014449 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014450 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014451
14452 if (has_edp_a(dev))
14453 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014454
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014455 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014456 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014457 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014458 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014459 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014460 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014461 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014462 }
14463
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014464 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014465 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014466
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014467 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014468 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014469
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014470 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014471 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014472
Daniel Vetter270b3042012-10-27 15:52:05 +020014473 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014474 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014475 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014476 /*
14477 * The DP_DETECTED bit is the latched state of the DDC
14478 * SDA pin at boot. However since eDP doesn't require DDC
14479 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14480 * eDP ports may have been muxed to an alternate function.
14481 * Thus we can't rely on the DP_DETECTED bit alone to detect
14482 * eDP ports. Consult the VBT as well as DP_DETECTED to
14483 * detect eDP ports.
14484 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014485 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014486 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014487 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14488 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014489 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014490 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014491
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014492 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014493 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014494 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14495 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014496 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014497 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014498
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014499 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014500 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014501 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14502 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14503 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14504 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014505 }
14506
Jani Nikula3cfca972013-08-27 15:12:26 +030014507 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014508 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014509 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014510
Paulo Zanonie2debe92013-02-18 19:00:27 -030014511 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014512 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014513 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014514 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014515 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014516 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014517 }
Ma Ling27185ae2009-08-24 13:50:23 +080014518
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014519 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014520 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014521 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014522
14523 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014524
Paulo Zanonie2debe92013-02-18 19:00:27 -030014525 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014526 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014527 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014528 }
Ma Ling27185ae2009-08-24 13:50:23 +080014529
Paulo Zanonie2debe92013-02-18 19:00:27 -030014530 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014531
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014532 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014533 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014534 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014535 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014536 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014537 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014538 }
Ma Ling27185ae2009-08-24 13:50:23 +080014539
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014540 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014541 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014542 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014543 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014544 intel_dvo_init(dev);
14545
Zhenyu Wang103a1962009-11-27 11:44:36 +080014546 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014547 intel_tv_init(dev);
14548
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014549 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014550
Damien Lespiaub2784e12014-08-05 11:29:37 +010014551 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014552 encoder->base.possible_crtcs = encoder->crtc_mask;
14553 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014554 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014555 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014556
Paulo Zanonidde86e22012-12-01 12:04:25 -020014557 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014558
14559 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014560}
14561
14562static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14563{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014564 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014565 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014566
Daniel Vetteref2d6332014-02-10 18:00:38 +010014567 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014568 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014569 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014570 drm_gem_object_unreference(&intel_fb->obj->base);
14571 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014572 kfree(intel_fb);
14573}
14574
14575static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014576 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014577 unsigned int *handle)
14578{
14579 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014580 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014581
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014582 if (obj->userptr.mm) {
14583 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14584 return -EINVAL;
14585 }
14586
Chris Wilson05394f32010-11-08 19:18:58 +000014587 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014588}
14589
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014590static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14591 struct drm_file *file,
14592 unsigned flags, unsigned color,
14593 struct drm_clip_rect *clips,
14594 unsigned num_clips)
14595{
14596 struct drm_device *dev = fb->dev;
14597 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14598 struct drm_i915_gem_object *obj = intel_fb->obj;
14599
14600 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014601 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014602 mutex_unlock(&dev->struct_mutex);
14603
14604 return 0;
14605}
14606
Jesse Barnes79e53942008-11-07 14:24:08 -080014607static const struct drm_framebuffer_funcs intel_fb_funcs = {
14608 .destroy = intel_user_framebuffer_destroy,
14609 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014610 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014611};
14612
Damien Lespiaub3218032015-02-27 11:15:18 +000014613static
14614u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14615 uint32_t pixel_format)
14616{
14617 u32 gen = INTEL_INFO(dev)->gen;
14618
14619 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014620 int cpp = drm_format_plane_cpp(pixel_format, 0);
14621
Damien Lespiaub3218032015-02-27 11:15:18 +000014622 /* "The stride in bytes must not exceed the of the size of 8K
14623 * pixels and 32K bytes."
14624 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014625 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014626 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014627 return 32*1024;
14628 } else if (gen >= 4) {
14629 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14630 return 16*1024;
14631 else
14632 return 32*1024;
14633 } else if (gen >= 3) {
14634 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14635 return 8*1024;
14636 else
14637 return 16*1024;
14638 } else {
14639 /* XXX DSPC is limited to 4k tiled */
14640 return 8*1024;
14641 }
14642}
14643
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014644static int intel_framebuffer_init(struct drm_device *dev,
14645 struct intel_framebuffer *intel_fb,
14646 struct drm_mode_fb_cmd2 *mode_cmd,
14647 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014648{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014649 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014650 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014651 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014652 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014653
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014654 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14655
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014656 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14657 /* Enforce that fb modifier and tiling mode match, but only for
14658 * X-tiled. This is needed for FBC. */
14659 if (!!(obj->tiling_mode == I915_TILING_X) !=
14660 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14661 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14662 return -EINVAL;
14663 }
14664 } else {
14665 if (obj->tiling_mode == I915_TILING_X)
14666 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14667 else if (obj->tiling_mode == I915_TILING_Y) {
14668 DRM_DEBUG("No Y tiling for legacy addfb\n");
14669 return -EINVAL;
14670 }
14671 }
14672
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014673 /* Passed in modifier sanity checking. */
14674 switch (mode_cmd->modifier[0]) {
14675 case I915_FORMAT_MOD_Y_TILED:
14676 case I915_FORMAT_MOD_Yf_TILED:
14677 if (INTEL_INFO(dev)->gen < 9) {
14678 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14679 mode_cmd->modifier[0]);
14680 return -EINVAL;
14681 }
14682 case DRM_FORMAT_MOD_NONE:
14683 case I915_FORMAT_MOD_X_TILED:
14684 break;
14685 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014686 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14687 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014688 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014689 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014690
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014691 stride_alignment = intel_fb_stride_alignment(dev_priv,
14692 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014693 mode_cmd->pixel_format);
14694 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14695 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14696 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014697 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014698 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014699
Damien Lespiaub3218032015-02-27 11:15:18 +000014700 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14701 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014702 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014703 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14704 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014705 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014706 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014707 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014708 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014709
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014710 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014711 mode_cmd->pitches[0] != obj->stride) {
14712 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14713 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014714 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014715 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014716
Ville Syrjälä57779d02012-10-31 17:50:14 +020014717 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014718 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014719 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014720 case DRM_FORMAT_RGB565:
14721 case DRM_FORMAT_XRGB8888:
14722 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014723 break;
14724 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014725 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014726 DRM_DEBUG("unsupported pixel format: %s\n",
14727 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014728 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014729 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014730 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014731 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014732 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14733 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014734 DRM_DEBUG("unsupported pixel format: %s\n",
14735 drm_get_format_name(mode_cmd->pixel_format));
14736 return -EINVAL;
14737 }
14738 break;
14739 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014740 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014741 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014742 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014743 DRM_DEBUG("unsupported pixel format: %s\n",
14744 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014745 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014746 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014747 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014748 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014749 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014750 DRM_DEBUG("unsupported pixel format: %s\n",
14751 drm_get_format_name(mode_cmd->pixel_format));
14752 return -EINVAL;
14753 }
14754 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014755 case DRM_FORMAT_YUYV:
14756 case DRM_FORMAT_UYVY:
14757 case DRM_FORMAT_YVYU:
14758 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014759 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014760 DRM_DEBUG("unsupported pixel format: %s\n",
14761 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014762 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014763 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014764 break;
14765 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014766 DRM_DEBUG("unsupported pixel format: %s\n",
14767 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014768 return -EINVAL;
14769 }
14770
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014771 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14772 if (mode_cmd->offsets[0] != 0)
14773 return -EINVAL;
14774
Damien Lespiauec2c9812015-01-20 12:51:45 +000014775 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014776 mode_cmd->pixel_format,
14777 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014778 /* FIXME drm helper for size checks (especially planar formats)? */
14779 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14780 return -EINVAL;
14781
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014782 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14783 intel_fb->obj = obj;
14784
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014785 intel_fill_fb_info(dev_priv, &intel_fb->base);
14786
Jesse Barnes79e53942008-11-07 14:24:08 -080014787 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14788 if (ret) {
14789 DRM_ERROR("framebuffer init failed %d\n", ret);
14790 return ret;
14791 }
14792
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020014793 intel_fb->obj->framebuffer_references++;
14794
Jesse Barnes79e53942008-11-07 14:24:08 -080014795 return 0;
14796}
14797
Jesse Barnes79e53942008-11-07 14:24:08 -080014798static struct drm_framebuffer *
14799intel_user_framebuffer_create(struct drm_device *dev,
14800 struct drm_file *filp,
Ville Syrjälä1eb834512015-11-11 19:11:29 +020014801 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014802{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014803 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014804 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014805 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014806
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014807 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014808 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014809 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014810 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014811
Daniel Vetter92907cb2015-11-23 09:04:05 +010014812 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014813 if (IS_ERR(fb))
14814 drm_gem_object_unreference_unlocked(&obj->base);
14815
14816 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014817}
14818
Daniel Vetter06957262015-08-10 13:34:08 +020014819#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014820static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014821{
14822}
14823#endif
14824
Jesse Barnes79e53942008-11-07 14:24:08 -080014825static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014826 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014827 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014828 .atomic_check = intel_atomic_check,
14829 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014830 .atomic_state_alloc = intel_atomic_state_alloc,
14831 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014832};
14833
Imre Deak88212942016-03-16 13:38:53 +020014834/**
14835 * intel_init_display_hooks - initialize the display modesetting hooks
14836 * @dev_priv: device private
14837 */
14838void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014839{
Imre Deak88212942016-03-16 13:38:53 +020014840 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014841 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014842 dev_priv->display.get_initial_plane_config =
14843 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014844 dev_priv->display.crtc_compute_clock =
14845 haswell_crtc_compute_clock;
14846 dev_priv->display.crtc_enable = haswell_crtc_enable;
14847 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014848 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014849 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014850 dev_priv->display.get_initial_plane_config =
14851 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014852 dev_priv->display.crtc_compute_clock =
14853 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014854 dev_priv->display.crtc_enable = haswell_crtc_enable;
14855 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014856 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014857 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014858 dev_priv->display.get_initial_plane_config =
14859 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014860 dev_priv->display.crtc_compute_clock =
14861 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014862 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14863 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014864 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014865 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014866 dev_priv->display.get_initial_plane_config =
14867 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014868 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14869 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14870 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14871 } else if (IS_VALLEYVIEW(dev_priv)) {
14872 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14873 dev_priv->display.get_initial_plane_config =
14874 i9xx_get_initial_plane_config;
14875 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014876 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14877 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020014878 } else if (IS_G4X(dev_priv)) {
14879 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14880 dev_priv->display.get_initial_plane_config =
14881 i9xx_get_initial_plane_config;
14882 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14883 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14884 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020014885 } else if (IS_PINEVIEW(dev_priv)) {
14886 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14887 dev_priv->display.get_initial_plane_config =
14888 i9xx_get_initial_plane_config;
14889 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14890 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14891 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014892 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014893 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014894 dev_priv->display.get_initial_plane_config =
14895 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014896 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014897 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14898 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014899 } else {
14900 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14901 dev_priv->display.get_initial_plane_config =
14902 i9xx_get_initial_plane_config;
14903 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14904 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14905 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014906 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014907
Jesse Barnese70236a2009-09-21 10:42:27 -070014908 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020014909 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014910 dev_priv->display.get_display_clock_speed =
14911 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014912 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014913 dev_priv->display.get_display_clock_speed =
14914 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014915 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014916 dev_priv->display.get_display_clock_speed =
14917 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014918 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014919 dev_priv->display.get_display_clock_speed =
14920 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014921 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014922 dev_priv->display.get_display_clock_speed =
14923 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014924 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014925 dev_priv->display.get_display_clock_speed =
14926 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014927 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14928 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014929 dev_priv->display.get_display_clock_speed =
14930 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014931 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014932 dev_priv->display.get_display_clock_speed =
14933 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014934 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014935 dev_priv->display.get_display_clock_speed =
14936 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014937 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014938 dev_priv->display.get_display_clock_speed =
14939 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014940 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014941 dev_priv->display.get_display_clock_speed =
14942 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014943 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014944 dev_priv->display.get_display_clock_speed =
14945 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014946 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014947 dev_priv->display.get_display_clock_speed =
14948 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014949 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014950 dev_priv->display.get_display_clock_speed =
14951 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014952 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014953 dev_priv->display.get_display_clock_speed =
14954 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014955 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014956 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014957 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014958 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020014959 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014960 dev_priv->display.get_display_clock_speed =
14961 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014962 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014963
Imre Deak88212942016-03-16 13:38:53 +020014964 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014965 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014966 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014967 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014968 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014969 /* FIXME: detect B0+ stepping and use auto training */
14970 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014971 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014972 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030014973 }
14974
14975 if (IS_BROADWELL(dev_priv)) {
14976 dev_priv->display.modeset_commit_cdclk =
14977 broadwell_modeset_commit_cdclk;
14978 dev_priv->display.modeset_calc_cdclk =
14979 broadwell_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020014980 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014981 dev_priv->display.modeset_commit_cdclk =
14982 valleyview_modeset_commit_cdclk;
14983 dev_priv->display.modeset_calc_cdclk =
14984 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020014985 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014986 dev_priv->display.modeset_commit_cdclk =
14987 broxton_modeset_commit_cdclk;
14988 dev_priv->display.modeset_calc_cdclk =
14989 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014990 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014991
Imre Deak88212942016-03-16 13:38:53 +020014992 switch (INTEL_INFO(dev_priv)->gen) {
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014993 case 2:
14994 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14995 break;
14996
14997 case 3:
14998 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14999 break;
15000
15001 case 4:
15002 case 5:
15003 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15004 break;
15005
15006 case 6:
15007 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15008 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015009 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070015010 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015011 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15012 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000015013 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000015014 /* Drop through - unsupported since execlist only. */
15015 default:
15016 /* Default just returns -ENODEV to indicate unsupported */
15017 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015018 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015019}
15020
Jesse Barnesb690e962010-07-19 13:53:12 -070015021/*
15022 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15023 * resume, or other times. This quirk makes sure that's the case for
15024 * affected systems.
15025 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015026static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015027{
15028 struct drm_i915_private *dev_priv = dev->dev_private;
15029
15030 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015031 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015032}
15033
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015034static void quirk_pipeb_force(struct drm_device *dev)
15035{
15036 struct drm_i915_private *dev_priv = dev->dev_private;
15037
15038 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15039 DRM_INFO("applying pipe b force quirk\n");
15040}
15041
Keith Packard435793d2011-07-12 14:56:22 -070015042/*
15043 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15044 */
15045static void quirk_ssc_force_disable(struct drm_device *dev)
15046{
15047 struct drm_i915_private *dev_priv = dev->dev_private;
15048 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015049 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015050}
15051
Carsten Emde4dca20e2012-03-15 15:56:26 +010015052/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015053 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15054 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015055 */
15056static void quirk_invert_brightness(struct drm_device *dev)
15057{
15058 struct drm_i915_private *dev_priv = dev->dev_private;
15059 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015060 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015061}
15062
Scot Doyle9c72cc62014-07-03 23:27:50 +000015063/* Some VBT's incorrectly indicate no backlight is present */
15064static void quirk_backlight_present(struct drm_device *dev)
15065{
15066 struct drm_i915_private *dev_priv = dev->dev_private;
15067 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15068 DRM_INFO("applying backlight present quirk\n");
15069}
15070
Jesse Barnesb690e962010-07-19 13:53:12 -070015071struct intel_quirk {
15072 int device;
15073 int subsystem_vendor;
15074 int subsystem_device;
15075 void (*hook)(struct drm_device *dev);
15076};
15077
Egbert Eich5f85f172012-10-14 15:46:38 +020015078/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15079struct intel_dmi_quirk {
15080 void (*hook)(struct drm_device *dev);
15081 const struct dmi_system_id (*dmi_id_list)[];
15082};
15083
15084static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15085{
15086 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15087 return 1;
15088}
15089
15090static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15091 {
15092 .dmi_id_list = &(const struct dmi_system_id[]) {
15093 {
15094 .callback = intel_dmi_reverse_brightness,
15095 .ident = "NCR Corporation",
15096 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15097 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15098 },
15099 },
15100 { } /* terminating entry */
15101 },
15102 .hook = quirk_invert_brightness,
15103 },
15104};
15105
Ben Widawskyc43b5632012-04-16 14:07:40 -070015106static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015107 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15108 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15109
Jesse Barnesb690e962010-07-19 13:53:12 -070015110 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15111 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15112
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015113 /* 830 needs to leave pipe A & dpll A up */
15114 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15115
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015116 /* 830 needs to leave pipe B & dpll B up */
15117 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15118
Keith Packard435793d2011-07-12 14:56:22 -070015119 /* Lenovo U160 cannot use SSC on LVDS */
15120 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015121
15122 /* Sony Vaio Y cannot use SSC on LVDS */
15123 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015124
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015125 /* Acer Aspire 5734Z must invert backlight brightness */
15126 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15127
15128 /* Acer/eMachines G725 */
15129 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15130
15131 /* Acer/eMachines e725 */
15132 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15133
15134 /* Acer/Packard Bell NCL20 */
15135 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15136
15137 /* Acer Aspire 4736Z */
15138 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015139
15140 /* Acer Aspire 5336 */
15141 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015142
15143 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15144 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015145
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015146 /* Acer C720 Chromebook (Core i3 4005U) */
15147 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15148
jens steinb2a96012014-10-28 20:25:53 +010015149 /* Apple Macbook 2,1 (Core 2 T7400) */
15150 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15151
Jani Nikula1b9448b02015-11-05 11:49:59 +020015152 /* Apple Macbook 4,1 */
15153 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15154
Scot Doyled4967d82014-07-03 23:27:52 +000015155 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15156 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015157
15158 /* HP Chromebook 14 (Celeron 2955U) */
15159 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015160
15161 /* Dell Chromebook 11 */
15162 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015163
15164 /* Dell Chromebook 11 (2015 version) */
15165 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015166};
15167
15168static void intel_init_quirks(struct drm_device *dev)
15169{
15170 struct pci_dev *d = dev->pdev;
15171 int i;
15172
15173 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15174 struct intel_quirk *q = &intel_quirks[i];
15175
15176 if (d->device == q->device &&
15177 (d->subsystem_vendor == q->subsystem_vendor ||
15178 q->subsystem_vendor == PCI_ANY_ID) &&
15179 (d->subsystem_device == q->subsystem_device ||
15180 q->subsystem_device == PCI_ANY_ID))
15181 q->hook(dev);
15182 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015183 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15184 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15185 intel_dmi_quirks[i].hook(dev);
15186 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015187}
15188
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015189/* Disable the VGA plane that we never use */
15190static void i915_disable_vga(struct drm_device *dev)
15191{
15192 struct drm_i915_private *dev_priv = dev->dev_private;
15193 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015194 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015195
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015196 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015197 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015198 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015199 sr1 = inb(VGA_SR_DATA);
15200 outb(sr1 | 1<<5, VGA_SR_DATA);
15201 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15202 udelay(300);
15203
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015204 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015205 POSTING_READ(vga_reg);
15206}
15207
Daniel Vetterf8175862012-04-10 15:50:11 +020015208void intel_modeset_init_hw(struct drm_device *dev)
15209{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015210 struct drm_i915_private *dev_priv = dev->dev_private;
15211
Ville Syrjäläb6283052015-06-03 15:45:07 +030015212 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015213
15214 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15215
Daniel Vetterf8175862012-04-10 15:50:11 +020015216 intel_init_clock_gating(dev);
Chris Wilsondc979972016-05-10 14:10:04 +010015217 intel_enable_gt_powersave(dev_priv);
Daniel Vetterf8175862012-04-10 15:50:11 +020015218}
15219
Matt Roperd93c0372015-12-03 11:37:41 -080015220/*
15221 * Calculate what we think the watermarks should be for the state we've read
15222 * out of the hardware and then immediately program those watermarks so that
15223 * we ensure the hardware settings match our internal state.
15224 *
15225 * We can calculate what we think WM's should be by creating a duplicate of the
15226 * current state (which was constructed during hardware readout) and running it
15227 * through the atomic check code to calculate new watermark values in the
15228 * state object.
15229 */
15230static void sanitize_watermarks(struct drm_device *dev)
15231{
15232 struct drm_i915_private *dev_priv = to_i915(dev);
15233 struct drm_atomic_state *state;
15234 struct drm_crtc *crtc;
15235 struct drm_crtc_state *cstate;
15236 struct drm_modeset_acquire_ctx ctx;
15237 int ret;
15238 int i;
15239
15240 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015241 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015242 return;
15243
15244 /*
15245 * We need to hold connection_mutex before calling duplicate_state so
15246 * that the connector loop is protected.
15247 */
15248 drm_modeset_acquire_init(&ctx, 0);
15249retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015250 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015251 if (ret == -EDEADLK) {
15252 drm_modeset_backoff(&ctx);
15253 goto retry;
15254 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015255 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015256 }
15257
15258 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15259 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015260 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015261
Matt Ropered4a6a72016-02-23 17:20:13 -080015262 /*
15263 * Hardware readout is the only time we don't want to calculate
15264 * intermediate watermarks (since we don't trust the current
15265 * watermarks).
15266 */
15267 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15268
Matt Roperd93c0372015-12-03 11:37:41 -080015269 ret = intel_atomic_check(dev, state);
15270 if (ret) {
15271 /*
15272 * If we fail here, it means that the hardware appears to be
15273 * programmed in a way that shouldn't be possible, given our
15274 * understanding of watermark requirements. This might mean a
15275 * mistake in the hardware readout code or a mistake in the
15276 * watermark calculations for a given platform. Raise a WARN
15277 * so that this is noticeable.
15278 *
15279 * If this actually happens, we'll have to just leave the
15280 * BIOS-programmed watermarks untouched and hope for the best.
15281 */
15282 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015283 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015284 }
15285
15286 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080015287 for_each_crtc_in_state(state, crtc, cstate, i) {
15288 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15289
Matt Ropered4a6a72016-02-23 17:20:13 -080015290 cs->wm.need_postvbl_update = true;
15291 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015292 }
15293
15294 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015295fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015296 drm_modeset_drop_locks(&ctx);
15297 drm_modeset_acquire_fini(&ctx);
15298}
15299
Jesse Barnes79e53942008-11-07 14:24:08 -080015300void intel_modeset_init(struct drm_device *dev)
15301{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015302 struct drm_i915_private *dev_priv = to_i915(dev);
15303 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015304 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015305 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015306 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015307
15308 drm_mode_config_init(dev);
15309
15310 dev->mode_config.min_width = 0;
15311 dev->mode_config.min_height = 0;
15312
Dave Airlie019d96c2011-09-29 16:20:42 +010015313 dev->mode_config.preferred_depth = 24;
15314 dev->mode_config.prefer_shadow = 1;
15315
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015316 dev->mode_config.allow_fb_modifiers = true;
15317
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015318 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015319
Jesse Barnesb690e962010-07-19 13:53:12 -070015320 intel_init_quirks(dev);
15321
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015322 intel_init_pm(dev);
15323
Ben Widawskye3c74752013-04-05 13:12:39 -070015324 if (INTEL_INFO(dev)->num_pipes == 0)
15325 return;
15326
Lukas Wunner69f92f62015-07-15 13:57:35 +020015327 /*
15328 * There may be no VBT; and if the BIOS enabled SSC we can
15329 * just keep using it to avoid unnecessary flicker. Whereas if the
15330 * BIOS isn't using it, don't assume it will work even if the VBT
15331 * indicates as much.
15332 */
15333 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15334 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15335 DREF_SSC1_ENABLE);
15336
15337 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15338 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15339 bios_lvds_use_ssc ? "en" : "dis",
15340 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15341 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15342 }
15343 }
15344
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015345 if (IS_GEN2(dev)) {
15346 dev->mode_config.max_width = 2048;
15347 dev->mode_config.max_height = 2048;
15348 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015349 dev->mode_config.max_width = 4096;
15350 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015351 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015352 dev->mode_config.max_width = 8192;
15353 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015354 }
Damien Lespiau068be562014-03-28 14:17:49 +000015355
Ville Syrjälädc41c152014-08-13 11:57:05 +030015356 if (IS_845G(dev) || IS_I865G(dev)) {
15357 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15358 dev->mode_config.cursor_height = 1023;
15359 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015360 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15361 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15362 } else {
15363 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15364 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15365 }
15366
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015367 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015368
Zhao Yakui28c97732009-10-09 11:39:41 +080015369 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015370 INTEL_INFO(dev)->num_pipes,
15371 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015372
Damien Lespiau055e3932014-08-18 13:49:10 +010015373 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015374 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015375 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015376 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015377 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015378 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015379 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015380 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015381 }
15382
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015383 intel_update_czclk(dev_priv);
15384 intel_update_cdclk(dev);
15385
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015386 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015387
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015388 /* Just disable it once at startup */
15389 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015390 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015391
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015392 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015393 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015394 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015395
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015396 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015397 struct intel_initial_plane_config plane_config = {};
15398
Jesse Barnes46f297f2014-03-07 08:57:48 -080015399 if (!crtc->active)
15400 continue;
15401
Jesse Barnes46f297f2014-03-07 08:57:48 -080015402 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015403 * Note that reserving the BIOS fb up front prevents us
15404 * from stuffing other stolen allocations like the ring
15405 * on top. This prevents some ugliness at boot time, and
15406 * can even allow for smooth boot transitions if the BIOS
15407 * fb is large enough for the active pipe configuration.
15408 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015409 dev_priv->display.get_initial_plane_config(crtc,
15410 &plane_config);
15411
15412 /*
15413 * If the fb is shared between multiple heads, we'll
15414 * just get the first one.
15415 */
15416 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015417 }
Matt Roperd93c0372015-12-03 11:37:41 -080015418
15419 /*
15420 * Make sure hardware watermarks really match the state we read out.
15421 * Note that we need to do this after reconstructing the BIOS fb's
15422 * since the watermark calculation done here will use pstate->fb.
15423 */
15424 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015425}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015426
Daniel Vetter7fad7982012-07-04 17:51:47 +020015427static void intel_enable_pipe_a(struct drm_device *dev)
15428{
15429 struct intel_connector *connector;
15430 struct drm_connector *crt = NULL;
15431 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015432 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015433
15434 /* We can't just switch on the pipe A, we need to set things up with a
15435 * proper mode and output configuration. As a gross hack, enable pipe A
15436 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015437 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015438 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15439 crt = &connector->base;
15440 break;
15441 }
15442 }
15443
15444 if (!crt)
15445 return;
15446
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015447 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015448 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015449}
15450
Daniel Vetterfa555832012-10-10 23:14:00 +020015451static bool
15452intel_check_plane_mapping(struct intel_crtc *crtc)
15453{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015454 struct drm_device *dev = crtc->base.dev;
15455 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015456 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015457
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015458 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015459 return true;
15460
Ville Syrjälä649636e2015-09-22 19:50:01 +030015461 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015462
15463 if ((val & DISPLAY_PLANE_ENABLE) &&
15464 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15465 return false;
15466
15467 return true;
15468}
15469
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015470static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15471{
15472 struct drm_device *dev = crtc->base.dev;
15473 struct intel_encoder *encoder;
15474
15475 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15476 return true;
15477
15478 return false;
15479}
15480
Ville Syrjälädd756192016-02-17 21:28:45 +020015481static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15482{
15483 struct drm_device *dev = encoder->base.dev;
15484 struct intel_connector *connector;
15485
15486 for_each_connector_on_encoder(dev, &encoder->base, connector)
15487 return true;
15488
15489 return false;
15490}
15491
Daniel Vetter24929352012-07-02 20:28:59 +020015492static void intel_sanitize_crtc(struct intel_crtc *crtc)
15493{
15494 struct drm_device *dev = crtc->base.dev;
15495 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015496 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015497
Daniel Vetter24929352012-07-02 20:28:59 +020015498 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015499 if (!transcoder_is_dsi(cpu_transcoder)) {
15500 i915_reg_t reg = PIPECONF(cpu_transcoder);
15501
15502 I915_WRITE(reg,
15503 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15504 }
Daniel Vetter24929352012-07-02 20:28:59 +020015505
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015506 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015507 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015508 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015509 struct intel_plane *plane;
15510
Daniel Vetter96256042015-02-13 21:03:42 +010015511 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015512
15513 /* Disable everything but the primary plane */
15514 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15515 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15516 continue;
15517
15518 plane->disable_plane(&plane->base, &crtc->base);
15519 }
Daniel Vetter96256042015-02-13 21:03:42 +010015520 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015521
Daniel Vetter24929352012-07-02 20:28:59 +020015522 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015523 * disable the crtc (and hence change the state) if it is wrong. Note
15524 * that gen4+ has a fixed plane -> pipe mapping. */
15525 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015526 bool plane;
15527
Daniel Vetter24929352012-07-02 20:28:59 +020015528 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15529 crtc->base.base.id);
15530
15531 /* Pipe has the wrong plane attached and the plane is active.
15532 * Temporarily change the plane mapping and disable everything
15533 * ... */
15534 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015535 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015536 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015537 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015538 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015539 }
Daniel Vetter24929352012-07-02 20:28:59 +020015540
Daniel Vetter7fad7982012-07-04 17:51:47 +020015541 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15542 crtc->pipe == PIPE_A && !crtc->active) {
15543 /* BIOS forgot to enable pipe A, this mostly happens after
15544 * resume. Force-enable the pipe to fix this, the update_dpms
15545 * call below we restore the pipe to the right state, but leave
15546 * the required bits on. */
15547 intel_enable_pipe_a(dev);
15548 }
15549
Daniel Vetter24929352012-07-02 20:28:59 +020015550 /* Adjust the state of the output pipe according to whether we
15551 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015552 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015553 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015554
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015555 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015556 /*
15557 * We start out with underrun reporting disabled to avoid races.
15558 * For correct bookkeeping mark this on active crtcs.
15559 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015560 * Also on gmch platforms we dont have any hardware bits to
15561 * disable the underrun reporting. Which means we need to start
15562 * out with underrun reporting disabled also on inactive pipes,
15563 * since otherwise we'll complain about the garbage we read when
15564 * e.g. coming up after runtime pm.
15565 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015566 * No protection against concurrent access is required - at
15567 * worst a fifo underrun happens which also sets this to false.
15568 */
15569 crtc->cpu_fifo_underrun_disabled = true;
15570 crtc->pch_fifo_underrun_disabled = true;
15571 }
Daniel Vetter24929352012-07-02 20:28:59 +020015572}
15573
15574static void intel_sanitize_encoder(struct intel_encoder *encoder)
15575{
15576 struct intel_connector *connector;
15577 struct drm_device *dev = encoder->base.dev;
15578
15579 /* We need to check both for a crtc link (meaning that the
15580 * encoder is active and trying to read from a pipe) and the
15581 * pipe itself being active. */
15582 bool has_active_crtc = encoder->base.crtc &&
15583 to_intel_crtc(encoder->base.crtc)->active;
15584
Ville Syrjälädd756192016-02-17 21:28:45 +020015585 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015586 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15587 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015588 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015589
15590 /* Connector is active, but has no active pipe. This is
15591 * fallout from our resume register restoring. Disable
15592 * the encoder manually again. */
15593 if (encoder->base.crtc) {
15594 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15595 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015596 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015597 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015598 if (encoder->post_disable)
15599 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015600 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015601 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015602
15603 /* Inconsistent output/port/pipe state happens presumably due to
15604 * a bug in one of the get_hw_state functions. Or someplace else
15605 * in our code, like the register restore mess on resume. Clamp
15606 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015607 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015608 if (connector->encoder != encoder)
15609 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015610 connector->base.dpms = DRM_MODE_DPMS_OFF;
15611 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015612 }
15613 }
15614 /* Enabled encoders without active connectors will be fixed in
15615 * the crtc fixup. */
15616}
15617
Imre Deak04098752014-02-18 00:02:16 +020015618void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015619{
15620 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015621 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015622
Imre Deak04098752014-02-18 00:02:16 +020015623 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15624 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15625 i915_disable_vga(dev);
15626 }
15627}
15628
15629void i915_redisable_vga(struct drm_device *dev)
15630{
15631 struct drm_i915_private *dev_priv = dev->dev_private;
15632
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015633 /* This function can be called both from intel_modeset_setup_hw_state or
15634 * at a very early point in our resume sequence, where the power well
15635 * structures are not yet restored. Since this function is at a very
15636 * paranoid "someone might have enabled VGA while we were not looking"
15637 * level, just check if the power well is enabled instead of trying to
15638 * follow the "don't touch the power well if we don't need it" policy
15639 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015640 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015641 return;
15642
Imre Deak04098752014-02-18 00:02:16 +020015643 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015644
15645 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015646}
15647
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015648static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015649{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015650 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015651
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015652 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015653}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015654
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015655/* FIXME read out full plane state for all planes */
15656static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015657{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015658 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015659 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015660 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015661
Matt Roper19b8d382015-09-24 15:53:17 -070015662 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015663 primary_get_hw_state(to_intel_plane(primary));
15664
15665 if (plane_state->visible)
15666 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015667}
15668
Daniel Vetter30e984d2013-06-05 13:34:17 +020015669static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015670{
15671 struct drm_i915_private *dev_priv = dev->dev_private;
15672 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015673 struct intel_crtc *crtc;
15674 struct intel_encoder *encoder;
15675 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015676 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015677
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015678 dev_priv->active_crtcs = 0;
15679
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015680 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015681 struct intel_crtc_state *crtc_state = crtc->config;
15682 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015683
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015684 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15685 memset(crtc_state, 0, sizeof(*crtc_state));
15686 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015687
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015688 crtc_state->base.active = crtc_state->base.enable =
15689 dev_priv->display.get_pipe_config(crtc, crtc_state);
15690
15691 crtc->base.enabled = crtc_state->base.enable;
15692 crtc->active = crtc_state->base.active;
15693
15694 if (crtc_state->base.active) {
15695 dev_priv->active_crtcs |= 1 << crtc->pipe;
15696
15697 if (IS_BROADWELL(dev_priv)) {
15698 pixclk = ilk_pipe_pixel_rate(crtc_state);
15699
15700 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15701 if (crtc_state->ips_enabled)
15702 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15703 } else if (IS_VALLEYVIEW(dev_priv) ||
15704 IS_CHERRYVIEW(dev_priv) ||
15705 IS_BROXTON(dev_priv))
15706 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15707 else
15708 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15709 }
15710
15711 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015712
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015713 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015714
15715 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15716 crtc->base.base.id,
15717 crtc->active ? "enabled" : "disabled");
15718 }
15719
Daniel Vetter53589012013-06-05 13:34:16 +020015720 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15721 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15722
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015723 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15724 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015725 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015726 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015727 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015728 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015729 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015730 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015731
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015732 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015733 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015734 }
15735
Damien Lespiaub2784e12014-08-05 11:29:37 +010015736 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015737 pipe = 0;
15738
15739 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015740 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15741 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015742 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015743 } else {
15744 encoder->base.crtc = NULL;
15745 }
15746
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015747 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015748 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015749 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015750 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015751 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015752 }
15753
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015754 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015755 if (connector->get_hw_state(connector)) {
15756 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015757
15758 encoder = connector->encoder;
15759 connector->base.encoder = &encoder->base;
15760
15761 if (encoder->base.crtc &&
15762 encoder->base.crtc->state->active) {
15763 /*
15764 * This has to be done during hardware readout
15765 * because anything calling .crtc_disable may
15766 * rely on the connector_mask being accurate.
15767 */
15768 encoder->base.crtc->state->connector_mask |=
15769 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015770 encoder->base.crtc->state->encoder_mask |=
15771 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015772 }
15773
Daniel Vetter24929352012-07-02 20:28:59 +020015774 } else {
15775 connector->base.dpms = DRM_MODE_DPMS_OFF;
15776 connector->base.encoder = NULL;
15777 }
15778 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15779 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015780 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015781 connector->base.encoder ? "enabled" : "disabled");
15782 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015783
15784 for_each_intel_crtc(dev, crtc) {
15785 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15786
15787 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15788 if (crtc->base.state->active) {
15789 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15790 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15791 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15792
15793 /*
15794 * The initial mode needs to be set in order to keep
15795 * the atomic core happy. It wants a valid mode if the
15796 * crtc's enabled, so we do the above call.
15797 *
15798 * At this point some state updated by the connectors
15799 * in their ->detect() callback has not run yet, so
15800 * no recalculation can be done yet.
15801 *
15802 * Even if we could do a recalculation and modeset
15803 * right now it would cause a double modeset if
15804 * fbdev or userspace chooses a different initial mode.
15805 *
15806 * If that happens, someone indicated they wanted a
15807 * mode change, which means it's safe to do a full
15808 * recalculation.
15809 */
15810 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015811
15812 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15813 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015814 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015815
15816 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015817 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015818}
15819
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015820/* Scan out the current hw modeset state,
15821 * and sanitizes it to the current state
15822 */
15823static void
15824intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015825{
15826 struct drm_i915_private *dev_priv = dev->dev_private;
15827 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015828 struct intel_crtc *crtc;
15829 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015830 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015831
15832 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015833
15834 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015835 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015836 intel_sanitize_encoder(encoder);
15837 }
15838
Damien Lespiau055e3932014-08-18 13:49:10 +010015839 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015840 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15841 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015842 intel_dump_pipe_config(crtc, crtc->config,
15843 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015844 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015845
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015846 intel_modeset_update_connector_atomic_state(dev);
15847
Daniel Vetter35c95372013-07-17 06:55:04 +020015848 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15849 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15850
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015851 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015852 continue;
15853
15854 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15855
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015856 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015857 pll->on = false;
15858 }
15859
Wayne Boyer666a4532015-12-09 12:29:35 -080015860 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015861 vlv_wm_get_hw_state(dev);
15862 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015863 skl_wm_get_hw_state(dev);
15864 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015865 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015866
15867 for_each_intel_crtc(dev, crtc) {
15868 unsigned long put_domains;
15869
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015870 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015871 if (WARN_ON(put_domains))
15872 modeset_put_power_domains(dev_priv, put_domains);
15873 }
15874 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015875
15876 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015877}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015878
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015879void intel_display_resume(struct drm_device *dev)
15880{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015881 struct drm_i915_private *dev_priv = to_i915(dev);
15882 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15883 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015884 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015885 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020015886
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015887 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015888
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015889 /*
15890 * This is a cludge because with real atomic modeset mode_config.mutex
15891 * won't be taken. Unfortunately some probed state like
15892 * audio_codec_enable is still protected by mode_config.mutex, so lock
15893 * it here for now.
15894 */
15895 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015896 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015897
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015898retry:
15899 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015900
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015901 if (ret == 0 && !setup) {
15902 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015903
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015904 intel_modeset_setup_hw_state(dev);
15905 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015906 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015907
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015908 if (ret == 0 && state) {
15909 struct drm_crtc_state *crtc_state;
15910 struct drm_crtc *crtc;
15911 int i;
15912
15913 state->acquire_ctx = &ctx;
15914
Ville Syrjäläe3d54572016-05-13 10:10:42 -070015915 /* ignore any reset values/BIOS leftovers in the WM registers */
15916 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15917
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015918 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15919 /*
15920 * Force recalculation even if we restore
15921 * current state. With fast modeset this may not result
15922 * in a modeset when the state is compatible.
15923 */
15924 crtc_state->mode_changed = true;
15925 }
15926
15927 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015928 }
15929
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015930 if (ret == -EDEADLK) {
15931 drm_modeset_backoff(&ctx);
15932 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015933 }
15934
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015935 drm_modeset_drop_locks(&ctx);
15936 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015937 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015938
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015939 if (ret) {
15940 DRM_ERROR("Restoring old state failed with %i\n", ret);
15941 drm_atomic_state_free(state);
15942 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015943}
15944
15945void intel_modeset_gem_init(struct drm_device *dev)
15946{
Chris Wilsondc979972016-05-10 14:10:04 +010015947 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015948 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015949 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015950 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015951
Chris Wilsondc979972016-05-10 14:10:04 +010015952 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030015953
Chris Wilson1833b132012-05-09 11:56:28 +010015954 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015955
Chris Wilson1ee8da62016-05-12 12:43:23 +010015956 intel_setup_overlay(dev_priv);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015957
15958 /*
15959 * Make sure any fbs we allocated at startup are properly
15960 * pinned & fenced. When we do the allocation it's too early
15961 * for this.
15962 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015963 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015964 obj = intel_fb_obj(c->primary->fb);
15965 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015966 continue;
15967
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015968 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020015969 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
15970 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015971 mutex_unlock(&dev->struct_mutex);
15972 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015973 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15974 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015975 drm_framebuffer_unreference(c->primary->fb);
15976 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015977 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015978 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015979 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015980 }
15981 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015982
15983 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015984}
15985
Imre Deak4932e2c2014-02-11 17:12:48 +020015986void intel_connector_unregister(struct intel_connector *intel_connector)
15987{
15988 struct drm_connector *connector = &intel_connector->base;
15989
15990 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015991 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015992}
15993
Jesse Barnes79e53942008-11-07 14:24:08 -080015994void intel_modeset_cleanup(struct drm_device *dev)
15995{
Jesse Barnes652c3932009-08-17 13:31:43 -070015996 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020015997 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015998
Chris Wilsondc979972016-05-10 14:10:04 +010015999 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020016000
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016001 intel_backlight_unregister(dev);
16002
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016003 /*
16004 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016005 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016006 * experience fancy races otherwise.
16007 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016008 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016009
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016010 /*
16011 * Due to the hpd irq storm handling the hotplug work can re-arm the
16012 * poll handlers. Hence disable polling after hpd handling is shut down.
16013 */
Keith Packardf87ea762010-10-03 19:36:26 -070016014 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016015
Jesse Barnes723bfd72010-10-07 16:01:13 -070016016 intel_unregister_dsm_handler();
16017
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016018 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016019
Chris Wilson1630fe72011-07-08 12:22:42 +010016020 /* flush any delayed tasks or pending work */
16021 flush_scheduled_work();
16022
Jani Nikuladb31af1d2013-11-08 16:48:53 +020016023 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020016024 for_each_intel_connector(dev, connector)
16025 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030016026
Jesse Barnes79e53942008-11-07 14:24:08 -080016027 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016028
Chris Wilson1ee8da62016-05-12 12:43:23 +010016029 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016030
Chris Wilsondc979972016-05-10 14:10:04 +010016031 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010016032
16033 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016034}
16035
Dave Airlie28d52042009-09-21 14:33:58 +100016036/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080016037 * Return which encoder is currently attached for connector.
16038 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010016039struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080016040{
Chris Wilsondf0e9242010-09-09 16:20:55 +010016041 return &intel_attached_encoder(connector)->base;
16042}
Jesse Barnes79e53942008-11-07 14:24:08 -080016043
Chris Wilsondf0e9242010-09-09 16:20:55 +010016044void intel_connector_attach_encoder(struct intel_connector *connector,
16045 struct intel_encoder *encoder)
16046{
16047 connector->encoder = encoder;
16048 drm_mode_connector_attach_encoder(&connector->base,
16049 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016050}
Dave Airlie28d52042009-09-21 14:33:58 +100016051
16052/*
16053 * set vga decode state - true == enable VGA decode
16054 */
16055int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16056{
16057 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016058 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016059 u16 gmch_ctrl;
16060
Chris Wilson75fa0412014-02-07 18:37:02 -020016061 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16062 DRM_ERROR("failed to read control word\n");
16063 return -EIO;
16064 }
16065
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016066 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16067 return 0;
16068
Dave Airlie28d52042009-09-21 14:33:58 +100016069 if (state)
16070 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16071 else
16072 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016073
16074 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16075 DRM_ERROR("failed to write control word\n");
16076 return -EIO;
16077 }
16078
Dave Airlie28d52042009-09-21 14:33:58 +100016079 return 0;
16080}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016081
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016082struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016083
16084 u32 power_well_driver;
16085
Chris Wilson63b66e52013-08-08 15:12:06 +020016086 int num_transcoders;
16087
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016088 struct intel_cursor_error_state {
16089 u32 control;
16090 u32 position;
16091 u32 base;
16092 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016093 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016094
16095 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016096 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016097 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030016098 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016099 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016100
16101 struct intel_plane_error_state {
16102 u32 control;
16103 u32 stride;
16104 u32 size;
16105 u32 pos;
16106 u32 addr;
16107 u32 surface;
16108 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016109 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016110
16111 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016112 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016113 enum transcoder cpu_transcoder;
16114
16115 u32 conf;
16116
16117 u32 htotal;
16118 u32 hblank;
16119 u32 hsync;
16120 u32 vtotal;
16121 u32 vblank;
16122 u32 vsync;
16123 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016124};
16125
16126struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010016127intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016128{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016129 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016130 int transcoders[] = {
16131 TRANSCODER_A,
16132 TRANSCODER_B,
16133 TRANSCODER_C,
16134 TRANSCODER_EDP,
16135 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016136 int i;
16137
Chris Wilsonc0336662016-05-06 15:40:21 +010016138 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020016139 return NULL;
16140
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016141 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016142 if (error == NULL)
16143 return NULL;
16144
Chris Wilsonc0336662016-05-06 15:40:21 +010016145 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016146 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16147
Damien Lespiau055e3932014-08-18 13:49:10 +010016148 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016149 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016150 __intel_display_power_is_enabled(dev_priv,
16151 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016152 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016153 continue;
16154
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016155 error->cursor[i].control = I915_READ(CURCNTR(i));
16156 error->cursor[i].position = I915_READ(CURPOS(i));
16157 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016158
16159 error->plane[i].control = I915_READ(DSPCNTR(i));
16160 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016161 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016162 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016163 error->plane[i].pos = I915_READ(DSPPOS(i));
16164 }
Chris Wilsonc0336662016-05-06 15:40:21 +010016165 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030016166 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016167 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016168 error->plane[i].surface = I915_READ(DSPSURF(i));
16169 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16170 }
16171
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016172 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030016173
Chris Wilsonc0336662016-05-06 15:40:21 +010016174 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e12014-04-18 15:55:04 +030016175 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016176 }
16177
Jani Nikula4d1de972016-03-18 17:05:42 +020016178 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010016179 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030016180 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020016181 error->num_transcoders++; /* Account for eDP. */
16182
16183 for (i = 0; i < error->num_transcoders; i++) {
16184 enum transcoder cpu_transcoder = transcoders[i];
16185
Imre Deakddf9c532013-11-27 22:02:02 +020016186 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016187 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016188 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016189 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016190 continue;
16191
Chris Wilson63b66e52013-08-08 15:12:06 +020016192 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16193
16194 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16195 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16196 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16197 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16198 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16199 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16200 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016201 }
16202
16203 return error;
16204}
16205
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016206#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16207
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016208void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016209intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016210 struct drm_device *dev,
16211 struct intel_display_error_state *error)
16212{
Damien Lespiau055e3932014-08-18 13:49:10 +010016213 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016214 int i;
16215
Chris Wilson63b66e52013-08-08 15:12:06 +020016216 if (!error)
16217 return;
16218
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016219 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016220 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016221 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016222 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016223 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016224 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016225 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016226 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016227 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030016228 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016229
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016230 err_printf(m, "Plane [%d]:\n", i);
16231 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16232 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016233 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016234 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16235 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016236 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016237 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016238 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016239 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016240 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16241 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016242 }
16243
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016244 err_printf(m, "Cursor [%d]:\n", i);
16245 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16246 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16247 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016248 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016249
16250 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016251 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016252 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016253 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016254 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016255 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16256 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16257 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16258 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16259 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16260 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16261 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16262 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016263}