blob: 6a95c31ac3fe55f49a16541b9b7a1bfc7594c17c [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);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100120
Jesse Barnes79e53942008-11-07 14:24:08 -0800121typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400122 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800123} intel_range_t;
124
125typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400126 int dot_limit;
127 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800128} intel_p2_t;
129
Ma Lingd4906092009-03-18 20:13:27 +0800130typedef struct intel_limit intel_limit_t;
131struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400132 intel_range_t dot, vco, n, m, m1, m2, p, p1;
133 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800134};
Jesse Barnes79e53942008-11-07 14:24:08 -0800135
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300136/* returns HPLL frequency in kHz */
137static int valleyview_get_vco(struct drm_i915_private *dev_priv)
138{
139 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
140
141 /* Obtain SKU information */
142 mutex_lock(&dev_priv->sb_lock);
143 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
144 CCK_FUSE_HPLL_FREQ_MASK;
145 mutex_unlock(&dev_priv->sb_lock);
146
147 return vco_freq[hpll_freq] * 1000;
148}
149
150static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
151 const char *name, u32 reg)
152{
153 u32 val;
154 int divider;
155
156 if (dev_priv->hpll_freq == 0)
157 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
158
159 mutex_lock(&dev_priv->sb_lock);
160 val = vlv_cck_read(dev_priv, reg);
161 mutex_unlock(&dev_priv->sb_lock);
162
163 divider = val & CCK_FREQUENCY_VALUES;
164
165 WARN((val & CCK_FREQUENCY_STATUS) !=
166 (divider << CCK_FREQUENCY_STATUS_SHIFT),
167 "%s change in progress\n", name);
168
169 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
170}
171
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200172static int
173intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200174{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200175 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200176}
177
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200178static int
179intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300180{
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200181 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
182 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200183}
184
185static int
186intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
187{
Jani Nikula79e50a42015-08-26 10:58:20 +0300188 uint32_t clkcfg;
189
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200190 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300191 clkcfg = I915_READ(CLKCFG);
192 switch (clkcfg & CLKCFG_FSB_MASK) {
193 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200194 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300195 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200196 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300197 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200198 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300199 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200200 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300201 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200202 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300203 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200204 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300205 /* these two are just a guess; one of them might be right */
206 case CLKCFG_FSB_1600:
207 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200208 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300209 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200210 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300211 }
212}
213
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200214static void intel_update_rawclk(struct drm_i915_private *dev_priv)
215{
216 if (HAS_PCH_SPLIT(dev_priv))
217 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
218 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
219 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
220 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
221 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
222 else
223 return; /* no rawclk on other platforms, or no need to know it */
224
225 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
226}
227
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300228static void intel_update_czclk(struct drm_i915_private *dev_priv)
229{
Wayne Boyer666a4532015-12-09 12:29:35 -0800230 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300231 return;
232
233 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
234 CCK_CZ_CLOCK_CONTROL);
235
236 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
237}
238
Chris Wilson021357a2010-09-07 20:54:59 +0100239static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200240intel_fdi_link_freq(struct drm_i915_private *dev_priv,
241 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100242{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200243 if (HAS_DDI(dev_priv))
244 return pipe_config->port_clock; /* SPLL */
245 else if (IS_GEN5(dev_priv))
246 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200247 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200248 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100249}
250
Daniel Vetter5d536e22013-07-06 12:52:06 +0200251static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400252 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200253 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200254 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400255 .m = { .min = 96, .max = 140 },
256 .m1 = { .min = 18, .max = 26 },
257 .m2 = { .min = 6, .max = 16 },
258 .p = { .min = 4, .max = 128 },
259 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700260 .p2 = { .dot_limit = 165000,
261 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700262};
263
Daniel Vetter5d536e22013-07-06 12:52:06 +0200264static const intel_limit_t intel_limits_i8xx_dvo = {
265 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200266 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200267 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200268 .m = { .min = 96, .max = 140 },
269 .m1 = { .min = 18, .max = 26 },
270 .m2 = { .min = 6, .max = 16 },
271 .p = { .min = 4, .max = 128 },
272 .p1 = { .min = 2, .max = 33 },
273 .p2 = { .dot_limit = 165000,
274 .p2_slow = 4, .p2_fast = 4 },
275};
276
Keith Packarde4b36692009-06-05 19:22:17 -0700277static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400278 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200279 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200280 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400281 .m = { .min = 96, .max = 140 },
282 .m1 = { .min = 18, .max = 26 },
283 .m2 = { .min = 6, .max = 16 },
284 .p = { .min = 4, .max = 128 },
285 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700286 .p2 = { .dot_limit = 165000,
287 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700288};
Eric Anholt273e27c2011-03-30 13:01:10 -0700289
Keith Packarde4b36692009-06-05 19:22:17 -0700290static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400291 .dot = { .min = 20000, .max = 400000 },
292 .vco = { .min = 1400000, .max = 2800000 },
293 .n = { .min = 1, .max = 6 },
294 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100295 .m1 = { .min = 8, .max = 18 },
296 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400297 .p = { .min = 5, .max = 80 },
298 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700299 .p2 = { .dot_limit = 200000,
300 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700301};
302
303static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400304 .dot = { .min = 20000, .max = 400000 },
305 .vco = { .min = 1400000, .max = 2800000 },
306 .n = { .min = 1, .max = 6 },
307 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100308 .m1 = { .min = 8, .max = 18 },
309 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400310 .p = { .min = 7, .max = 98 },
311 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700312 .p2 = { .dot_limit = 112000,
313 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700314};
315
Eric Anholt273e27c2011-03-30 13:01:10 -0700316
Keith Packarde4b36692009-06-05 19:22:17 -0700317static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700318 .dot = { .min = 25000, .max = 270000 },
319 .vco = { .min = 1750000, .max = 3500000},
320 .n = { .min = 1, .max = 4 },
321 .m = { .min = 104, .max = 138 },
322 .m1 = { .min = 17, .max = 23 },
323 .m2 = { .min = 5, .max = 11 },
324 .p = { .min = 10, .max = 30 },
325 .p1 = { .min = 1, .max = 3},
326 .p2 = { .dot_limit = 270000,
327 .p2_slow = 10,
328 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800329 },
Keith Packarde4b36692009-06-05 19:22:17 -0700330};
331
332static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700333 .dot = { .min = 22000, .max = 400000 },
334 .vco = { .min = 1750000, .max = 3500000},
335 .n = { .min = 1, .max = 4 },
336 .m = { .min = 104, .max = 138 },
337 .m1 = { .min = 16, .max = 23 },
338 .m2 = { .min = 5, .max = 11 },
339 .p = { .min = 5, .max = 80 },
340 .p1 = { .min = 1, .max = 8},
341 .p2 = { .dot_limit = 165000,
342 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700343};
344
345static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700346 .dot = { .min = 20000, .max = 115000 },
347 .vco = { .min = 1750000, .max = 3500000 },
348 .n = { .min = 1, .max = 3 },
349 .m = { .min = 104, .max = 138 },
350 .m1 = { .min = 17, .max = 23 },
351 .m2 = { .min = 5, .max = 11 },
352 .p = { .min = 28, .max = 112 },
353 .p1 = { .min = 2, .max = 8 },
354 .p2 = { .dot_limit = 0,
355 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800356 },
Keith Packarde4b36692009-06-05 19:22:17 -0700357};
358
359static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700360 .dot = { .min = 80000, .max = 224000 },
361 .vco = { .min = 1750000, .max = 3500000 },
362 .n = { .min = 1, .max = 3 },
363 .m = { .min = 104, .max = 138 },
364 .m1 = { .min = 17, .max = 23 },
365 .m2 = { .min = 5, .max = 11 },
366 .p = { .min = 14, .max = 42 },
367 .p1 = { .min = 2, .max = 6 },
368 .p2 = { .dot_limit = 0,
369 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800370 },
Keith Packarde4b36692009-06-05 19:22:17 -0700371};
372
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500373static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400374 .dot = { .min = 20000, .max = 400000},
375 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700376 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400377 .n = { .min = 3, .max = 6 },
378 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700379 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400380 .m1 = { .min = 0, .max = 0 },
381 .m2 = { .min = 0, .max = 254 },
382 .p = { .min = 5, .max = 80 },
383 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700384 .p2 = { .dot_limit = 200000,
385 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700386};
387
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500388static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400389 .dot = { .min = 20000, .max = 400000 },
390 .vco = { .min = 1700000, .max = 3500000 },
391 .n = { .min = 3, .max = 6 },
392 .m = { .min = 2, .max = 256 },
393 .m1 = { .min = 0, .max = 0 },
394 .m2 = { .min = 0, .max = 254 },
395 .p = { .min = 7, .max = 112 },
396 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700397 .p2 = { .dot_limit = 112000,
398 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700399};
400
Eric Anholt273e27c2011-03-30 13:01:10 -0700401/* Ironlake / Sandybridge
402 *
403 * We calculate clock using (register_value + 2) for N/M1/M2, so here
404 * the range value for them is (actual_value - 2).
405 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800406static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700407 .dot = { .min = 25000, .max = 350000 },
408 .vco = { .min = 1760000, .max = 3510000 },
409 .n = { .min = 1, .max = 5 },
410 .m = { .min = 79, .max = 127 },
411 .m1 = { .min = 12, .max = 22 },
412 .m2 = { .min = 5, .max = 9 },
413 .p = { .min = 5, .max = 80 },
414 .p1 = { .min = 1, .max = 8 },
415 .p2 = { .dot_limit = 225000,
416 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700417};
418
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800419static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700420 .dot = { .min = 25000, .max = 350000 },
421 .vco = { .min = 1760000, .max = 3510000 },
422 .n = { .min = 1, .max = 3 },
423 .m = { .min = 79, .max = 118 },
424 .m1 = { .min = 12, .max = 22 },
425 .m2 = { .min = 5, .max = 9 },
426 .p = { .min = 28, .max = 112 },
427 .p1 = { .min = 2, .max = 8 },
428 .p2 = { .dot_limit = 225000,
429 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800430};
431
432static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700433 .dot = { .min = 25000, .max = 350000 },
434 .vco = { .min = 1760000, .max = 3510000 },
435 .n = { .min = 1, .max = 3 },
436 .m = { .min = 79, .max = 127 },
437 .m1 = { .min = 12, .max = 22 },
438 .m2 = { .min = 5, .max = 9 },
439 .p = { .min = 14, .max = 56 },
440 .p1 = { .min = 2, .max = 8 },
441 .p2 = { .dot_limit = 225000,
442 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800443};
444
Eric Anholt273e27c2011-03-30 13:01:10 -0700445/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800446static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700447 .dot = { .min = 25000, .max = 350000 },
448 .vco = { .min = 1760000, .max = 3510000 },
449 .n = { .min = 1, .max = 2 },
450 .m = { .min = 79, .max = 126 },
451 .m1 = { .min = 12, .max = 22 },
452 .m2 = { .min = 5, .max = 9 },
453 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400454 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700455 .p2 = { .dot_limit = 225000,
456 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800457};
458
459static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700460 .dot = { .min = 25000, .max = 350000 },
461 .vco = { .min = 1760000, .max = 3510000 },
462 .n = { .min = 1, .max = 3 },
463 .m = { .min = 79, .max = 126 },
464 .m1 = { .min = 12, .max = 22 },
465 .m2 = { .min = 5, .max = 9 },
466 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400467 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700468 .p2 = { .dot_limit = 225000,
469 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800470};
471
Ville Syrjälädc730512013-09-24 21:26:30 +0300472static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300473 /*
474 * These are the data rate limits (measured in fast clocks)
475 * since those are the strictest limits we have. The fast
476 * clock and actual rate limits are more relaxed, so checking
477 * them would make no difference.
478 */
479 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200480 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700481 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700482 .m1 = { .min = 2, .max = 3 },
483 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300484 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300485 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700486};
487
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300488static const intel_limit_t intel_limits_chv = {
489 /*
490 * These are the data rate limits (measured in fast clocks)
491 * since those are the strictest limits we have. The fast
492 * clock and actual rate limits are more relaxed, so checking
493 * them would make no difference.
494 */
495 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200496 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300497 .n = { .min = 1, .max = 1 },
498 .m1 = { .min = 2, .max = 2 },
499 .m2 = { .min = 24 << 22, .max = 175 << 22 },
500 .p1 = { .min = 2, .max = 4 },
501 .p2 = { .p2_slow = 1, .p2_fast = 14 },
502};
503
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200504static const intel_limit_t intel_limits_bxt = {
505 /* FIXME: find real dot limits */
506 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530507 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200508 .n = { .min = 1, .max = 1 },
509 .m1 = { .min = 2, .max = 2 },
510 /* FIXME: find real m2 limits */
511 .m2 = { .min = 2 << 22, .max = 255 << 22 },
512 .p1 = { .min = 2, .max = 4 },
513 .p2 = { .p2_slow = 1, .p2_fast = 20 },
514};
515
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200516static bool
517needs_modeset(struct drm_crtc_state *state)
518{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200519 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200520}
521
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300522/**
523 * Returns whether any output on the specified pipe is of the specified type
524 */
Damien Lespiau40935612014-10-29 11:16:59 +0000525bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300526{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300527 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300528 struct intel_encoder *encoder;
529
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300530 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300531 if (encoder->type == type)
532 return true;
533
534 return false;
535}
536
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200537/**
538 * Returns whether any output on the specified pipe will have the specified
539 * type after a staged modeset is complete, i.e., the same as
540 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
541 * encoder->crtc.
542 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200543static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
544 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200545{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200546 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300547 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200548 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200549 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200550 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200551
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300552 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200553 if (connector_state->crtc != crtc_state->base.crtc)
554 continue;
555
556 num_connectors++;
557
558 encoder = to_intel_encoder(connector_state->best_encoder);
559 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200560 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200561 }
562
563 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200564
565 return false;
566}
567
Imre Deakdccbea32015-06-22 23:35:51 +0300568/*
569 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
570 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
571 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
572 * The helpers' return value is the rate of the clock that is fed to the
573 * display engine's pipe which can be the above fast dot clock rate or a
574 * divided-down version of it.
575 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500576/* m1 is reserved as 0 in Pineview, n is a ring counter */
Imre Deakdccbea32015-06-22 23:35:51 +0300577static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800578{
Shaohua Li21778322009-02-23 15:19:16 +0800579 clock->m = clock->m2 + 2;
580 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200581 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300582 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300583 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
584 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300585
586 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800587}
588
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200589static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
590{
591 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
592}
593
Imre Deakdccbea32015-06-22 23:35:51 +0300594static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800595{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200596 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800597 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200598 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300599 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300600 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
601 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300602
603 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800604}
605
Imre Deakdccbea32015-06-22 23:35:51 +0300606static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300607{
608 clock->m = clock->m1 * clock->m2;
609 clock->p = clock->p1 * clock->p2;
610 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300611 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300612 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
613 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300614
615 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300616}
617
Imre Deakdccbea32015-06-22 23:35:51 +0300618int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300619{
620 clock->m = clock->m1 * clock->m2;
621 clock->p = clock->p1 * clock->p2;
622 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300623 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300624 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
625 clock->n << 22);
626 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300627
628 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300629}
630
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800631#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800632/**
633 * Returns whether the given set of divisors are valid for a given refclk with
634 * the given connectors.
635 */
636
Chris Wilson1b894b52010-12-14 20:04:54 +0000637static bool intel_PLL_is_valid(struct drm_device *dev,
638 const intel_limit_t *limit,
639 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800640{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300641 if (clock->n < limit->n.min || limit->n.max < clock->n)
642 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800643 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400644 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800645 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400646 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800647 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400648 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300649
Wayne Boyer666a4532015-12-09 12:29:35 -0800650 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
651 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300652 if (clock->m1 <= clock->m2)
653 INTELPllInvalid("m1 <= m2\n");
654
Wayne Boyer666a4532015-12-09 12:29:35 -0800655 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300656 if (clock->p < limit->p.min || limit->p.max < clock->p)
657 INTELPllInvalid("p out of range\n");
658 if (clock->m < limit->m.min || limit->m.max < clock->m)
659 INTELPllInvalid("m out of range\n");
660 }
661
Jesse Barnes79e53942008-11-07 14:24:08 -0800662 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400663 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800664 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
665 * connector, etc., rather than just a single range.
666 */
667 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400668 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800669
670 return true;
671}
672
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300673static int
674i9xx_select_p2_div(const intel_limit_t *limit,
675 const struct intel_crtc_state *crtc_state,
676 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800677{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300678 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800679
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200680 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800681 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100682 * For LVDS just rely on its current settings for dual-channel.
683 * We haven't figured out how to reliably set up different
684 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800685 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100686 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300687 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800688 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300689 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800690 } else {
691 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300692 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800693 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300694 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800695 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300696}
697
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200698/*
699 * Returns a set of divisors for the desired target clock with the given
700 * refclk, or FALSE. The returned values represent the clock equation:
701 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
702 *
703 * Target and reference clocks are specified in kHz.
704 *
705 * If match_clock is provided, then best_clock P divider must match the P
706 * divider from @match_clock used for LVDS downclocking.
707 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300708static bool
709i9xx_find_best_dpll(const intel_limit_t *limit,
710 struct intel_crtc_state *crtc_state,
711 int target, int refclk, intel_clock_t *match_clock,
712 intel_clock_t *best_clock)
713{
714 struct drm_device *dev = crtc_state->base.crtc->dev;
715 intel_clock_t clock;
716 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800717
Akshay Joshi0206e352011-08-16 15:34:10 -0400718 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800719
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300720 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
721
Zhao Yakui42158662009-11-20 11:24:18 +0800722 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
723 clock.m1++) {
724 for (clock.m2 = limit->m2.min;
725 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200726 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800727 break;
728 for (clock.n = limit->n.min;
729 clock.n <= limit->n.max; clock.n++) {
730 for (clock.p1 = limit->p1.min;
731 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800732 int this_err;
733
Imre Deakdccbea32015-06-22 23:35:51 +0300734 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000735 if (!intel_PLL_is_valid(dev, limit,
736 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800737 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800738 if (match_clock &&
739 clock.p != match_clock->p)
740 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800741
742 this_err = abs(clock.dot - target);
743 if (this_err < err) {
744 *best_clock = clock;
745 err = this_err;
746 }
747 }
748 }
749 }
750 }
751
752 return (err != target);
753}
754
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200755/*
756 * Returns a set of divisors for the desired target clock with the given
757 * refclk, or FALSE. The returned values represent the clock equation:
758 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
759 *
760 * Target and reference clocks are specified in kHz.
761 *
762 * If match_clock is provided, then best_clock P divider must match the P
763 * divider from @match_clock used for LVDS downclocking.
764 */
Ma Lingd4906092009-03-18 20:13:27 +0800765static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200766pnv_find_best_dpll(const intel_limit_t *limit,
767 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200768 int target, int refclk, intel_clock_t *match_clock,
769 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200770{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300771 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200772 intel_clock_t clock;
773 int err = target;
774
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200775 memset(best_clock, 0, sizeof(*best_clock));
776
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300777 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
778
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200779 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
780 clock.m1++) {
781 for (clock.m2 = limit->m2.min;
782 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200783 for (clock.n = limit->n.min;
784 clock.n <= limit->n.max; clock.n++) {
785 for (clock.p1 = limit->p1.min;
786 clock.p1 <= limit->p1.max; clock.p1++) {
787 int this_err;
788
Imre Deakdccbea32015-06-22 23:35:51 +0300789 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800790 if (!intel_PLL_is_valid(dev, limit,
791 &clock))
792 continue;
793 if (match_clock &&
794 clock.p != match_clock->p)
795 continue;
796
797 this_err = abs(clock.dot - target);
798 if (this_err < err) {
799 *best_clock = clock;
800 err = this_err;
801 }
802 }
803 }
804 }
805 }
806
807 return (err != target);
808}
809
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200810/*
811 * Returns a set of divisors for the desired target clock with the given
812 * refclk, or FALSE. The returned values represent the clock equation:
813 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200814 *
815 * Target and reference clocks are specified in kHz.
816 *
817 * If match_clock is provided, then best_clock P divider must match the P
818 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200819 */
Ma Lingd4906092009-03-18 20:13:27 +0800820static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200821g4x_find_best_dpll(const intel_limit_t *limit,
822 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200823 int target, int refclk, intel_clock_t *match_clock,
824 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800825{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300826 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800827 intel_clock_t clock;
828 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300829 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400830 /* approximately equals target * 0.00585 */
831 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800832
833 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300834
835 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
836
Ma Lingd4906092009-03-18 20:13:27 +0800837 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200838 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800839 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200840 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800841 for (clock.m1 = limit->m1.max;
842 clock.m1 >= limit->m1.min; clock.m1--) {
843 for (clock.m2 = limit->m2.max;
844 clock.m2 >= limit->m2.min; clock.m2--) {
845 for (clock.p1 = limit->p1.max;
846 clock.p1 >= limit->p1.min; clock.p1--) {
847 int this_err;
848
Imre Deakdccbea32015-06-22 23:35:51 +0300849 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000850 if (!intel_PLL_is_valid(dev, limit,
851 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800852 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000853
854 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800855 if (this_err < err_most) {
856 *best_clock = clock;
857 err_most = this_err;
858 max_n = clock.n;
859 found = true;
860 }
861 }
862 }
863 }
864 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800865 return found;
866}
Ma Lingd4906092009-03-18 20:13:27 +0800867
Imre Deakd5dd62b2015-03-17 11:40:03 +0200868/*
869 * Check if the calculated PLL configuration is more optimal compared to the
870 * best configuration and error found so far. Return the calculated error.
871 */
872static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
873 const intel_clock_t *calculated_clock,
874 const intel_clock_t *best_clock,
875 unsigned int best_error_ppm,
876 unsigned int *error_ppm)
877{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200878 /*
879 * For CHV ignore the error and consider only the P value.
880 * Prefer a bigger P value based on HW requirements.
881 */
882 if (IS_CHERRYVIEW(dev)) {
883 *error_ppm = 0;
884
885 return calculated_clock->p > best_clock->p;
886 }
887
Imre Deak24be4e42015-03-17 11:40:04 +0200888 if (WARN_ON_ONCE(!target_freq))
889 return false;
890
Imre Deakd5dd62b2015-03-17 11:40:03 +0200891 *error_ppm = div_u64(1000000ULL *
892 abs(target_freq - calculated_clock->dot),
893 target_freq);
894 /*
895 * Prefer a better P value over a better (smaller) error if the error
896 * is small. Ensure this preference for future configurations too by
897 * setting the error to 0.
898 */
899 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
900 *error_ppm = 0;
901
902 return true;
903 }
904
905 return *error_ppm + 10 < best_error_ppm;
906}
907
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200908/*
909 * Returns a set of divisors for the desired target clock with the given
910 * refclk, or FALSE. The returned values represent the clock equation:
911 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
912 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800913static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200914vlv_find_best_dpll(const intel_limit_t *limit,
915 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200916 int target, int refclk, intel_clock_t *match_clock,
917 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700918{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200919 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300920 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300921 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300922 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300923 /* min update 19.2 MHz */
924 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300925 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700926
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300927 target *= 5; /* fast clock */
928
929 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700930
931 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300932 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300933 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300934 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300935 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300936 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700937 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300938 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200939 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300940
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300941 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
942 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300943
Imre Deakdccbea32015-06-22 23:35:51 +0300944 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300945
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300946 if (!intel_PLL_is_valid(dev, limit,
947 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300948 continue;
949
Imre Deakd5dd62b2015-03-17 11:40:03 +0200950 if (!vlv_PLL_is_optimal(dev, target,
951 &clock,
952 best_clock,
953 bestppm, &ppm))
954 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300955
Imre Deakd5dd62b2015-03-17 11:40:03 +0200956 *best_clock = clock;
957 bestppm = ppm;
958 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700959 }
960 }
961 }
962 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700963
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300964 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700965}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700966
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200967/*
968 * Returns a set of divisors for the desired target clock with the given
969 * refclk, or FALSE. The returned values represent the clock equation:
970 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
971 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300972static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200973chv_find_best_dpll(const intel_limit_t *limit,
974 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300975 int target, int refclk, intel_clock_t *match_clock,
976 intel_clock_t *best_clock)
977{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200978 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300979 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200980 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300981 intel_clock_t clock;
982 uint64_t m2;
983 int found = false;
984
985 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200986 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300987
988 /*
989 * Based on hardware doc, the n always set to 1, and m1 always
990 * set to 2. If requires to support 200Mhz refclk, we need to
991 * revisit this because n may not 1 anymore.
992 */
993 clock.n = 1, clock.m1 = 2;
994 target *= 5; /* fast clock */
995
996 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
997 for (clock.p2 = limit->p2.p2_fast;
998 clock.p2 >= limit->p2.p2_slow;
999 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001000 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001001
1002 clock.p = clock.p1 * clock.p2;
1003
1004 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1005 clock.n) << 22, refclk * clock.m1);
1006
1007 if (m2 > INT_MAX/clock.m1)
1008 continue;
1009
1010 clock.m2 = m2;
1011
Imre Deakdccbea32015-06-22 23:35:51 +03001012 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001013
1014 if (!intel_PLL_is_valid(dev, limit, &clock))
1015 continue;
1016
Imre Deak9ca3ba02015-03-17 11:40:05 +02001017 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1018 best_error_ppm, &error_ppm))
1019 continue;
1020
1021 *best_clock = clock;
1022 best_error_ppm = error_ppm;
1023 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001024 }
1025 }
1026
1027 return found;
1028}
1029
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001030bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1031 intel_clock_t *best_clock)
1032{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001033 int refclk = 100000;
1034 const intel_limit_t *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001035
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001036 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001037 target_clock, refclk, NULL, best_clock);
1038}
1039
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001040bool intel_crtc_active(struct drm_crtc *crtc)
1041{
1042 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1043
1044 /* Be paranoid as we can arrive here with only partial
1045 * state retrieved from the hardware during setup.
1046 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001047 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001048 * as Haswell has gained clock readout/fastboot support.
1049 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001050 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001051 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001052 *
1053 * FIXME: The intel_crtc->active here should be switched to
1054 * crtc->state->active once we have proper CRTC states wired up
1055 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001056 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001057 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001058 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001059}
1060
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001061enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1062 enum pipe pipe)
1063{
1064 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1065 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1066
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001067 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001068}
1069
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001070static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1071{
1072 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001073 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001074 u32 line1, line2;
1075 u32 line_mask;
1076
1077 if (IS_GEN2(dev))
1078 line_mask = DSL_LINEMASK_GEN2;
1079 else
1080 line_mask = DSL_LINEMASK_GEN3;
1081
1082 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001083 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001084 line2 = I915_READ(reg) & line_mask;
1085
1086 return line1 == line2;
1087}
1088
Keith Packardab7ad7f2010-10-03 00:33:06 -07001089/*
1090 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001091 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001092 *
1093 * After disabling a pipe, we can't wait for vblank in the usual way,
1094 * spinning on the vblank interrupt status bit, since we won't actually
1095 * see an interrupt when the pipe is disabled.
1096 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001097 * On Gen4 and above:
1098 * wait for the pipe register state bit to turn off
1099 *
1100 * Otherwise:
1101 * wait for the display line value to settle (it usually
1102 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001103 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001104 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001105static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001106{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001107 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001108 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001109 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001110 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001111
Keith Packardab7ad7f2010-10-03 00:33:06 -07001112 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001113 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001114
Keith Packardab7ad7f2010-10-03 00:33:06 -07001115 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001116 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1117 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001118 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001119 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001120 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001121 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001122 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001123 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001124}
1125
Jesse Barnesb24e7172011-01-04 15:09:30 -08001126/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001127void assert_pll(struct drm_i915_private *dev_priv,
1128 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001129{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001130 u32 val;
1131 bool cur_state;
1132
Ville Syrjälä649636e2015-09-22 19:50:01 +03001133 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001134 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001135 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001136 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001137 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001138}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001139
Jani Nikula23538ef2013-08-27 15:12:22 +03001140/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001141void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001142{
1143 u32 val;
1144 bool cur_state;
1145
Ville Syrjäläa5805162015-05-26 20:42:30 +03001146 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001147 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001148 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001149
1150 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001151 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001152 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001153 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001154}
Jani Nikula23538ef2013-08-27 15:12:22 +03001155
Jesse Barnes040484a2011-01-03 12:14:26 -08001156static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1157 enum pipe pipe, bool state)
1158{
Jesse Barnes040484a2011-01-03 12:14:26 -08001159 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001160 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1161 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001162
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001163 if (HAS_DDI(dev_priv->dev)) {
1164 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001165 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001166 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001167 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001168 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001169 cur_state = !!(val & FDI_TX_ENABLE);
1170 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001171 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001172 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001173 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001174}
1175#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1176#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1177
1178static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1179 enum pipe pipe, bool state)
1180{
Jesse Barnes040484a2011-01-03 12:14:26 -08001181 u32 val;
1182 bool cur_state;
1183
Ville Syrjälä649636e2015-09-22 19:50:01 +03001184 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001185 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001186 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001187 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001188 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001189}
1190#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1191#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1192
1193static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1194 enum pipe pipe)
1195{
Jesse Barnes040484a2011-01-03 12:14:26 -08001196 u32 val;
1197
1198 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001199 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001200 return;
1201
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001202 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001203 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001204 return;
1205
Ville Syrjälä649636e2015-09-22 19:50:01 +03001206 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001207 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 -08001208}
1209
Daniel Vetter55607e82013-06-16 21:42:39 +02001210void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1211 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001212{
Jesse Barnes040484a2011-01-03 12:14:26 -08001213 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001214 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001215
Ville Syrjälä649636e2015-09-22 19:50:01 +03001216 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001217 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001218 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001219 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001220 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001221}
1222
Daniel Vetterb680c372014-09-19 18:27:27 +02001223void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1224 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001225{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001226 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001227 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001228 u32 val;
1229 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001230 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001231
Jani Nikulabedd4db2014-08-22 15:04:13 +03001232 if (WARN_ON(HAS_DDI(dev)))
1233 return;
1234
1235 if (HAS_PCH_SPLIT(dev)) {
1236 u32 port_sel;
1237
Jesse Barnesea0760c2011-01-04 15:09:32 -08001238 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001239 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1240
1241 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1242 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1243 panel_pipe = PIPE_B;
1244 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001245 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001246 /* presumably write lock depends on pipe, not port select */
1247 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1248 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001249 } else {
1250 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001251 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1252 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001253 }
1254
1255 val = I915_READ(pp_reg);
1256 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001257 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001258 locked = false;
1259
Rob Clarke2c719b2014-12-15 13:56:32 -05001260 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001261 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001262 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001263}
1264
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001265static void assert_cursor(struct drm_i915_private *dev_priv,
1266 enum pipe pipe, bool state)
1267{
1268 struct drm_device *dev = dev_priv->dev;
1269 bool cur_state;
1270
Paulo Zanonid9d82082014-02-27 16:30:56 -03001271 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001272 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001273 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001274 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001275
Rob Clarke2c719b2014-12-15 13:56:32 -05001276 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001277 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001278 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001279}
1280#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1281#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1282
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001283void assert_pipe(struct drm_i915_private *dev_priv,
1284 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001285{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001286 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001287 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1288 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001289 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001290
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001291 /* if we need the pipe quirk it must be always on */
1292 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1293 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001294 state = true;
1295
Imre Deak4feed0e2016-02-12 18:55:14 +02001296 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1297 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001298 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001299 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001300
1301 intel_display_power_put(dev_priv, power_domain);
1302 } else {
1303 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001304 }
1305
Rob Clarke2c719b2014-12-15 13:56:32 -05001306 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001307 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001308 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001309}
1310
Chris Wilson931872f2012-01-16 23:01:13 +00001311static void assert_plane(struct drm_i915_private *dev_priv,
1312 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001313{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001314 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001315 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001316
Ville Syrjälä649636e2015-09-22 19:50:01 +03001317 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001318 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001319 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001320 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001321 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001322}
1323
Chris Wilson931872f2012-01-16 23:01:13 +00001324#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1325#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1326
Jesse Barnesb24e7172011-01-04 15:09:30 -08001327static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1328 enum pipe pipe)
1329{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001330 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001331 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001332
Ville Syrjälä653e1022013-06-04 13:49:05 +03001333 /* Primary planes are fixed to pipes on gen4+ */
1334 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001335 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001336 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001337 "plane %c assertion failure, should be disabled but not\n",
1338 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001339 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001340 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001341
Jesse Barnesb24e7172011-01-04 15:09:30 -08001342 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001343 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001344 u32 val = I915_READ(DSPCNTR(i));
1345 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001346 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001347 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001348 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1349 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001350 }
1351}
1352
Jesse Barnes19332d72013-03-28 09:55:38 -07001353static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1354 enum pipe pipe)
1355{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001356 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001357 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001358
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001359 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001360 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001361 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001362 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001363 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1364 sprite, pipe_name(pipe));
1365 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001366 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001367 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001368 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001369 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001370 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001371 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001372 }
1373 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001374 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001375 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001376 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001377 plane_name(pipe), pipe_name(pipe));
1378 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001379 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001380 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001381 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1382 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001383 }
1384}
1385
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001386static void assert_vblank_disabled(struct drm_crtc *crtc)
1387{
Rob Clarke2c719b2014-12-15 13:56:32 -05001388 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001389 drm_crtc_vblank_put(crtc);
1390}
1391
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001392void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1393 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001394{
Jesse Barnes92f25842011-01-04 15:09:34 -08001395 u32 val;
1396 bool enabled;
1397
Ville Syrjälä649636e2015-09-22 19:50:01 +03001398 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001399 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001400 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001401 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1402 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001403}
1404
Keith Packard4e634382011-08-06 10:39:45 -07001405static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1406 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001407{
1408 if ((val & DP_PORT_EN) == 0)
1409 return false;
1410
1411 if (HAS_PCH_CPT(dev_priv->dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001412 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001413 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1414 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001415 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1416 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1417 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001418 } else {
1419 if ((val & DP_PIPE_MASK) != (pipe << 30))
1420 return false;
1421 }
1422 return true;
1423}
1424
Keith Packard1519b992011-08-06 10:35:34 -07001425static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1426 enum pipe pipe, u32 val)
1427{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001428 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001429 return false;
1430
1431 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001432 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001433 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001434 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1435 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1436 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001437 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001438 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001439 return false;
1440 }
1441 return true;
1442}
1443
1444static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1445 enum pipe pipe, u32 val)
1446{
1447 if ((val & LVDS_PORT_EN) == 0)
1448 return false;
1449
1450 if (HAS_PCH_CPT(dev_priv->dev)) {
1451 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1452 return false;
1453 } else {
1454 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1455 return false;
1456 }
1457 return true;
1458}
1459
1460static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1461 enum pipe pipe, u32 val)
1462{
1463 if ((val & ADPA_DAC_ENABLE) == 0)
1464 return false;
1465 if (HAS_PCH_CPT(dev_priv->dev)) {
1466 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1467 return false;
1468 } else {
1469 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1470 return false;
1471 }
1472 return true;
1473}
1474
Jesse Barnes291906f2011-02-02 12:28:03 -08001475static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001476 enum pipe pipe, i915_reg_t reg,
1477 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001478{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001479 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001480 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001481 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001482 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001483
Rob Clarke2c719b2014-12-15 13:56:32 -05001484 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001485 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001486 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001487}
1488
1489static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001490 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001491{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001492 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001493 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001494 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001495 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001496
Rob Clarke2c719b2014-12-15 13:56:32 -05001497 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001498 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001499 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001500}
1501
1502static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1503 enum pipe pipe)
1504{
Jesse Barnes291906f2011-02-02 12:28:03 -08001505 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001506
Keith Packardf0575e92011-07-25 22:12:43 -07001507 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1508 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1509 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001510
Ville Syrjälä649636e2015-09-22 19:50:01 +03001511 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001512 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001513 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001514 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001515
Ville Syrjälä649636e2015-09-22 19:50:01 +03001516 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001517 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001518 "PCH LVDS 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
Paulo Zanonie2debe92013-02-18 19:00:27 -03001521 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1522 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1523 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001524}
1525
Ville Syrjäläd288f652014-10-28 13:20:22 +02001526static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001527 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001528{
Daniel Vetter426115c2013-07-11 22:13:42 +02001529 struct drm_device *dev = crtc->base.dev;
1530 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001531 enum pipe pipe = crtc->pipe;
1532 i915_reg_t reg = DPLL(pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001533 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001534
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001535 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001536
Daniel Vetter87442f72013-06-06 00:52:17 +02001537 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001538 if (IS_MOBILE(dev_priv->dev))
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001539 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001540
Daniel Vetter426115c2013-07-11 22:13:42 +02001541 I915_WRITE(reg, dpll);
1542 POSTING_READ(reg);
1543 udelay(150);
1544
1545 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001546 DRM_ERROR("DPLL %d failed to lock\n", pipe);
Daniel Vetter426115c2013-07-11 22:13:42 +02001547
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001548 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1549 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001550
1551 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001552 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001553 POSTING_READ(reg);
1554 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001555 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001556 POSTING_READ(reg);
1557 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001558 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001559 POSTING_READ(reg);
1560 udelay(150); /* wait for warmup */
1561}
1562
Ville Syrjäläd288f652014-10-28 13:20:22 +02001563static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001564 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001565{
1566 struct drm_device *dev = crtc->base.dev;
1567 struct drm_i915_private *dev_priv = dev->dev_private;
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ä8bd3f302016-03-15 16:39:57 +02001572 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001573
Ville Syrjäläa5805162015-05-26 20:42:30 +03001574 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001575
1576 /* Enable back the 10bit clock to display controller */
1577 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1578 tmp |= DPIO_DCLKP_EN;
1579 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1580
Ville Syrjälä54433e92015-05-26 20:42:31 +03001581 mutex_unlock(&dev_priv->sb_lock);
1582
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001583 /*
1584 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1585 */
1586 udelay(1);
1587
1588 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001589 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001590
1591 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001592 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001593 DRM_ERROR("PLL %d failed to lock\n", pipe);
1594
Ville Syrjäläc2317752016-03-15 16:39:56 +02001595 if (pipe != PIPE_A) {
1596 /*
1597 * WaPixelRepeatModeFixForC0:chv
1598 *
1599 * DPLLCMD is AWOL. Use chicken bits to propagate
1600 * the value from DPLLBMD to either pipe B or C.
1601 */
1602 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1603 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1604 I915_WRITE(CBR4_VLV, 0);
1605 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1606
1607 /*
1608 * DPLLB VGA mode also seems to cause problems.
1609 * We should always have it disabled.
1610 */
1611 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1612 } else {
1613 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1614 POSTING_READ(DPLL_MD(pipe));
1615 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001616}
1617
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001618static int intel_num_dvo_pipes(struct drm_device *dev)
1619{
1620 struct intel_crtc *crtc;
1621 int count = 0;
1622
1623 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001624 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001625 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001626
1627 return count;
1628}
1629
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001630static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001631{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001632 struct drm_device *dev = crtc->base.dev;
1633 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001634 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001635 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001636
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001637 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001638
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001639 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001640 if (IS_MOBILE(dev) && !IS_I830(dev))
1641 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001642
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001643 /* Enable DVO 2x clock on both PLLs if necessary */
1644 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1645 /*
1646 * It appears to be important that we don't enable this
1647 * for the current pipe before otherwise configuring the
1648 * PLL. No idea how this should be handled if multiple
1649 * DVO outputs are enabled simultaneosly.
1650 */
1651 dpll |= DPLL_DVO_2X_MODE;
1652 I915_WRITE(DPLL(!crtc->pipe),
1653 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1654 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001655
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001656 /*
1657 * Apparently we need to have VGA mode enabled prior to changing
1658 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1659 * dividers, even though the register value does change.
1660 */
1661 I915_WRITE(reg, 0);
1662
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001663 I915_WRITE(reg, dpll);
1664
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001665 /* Wait for the clocks to stabilize. */
1666 POSTING_READ(reg);
1667 udelay(150);
1668
1669 if (INTEL_INFO(dev)->gen >= 4) {
1670 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001671 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001672 } else {
1673 /* The pixel multiplier can only be updated once the
1674 * DPLL is enabled and the clocks are stable.
1675 *
1676 * So write it again.
1677 */
1678 I915_WRITE(reg, dpll);
1679 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001680
1681 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001682 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001683 POSTING_READ(reg);
1684 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001685 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001686 POSTING_READ(reg);
1687 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001688 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001689 POSTING_READ(reg);
1690 udelay(150); /* wait for warmup */
1691}
1692
1693/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001694 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001695 * @dev_priv: i915 private structure
1696 * @pipe: pipe PLL to disable
1697 *
1698 * Disable the PLL for @pipe, making sure the pipe is off first.
1699 *
1700 * Note! This is for pre-ILK only.
1701 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001702static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001703{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001704 struct drm_device *dev = crtc->base.dev;
1705 struct drm_i915_private *dev_priv = dev->dev_private;
1706 enum pipe pipe = crtc->pipe;
1707
1708 /* Disable DVO 2x clock on both PLLs if necessary */
1709 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001710 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001711 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001712 I915_WRITE(DPLL(PIPE_B),
1713 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1714 I915_WRITE(DPLL(PIPE_A),
1715 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1716 }
1717
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001718 /* Don't disable pipe or pipe PLLs if needed */
1719 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1720 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001721 return;
1722
1723 /* Make sure the pipe isn't still relying on us */
1724 assert_pipe_disabled(dev_priv, pipe);
1725
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001726 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001727 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001728}
1729
Jesse Barnesf6071162013-10-01 10:41:38 -07001730static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1731{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001732 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001733
1734 /* Make sure the pipe isn't still relying on us */
1735 assert_pipe_disabled(dev_priv, pipe);
1736
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001737 val = DPLL_INTEGRATED_REF_CLK_VLV |
1738 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1739 if (pipe != PIPE_A)
1740 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1741
Jesse Barnesf6071162013-10-01 10:41:38 -07001742 I915_WRITE(DPLL(pipe), val);
1743 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001744}
1745
1746static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1747{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001748 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001749 u32 val;
1750
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001751 /* Make sure the pipe isn't still relying on us */
1752 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001753
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001754 val = DPLL_SSC_REF_CLK_CHV |
1755 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001756 if (pipe != PIPE_A)
1757 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001758
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001759 I915_WRITE(DPLL(pipe), val);
1760 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001761
Ville Syrjäläa5805162015-05-26 20:42:30 +03001762 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001763
1764 /* Disable 10bit clock to display controller */
1765 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1766 val &= ~DPIO_DCLKP_EN;
1767 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1768
Ville Syrjäläa5805162015-05-26 20:42:30 +03001769 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001770}
1771
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001772void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001773 struct intel_digital_port *dport,
1774 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001775{
1776 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001777 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001778
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001779 switch (dport->port) {
1780 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001781 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001782 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001783 break;
1784 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001785 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001786 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001787 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001788 break;
1789 case PORT_D:
1790 port_mask = DPLL_PORTD_READY_MASK;
1791 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001792 break;
1793 default:
1794 BUG();
1795 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001796
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001797 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1798 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1799 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001800}
1801
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001802static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1803 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001804{
Daniel Vetter23670b322012-11-01 09:15:30 +01001805 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001806 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001807 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001808 i915_reg_t reg;
1809 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001810
Jesse Barnes040484a2011-01-03 12:14:26 -08001811 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001812 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001813
1814 /* FDI must be feeding us bits for PCH ports */
1815 assert_fdi_tx_enabled(dev_priv, pipe);
1816 assert_fdi_rx_enabled(dev_priv, pipe);
1817
Daniel Vetter23670b322012-11-01 09:15:30 +01001818 if (HAS_PCH_CPT(dev)) {
1819 /* Workaround: Set the timing override bit before enabling the
1820 * pch transcoder. */
1821 reg = TRANS_CHICKEN2(pipe);
1822 val = I915_READ(reg);
1823 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1824 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001825 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001826
Daniel Vetterab9412b2013-05-03 11:49:46 +02001827 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001828 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001829 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001830
1831 if (HAS_PCH_IBX(dev_priv->dev)) {
1832 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001833 * Make the BPC in transcoder be consistent with
1834 * that in pipeconf reg. For HDMI we must use 8bpc
1835 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001836 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001837 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001838 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1839 val |= PIPECONF_8BPC;
1840 else
1841 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001842 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001843
1844 val &= ~TRANS_INTERLACE_MASK;
1845 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001846 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001847 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001848 val |= TRANS_LEGACY_INTERLACED_ILK;
1849 else
1850 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001851 else
1852 val |= TRANS_PROGRESSIVE;
1853
Jesse Barnes040484a2011-01-03 12:14:26 -08001854 I915_WRITE(reg, val | TRANS_ENABLE);
1855 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001856 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001857}
1858
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001859static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001860 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001861{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001862 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001863
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001864 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001865 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001866 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001867
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001868 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001869 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001870 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001871 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001872
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001873 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001874 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001875
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001876 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1877 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001878 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001879 else
1880 val |= TRANS_PROGRESSIVE;
1881
Daniel Vetterab9412b2013-05-03 11:49:46 +02001882 I915_WRITE(LPT_TRANSCONF, val);
1883 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001884 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001885}
1886
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001887static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1888 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001889{
Daniel Vetter23670b322012-11-01 09:15:30 +01001890 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001891 i915_reg_t reg;
1892 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001893
1894 /* FDI relies on the transcoder */
1895 assert_fdi_tx_disabled(dev_priv, pipe);
1896 assert_fdi_rx_disabled(dev_priv, pipe);
1897
Jesse Barnes291906f2011-02-02 12:28:03 -08001898 /* Ports must be off as well */
1899 assert_pch_ports_disabled(dev_priv, pipe);
1900
Daniel Vetterab9412b2013-05-03 11:49:46 +02001901 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001902 val = I915_READ(reg);
1903 val &= ~TRANS_ENABLE;
1904 I915_WRITE(reg, val);
1905 /* wait for PCH transcoder off, transcoder state */
1906 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001907 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001908
Ville Syrjäläc4656132015-10-29 21:25:56 +02001909 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001910 /* Workaround: Clear the timing override chicken bit again. */
1911 reg = TRANS_CHICKEN2(pipe);
1912 val = I915_READ(reg);
1913 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1914 I915_WRITE(reg, val);
1915 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001916}
1917
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001918static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001919{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001920 u32 val;
1921
Daniel Vetterab9412b2013-05-03 11:49:46 +02001922 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001923 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001924 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001925 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001926 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001927 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001928
1929 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001930 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001931 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001932 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001933}
1934
1935/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001936 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001937 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001938 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001939 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001940 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001941 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001942static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001943{
Paulo Zanoni03722642014-01-17 13:51:09 -02001944 struct drm_device *dev = crtc->base.dev;
1945 struct drm_i915_private *dev_priv = dev->dev_private;
1946 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001947 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001948 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001949 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001950 u32 val;
1951
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001952 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1953
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001954 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001955 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001956 assert_sprites_disabled(dev_priv, pipe);
1957
Paulo Zanoni681e5812012-12-06 11:12:38 -02001958 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001959 pch_transcoder = TRANSCODER_A;
1960 else
1961 pch_transcoder = pipe;
1962
Jesse Barnesb24e7172011-01-04 15:09:30 -08001963 /*
1964 * A pipe without a PLL won't actually be able to drive bits from
1965 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1966 * need the check.
1967 */
Imre Deak50360402015-01-16 00:55:16 -08001968 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Jani Nikulaa65347b2015-11-27 12:21:46 +02001969 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03001970 assert_dsi_pll_enabled(dev_priv);
1971 else
1972 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001973 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001974 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001975 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001976 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001977 assert_fdi_tx_pll_enabled(dev_priv,
1978 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001979 }
1980 /* FIXME: assert CPU port conditions for SNB+ */
1981 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001982
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001983 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001984 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001985 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001986 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1987 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00001988 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001989 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001990
1991 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001992 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001993
1994 /*
1995 * Until the pipe starts DSL will read as 0, which would cause
1996 * an apparent vblank timestamp jump, which messes up also the
1997 * frame count when it's derived from the timestamps. So let's
1998 * wait for the pipe to start properly before we call
1999 * drm_crtc_vblank_on()
2000 */
2001 if (dev->max_vblank_count == 0 &&
2002 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2003 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002004}
2005
2006/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002007 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002008 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002009 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002010 * Disable the pipe of @crtc, making sure that various hardware
2011 * specific requirements are met, if applicable, e.g. plane
2012 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002013 *
2014 * Will wait until the pipe has shut down before returning.
2015 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002016static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002017{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002018 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002019 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002020 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002021 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002022 u32 val;
2023
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002024 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2025
Jesse Barnesb24e7172011-01-04 15:09:30 -08002026 /*
2027 * Make sure planes won't keep trying to pump pixels to us,
2028 * or we might hang the display.
2029 */
2030 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002031 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002032 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002033
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002034 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002035 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002036 if ((val & PIPECONF_ENABLE) == 0)
2037 return;
2038
Ville Syrjälä67adc642014-08-15 01:21:57 +03002039 /*
2040 * Double wide has implications for planes
2041 * so best keep it disabled when not needed.
2042 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002043 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002044 val &= ~PIPECONF_DOUBLE_WIDE;
2045
2046 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002047 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2048 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002049 val &= ~PIPECONF_ENABLE;
2050
2051 I915_WRITE(reg, val);
2052 if ((val & PIPECONF_ENABLE) == 0)
2053 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002054}
2055
Chris Wilson693db182013-03-05 14:52:39 +00002056static bool need_vtd_wa(struct drm_device *dev)
2057{
2058#ifdef CONFIG_INTEL_IOMMU
2059 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2060 return true;
2061#endif
2062 return false;
2063}
2064
Ville Syrjälä832be822016-01-12 21:08:33 +02002065static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2066{
2067 return IS_GEN2(dev_priv) ? 2048 : 4096;
2068}
2069
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002070static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2071 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002072{
2073 switch (fb_modifier) {
2074 case DRM_FORMAT_MOD_NONE:
2075 return cpp;
2076 case I915_FORMAT_MOD_X_TILED:
2077 if (IS_GEN2(dev_priv))
2078 return 128;
2079 else
2080 return 512;
2081 case I915_FORMAT_MOD_Y_TILED:
2082 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2083 return 128;
2084 else
2085 return 512;
2086 case I915_FORMAT_MOD_Yf_TILED:
2087 switch (cpp) {
2088 case 1:
2089 return 64;
2090 case 2:
2091 case 4:
2092 return 128;
2093 case 8:
2094 case 16:
2095 return 256;
2096 default:
2097 MISSING_CASE(cpp);
2098 return cpp;
2099 }
2100 break;
2101 default:
2102 MISSING_CASE(fb_modifier);
2103 return cpp;
2104 }
2105}
2106
Ville Syrjälä832be822016-01-12 21:08:33 +02002107unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2108 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002109{
Ville Syrjälä832be822016-01-12 21:08:33 +02002110 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2111 return 1;
2112 else
2113 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002114 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002115}
2116
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002117/* Return the tile dimensions in pixel units */
2118static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2119 unsigned int *tile_width,
2120 unsigned int *tile_height,
2121 uint64_t fb_modifier,
2122 unsigned int cpp)
2123{
2124 unsigned int tile_width_bytes =
2125 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2126
2127 *tile_width = tile_width_bytes / cpp;
2128 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2129}
2130
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002131unsigned int
2132intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002133 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002134{
Ville Syrjälä832be822016-01-12 21:08:33 +02002135 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2136 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2137
2138 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002139}
2140
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002141unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2142{
2143 unsigned int size = 0;
2144 int i;
2145
2146 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2147 size += rot_info->plane[i].width * rot_info->plane[i].height;
2148
2149 return size;
2150}
2151
Daniel Vetter75c82a52015-10-14 16:51:04 +02002152static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002153intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2154 const struct drm_framebuffer *fb,
2155 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002156{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002157 if (intel_rotation_90_or_270(rotation)) {
2158 *view = i915_ggtt_view_rotated;
2159 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2160 } else {
2161 *view = i915_ggtt_view_normal;
2162 }
2163}
2164
2165static void
2166intel_fill_fb_info(struct drm_i915_private *dev_priv,
2167 struct drm_framebuffer *fb)
2168{
2169 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002170 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002171
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002172 tile_size = intel_tile_size(dev_priv);
2173
2174 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002175 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2176 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002177
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002178 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2179 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002180
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002181 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002182 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002183 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2184 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002185
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002186 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002187 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2188 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002189 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002190}
2191
Ville Syrjälä603525d2016-01-12 21:08:37 +02002192static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002193{
2194 if (INTEL_INFO(dev_priv)->gen >= 9)
2195 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002196 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002197 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002198 return 128 * 1024;
2199 else if (INTEL_INFO(dev_priv)->gen >= 4)
2200 return 4 * 1024;
2201 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002202 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002203}
2204
Ville Syrjälä603525d2016-01-12 21:08:37 +02002205static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2206 uint64_t fb_modifier)
2207{
2208 switch (fb_modifier) {
2209 case DRM_FORMAT_MOD_NONE:
2210 return intel_linear_alignment(dev_priv);
2211 case I915_FORMAT_MOD_X_TILED:
2212 if (INTEL_INFO(dev_priv)->gen >= 9)
2213 return 256 * 1024;
2214 return 0;
2215 case I915_FORMAT_MOD_Y_TILED:
2216 case I915_FORMAT_MOD_Yf_TILED:
2217 return 1 * 1024 * 1024;
2218 default:
2219 MISSING_CASE(fb_modifier);
2220 return 0;
2221 }
2222}
2223
Chris Wilson127bd2a2010-07-23 23:32:05 +01002224int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002225intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2226 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002227{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002228 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002229 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002230 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002231 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002232 u32 alignment;
2233 int ret;
2234
Matt Roperebcdd392014-07-09 16:22:11 -07002235 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2236
Ville Syrjälä603525d2016-01-12 21:08:37 +02002237 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002238
Ville Syrjälä3465c582016-02-15 22:54:43 +02002239 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002240
Chris Wilson693db182013-03-05 14:52:39 +00002241 /* Note that the w/a also requires 64 PTE of padding following the
2242 * bo. We currently fill all unused PTE with the shadow page and so
2243 * we should always have valid PTE following the scanout preventing
2244 * the VT-d warning.
2245 */
2246 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2247 alignment = 256 * 1024;
2248
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002249 /*
2250 * Global gtt pte registers are special registers which actually forward
2251 * writes to a chunk of system memory. Which means that there is no risk
2252 * that the register values disappear as soon as we call
2253 * intel_runtime_pm_put(), so it is correct to wrap only the
2254 * pin/unpin/fence and not more.
2255 */
2256 intel_runtime_pm_get(dev_priv);
2257
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002258 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2259 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002260 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002261 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002262
2263 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2264 * fence, whereas 965+ only requires a fence if using
2265 * framebuffer compression. For simplicity, we always install
2266 * a fence as the cost is not that onerous.
2267 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002268 if (view.type == I915_GGTT_VIEW_NORMAL) {
2269 ret = i915_gem_object_get_fence(obj);
2270 if (ret == -EDEADLK) {
2271 /*
2272 * -EDEADLK means there are no free fences
2273 * no pending flips.
2274 *
2275 * This is propagated to atomic, but it uses
2276 * -EDEADLK to force a locking recovery, so
2277 * change the returned error to -EBUSY.
2278 */
2279 ret = -EBUSY;
2280 goto err_unpin;
2281 } else if (ret)
2282 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002283
Vivek Kasireddy98072162015-10-29 18:54:38 -07002284 i915_gem_object_pin_fence(obj);
2285 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002286
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002287 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002288 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002289
2290err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002291 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002292err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002293 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002294 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002295}
2296
Ville Syrjälä3465c582016-02-15 22:54:43 +02002297static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002298{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002299 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002300 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002301
Matt Roperebcdd392014-07-09 16:22:11 -07002302 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2303
Ville Syrjälä3465c582016-02-15 22:54:43 +02002304 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002305
Vivek Kasireddy98072162015-10-29 18:54:38 -07002306 if (view.type == I915_GGTT_VIEW_NORMAL)
2307 i915_gem_object_unpin_fence(obj);
2308
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002309 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002310}
2311
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002312/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002313 * Adjust the tile offset by moving the difference into
2314 * the x/y offsets.
2315 *
2316 * Input tile dimensions and pitch must already be
2317 * rotated to match x and y, and in pixel units.
2318 */
2319static u32 intel_adjust_tile_offset(int *x, int *y,
2320 unsigned int tile_width,
2321 unsigned int tile_height,
2322 unsigned int tile_size,
2323 unsigned int pitch_tiles,
2324 u32 old_offset,
2325 u32 new_offset)
2326{
2327 unsigned int tiles;
2328
2329 WARN_ON(old_offset & (tile_size - 1));
2330 WARN_ON(new_offset & (tile_size - 1));
2331 WARN_ON(new_offset > old_offset);
2332
2333 tiles = (old_offset - new_offset) / tile_size;
2334
2335 *y += tiles / pitch_tiles * tile_height;
2336 *x += tiles % pitch_tiles * tile_width;
2337
2338 return new_offset;
2339}
2340
2341/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002342 * Computes the linear offset to the base tile and adjusts
2343 * x, y. bytes per pixel is assumed to be a power-of-two.
2344 *
2345 * In the 90/270 rotated case, x and y are assumed
2346 * to be already rotated to match the rotated GTT view, and
2347 * pitch is the tile_height aligned framebuffer height.
2348 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002349u32 intel_compute_tile_offset(int *x, int *y,
2350 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002351 unsigned int pitch,
2352 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002353{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002354 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2355 uint64_t fb_modifier = fb->modifier[plane];
2356 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002357 u32 offset, offset_aligned, alignment;
2358
2359 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2360 if (alignment)
2361 alignment--;
2362
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002363 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002364 unsigned int tile_size, tile_width, tile_height;
2365 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002366
Ville Syrjäläd8433102016-01-12 21:08:35 +02002367 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002368 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2369 fb_modifier, cpp);
2370
2371 if (intel_rotation_90_or_270(rotation)) {
2372 pitch_tiles = pitch / tile_height;
2373 swap(tile_width, tile_height);
2374 } else {
2375 pitch_tiles = pitch / (tile_width * cpp);
2376 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002377
Ville Syrjäläd8433102016-01-12 21:08:35 +02002378 tile_rows = *y / tile_height;
2379 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002380
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002381 tiles = *x / tile_width;
2382 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002383
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002384 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2385 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002386
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002387 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2388 tile_size, pitch_tiles,
2389 offset, offset_aligned);
2390 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002391 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002392 offset_aligned = offset & ~alignment;
2393
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002394 *y = (offset & alignment) / pitch;
2395 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002396 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002397
2398 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002399}
2400
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002401static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002402{
2403 switch (format) {
2404 case DISPPLANE_8BPP:
2405 return DRM_FORMAT_C8;
2406 case DISPPLANE_BGRX555:
2407 return DRM_FORMAT_XRGB1555;
2408 case DISPPLANE_BGRX565:
2409 return DRM_FORMAT_RGB565;
2410 default:
2411 case DISPPLANE_BGRX888:
2412 return DRM_FORMAT_XRGB8888;
2413 case DISPPLANE_RGBX888:
2414 return DRM_FORMAT_XBGR8888;
2415 case DISPPLANE_BGRX101010:
2416 return DRM_FORMAT_XRGB2101010;
2417 case DISPPLANE_RGBX101010:
2418 return DRM_FORMAT_XBGR2101010;
2419 }
2420}
2421
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002422static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2423{
2424 switch (format) {
2425 case PLANE_CTL_FORMAT_RGB_565:
2426 return DRM_FORMAT_RGB565;
2427 default:
2428 case PLANE_CTL_FORMAT_XRGB_8888:
2429 if (rgb_order) {
2430 if (alpha)
2431 return DRM_FORMAT_ABGR8888;
2432 else
2433 return DRM_FORMAT_XBGR8888;
2434 } else {
2435 if (alpha)
2436 return DRM_FORMAT_ARGB8888;
2437 else
2438 return DRM_FORMAT_XRGB8888;
2439 }
2440 case PLANE_CTL_FORMAT_XRGB_2101010:
2441 if (rgb_order)
2442 return DRM_FORMAT_XBGR2101010;
2443 else
2444 return DRM_FORMAT_XRGB2101010;
2445 }
2446}
2447
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002448static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002449intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2450 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002451{
2452 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002453 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002454 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002455 struct drm_i915_gem_object *obj = NULL;
2456 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002457 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002458 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2459 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2460 PAGE_SIZE);
2461
2462 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002463
Chris Wilsonff2652e2014-03-10 08:07:02 +00002464 if (plane_config->size == 0)
2465 return false;
2466
Paulo Zanoni3badb492015-09-23 12:52:23 -03002467 /* If the FB is too big, just don't use it since fbdev is not very
2468 * important and we should probably use that space with FBC or other
2469 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002470 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002471 return false;
2472
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002473 mutex_lock(&dev->struct_mutex);
2474
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002475 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2476 base_aligned,
2477 base_aligned,
2478 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002479 if (!obj) {
2480 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002481 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002482 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002483
Damien Lespiau49af4492015-01-20 12:51:44 +00002484 obj->tiling_mode = plane_config->tiling;
2485 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002486 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002487
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002488 mode_cmd.pixel_format = fb->pixel_format;
2489 mode_cmd.width = fb->width;
2490 mode_cmd.height = fb->height;
2491 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002492 mode_cmd.modifier[0] = fb->modifier[0];
2493 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002494
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002495 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002496 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002497 DRM_DEBUG_KMS("intel fb init failed\n");
2498 goto out_unref_obj;
2499 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002500
Jesse Barnes46f297f2014-03-07 08:57:48 -08002501 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002502
Daniel Vetterf6936e22015-03-26 12:17:05 +01002503 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002504 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002505
2506out_unref_obj:
2507 drm_gem_object_unreference(&obj->base);
2508 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002509 return false;
2510}
2511
Matt Roperafd65eb2015-02-03 13:10:04 -08002512/* Update plane->state->fb to match plane->fb after driver-internal updates */
2513static void
2514update_state_fb(struct drm_plane *plane)
2515{
2516 if (plane->fb == plane->state->fb)
2517 return;
2518
2519 if (plane->state->fb)
2520 drm_framebuffer_unreference(plane->state->fb);
2521 plane->state->fb = plane->fb;
2522 if (plane->state->fb)
2523 drm_framebuffer_reference(plane->state->fb);
2524}
2525
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002526static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002527intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2528 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002529{
2530 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002531 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002532 struct drm_crtc *c;
2533 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002534 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002535 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002536 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002537 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2538 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002539 struct intel_plane_state *intel_state =
2540 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002541 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002542
Damien Lespiau2d140302015-02-05 17:22:18 +00002543 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002544 return;
2545
Daniel Vetterf6936e22015-03-26 12:17:05 +01002546 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002547 fb = &plane_config->fb->base;
2548 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002549 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002550
Damien Lespiau2d140302015-02-05 17:22:18 +00002551 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002552
2553 /*
2554 * Failed to alloc the obj, check to see if we should share
2555 * an fb with another CRTC instead
2556 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002557 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002558 i = to_intel_crtc(c);
2559
2560 if (c == &intel_crtc->base)
2561 continue;
2562
Matt Roper2ff8fde2014-07-08 07:50:07 -07002563 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002564 continue;
2565
Daniel Vetter88595ac2015-03-26 12:42:24 +01002566 fb = c->primary->fb;
2567 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002568 continue;
2569
Daniel Vetter88595ac2015-03-26 12:42:24 +01002570 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002571 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002572 drm_framebuffer_reference(fb);
2573 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002574 }
2575 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002576
Matt Roper200757f2015-12-03 11:37:36 -08002577 /*
2578 * We've failed to reconstruct the BIOS FB. Current display state
2579 * indicates that the primary plane is visible, but has a NULL FB,
2580 * which will lead to problems later if we don't fix it up. The
2581 * simplest solution is to just disable the primary plane now and
2582 * pretend the BIOS never had it enabled.
2583 */
2584 to_intel_plane_state(plane_state)->visible = false;
2585 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002586 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002587 intel_plane->disable_plane(primary, &intel_crtc->base);
2588
Daniel Vetter88595ac2015-03-26 12:42:24 +01002589 return;
2590
2591valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002592 plane_state->src_x = 0;
2593 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002594 plane_state->src_w = fb->width << 16;
2595 plane_state->src_h = fb->height << 16;
2596
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002597 plane_state->crtc_x = 0;
2598 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002599 plane_state->crtc_w = fb->width;
2600 plane_state->crtc_h = fb->height;
2601
Matt Roper0a8d8a82015-12-03 11:37:38 -08002602 intel_state->src.x1 = plane_state->src_x;
2603 intel_state->src.y1 = plane_state->src_y;
2604 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2605 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2606 intel_state->dst.x1 = plane_state->crtc_x;
2607 intel_state->dst.y1 = plane_state->crtc_y;
2608 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2609 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2610
Daniel Vetter88595ac2015-03-26 12:42:24 +01002611 obj = intel_fb_obj(fb);
2612 if (obj->tiling_mode != I915_TILING_NONE)
2613 dev_priv->preserve_bios_swizzle = true;
2614
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002615 drm_framebuffer_reference(fb);
2616 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002617 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002618 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002619 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002620}
2621
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002622static void i9xx_update_primary_plane(struct drm_plane *primary,
2623 const struct intel_crtc_state *crtc_state,
2624 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002625{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002626 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002627 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002628 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2629 struct drm_framebuffer *fb = plane_state->base.fb;
2630 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002631 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002632 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002633 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002634 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002635 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002636 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002637 int x = plane_state->src.x1 >> 16;
2638 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002639
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002640 dspcntr = DISPPLANE_GAMMA_ENABLE;
2641
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002642 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002643
2644 if (INTEL_INFO(dev)->gen < 4) {
2645 if (intel_crtc->pipe == PIPE_B)
2646 dspcntr |= DISPPLANE_SEL_PIPE_B;
2647
2648 /* pipesrc and dspsize control the size that is scaled from,
2649 * which should always be the user's requested size.
2650 */
2651 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002652 ((crtc_state->pipe_src_h - 1) << 16) |
2653 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002654 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002655 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2656 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002657 ((crtc_state->pipe_src_h - 1) << 16) |
2658 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002659 I915_WRITE(PRIMPOS(plane), 0);
2660 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002661 }
2662
Ville Syrjälä57779d02012-10-31 17:50:14 +02002663 switch (fb->pixel_format) {
2664 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002665 dspcntr |= DISPPLANE_8BPP;
2666 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002667 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002668 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002669 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002670 case DRM_FORMAT_RGB565:
2671 dspcntr |= DISPPLANE_BGRX565;
2672 break;
2673 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002674 dspcntr |= DISPPLANE_BGRX888;
2675 break;
2676 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002677 dspcntr |= DISPPLANE_RGBX888;
2678 break;
2679 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002680 dspcntr |= DISPPLANE_BGRX101010;
2681 break;
2682 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002683 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002684 break;
2685 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002686 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002687 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002688
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002689 if (INTEL_INFO(dev)->gen >= 4 &&
2690 obj->tiling_mode != I915_TILING_NONE)
2691 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002692
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002693 if (IS_G4X(dev))
2694 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2695
Ville Syrjäläac484962016-01-20 21:05:26 +02002696 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002697
Daniel Vetterc2c75132012-07-05 12:17:30 +02002698 if (INTEL_INFO(dev)->gen >= 4) {
2699 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002700 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002701 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002702 linear_offset -= intel_crtc->dspaddr_offset;
2703 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002704 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002705 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002706
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002707 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302708 dspcntr |= DISPPLANE_ROTATE_180;
2709
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002710 x += (crtc_state->pipe_src_w - 1);
2711 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302712
2713 /* Finding the last pixel of the last line of the display
2714 data and adding to linear_offset*/
2715 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002716 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002717 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302718 }
2719
Paulo Zanoni2db33662015-09-14 15:20:03 -03002720 intel_crtc->adjusted_x = x;
2721 intel_crtc->adjusted_y = y;
2722
Sonika Jindal48404c12014-08-22 14:06:04 +05302723 I915_WRITE(reg, dspcntr);
2724
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002725 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002726 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002727 I915_WRITE(DSPSURF(plane),
2728 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002729 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002730 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002731 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002732 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002733 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002734}
2735
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002736static void i9xx_disable_primary_plane(struct drm_plane *primary,
2737 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002738{
2739 struct drm_device *dev = crtc->dev;
2740 struct drm_i915_private *dev_priv = dev->dev_private;
2741 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002742 int plane = intel_crtc->plane;
2743
2744 I915_WRITE(DSPCNTR(plane), 0);
2745 if (INTEL_INFO(dev_priv)->gen >= 4)
2746 I915_WRITE(DSPSURF(plane), 0);
2747 else
2748 I915_WRITE(DSPADDR(plane), 0);
2749 POSTING_READ(DSPCNTR(plane));
2750}
2751
2752static void ironlake_update_primary_plane(struct drm_plane *primary,
2753 const struct intel_crtc_state *crtc_state,
2754 const struct intel_plane_state *plane_state)
2755{
2756 struct drm_device *dev = primary->dev;
2757 struct drm_i915_private *dev_priv = dev->dev_private;
2758 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2759 struct drm_framebuffer *fb = plane_state->base.fb;
2760 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002761 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002762 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002763 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002764 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002765 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002766 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002767 int x = plane_state->src.x1 >> 16;
2768 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002769
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002770 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002771 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002772
2773 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2774 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2775
Ville Syrjälä57779d02012-10-31 17:50:14 +02002776 switch (fb->pixel_format) {
2777 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002778 dspcntr |= DISPPLANE_8BPP;
2779 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002780 case DRM_FORMAT_RGB565:
2781 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002782 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002783 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002784 dspcntr |= DISPPLANE_BGRX888;
2785 break;
2786 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002787 dspcntr |= DISPPLANE_RGBX888;
2788 break;
2789 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002790 dspcntr |= DISPPLANE_BGRX101010;
2791 break;
2792 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002793 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002794 break;
2795 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002796 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002797 }
2798
2799 if (obj->tiling_mode != I915_TILING_NONE)
2800 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002801
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002802 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002803 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002804
Ville Syrjäläac484962016-01-20 21:05:26 +02002805 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002806 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002807 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002808 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002809 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002810 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302811 dspcntr |= DISPPLANE_ROTATE_180;
2812
2813 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002814 x += (crtc_state->pipe_src_w - 1);
2815 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302816
2817 /* Finding the last pixel of the last line of the display
2818 data and adding to linear_offset*/
2819 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002820 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002821 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302822 }
2823 }
2824
Paulo Zanoni2db33662015-09-14 15:20:03 -03002825 intel_crtc->adjusted_x = x;
2826 intel_crtc->adjusted_y = y;
2827
Sonika Jindal48404c12014-08-22 14:06:04 +05302828 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002829
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002830 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002831 I915_WRITE(DSPSURF(plane),
2832 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002833 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002834 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2835 } else {
2836 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2837 I915_WRITE(DSPLINOFF(plane), linear_offset);
2838 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002839 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002840}
2841
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002842u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2843 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002844{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002845 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2846 return 64;
2847 } else {
2848 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002849
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002850 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002851 }
2852}
2853
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002854u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2855 struct drm_i915_gem_object *obj,
2856 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002857{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002858 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002859 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002860 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002861
Ville Syrjäläe7941292016-01-19 18:23:17 +02002862 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002863 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002864
Daniel Vetterce7f1722015-10-14 16:51:06 +02002865 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002866 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002867 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002868 return -1;
2869
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002870 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002871
2872 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002873 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002874 PAGE_SIZE;
2875 }
2876
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002877 WARN_ON(upper_32_bits(offset));
2878
2879 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002880}
2881
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002882static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2883{
2884 struct drm_device *dev = intel_crtc->base.dev;
2885 struct drm_i915_private *dev_priv = dev->dev_private;
2886
2887 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2888 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2889 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002890}
2891
Chandra Kondurua1b22782015-04-07 15:28:45 -07002892/*
2893 * This function detaches (aka. unbinds) unused scalers in hardware
2894 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002895static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002896{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002897 struct intel_crtc_scaler_state *scaler_state;
2898 int i;
2899
Chandra Kondurua1b22782015-04-07 15:28:45 -07002900 scaler_state = &intel_crtc->config->scaler_state;
2901
2902 /* loop through and disable scalers that aren't in use */
2903 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002904 if (!scaler_state->scalers[i].in_use)
2905 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002906 }
2907}
2908
Chandra Konduru6156a452015-04-27 13:48:39 -07002909u32 skl_plane_ctl_format(uint32_t pixel_format)
2910{
Chandra Konduru6156a452015-04-27 13:48:39 -07002911 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002912 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002913 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002914 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002915 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002916 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002917 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002918 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002919 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002920 /*
2921 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2922 * to be already pre-multiplied. We need to add a knob (or a different
2923 * DRM_FORMAT) for user-space to configure that.
2924 */
2925 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002926 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002927 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002928 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002929 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002930 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002931 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002932 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002933 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002934 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002935 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002936 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002937 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002938 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002939 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002940 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002941 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002942 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002943 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002944 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002945 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002946
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002947 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002948}
2949
2950u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2951{
Chandra Konduru6156a452015-04-27 13:48:39 -07002952 switch (fb_modifier) {
2953 case DRM_FORMAT_MOD_NONE:
2954 break;
2955 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002956 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002957 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002958 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002959 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002960 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 default:
2962 MISSING_CASE(fb_modifier);
2963 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002964
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002965 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002966}
2967
2968u32 skl_plane_ctl_rotation(unsigned int rotation)
2969{
Chandra Konduru6156a452015-04-27 13:48:39 -07002970 switch (rotation) {
2971 case BIT(DRM_ROTATE_0):
2972 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302973 /*
2974 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2975 * while i915 HW rotation is clockwise, thats why this swapping.
2976 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002977 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302978 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002979 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002980 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002981 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302982 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002983 default:
2984 MISSING_CASE(rotation);
2985 }
2986
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002987 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002988}
2989
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002990static void skylake_update_primary_plane(struct drm_plane *plane,
2991 const struct intel_crtc_state *crtc_state,
2992 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01002993{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002994 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002995 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002996 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2997 struct drm_framebuffer *fb = plane_state->base.fb;
2998 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002999 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303000 u32 plane_ctl, stride_div, stride;
3001 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003002 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303003 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003004 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003005 int scaler_id = plane_state->scaler_id;
3006 int src_x = plane_state->src.x1 >> 16;
3007 int src_y = plane_state->src.y1 >> 16;
3008 int src_w = drm_rect_width(&plane_state->src) >> 16;
3009 int src_h = drm_rect_height(&plane_state->src) >> 16;
3010 int dst_x = plane_state->dst.x1;
3011 int dst_y = plane_state->dst.y1;
3012 int dst_w = drm_rect_width(&plane_state->dst);
3013 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003014
3015 plane_ctl = PLANE_CTL_ENABLE |
3016 PLANE_CTL_PIPE_GAMMA_ENABLE |
3017 PLANE_CTL_PIPE_CSC_ENABLE;
3018
Chandra Konduru6156a452015-04-27 13:48:39 -07003019 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3020 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003021 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003022 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003023
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003024 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003025 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003026 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303027
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003028 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003029
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303030 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003031 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3032
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303033 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003034 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303035 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003036 x_offset = stride * tile_height - src_y - src_h;
3037 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003038 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303039 } else {
3040 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003041 x_offset = src_x;
3042 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003043 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303044 }
3045 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003046
Paulo Zanoni2db33662015-09-14 15:20:03 -03003047 intel_crtc->adjusted_x = x_offset;
3048 intel_crtc->adjusted_y = y_offset;
3049
Damien Lespiau70d21f02013-07-03 21:06:04 +01003050 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303051 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3052 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3053 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003054
3055 if (scaler_id >= 0) {
3056 uint32_t ps_ctrl = 0;
3057
3058 WARN_ON(!dst_w || !dst_h);
3059 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3060 crtc_state->scaler_state.scalers[scaler_id].mode;
3061 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3062 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3063 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3064 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3065 I915_WRITE(PLANE_POS(pipe, 0), 0);
3066 } else {
3067 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3068 }
3069
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003070 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003071
3072 POSTING_READ(PLANE_SURF(pipe, 0));
3073}
3074
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003075static void skylake_disable_primary_plane(struct drm_plane *primary,
3076 struct drm_crtc *crtc)
3077{
3078 struct drm_device *dev = crtc->dev;
3079 struct drm_i915_private *dev_priv = dev->dev_private;
3080 int pipe = to_intel_crtc(crtc)->pipe;
3081
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003082 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3083 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3084 POSTING_READ(PLANE_SURF(pipe, 0));
3085}
3086
Jesse Barnes17638cd2011-06-24 12:19:23 -07003087/* Assume fb object is pinned & idle & fenced and just update base pointers */
3088static int
3089intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3090 int x, int y, enum mode_set_atomic state)
3091{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003092 /* Support for kgdboc is disabled, this needs a major rework. */
3093 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003094
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003095 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003096}
3097
Ville Syrjälä75147472014-11-24 18:28:11 +02003098static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003099{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003100 struct drm_crtc *crtc;
3101
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003102 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003103 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3104 enum plane plane = intel_crtc->plane;
3105
3106 intel_prepare_page_flip(dev, plane);
3107 intel_finish_page_flip_plane(dev, plane);
3108 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003109}
3110
3111static void intel_update_primary_planes(struct drm_device *dev)
3112{
Ville Syrjälä75147472014-11-24 18:28:11 +02003113 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003114
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003115 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003116 struct intel_plane *plane = to_intel_plane(crtc->primary);
3117 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003118
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003119 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003120 plane_state = to_intel_plane_state(plane->base.state);
3121
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003122 if (plane_state->visible)
3123 plane->update_plane(&plane->base,
3124 to_intel_crtc_state(crtc->state),
3125 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003126
3127 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003128 }
3129}
3130
Ville Syrjälä75147472014-11-24 18:28:11 +02003131void intel_prepare_reset(struct drm_device *dev)
3132{
3133 /* no reset support for gen2 */
3134 if (IS_GEN2(dev))
3135 return;
3136
3137 /* reset doesn't touch the display */
3138 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3139 return;
3140
3141 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003142 /*
3143 * Disabling the crtcs gracefully seems nicer. Also the
3144 * g33 docs say we should at least disable all the planes.
3145 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003146 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003147}
3148
3149void intel_finish_reset(struct drm_device *dev)
3150{
3151 struct drm_i915_private *dev_priv = to_i915(dev);
3152
3153 /*
3154 * Flips in the rings will be nuked by the reset,
3155 * so complete all pending flips so that user space
3156 * will get its events and not get stuck.
3157 */
3158 intel_complete_page_flips(dev);
3159
3160 /* no reset support for gen2 */
3161 if (IS_GEN2(dev))
3162 return;
3163
3164 /* reset doesn't touch the display */
3165 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3166 /*
3167 * Flips in the rings have been nuked by the reset,
3168 * so update the base address of all primary
3169 * planes to the the last fb to make sure we're
3170 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003171 *
3172 * FIXME: Atomic will make this obsolete since we won't schedule
3173 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003174 */
3175 intel_update_primary_planes(dev);
3176 return;
3177 }
3178
3179 /*
3180 * The display has been reset as well,
3181 * so need a full re-initialization.
3182 */
3183 intel_runtime_pm_disable_interrupts(dev_priv);
3184 intel_runtime_pm_enable_interrupts(dev_priv);
3185
3186 intel_modeset_init_hw(dev);
3187
3188 spin_lock_irq(&dev_priv->irq_lock);
3189 if (dev_priv->display.hpd_irq_setup)
3190 dev_priv->display.hpd_irq_setup(dev);
3191 spin_unlock_irq(&dev_priv->irq_lock);
3192
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003193 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003194
3195 intel_hpd_init(dev_priv);
3196
3197 drm_modeset_unlock_all(dev);
3198}
3199
Chris Wilson7d5e3792014-03-04 13:15:08 +00003200static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3201{
3202 struct drm_device *dev = crtc->dev;
3203 struct drm_i915_private *dev_priv = dev->dev_private;
3204 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003205 bool pending;
3206
3207 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3208 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3209 return false;
3210
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003211 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003212 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003213 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003214
3215 return pending;
3216}
3217
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003218static void intel_update_pipe_config(struct intel_crtc *crtc,
3219 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003220{
3221 struct drm_device *dev = crtc->base.dev;
3222 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003223 struct intel_crtc_state *pipe_config =
3224 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003225
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003226 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3227 crtc->base.mode = crtc->base.state->mode;
3228
3229 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3230 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3231 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003232
3233 /*
3234 * Update pipe size and adjust fitter if needed: the reason for this is
3235 * that in compute_mode_changes we check the native mode (not the pfit
3236 * mode) to see if we can flip rather than do a full mode set. In the
3237 * fastboot case, we'll flip, but if we don't update the pipesrc and
3238 * pfit state, we'll end up with a big fb scanned out into the wrong
3239 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003240 */
3241
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003242 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003243 ((pipe_config->pipe_src_w - 1) << 16) |
3244 (pipe_config->pipe_src_h - 1));
3245
3246 /* on skylake this is done by detaching scalers */
3247 if (INTEL_INFO(dev)->gen >= 9) {
3248 skl_detach_scalers(crtc);
3249
3250 if (pipe_config->pch_pfit.enabled)
3251 skylake_pfit_enable(crtc);
3252 } else if (HAS_PCH_SPLIT(dev)) {
3253 if (pipe_config->pch_pfit.enabled)
3254 ironlake_pfit_enable(crtc);
3255 else if (old_crtc_state->pch_pfit.enabled)
3256 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003257 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003258}
3259
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003260static void intel_fdi_normal_train(struct drm_crtc *crtc)
3261{
3262 struct drm_device *dev = crtc->dev;
3263 struct drm_i915_private *dev_priv = dev->dev_private;
3264 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3265 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003266 i915_reg_t reg;
3267 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003268
3269 /* enable normal train */
3270 reg = FDI_TX_CTL(pipe);
3271 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003272 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003273 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3274 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003275 } else {
3276 temp &= ~FDI_LINK_TRAIN_NONE;
3277 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003278 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003279 I915_WRITE(reg, temp);
3280
3281 reg = FDI_RX_CTL(pipe);
3282 temp = I915_READ(reg);
3283 if (HAS_PCH_CPT(dev)) {
3284 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3285 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3286 } else {
3287 temp &= ~FDI_LINK_TRAIN_NONE;
3288 temp |= FDI_LINK_TRAIN_NONE;
3289 }
3290 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3291
3292 /* wait one idle pattern time */
3293 POSTING_READ(reg);
3294 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003295
3296 /* IVB wants error correction enabled */
3297 if (IS_IVYBRIDGE(dev))
3298 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3299 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003300}
3301
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003302/* The FDI link training functions for ILK/Ibexpeak. */
3303static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3304{
3305 struct drm_device *dev = crtc->dev;
3306 struct drm_i915_private *dev_priv = dev->dev_private;
3307 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3308 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003309 i915_reg_t reg;
3310 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003311
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003312 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003313 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003314
Adam Jacksone1a44742010-06-25 15:32:14 -04003315 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3316 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003317 reg = FDI_RX_IMR(pipe);
3318 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003319 temp &= ~FDI_RX_SYMBOL_LOCK;
3320 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003321 I915_WRITE(reg, temp);
3322 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003323 udelay(150);
3324
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003325 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003326 reg = FDI_TX_CTL(pipe);
3327 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003328 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003329 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003330 temp &= ~FDI_LINK_TRAIN_NONE;
3331 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003332 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003333
Chris Wilson5eddb702010-09-11 13:48:45 +01003334 reg = FDI_RX_CTL(pipe);
3335 temp = I915_READ(reg);
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_RX_ENABLE);
3339
3340 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003341 udelay(150);
3342
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003343 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003344 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3345 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3346 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003347
Chris Wilson5eddb702010-09-11 13:48:45 +01003348 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003349 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003350 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003351 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3352
3353 if ((temp & FDI_RX_BIT_LOCK)) {
3354 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003355 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003356 break;
3357 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003358 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003359 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003360 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003361
3362 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003363 reg = FDI_TX_CTL(pipe);
3364 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003365 temp &= ~FDI_LINK_TRAIN_NONE;
3366 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003367 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003368
Chris Wilson5eddb702010-09-11 13:48:45 +01003369 reg = FDI_RX_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);
3374
3375 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003376 udelay(150);
3377
Chris Wilson5eddb702010-09-11 13:48:45 +01003378 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003379 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003380 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003381 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3382
3383 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003384 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003385 DRM_DEBUG_KMS("FDI train 2 done.\n");
3386 break;
3387 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003388 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003389 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003390 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003391
3392 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003393
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003394}
3395
Akshay Joshi0206e352011-08-16 15:34:10 -04003396static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003397 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3398 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3399 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3400 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3401};
3402
3403/* The FDI link training functions for SNB/Cougarpoint. */
3404static void gen6_fdi_link_train(struct drm_crtc *crtc)
3405{
3406 struct drm_device *dev = crtc->dev;
3407 struct drm_i915_private *dev_priv = dev->dev_private;
3408 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3409 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003410 i915_reg_t reg;
3411 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003412
Adam Jacksone1a44742010-06-25 15:32:14 -04003413 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3414 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003415 reg = FDI_RX_IMR(pipe);
3416 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003417 temp &= ~FDI_RX_SYMBOL_LOCK;
3418 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003419 I915_WRITE(reg, temp);
3420
3421 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003422 udelay(150);
3423
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003424 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003425 reg = FDI_TX_CTL(pipe);
3426 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003427 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003428 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003429 temp &= ~FDI_LINK_TRAIN_NONE;
3430 temp |= FDI_LINK_TRAIN_PATTERN_1;
3431 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3432 /* SNB-B */
3433 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003434 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003435
Daniel Vetterd74cf322012-10-26 10:58:13 +02003436 I915_WRITE(FDI_RX_MISC(pipe),
3437 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3438
Chris Wilson5eddb702010-09-11 13:48:45 +01003439 reg = FDI_RX_CTL(pipe);
3440 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003441 if (HAS_PCH_CPT(dev)) {
3442 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3443 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3444 } else {
3445 temp &= ~FDI_LINK_TRAIN_NONE;
3446 temp |= FDI_LINK_TRAIN_PATTERN_1;
3447 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003448 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3449
3450 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003451 udelay(150);
3452
Akshay Joshi0206e352011-08-16 15:34:10 -04003453 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003454 reg = FDI_TX_CTL(pipe);
3455 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003456 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3457 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003458 I915_WRITE(reg, temp);
3459
3460 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003461 udelay(500);
3462
Sean Paulfa37d392012-03-02 12:53:39 -05003463 for (retry = 0; retry < 5; retry++) {
3464 reg = FDI_RX_IIR(pipe);
3465 temp = I915_READ(reg);
3466 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3467 if (temp & FDI_RX_BIT_LOCK) {
3468 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3469 DRM_DEBUG_KMS("FDI train 1 done.\n");
3470 break;
3471 }
3472 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003473 }
Sean Paulfa37d392012-03-02 12:53:39 -05003474 if (retry < 5)
3475 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003476 }
3477 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003478 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003479
3480 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003481 reg = FDI_TX_CTL(pipe);
3482 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003483 temp &= ~FDI_LINK_TRAIN_NONE;
3484 temp |= FDI_LINK_TRAIN_PATTERN_2;
3485 if (IS_GEN6(dev)) {
3486 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3487 /* SNB-B */
3488 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3489 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003490 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003491
Chris Wilson5eddb702010-09-11 13:48:45 +01003492 reg = FDI_RX_CTL(pipe);
3493 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003494 if (HAS_PCH_CPT(dev)) {
3495 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3496 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3497 } else {
3498 temp &= ~FDI_LINK_TRAIN_NONE;
3499 temp |= FDI_LINK_TRAIN_PATTERN_2;
3500 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003501 I915_WRITE(reg, temp);
3502
3503 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003504 udelay(150);
3505
Akshay Joshi0206e352011-08-16 15:34:10 -04003506 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003507 reg = FDI_TX_CTL(pipe);
3508 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003509 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3510 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003511 I915_WRITE(reg, temp);
3512
3513 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003514 udelay(500);
3515
Sean Paulfa37d392012-03-02 12:53:39 -05003516 for (retry = 0; retry < 5; retry++) {
3517 reg = FDI_RX_IIR(pipe);
3518 temp = I915_READ(reg);
3519 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3520 if (temp & FDI_RX_SYMBOL_LOCK) {
3521 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3522 DRM_DEBUG_KMS("FDI train 2 done.\n");
3523 break;
3524 }
3525 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003526 }
Sean Paulfa37d392012-03-02 12:53:39 -05003527 if (retry < 5)
3528 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003529 }
3530 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003531 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003532
3533 DRM_DEBUG_KMS("FDI train done.\n");
3534}
3535
Jesse Barnes357555c2011-04-28 15:09:55 -07003536/* Manual link training for Ivy Bridge A0 parts */
3537static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3538{
3539 struct drm_device *dev = crtc->dev;
3540 struct drm_i915_private *dev_priv = dev->dev_private;
3541 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3542 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003543 i915_reg_t reg;
3544 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003545
3546 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3547 for train result */
3548 reg = FDI_RX_IMR(pipe);
3549 temp = I915_READ(reg);
3550 temp &= ~FDI_RX_SYMBOL_LOCK;
3551 temp &= ~FDI_RX_BIT_LOCK;
3552 I915_WRITE(reg, temp);
3553
3554 POSTING_READ(reg);
3555 udelay(150);
3556
Daniel Vetter01a415f2012-10-27 15:58:40 +02003557 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3558 I915_READ(FDI_RX_IIR(pipe)));
3559
Jesse Barnes139ccd32013-08-19 11:04:55 -07003560 /* Try each vswing and preemphasis setting twice before moving on */
3561 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3562 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003563 reg = FDI_TX_CTL(pipe);
3564 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003565 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3566 temp &= ~FDI_TX_ENABLE;
3567 I915_WRITE(reg, temp);
3568
3569 reg = FDI_RX_CTL(pipe);
3570 temp = I915_READ(reg);
3571 temp &= ~FDI_LINK_TRAIN_AUTO;
3572 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3573 temp &= ~FDI_RX_ENABLE;
3574 I915_WRITE(reg, temp);
3575
3576 /* enable CPU FDI TX and PCH FDI RX */
3577 reg = FDI_TX_CTL(pipe);
3578 temp = I915_READ(reg);
3579 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003580 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003581 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003582 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003583 temp |= snb_b_fdi_train_param[j/2];
3584 temp |= FDI_COMPOSITE_SYNC;
3585 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3586
3587 I915_WRITE(FDI_RX_MISC(pipe),
3588 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3589
3590 reg = FDI_RX_CTL(pipe);
3591 temp = I915_READ(reg);
3592 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3593 temp |= FDI_COMPOSITE_SYNC;
3594 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3595
3596 POSTING_READ(reg);
3597 udelay(1); /* should be 0.5us */
3598
3599 for (i = 0; i < 4; i++) {
3600 reg = FDI_RX_IIR(pipe);
3601 temp = I915_READ(reg);
3602 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3603
3604 if (temp & FDI_RX_BIT_LOCK ||
3605 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3606 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3607 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3608 i);
3609 break;
3610 }
3611 udelay(1); /* should be 0.5us */
3612 }
3613 if (i == 4) {
3614 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3615 continue;
3616 }
3617
3618 /* Train 2 */
3619 reg = FDI_TX_CTL(pipe);
3620 temp = I915_READ(reg);
3621 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3622 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3623 I915_WRITE(reg, temp);
3624
3625 reg = FDI_RX_CTL(pipe);
3626 temp = I915_READ(reg);
3627 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3628 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003629 I915_WRITE(reg, temp);
3630
3631 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003632 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003633
Jesse Barnes139ccd32013-08-19 11:04:55 -07003634 for (i = 0; i < 4; i++) {
3635 reg = FDI_RX_IIR(pipe);
3636 temp = I915_READ(reg);
3637 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003638
Jesse Barnes139ccd32013-08-19 11:04:55 -07003639 if (temp & FDI_RX_SYMBOL_LOCK ||
3640 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3641 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3642 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3643 i);
3644 goto train_done;
3645 }
3646 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003647 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003648 if (i == 4)
3649 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003650 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003651
Jesse Barnes139ccd32013-08-19 11:04:55 -07003652train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003653 DRM_DEBUG_KMS("FDI train done.\n");
3654}
3655
Daniel Vetter88cefb62012-08-12 19:27:14 +02003656static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003657{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003658 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003659 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003660 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003661 i915_reg_t reg;
3662 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003663
Jesse Barnes0e23b992010-09-10 11:10:00 -07003664 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003665 reg = FDI_RX_CTL(pipe);
3666 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003667 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003668 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003669 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003670 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3671
3672 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003673 udelay(200);
3674
3675 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003676 temp = I915_READ(reg);
3677 I915_WRITE(reg, temp | FDI_PCDCLK);
3678
3679 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003680 udelay(200);
3681
Paulo Zanoni20749732012-11-23 15:30:38 -02003682 /* Enable CPU FDI TX PLL, always on for Ironlake */
3683 reg = FDI_TX_CTL(pipe);
3684 temp = I915_READ(reg);
3685 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3686 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003687
Paulo Zanoni20749732012-11-23 15:30:38 -02003688 POSTING_READ(reg);
3689 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003690 }
3691}
3692
Daniel Vetter88cefb62012-08-12 19:27:14 +02003693static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3694{
3695 struct drm_device *dev = intel_crtc->base.dev;
3696 struct drm_i915_private *dev_priv = dev->dev_private;
3697 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003698 i915_reg_t reg;
3699 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003700
3701 /* Switch from PCDclk to Rawclk */
3702 reg = FDI_RX_CTL(pipe);
3703 temp = I915_READ(reg);
3704 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3705
3706 /* Disable CPU FDI TX PLL */
3707 reg = FDI_TX_CTL(pipe);
3708 temp = I915_READ(reg);
3709 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3710
3711 POSTING_READ(reg);
3712 udelay(100);
3713
3714 reg = FDI_RX_CTL(pipe);
3715 temp = I915_READ(reg);
3716 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3717
3718 /* Wait for the clocks to turn off. */
3719 POSTING_READ(reg);
3720 udelay(100);
3721}
3722
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003723static void ironlake_fdi_disable(struct drm_crtc *crtc)
3724{
3725 struct drm_device *dev = crtc->dev;
3726 struct drm_i915_private *dev_priv = dev->dev_private;
3727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3728 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003729 i915_reg_t reg;
3730 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003731
3732 /* disable CPU FDI tx and PCH FDI rx */
3733 reg = FDI_TX_CTL(pipe);
3734 temp = I915_READ(reg);
3735 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3736 POSTING_READ(reg);
3737
3738 reg = FDI_RX_CTL(pipe);
3739 temp = I915_READ(reg);
3740 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003741 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003742 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3743
3744 POSTING_READ(reg);
3745 udelay(100);
3746
3747 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003748 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003749 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003750
3751 /* still set train pattern 1 */
3752 reg = FDI_TX_CTL(pipe);
3753 temp = I915_READ(reg);
3754 temp &= ~FDI_LINK_TRAIN_NONE;
3755 temp |= FDI_LINK_TRAIN_PATTERN_1;
3756 I915_WRITE(reg, temp);
3757
3758 reg = FDI_RX_CTL(pipe);
3759 temp = I915_READ(reg);
3760 if (HAS_PCH_CPT(dev)) {
3761 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3762 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3763 } else {
3764 temp &= ~FDI_LINK_TRAIN_NONE;
3765 temp |= FDI_LINK_TRAIN_PATTERN_1;
3766 }
3767 /* BPC in FDI rx is consistent with that in PIPECONF */
3768 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003769 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003770 I915_WRITE(reg, temp);
3771
3772 POSTING_READ(reg);
3773 udelay(100);
3774}
3775
Chris Wilson5dce5b932014-01-20 10:17:36 +00003776bool intel_has_pending_fb_unpin(struct drm_device *dev)
3777{
3778 struct intel_crtc *crtc;
3779
3780 /* Note that we don't need to be called with mode_config.lock here
3781 * as our list of CRTC objects is static for the lifetime of the
3782 * device and so cannot disappear as we iterate. Similarly, we can
3783 * happily treat the predicates as racy, atomic checks as userspace
3784 * cannot claim and pin a new fb without at least acquring the
3785 * struct_mutex and so serialising with us.
3786 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003787 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003788 if (atomic_read(&crtc->unpin_work_count) == 0)
3789 continue;
3790
3791 if (crtc->unpin_work)
3792 intel_wait_for_vblank(dev, crtc->pipe);
3793
3794 return true;
3795 }
3796
3797 return false;
3798}
3799
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003800static void page_flip_completed(struct intel_crtc *intel_crtc)
3801{
3802 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3803 struct intel_unpin_work *work = intel_crtc->unpin_work;
3804
3805 /* ensure that the unpin work is consistent wrt ->pending. */
3806 smp_rmb();
3807 intel_crtc->unpin_work = NULL;
3808
3809 if (work->event)
3810 drm_send_vblank_event(intel_crtc->base.dev,
3811 intel_crtc->pipe,
3812 work->event);
3813
3814 drm_crtc_vblank_put(&intel_crtc->base);
3815
3816 wake_up_all(&dev_priv->pending_flip_queue);
3817 queue_work(dev_priv->wq, &work->work);
3818
3819 trace_i915_flip_complete(intel_crtc->plane,
3820 work->pending_flip_obj);
3821}
3822
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003823static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003824{
Chris Wilson0f911282012-04-17 10:05:38 +01003825 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003826 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003827 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003828
Daniel Vetter2c10d572012-12-20 21:24:07 +01003829 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003830
3831 ret = wait_event_interruptible_timeout(
3832 dev_priv->pending_flip_queue,
3833 !intel_crtc_has_pending_flip(crtc),
3834 60*HZ);
3835
3836 if (ret < 0)
3837 return ret;
3838
3839 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01003840 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003841
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003842 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003843 if (intel_crtc->unpin_work) {
3844 WARN_ONCE(1, "Removing stuck page flip\n");
3845 page_flip_completed(intel_crtc);
3846 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003847 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003848 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003849
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003850 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003851}
3852
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003853static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3854{
3855 u32 temp;
3856
3857 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3858
3859 mutex_lock(&dev_priv->sb_lock);
3860
3861 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3862 temp |= SBI_SSCCTL_DISABLE;
3863 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3864
3865 mutex_unlock(&dev_priv->sb_lock);
3866}
3867
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003868/* Program iCLKIP clock to the desired frequency */
3869static void lpt_program_iclkip(struct drm_crtc *crtc)
3870{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003871 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003872 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003873 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3874 u32 temp;
3875
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003876 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003877
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003878 /* The iCLK virtual clock root frequency is in MHz,
3879 * but the adjusted_mode->crtc_clock in in KHz. To get the
3880 * divisors, it is necessary to divide one by another, so we
3881 * convert the virtual clock precision to KHz here for higher
3882 * precision.
3883 */
3884 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003885 u32 iclk_virtual_root_freq = 172800 * 1000;
3886 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003887 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003888
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003889 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3890 clock << auxdiv);
3891 divsel = (desired_divisor / iclk_pi_range) - 2;
3892 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003893
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003894 /*
3895 * Near 20MHz is a corner case which is
3896 * out of range for the 7-bit divisor
3897 */
3898 if (divsel <= 0x7f)
3899 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003900 }
3901
3902 /* This should not happen with any sane values */
3903 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3904 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3905 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3906 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3907
3908 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 +03003909 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003910 auxdiv,
3911 divsel,
3912 phasedir,
3913 phaseinc);
3914
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003915 mutex_lock(&dev_priv->sb_lock);
3916
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003917 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003918 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003919 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3920 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3921 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3922 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3923 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3924 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003925 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003926
3927 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003928 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003929 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3930 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003931 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003932
3933 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003934 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003935 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003936 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003937
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003938 mutex_unlock(&dev_priv->sb_lock);
3939
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003940 /* Wait for initialization time */
3941 udelay(24);
3942
3943 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3944}
3945
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003946int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3947{
3948 u32 divsel, phaseinc, auxdiv;
3949 u32 iclk_virtual_root_freq = 172800 * 1000;
3950 u32 iclk_pi_range = 64;
3951 u32 desired_divisor;
3952 u32 temp;
3953
3954 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3955 return 0;
3956
3957 mutex_lock(&dev_priv->sb_lock);
3958
3959 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3960 if (temp & SBI_SSCCTL_DISABLE) {
3961 mutex_unlock(&dev_priv->sb_lock);
3962 return 0;
3963 }
3964
3965 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3966 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3967 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3968 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3969 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3970
3971 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3972 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3973 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3974
3975 mutex_unlock(&dev_priv->sb_lock);
3976
3977 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3978
3979 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3980 desired_divisor << auxdiv);
3981}
3982
Daniel Vetter275f01b22013-05-03 11:49:47 +02003983static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3984 enum pipe pch_transcoder)
3985{
3986 struct drm_device *dev = crtc->base.dev;
3987 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003988 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003989
3990 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3991 I915_READ(HTOTAL(cpu_transcoder)));
3992 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3993 I915_READ(HBLANK(cpu_transcoder)));
3994 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3995 I915_READ(HSYNC(cpu_transcoder)));
3996
3997 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3998 I915_READ(VTOTAL(cpu_transcoder)));
3999 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4000 I915_READ(VBLANK(cpu_transcoder)));
4001 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4002 I915_READ(VSYNC(cpu_transcoder)));
4003 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4004 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4005}
4006
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004007static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004008{
4009 struct drm_i915_private *dev_priv = dev->dev_private;
4010 uint32_t temp;
4011
4012 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004013 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004014 return;
4015
4016 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4017 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4018
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004019 temp &= ~FDI_BC_BIFURCATION_SELECT;
4020 if (enable)
4021 temp |= FDI_BC_BIFURCATION_SELECT;
4022
4023 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004024 I915_WRITE(SOUTH_CHICKEN1, temp);
4025 POSTING_READ(SOUTH_CHICKEN1);
4026}
4027
4028static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4029{
4030 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004031
4032 switch (intel_crtc->pipe) {
4033 case PIPE_A:
4034 break;
4035 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004036 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004037 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004038 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004039 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004040
4041 break;
4042 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004043 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004044
4045 break;
4046 default:
4047 BUG();
4048 }
4049}
4050
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004051/* Return which DP Port should be selected for Transcoder DP control */
4052static enum port
4053intel_trans_dp_port_sel(struct drm_crtc *crtc)
4054{
4055 struct drm_device *dev = crtc->dev;
4056 struct intel_encoder *encoder;
4057
4058 for_each_encoder_on_crtc(dev, crtc, encoder) {
4059 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4060 encoder->type == INTEL_OUTPUT_EDP)
4061 return enc_to_dig_port(&encoder->base)->port;
4062 }
4063
4064 return -1;
4065}
4066
Jesse Barnesf67a5592011-01-05 10:31:48 -08004067/*
4068 * Enable PCH resources required for PCH ports:
4069 * - PCH PLLs
4070 * - FDI training & RX/TX
4071 * - update transcoder timings
4072 * - DP transcoding bits
4073 * - transcoder
4074 */
4075static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004076{
4077 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004078 struct drm_i915_private *dev_priv = dev->dev_private;
4079 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4080 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004081 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004082
Daniel Vetterab9412b2013-05-03 11:49:46 +02004083 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004084
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004085 if (IS_IVYBRIDGE(dev))
4086 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4087
Daniel Vettercd986ab2012-10-26 10:58:12 +02004088 /* Write the TU size bits before fdi link training, so that error
4089 * detection works. */
4090 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4091 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4092
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004093 /*
4094 * Sometimes spurious CPU pipe underruns happen during FDI
4095 * training, at least with VGA+HDMI cloning. Suppress them.
4096 */
4097 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4098
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004099 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004100 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004101
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004102 /* We need to program the right clock selection before writing the pixel
4103 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004104 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004105 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004106
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004107 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004108 temp |= TRANS_DPLL_ENABLE(pipe);
4109 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004110 if (intel_crtc->config->shared_dpll ==
4111 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004112 temp |= sel;
4113 else
4114 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004115 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004116 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004117
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004118 /* XXX: pch pll's can be enabled any time before we enable the PCH
4119 * transcoder, and we actually should do this to not upset any PCH
4120 * transcoder that already use the clock when we share it.
4121 *
4122 * Note that enable_shared_dpll tries to do the right thing, but
4123 * get_shared_dpll unconditionally resets the pll - we need that to have
4124 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004125 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004126
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004127 /* set transcoder timing, panel must allow it */
4128 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004129 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004130
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004131 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004132
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004133 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4134
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004135 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004136 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004137 const struct drm_display_mode *adjusted_mode =
4138 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004139 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004140 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004141 temp = I915_READ(reg);
4142 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004143 TRANS_DP_SYNC_MASK |
4144 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004145 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004146 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004147
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004148 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004149 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004150 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004151 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004152
4153 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004154 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004155 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004156 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004157 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004158 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004159 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004160 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004161 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004162 break;
4163 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004164 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004165 }
4166
Chris Wilson5eddb702010-09-11 13:48:45 +01004167 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004168 }
4169
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004170 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004171}
4172
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004173static void lpt_pch_enable(struct drm_crtc *crtc)
4174{
4175 struct drm_device *dev = crtc->dev;
4176 struct drm_i915_private *dev_priv = dev->dev_private;
4177 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004178 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004179
Daniel Vetterab9412b2013-05-03 11:49:46 +02004180 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004181
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004182 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004183
Paulo Zanoni0540e482012-10-31 18:12:40 -02004184 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004185 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004186
Paulo Zanoni937bb612012-10-31 18:12:47 -02004187 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004188}
4189
Daniel Vettera1520312013-05-03 11:49:50 +02004190static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004191{
4192 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004193 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004194 u32 temp;
4195
4196 temp = I915_READ(dslreg);
4197 udelay(500);
4198 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004199 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004200 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004201 }
4202}
4203
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004204static int
4205skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4206 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4207 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004208{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004209 struct intel_crtc_scaler_state *scaler_state =
4210 &crtc_state->scaler_state;
4211 struct intel_crtc *intel_crtc =
4212 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004213 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004214
4215 need_scaling = intel_rotation_90_or_270(rotation) ?
4216 (src_h != dst_w || src_w != dst_h):
4217 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004218
4219 /*
4220 * if plane is being disabled or scaler is no more required or force detach
4221 * - free scaler binded to this plane/crtc
4222 * - in order to do this, update crtc->scaler_usage
4223 *
4224 * Here scaler state in crtc_state is set free so that
4225 * scaler can be assigned to other user. Actual register
4226 * update to free the scaler is done in plane/panel-fit programming.
4227 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4228 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004229 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004230 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004231 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004232 scaler_state->scalers[*scaler_id].in_use = 0;
4233
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004234 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4235 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4236 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004237 scaler_state->scaler_users);
4238 *scaler_id = -1;
4239 }
4240 return 0;
4241 }
4242
4243 /* range checks */
4244 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4245 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4246
4247 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4248 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004249 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004250 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004251 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004252 return -EINVAL;
4253 }
4254
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004255 /* mark this plane as a scaler user in crtc_state */
4256 scaler_state->scaler_users |= (1 << scaler_user);
4257 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4258 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4259 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4260 scaler_state->scaler_users);
4261
4262 return 0;
4263}
4264
4265/**
4266 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4267 *
4268 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004269 *
4270 * Return
4271 * 0 - scaler_usage updated successfully
4272 * error - requested scaling cannot be supported or other error condition
4273 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004274int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004275{
4276 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004277 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004278
4279 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4280 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4281
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004282 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004283 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004284 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004285 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004286}
4287
4288/**
4289 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4290 *
4291 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004292 * @plane_state: atomic plane state to update
4293 *
4294 * Return
4295 * 0 - scaler_usage updated successfully
4296 * error - requested scaling cannot be supported or other error condition
4297 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004298static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4299 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004300{
4301
4302 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004303 struct intel_plane *intel_plane =
4304 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004305 struct drm_framebuffer *fb = plane_state->base.fb;
4306 int ret;
4307
4308 bool force_detach = !fb || !plane_state->visible;
4309
4310 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4311 intel_plane->base.base.id, intel_crtc->pipe,
4312 drm_plane_index(&intel_plane->base));
4313
4314 ret = skl_update_scaler(crtc_state, force_detach,
4315 drm_plane_index(&intel_plane->base),
4316 &plane_state->scaler_id,
4317 plane_state->base.rotation,
4318 drm_rect_width(&plane_state->src) >> 16,
4319 drm_rect_height(&plane_state->src) >> 16,
4320 drm_rect_width(&plane_state->dst),
4321 drm_rect_height(&plane_state->dst));
4322
4323 if (ret || plane_state->scaler_id < 0)
4324 return ret;
4325
Chandra Kondurua1b22782015-04-07 15:28:45 -07004326 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004327 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004328 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004329 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004330 return -EINVAL;
4331 }
4332
4333 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004334 switch (fb->pixel_format) {
4335 case DRM_FORMAT_RGB565:
4336 case DRM_FORMAT_XBGR8888:
4337 case DRM_FORMAT_XRGB8888:
4338 case DRM_FORMAT_ABGR8888:
4339 case DRM_FORMAT_ARGB8888:
4340 case DRM_FORMAT_XRGB2101010:
4341 case DRM_FORMAT_XBGR2101010:
4342 case DRM_FORMAT_YUYV:
4343 case DRM_FORMAT_YVYU:
4344 case DRM_FORMAT_UYVY:
4345 case DRM_FORMAT_VYUY:
4346 break;
4347 default:
4348 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4349 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4350 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004351 }
4352
Chandra Kondurua1b22782015-04-07 15:28:45 -07004353 return 0;
4354}
4355
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004356static void skylake_scaler_disable(struct intel_crtc *crtc)
4357{
4358 int i;
4359
4360 for (i = 0; i < crtc->num_scalers; i++)
4361 skl_detach_scaler(crtc, i);
4362}
4363
4364static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004365{
4366 struct drm_device *dev = crtc->base.dev;
4367 struct drm_i915_private *dev_priv = dev->dev_private;
4368 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004369 struct intel_crtc_scaler_state *scaler_state =
4370 &crtc->config->scaler_state;
4371
4372 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4373
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004374 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004375 int id;
4376
4377 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4378 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4379 return;
4380 }
4381
4382 id = scaler_state->scaler_id;
4383 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4384 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4385 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4386 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4387
4388 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004389 }
4390}
4391
Jesse Barnesb074cec2013-04-25 12:55:02 -07004392static void ironlake_pfit_enable(struct intel_crtc *crtc)
4393{
4394 struct drm_device *dev = crtc->base.dev;
4395 struct drm_i915_private *dev_priv = dev->dev_private;
4396 int pipe = crtc->pipe;
4397
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004398 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004399 /* Force use of hard-coded filter coefficients
4400 * as some pre-programmed values are broken,
4401 * e.g. x201.
4402 */
4403 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4404 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4405 PF_PIPE_SEL_IVB(pipe));
4406 else
4407 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004408 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4409 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004410 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004411}
4412
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004413void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004414{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004415 struct drm_device *dev = crtc->base.dev;
4416 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004417
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004418 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004419 return;
4420
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004421 /*
4422 * We can only enable IPS after we enable a plane and wait for a vblank
4423 * This function is called from post_plane_update, which is run after
4424 * a vblank wait.
4425 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004426
Paulo Zanonid77e4532013-09-24 13:52:55 -03004427 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004428 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004429 mutex_lock(&dev_priv->rps.hw_lock);
4430 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4431 mutex_unlock(&dev_priv->rps.hw_lock);
4432 /* Quoting Art Runyan: "its not safe to expect any particular
4433 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004434 * mailbox." Moreover, the mailbox may return a bogus state,
4435 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004436 */
4437 } else {
4438 I915_WRITE(IPS_CTL, IPS_ENABLE);
4439 /* The bit only becomes 1 in the next vblank, so this wait here
4440 * is essentially intel_wait_for_vblank. If we don't have this
4441 * and don't wait for vblanks until the end of crtc_enable, then
4442 * the HW state readout code will complain that the expected
4443 * IPS_CTL value is not the one we read. */
4444 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4445 DRM_ERROR("Timed out waiting for IPS enable\n");
4446 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004447}
4448
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004449void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004450{
4451 struct drm_device *dev = crtc->base.dev;
4452 struct drm_i915_private *dev_priv = dev->dev_private;
4453
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004454 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004455 return;
4456
4457 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004458 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004459 mutex_lock(&dev_priv->rps.hw_lock);
4460 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4461 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004462 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4463 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4464 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004465 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004466 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004467 POSTING_READ(IPS_CTL);
4468 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004469
4470 /* We need to wait for a vblank before we can disable the plane. */
4471 intel_wait_for_vblank(dev, crtc->pipe);
4472}
4473
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004474static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004475{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004476 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004477 struct drm_device *dev = intel_crtc->base.dev;
4478 struct drm_i915_private *dev_priv = dev->dev_private;
4479
4480 mutex_lock(&dev->struct_mutex);
4481 dev_priv->mm.interruptible = false;
4482 (void) intel_overlay_switch_off(intel_crtc->overlay);
4483 dev_priv->mm.interruptible = true;
4484 mutex_unlock(&dev->struct_mutex);
4485 }
4486
4487 /* Let userspace switch the overlay on again. In most cases userspace
4488 * has to recompute where to put it anyway.
4489 */
4490}
4491
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004492/**
4493 * intel_post_enable_primary - Perform operations after enabling primary plane
4494 * @crtc: the CRTC whose primary plane was just enabled
4495 *
4496 * Performs potentially sleeping operations that must be done after the primary
4497 * plane is enabled, such as updating FBC and IPS. Note that this may be
4498 * called due to an explicit primary plane update, or due to an implicit
4499 * re-enable that is caused when a sprite plane is updated to no longer
4500 * completely hide the primary plane.
4501 */
4502static void
4503intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004504{
4505 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004506 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004507 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4508 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004509
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004510 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004511 * FIXME IPS should be fine as long as one plane is
4512 * enabled, but in practice it seems to have problems
4513 * when going from primary only to sprite only and vice
4514 * versa.
4515 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004516 hsw_enable_ips(intel_crtc);
4517
Daniel Vetterf99d7062014-06-19 16:01:59 +02004518 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004519 * Gen2 reports pipe underruns whenever all planes are disabled.
4520 * So don't enable underrun reporting before at least some planes
4521 * are enabled.
4522 * FIXME: Need to fix the logic to work when we turn off all planes
4523 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004524 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004525 if (IS_GEN2(dev))
4526 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4527
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004528 /* Underruns don't always raise interrupts, so check manually. */
4529 intel_check_cpu_fifo_underruns(dev_priv);
4530 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004531}
4532
Ville Syrjälä2622a082016-03-09 19:07:26 +02004533/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004534static void
4535intel_pre_disable_primary(struct drm_crtc *crtc)
4536{
4537 struct drm_device *dev = crtc->dev;
4538 struct drm_i915_private *dev_priv = dev->dev_private;
4539 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4540 int pipe = intel_crtc->pipe;
4541
4542 /*
4543 * Gen2 reports pipe underruns whenever all planes are disabled.
4544 * So diasble underrun reporting before all the planes get disabled.
4545 * FIXME: Need to fix the logic to work when we turn off all planes
4546 * but leave the pipe running.
4547 */
4548 if (IS_GEN2(dev))
4549 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4550
4551 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004552 * FIXME IPS should be fine as long as one plane is
4553 * enabled, but in practice it seems to have problems
4554 * when going from primary only to sprite only and vice
4555 * versa.
4556 */
4557 hsw_disable_ips(intel_crtc);
4558}
4559
4560/* FIXME get rid of this and use pre_plane_update */
4561static void
4562intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4563{
4564 struct drm_device *dev = crtc->dev;
4565 struct drm_i915_private *dev_priv = dev->dev_private;
4566 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4567 int pipe = intel_crtc->pipe;
4568
4569 intel_pre_disable_primary(crtc);
4570
4571 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004572 * Vblank time updates from the shadow to live plane control register
4573 * are blocked if the memory self-refresh mode is active at that
4574 * moment. So to make sure the plane gets truly disabled, disable
4575 * first the self-refresh mode. The self-refresh enable bit in turn
4576 * will be checked/applied by the HW only at the next frame start
4577 * event which is after the vblank start event, so we need to have a
4578 * wait-for-vblank between disabling the plane and the pipe.
4579 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004580 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004581 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004582 dev_priv->wm.vlv.cxsr = false;
4583 intel_wait_for_vblank(dev, pipe);
4584 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004585}
4586
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004587static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004588{
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004589 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4590 struct drm_atomic_state *old_state = old_crtc_state->base.state;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004591 struct intel_crtc_state *pipe_config =
4592 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004593 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004594 struct drm_plane *primary = crtc->base.primary;
4595 struct drm_plane_state *old_pri_state =
4596 drm_atomic_get_existing_plane_state(old_state, primary);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004597
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004598 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004599
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004600 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004601
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004602 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004603 intel_update_watermarks(&crtc->base);
4604
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004605 if (old_pri_state) {
4606 struct intel_plane_state *primary_state =
4607 to_intel_plane_state(primary->state);
4608 struct intel_plane_state *old_primary_state =
4609 to_intel_plane_state(old_pri_state);
4610
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004611 intel_fbc_post_update(crtc);
4612
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004613 if (primary_state->visible &&
4614 (needs_modeset(&pipe_config->base) ||
4615 !old_primary_state->visible))
4616 intel_post_enable_primary(&crtc->base);
4617 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004618}
4619
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004620static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004621{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004622 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004623 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004624 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004625 struct intel_crtc_state *pipe_config =
4626 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004627 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4628 struct drm_plane *primary = crtc->base.primary;
4629 struct drm_plane_state *old_pri_state =
4630 drm_atomic_get_existing_plane_state(old_state, primary);
4631 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004632
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004633 if (old_pri_state) {
4634 struct intel_plane_state *primary_state =
4635 to_intel_plane_state(primary->state);
4636 struct intel_plane_state *old_primary_state =
4637 to_intel_plane_state(old_pri_state);
4638
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004639 intel_fbc_pre_update(crtc);
4640
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004641 if (old_primary_state->visible &&
4642 (modeset || !primary_state->visible))
4643 intel_pre_disable_primary(&crtc->base);
4644 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004645
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004646 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004647 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004648
Ville Syrjälä2622a082016-03-09 19:07:26 +02004649 /*
4650 * Vblank time updates from the shadow to live plane control register
4651 * are blocked if the memory self-refresh mode is active at that
4652 * moment. So to make sure the plane gets truly disabled, disable
4653 * first the self-refresh mode. The self-refresh enable bit in turn
4654 * will be checked/applied by the HW only at the next frame start
4655 * event which is after the vblank start event, so we need to have a
4656 * wait-for-vblank between disabling the plane and the pipe.
4657 */
4658 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004659 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004660 dev_priv->wm.vlv.cxsr = false;
4661 intel_wait_for_vblank(dev, crtc->pipe);
4662 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004663 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004664
Matt Ropered4a6a72016-02-23 17:20:13 -08004665 /*
4666 * IVB workaround: must disable low power watermarks for at least
4667 * one frame before enabling scaling. LP watermarks can be re-enabled
4668 * when scaling is disabled.
4669 *
4670 * WaCxSRDisabledForSpriteScaling:ivb
4671 */
4672 if (pipe_config->disable_lp_wm) {
4673 ilk_disable_lp_wm(dev);
4674 intel_wait_for_vblank(dev, crtc->pipe);
4675 }
4676
4677 /*
4678 * If we're doing a modeset, we're done. No need to do any pre-vblank
4679 * watermark programming here.
4680 */
4681 if (needs_modeset(&pipe_config->base))
4682 return;
4683
4684 /*
4685 * For platforms that support atomic watermarks, program the
4686 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4687 * will be the intermediate values that are safe for both pre- and
4688 * post- vblank; when vblank happens, the 'active' values will be set
4689 * to the final 'target' values and we'll do this again to get the
4690 * optimal watermarks. For gen9+ platforms, the values we program here
4691 * will be the final target values which will get automatically latched
4692 * at vblank time; no further programming will be necessary.
4693 *
4694 * If a platform hasn't been transitioned to atomic watermarks yet,
4695 * we'll continue to update watermarks the old way, if flags tell
4696 * us to.
4697 */
4698 if (dev_priv->display.initial_watermarks != NULL)
4699 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004700 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004701 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004702}
4703
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004704static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004705{
4706 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004707 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004708 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004709 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004710
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004711 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004712
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004713 drm_for_each_plane_mask(p, dev, plane_mask)
4714 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004715
Daniel Vetterf99d7062014-06-19 16:01:59 +02004716 /*
4717 * FIXME: Once we grow proper nuclear flip support out of this we need
4718 * to compute the mask of flip planes precisely. For the time being
4719 * consider this a flip to a NULL plane.
4720 */
4721 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004722}
4723
Jesse Barnesf67a5592011-01-05 10:31:48 -08004724static void ironlake_crtc_enable(struct drm_crtc *crtc)
4725{
4726 struct drm_device *dev = crtc->dev;
4727 struct drm_i915_private *dev_priv = dev->dev_private;
4728 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004729 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004730 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004731 struct intel_crtc_state *pipe_config =
4732 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004733
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004734 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004735 return;
4736
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004737 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004738 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4739
4740 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004741 intel_prepare_shared_dpll(intel_crtc);
4742
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004743 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304744 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004745
4746 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004747 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004748
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004749 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004750 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004751 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004752 }
4753
4754 ironlake_set_pipeconf(crtc);
4755
Jesse Barnesf67a5592011-01-05 10:31:48 -08004756 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004757
Daniel Vettera72e4c92014-09-30 10:56:47 +02004758 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004759
Daniel Vetterf6736a12013-06-05 13:34:30 +02004760 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004761 if (encoder->pre_enable)
4762 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004763
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004764 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004765 /* Note: FDI PLL enabling _must_ be done before we enable the
4766 * cpu pipes, hence this is separate from all the other fdi/pch
4767 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004768 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004769 } else {
4770 assert_fdi_tx_disabled(dev_priv, pipe);
4771 assert_fdi_rx_disabled(dev_priv, pipe);
4772 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004773
Jesse Barnesb074cec2013-04-25 12:55:02 -07004774 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004775
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004776 /*
4777 * On ILK+ LUT must be loaded before the pipe is running but with
4778 * clocks enabled
4779 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004780 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004781
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004782 if (dev_priv->display.initial_watermarks != NULL)
4783 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004784 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004785
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004786 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004787 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004788
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004789 assert_vblank_disabled(crtc);
4790 drm_crtc_vblank_on(crtc);
4791
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004792 for_each_encoder_on_crtc(dev, crtc, encoder)
4793 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004794
4795 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004796 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004797
4798 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4799 if (intel_crtc->config->has_pch_encoder)
4800 intel_wait_for_vblank(dev, pipe);
4801 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004802}
4803
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004804/* IPS only exists on ULT machines and is tied to pipe A. */
4805static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4806{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004807 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004808}
4809
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004810static void haswell_crtc_enable(struct drm_crtc *crtc)
4811{
4812 struct drm_device *dev = crtc->dev;
4813 struct drm_i915_private *dev_priv = dev->dev_private;
4814 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4815 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004816 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004817 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004818 struct intel_crtc_state *pipe_config =
4819 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004820
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004821 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004822 return;
4823
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004824 if (intel_crtc->config->has_pch_encoder)
4825 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4826 false);
4827
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004828 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004829 intel_enable_shared_dpll(intel_crtc);
4830
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004831 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304832 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004833
Jani Nikula4d1de972016-03-18 17:05:42 +02004834 if (!intel_crtc->config->has_dsi_encoder)
4835 intel_set_pipe_timings(intel_crtc);
4836
Jani Nikulabc58be62016-03-18 17:05:39 +02004837 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004838
Jani Nikula4d1de972016-03-18 17:05:42 +02004839 if (cpu_transcoder != TRANSCODER_EDP &&
4840 !transcoder_is_dsi(cpu_transcoder)) {
4841 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004842 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004843 }
4844
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004845 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004846 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004847 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004848 }
4849
Jani Nikula4d1de972016-03-18 17:05:42 +02004850 if (!intel_crtc->config->has_dsi_encoder)
4851 haswell_set_pipeconf(crtc);
4852
Jani Nikula391bf042016-03-18 17:05:40 +02004853 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004854
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004855 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02004856
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004857 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004858
Daniel Vetter6b698512015-11-28 11:05:39 +01004859 if (intel_crtc->config->has_pch_encoder)
4860 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4861 else
4862 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4863
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304864 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004865 if (encoder->pre_enable)
4866 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304867 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004868
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004869 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004870 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004871
Jani Nikulaa65347b2015-11-27 12:21:46 +02004872 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304873 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004874
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004875 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004876 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004877 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004878 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004879
4880 /*
4881 * On ILK+ LUT must be loaded before the pipe is running but with
4882 * clocks enabled
4883 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004884 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004885
Paulo Zanoni1f544382012-10-24 11:32:00 -02004886 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004887 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304888 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004889
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004890 if (dev_priv->display.initial_watermarks != NULL)
4891 dev_priv->display.initial_watermarks(pipe_config);
4892 else
4893 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004894
4895 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4896 if (!intel_crtc->config->has_dsi_encoder)
4897 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004898
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004899 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004900 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004901
Jani Nikulaa65347b2015-11-27 12:21:46 +02004902 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004903 intel_ddi_set_vc_payload_alloc(crtc, true);
4904
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004905 assert_vblank_disabled(crtc);
4906 drm_crtc_vblank_on(crtc);
4907
Jani Nikula8807e552013-08-30 19:40:32 +03004908 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004909 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004910 intel_opregion_notify_encoder(encoder, true);
4911 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004912
Daniel Vetter6b698512015-11-28 11:05:39 +01004913 if (intel_crtc->config->has_pch_encoder) {
4914 intel_wait_for_vblank(dev, pipe);
4915 intel_wait_for_vblank(dev, pipe);
4916 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004917 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4918 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004919 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004920
Paulo Zanonie4916942013-09-20 16:21:19 -03004921 /* If we change the relative order between pipe/planes enabling, we need
4922 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004923 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4924 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4925 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4926 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4927 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004928}
4929
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004930static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004931{
4932 struct drm_device *dev = crtc->base.dev;
4933 struct drm_i915_private *dev_priv = dev->dev_private;
4934 int pipe = crtc->pipe;
4935
4936 /* To avoid upsetting the power well on haswell only disable the pfit if
4937 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004938 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004939 I915_WRITE(PF_CTL(pipe), 0);
4940 I915_WRITE(PF_WIN_POS(pipe), 0);
4941 I915_WRITE(PF_WIN_SZ(pipe), 0);
4942 }
4943}
4944
Jesse Barnes6be4a602010-09-10 10:26:01 -07004945static void ironlake_crtc_disable(struct drm_crtc *crtc)
4946{
4947 struct drm_device *dev = crtc->dev;
4948 struct drm_i915_private *dev_priv = dev->dev_private;
4949 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004950 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004951 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004952
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004953 if (intel_crtc->config->has_pch_encoder)
4954 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4955
Daniel Vetterea9d7582012-07-10 10:42:52 +02004956 for_each_encoder_on_crtc(dev, crtc, encoder)
4957 encoder->disable(encoder);
4958
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004959 drm_crtc_vblank_off(crtc);
4960 assert_vblank_disabled(crtc);
4961
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004962 /*
4963 * Sometimes spurious CPU pipe underruns happen when the
4964 * pipe is already disabled, but FDI RX/TX is still enabled.
4965 * Happens at least with VGA+HDMI cloning. Suppress them.
4966 */
4967 if (intel_crtc->config->has_pch_encoder)
4968 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4969
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004970 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004971
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004972 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004973
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004974 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004975 ironlake_fdi_disable(crtc);
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004976 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4977 }
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004978
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004979 for_each_encoder_on_crtc(dev, crtc, encoder)
4980 if (encoder->post_disable)
4981 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004982
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004983 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004984 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004985
Daniel Vetterd925c592013-06-05 13:34:04 +02004986 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004987 i915_reg_t reg;
4988 u32 temp;
4989
Daniel Vetterd925c592013-06-05 13:34:04 +02004990 /* disable TRANS_DP_CTL */
4991 reg = TRANS_DP_CTL(pipe);
4992 temp = I915_READ(reg);
4993 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4994 TRANS_DP_PORT_SEL_MASK);
4995 temp |= TRANS_DP_PORT_SEL_NONE;
4996 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004997
Daniel Vetterd925c592013-06-05 13:34:04 +02004998 /* disable DPLL_SEL */
4999 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005000 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005001 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005002 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005003
Daniel Vetterd925c592013-06-05 13:34:04 +02005004 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005005 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005006
5007 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005008}
5009
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005010static void haswell_crtc_disable(struct drm_crtc *crtc)
5011{
5012 struct drm_device *dev = crtc->dev;
5013 struct drm_i915_private *dev_priv = dev->dev_private;
5014 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5015 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005016 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005017
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005018 if (intel_crtc->config->has_pch_encoder)
5019 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5020 false);
5021
Jani Nikula8807e552013-08-30 19:40:32 +03005022 for_each_encoder_on_crtc(dev, crtc, encoder) {
5023 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005024 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005025 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005026
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005027 drm_crtc_vblank_off(crtc);
5028 assert_vblank_disabled(crtc);
5029
Jani Nikula4d1de972016-03-18 17:05:42 +02005030 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5031 if (!intel_crtc->config->has_dsi_encoder)
5032 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005033
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005034 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005035 intel_ddi_set_vc_payload_alloc(crtc, false);
5036
Jani Nikulaa65347b2015-11-27 12:21:46 +02005037 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305038 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005039
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005040 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005041 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005042 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005043 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005044
Jani Nikulaa65347b2015-11-27 12:21:46 +02005045 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305046 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005047
Imre Deak97b040a2014-06-25 22:01:50 +03005048 for_each_encoder_on_crtc(dev, crtc, encoder)
5049 if (encoder->post_disable)
5050 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005051
Ville Syrjälä92966a32015-12-08 16:05:48 +02005052 if (intel_crtc->config->has_pch_encoder) {
5053 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005054 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005055 intel_ddi_fdi_disable(crtc);
5056
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005057 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5058 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005059 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005060}
5061
Jesse Barnes2dd24552013-04-25 12:55:01 -07005062static void i9xx_pfit_enable(struct intel_crtc *crtc)
5063{
5064 struct drm_device *dev = crtc->base.dev;
5065 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005066 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005067
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005068 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005069 return;
5070
Daniel Vetterc0b03412013-05-28 12:05:54 +02005071 /*
5072 * The panel fitter should only be adjusted whilst the pipe is disabled,
5073 * according to register description and PRM.
5074 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005075 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5076 assert_pipe_disabled(dev_priv, crtc->pipe);
5077
Jesse Barnesb074cec2013-04-25 12:55:02 -07005078 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5079 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005080
5081 /* Border color in case we don't scale up to the full screen. Black by
5082 * default, change to something else for debugging. */
5083 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005084}
5085
Dave Airlied05410f2014-06-05 13:22:59 +10005086static enum intel_display_power_domain port_to_power_domain(enum port port)
5087{
5088 switch (port) {
5089 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005090 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005091 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005092 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005093 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005094 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005095 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005096 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005097 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005098 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005099 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005100 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005101 return POWER_DOMAIN_PORT_OTHER;
5102 }
5103}
5104
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005105static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5106{
5107 switch (port) {
5108 case PORT_A:
5109 return POWER_DOMAIN_AUX_A;
5110 case PORT_B:
5111 return POWER_DOMAIN_AUX_B;
5112 case PORT_C:
5113 return POWER_DOMAIN_AUX_C;
5114 case PORT_D:
5115 return POWER_DOMAIN_AUX_D;
5116 case PORT_E:
5117 /* FIXME: Check VBT for actual wiring of PORT E */
5118 return POWER_DOMAIN_AUX_D;
5119 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005120 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005121 return POWER_DOMAIN_AUX_A;
5122 }
5123}
5124
Imre Deak319be8a2014-03-04 19:22:57 +02005125enum intel_display_power_domain
5126intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005127{
Imre Deak319be8a2014-03-04 19:22:57 +02005128 struct drm_device *dev = intel_encoder->base.dev;
5129 struct intel_digital_port *intel_dig_port;
5130
5131 switch (intel_encoder->type) {
5132 case INTEL_OUTPUT_UNKNOWN:
5133 /* Only DDI platforms should ever use this output type */
5134 WARN_ON_ONCE(!HAS_DDI(dev));
5135 case INTEL_OUTPUT_DISPLAYPORT:
5136 case INTEL_OUTPUT_HDMI:
5137 case INTEL_OUTPUT_EDP:
5138 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005139 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005140 case INTEL_OUTPUT_DP_MST:
5141 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5142 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005143 case INTEL_OUTPUT_ANALOG:
5144 return POWER_DOMAIN_PORT_CRT;
5145 case INTEL_OUTPUT_DSI:
5146 return POWER_DOMAIN_PORT_DSI;
5147 default:
5148 return POWER_DOMAIN_PORT_OTHER;
5149 }
5150}
5151
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005152enum intel_display_power_domain
5153intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5154{
5155 struct drm_device *dev = intel_encoder->base.dev;
5156 struct intel_digital_port *intel_dig_port;
5157
5158 switch (intel_encoder->type) {
5159 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005160 case INTEL_OUTPUT_HDMI:
5161 /*
5162 * Only DDI platforms should ever use these output types.
5163 * We can get here after the HDMI detect code has already set
5164 * the type of the shared encoder. Since we can't be sure
5165 * what's the status of the given connectors, play safe and
5166 * run the DP detection too.
5167 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005168 WARN_ON_ONCE(!HAS_DDI(dev));
5169 case INTEL_OUTPUT_DISPLAYPORT:
5170 case INTEL_OUTPUT_EDP:
5171 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5172 return port_to_aux_power_domain(intel_dig_port->port);
5173 case INTEL_OUTPUT_DP_MST:
5174 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5175 return port_to_aux_power_domain(intel_dig_port->port);
5176 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005177 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005178 return POWER_DOMAIN_AUX_A;
5179 }
5180}
5181
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005182static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5183 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005184{
5185 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005186 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005187 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5188 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005189 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005190 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005191
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005192 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005193 return 0;
5194
Imre Deak77d22dc2014-03-05 16:20:52 +02005195 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5196 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005197 if (crtc_state->pch_pfit.enabled ||
5198 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005199 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5200
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005201 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5202 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5203
Imre Deak319be8a2014-03-04 19:22:57 +02005204 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005205 }
Imre Deak319be8a2014-03-04 19:22:57 +02005206
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005207 if (crtc_state->shared_dpll)
5208 mask |= BIT(POWER_DOMAIN_PLLS);
5209
Imre Deak77d22dc2014-03-05 16:20:52 +02005210 return mask;
5211}
5212
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005213static unsigned long
5214modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5215 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005216{
5217 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5218 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5219 enum intel_display_power_domain domain;
5220 unsigned long domains, new_domains, old_domains;
5221
5222 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005223 intel_crtc->enabled_power_domains = new_domains =
5224 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005225
5226 domains = new_domains & ~old_domains;
5227
5228 for_each_power_domain(domain, domains)
5229 intel_display_power_get(dev_priv, domain);
5230
5231 return old_domains & ~new_domains;
5232}
5233
5234static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5235 unsigned long domains)
5236{
5237 enum intel_display_power_domain domain;
5238
5239 for_each_power_domain(domain, domains)
5240 intel_display_power_put(dev_priv, domain);
5241}
5242
Mika Kaholaadafdc62015-08-18 14:36:59 +03005243static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5244{
5245 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5246
5247 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5248 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5249 return max_cdclk_freq;
5250 else if (IS_CHERRYVIEW(dev_priv))
5251 return max_cdclk_freq*95/100;
5252 else if (INTEL_INFO(dev_priv)->gen < 4)
5253 return 2*max_cdclk_freq*90/100;
5254 else
5255 return max_cdclk_freq*90/100;
5256}
5257
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005258static void intel_update_max_cdclk(struct drm_device *dev)
5259{
5260 struct drm_i915_private *dev_priv = dev->dev_private;
5261
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005262 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005263 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5264
5265 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5266 dev_priv->max_cdclk_freq = 675000;
5267 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5268 dev_priv->max_cdclk_freq = 540000;
5269 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5270 dev_priv->max_cdclk_freq = 450000;
5271 else
5272 dev_priv->max_cdclk_freq = 337500;
5273 } else if (IS_BROADWELL(dev)) {
5274 /*
5275 * FIXME with extra cooling we can allow
5276 * 540 MHz for ULX and 675 Mhz for ULT.
5277 * How can we know if extra cooling is
5278 * available? PCI ID, VTB, something else?
5279 */
5280 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5281 dev_priv->max_cdclk_freq = 450000;
5282 else if (IS_BDW_ULX(dev))
5283 dev_priv->max_cdclk_freq = 450000;
5284 else if (IS_BDW_ULT(dev))
5285 dev_priv->max_cdclk_freq = 540000;
5286 else
5287 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005288 } else if (IS_CHERRYVIEW(dev)) {
5289 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005290 } else if (IS_VALLEYVIEW(dev)) {
5291 dev_priv->max_cdclk_freq = 400000;
5292 } else {
5293 /* otherwise assume cdclk is fixed */
5294 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5295 }
5296
Mika Kaholaadafdc62015-08-18 14:36:59 +03005297 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5298
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005299 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5300 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005301
5302 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5303 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005304}
5305
5306static void intel_update_cdclk(struct drm_device *dev)
5307{
5308 struct drm_i915_private *dev_priv = dev->dev_private;
5309
5310 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5311 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5312 dev_priv->cdclk_freq);
5313
5314 /*
5315 * Program the gmbus_freq based on the cdclk frequency.
5316 * BSpec erroneously claims we should aim for 4MHz, but
5317 * in fact 1MHz is the correct frequency.
5318 */
Wayne Boyer666a4532015-12-09 12:29:35 -08005319 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005320 /*
5321 * Program the gmbus_freq based on the cdclk frequency.
5322 * BSpec erroneously claims we should aim for 4MHz, but
5323 * in fact 1MHz is the correct frequency.
5324 */
5325 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5326 }
5327
5328 if (dev_priv->max_cdclk_freq == 0)
5329 intel_update_max_cdclk(dev);
5330}
5331
Damien Lespiau70d0c572015-06-04 18:21:29 +01005332static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305333{
5334 struct drm_i915_private *dev_priv = dev->dev_private;
5335 uint32_t divider;
5336 uint32_t ratio;
5337 uint32_t current_freq;
5338 int ret;
5339
5340 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5341 switch (frequency) {
5342 case 144000:
5343 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5344 ratio = BXT_DE_PLL_RATIO(60);
5345 break;
5346 case 288000:
5347 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5348 ratio = BXT_DE_PLL_RATIO(60);
5349 break;
5350 case 384000:
5351 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5352 ratio = BXT_DE_PLL_RATIO(60);
5353 break;
5354 case 576000:
5355 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5356 ratio = BXT_DE_PLL_RATIO(60);
5357 break;
5358 case 624000:
5359 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5360 ratio = BXT_DE_PLL_RATIO(65);
5361 break;
5362 case 19200:
5363 /*
5364 * Bypass frequency with DE PLL disabled. Init ratio, divider
5365 * to suppress GCC warning.
5366 */
5367 ratio = 0;
5368 divider = 0;
5369 break;
5370 default:
5371 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5372
5373 return;
5374 }
5375
5376 mutex_lock(&dev_priv->rps.hw_lock);
5377 /* Inform power controller of upcoming frequency change */
5378 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5379 0x80000000);
5380 mutex_unlock(&dev_priv->rps.hw_lock);
5381
5382 if (ret) {
5383 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5384 ret, frequency);
5385 return;
5386 }
5387
5388 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5389 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5390 current_freq = current_freq * 500 + 1000;
5391
5392 /*
5393 * DE PLL has to be disabled when
5394 * - setting to 19.2MHz (bypass, PLL isn't used)
5395 * - before setting to 624MHz (PLL needs toggling)
5396 * - before setting to any frequency from 624MHz (PLL needs toggling)
5397 */
5398 if (frequency == 19200 || frequency == 624000 ||
5399 current_freq == 624000) {
5400 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5401 /* Timeout 200us */
5402 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5403 1))
5404 DRM_ERROR("timout waiting for DE PLL unlock\n");
5405 }
5406
5407 if (frequency != 19200) {
5408 uint32_t val;
5409
5410 val = I915_READ(BXT_DE_PLL_CTL);
5411 val &= ~BXT_DE_PLL_RATIO_MASK;
5412 val |= ratio;
5413 I915_WRITE(BXT_DE_PLL_CTL, val);
5414
5415 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5416 /* Timeout 200us */
5417 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5418 DRM_ERROR("timeout waiting for DE PLL lock\n");
5419
5420 val = I915_READ(CDCLK_CTL);
5421 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5422 val |= divider;
5423 /*
5424 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5425 * enable otherwise.
5426 */
5427 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5428 if (frequency >= 500000)
5429 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5430
5431 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5432 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5433 val |= (frequency - 1000) / 500;
5434 I915_WRITE(CDCLK_CTL, val);
5435 }
5436
5437 mutex_lock(&dev_priv->rps.hw_lock);
5438 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5439 DIV_ROUND_UP(frequency, 25000));
5440 mutex_unlock(&dev_priv->rps.hw_lock);
5441
5442 if (ret) {
5443 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5444 ret, frequency);
5445 return;
5446 }
5447
Damien Lespiaua47871b2015-06-04 18:21:34 +01005448 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305449}
5450
5451void broxton_init_cdclk(struct drm_device *dev)
5452{
5453 struct drm_i915_private *dev_priv = dev->dev_private;
5454 uint32_t val;
5455
5456 /*
5457 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5458 * or else the reset will hang because there is no PCH to respond.
5459 * Move the handshake programming to initialization sequence.
5460 * Previously was left up to BIOS.
5461 */
5462 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5463 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5464 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5465
5466 /* Enable PG1 for cdclk */
5467 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5468
5469 /* check if cd clock is enabled */
5470 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5471 DRM_DEBUG_KMS("Display already initialized\n");
5472 return;
5473 }
5474
5475 /*
5476 * FIXME:
5477 * - The initial CDCLK needs to be read from VBT.
5478 * Need to make this change after VBT has changes for BXT.
5479 * - check if setting the max (or any) cdclk freq is really necessary
5480 * here, it belongs to modeset time
5481 */
5482 broxton_set_cdclk(dev, 624000);
5483
5484 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005485 POSTING_READ(DBUF_CTL);
5486
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305487 udelay(10);
5488
5489 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5490 DRM_ERROR("DBuf power enable timeout!\n");
5491}
5492
5493void broxton_uninit_cdclk(struct drm_device *dev)
5494{
5495 struct drm_i915_private *dev_priv = dev->dev_private;
5496
5497 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005498 POSTING_READ(DBUF_CTL);
5499
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305500 udelay(10);
5501
5502 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5503 DRM_ERROR("DBuf power disable timeout!\n");
5504
5505 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5506 broxton_set_cdclk(dev, 19200);
5507
5508 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5509}
5510
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005511static const struct skl_cdclk_entry {
5512 unsigned int freq;
5513 unsigned int vco;
5514} skl_cdclk_frequencies[] = {
5515 { .freq = 308570, .vco = 8640 },
5516 { .freq = 337500, .vco = 8100 },
5517 { .freq = 432000, .vco = 8640 },
5518 { .freq = 450000, .vco = 8100 },
5519 { .freq = 540000, .vco = 8100 },
5520 { .freq = 617140, .vco = 8640 },
5521 { .freq = 675000, .vco = 8100 },
5522};
5523
5524static unsigned int skl_cdclk_decimal(unsigned int freq)
5525{
5526 return (freq - 1000) / 500;
5527}
5528
5529static unsigned int skl_cdclk_get_vco(unsigned int freq)
5530{
5531 unsigned int i;
5532
5533 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5534 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5535
5536 if (e->freq == freq)
5537 return e->vco;
5538 }
5539
5540 return 8100;
5541}
5542
5543static void
5544skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5545{
5546 unsigned int min_freq;
5547 u32 val;
5548
5549 /* select the minimum CDCLK before enabling DPLL 0 */
5550 val = I915_READ(CDCLK_CTL);
5551 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5552 val |= CDCLK_FREQ_337_308;
5553
5554 if (required_vco == 8640)
5555 min_freq = 308570;
5556 else
5557 min_freq = 337500;
5558
5559 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5560
5561 I915_WRITE(CDCLK_CTL, val);
5562 POSTING_READ(CDCLK_CTL);
5563
5564 /*
5565 * We always enable DPLL0 with the lowest link rate possible, but still
5566 * taking into account the VCO required to operate the eDP panel at the
5567 * desired frequency. The usual DP link rates operate with a VCO of
5568 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5569 * The modeset code is responsible for the selection of the exact link
5570 * rate later on, with the constraint of choosing a frequency that
5571 * works with required_vco.
5572 */
5573 val = I915_READ(DPLL_CTRL1);
5574
5575 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5576 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5577 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5578 if (required_vco == 8640)
5579 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5580 SKL_DPLL0);
5581 else
5582 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5583 SKL_DPLL0);
5584
5585 I915_WRITE(DPLL_CTRL1, val);
5586 POSTING_READ(DPLL_CTRL1);
5587
5588 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5589
5590 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5591 DRM_ERROR("DPLL0 not locked\n");
5592}
5593
5594static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5595{
5596 int ret;
5597 u32 val;
5598
5599 /* inform PCU we want to change CDCLK */
5600 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5601 mutex_lock(&dev_priv->rps.hw_lock);
5602 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5603 mutex_unlock(&dev_priv->rps.hw_lock);
5604
5605 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5606}
5607
5608static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5609{
5610 unsigned int i;
5611
5612 for (i = 0; i < 15; i++) {
5613 if (skl_cdclk_pcu_ready(dev_priv))
5614 return true;
5615 udelay(10);
5616 }
5617
5618 return false;
5619}
5620
5621static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5622{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005623 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005624 u32 freq_select, pcu_ack;
5625
5626 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5627
5628 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5629 DRM_ERROR("failed to inform PCU about cdclk change\n");
5630 return;
5631 }
5632
5633 /* set CDCLK_CTL */
5634 switch(freq) {
5635 case 450000:
5636 case 432000:
5637 freq_select = CDCLK_FREQ_450_432;
5638 pcu_ack = 1;
5639 break;
5640 case 540000:
5641 freq_select = CDCLK_FREQ_540;
5642 pcu_ack = 2;
5643 break;
5644 case 308570:
5645 case 337500:
5646 default:
5647 freq_select = CDCLK_FREQ_337_308;
5648 pcu_ack = 0;
5649 break;
5650 case 617140:
5651 case 675000:
5652 freq_select = CDCLK_FREQ_675_617;
5653 pcu_ack = 3;
5654 break;
5655 }
5656
5657 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5658 POSTING_READ(CDCLK_CTL);
5659
5660 /* inform PCU of the change */
5661 mutex_lock(&dev_priv->rps.hw_lock);
5662 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5663 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005664
5665 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005666}
5667
5668void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5669{
5670 /* disable DBUF power */
5671 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5672 POSTING_READ(DBUF_CTL);
5673
5674 udelay(10);
5675
5676 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5677 DRM_ERROR("DBuf power disable timeout\n");
5678
Imre Deakab96c1ee2015-11-04 19:24:18 +02005679 /* disable DPLL0 */
5680 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5681 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5682 DRM_ERROR("Couldn't disable DPLL0\n");
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005683}
5684
5685void skl_init_cdclk(struct drm_i915_private *dev_priv)
5686{
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005687 unsigned int required_vco;
5688
Gary Wang39d9b852015-08-28 16:40:34 +08005689 /* DPLL0 not enabled (happens on early BIOS versions) */
5690 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5691 /* enable DPLL0 */
5692 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5693 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005694 }
5695
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005696 /* set CDCLK to the frequency the BIOS chose */
5697 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5698
5699 /* enable DBUF power */
5700 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5701 POSTING_READ(DBUF_CTL);
5702
5703 udelay(10);
5704
5705 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5706 DRM_ERROR("DBuf power enable timeout\n");
5707}
5708
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305709int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5710{
5711 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5712 uint32_t cdctl = I915_READ(CDCLK_CTL);
5713 int freq = dev_priv->skl_boot_cdclk;
5714
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305715 /*
5716 * check if the pre-os intialized the display
5717 * There is SWF18 scratchpad register defined which is set by the
5718 * pre-os which can be used by the OS drivers to check the status
5719 */
5720 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5721 goto sanitize;
5722
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305723 /* Is PLL enabled and locked ? */
5724 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5725 goto sanitize;
5726
5727 /* DPLL okay; verify the cdclock
5728 *
5729 * Noticed in some instances that the freq selection is correct but
5730 * decimal part is programmed wrong from BIOS where pre-os does not
5731 * enable display. Verify the same as well.
5732 */
5733 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5734 /* All well; nothing to sanitize */
5735 return false;
5736sanitize:
5737 /*
5738 * As of now initialize with max cdclk till
5739 * we get dynamic cdclk support
5740 * */
5741 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5742 skl_init_cdclk(dev_priv);
5743
5744 /* we did have to sanitize */
5745 return true;
5746}
5747
Jesse Barnes30a970c2013-11-04 13:48:12 -08005748/* Adjust CDclk dividers to allow high res or save power if possible */
5749static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5750{
5751 struct drm_i915_private *dev_priv = dev->dev_private;
5752 u32 val, cmd;
5753
Vandana Kannan164dfd22014-11-24 13:37:41 +05305754 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5755 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005756
Ville Syrjälädfcab172014-06-13 13:37:47 +03005757 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005758 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005759 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005760 cmd = 1;
5761 else
5762 cmd = 0;
5763
5764 mutex_lock(&dev_priv->rps.hw_lock);
5765 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5766 val &= ~DSPFREQGUAR_MASK;
5767 val |= (cmd << DSPFREQGUAR_SHIFT);
5768 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5769 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5770 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5771 50)) {
5772 DRM_ERROR("timed out waiting for CDclk change\n");
5773 }
5774 mutex_unlock(&dev_priv->rps.hw_lock);
5775
Ville Syrjälä54433e92015-05-26 20:42:31 +03005776 mutex_lock(&dev_priv->sb_lock);
5777
Ville Syrjälädfcab172014-06-13 13:37:47 +03005778 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005779 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005780
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005781 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005782
Jesse Barnes30a970c2013-11-04 13:48:12 -08005783 /* adjust cdclk divider */
5784 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005785 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005786 val |= divider;
5787 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005788
5789 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005790 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005791 50))
5792 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005793 }
5794
Jesse Barnes30a970c2013-11-04 13:48:12 -08005795 /* adjust self-refresh exit latency value */
5796 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5797 val &= ~0x7f;
5798
5799 /*
5800 * For high bandwidth configs, we set a higher latency in the bunit
5801 * so that the core display fetch happens in time to avoid underruns.
5802 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005803 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005804 val |= 4500 / 250; /* 4.5 usec */
5805 else
5806 val |= 3000 / 250; /* 3.0 usec */
5807 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005808
Ville Syrjäläa5805162015-05-26 20:42:30 +03005809 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005810
Ville Syrjäläb6283052015-06-03 15:45:07 +03005811 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005812}
5813
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005814static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5815{
5816 struct drm_i915_private *dev_priv = dev->dev_private;
5817 u32 val, cmd;
5818
Vandana Kannan164dfd22014-11-24 13:37:41 +05305819 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5820 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005821
5822 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005823 case 333333:
5824 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005825 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005826 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005827 break;
5828 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005829 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005830 return;
5831 }
5832
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005833 /*
5834 * Specs are full of misinformation, but testing on actual
5835 * hardware has shown that we just need to write the desired
5836 * CCK divider into the Punit register.
5837 */
5838 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5839
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005840 mutex_lock(&dev_priv->rps.hw_lock);
5841 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5842 val &= ~DSPFREQGUAR_MASK_CHV;
5843 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5844 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5845 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5846 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5847 50)) {
5848 DRM_ERROR("timed out waiting for CDclk change\n");
5849 }
5850 mutex_unlock(&dev_priv->rps.hw_lock);
5851
Ville Syrjäläb6283052015-06-03 15:45:07 +03005852 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005853}
5854
Jesse Barnes30a970c2013-11-04 13:48:12 -08005855static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5856 int max_pixclk)
5857{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005858 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005859 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005860
Jesse Barnes30a970c2013-11-04 13:48:12 -08005861 /*
5862 * Really only a few cases to deal with, as only 4 CDclks are supported:
5863 * 200MHz
5864 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005865 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005866 * 400MHz (VLV only)
5867 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5868 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005869 *
5870 * We seem to get an unstable or solid color picture at 200MHz.
5871 * Not sure what's wrong. For now use 200MHz only when all pipes
5872 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005873 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005874 if (!IS_CHERRYVIEW(dev_priv) &&
5875 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005876 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005877 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005878 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005879 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005880 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005881 else
5882 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005883}
5884
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305885static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5886 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005887{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305888 /*
5889 * FIXME:
5890 * - remove the guardband, it's not needed on BXT
5891 * - set 19.2MHz bypass frequency if there are no active pipes
5892 */
5893 if (max_pixclk > 576000*9/10)
5894 return 624000;
5895 else if (max_pixclk > 384000*9/10)
5896 return 576000;
5897 else if (max_pixclk > 288000*9/10)
5898 return 384000;
5899 else if (max_pixclk > 144000*9/10)
5900 return 288000;
5901 else
5902 return 144000;
5903}
5904
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005905/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005906static int intel_mode_max_pixclk(struct drm_device *dev,
5907 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005908{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005909 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5910 struct drm_i915_private *dev_priv = dev->dev_private;
5911 struct drm_crtc *crtc;
5912 struct drm_crtc_state *crtc_state;
5913 unsigned max_pixclk = 0, i;
5914 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005915
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005916 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5917 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005918
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005919 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5920 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005921
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005922 if (crtc_state->enable)
5923 pixclk = crtc_state->adjusted_mode.crtc_clock;
5924
5925 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005926 }
5927
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005928 for_each_pipe(dev_priv, pipe)
5929 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5930
Jesse Barnes30a970c2013-11-04 13:48:12 -08005931 return max_pixclk;
5932}
5933
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005934static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005935{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005936 struct drm_device *dev = state->dev;
5937 struct drm_i915_private *dev_priv = dev->dev_private;
5938 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005939 struct intel_atomic_state *intel_state =
5940 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005941
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005942 if (max_pixclk < 0)
5943 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005944
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005945 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005946 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305947
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005948 if (!intel_state->active_crtcs)
5949 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5950
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005951 return 0;
5952}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005953
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005954static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5955{
5956 struct drm_device *dev = state->dev;
5957 struct drm_i915_private *dev_priv = dev->dev_private;
5958 int max_pixclk = intel_mode_max_pixclk(dev, 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 Lankhorst27c329e2015-06-15 12:33:56 +02005962 if (max_pixclk < 0)
5963 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005964
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005965 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005966 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005967
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005968 if (!intel_state->active_crtcs)
5969 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
5970
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005971 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005972}
5973
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005974static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5975{
5976 unsigned int credits, default_credits;
5977
5978 if (IS_CHERRYVIEW(dev_priv))
5979 default_credits = PFI_CREDIT(12);
5980 else
5981 default_credits = PFI_CREDIT(8);
5982
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005983 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005984 /* CHV suggested value is 31 or 63 */
5985 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005986 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005987 else
5988 credits = PFI_CREDIT(15);
5989 } else {
5990 credits = default_credits;
5991 }
5992
5993 /*
5994 * WA - write default credits before re-programming
5995 * FIXME: should we also set the resend bit here?
5996 */
5997 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5998 default_credits);
5999
6000 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6001 credits | PFI_CREDIT_RESEND);
6002
6003 /*
6004 * FIXME is this guaranteed to clear
6005 * immediately or should we poll for it?
6006 */
6007 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6008}
6009
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006010static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006011{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006012 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006013 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006014 struct intel_atomic_state *old_intel_state =
6015 to_intel_atomic_state(old_state);
6016 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006017
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006018 /*
6019 * FIXME: We can end up here with all power domains off, yet
6020 * with a CDCLK frequency other than the minimum. To account
6021 * for this take the PIPE-A power domain, which covers the HW
6022 * blocks needed for the following programming. This can be
6023 * removed once it's guaranteed that we get here either with
6024 * the minimum CDCLK set, or the required power domains
6025 * enabled.
6026 */
6027 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006028
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006029 if (IS_CHERRYVIEW(dev))
6030 cherryview_set_cdclk(dev, req_cdclk);
6031 else
6032 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006033
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006034 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006035
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006036 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006037}
6038
Jesse Barnes89b667f2013-04-18 14:51:36 -07006039static void valleyview_crtc_enable(struct drm_crtc *crtc)
6040{
6041 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006042 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006043 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6044 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006045 struct intel_crtc_state *pipe_config =
6046 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006047 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006048
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006049 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006050 return;
6051
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006052 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306053 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006054
6055 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006056 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006057
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006058 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6059 struct drm_i915_private *dev_priv = dev->dev_private;
6060
6061 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6062 I915_WRITE(CHV_CANVAS(pipe), 0);
6063 }
6064
Daniel Vetter5b18e572014-04-24 23:55:06 +02006065 i9xx_set_pipeconf(intel_crtc);
6066
Jesse Barnes89b667f2013-04-18 14:51:36 -07006067 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006068
Daniel Vettera72e4c92014-09-30 10:56:47 +02006069 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006070
Jesse Barnes89b667f2013-04-18 14:51:36 -07006071 for_each_encoder_on_crtc(dev, crtc, encoder)
6072 if (encoder->pre_pll_enable)
6073 encoder->pre_pll_enable(encoder);
6074
Jani Nikulaa65347b2015-11-27 12:21:46 +02006075 if (!intel_crtc->config->has_dsi_encoder) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006076 if (IS_CHERRYVIEW(dev)) {
6077 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006078 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006079 } else {
6080 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006081 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006082 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006083 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006084
6085 for_each_encoder_on_crtc(dev, crtc, encoder)
6086 if (encoder->pre_enable)
6087 encoder->pre_enable(encoder);
6088
Jesse Barnes2dd24552013-04-25 12:55:01 -07006089 i9xx_pfit_enable(intel_crtc);
6090
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006091 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006092
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006093 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006094 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006095
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006096 assert_vblank_disabled(crtc);
6097 drm_crtc_vblank_on(crtc);
6098
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006099 for_each_encoder_on_crtc(dev, crtc, encoder)
6100 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006101}
6102
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006103static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6104{
6105 struct drm_device *dev = crtc->base.dev;
6106 struct drm_i915_private *dev_priv = dev->dev_private;
6107
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006108 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6109 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006110}
6111
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006112static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006113{
6114 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006115 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006116 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006117 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006118 struct intel_crtc_state *pipe_config =
6119 to_intel_crtc_state(crtc->state);
Jesse Barnes79e53942008-11-07 14:24:08 -08006120 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006121
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006122 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006123 return;
6124
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006125 i9xx_set_pll_dividers(intel_crtc);
6126
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006127 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306128 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006129
6130 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006131 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006132
Daniel Vetter5b18e572014-04-24 23:55:06 +02006133 i9xx_set_pipeconf(intel_crtc);
6134
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006135 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006136
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006137 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006138 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006139
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006140 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006141 if (encoder->pre_enable)
6142 encoder->pre_enable(encoder);
6143
Daniel Vetterf6736a12013-06-05 13:34:30 +02006144 i9xx_enable_pll(intel_crtc);
6145
Jesse Barnes2dd24552013-04-25 12:55:01 -07006146 i9xx_pfit_enable(intel_crtc);
6147
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006148 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006149
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006150 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006151 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006152
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006153 assert_vblank_disabled(crtc);
6154 drm_crtc_vblank_on(crtc);
6155
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006156 for_each_encoder_on_crtc(dev, crtc, encoder)
6157 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006158}
6159
Daniel Vetter87476d62013-04-11 16:29:06 +02006160static void i9xx_pfit_disable(struct intel_crtc *crtc)
6161{
6162 struct drm_device *dev = crtc->base.dev;
6163 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006164
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006165 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006166 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006167
6168 assert_pipe_disabled(dev_priv, crtc->pipe);
6169
Daniel Vetter328d8e82013-05-08 10:36:31 +02006170 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6171 I915_READ(PFIT_CONTROL));
6172 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006173}
6174
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006175static void i9xx_crtc_disable(struct drm_crtc *crtc)
6176{
6177 struct drm_device *dev = crtc->dev;
6178 struct drm_i915_private *dev_priv = dev->dev_private;
6179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006180 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006181 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006182
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006183 /*
6184 * On gen2 planes are double buffered but the pipe isn't, so we must
6185 * wait for planes to fully turn off before disabling the pipe.
6186 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006187 if (IS_GEN2(dev))
6188 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006189
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006190 for_each_encoder_on_crtc(dev, crtc, encoder)
6191 encoder->disable(encoder);
6192
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006193 drm_crtc_vblank_off(crtc);
6194 assert_vblank_disabled(crtc);
6195
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006196 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006197
Daniel Vetter87476d62013-04-11 16:29:06 +02006198 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006199
Jesse Barnes89b667f2013-04-18 14:51:36 -07006200 for_each_encoder_on_crtc(dev, crtc, encoder)
6201 if (encoder->post_disable)
6202 encoder->post_disable(encoder);
6203
Jani Nikulaa65347b2015-11-27 12:21:46 +02006204 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006205 if (IS_CHERRYVIEW(dev))
6206 chv_disable_pll(dev_priv, pipe);
6207 else if (IS_VALLEYVIEW(dev))
6208 vlv_disable_pll(dev_priv, pipe);
6209 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006210 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006211 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006212
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006213 for_each_encoder_on_crtc(dev, crtc, encoder)
6214 if (encoder->post_pll_disable)
6215 encoder->post_pll_disable(encoder);
6216
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006217 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006218 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006219}
6220
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006221static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006222{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006223 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006224 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006225 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006226 enum intel_display_power_domain domain;
6227 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006228
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006229 if (!intel_crtc->active)
6230 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006231
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006232 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006233 WARN_ON(intel_crtc->unpin_work);
6234
Ville Syrjälä2622a082016-03-09 19:07:26 +02006235 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006236
6237 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6238 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006239 }
6240
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006241 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006242
6243 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6244 crtc->base.id);
6245
6246 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6247 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006248 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006249 crtc->enabled = false;
6250 crtc->state->connector_mask = 0;
6251 crtc->state->encoder_mask = 0;
6252
6253 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6254 encoder->base.crtc = NULL;
6255
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006256 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006257 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006258 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006259
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006260 domains = intel_crtc->enabled_power_domains;
6261 for_each_power_domain(domain, domains)
6262 intel_display_power_put(dev_priv, domain);
6263 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006264
6265 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6266 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006267}
6268
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006269/*
6270 * turn all crtc's off, but do not adjust state
6271 * This has to be paired with a call to intel_modeset_setup_hw_state.
6272 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006273int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006274{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006275 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006276 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006277 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006278
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006279 state = drm_atomic_helper_suspend(dev);
6280 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006281 if (ret)
6282 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006283 else
6284 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006285 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006286}
6287
Chris Wilsonea5b2132010-08-04 13:50:23 +01006288void intel_encoder_destroy(struct drm_encoder *encoder)
6289{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006290 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006291
Chris Wilsonea5b2132010-08-04 13:50:23 +01006292 drm_encoder_cleanup(encoder);
6293 kfree(intel_encoder);
6294}
6295
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006296/* Cross check the actual hw state with our own modeset state tracking (and it's
6297 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006298static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006299{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006300 struct drm_crtc *crtc = connector->base.state->crtc;
6301
6302 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6303 connector->base.base.id,
6304 connector->base.name);
6305
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006306 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006307 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006308 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006309
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006310 I915_STATE_WARN(!crtc,
6311 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006312
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006313 if (!crtc)
6314 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006315
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006316 I915_STATE_WARN(!crtc->state->active,
6317 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006318
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006319 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006320 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006321
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006322 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006323 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006324
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006325 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006326 "attached encoder crtc differs from connector crtc\n");
6327 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006328 I915_STATE_WARN(crtc && crtc->state->active,
6329 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006330 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6331 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006332 }
6333}
6334
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006335int intel_connector_init(struct intel_connector *connector)
6336{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006337 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006338
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006339 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006340 return -ENOMEM;
6341
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006342 return 0;
6343}
6344
6345struct intel_connector *intel_connector_alloc(void)
6346{
6347 struct intel_connector *connector;
6348
6349 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6350 if (!connector)
6351 return NULL;
6352
6353 if (intel_connector_init(connector) < 0) {
6354 kfree(connector);
6355 return NULL;
6356 }
6357
6358 return connector;
6359}
6360
Daniel Vetterf0947c32012-07-02 13:10:34 +02006361/* Simple connector->get_hw_state implementation for encoders that support only
6362 * one connector and no cloning and hence the encoder state determines the state
6363 * of the connector. */
6364bool intel_connector_get_hw_state(struct intel_connector *connector)
6365{
Daniel Vetter24929352012-07-02 20:28:59 +02006366 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006367 struct intel_encoder *encoder = connector->encoder;
6368
6369 return encoder->get_hw_state(encoder, &pipe);
6370}
6371
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006372static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006373{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006374 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6375 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006376
6377 return 0;
6378}
6379
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006380static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006381 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006382{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006383 struct drm_atomic_state *state = pipe_config->base.state;
6384 struct intel_crtc *other_crtc;
6385 struct intel_crtc_state *other_crtc_state;
6386
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006387 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6388 pipe_name(pipe), pipe_config->fdi_lanes);
6389 if (pipe_config->fdi_lanes > 4) {
6390 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6391 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006392 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006393 }
6394
Paulo Zanonibafb6552013-11-02 21:07:44 -07006395 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006396 if (pipe_config->fdi_lanes > 2) {
6397 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6398 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006399 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006400 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006401 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006402 }
6403 }
6404
6405 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006406 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006407
6408 /* Ivybridge 3 pipe is really complicated */
6409 switch (pipe) {
6410 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006411 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006412 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006413 if (pipe_config->fdi_lanes <= 2)
6414 return 0;
6415
6416 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6417 other_crtc_state =
6418 intel_atomic_get_crtc_state(state, other_crtc);
6419 if (IS_ERR(other_crtc_state))
6420 return PTR_ERR(other_crtc_state);
6421
6422 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006423 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6424 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006425 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006426 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006427 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006428 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006429 if (pipe_config->fdi_lanes > 2) {
6430 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6431 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006432 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006433 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006434
6435 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6436 other_crtc_state =
6437 intel_atomic_get_crtc_state(state, other_crtc);
6438 if (IS_ERR(other_crtc_state))
6439 return PTR_ERR(other_crtc_state);
6440
6441 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006442 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006443 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006444 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006445 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006446 default:
6447 BUG();
6448 }
6449}
6450
Daniel Vettere29c22c2013-02-21 00:00:16 +01006451#define RETRY 1
6452static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006453 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006454{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006455 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006456 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006457 int lane, link_bw, fdi_dotclock, ret;
6458 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006459
Daniel Vettere29c22c2013-02-21 00:00:16 +01006460retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006461 /* FDI is a binary signal running at ~2.7GHz, encoding
6462 * each output octet as 10 bits. The actual frequency
6463 * is stored as a divider into a 100MHz clock, and the
6464 * mode pixel clock is stored in units of 1KHz.
6465 * Hence the bw of each lane in terms of the mode signal
6466 * is:
6467 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006468 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006469
Damien Lespiau241bfc32013-09-25 16:45:37 +01006470 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006471
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006472 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006473 pipe_config->pipe_bpp);
6474
6475 pipe_config->fdi_lanes = lane;
6476
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006477 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006478 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006479
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006480 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006481 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006482 pipe_config->pipe_bpp -= 2*3;
6483 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6484 pipe_config->pipe_bpp);
6485 needs_recompute = true;
6486 pipe_config->bw_constrained = true;
6487
6488 goto retry;
6489 }
6490
6491 if (needs_recompute)
6492 return RETRY;
6493
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006494 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006495}
6496
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006497static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6498 struct intel_crtc_state *pipe_config)
6499{
6500 if (pipe_config->pipe_bpp > 24)
6501 return false;
6502
6503 /* HSW can handle pixel rate up to cdclk? */
6504 if (IS_HASWELL(dev_priv->dev))
6505 return true;
6506
6507 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006508 * We compare against max which means we must take
6509 * the increased cdclk requirement into account when
6510 * calculating the new cdclk.
6511 *
6512 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006513 */
6514 return ilk_pipe_pixel_rate(pipe_config) <=
6515 dev_priv->max_cdclk_freq * 95 / 100;
6516}
6517
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006518static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006519 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006520{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006521 struct drm_device *dev = crtc->base.dev;
6522 struct drm_i915_private *dev_priv = dev->dev_private;
6523
Jani Nikulad330a952014-01-21 11:24:25 +02006524 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006525 hsw_crtc_supports_ips(crtc) &&
6526 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006527}
6528
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006529static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6530{
6531 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6532
6533 /* GDG double wide on either pipe, otherwise pipe A only */
6534 return INTEL_INFO(dev_priv)->gen < 4 &&
6535 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6536}
6537
Daniel Vettera43f6e02013-06-07 23:10:32 +02006538static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006539 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006540{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006541 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006542 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006543 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006544
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006545 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006546 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006547 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006548
6549 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006550 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006551 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006552 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006553 if (intel_crtc_supports_double_wide(crtc) &&
6554 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006555 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006556 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006557 }
6558
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006559 if (adjusted_mode->crtc_clock > clock_limit) {
6560 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6561 adjusted_mode->crtc_clock, clock_limit,
6562 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006563 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006564 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006565 }
Chris Wilson89749352010-09-12 18:25:19 +01006566
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006567 /*
6568 * Pipe horizontal size must be even in:
6569 * - DVO ganged mode
6570 * - LVDS dual channel mode
6571 * - Double wide pipe
6572 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006573 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006574 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6575 pipe_config->pipe_src_w &= ~1;
6576
Damien Lespiau8693a822013-05-03 18:48:11 +01006577 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6578 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006579 */
6580 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006581 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006582 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006583
Damien Lespiauf5adf942013-06-24 18:29:34 +01006584 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006585 hsw_compute_ips_config(crtc, pipe_config);
6586
Daniel Vetter877d48d2013-04-19 11:24:43 +02006587 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006588 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006589
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006590 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006591}
6592
Ville Syrjälä1652d192015-03-31 14:12:01 +03006593static int skylake_get_display_clock_speed(struct drm_device *dev)
6594{
6595 struct drm_i915_private *dev_priv = to_i915(dev);
6596 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6597 uint32_t cdctl = I915_READ(CDCLK_CTL);
6598 uint32_t linkrate;
6599
Damien Lespiau414355a2015-06-04 18:21:31 +01006600 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006601 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006602
6603 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6604 return 540000;
6605
6606 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006607 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006608
Damien Lespiau71cd8422015-04-30 16:39:17 +01006609 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6610 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006611 /* vco 8640 */
6612 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6613 case CDCLK_FREQ_450_432:
6614 return 432000;
6615 case CDCLK_FREQ_337_308:
6616 return 308570;
6617 case CDCLK_FREQ_675_617:
6618 return 617140;
6619 default:
6620 WARN(1, "Unknown cd freq selection\n");
6621 }
6622 } else {
6623 /* vco 8100 */
6624 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6625 case CDCLK_FREQ_450_432:
6626 return 450000;
6627 case CDCLK_FREQ_337_308:
6628 return 337500;
6629 case CDCLK_FREQ_675_617:
6630 return 675000;
6631 default:
6632 WARN(1, "Unknown cd freq selection\n");
6633 }
6634 }
6635
6636 /* error case, do as if DPLL0 isn't enabled */
6637 return 24000;
6638}
6639
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006640static int broxton_get_display_clock_speed(struct drm_device *dev)
6641{
6642 struct drm_i915_private *dev_priv = to_i915(dev);
6643 uint32_t cdctl = I915_READ(CDCLK_CTL);
6644 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6645 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6646 int cdclk;
6647
6648 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6649 return 19200;
6650
6651 cdclk = 19200 * pll_ratio / 2;
6652
6653 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6654 case BXT_CDCLK_CD2X_DIV_SEL_1:
6655 return cdclk; /* 576MHz or 624MHz */
6656 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6657 return cdclk * 2 / 3; /* 384MHz */
6658 case BXT_CDCLK_CD2X_DIV_SEL_2:
6659 return cdclk / 2; /* 288MHz */
6660 case BXT_CDCLK_CD2X_DIV_SEL_4:
6661 return cdclk / 4; /* 144MHz */
6662 }
6663
6664 /* error case, do as if DE PLL isn't enabled */
6665 return 19200;
6666}
6667
Ville Syrjälä1652d192015-03-31 14:12:01 +03006668static int broadwell_get_display_clock_speed(struct drm_device *dev)
6669{
6670 struct drm_i915_private *dev_priv = dev->dev_private;
6671 uint32_t lcpll = I915_READ(LCPLL_CTL);
6672 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6673
6674 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6675 return 800000;
6676 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6677 return 450000;
6678 else if (freq == LCPLL_CLK_FREQ_450)
6679 return 450000;
6680 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6681 return 540000;
6682 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6683 return 337500;
6684 else
6685 return 675000;
6686}
6687
6688static int haswell_get_display_clock_speed(struct drm_device *dev)
6689{
6690 struct drm_i915_private *dev_priv = dev->dev_private;
6691 uint32_t lcpll = I915_READ(LCPLL_CTL);
6692 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6693
6694 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6695 return 800000;
6696 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6697 return 450000;
6698 else if (freq == LCPLL_CLK_FREQ_450)
6699 return 450000;
6700 else if (IS_HSW_ULT(dev))
6701 return 337500;
6702 else
6703 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006704}
6705
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006706static int valleyview_get_display_clock_speed(struct drm_device *dev)
6707{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006708 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6709 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006710}
6711
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006712static int ilk_get_display_clock_speed(struct drm_device *dev)
6713{
6714 return 450000;
6715}
6716
Jesse Barnese70236a2009-09-21 10:42:27 -07006717static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006718{
Jesse Barnese70236a2009-09-21 10:42:27 -07006719 return 400000;
6720}
Jesse Barnes79e53942008-11-07 14:24:08 -08006721
Jesse Barnese70236a2009-09-21 10:42:27 -07006722static int i915_get_display_clock_speed(struct drm_device *dev)
6723{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006724 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006725}
Jesse Barnes79e53942008-11-07 14:24:08 -08006726
Jesse Barnese70236a2009-09-21 10:42:27 -07006727static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6728{
6729 return 200000;
6730}
Jesse Barnes79e53942008-11-07 14:24:08 -08006731
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006732static int pnv_get_display_clock_speed(struct drm_device *dev)
6733{
6734 u16 gcfgc = 0;
6735
6736 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6737
6738 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6739 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006740 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006741 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006742 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006743 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006744 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006745 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6746 return 200000;
6747 default:
6748 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6749 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006750 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006751 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006752 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006753 }
6754}
6755
Jesse Barnese70236a2009-09-21 10:42:27 -07006756static int i915gm_get_display_clock_speed(struct drm_device *dev)
6757{
6758 u16 gcfgc = 0;
6759
6760 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6761
6762 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006763 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006764 else {
6765 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6766 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006767 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006768 default:
6769 case GC_DISPLAY_CLOCK_190_200_MHZ:
6770 return 190000;
6771 }
6772 }
6773}
Jesse Barnes79e53942008-11-07 14:24:08 -08006774
Jesse Barnese70236a2009-09-21 10:42:27 -07006775static int i865_get_display_clock_speed(struct drm_device *dev)
6776{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006777 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006778}
6779
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006780static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006781{
6782 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006783
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006784 /*
6785 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6786 * encoding is different :(
6787 * FIXME is this the right way to detect 852GM/852GMV?
6788 */
6789 if (dev->pdev->revision == 0x1)
6790 return 133333;
6791
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006792 pci_bus_read_config_word(dev->pdev->bus,
6793 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6794
Jesse Barnese70236a2009-09-21 10:42:27 -07006795 /* Assume that the hardware is in the high speed state. This
6796 * should be the default.
6797 */
6798 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6799 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006800 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006801 case GC_CLOCK_100_200:
6802 return 200000;
6803 case GC_CLOCK_166_250:
6804 return 250000;
6805 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006806 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006807 case GC_CLOCK_133_266:
6808 case GC_CLOCK_133_266_2:
6809 case GC_CLOCK_166_266:
6810 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006811 }
6812
6813 /* Shouldn't happen */
6814 return 0;
6815}
6816
6817static int i830_get_display_clock_speed(struct drm_device *dev)
6818{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006819 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006820}
6821
Ville Syrjälä34edce22015-05-22 11:22:33 +03006822static unsigned int intel_hpll_vco(struct drm_device *dev)
6823{
6824 struct drm_i915_private *dev_priv = dev->dev_private;
6825 static const unsigned int blb_vco[8] = {
6826 [0] = 3200000,
6827 [1] = 4000000,
6828 [2] = 5333333,
6829 [3] = 4800000,
6830 [4] = 6400000,
6831 };
6832 static const unsigned int pnv_vco[8] = {
6833 [0] = 3200000,
6834 [1] = 4000000,
6835 [2] = 5333333,
6836 [3] = 4800000,
6837 [4] = 2666667,
6838 };
6839 static const unsigned int cl_vco[8] = {
6840 [0] = 3200000,
6841 [1] = 4000000,
6842 [2] = 5333333,
6843 [3] = 6400000,
6844 [4] = 3333333,
6845 [5] = 3566667,
6846 [6] = 4266667,
6847 };
6848 static const unsigned int elk_vco[8] = {
6849 [0] = 3200000,
6850 [1] = 4000000,
6851 [2] = 5333333,
6852 [3] = 4800000,
6853 };
6854 static const unsigned int ctg_vco[8] = {
6855 [0] = 3200000,
6856 [1] = 4000000,
6857 [2] = 5333333,
6858 [3] = 6400000,
6859 [4] = 2666667,
6860 [5] = 4266667,
6861 };
6862 const unsigned int *vco_table;
6863 unsigned int vco;
6864 uint8_t tmp = 0;
6865
6866 /* FIXME other chipsets? */
6867 if (IS_GM45(dev))
6868 vco_table = ctg_vco;
6869 else if (IS_G4X(dev))
6870 vco_table = elk_vco;
6871 else if (IS_CRESTLINE(dev))
6872 vco_table = cl_vco;
6873 else if (IS_PINEVIEW(dev))
6874 vco_table = pnv_vco;
6875 else if (IS_G33(dev))
6876 vco_table = blb_vco;
6877 else
6878 return 0;
6879
6880 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6881
6882 vco = vco_table[tmp & 0x7];
6883 if (vco == 0)
6884 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6885 else
6886 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6887
6888 return vco;
6889}
6890
6891static int gm45_get_display_clock_speed(struct drm_device *dev)
6892{
6893 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6894 uint16_t tmp = 0;
6895
6896 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6897
6898 cdclk_sel = (tmp >> 12) & 0x1;
6899
6900 switch (vco) {
6901 case 2666667:
6902 case 4000000:
6903 case 5333333:
6904 return cdclk_sel ? 333333 : 222222;
6905 case 3200000:
6906 return cdclk_sel ? 320000 : 228571;
6907 default:
6908 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6909 return 222222;
6910 }
6911}
6912
6913static int i965gm_get_display_clock_speed(struct drm_device *dev)
6914{
6915 static const uint8_t div_3200[] = { 16, 10, 8 };
6916 static const uint8_t div_4000[] = { 20, 12, 10 };
6917 static const uint8_t div_5333[] = { 24, 16, 14 };
6918 const uint8_t *div_table;
6919 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6920 uint16_t tmp = 0;
6921
6922 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6923
6924 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6925
6926 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6927 goto fail;
6928
6929 switch (vco) {
6930 case 3200000:
6931 div_table = div_3200;
6932 break;
6933 case 4000000:
6934 div_table = div_4000;
6935 break;
6936 case 5333333:
6937 div_table = div_5333;
6938 break;
6939 default:
6940 goto fail;
6941 }
6942
6943 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6944
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006945fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006946 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6947 return 200000;
6948}
6949
6950static int g33_get_display_clock_speed(struct drm_device *dev)
6951{
6952 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6953 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6954 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6955 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6956 const uint8_t *div_table;
6957 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6958 uint16_t tmp = 0;
6959
6960 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6961
6962 cdclk_sel = (tmp >> 4) & 0x7;
6963
6964 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6965 goto fail;
6966
6967 switch (vco) {
6968 case 3200000:
6969 div_table = div_3200;
6970 break;
6971 case 4000000:
6972 div_table = div_4000;
6973 break;
6974 case 4800000:
6975 div_table = div_4800;
6976 break;
6977 case 5333333:
6978 div_table = div_5333;
6979 break;
6980 default:
6981 goto fail;
6982 }
6983
6984 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6985
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006986fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006987 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6988 return 190476;
6989}
6990
Zhenyu Wang2c072452009-06-05 15:38:42 +08006991static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006992intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006993{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006994 while (*num > DATA_LINK_M_N_MASK ||
6995 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006996 *num >>= 1;
6997 *den >>= 1;
6998 }
6999}
7000
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007001static void compute_m_n(unsigned int m, unsigned int n,
7002 uint32_t *ret_m, uint32_t *ret_n)
7003{
7004 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7005 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7006 intel_reduce_m_n_ratio(ret_m, ret_n);
7007}
7008
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007009void
7010intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7011 int pixel_clock, int link_clock,
7012 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007013{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007014 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007015
7016 compute_m_n(bits_per_pixel * pixel_clock,
7017 link_clock * nlanes * 8,
7018 &m_n->gmch_m, &m_n->gmch_n);
7019
7020 compute_m_n(pixel_clock, link_clock,
7021 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007022}
7023
Chris Wilsona7615032011-01-12 17:04:08 +00007024static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7025{
Jani Nikulad330a952014-01-21 11:24:25 +02007026 if (i915.panel_use_ssc >= 0)
7027 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007028 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007029 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007030}
7031
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007032static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007033{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007034 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007035}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007036
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007037static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7038{
7039 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007040}
7041
Daniel Vetterf47709a2013-03-28 10:42:02 +01007042static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007043 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007044 intel_clock_t *reduced_clock)
7045{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007046 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007047 u32 fp, fp2 = 0;
7048
7049 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007050 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007051 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007052 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007053 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007054 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007055 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007056 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007057 }
7058
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007059 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007060
Daniel Vetterf47709a2013-03-28 10:42:02 +01007061 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007062 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007063 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007064 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007065 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007066 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007067 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007068 }
7069}
7070
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007071static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7072 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007073{
7074 u32 reg_val;
7075
7076 /*
7077 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7078 * and set it to a reasonable value instead.
7079 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007080 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007081 reg_val &= 0xffffff00;
7082 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007083 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), 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_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007086 reg_val &= 0x8cffffff;
7087 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007088 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007089
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007090 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007091 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007092 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007093
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007094 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007095 reg_val &= 0x00ffffff;
7096 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007097 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007098}
7099
Daniel Vetterb5518422013-05-03 11:49:48 +02007100static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7101 struct intel_link_m_n *m_n)
7102{
7103 struct drm_device *dev = crtc->base.dev;
7104 struct drm_i915_private *dev_priv = dev->dev_private;
7105 int pipe = crtc->pipe;
7106
Daniel Vettere3b95f12013-05-03 11:49:49 +02007107 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7108 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7109 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7110 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007111}
7112
7113static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007114 struct intel_link_m_n *m_n,
7115 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007116{
7117 struct drm_device *dev = crtc->base.dev;
7118 struct drm_i915_private *dev_priv = dev->dev_private;
7119 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007120 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007121
7122 if (INTEL_INFO(dev)->gen >= 5) {
7123 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7124 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7125 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7126 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007127 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7128 * for gen < 8) and if DRRS is supported (to make sure the
7129 * registers are not unnecessarily accessed).
7130 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307131 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007132 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007133 I915_WRITE(PIPE_DATA_M2(transcoder),
7134 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7135 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7136 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7137 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7138 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007139 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007140 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7141 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7142 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7143 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007144 }
7145}
7146
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307147void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007148{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307149 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7150
7151 if (m_n == M1_N1) {
7152 dp_m_n = &crtc->config->dp_m_n;
7153 dp_m2_n2 = &crtc->config->dp_m2_n2;
7154 } else if (m_n == M2_N2) {
7155
7156 /*
7157 * M2_N2 registers are not supported. Hence m2_n2 divider value
7158 * needs to be programmed into M1_N1.
7159 */
7160 dp_m_n = &crtc->config->dp_m2_n2;
7161 } else {
7162 DRM_ERROR("Unsupported divider value\n");
7163 return;
7164 }
7165
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007166 if (crtc->config->has_pch_encoder)
7167 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007168 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307169 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007170}
7171
Daniel Vetter251ac862015-06-18 10:30:24 +02007172static void vlv_compute_dpll(struct intel_crtc *crtc,
7173 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007174{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007175 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
7176 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7177 DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV;
7178 if (crtc->pipe != PIPE_A)
7179 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007180
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007181 pipe_config->dpll_hw_state.dpll_md =
7182 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7183}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007184
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007185static void chv_compute_dpll(struct intel_crtc *crtc,
7186 struct intel_crtc_state *pipe_config)
7187{
7188 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7189 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7190 DPLL_VCO_ENABLE;
7191 if (crtc->pipe != PIPE_A)
7192 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7193
7194 pipe_config->dpll_hw_state.dpll_md =
7195 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007196}
7197
Ville Syrjäläd288f652014-10-28 13:20:22 +02007198static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007199 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007200{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007201 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007202 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007203 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007204 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007205 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007206 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007207
Ville Syrjäläa5805162015-05-26 20:42:30 +03007208 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007209
Ville Syrjäläd288f652014-10-28 13:20:22 +02007210 bestn = pipe_config->dpll.n;
7211 bestm1 = pipe_config->dpll.m1;
7212 bestm2 = pipe_config->dpll.m2;
7213 bestp1 = pipe_config->dpll.p1;
7214 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007215
Jesse Barnes89b667f2013-04-18 14:51:36 -07007216 /* See eDP HDMI DPIO driver vbios notes doc */
7217
7218 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007219 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007220 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007221
7222 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007223 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007224
7225 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007226 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007227 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007228 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007229
7230 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007231 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007232
7233 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007234 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7235 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7236 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007237 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007238
7239 /*
7240 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7241 * but we don't support that).
7242 * Note: don't use the DAC post divider as it seems unstable.
7243 */
7244 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007245 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007246
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007247 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007248 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007249
Jesse Barnes89b667f2013-04-18 14:51:36 -07007250 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007251 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007252 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7253 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007254 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007255 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007256 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007257 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007258 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007259
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007260 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007261 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007262 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007263 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007264 0x0df40000);
7265 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007266 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007267 0x0df70000);
7268 } else { /* HDMI or VGA */
7269 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007270 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007271 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007272 0x0df70000);
7273 else
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 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007277
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007278 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007279 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007280 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7281 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007282 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007283 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007284
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007285 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007286 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007287}
7288
Ville Syrjäläd288f652014-10-28 13:20:22 +02007289static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007290 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007291{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007292 struct drm_device *dev = crtc->base.dev;
7293 struct drm_i915_private *dev_priv = dev->dev_private;
7294 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02007295 i915_reg_t dpll_reg = DPLL(crtc->pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007296 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307297 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007298 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307299 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307300 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007301
Ville Syrjäläd288f652014-10-28 13:20:22 +02007302 bestn = pipe_config->dpll.n;
7303 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7304 bestm1 = pipe_config->dpll.m1;
7305 bestm2 = pipe_config->dpll.m2 >> 22;
7306 bestp1 = pipe_config->dpll.p1;
7307 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307308 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307309 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307310 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007311
7312 /*
7313 * Enable Refclk and SSC
7314 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007315 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007316 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007317
Ville Syrjäläa5805162015-05-26 20:42:30 +03007318 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007319
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007320 /* p1 and p2 divider */
7321 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7322 5 << DPIO_CHV_S1_DIV_SHIFT |
7323 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7324 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7325 1 << DPIO_CHV_K_DIV_SHIFT);
7326
7327 /* Feedback post-divider - m2 */
7328 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7329
7330 /* Feedback refclk divider - n and m1 */
7331 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7332 DPIO_CHV_M1_DIV_BY_2 |
7333 1 << DPIO_CHV_N_DIV_SHIFT);
7334
7335 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007336 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007337
7338 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307339 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7340 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7341 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7342 if (bestm2_frac)
7343 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7344 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007345
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307346 /* Program digital lock detect threshold */
7347 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7348 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7349 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7350 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7351 if (!bestm2_frac)
7352 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7353 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7354
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007355 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307356 if (vco == 5400000) {
7357 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7358 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7359 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7360 tribuf_calcntr = 0x9;
7361 } else if (vco <= 6200000) {
7362 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7363 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7364 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7365 tribuf_calcntr = 0x9;
7366 } else if (vco <= 6480000) {
7367 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7368 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7369 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7370 tribuf_calcntr = 0x8;
7371 } else {
7372 /* Not supported. Apply the same limits as in the max case */
7373 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7374 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7375 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7376 tribuf_calcntr = 0;
7377 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007378 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7379
Ville Syrjälä968040b2015-03-11 22:52:08 +02007380 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307381 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7382 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7383 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7384
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007385 /* AFC Recal */
7386 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7387 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7388 DPIO_AFC_RECAL);
7389
Ville Syrjäläa5805162015-05-26 20:42:30 +03007390 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007391}
7392
Ville Syrjäläd288f652014-10-28 13:20:22 +02007393/**
7394 * vlv_force_pll_on - forcibly enable just the PLL
7395 * @dev_priv: i915 private structure
7396 * @pipe: pipe PLL to enable
7397 * @dpll: PLL configuration
7398 *
7399 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7400 * in cases where we need the PLL enabled even when @pipe is not going to
7401 * be enabled.
7402 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007403int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7404 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007405{
7406 struct intel_crtc *crtc =
7407 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007408 struct intel_crtc_state *pipe_config;
7409
7410 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7411 if (!pipe_config)
7412 return -ENOMEM;
7413
7414 pipe_config->base.crtc = &crtc->base;
7415 pipe_config->pixel_multiplier = 1;
7416 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007417
7418 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007419 chv_compute_dpll(crtc, pipe_config);
7420 chv_prepare_pll(crtc, pipe_config);
7421 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007422 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007423 vlv_compute_dpll(crtc, pipe_config);
7424 vlv_prepare_pll(crtc, pipe_config);
7425 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007426 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007427
7428 kfree(pipe_config);
7429
7430 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007431}
7432
7433/**
7434 * vlv_force_pll_off - forcibly disable just the PLL
7435 * @dev_priv: i915 private structure
7436 * @pipe: pipe PLL to disable
7437 *
7438 * Disable the PLL for @pipe. To be used in cases where we need
7439 * the PLL enabled even when @pipe is not going to be enabled.
7440 */
7441void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7442{
7443 if (IS_CHERRYVIEW(dev))
7444 chv_disable_pll(to_i915(dev), pipe);
7445 else
7446 vlv_disable_pll(to_i915(dev), pipe);
7447}
7448
Daniel Vetter251ac862015-06-18 10:30:24 +02007449static void i9xx_compute_dpll(struct intel_crtc *crtc,
7450 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007451 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007452{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007453 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007454 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007455 u32 dpll;
7456 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007457 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007458
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007459 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307460
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007461 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7462 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007463
7464 dpll = DPLL_VGA_MODE_DIS;
7465
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007466 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007467 dpll |= DPLLB_MODE_LVDS;
7468 else
7469 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007470
Daniel Vetteref1b4602013-06-01 17:17:04 +02007471 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007472 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007473 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007474 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007475
7476 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007477 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007478
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007479 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007480 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007481
7482 /* compute bitmask from p1 value */
7483 if (IS_PINEVIEW(dev))
7484 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7485 else {
7486 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7487 if (IS_G4X(dev) && reduced_clock)
7488 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7489 }
7490 switch (clock->p2) {
7491 case 5:
7492 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7493 break;
7494 case 7:
7495 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7496 break;
7497 case 10:
7498 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7499 break;
7500 case 14:
7501 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7502 break;
7503 }
7504 if (INTEL_INFO(dev)->gen >= 4)
7505 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7506
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007507 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007508 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007509 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007510 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007511 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7512 else
7513 dpll |= PLL_REF_INPUT_DREFCLK;
7514
7515 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007516 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007517
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007518 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007519 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007520 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007521 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007522 }
7523}
7524
Daniel Vetter251ac862015-06-18 10:30:24 +02007525static void i8xx_compute_dpll(struct intel_crtc *crtc,
7526 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007527 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007528{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007529 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007530 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007531 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007532 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007533
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007534 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307535
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007536 dpll = DPLL_VGA_MODE_DIS;
7537
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007538 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007539 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7540 } else {
7541 if (clock->p1 == 2)
7542 dpll |= PLL_P1_DIVIDE_BY_TWO;
7543 else
7544 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7545 if (clock->p2 == 4)
7546 dpll |= PLL_P2_DIVIDE_BY_4;
7547 }
7548
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007549 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007550 dpll |= DPLL_DVO_2X_MODE;
7551
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007552 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007553 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007554 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7555 else
7556 dpll |= PLL_REF_INPUT_DREFCLK;
7557
7558 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007559 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007560}
7561
Daniel Vetter8a654f32013-06-01 17:16:22 +02007562static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007563{
7564 struct drm_device *dev = intel_crtc->base.dev;
7565 struct drm_i915_private *dev_priv = dev->dev_private;
7566 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007567 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007568 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007569 uint32_t crtc_vtotal, crtc_vblank_end;
7570 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007571
7572 /* We need to be careful not to changed the adjusted mode, for otherwise
7573 * the hw state checker will get angry at the mismatch. */
7574 crtc_vtotal = adjusted_mode->crtc_vtotal;
7575 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007576
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007577 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007578 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007579 crtc_vtotal -= 1;
7580 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007581
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007582 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007583 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7584 else
7585 vsyncshift = adjusted_mode->crtc_hsync_start -
7586 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007587 if (vsyncshift < 0)
7588 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007589 }
7590
7591 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007592 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007593
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007594 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007595 (adjusted_mode->crtc_hdisplay - 1) |
7596 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007597 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007598 (adjusted_mode->crtc_hblank_start - 1) |
7599 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007600 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007601 (adjusted_mode->crtc_hsync_start - 1) |
7602 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7603
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007604 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007605 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007606 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007607 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007608 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007609 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007610 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007611 (adjusted_mode->crtc_vsync_start - 1) |
7612 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7613
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007614 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7615 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7616 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7617 * bits. */
7618 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7619 (pipe == PIPE_B || pipe == PIPE_C))
7620 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7621
Jani Nikulabc58be62016-03-18 17:05:39 +02007622}
7623
7624static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7625{
7626 struct drm_device *dev = intel_crtc->base.dev;
7627 struct drm_i915_private *dev_priv = dev->dev_private;
7628 enum pipe pipe = intel_crtc->pipe;
7629
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007630 /* pipesrc controls the size that is scaled from, which should
7631 * always be the user's requested size.
7632 */
7633 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007634 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7635 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007636}
7637
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007638static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007639 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007640{
7641 struct drm_device *dev = crtc->base.dev;
7642 struct drm_i915_private *dev_priv = dev->dev_private;
7643 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7644 uint32_t tmp;
7645
7646 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007647 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7648 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007649 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007650 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7651 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007652 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007653 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7654 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007655
7656 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007657 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7658 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007659 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007660 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7661 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007662 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007663 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7664 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007665
7666 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007667 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7668 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7669 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007670 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007671}
7672
7673static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7674 struct intel_crtc_state *pipe_config)
7675{
7676 struct drm_device *dev = crtc->base.dev;
7677 struct drm_i915_private *dev_priv = dev->dev_private;
7678 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007679
7680 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007681 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7682 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7683
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007684 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7685 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007686}
7687
Daniel Vetterf6a83282014-02-11 15:28:57 -08007688void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007689 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007690{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007691 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7692 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7693 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7694 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007695
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007696 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7697 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7698 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7699 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007700
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007701 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007702 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007703
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007704 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7705 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007706
7707 mode->hsync = drm_mode_hsync(mode);
7708 mode->vrefresh = drm_mode_vrefresh(mode);
7709 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007710}
7711
Daniel Vetter84b046f2013-02-19 18:48:54 +01007712static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7713{
7714 struct drm_device *dev = intel_crtc->base.dev;
7715 struct drm_i915_private *dev_priv = dev->dev_private;
7716 uint32_t pipeconf;
7717
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007718 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007719
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007720 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7721 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7722 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007723
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007724 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007725 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007726
Daniel Vetterff9ce462013-04-24 14:57:17 +02007727 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007728 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007729 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007730 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007731 pipeconf |= PIPECONF_DITHER_EN |
7732 PIPECONF_DITHER_TYPE_SP;
7733
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007734 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007735 case 18:
7736 pipeconf |= PIPECONF_6BPC;
7737 break;
7738 case 24:
7739 pipeconf |= PIPECONF_8BPC;
7740 break;
7741 case 30:
7742 pipeconf |= PIPECONF_10BPC;
7743 break;
7744 default:
7745 /* Case prevented by intel_choose_pipe_bpp_dither. */
7746 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007747 }
7748 }
7749
7750 if (HAS_PIPE_CXSR(dev)) {
7751 if (intel_crtc->lowfreq_avail) {
7752 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7753 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7754 } else {
7755 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007756 }
7757 }
7758
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007759 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007760 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007761 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007762 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7763 else
7764 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7765 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007766 pipeconf |= PIPECONF_PROGRESSIVE;
7767
Wayne Boyer666a4532015-12-09 12:29:35 -08007768 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7769 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007770 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007771
Daniel Vetter84b046f2013-02-19 18:48:54 +01007772 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7773 POSTING_READ(PIPECONF(intel_crtc->pipe));
7774}
7775
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007776static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7777 struct intel_crtc_state *crtc_state)
7778{
7779 struct drm_device *dev = crtc->base.dev;
7780 struct drm_i915_private *dev_priv = dev->dev_private;
7781 const intel_limit_t *limit;
7782 int refclk = 48000;
7783
7784 memset(&crtc_state->dpll_hw_state, 0,
7785 sizeof(crtc_state->dpll_hw_state));
7786
7787 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7788 if (intel_panel_use_ssc(dev_priv)) {
7789 refclk = dev_priv->vbt.lvds_ssc_freq;
7790 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7791 }
7792
7793 limit = &intel_limits_i8xx_lvds;
7794 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7795 limit = &intel_limits_i8xx_dvo;
7796 } else {
7797 limit = &intel_limits_i8xx_dac;
7798 }
7799
7800 if (!crtc_state->clock_set &&
7801 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7802 refclk, NULL, &crtc_state->dpll)) {
7803 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7804 return -EINVAL;
7805 }
7806
7807 i8xx_compute_dpll(crtc, crtc_state, NULL);
7808
7809 return 0;
7810}
7811
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007812static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7813 struct intel_crtc_state *crtc_state)
7814{
7815 struct drm_device *dev = crtc->base.dev;
7816 struct drm_i915_private *dev_priv = dev->dev_private;
7817 const intel_limit_t *limit;
7818 int refclk = 96000;
7819
7820 memset(&crtc_state->dpll_hw_state, 0,
7821 sizeof(crtc_state->dpll_hw_state));
7822
7823 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7824 if (intel_panel_use_ssc(dev_priv)) {
7825 refclk = dev_priv->vbt.lvds_ssc_freq;
7826 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7827 }
7828
7829 if (intel_is_dual_link_lvds(dev))
7830 limit = &intel_limits_g4x_dual_channel_lvds;
7831 else
7832 limit = &intel_limits_g4x_single_channel_lvds;
7833 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7834 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7835 limit = &intel_limits_g4x_hdmi;
7836 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7837 limit = &intel_limits_g4x_sdvo;
7838 } else {
7839 /* The option is for other outputs */
7840 limit = &intel_limits_i9xx_sdvo;
7841 }
7842
7843 if (!crtc_state->clock_set &&
7844 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7845 refclk, NULL, &crtc_state->dpll)) {
7846 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7847 return -EINVAL;
7848 }
7849
7850 i9xx_compute_dpll(crtc, crtc_state, NULL);
7851
7852 return 0;
7853}
7854
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007855static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7856 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007857{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007858 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007859 struct drm_i915_private *dev_priv = dev->dev_private;
Ma Lingd4906092009-03-18 20:13:27 +08007860 const intel_limit_t *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007861 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007862
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007863 memset(&crtc_state->dpll_hw_state, 0,
7864 sizeof(crtc_state->dpll_hw_state));
7865
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007866 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7867 if (intel_panel_use_ssc(dev_priv)) {
7868 refclk = dev_priv->vbt.lvds_ssc_freq;
7869 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7870 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007871
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007872 limit = &intel_limits_pineview_lvds;
7873 } else {
7874 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007875 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007876
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007877 if (!crtc_state->clock_set &&
7878 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7879 refclk, NULL, &crtc_state->dpll)) {
7880 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7881 return -EINVAL;
7882 }
7883
7884 i9xx_compute_dpll(crtc, crtc_state, NULL);
7885
7886 return 0;
7887}
7888
7889static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7890 struct intel_crtc_state *crtc_state)
7891{
7892 struct drm_device *dev = crtc->base.dev;
7893 struct drm_i915_private *dev_priv = dev->dev_private;
7894 const intel_limit_t *limit;
7895 int refclk = 96000;
7896
7897 memset(&crtc_state->dpll_hw_state, 0,
7898 sizeof(crtc_state->dpll_hw_state));
7899
7900 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7901 if (intel_panel_use_ssc(dev_priv)) {
7902 refclk = dev_priv->vbt.lvds_ssc_freq;
7903 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007904 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007905
7906 limit = &intel_limits_i9xx_lvds;
7907 } else {
7908 limit = &intel_limits_i9xx_sdvo;
7909 }
7910
7911 if (!crtc_state->clock_set &&
7912 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7913 refclk, NULL, &crtc_state->dpll)) {
7914 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7915 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007916 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007917
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007918 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007919
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007920 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007921}
7922
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007923static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7924 struct intel_crtc_state *crtc_state)
7925{
7926 int refclk = 100000;
7927 const intel_limit_t *limit = &intel_limits_chv;
7928
7929 memset(&crtc_state->dpll_hw_state, 0,
7930 sizeof(crtc_state->dpll_hw_state));
7931
7932 if (crtc_state->has_dsi_encoder)
7933 return 0;
7934
7935 if (!crtc_state->clock_set &&
7936 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7937 refclk, NULL, &crtc_state->dpll)) {
7938 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7939 return -EINVAL;
7940 }
7941
7942 chv_compute_dpll(crtc, crtc_state);
7943
7944 return 0;
7945}
7946
7947static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7948 struct intel_crtc_state *crtc_state)
7949{
7950 int refclk = 100000;
7951 const intel_limit_t *limit = &intel_limits_vlv;
7952
7953 memset(&crtc_state->dpll_hw_state, 0,
7954 sizeof(crtc_state->dpll_hw_state));
7955
7956 if (crtc_state->has_dsi_encoder)
7957 return 0;
7958
7959 if (!crtc_state->clock_set &&
7960 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7961 refclk, NULL, &crtc_state->dpll)) {
7962 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7963 return -EINVAL;
7964 }
7965
7966 vlv_compute_dpll(crtc, crtc_state);
7967
7968 return 0;
7969}
7970
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007971static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007972 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007973{
7974 struct drm_device *dev = crtc->base.dev;
7975 struct drm_i915_private *dev_priv = dev->dev_private;
7976 uint32_t tmp;
7977
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007978 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7979 return;
7980
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007981 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007982 if (!(tmp & PFIT_ENABLE))
7983 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007984
Daniel Vetter06922822013-07-11 13:35:40 +02007985 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007986 if (INTEL_INFO(dev)->gen < 4) {
7987 if (crtc->pipe != PIPE_B)
7988 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007989 } else {
7990 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7991 return;
7992 }
7993
Daniel Vetter06922822013-07-11 13:35:40 +02007994 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007995 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7996 if (INTEL_INFO(dev)->gen < 5)
7997 pipe_config->gmch_pfit.lvds_border_bits =
7998 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7999}
8000
Jesse Barnesacbec812013-09-20 11:29:32 -07008001static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008002 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008003{
8004 struct drm_device *dev = crtc->base.dev;
8005 struct drm_i915_private *dev_priv = dev->dev_private;
8006 int pipe = pipe_config->cpu_transcoder;
8007 intel_clock_t clock;
8008 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008009 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008010
Shobhit Kumarf573de52014-07-30 20:32:37 +05308011 /* In case of MIPI DPLL will not even be used */
8012 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8013 return;
8014
Ville Syrjäläa5805162015-05-26 20:42:30 +03008015 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008016 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008017 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008018
8019 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8020 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8021 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8022 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8023 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8024
Imre Deakdccbea32015-06-22 23:35:51 +03008025 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008026}
8027
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008028static void
8029i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8030 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008031{
8032 struct drm_device *dev = crtc->base.dev;
8033 struct drm_i915_private *dev_priv = dev->dev_private;
8034 u32 val, base, offset;
8035 int pipe = crtc->pipe, plane = crtc->plane;
8036 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008037 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008038 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008039 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008040
Damien Lespiau42a7b082015-02-05 19:35:13 +00008041 val = I915_READ(DSPCNTR(plane));
8042 if (!(val & DISPLAY_PLANE_ENABLE))
8043 return;
8044
Damien Lespiaud9806c92015-01-21 14:07:19 +00008045 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008046 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008047 DRM_DEBUG_KMS("failed to alloc fb\n");
8048 return;
8049 }
8050
Damien Lespiau1b842c82015-01-21 13:50:54 +00008051 fb = &intel_fb->base;
8052
Daniel Vetter18c52472015-02-10 17:16:09 +00008053 if (INTEL_INFO(dev)->gen >= 4) {
8054 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008055 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008056 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8057 }
8058 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008059
8060 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008061 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008062 fb->pixel_format = fourcc;
8063 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008064
8065 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008066 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008067 offset = I915_READ(DSPTILEOFF(plane));
8068 else
8069 offset = I915_READ(DSPLINOFF(plane));
8070 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8071 } else {
8072 base = I915_READ(DSPADDR(plane));
8073 }
8074 plane_config->base = base;
8075
8076 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008077 fb->width = ((val >> 16) & 0xfff) + 1;
8078 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008079
8080 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008081 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008082
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008083 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008084 fb->pixel_format,
8085 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008086
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008087 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008088
Damien Lespiau2844a922015-01-20 12:51:48 +00008089 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8090 pipe_name(pipe), plane, fb->width, fb->height,
8091 fb->bits_per_pixel, base, fb->pitches[0],
8092 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008093
Damien Lespiau2d140302015-02-05 17:22:18 +00008094 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008095}
8096
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008097static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008098 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008099{
8100 struct drm_device *dev = crtc->base.dev;
8101 struct drm_i915_private *dev_priv = dev->dev_private;
8102 int pipe = pipe_config->cpu_transcoder;
8103 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8104 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008105 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008106 int refclk = 100000;
8107
Ville Syrjäläa5805162015-05-26 20:42:30 +03008108 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008109 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8110 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8111 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8112 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008113 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008114 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008115
8116 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008117 clock.m2 = (pll_dw0 & 0xff) << 22;
8118 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8119 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008120 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8121 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8122 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8123
Imre Deakdccbea32015-06-22 23:35:51 +03008124 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008125}
8126
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008127static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008128 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008129{
8130 struct drm_device *dev = crtc->base.dev;
8131 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008132 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008133 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008134 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008135
Imre Deak17290502016-02-12 18:55:11 +02008136 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8137 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008138 return false;
8139
Daniel Vettere143a212013-07-04 12:01:15 +02008140 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008141 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008142
Imre Deak17290502016-02-12 18:55:11 +02008143 ret = false;
8144
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008145 tmp = I915_READ(PIPECONF(crtc->pipe));
8146 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008147 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008148
Wayne Boyer666a4532015-12-09 12:29:35 -08008149 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008150 switch (tmp & PIPECONF_BPC_MASK) {
8151 case PIPECONF_6BPC:
8152 pipe_config->pipe_bpp = 18;
8153 break;
8154 case PIPECONF_8BPC:
8155 pipe_config->pipe_bpp = 24;
8156 break;
8157 case PIPECONF_10BPC:
8158 pipe_config->pipe_bpp = 30;
8159 break;
8160 default:
8161 break;
8162 }
8163 }
8164
Wayne Boyer666a4532015-12-09 12:29:35 -08008165 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8166 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008167 pipe_config->limited_color_range = true;
8168
Ville Syrjälä282740f2013-09-04 18:30:03 +03008169 if (INTEL_INFO(dev)->gen < 4)
8170 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8171
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008172 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008173 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008174
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008175 i9xx_get_pfit_config(crtc, pipe_config);
8176
Daniel Vetter6c49f242013-06-06 12:45:25 +02008177 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008178 /* No way to read it out on pipes B and C */
8179 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8180 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8181 else
8182 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008183 pipe_config->pixel_multiplier =
8184 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8185 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008186 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008187 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8188 tmp = I915_READ(DPLL(crtc->pipe));
8189 pipe_config->pixel_multiplier =
8190 ((tmp & SDVO_MULTIPLIER_MASK)
8191 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8192 } else {
8193 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8194 * port and will be fixed up in the encoder->get_config
8195 * function. */
8196 pipe_config->pixel_multiplier = 1;
8197 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008198 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008199 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008200 /*
8201 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8202 * on 830. Filter it out here so that we don't
8203 * report errors due to that.
8204 */
8205 if (IS_I830(dev))
8206 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8207
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008208 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8209 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008210 } else {
8211 /* Mask out read-only status bits. */
8212 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8213 DPLL_PORTC_READY_MASK |
8214 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008215 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008216
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008217 if (IS_CHERRYVIEW(dev))
8218 chv_crtc_clock_get(crtc, pipe_config);
8219 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008220 vlv_crtc_clock_get(crtc, pipe_config);
8221 else
8222 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008223
Ville Syrjälä0f646142015-08-26 19:39:18 +03008224 /*
8225 * Normally the dotclock is filled in by the encoder .get_config()
8226 * but in case the pipe is enabled w/o any ports we need a sane
8227 * default.
8228 */
8229 pipe_config->base.adjusted_mode.crtc_clock =
8230 pipe_config->port_clock / pipe_config->pixel_multiplier;
8231
Imre Deak17290502016-02-12 18:55:11 +02008232 ret = true;
8233
8234out:
8235 intel_display_power_put(dev_priv, power_domain);
8236
8237 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008238}
8239
Paulo Zanonidde86e22012-12-01 12:04:25 -02008240static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008241{
8242 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008243 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008244 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008245 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008246 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008247 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008248 bool has_ck505 = false;
8249 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008250
8251 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008252 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008253 switch (encoder->type) {
8254 case INTEL_OUTPUT_LVDS:
8255 has_panel = true;
8256 has_lvds = true;
8257 break;
8258 case INTEL_OUTPUT_EDP:
8259 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008260 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008261 has_cpu_edp = true;
8262 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008263 default:
8264 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008265 }
8266 }
8267
Keith Packard99eb6a02011-09-26 14:29:12 -07008268 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008269 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008270 can_ssc = has_ck505;
8271 } else {
8272 has_ck505 = false;
8273 can_ssc = true;
8274 }
8275
Imre Deak2de69052013-05-08 13:14:04 +03008276 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8277 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008278
8279 /* Ironlake: try to setup display ref clock before DPLL
8280 * enabling. This is only under driver's control after
8281 * PCH B stepping, previous chipset stepping should be
8282 * ignoring this setting.
8283 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008284 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008285
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008286 /* As we must carefully and slowly disable/enable each source in turn,
8287 * compute the final state we want first and check if we need to
8288 * make any changes at all.
8289 */
8290 final = val;
8291 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008292 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008293 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008294 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008295 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8296
8297 final &= ~DREF_SSC_SOURCE_MASK;
8298 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8299 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008300
Keith Packard199e5d72011-09-22 12:01:57 -07008301 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008302 final |= DREF_SSC_SOURCE_ENABLE;
8303
8304 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8305 final |= DREF_SSC1_ENABLE;
8306
8307 if (has_cpu_edp) {
8308 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8309 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8310 else
8311 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8312 } else
8313 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8314 } else {
8315 final |= DREF_SSC_SOURCE_DISABLE;
8316 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8317 }
8318
8319 if (final == val)
8320 return;
8321
8322 /* Always enable nonspread source */
8323 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8324
8325 if (has_ck505)
8326 val |= DREF_NONSPREAD_CK505_ENABLE;
8327 else
8328 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8329
8330 if (has_panel) {
8331 val &= ~DREF_SSC_SOURCE_MASK;
8332 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008333
Keith Packard199e5d72011-09-22 12:01:57 -07008334 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008335 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008336 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008337 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008338 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008339 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008340
8341 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008342 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008343 POSTING_READ(PCH_DREF_CONTROL);
8344 udelay(200);
8345
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008346 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008347
8348 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008349 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008350 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008351 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008352 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008353 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008354 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008355 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008356 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008357
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008358 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008359 POSTING_READ(PCH_DREF_CONTROL);
8360 udelay(200);
8361 } else {
8362 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8363
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008364 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008365
8366 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008367 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008368
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008369 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008370 POSTING_READ(PCH_DREF_CONTROL);
8371 udelay(200);
8372
8373 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008374 val &= ~DREF_SSC_SOURCE_MASK;
8375 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008376
8377 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008378 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008379
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008380 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008381 POSTING_READ(PCH_DREF_CONTROL);
8382 udelay(200);
8383 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008384
8385 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008386}
8387
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008388static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008389{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008390 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008391
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008392 tmp = I915_READ(SOUTH_CHICKEN2);
8393 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8394 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008395
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008396 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8397 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8398 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008399
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008400 tmp = I915_READ(SOUTH_CHICKEN2);
8401 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8402 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008403
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008404 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8405 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8406 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008407}
8408
8409/* WaMPhyProgramming:hsw */
8410static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8411{
8412 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008413
8414 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8415 tmp &= ~(0xFF << 24);
8416 tmp |= (0x12 << 24);
8417 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8418
Paulo Zanonidde86e22012-12-01 12:04:25 -02008419 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8420 tmp |= (1 << 11);
8421 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8422
8423 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8424 tmp |= (1 << 11);
8425 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8426
Paulo Zanonidde86e22012-12-01 12:04:25 -02008427 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8428 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8429 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8430
8431 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8432 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8433 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8434
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008435 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8436 tmp &= ~(7 << 13);
8437 tmp |= (5 << 13);
8438 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008439
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008440 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8441 tmp &= ~(7 << 13);
8442 tmp |= (5 << 13);
8443 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008444
8445 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8446 tmp &= ~0xFF;
8447 tmp |= 0x1C;
8448 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8449
8450 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8451 tmp &= ~0xFF;
8452 tmp |= 0x1C;
8453 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8454
8455 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8456 tmp &= ~(0xFF << 16);
8457 tmp |= (0x1C << 16);
8458 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8459
8460 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8461 tmp &= ~(0xFF << 16);
8462 tmp |= (0x1C << 16);
8463 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8464
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008465 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8466 tmp |= (1 << 27);
8467 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008468
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008469 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8470 tmp |= (1 << 27);
8471 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008472
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008473 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8474 tmp &= ~(0xF << 28);
8475 tmp |= (4 << 28);
8476 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008477
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008478 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8479 tmp &= ~(0xF << 28);
8480 tmp |= (4 << 28);
8481 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008482}
8483
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008484/* Implements 3 different sequences from BSpec chapter "Display iCLK
8485 * Programming" based on the parameters passed:
8486 * - Sequence to enable CLKOUT_DP
8487 * - Sequence to enable CLKOUT_DP without spread
8488 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8489 */
8490static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8491 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008492{
8493 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008494 uint32_t reg, tmp;
8495
8496 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8497 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008498 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008499 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008500
Ville Syrjäläa5805162015-05-26 20:42:30 +03008501 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008502
8503 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8504 tmp &= ~SBI_SSCCTL_DISABLE;
8505 tmp |= SBI_SSCCTL_PATHALT;
8506 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8507
8508 udelay(24);
8509
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008510 if (with_spread) {
8511 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8512 tmp &= ~SBI_SSCCTL_PATHALT;
8513 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008514
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008515 if (with_fdi) {
8516 lpt_reset_fdi_mphy(dev_priv);
8517 lpt_program_fdi_mphy(dev_priv);
8518 }
8519 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008520
Ville Syrjäläc2699522015-08-27 23:55:59 +03008521 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008522 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8523 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8524 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008525
Ville Syrjäläa5805162015-05-26 20:42:30 +03008526 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008527}
8528
Paulo Zanoni47701c32013-07-23 11:19:25 -03008529/* Sequence to disable CLKOUT_DP */
8530static void lpt_disable_clkout_dp(struct drm_device *dev)
8531{
8532 struct drm_i915_private *dev_priv = dev->dev_private;
8533 uint32_t reg, tmp;
8534
Ville Syrjäläa5805162015-05-26 20:42:30 +03008535 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008536
Ville Syrjäläc2699522015-08-27 23:55:59 +03008537 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008538 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8539 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8540 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8541
8542 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8543 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8544 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8545 tmp |= SBI_SSCCTL_PATHALT;
8546 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8547 udelay(32);
8548 }
8549 tmp |= SBI_SSCCTL_DISABLE;
8550 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8551 }
8552
Ville Syrjäläa5805162015-05-26 20:42:30 +03008553 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008554}
8555
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008556#define BEND_IDX(steps) ((50 + (steps)) / 5)
8557
8558static const uint16_t sscdivintphase[] = {
8559 [BEND_IDX( 50)] = 0x3B23,
8560 [BEND_IDX( 45)] = 0x3B23,
8561 [BEND_IDX( 40)] = 0x3C23,
8562 [BEND_IDX( 35)] = 0x3C23,
8563 [BEND_IDX( 30)] = 0x3D23,
8564 [BEND_IDX( 25)] = 0x3D23,
8565 [BEND_IDX( 20)] = 0x3E23,
8566 [BEND_IDX( 15)] = 0x3E23,
8567 [BEND_IDX( 10)] = 0x3F23,
8568 [BEND_IDX( 5)] = 0x3F23,
8569 [BEND_IDX( 0)] = 0x0025,
8570 [BEND_IDX( -5)] = 0x0025,
8571 [BEND_IDX(-10)] = 0x0125,
8572 [BEND_IDX(-15)] = 0x0125,
8573 [BEND_IDX(-20)] = 0x0225,
8574 [BEND_IDX(-25)] = 0x0225,
8575 [BEND_IDX(-30)] = 0x0325,
8576 [BEND_IDX(-35)] = 0x0325,
8577 [BEND_IDX(-40)] = 0x0425,
8578 [BEND_IDX(-45)] = 0x0425,
8579 [BEND_IDX(-50)] = 0x0525,
8580};
8581
8582/*
8583 * Bend CLKOUT_DP
8584 * steps -50 to 50 inclusive, in steps of 5
8585 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8586 * change in clock period = -(steps / 10) * 5.787 ps
8587 */
8588static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8589{
8590 uint32_t tmp;
8591 int idx = BEND_IDX(steps);
8592
8593 if (WARN_ON(steps % 5 != 0))
8594 return;
8595
8596 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8597 return;
8598
8599 mutex_lock(&dev_priv->sb_lock);
8600
8601 if (steps % 10 != 0)
8602 tmp = 0xAAAAAAAB;
8603 else
8604 tmp = 0x00000000;
8605 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8606
8607 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8608 tmp &= 0xffff0000;
8609 tmp |= sscdivintphase[idx];
8610 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8611
8612 mutex_unlock(&dev_priv->sb_lock);
8613}
8614
8615#undef BEND_IDX
8616
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008617static void lpt_init_pch_refclk(struct drm_device *dev)
8618{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008619 struct intel_encoder *encoder;
8620 bool has_vga = false;
8621
Damien Lespiaub2784e12014-08-05 11:29:37 +01008622 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008623 switch (encoder->type) {
8624 case INTEL_OUTPUT_ANALOG:
8625 has_vga = true;
8626 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008627 default:
8628 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008629 }
8630 }
8631
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008632 if (has_vga) {
8633 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008634 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008635 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008636 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008637 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008638}
8639
Paulo Zanonidde86e22012-12-01 12:04:25 -02008640/*
8641 * Initialize reference clocks when the driver loads
8642 */
8643void intel_init_pch_refclk(struct drm_device *dev)
8644{
8645 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8646 ironlake_init_pch_refclk(dev);
8647 else if (HAS_PCH_LPT(dev))
8648 lpt_init_pch_refclk(dev);
8649}
8650
Daniel Vetter6ff93602013-04-19 11:24:36 +02008651static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008652{
8653 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8655 int pipe = intel_crtc->pipe;
8656 uint32_t val;
8657
Daniel Vetter78114072013-06-13 00:54:57 +02008658 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008659
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008660 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008661 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008662 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008663 break;
8664 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008665 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008666 break;
8667 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008668 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008669 break;
8670 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008671 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008672 break;
8673 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008674 /* Case prevented by intel_choose_pipe_bpp_dither. */
8675 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008676 }
8677
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008678 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008679 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8680
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008681 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008682 val |= PIPECONF_INTERLACED_ILK;
8683 else
8684 val |= PIPECONF_PROGRESSIVE;
8685
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008686 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008687 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008688
Paulo Zanonic8203562012-09-12 10:06:29 -03008689 I915_WRITE(PIPECONF(pipe), val);
8690 POSTING_READ(PIPECONF(pipe));
8691}
8692
Daniel Vetter6ff93602013-04-19 11:24:36 +02008693static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008694{
Jani Nikula391bf042016-03-18 17:05:40 +02008695 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008696 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008697 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008698 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008699
Jani Nikula391bf042016-03-18 17:05:40 +02008700 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008701 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8702
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008703 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008704 val |= PIPECONF_INTERLACED_ILK;
8705 else
8706 val |= PIPECONF_PROGRESSIVE;
8707
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008708 I915_WRITE(PIPECONF(cpu_transcoder), val);
8709 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008710}
8711
Jani Nikula391bf042016-03-18 17:05:40 +02008712static void haswell_set_pipemisc(struct drm_crtc *crtc)
8713{
8714 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8715 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8716
8717 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8718 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008719
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008720 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008721 case 18:
8722 val |= PIPEMISC_DITHER_6_BPC;
8723 break;
8724 case 24:
8725 val |= PIPEMISC_DITHER_8_BPC;
8726 break;
8727 case 30:
8728 val |= PIPEMISC_DITHER_10_BPC;
8729 break;
8730 case 36:
8731 val |= PIPEMISC_DITHER_12_BPC;
8732 break;
8733 default:
8734 /* Case prevented by pipe_config_set_bpp. */
8735 BUG();
8736 }
8737
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008738 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008739 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8740
Jani Nikula391bf042016-03-18 17:05:40 +02008741 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008742 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008743}
8744
Paulo Zanonid4b19312012-11-29 11:29:32 -02008745int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8746{
8747 /*
8748 * Account for spread spectrum to avoid
8749 * oversubscribing the link. Max center spread
8750 * is 2.5%; use 5% for safety's sake.
8751 */
8752 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008753 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008754}
8755
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008756static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008757{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008758 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008759}
8760
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008761static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8762 struct intel_crtc_state *crtc_state,
8763 intel_clock_t *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008764{
8765 struct drm_crtc *crtc = &intel_crtc->base;
8766 struct drm_device *dev = crtc->dev;
8767 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008768 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008769 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008770 struct drm_connector_state *connector_state;
8771 struct intel_encoder *encoder;
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008772 u32 dpll, fp, fp2;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008773 int factor, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008774 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008775
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008776 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008777 if (connector_state->crtc != crtc_state->base.crtc)
8778 continue;
8779
8780 encoder = to_intel_encoder(connector_state->best_encoder);
8781
8782 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008783 case INTEL_OUTPUT_LVDS:
8784 is_lvds = true;
8785 break;
8786 case INTEL_OUTPUT_SDVO:
8787 case INTEL_OUTPUT_HDMI:
8788 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008789 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008790 default:
8791 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008792 }
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008793 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008794
Chris Wilsonc1858122010-12-03 21:35:48 +00008795 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008796 factor = 21;
8797 if (is_lvds) {
8798 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008799 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008800 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008801 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008802 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008803 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008804
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008805 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008806
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008807 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8808 fp |= FP_CB_TUNE;
8809
8810 if (reduced_clock) {
8811 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8812
8813 if (reduced_clock->m < factor * reduced_clock->n)
8814 fp2 |= FP_CB_TUNE;
8815 } else {
8816 fp2 = fp;
8817 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008818
Chris Wilson5eddb702010-09-11 13:48:45 +01008819 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008820
Eric Anholta07d6782011-03-30 13:01:08 -07008821 if (is_lvds)
8822 dpll |= DPLLB_MODE_LVDS;
8823 else
8824 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008825
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008826 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008827 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008828
8829 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008830 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008831 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008832 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008833
Eric Anholta07d6782011-03-30 13:01:08 -07008834 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008835 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008836 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008837 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008838
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008839 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008840 case 5:
8841 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8842 break;
8843 case 7:
8844 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8845 break;
8846 case 10:
8847 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8848 break;
8849 case 14:
8850 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8851 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008852 }
8853
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008854 if (is_lvds && intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008855 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008856 else
8857 dpll |= PLL_REF_INPUT_DREFCLK;
8858
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008859 dpll |= DPLL_VCO_ENABLE;
8860
8861 crtc_state->dpll_hw_state.dpll = dpll;
8862 crtc_state->dpll_hw_state.fp0 = fp;
8863 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008864}
8865
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008866static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8867 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008868{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008869 struct drm_device *dev = crtc->base.dev;
8870 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008871 intel_clock_t reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008872 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008873 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008874 const intel_limit_t *limit;
8875 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008876
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008877 memset(&crtc_state->dpll_hw_state, 0,
8878 sizeof(crtc_state->dpll_hw_state));
8879
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008880 crtc->lowfreq_avail = false;
8881
8882 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8883 if (!crtc_state->has_pch_encoder)
8884 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008885
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008886 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8887 if (intel_panel_use_ssc(dev_priv)) {
8888 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8889 dev_priv->vbt.lvds_ssc_freq);
8890 refclk = dev_priv->vbt.lvds_ssc_freq;
8891 }
8892
8893 if (intel_is_dual_link_lvds(dev)) {
8894 if (refclk == 100000)
8895 limit = &intel_limits_ironlake_dual_lvds_100m;
8896 else
8897 limit = &intel_limits_ironlake_dual_lvds;
8898 } else {
8899 if (refclk == 100000)
8900 limit = &intel_limits_ironlake_single_lvds_100m;
8901 else
8902 limit = &intel_limits_ironlake_single_lvds;
8903 }
8904 } else {
8905 limit = &intel_limits_ironlake_dac;
8906 }
8907
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008908 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008909 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8910 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008911 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8912 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008913 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008914
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008915 ironlake_compute_dpll(crtc, crtc_state,
8916 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008917
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008918 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8919 if (pll == NULL) {
8920 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8921 pipe_name(crtc->pipe));
8922 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008923 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008924
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008925 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8926 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008927 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02008928
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008929 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008930}
8931
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008932static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8933 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008934{
8935 struct drm_device *dev = crtc->base.dev;
8936 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008937 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008938
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008939 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8940 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8941 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8942 & ~TU_SIZE_MASK;
8943 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8944 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8945 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8946}
8947
8948static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8949 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008950 struct intel_link_m_n *m_n,
8951 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008952{
8953 struct drm_device *dev = crtc->base.dev;
8954 struct drm_i915_private *dev_priv = dev->dev_private;
8955 enum pipe pipe = crtc->pipe;
8956
8957 if (INTEL_INFO(dev)->gen >= 5) {
8958 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8959 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8960 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8961 & ~TU_SIZE_MASK;
8962 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8963 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8964 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008965 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8966 * gen < 8) and if DRRS is supported (to make sure the
8967 * registers are not unnecessarily read).
8968 */
8969 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008970 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008971 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8972 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8973 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8974 & ~TU_SIZE_MASK;
8975 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8976 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8977 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8978 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008979 } else {
8980 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8981 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8982 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8983 & ~TU_SIZE_MASK;
8984 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8985 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8986 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8987 }
8988}
8989
8990void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008991 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008992{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008993 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008994 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8995 else
8996 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008997 &pipe_config->dp_m_n,
8998 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008999}
9000
Daniel Vetter72419202013-04-04 13:28:53 +02009001static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009002 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009003{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009004 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009005 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009006}
9007
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009008static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009009 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009010{
9011 struct drm_device *dev = crtc->base.dev;
9012 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009013 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9014 uint32_t ps_ctrl = 0;
9015 int id = -1;
9016 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009017
Chandra Kondurua1b22782015-04-07 15:28:45 -07009018 /* find scaler attached to this pipe */
9019 for (i = 0; i < crtc->num_scalers; i++) {
9020 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9021 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9022 id = i;
9023 pipe_config->pch_pfit.enabled = true;
9024 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9025 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9026 break;
9027 }
9028 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009029
Chandra Kondurua1b22782015-04-07 15:28:45 -07009030 scaler_state->scaler_id = id;
9031 if (id >= 0) {
9032 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9033 } else {
9034 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009035 }
9036}
9037
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009038static void
9039skylake_get_initial_plane_config(struct intel_crtc *crtc,
9040 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009041{
9042 struct drm_device *dev = crtc->base.dev;
9043 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009044 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009045 int pipe = crtc->pipe;
9046 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009047 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009048 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009049 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009050
Damien Lespiaud9806c92015-01-21 14:07:19 +00009051 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009052 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009053 DRM_DEBUG_KMS("failed to alloc fb\n");
9054 return;
9055 }
9056
Damien Lespiau1b842c82015-01-21 13:50:54 +00009057 fb = &intel_fb->base;
9058
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009059 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009060 if (!(val & PLANE_CTL_ENABLE))
9061 goto error;
9062
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009063 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9064 fourcc = skl_format_to_fourcc(pixel_format,
9065 val & PLANE_CTL_ORDER_RGBX,
9066 val & PLANE_CTL_ALPHA_MASK);
9067 fb->pixel_format = fourcc;
9068 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9069
Damien Lespiau40f46282015-02-27 11:15:21 +00009070 tiling = val & PLANE_CTL_TILED_MASK;
9071 switch (tiling) {
9072 case PLANE_CTL_TILED_LINEAR:
9073 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9074 break;
9075 case PLANE_CTL_TILED_X:
9076 plane_config->tiling = I915_TILING_X;
9077 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9078 break;
9079 case PLANE_CTL_TILED_Y:
9080 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9081 break;
9082 case PLANE_CTL_TILED_YF:
9083 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9084 break;
9085 default:
9086 MISSING_CASE(tiling);
9087 goto error;
9088 }
9089
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009090 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9091 plane_config->base = base;
9092
9093 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9094
9095 val = I915_READ(PLANE_SIZE(pipe, 0));
9096 fb->height = ((val >> 16) & 0xfff) + 1;
9097 fb->width = ((val >> 0) & 0x1fff) + 1;
9098
9099 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009100 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009101 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009102 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9103
9104 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009105 fb->pixel_format,
9106 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009107
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009108 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009109
9110 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9111 pipe_name(pipe), fb->width, fb->height,
9112 fb->bits_per_pixel, base, fb->pitches[0],
9113 plane_config->size);
9114
Damien Lespiau2d140302015-02-05 17:22:18 +00009115 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009116 return;
9117
9118error:
9119 kfree(fb);
9120}
9121
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009122static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009123 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009124{
9125 struct drm_device *dev = crtc->base.dev;
9126 struct drm_i915_private *dev_priv = dev->dev_private;
9127 uint32_t tmp;
9128
9129 tmp = I915_READ(PF_CTL(crtc->pipe));
9130
9131 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009132 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009133 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9134 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009135
9136 /* We currently do not free assignements of panel fitters on
9137 * ivb/hsw (since we don't use the higher upscaling modes which
9138 * differentiates them) so just WARN about this case for now. */
9139 if (IS_GEN7(dev)) {
9140 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9141 PF_PIPE_SEL_IVB(crtc->pipe));
9142 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009143 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009144}
9145
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009146static void
9147ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9148 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009149{
9150 struct drm_device *dev = crtc->base.dev;
9151 struct drm_i915_private *dev_priv = dev->dev_private;
9152 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009153 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009154 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009155 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009156 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009157 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009158
Damien Lespiau42a7b082015-02-05 19:35:13 +00009159 val = I915_READ(DSPCNTR(pipe));
9160 if (!(val & DISPLAY_PLANE_ENABLE))
9161 return;
9162
Damien Lespiaud9806c92015-01-21 14:07:19 +00009163 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009164 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009165 DRM_DEBUG_KMS("failed to alloc fb\n");
9166 return;
9167 }
9168
Damien Lespiau1b842c82015-01-21 13:50:54 +00009169 fb = &intel_fb->base;
9170
Daniel Vetter18c52472015-02-10 17:16:09 +00009171 if (INTEL_INFO(dev)->gen >= 4) {
9172 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009173 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009174 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9175 }
9176 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009177
9178 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009179 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009180 fb->pixel_format = fourcc;
9181 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009182
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009183 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009184 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009185 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009186 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009187 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009188 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009189 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009190 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009191 }
9192 plane_config->base = base;
9193
9194 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009195 fb->width = ((val >> 16) & 0xfff) + 1;
9196 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009197
9198 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009199 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009200
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009201 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009202 fb->pixel_format,
9203 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009204
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009205 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009206
Damien Lespiau2844a922015-01-20 12:51:48 +00009207 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9208 pipe_name(pipe), fb->width, fb->height,
9209 fb->bits_per_pixel, base, fb->pitches[0],
9210 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009211
Damien Lespiau2d140302015-02-05 17:22:18 +00009212 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009213}
9214
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009215static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009216 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009217{
9218 struct drm_device *dev = crtc->base.dev;
9219 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009220 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009221 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009222 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009223
Imre Deak17290502016-02-12 18:55:11 +02009224 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9225 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009226 return false;
9227
Daniel Vettere143a212013-07-04 12:01:15 +02009228 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009229 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009230
Imre Deak17290502016-02-12 18:55:11 +02009231 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009232 tmp = I915_READ(PIPECONF(crtc->pipe));
9233 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009234 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009235
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009236 switch (tmp & PIPECONF_BPC_MASK) {
9237 case PIPECONF_6BPC:
9238 pipe_config->pipe_bpp = 18;
9239 break;
9240 case PIPECONF_8BPC:
9241 pipe_config->pipe_bpp = 24;
9242 break;
9243 case PIPECONF_10BPC:
9244 pipe_config->pipe_bpp = 30;
9245 break;
9246 case PIPECONF_12BPC:
9247 pipe_config->pipe_bpp = 36;
9248 break;
9249 default:
9250 break;
9251 }
9252
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009253 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9254 pipe_config->limited_color_range = true;
9255
Daniel Vetterab9412b2013-05-03 11:49:46 +02009256 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009257 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009258 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009259
Daniel Vetter88adfff2013-03-28 10:42:01 +01009260 pipe_config->has_pch_encoder = true;
9261
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009262 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9263 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9264 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009265
9266 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009267
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009268 if (HAS_PCH_IBX(dev_priv->dev)) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009269 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009270 } else {
9271 tmp = I915_READ(PCH_DPLL_SEL);
9272 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009273 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009274 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009275 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009276 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009277
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009278 pipe_config->shared_dpll =
9279 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9280 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009281
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009282 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9283 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009284
9285 tmp = pipe_config->dpll_hw_state.dpll;
9286 pipe_config->pixel_multiplier =
9287 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9288 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009289
9290 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009291 } else {
9292 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009293 }
9294
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009295 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009296 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009297
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009298 ironlake_get_pfit_config(crtc, pipe_config);
9299
Imre Deak17290502016-02-12 18:55:11 +02009300 ret = true;
9301
9302out:
9303 intel_display_power_put(dev_priv, power_domain);
9304
9305 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009306}
9307
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009308static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9309{
9310 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009311 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009312
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009313 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009314 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009315 pipe_name(crtc->pipe));
9316
Rob Clarke2c719b2014-12-15 13:56:32 -05009317 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9318 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009319 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9320 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009321 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9322 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009323 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009324 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009325 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009326 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009327 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009328 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009329 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009330 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009331 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009332
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009333 /*
9334 * In theory we can still leave IRQs enabled, as long as only the HPD
9335 * interrupts remain enabled. We used to check for that, but since it's
9336 * gen-specific and since we only disable LCPLL after we fully disable
9337 * the interrupts, the check below should be enough.
9338 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009339 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009340}
9341
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009342static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9343{
9344 struct drm_device *dev = dev_priv->dev;
9345
9346 if (IS_HASWELL(dev))
9347 return I915_READ(D_COMP_HSW);
9348 else
9349 return I915_READ(D_COMP_BDW);
9350}
9351
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009352static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9353{
9354 struct drm_device *dev = dev_priv->dev;
9355
9356 if (IS_HASWELL(dev)) {
9357 mutex_lock(&dev_priv->rps.hw_lock);
9358 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9359 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009360 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009361 mutex_unlock(&dev_priv->rps.hw_lock);
9362 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009363 I915_WRITE(D_COMP_BDW, val);
9364 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009365 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009366}
9367
9368/*
9369 * This function implements pieces of two sequences from BSpec:
9370 * - Sequence for display software to disable LCPLL
9371 * - Sequence for display software to allow package C8+
9372 * The steps implemented here are just the steps that actually touch the LCPLL
9373 * register. Callers should take care of disabling all the display engine
9374 * functions, doing the mode unset, fixing interrupts, etc.
9375 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009376static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9377 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009378{
9379 uint32_t val;
9380
9381 assert_can_disable_lcpll(dev_priv);
9382
9383 val = I915_READ(LCPLL_CTL);
9384
9385 if (switch_to_fclk) {
9386 val |= LCPLL_CD_SOURCE_FCLK;
9387 I915_WRITE(LCPLL_CTL, val);
9388
9389 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9390 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9391 DRM_ERROR("Switching to FCLK failed\n");
9392
9393 val = I915_READ(LCPLL_CTL);
9394 }
9395
9396 val |= LCPLL_PLL_DISABLE;
9397 I915_WRITE(LCPLL_CTL, val);
9398 POSTING_READ(LCPLL_CTL);
9399
9400 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9401 DRM_ERROR("LCPLL still locked\n");
9402
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009403 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009404 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009405 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009406 ndelay(100);
9407
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009408 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9409 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009410 DRM_ERROR("D_COMP RCOMP still in progress\n");
9411
9412 if (allow_power_down) {
9413 val = I915_READ(LCPLL_CTL);
9414 val |= LCPLL_POWER_DOWN_ALLOW;
9415 I915_WRITE(LCPLL_CTL, val);
9416 POSTING_READ(LCPLL_CTL);
9417 }
9418}
9419
9420/*
9421 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9422 * source.
9423 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009424static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009425{
9426 uint32_t val;
9427
9428 val = I915_READ(LCPLL_CTL);
9429
9430 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9431 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9432 return;
9433
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009434 /*
9435 * Make sure we're not on PC8 state before disabling PC8, otherwise
9436 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009437 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009438 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009439
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009440 if (val & LCPLL_POWER_DOWN_ALLOW) {
9441 val &= ~LCPLL_POWER_DOWN_ALLOW;
9442 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009443 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009444 }
9445
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009446 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009447 val |= D_COMP_COMP_FORCE;
9448 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009449 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009450
9451 val = I915_READ(LCPLL_CTL);
9452 val &= ~LCPLL_PLL_DISABLE;
9453 I915_WRITE(LCPLL_CTL, val);
9454
9455 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9456 DRM_ERROR("LCPLL not locked yet\n");
9457
9458 if (val & LCPLL_CD_SOURCE_FCLK) {
9459 val = I915_READ(LCPLL_CTL);
9460 val &= ~LCPLL_CD_SOURCE_FCLK;
9461 I915_WRITE(LCPLL_CTL, val);
9462
9463 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9464 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9465 DRM_ERROR("Switching back to LCPLL failed\n");
9466 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009467
Mika Kuoppala59bad942015-01-16 11:34:40 +02009468 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009469 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009470}
9471
Paulo Zanoni765dab672014-03-07 20:08:18 -03009472/*
9473 * Package states C8 and deeper are really deep PC states that can only be
9474 * reached when all the devices on the system allow it, so even if the graphics
9475 * device allows PC8+, it doesn't mean the system will actually get to these
9476 * states. Our driver only allows PC8+ when going into runtime PM.
9477 *
9478 * The requirements for PC8+ are that all the outputs are disabled, the power
9479 * well is disabled and most interrupts are disabled, and these are also
9480 * requirements for runtime PM. When these conditions are met, we manually do
9481 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9482 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9483 * hang the machine.
9484 *
9485 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9486 * the state of some registers, so when we come back from PC8+ we need to
9487 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9488 * need to take care of the registers kept by RC6. Notice that this happens even
9489 * if we don't put the device in PCI D3 state (which is what currently happens
9490 * because of the runtime PM support).
9491 *
9492 * For more, read "Display Sequences for Package C8" on the hardware
9493 * documentation.
9494 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009495void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009496{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009497 struct drm_device *dev = dev_priv->dev;
9498 uint32_t val;
9499
Paulo Zanonic67a4702013-08-19 13:18:09 -03009500 DRM_DEBUG_KMS("Enabling package C8+\n");
9501
Ville Syrjäläc2699522015-08-27 23:55:59 +03009502 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009503 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9504 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9505 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9506 }
9507
9508 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009509 hsw_disable_lcpll(dev_priv, true, true);
9510}
9511
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009512void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009513{
9514 struct drm_device *dev = dev_priv->dev;
9515 uint32_t val;
9516
Paulo Zanonic67a4702013-08-19 13:18:09 -03009517 DRM_DEBUG_KMS("Disabling package C8+\n");
9518
9519 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009520 lpt_init_pch_refclk(dev);
9521
Ville Syrjäläc2699522015-08-27 23:55:59 +03009522 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009523 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9524 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9525 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9526 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009527}
9528
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009529static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309530{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009531 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009532 struct intel_atomic_state *old_intel_state =
9533 to_intel_atomic_state(old_state);
9534 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309535
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009536 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309537}
9538
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009539/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009540static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009541{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009542 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9543 struct drm_i915_private *dev_priv = state->dev->dev_private;
9544 struct drm_crtc *crtc;
9545 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009546 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009547 unsigned max_pixel_rate = 0, i;
9548 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009549
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009550 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9551 sizeof(intel_state->min_pixclk));
9552
9553 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009554 int pixel_rate;
9555
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009556 crtc_state = to_intel_crtc_state(cstate);
9557 if (!crtc_state->base.enable) {
9558 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009559 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009560 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009561
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009562 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009563
9564 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009565 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009566 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9567
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009568 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009569 }
9570
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009571 for_each_pipe(dev_priv, pipe)
9572 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9573
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009574 return max_pixel_rate;
9575}
9576
9577static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9578{
9579 struct drm_i915_private *dev_priv = dev->dev_private;
9580 uint32_t val, data;
9581 int ret;
9582
9583 if (WARN((I915_READ(LCPLL_CTL) &
9584 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9585 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9586 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9587 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9588 "trying to change cdclk frequency with cdclk not enabled\n"))
9589 return;
9590
9591 mutex_lock(&dev_priv->rps.hw_lock);
9592 ret = sandybridge_pcode_write(dev_priv,
9593 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9594 mutex_unlock(&dev_priv->rps.hw_lock);
9595 if (ret) {
9596 DRM_ERROR("failed to inform pcode about cdclk change\n");
9597 return;
9598 }
9599
9600 val = I915_READ(LCPLL_CTL);
9601 val |= LCPLL_CD_SOURCE_FCLK;
9602 I915_WRITE(LCPLL_CTL, val);
9603
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009604 if (wait_for_us(I915_READ(LCPLL_CTL) &
9605 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009606 DRM_ERROR("Switching to FCLK failed\n");
9607
9608 val = I915_READ(LCPLL_CTL);
9609 val &= ~LCPLL_CLK_FREQ_MASK;
9610
9611 switch (cdclk) {
9612 case 450000:
9613 val |= LCPLL_CLK_FREQ_450;
9614 data = 0;
9615 break;
9616 case 540000:
9617 val |= LCPLL_CLK_FREQ_54O_BDW;
9618 data = 1;
9619 break;
9620 case 337500:
9621 val |= LCPLL_CLK_FREQ_337_5_BDW;
9622 data = 2;
9623 break;
9624 case 675000:
9625 val |= LCPLL_CLK_FREQ_675_BDW;
9626 data = 3;
9627 break;
9628 default:
9629 WARN(1, "invalid cdclk frequency\n");
9630 return;
9631 }
9632
9633 I915_WRITE(LCPLL_CTL, val);
9634
9635 val = I915_READ(LCPLL_CTL);
9636 val &= ~LCPLL_CD_SOURCE_FCLK;
9637 I915_WRITE(LCPLL_CTL, val);
9638
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009639 if (wait_for_us((I915_READ(LCPLL_CTL) &
9640 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009641 DRM_ERROR("Switching back to LCPLL failed\n");
9642
9643 mutex_lock(&dev_priv->rps.hw_lock);
9644 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9645 mutex_unlock(&dev_priv->rps.hw_lock);
9646
9647 intel_update_cdclk(dev);
9648
9649 WARN(cdclk != dev_priv->cdclk_freq,
9650 "cdclk requested %d kHz but got %d kHz\n",
9651 cdclk, dev_priv->cdclk_freq);
9652}
9653
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009654static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009655{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009656 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009657 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009658 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009659 int cdclk;
9660
9661 /*
9662 * FIXME should also account for plane ratio
9663 * once 64bpp pixel formats are supported.
9664 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009665 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009666 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009667 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009668 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009669 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009670 cdclk = 450000;
9671 else
9672 cdclk = 337500;
9673
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009674 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009675 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9676 cdclk, dev_priv->max_cdclk_freq);
9677 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009678 }
9679
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009680 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9681 if (!intel_state->active_crtcs)
9682 intel_state->dev_cdclk = 337500;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009683
9684 return 0;
9685}
9686
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009687static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009688{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009689 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009690 struct intel_atomic_state *old_intel_state =
9691 to_intel_atomic_state(old_state);
9692 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009693
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009694 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009695}
9696
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009697static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9698 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009699{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009700 struct intel_encoder *intel_encoder =
9701 intel_ddi_get_crtc_new_encoder(crtc_state);
9702
9703 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9704 if (!intel_ddi_pll_select(crtc, crtc_state))
9705 return -EINVAL;
9706 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009707
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009708 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009709
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009710 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009711}
9712
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309713static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9714 enum port port,
9715 struct intel_crtc_state *pipe_config)
9716{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009717 enum intel_dpll_id id;
9718
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309719 switch (port) {
9720 case PORT_A:
9721 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009722 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309723 break;
9724 case PORT_B:
9725 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009726 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309727 break;
9728 case PORT_C:
9729 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009730 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309731 break;
9732 default:
9733 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009734 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309735 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009736
9737 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309738}
9739
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009740static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9741 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009742 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009743{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009744 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009745 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009746
9747 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9748 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9749
9750 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009751 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009752 id = DPLL_ID_SKL_DPLL0;
9753 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009754 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009755 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009756 break;
9757 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009758 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009759 break;
9760 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009761 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009762 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009763 default:
9764 MISSING_CASE(pipe_config->ddi_pll_sel);
9765 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009766 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009767
9768 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009769}
9770
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009771static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9772 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009773 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009774{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009775 enum intel_dpll_id id;
9776
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009777 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9778
9779 switch (pipe_config->ddi_pll_sel) {
9780 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009781 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009782 break;
9783 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009784 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009785 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009786 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009787 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009788 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009789 case PORT_CLK_SEL_LCPLL_810:
9790 id = DPLL_ID_LCPLL_810;
9791 break;
9792 case PORT_CLK_SEL_LCPLL_1350:
9793 id = DPLL_ID_LCPLL_1350;
9794 break;
9795 case PORT_CLK_SEL_LCPLL_2700:
9796 id = DPLL_ID_LCPLL_2700;
9797 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009798 default:
9799 MISSING_CASE(pipe_config->ddi_pll_sel);
9800 /* fall through */
9801 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009802 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009803 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009804
9805 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009806}
9807
Jani Nikulacf304292016-03-18 17:05:41 +02009808static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9809 struct intel_crtc_state *pipe_config,
9810 unsigned long *power_domain_mask)
9811{
9812 struct drm_device *dev = crtc->base.dev;
9813 struct drm_i915_private *dev_priv = dev->dev_private;
9814 enum intel_display_power_domain power_domain;
9815 u32 tmp;
9816
9817 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9818
9819 /*
9820 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9821 * consistency and less surprising code; it's in always on power).
9822 */
9823 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9824 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9825 enum pipe trans_edp_pipe;
9826 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9827 default:
9828 WARN(1, "unknown pipe linked to edp transcoder\n");
9829 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9830 case TRANS_DDI_EDP_INPUT_A_ON:
9831 trans_edp_pipe = PIPE_A;
9832 break;
9833 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9834 trans_edp_pipe = PIPE_B;
9835 break;
9836 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9837 trans_edp_pipe = PIPE_C;
9838 break;
9839 }
9840
9841 if (trans_edp_pipe == crtc->pipe)
9842 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9843 }
9844
9845 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9846 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9847 return false;
9848 *power_domain_mask |= BIT(power_domain);
9849
9850 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9851
9852 return tmp & PIPECONF_ENABLE;
9853}
9854
Jani Nikula4d1de972016-03-18 17:05:42 +02009855static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9856 struct intel_crtc_state *pipe_config,
9857 unsigned long *power_domain_mask)
9858{
9859 struct drm_device *dev = crtc->base.dev;
9860 struct drm_i915_private *dev_priv = dev->dev_private;
9861 enum intel_display_power_domain power_domain;
9862 enum port port;
9863 enum transcoder cpu_transcoder;
9864 u32 tmp;
9865
9866 pipe_config->has_dsi_encoder = false;
9867
9868 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9869 if (port == PORT_A)
9870 cpu_transcoder = TRANSCODER_DSI_A;
9871 else
9872 cpu_transcoder = TRANSCODER_DSI_C;
9873
9874 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9875 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9876 continue;
9877 *power_domain_mask |= BIT(power_domain);
9878
Imre Deakdb18b6a2016-03-24 12:41:40 +02009879 /*
9880 * The PLL needs to be enabled with a valid divider
9881 * configuration, otherwise accessing DSI registers will hang
9882 * the machine. See BSpec North Display Engine
9883 * registers/MIPI[BXT]. We can break out here early, since we
9884 * need the same DSI PLL to be enabled for both DSI ports.
9885 */
9886 if (!intel_dsi_pll_is_enabled(dev_priv))
9887 break;
9888
Jani Nikula4d1de972016-03-18 17:05:42 +02009889 /* XXX: this works for video mode only */
9890 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9891 if (!(tmp & DPI_ENABLE))
9892 continue;
9893
9894 tmp = I915_READ(MIPI_CTRL(port));
9895 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9896 continue;
9897
9898 pipe_config->cpu_transcoder = cpu_transcoder;
9899 pipe_config->has_dsi_encoder = true;
9900 break;
9901 }
9902
9903 return pipe_config->has_dsi_encoder;
9904}
9905
Daniel Vetter26804af2014-06-25 22:01:55 +03009906static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009907 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009908{
9909 struct drm_device *dev = crtc->base.dev;
9910 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009911 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009912 enum port port;
9913 uint32_t tmp;
9914
9915 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9916
9917 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9918
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07009919 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009920 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309921 else if (IS_BROXTON(dev))
9922 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009923 else
9924 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009925
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009926 pll = pipe_config->shared_dpll;
9927 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009928 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9929 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009930 }
9931
Daniel Vetter26804af2014-06-25 22:01:55 +03009932 /*
9933 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9934 * DDI E. So just check whether this pipe is wired to DDI E and whether
9935 * the PCH transcoder is on.
9936 */
Damien Lespiauca370452013-12-03 13:56:24 +00009937 if (INTEL_INFO(dev)->gen < 9 &&
9938 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009939 pipe_config->has_pch_encoder = true;
9940
9941 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9942 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9943 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9944
9945 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9946 }
9947}
9948
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009949static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009950 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009951{
9952 struct drm_device *dev = crtc->base.dev;
9953 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009954 enum intel_display_power_domain power_domain;
9955 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009956 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009957
Imre Deak17290502016-02-12 18:55:11 +02009958 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9959 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009960 return false;
Imre Deak17290502016-02-12 18:55:11 +02009961 power_domain_mask = BIT(power_domain);
9962
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009963 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009964
Jani Nikulacf304292016-03-18 17:05:41 +02009965 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +02009966
Jani Nikula4d1de972016-03-18 17:05:42 +02009967 if (IS_BROXTON(dev_priv)) {
9968 bxt_get_dsi_transcoder_state(crtc, pipe_config,
9969 &power_domain_mask);
9970 WARN_ON(active && pipe_config->has_dsi_encoder);
9971 if (pipe_config->has_dsi_encoder)
9972 active = true;
9973 }
9974
Jani Nikulacf304292016-03-18 17:05:41 +02009975 if (!active)
Imre Deak17290502016-02-12 18:55:11 +02009976 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009977
Jani Nikula4d1de972016-03-18 17:05:42 +02009978 if (!pipe_config->has_dsi_encoder) {
9979 haswell_get_ddi_port_state(crtc, pipe_config);
9980 intel_get_pipe_timings(crtc, pipe_config);
9981 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009982
Jani Nikulabc58be62016-03-18 17:05:39 +02009983 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009984
Lionel Landwerlin05dc6982016-03-16 10:57:15 +00009985 pipe_config->gamma_mode =
9986 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9987
Chandra Kondurua1b22782015-04-07 15:28:45 -07009988 if (INTEL_INFO(dev)->gen >= 9) {
9989 skl_init_scalers(dev, crtc, pipe_config);
9990 }
9991
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009992 if (INTEL_INFO(dev)->gen >= 9) {
9993 pipe_config->scaler_state.scaler_id = -1;
9994 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9995 }
9996
Imre Deak17290502016-02-12 18:55:11 +02009997 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9998 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
9999 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010000 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010001 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010002 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010003 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010004 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010005
Jesse Barnese59150d2014-01-07 13:30:45 -080010006 if (IS_HASWELL(dev))
10007 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10008 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010009
Jani Nikula4d1de972016-03-18 17:05:42 +020010010 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10011 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010012 pipe_config->pixel_multiplier =
10013 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10014 } else {
10015 pipe_config->pixel_multiplier = 1;
10016 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010017
Imre Deak17290502016-02-12 18:55:11 +020010018out:
10019 for_each_power_domain(power_domain, power_domain_mask)
10020 intel_display_power_put(dev_priv, power_domain);
10021
Jani Nikulacf304292016-03-18 17:05:41 +020010022 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010023}
10024
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010025static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10026 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010027{
10028 struct drm_device *dev = crtc->dev;
10029 struct drm_i915_private *dev_priv = dev->dev_private;
10030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010031 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010032
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010033 if (plane_state && plane_state->visible) {
10034 unsigned int width = plane_state->base.crtc_w;
10035 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010036 unsigned int stride = roundup_pow_of_two(width) * 4;
10037
10038 switch (stride) {
10039 default:
10040 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10041 width, stride);
10042 stride = 256;
10043 /* fallthrough */
10044 case 256:
10045 case 512:
10046 case 1024:
10047 case 2048:
10048 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010049 }
10050
Ville Syrjälädc41c152014-08-13 11:57:05 +030010051 cntl |= CURSOR_ENABLE |
10052 CURSOR_GAMMA_ENABLE |
10053 CURSOR_FORMAT_ARGB |
10054 CURSOR_STRIDE(stride);
10055
10056 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010057 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010058
Ville Syrjälädc41c152014-08-13 11:57:05 +030010059 if (intel_crtc->cursor_cntl != 0 &&
10060 (intel_crtc->cursor_base != base ||
10061 intel_crtc->cursor_size != size ||
10062 intel_crtc->cursor_cntl != cntl)) {
10063 /* On these chipsets we can only modify the base/size/stride
10064 * whilst the cursor is disabled.
10065 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010066 I915_WRITE(CURCNTR(PIPE_A), 0);
10067 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010068 intel_crtc->cursor_cntl = 0;
10069 }
10070
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010071 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010072 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010073 intel_crtc->cursor_base = base;
10074 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010075
10076 if (intel_crtc->cursor_size != size) {
10077 I915_WRITE(CURSIZE, size);
10078 intel_crtc->cursor_size = size;
10079 }
10080
Chris Wilson4b0e3332014-05-30 16:35:26 +030010081 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010082 I915_WRITE(CURCNTR(PIPE_A), cntl);
10083 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010084 intel_crtc->cursor_cntl = cntl;
10085 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010086}
10087
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010088static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10089 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010090{
10091 struct drm_device *dev = crtc->dev;
10092 struct drm_i915_private *dev_priv = dev->dev_private;
10093 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10094 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010095 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010096
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010097 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010098 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010099 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010100 case 64:
10101 cntl |= CURSOR_MODE_64_ARGB_AX;
10102 break;
10103 case 128:
10104 cntl |= CURSOR_MODE_128_ARGB_AX;
10105 break;
10106 case 256:
10107 cntl |= CURSOR_MODE_256_ARGB_AX;
10108 break;
10109 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010110 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010111 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010112 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010113 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010114
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010115 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010116 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010117
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010118 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10119 cntl |= CURSOR_ROTATE_180;
10120 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010121
Chris Wilson4b0e3332014-05-30 16:35:26 +030010122 if (intel_crtc->cursor_cntl != cntl) {
10123 I915_WRITE(CURCNTR(pipe), cntl);
10124 POSTING_READ(CURCNTR(pipe));
10125 intel_crtc->cursor_cntl = cntl;
10126 }
10127
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010128 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010129 I915_WRITE(CURBASE(pipe), base);
10130 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010131
10132 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010133}
10134
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010135/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010136static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010137 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010138{
10139 struct drm_device *dev = crtc->dev;
10140 struct drm_i915_private *dev_priv = dev->dev_private;
10141 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10142 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010143 u32 base = intel_crtc->cursor_addr;
10144 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010145
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010146 if (plane_state) {
10147 int x = plane_state->base.crtc_x;
10148 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010149
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010150 if (x < 0) {
10151 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10152 x = -x;
10153 }
10154 pos |= x << CURSOR_X_SHIFT;
10155
10156 if (y < 0) {
10157 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10158 y = -y;
10159 }
10160 pos |= y << CURSOR_Y_SHIFT;
10161
10162 /* ILK+ do this automagically */
10163 if (HAS_GMCH_DISPLAY(dev) &&
10164 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10165 base += (plane_state->base.crtc_h *
10166 plane_state->base.crtc_w - 1) * 4;
10167 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010168 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010169
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010170 I915_WRITE(CURPOS(pipe), pos);
10171
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010172 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010173 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010174 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010175 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010176}
10177
Ville Syrjälädc41c152014-08-13 11:57:05 +030010178static bool cursor_size_ok(struct drm_device *dev,
10179 uint32_t width, uint32_t height)
10180{
10181 if (width == 0 || height == 0)
10182 return false;
10183
10184 /*
10185 * 845g/865g are special in that they are only limited by
10186 * the width of their cursors, the height is arbitrary up to
10187 * the precision of the register. Everything else requires
10188 * square cursors, limited to a few power-of-two sizes.
10189 */
10190 if (IS_845G(dev) || IS_I865G(dev)) {
10191 if ((width & 63) != 0)
10192 return false;
10193
10194 if (width > (IS_845G(dev) ? 64 : 512))
10195 return false;
10196
10197 if (height > 1023)
10198 return false;
10199 } else {
10200 switch (width | height) {
10201 case 256:
10202 case 128:
10203 if (IS_GEN2(dev))
10204 return false;
10205 case 64:
10206 break;
10207 default:
10208 return false;
10209 }
10210 }
10211
10212 return true;
10213}
10214
Jesse Barnes79e53942008-11-07 14:24:08 -080010215/* VESA 640x480x72Hz mode to set on the pipe */
10216static struct drm_display_mode load_detect_mode = {
10217 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10218 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10219};
10220
Daniel Vettera8bb6812014-02-10 18:00:39 +010010221struct drm_framebuffer *
10222__intel_framebuffer_create(struct drm_device *dev,
10223 struct drm_mode_fb_cmd2 *mode_cmd,
10224 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010225{
10226 struct intel_framebuffer *intel_fb;
10227 int ret;
10228
10229 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010230 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010231 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010232
10233 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010234 if (ret)
10235 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010236
10237 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010238
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010239err:
10240 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010241 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010242}
10243
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010244static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010245intel_framebuffer_create(struct drm_device *dev,
10246 struct drm_mode_fb_cmd2 *mode_cmd,
10247 struct drm_i915_gem_object *obj)
10248{
10249 struct drm_framebuffer *fb;
10250 int ret;
10251
10252 ret = i915_mutex_lock_interruptible(dev);
10253 if (ret)
10254 return ERR_PTR(ret);
10255 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10256 mutex_unlock(&dev->struct_mutex);
10257
10258 return fb;
10259}
10260
Chris Wilsond2dff872011-04-19 08:36:26 +010010261static u32
10262intel_framebuffer_pitch_for_width(int width, int bpp)
10263{
10264 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10265 return ALIGN(pitch, 64);
10266}
10267
10268static u32
10269intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10270{
10271 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010272 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010273}
10274
10275static struct drm_framebuffer *
10276intel_framebuffer_create_for_mode(struct drm_device *dev,
10277 struct drm_display_mode *mode,
10278 int depth, int bpp)
10279{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010280 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010281 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010282 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010283
10284 obj = i915_gem_alloc_object(dev,
10285 intel_framebuffer_size_for_mode(mode, bpp));
10286 if (obj == NULL)
10287 return ERR_PTR(-ENOMEM);
10288
10289 mode_cmd.width = mode->hdisplay;
10290 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010291 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10292 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010293 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010294
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010295 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10296 if (IS_ERR(fb))
10297 drm_gem_object_unreference_unlocked(&obj->base);
10298
10299 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010300}
10301
10302static struct drm_framebuffer *
10303mode_fits_in_fbdev(struct drm_device *dev,
10304 struct drm_display_mode *mode)
10305{
Daniel Vetter06957262015-08-10 13:34:08 +020010306#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010307 struct drm_i915_private *dev_priv = dev->dev_private;
10308 struct drm_i915_gem_object *obj;
10309 struct drm_framebuffer *fb;
10310
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010311 if (!dev_priv->fbdev)
10312 return NULL;
10313
10314 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010315 return NULL;
10316
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010317 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010318 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010319
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010320 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010321 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10322 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010323 return NULL;
10324
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010325 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010326 return NULL;
10327
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010328 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010329 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010330#else
10331 return NULL;
10332#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010333}
10334
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010335static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10336 struct drm_crtc *crtc,
10337 struct drm_display_mode *mode,
10338 struct drm_framebuffer *fb,
10339 int x, int y)
10340{
10341 struct drm_plane_state *plane_state;
10342 int hdisplay, vdisplay;
10343 int ret;
10344
10345 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10346 if (IS_ERR(plane_state))
10347 return PTR_ERR(plane_state);
10348
10349 if (mode)
10350 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10351 else
10352 hdisplay = vdisplay = 0;
10353
10354 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10355 if (ret)
10356 return ret;
10357 drm_atomic_set_fb_for_plane(plane_state, fb);
10358 plane_state->crtc_x = 0;
10359 plane_state->crtc_y = 0;
10360 plane_state->crtc_w = hdisplay;
10361 plane_state->crtc_h = vdisplay;
10362 plane_state->src_x = x << 16;
10363 plane_state->src_y = y << 16;
10364 plane_state->src_w = hdisplay << 16;
10365 plane_state->src_h = vdisplay << 16;
10366
10367 return 0;
10368}
10369
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010370bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010371 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010372 struct intel_load_detect_pipe *old,
10373 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010374{
10375 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010376 struct intel_encoder *intel_encoder =
10377 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010378 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010379 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010380 struct drm_crtc *crtc = NULL;
10381 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010382 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010383 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010384 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010385 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010386 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010387 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010388
Chris Wilsond2dff872011-04-19 08:36:26 +010010389 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010390 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010391 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010392
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010393 old->restore_state = NULL;
10394
Rob Clark51fd3712013-11-19 12:10:12 -050010395retry:
10396 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10397 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010398 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010399
Jesse Barnes79e53942008-11-07 14:24:08 -080010400 /*
10401 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010402 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010403 * - if the connector already has an assigned crtc, use it (but make
10404 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010405 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010406 * - try to find the first unused crtc that can drive this connector,
10407 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010408 */
10409
10410 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010411 if (connector->state->crtc) {
10412 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010413
Rob Clark51fd3712013-11-19 12:10:12 -050010414 ret = drm_modeset_lock(&crtc->mutex, ctx);
10415 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010416 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010417
10418 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010419 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010420 }
10421
10422 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010423 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010424 i++;
10425 if (!(encoder->possible_crtcs & (1 << i)))
10426 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010427
10428 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10429 if (ret)
10430 goto fail;
10431
10432 if (possible_crtc->state->enable) {
10433 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010434 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010435 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010436
10437 crtc = possible_crtc;
10438 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010439 }
10440
10441 /*
10442 * If we didn't find an unused CRTC, don't use any.
10443 */
10444 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010445 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010446 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010447 }
10448
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010449found:
10450 intel_crtc = to_intel_crtc(crtc);
10451
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010452 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10453 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010454 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010455
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010456 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010457 restore_state = drm_atomic_state_alloc(dev);
10458 if (!state || !restore_state) {
10459 ret = -ENOMEM;
10460 goto fail;
10461 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010462
10463 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010464 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010465
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010466 connector_state = drm_atomic_get_connector_state(state, connector);
10467 if (IS_ERR(connector_state)) {
10468 ret = PTR_ERR(connector_state);
10469 goto fail;
10470 }
10471
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010472 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10473 if (ret)
10474 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010475
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010476 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10477 if (IS_ERR(crtc_state)) {
10478 ret = PTR_ERR(crtc_state);
10479 goto fail;
10480 }
10481
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010482 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010483
Chris Wilson64927112011-04-20 07:25:26 +010010484 if (!mode)
10485 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010486
Chris Wilsond2dff872011-04-19 08:36:26 +010010487 /* We need a framebuffer large enough to accommodate all accesses
10488 * that the plane may generate whilst we perform load detection.
10489 * We can not rely on the fbcon either being present (we get called
10490 * during its initialisation to detect all boot displays, or it may
10491 * not even exist) or that it is large enough to satisfy the
10492 * requested mode.
10493 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010494 fb = mode_fits_in_fbdev(dev, mode);
10495 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010496 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010497 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010498 } else
10499 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010500 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010501 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010502 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010503 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010504
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010505 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10506 if (ret)
10507 goto fail;
10508
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010509 drm_framebuffer_unreference(fb);
10510
10511 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10512 if (ret)
10513 goto fail;
10514
10515 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10516 if (!ret)
10517 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10518 if (!ret)
10519 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10520 if (ret) {
10521 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10522 goto fail;
10523 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010524
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010525 ret = drm_atomic_commit(state);
10526 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010527 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010528 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010529 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010530
10531 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010532
Jesse Barnes79e53942008-11-07 14:24:08 -080010533 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010534 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010535 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010536
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010537fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010538 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010539 drm_atomic_state_free(restore_state);
10540 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010541
Rob Clark51fd3712013-11-19 12:10:12 -050010542 if (ret == -EDEADLK) {
10543 drm_modeset_backoff(ctx);
10544 goto retry;
10545 }
10546
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010547 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010548}
10549
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010550void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010551 struct intel_load_detect_pipe *old,
10552 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010553{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010554 struct intel_encoder *intel_encoder =
10555 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010556 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010557 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010558 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010559
Chris Wilsond2dff872011-04-19 08:36:26 +010010560 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010561 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010562 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010563
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010564 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010565 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010566
10567 ret = drm_atomic_commit(state);
10568 if (ret) {
10569 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10570 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010571 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010572}
10573
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010574static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010575 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010576{
10577 struct drm_i915_private *dev_priv = dev->dev_private;
10578 u32 dpll = pipe_config->dpll_hw_state.dpll;
10579
10580 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010581 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010582 else if (HAS_PCH_SPLIT(dev))
10583 return 120000;
10584 else if (!IS_GEN2(dev))
10585 return 96000;
10586 else
10587 return 48000;
10588}
10589
Jesse Barnes79e53942008-11-07 14:24:08 -080010590/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010591static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010592 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010593{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010594 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010595 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010596 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010597 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010598 u32 fp;
10599 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010600 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010601 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010602
10603 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010604 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010605 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010606 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010607
10608 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010609 if (IS_PINEVIEW(dev)) {
10610 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10611 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010612 } else {
10613 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10614 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10615 }
10616
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010617 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010618 if (IS_PINEVIEW(dev))
10619 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10620 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010621 else
10622 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010623 DPLL_FPA01_P1_POST_DIV_SHIFT);
10624
10625 switch (dpll & DPLL_MODE_MASK) {
10626 case DPLLB_MODE_DAC_SERIAL:
10627 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10628 5 : 10;
10629 break;
10630 case DPLLB_MODE_LVDS:
10631 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10632 7 : 14;
10633 break;
10634 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010635 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010636 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010637 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010638 }
10639
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010640 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010641 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010642 else
Imre Deakdccbea32015-06-22 23:35:51 +030010643 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010644 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010645 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010646 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010647
10648 if (is_lvds) {
10649 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10650 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010651
10652 if (lvds & LVDS_CLKB_POWER_UP)
10653 clock.p2 = 7;
10654 else
10655 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010656 } else {
10657 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10658 clock.p1 = 2;
10659 else {
10660 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10661 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10662 }
10663 if (dpll & PLL_P2_DIVIDE_BY_4)
10664 clock.p2 = 4;
10665 else
10666 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010667 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010668
Imre Deakdccbea32015-06-22 23:35:51 +030010669 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010670 }
10671
Ville Syrjälä18442d02013-09-13 16:00:08 +030010672 /*
10673 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010674 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010675 * encoder's get_config() function.
10676 */
Imre Deakdccbea32015-06-22 23:35:51 +030010677 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010678}
10679
Ville Syrjälä6878da02013-09-13 15:59:11 +030010680int intel_dotclock_calculate(int link_freq,
10681 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010682{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010683 /*
10684 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010685 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010686 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010687 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010688 *
10689 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010690 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010691 */
10692
Ville Syrjälä6878da02013-09-13 15:59:11 +030010693 if (!m_n->link_n)
10694 return 0;
10695
10696 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10697}
10698
Ville Syrjälä18442d02013-09-13 16:00:08 +030010699static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010700 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010701{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010702 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010703
10704 /* read out port_clock from the DPLL */
10705 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010706
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010707 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010708 * In case there is an active pipe without active ports,
10709 * we may need some idea for the dotclock anyway.
10710 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010711 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010712 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010713 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010714 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010715}
10716
10717/** Returns the currently programmed mode of the given pipe. */
10718struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10719 struct drm_crtc *crtc)
10720{
Jesse Barnes548f2452011-02-17 10:40:53 -080010721 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010722 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010723 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010724 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010725 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010726 int htot = I915_READ(HTOTAL(cpu_transcoder));
10727 int hsync = I915_READ(HSYNC(cpu_transcoder));
10728 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10729 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010730 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010731
10732 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10733 if (!mode)
10734 return NULL;
10735
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010736 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10737 if (!pipe_config) {
10738 kfree(mode);
10739 return NULL;
10740 }
10741
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010742 /*
10743 * Construct a pipe_config sufficient for getting the clock info
10744 * back out of crtc_clock_get.
10745 *
10746 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10747 * to use a real value here instead.
10748 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010749 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10750 pipe_config->pixel_multiplier = 1;
10751 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10752 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10753 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10754 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010755
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010756 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010757 mode->hdisplay = (htot & 0xffff) + 1;
10758 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10759 mode->hsync_start = (hsync & 0xffff) + 1;
10760 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10761 mode->vdisplay = (vtot & 0xffff) + 1;
10762 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10763 mode->vsync_start = (vsync & 0xffff) + 1;
10764 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10765
10766 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010767
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010768 kfree(pipe_config);
10769
Jesse Barnes79e53942008-11-07 14:24:08 -080010770 return mode;
10771}
10772
Chris Wilsonf047e392012-07-21 12:31:41 +010010773void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010774{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010775 struct drm_i915_private *dev_priv = dev->dev_private;
10776
Chris Wilsonf62a0072014-02-21 17:55:39 +000010777 if (dev_priv->mm.busy)
10778 return;
10779
Paulo Zanoni43694d62014-03-07 20:08:08 -030010780 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010781 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010782 if (INTEL_INFO(dev)->gen >= 6)
10783 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010784 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010785}
10786
10787void intel_mark_idle(struct drm_device *dev)
10788{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010789 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010790
Chris Wilsonf62a0072014-02-21 17:55:39 +000010791 if (!dev_priv->mm.busy)
10792 return;
10793
10794 dev_priv->mm.busy = false;
10795
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010796 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010797 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010798
Paulo Zanoni43694d62014-03-07 20:08:08 -030010799 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010800}
10801
Jesse Barnes79e53942008-11-07 14:24:08 -080010802static void intel_crtc_destroy(struct drm_crtc *crtc)
10803{
10804 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010805 struct drm_device *dev = crtc->dev;
10806 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010807
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010808 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010809 work = intel_crtc->unpin_work;
10810 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010811 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010812
10813 if (work) {
10814 cancel_work_sync(&work->work);
10815 kfree(work);
10816 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010817
10818 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010819
Jesse Barnes79e53942008-11-07 14:24:08 -080010820 kfree(intel_crtc);
10821}
10822
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010823static void intel_unpin_work_fn(struct work_struct *__work)
10824{
10825 struct intel_unpin_work *work =
10826 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010827 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10828 struct drm_device *dev = crtc->base.dev;
10829 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010830
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010831 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020010832 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilson05394f32010-11-08 19:18:58 +000010833 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010834
John Harrisonf06cc1b2014-11-24 18:49:37 +000010835 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010836 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010837 mutex_unlock(&dev->struct_mutex);
10838
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010839 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanoni1eb52232016-01-19 11:35:44 -020010840 intel_fbc_post_update(crtc);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010841 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010842
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010843 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10844 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010845
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010846 kfree(work);
10847}
10848
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010849static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010850 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010851{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010852 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10853 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010854 unsigned long flags;
10855
10856 /* Ignore early vblank irqs */
10857 if (intel_crtc == NULL)
10858 return;
10859
Daniel Vetterf3260382014-09-15 14:55:23 +020010860 /*
10861 * This is called both by irq handlers and the reset code (to complete
10862 * lost pageflips) so needs the full irqsave spinlocks.
10863 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010864 spin_lock_irqsave(&dev->event_lock, flags);
10865 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010866
10867 /* Ensure we don't miss a work->pending update ... */
10868 smp_rmb();
10869
10870 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010871 spin_unlock_irqrestore(&dev->event_lock, flags);
10872 return;
10873 }
10874
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010875 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010876
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010877 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010878}
10879
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010880void intel_finish_page_flip(struct drm_device *dev, int pipe)
10881{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010882 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010883 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10884
Mario Kleiner49b14a52010-12-09 07:00:07 +010010885 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010886}
10887
10888void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10889{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010890 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010891 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10892
Mario Kleiner49b14a52010-12-09 07:00:07 +010010893 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010894}
10895
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010896/* Is 'a' after or equal to 'b'? */
10897static bool g4x_flip_count_after_eq(u32 a, u32 b)
10898{
10899 return !((a - b) & 0x80000000);
10900}
10901
10902static bool page_flip_finished(struct intel_crtc *crtc)
10903{
10904 struct drm_device *dev = crtc->base.dev;
10905 struct drm_i915_private *dev_priv = dev->dev_private;
10906
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010907 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10908 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10909 return true;
10910
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010911 /*
10912 * The relevant registers doen't exist on pre-ctg.
10913 * As the flip done interrupt doesn't trigger for mmio
10914 * flips on gmch platforms, a flip count check isn't
10915 * really needed there. But since ctg has the registers,
10916 * include it in the check anyway.
10917 */
10918 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10919 return true;
10920
10921 /*
Maarten Lankhorste8861672016-02-24 11:24:26 +010010922 * BDW signals flip done immediately if the plane
10923 * is disabled, even if the plane enable is already
10924 * armed to occur at the next vblank :(
10925 */
10926
10927 /*
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010928 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10929 * used the same base address. In that case the mmio flip might
10930 * have completed, but the CS hasn't even executed the flip yet.
10931 *
10932 * A flip count check isn't enough as the CS might have updated
10933 * the base address just after start of vblank, but before we
10934 * managed to process the interrupt. This means we'd complete the
10935 * CS flip too soon.
10936 *
10937 * Combining both checks should get us a good enough result. It may
10938 * still happen that the CS flip has been executed, but has not
10939 * yet actually completed. But in case the base address is the same
10940 * anyway, we don't really care.
10941 */
10942 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10943 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030010944 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010945 crtc->unpin_work->flip_count);
10946}
10947
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010948void intel_prepare_page_flip(struct drm_device *dev, int plane)
10949{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010950 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010951 struct intel_crtc *intel_crtc =
10952 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10953 unsigned long flags;
10954
Daniel Vetterf3260382014-09-15 14:55:23 +020010955
10956 /*
10957 * This is called both by irq handlers and the reset code (to complete
10958 * lost pageflips) so needs the full irqsave spinlocks.
10959 *
10960 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010961 * generate a page-flip completion irq, i.e. every modeset
10962 * is also accompanied by a spurious intel_prepare_page_flip().
10963 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010964 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010965 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010966 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010967 spin_unlock_irqrestore(&dev->event_lock, flags);
10968}
10969
Chris Wilson60426392015-10-10 10:44:32 +010010970static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010971{
10972 /* Ensure that the work item is consistent when activating it ... */
10973 smp_wmb();
Chris Wilson60426392015-10-10 10:44:32 +010010974 atomic_set(&work->pending, INTEL_FLIP_PENDING);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010975 /* and that it is marked active as soon as the irq could fire. */
10976 smp_wmb();
10977}
10978
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010979static int intel_gen2_queue_flip(struct drm_device *dev,
10980 struct drm_crtc *crtc,
10981 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010982 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010983 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010984 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010985{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010986 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010987 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010988 u32 flip_mask;
10989 int ret;
10990
John Harrison5fb9de12015-05-29 17:44:07 +010010991 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010992 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010993 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010994
10995 /* Can't queue multiple flips, so wait for the previous
10996 * one to finish before executing the next.
10997 */
10998 if (intel_crtc->plane)
10999 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11000 else
11001 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011002 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11003 intel_ring_emit(engine, MI_NOOP);
11004 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011005 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011006 intel_ring_emit(engine, fb->pitches[0]);
11007 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11008 intel_ring_emit(engine, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000011009
Chris Wilson60426392015-10-10 10:44:32 +010011010 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011011 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011012}
11013
11014static int intel_gen3_queue_flip(struct drm_device *dev,
11015 struct drm_crtc *crtc,
11016 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011017 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011018 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011019 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011020{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011021 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011022 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011023 u32 flip_mask;
11024 int ret;
11025
John Harrison5fb9de12015-05-29 17:44:07 +010011026 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011027 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011028 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011029
11030 if (intel_crtc->plane)
11031 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11032 else
11033 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011034 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11035 intel_ring_emit(engine, MI_NOOP);
11036 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011037 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011038 intel_ring_emit(engine, fb->pitches[0]);
11039 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11040 intel_ring_emit(engine, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011041
Chris Wilson60426392015-10-10 10:44:32 +010011042 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011043 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011044}
11045
11046static int intel_gen4_queue_flip(struct drm_device *dev,
11047 struct drm_crtc *crtc,
11048 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011049 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011050 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011051 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011052{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011053 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011054 struct drm_i915_private *dev_priv = dev->dev_private;
11055 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11056 uint32_t pf, pipesrc;
11057 int ret;
11058
John Harrison5fb9de12015-05-29 17:44:07 +010011059 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011060 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011061 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011062
11063 /* i965+ uses the linear or tiled offsets from the
11064 * Display Registers (which do not change across a page-flip)
11065 * so we need only reprogram the base address.
11066 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011067 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011068 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011069 intel_ring_emit(engine, fb->pitches[0]);
11070 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011071 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011072
11073 /* XXX Enabling the panel-fitter across page-flip is so far
11074 * untested on non-native modes, so ignore it for now.
11075 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11076 */
11077 pf = 0;
11078 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011079 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011080
Chris Wilson60426392015-10-10 10:44:32 +010011081 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011082 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011083}
11084
11085static int intel_gen6_queue_flip(struct drm_device *dev,
11086 struct drm_crtc *crtc,
11087 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011088 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011089 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011090 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011091{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011092 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011093 struct drm_i915_private *dev_priv = dev->dev_private;
11094 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11095 uint32_t pf, pipesrc;
11096 int ret;
11097
John Harrison5fb9de12015-05-29 17:44:07 +010011098 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011099 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011100 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011101
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011102 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011103 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011104 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11105 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011106
Chris Wilson99d9acd2012-04-17 20:37:00 +010011107 /* Contrary to the suggestions in the documentation,
11108 * "Enable Panel Fitter" does not seem to be required when page
11109 * flipping with a non-native mode, and worse causes a normal
11110 * modeset to fail.
11111 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11112 */
11113 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011114 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011115 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011116
Chris Wilson60426392015-10-10 10:44:32 +010011117 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011118 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011119}
11120
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011121static int intel_gen7_queue_flip(struct drm_device *dev,
11122 struct drm_crtc *crtc,
11123 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011124 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011125 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011126 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011127{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011128 struct intel_engine_cs *engine = req->engine;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011129 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011130 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011131 int len, ret;
11132
Robin Schroereba905b2014-05-18 02:24:50 +020011133 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011134 case PLANE_A:
11135 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11136 break;
11137 case PLANE_B:
11138 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11139 break;
11140 case PLANE_C:
11141 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11142 break;
11143 default:
11144 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011145 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011146 }
11147
Chris Wilsonffe74d72013-08-26 20:58:12 +010011148 len = 4;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011149 if (engine->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011150 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011151 /*
11152 * On Gen 8, SRM is now taking an extra dword to accommodate
11153 * 48bits addresses, and we need a NOOP for the batch size to
11154 * stay even.
11155 */
11156 if (IS_GEN8(dev))
11157 len += 2;
11158 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011159
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011160 /*
11161 * BSpec MI_DISPLAY_FLIP for IVB:
11162 * "The full packet must be contained within the same cache line."
11163 *
11164 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11165 * cacheline, if we ever start emitting more commands before
11166 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11167 * then do the cacheline alignment, and finally emit the
11168 * MI_DISPLAY_FLIP.
11169 */
John Harrisonbba09b12015-05-29 17:44:06 +010011170 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011171 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011172 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011173
John Harrison5fb9de12015-05-29 17:44:07 +010011174 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011175 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011176 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011177
Chris Wilsonffe74d72013-08-26 20:58:12 +010011178 /* Unmask the flip-done completion message. Note that the bspec says that
11179 * we should do this for both the BCS and RCS, and that we must not unmask
11180 * more than one flip event at any time (or ensure that one flip message
11181 * can be sent by waiting for flip-done prior to queueing new flips).
11182 * Experimentation says that BCS works despite DERRMR masking all
11183 * flip-done completion events and that unmasking all planes at once
11184 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11185 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11186 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011187 if (engine->id == RCS) {
11188 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11189 intel_ring_emit_reg(engine, DERRMR);
11190 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11191 DERRMR_PIPEB_PRI_FLIP_DONE |
11192 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011193 if (IS_GEN8(dev))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011194 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011195 MI_SRM_LRM_GLOBAL_GTT);
11196 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011197 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011198 MI_SRM_LRM_GLOBAL_GTT);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011199 intel_ring_emit_reg(engine, DERRMR);
11200 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011201 if (IS_GEN8(dev)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011202 intel_ring_emit(engine, 0);
11203 intel_ring_emit(engine, MI_NOOP);
Damien Lespiauf4768282014-04-07 20:24:34 +010011204 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011205 }
11206
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011207 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11208 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11209 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11210 intel_ring_emit(engine, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011211
Chris Wilson60426392015-10-10 10:44:32 +010011212 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011213 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011214}
11215
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011216static bool use_mmio_flip(struct intel_engine_cs *engine,
Sourab Gupta84c33a62014-06-02 16:47:17 +053011217 struct drm_i915_gem_object *obj)
11218{
11219 /*
11220 * This is not being used for older platforms, because
11221 * non-availability of flip done interrupt forces us to use
11222 * CS flips. Older platforms derive flip done using some clever
11223 * tricks involving the flip_pending status bits and vblank irqs.
11224 * So using MMIO flips there would disrupt this mechanism.
11225 */
11226
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011227 if (engine == NULL)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011228 return true;
11229
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011230 if (INTEL_INFO(engine->dev)->gen < 5)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011231 return false;
11232
11233 if (i915.use_mmio_flip < 0)
11234 return false;
11235 else if (i915.use_mmio_flip > 0)
11236 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011237 else if (i915.enable_execlists)
11238 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011239 else if (obj->base.dma_buf &&
11240 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11241 false))
11242 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011243 else
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011244 return engine != i915_gem_request_get_engine(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011245}
11246
Chris Wilson60426392015-10-10 10:44:32 +010011247static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011248 unsigned int rotation,
Chris Wilson60426392015-10-10 10:44:32 +010011249 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011250{
11251 struct drm_device *dev = intel_crtc->base.dev;
11252 struct drm_i915_private *dev_priv = dev->dev_private;
11253 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011254 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011255 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011256
11257 ctl = I915_READ(PLANE_CTL(pipe, 0));
11258 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011259 switch (fb->modifier[0]) {
11260 case DRM_FORMAT_MOD_NONE:
11261 break;
11262 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011263 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011264 break;
11265 case I915_FORMAT_MOD_Y_TILED:
11266 ctl |= PLANE_CTL_TILED_Y;
11267 break;
11268 case I915_FORMAT_MOD_Yf_TILED:
11269 ctl |= PLANE_CTL_TILED_YF;
11270 break;
11271 default:
11272 MISSING_CASE(fb->modifier[0]);
11273 }
Damien Lespiauff944562014-11-20 14:58:16 +000011274
11275 /*
11276 * The stride is either expressed as a multiple of 64 bytes chunks for
11277 * linear buffers or in number of tiles for tiled buffers.
11278 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011279 if (intel_rotation_90_or_270(rotation)) {
11280 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +020011281 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011282 stride = DIV_ROUND_UP(fb->height, tile_height);
11283 } else {
11284 stride = fb->pitches[0] /
Ville Syrjälä7b49f942016-01-12 21:08:32 +020011285 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11286 fb->pixel_format);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011287 }
Damien Lespiauff944562014-11-20 14:58:16 +000011288
11289 /*
11290 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11291 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11292 */
11293 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11294 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11295
Chris Wilson60426392015-10-10 10:44:32 +010011296 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011297 POSTING_READ(PLANE_SURF(pipe, 0));
11298}
11299
Chris Wilson60426392015-10-10 10:44:32 +010011300static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11301 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011302{
11303 struct drm_device *dev = intel_crtc->base.dev;
11304 struct drm_i915_private *dev_priv = dev->dev_private;
11305 struct intel_framebuffer *intel_fb =
11306 to_intel_framebuffer(intel_crtc->base.primary->fb);
11307 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011308 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011309 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011310
Sourab Gupta84c33a62014-06-02 16:47:17 +053011311 dspcntr = I915_READ(reg);
11312
Damien Lespiauc5d97472014-10-25 00:11:11 +010011313 if (obj->tiling_mode != I915_TILING_NONE)
11314 dspcntr |= DISPPLANE_TILED;
11315 else
11316 dspcntr &= ~DISPPLANE_TILED;
11317
Sourab Gupta84c33a62014-06-02 16:47:17 +053011318 I915_WRITE(reg, dspcntr);
11319
Chris Wilson60426392015-10-10 10:44:32 +010011320 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011321 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011322}
11323
11324/*
11325 * XXX: This is the temporary way to update the plane registers until we get
11326 * around to using the usual plane update functions for MMIO flips
11327 */
Chris Wilson60426392015-10-10 10:44:32 +010011328static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011329{
Chris Wilson60426392015-10-10 10:44:32 +010011330 struct intel_crtc *crtc = mmio_flip->crtc;
11331 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011332
Chris Wilson60426392015-10-10 10:44:32 +010011333 spin_lock_irq(&crtc->base.dev->event_lock);
11334 work = crtc->unpin_work;
11335 spin_unlock_irq(&crtc->base.dev->event_lock);
11336 if (work == NULL)
11337 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011338
Chris Wilson60426392015-10-10 10:44:32 +010011339 intel_mark_page_flip_active(work);
Damien Lespiauff944562014-11-20 14:58:16 +000011340
Chris Wilson60426392015-10-10 10:44:32 +010011341 intel_pipe_update_start(crtc);
11342
11343 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011344 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011345 else
11346 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011347 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011348
Chris Wilson60426392015-10-10 10:44:32 +010011349 intel_pipe_update_end(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011350}
11351
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011352static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011353{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011354 struct intel_mmio_flip *mmio_flip =
11355 container_of(work, struct intel_mmio_flip, work);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011356 struct intel_framebuffer *intel_fb =
11357 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11358 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011359
Chris Wilson60426392015-10-10 10:44:32 +010011360 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011361 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011362 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011363 false, NULL,
11364 &mmio_flip->i915->rps.mmioflips));
Chris Wilson60426392015-10-10 10:44:32 +010011365 i915_gem_request_unreference__unlocked(mmio_flip->req);
11366 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011367
Alex Goinsfd8e0582015-11-25 18:43:38 -080011368 /* For framebuffer backed by dmabuf, wait for fence */
11369 if (obj->base.dma_buf)
11370 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11371 false, false,
11372 MAX_SCHEDULE_TIMEOUT) < 0);
11373
Chris Wilson60426392015-10-10 10:44:32 +010011374 intel_do_mmio_flip(mmio_flip);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011375 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011376}
11377
11378static int intel_queue_mmio_flip(struct drm_device *dev,
11379 struct drm_crtc *crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011380 struct drm_i915_gem_object *obj)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011381{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011382 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011383
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011384 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11385 if (mmio_flip == NULL)
11386 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011387
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011388 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011389 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011390 mmio_flip->crtc = to_intel_crtc(crtc);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011391 mmio_flip->rotation = crtc->primary->state->rotation;
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011392
11393 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11394 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011395
Sourab Gupta84c33a62014-06-02 16:47:17 +053011396 return 0;
11397}
11398
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011399static int intel_default_queue_flip(struct drm_device *dev,
11400 struct drm_crtc *crtc,
11401 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011402 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011403 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011404 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011405{
11406 return -ENODEV;
11407}
11408
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011409static bool __intel_pageflip_stall_check(struct drm_device *dev,
11410 struct drm_crtc *crtc)
11411{
11412 struct drm_i915_private *dev_priv = dev->dev_private;
11413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11414 struct intel_unpin_work *work = intel_crtc->unpin_work;
11415 u32 addr;
11416
11417 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11418 return true;
11419
Chris Wilson908565c2015-08-12 13:08:22 +010011420 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11421 return false;
11422
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011423 if (!work->enable_stall_check)
11424 return false;
11425
11426 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011427 if (work->flip_queued_req &&
11428 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011429 return false;
11430
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011431 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011432 }
11433
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011434 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011435 return false;
11436
11437 /* Potential stall - if we see that the flip has happened,
11438 * assume a missed interrupt. */
11439 if (INTEL_INFO(dev)->gen >= 4)
11440 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11441 else
11442 addr = I915_READ(DSPADDR(intel_crtc->plane));
11443
11444 /* There is a potential issue here with a false positive after a flip
11445 * to the same address. We could address this by checking for a
11446 * non-incrementing frame counter.
11447 */
11448 return addr == work->gtt_offset;
11449}
11450
11451void intel_check_page_flip(struct drm_device *dev, int pipe)
11452{
11453 struct drm_i915_private *dev_priv = dev->dev_private;
11454 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11455 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011456 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011457
Dave Gordon6c51d462015-03-06 15:34:26 +000011458 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011459
11460 if (crtc == NULL)
11461 return;
11462
Daniel Vetterf3260382014-09-15 14:55:23 +020011463 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011464 work = intel_crtc->unpin_work;
11465 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011466 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011467 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011468 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011469 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011470 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011471 if (work != NULL &&
11472 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11473 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011474 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011475}
11476
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011477static int intel_crtc_page_flip(struct drm_crtc *crtc,
11478 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011479 struct drm_pending_vblank_event *event,
11480 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011481{
11482 struct drm_device *dev = crtc->dev;
11483 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011484 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011485 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011486 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011487 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011488 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011489 struct intel_unpin_work *work;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011490 struct intel_engine_cs *engine;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011491 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011492 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011493 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011494
Matt Roper2ff8fde2014-07-08 07:50:07 -070011495 /*
11496 * drm_mode_page_flip_ioctl() should already catch this, but double
11497 * check to be safe. In the future we may enable pageflipping from
11498 * a disabled primary plane.
11499 */
11500 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11501 return -EBUSY;
11502
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011503 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011504 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011505 return -EINVAL;
11506
11507 /*
11508 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11509 * Note that pitch changes could also affect these register.
11510 */
11511 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011512 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11513 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011514 return -EINVAL;
11515
Chris Wilsonf900db42014-02-20 09:26:13 +000011516 if (i915_terminally_wedged(&dev_priv->gpu_error))
11517 goto out_hang;
11518
Daniel Vetterb14c5672013-09-19 12:18:32 +020011519 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011520 if (work == NULL)
11521 return -ENOMEM;
11522
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011523 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011524 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011525 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011526 INIT_WORK(&work->work, intel_unpin_work_fn);
11527
Daniel Vetter87b6b102014-05-15 15:33:46 +020011528 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011529 if (ret)
11530 goto free_work;
11531
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011532 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011533 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011534 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011535 /* Before declaring the flip queue wedged, check if
11536 * the hardware completed the operation behind our backs.
11537 */
11538 if (__intel_pageflip_stall_check(dev, crtc)) {
11539 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11540 page_flip_completed(intel_crtc);
11541 } else {
11542 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011543 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011544
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011545 drm_crtc_vblank_put(crtc);
11546 kfree(work);
11547 return -EBUSY;
11548 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011549 }
11550 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011551 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011552
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011553 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11554 flush_workqueue(dev_priv->wq);
11555
Jesse Barnes75dfca82010-02-10 15:09:44 -080011556 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011557 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011558 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011559
Matt Roperf4510a22014-04-01 15:22:40 -070011560 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011561 update_state_fb(crtc->primary);
Paulo Zanonie8216e52016-01-19 11:35:56 -020011562 intel_fbc_pre_update(intel_crtc);
Matt Roper1ed1f962015-01-30 16:22:36 -080011563
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011564 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011565
Chris Wilson89ed88b2015-02-16 14:31:49 +000011566 ret = i915_mutex_lock_interruptible(dev);
11567 if (ret)
11568 goto cleanup;
11569
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011570 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011571 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011572
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011573 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030011574 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011575
Wayne Boyer666a4532015-12-09 12:29:35 -080011576 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011577 engine = &dev_priv->engine[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011578 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011579 /* vlv: DISPLAY_FLIP fails to change tiling */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011580 engine = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011581 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011582 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011583 } else if (INTEL_INFO(dev)->gen >= 7) {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011584 engine = i915_gem_request_get_engine(obj->last_write_req);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011585 if (engine == NULL || engine->id != RCS)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011586 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011587 } else {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011588 engine = &dev_priv->engine[RCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011589 }
11590
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011591 mmio_flip = use_mmio_flip(engine, obj);
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011592
11593 /* When using CS flips, we want to emit semaphores between rings.
11594 * However, when using mmio flips we will create a task to do the
11595 * synchronisation, so all we want here is to pin the framebuffer
11596 * into the display plane and skip any waits.
11597 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011598 if (!mmio_flip) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011599 ret = i915_gem_object_sync(obj, engine, &request);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011600 if (ret)
11601 goto cleanup_pending;
11602 }
11603
Ville Syrjälä3465c582016-02-15 22:54:43 +020011604 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011605 if (ret)
11606 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011607
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011608 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11609 obj, 0);
11610 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011611
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011612 if (mmio_flip) {
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011613 ret = intel_queue_mmio_flip(dev, crtc, obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011614 if (ret)
11615 goto cleanup_unpin;
11616
John Harrisonf06cc1b2014-11-24 18:49:37 +000011617 i915_gem_request_assign(&work->flip_queued_req,
11618 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011619 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011620 if (!request) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011621 request = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +000011622 if (IS_ERR(request)) {
11623 ret = PTR_ERR(request);
John Harrison6258fbe2015-05-29 17:43:48 +010011624 goto cleanup_unpin;
Dave Gordon26827082016-01-19 19:02:53 +000011625 }
John Harrison6258fbe2015-05-29 17:43:48 +010011626 }
11627
11628 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011629 page_flip_flags);
11630 if (ret)
11631 goto cleanup_unpin;
11632
John Harrison6258fbe2015-05-29 17:43:48 +010011633 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011634 }
11635
John Harrison91af1272015-06-18 13:14:56 +010011636 if (request)
John Harrison75289872015-05-29 17:43:49 +010011637 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011638
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011639 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011640 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011641
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011642 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011643 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011644 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011645
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011646 intel_frontbuffer_flip_prepare(dev,
11647 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011648
Jesse Barnese5510fa2010-07-01 16:48:37 -070011649 trace_i915_flip_request(intel_crtc->plane, obj);
11650
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011651 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011652
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011653cleanup_unpin:
Ville Syrjälä3465c582016-02-15 22:54:43 +020011654 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011655cleanup_pending:
Dave Gordon0aa498d2016-01-28 10:48:09 +000011656 if (!IS_ERR_OR_NULL(request))
John Harrison91af1272015-06-18 13:14:56 +010011657 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011658 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011659 mutex_unlock(&dev->struct_mutex);
11660cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011661 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011662 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011663
Chris Wilson89ed88b2015-02-16 14:31:49 +000011664 drm_gem_object_unreference_unlocked(&obj->base);
11665 drm_framebuffer_unreference(work->old_fb);
11666
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011667 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011668 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011669 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011670
Daniel Vetter87b6b102014-05-15 15:33:46 +020011671 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011672free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011673 kfree(work);
11674
Chris Wilsonf900db42014-02-20 09:26:13 +000011675 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011676 struct drm_atomic_state *state;
11677 struct drm_plane_state *plane_state;
11678
Chris Wilsonf900db42014-02-20 09:26:13 +000011679out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011680 state = drm_atomic_state_alloc(dev);
11681 if (!state)
11682 return -ENOMEM;
11683 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11684
11685retry:
11686 plane_state = drm_atomic_get_plane_state(state, primary);
11687 ret = PTR_ERR_OR_ZERO(plane_state);
11688 if (!ret) {
11689 drm_atomic_set_fb_for_plane(plane_state, fb);
11690
11691 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11692 if (!ret)
11693 ret = drm_atomic_commit(state);
11694 }
11695
11696 if (ret == -EDEADLK) {
11697 drm_modeset_backoff(state->acquire_ctx);
11698 drm_atomic_state_clear(state);
11699 goto retry;
11700 }
11701
11702 if (ret)
11703 drm_atomic_state_free(state);
11704
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011705 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011706 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011707 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011708 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011709 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011710 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011711 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011712}
11713
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011714
11715/**
11716 * intel_wm_need_update - Check whether watermarks need updating
11717 * @plane: drm plane
11718 * @state: new plane state
11719 *
11720 * Check current plane state versus the new one to determine whether
11721 * watermarks need to be recalculated.
11722 *
11723 * Returns true or false.
11724 */
11725static bool intel_wm_need_update(struct drm_plane *plane,
11726 struct drm_plane_state *state)
11727{
Matt Roperd21fbe82015-09-24 15:53:12 -070011728 struct intel_plane_state *new = to_intel_plane_state(state);
11729 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11730
11731 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011732 if (new->visible != cur->visible)
11733 return true;
11734
11735 if (!cur->base.fb || !new->base.fb)
11736 return false;
11737
11738 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11739 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011740 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11741 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11742 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11743 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011744 return true;
11745
11746 return false;
11747}
11748
Matt Roperd21fbe82015-09-24 15:53:12 -070011749static bool needs_scaling(struct intel_plane_state *state)
11750{
11751 int src_w = drm_rect_width(&state->src) >> 16;
11752 int src_h = drm_rect_height(&state->src) >> 16;
11753 int dst_w = drm_rect_width(&state->dst);
11754 int dst_h = drm_rect_height(&state->dst);
11755
11756 return (src_w != dst_w || src_h != dst_h);
11757}
11758
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011759int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11760 struct drm_plane_state *plane_state)
11761{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011762 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011763 struct drm_crtc *crtc = crtc_state->crtc;
11764 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11765 struct drm_plane *plane = plane_state->plane;
11766 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011767 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011768 struct intel_plane_state *old_plane_state =
11769 to_intel_plane_state(plane->state);
11770 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011771 bool mode_changed = needs_modeset(crtc_state);
11772 bool was_crtc_enabled = crtc->state->active;
11773 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011774 bool turn_off, turn_on, visible, was_visible;
11775 struct drm_framebuffer *fb = plane_state->fb;
11776
11777 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11778 plane->type != DRM_PLANE_TYPE_CURSOR) {
11779 ret = skl_update_scaler_plane(
11780 to_intel_crtc_state(crtc_state),
11781 to_intel_plane_state(plane_state));
11782 if (ret)
11783 return ret;
11784 }
11785
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011786 was_visible = old_plane_state->visible;
11787 visible = to_intel_plane_state(plane_state)->visible;
11788
11789 if (!was_crtc_enabled && WARN_ON(was_visible))
11790 was_visible = false;
11791
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011792 /*
11793 * Visibility is calculated as if the crtc was on, but
11794 * after scaler setup everything depends on it being off
11795 * when the crtc isn't active.
11796 */
11797 if (!is_crtc_enabled)
11798 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011799
11800 if (!was_visible && !visible)
11801 return 0;
11802
Maarten Lankhorste8861672016-02-24 11:24:26 +010011803 if (fb != old_plane_state->base.fb)
11804 pipe_config->fb_changed = true;
11805
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011806 turn_off = was_visible && (!visible || mode_changed);
11807 turn_on = visible && (!was_visible || mode_changed);
11808
11809 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11810 plane->base.id, fb ? fb->base.id : -1);
11811
11812 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11813 plane->base.id, was_visible, visible,
11814 turn_off, turn_on, mode_changed);
11815
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011816 if (turn_on) {
11817 pipe_config->update_wm_pre = true;
11818
11819 /* must disable cxsr around plane enable/disable */
11820 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11821 pipe_config->disable_cxsr = true;
11822 } else if (turn_off) {
11823 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011824
Ville Syrjälä852eb002015-06-24 22:00:07 +030011825 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011826 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011827 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011828 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011829 /* FIXME bollocks */
11830 pipe_config->update_wm_pre = true;
11831 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011832 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011833
Matt Ropered4a6a72016-02-23 17:20:13 -080011834 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011835 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11836 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011837 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11838
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011839 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011840 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011841
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011842 /*
11843 * WaCxSRDisabledForSpriteScaling:ivb
11844 *
11845 * cstate->update_wm was already set above, so this flag will
11846 * take effect when we commit and program watermarks.
11847 */
11848 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11849 needs_scaling(to_intel_plane_state(plane_state)) &&
11850 !needs_scaling(old_plane_state))
11851 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011852
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011853 return 0;
11854}
11855
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011856static bool encoders_cloneable(const struct intel_encoder *a,
11857 const struct intel_encoder *b)
11858{
11859 /* masks could be asymmetric, so check both ways */
11860 return a == b || (a->cloneable & (1 << b->type) &&
11861 b->cloneable & (1 << a->type));
11862}
11863
11864static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11865 struct intel_crtc *crtc,
11866 struct intel_encoder *encoder)
11867{
11868 struct intel_encoder *source_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 source_encoder =
11878 to_intel_encoder(connector_state->best_encoder);
11879 if (!encoders_cloneable(encoder, source_encoder))
11880 return false;
11881 }
11882
11883 return true;
11884}
11885
11886static bool check_encoder_cloning(struct drm_atomic_state *state,
11887 struct intel_crtc *crtc)
11888{
11889 struct intel_encoder *encoder;
11890 struct drm_connector *connector;
11891 struct drm_connector_state *connector_state;
11892 int i;
11893
11894 for_each_connector_in_state(state, connector, connector_state, i) {
11895 if (connector_state->crtc != &crtc->base)
11896 continue;
11897
11898 encoder = to_intel_encoder(connector_state->best_encoder);
11899 if (!check_single_encoder_cloning(state, crtc, encoder))
11900 return false;
11901 }
11902
11903 return true;
11904}
11905
11906static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11907 struct drm_crtc_state *crtc_state)
11908{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011909 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011910 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011911 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011912 struct intel_crtc_state *pipe_config =
11913 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011914 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011915 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011916 bool mode_changed = needs_modeset(crtc_state);
11917
11918 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11919 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11920 return -EINVAL;
11921 }
11922
Ville Syrjälä852eb002015-06-24 22:00:07 +030011923 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011924 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011925
Maarten Lankhorstad421372015-06-15 12:33:42 +020011926 if (mode_changed && crtc_state->enable &&
11927 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011928 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020011929 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11930 pipe_config);
11931 if (ret)
11932 return ret;
11933 }
11934
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000011935 if (crtc_state->color_mgmt_changed) {
11936 ret = intel_color_check(crtc, crtc_state);
11937 if (ret)
11938 return ret;
11939 }
11940
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011941 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011942 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010011943 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080011944 if (ret) {
11945 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070011946 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080011947 }
11948 }
11949
11950 if (dev_priv->display.compute_intermediate_wm &&
11951 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11952 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11953 return 0;
11954
11955 /*
11956 * Calculate 'intermediate' watermarks that satisfy both the
11957 * old state and the new state. We can program these
11958 * immediately.
11959 */
11960 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11961 intel_crtc,
11962 pipe_config);
11963 if (ret) {
11964 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
11965 return ret;
11966 }
Matt Roper86c8bbb2015-09-24 15:53:16 -070011967 }
11968
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011969 if (INTEL_INFO(dev)->gen >= 9) {
11970 if (mode_changed)
11971 ret = skl_update_scaler_crtc(pipe_config);
11972
11973 if (!ret)
11974 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11975 pipe_config);
11976 }
11977
11978 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011979}
11980
Jani Nikula65b38e02015-04-13 11:26:56 +030011981static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011982 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Matt Roperea2c67b2014-12-23 10:41:52 -080011983 .atomic_begin = intel_begin_crtc_commit,
11984 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011985 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011986};
11987
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011988static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11989{
11990 struct intel_connector *connector;
11991
11992 for_each_intel_connector(dev, connector) {
11993 if (connector->base.encoder) {
11994 connector->base.state->best_encoder =
11995 connector->base.encoder;
11996 connector->base.state->crtc =
11997 connector->base.encoder->crtc;
11998 } else {
11999 connector->base.state->best_encoder = NULL;
12000 connector->base.state->crtc = NULL;
12001 }
12002 }
12003}
12004
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012005static void
Robin Schroereba905b2014-05-18 02:24:50 +020012006connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012007 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012008{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012009 int bpp = pipe_config->pipe_bpp;
12010
12011 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12012 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012013 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012014
12015 /* Don't use an invalid EDID bpc value */
12016 if (connector->base.display_info.bpc &&
12017 connector->base.display_info.bpc * 3 < bpp) {
12018 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12019 bpp, connector->base.display_info.bpc*3);
12020 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12021 }
12022
Jani Nikula013dd9e2016-01-13 16:35:20 +020012023 /* Clamp bpp to default limit on screens without EDID 1.4 */
12024 if (connector->base.display_info.bpc == 0) {
12025 int type = connector->base.connector_type;
12026 int clamp_bpp = 24;
12027
12028 /* Fall back to 18 bpp when DP sink capability is unknown. */
12029 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12030 type == DRM_MODE_CONNECTOR_eDP)
12031 clamp_bpp = 18;
12032
12033 if (bpp > clamp_bpp) {
12034 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12035 bpp, clamp_bpp);
12036 pipe_config->pipe_bpp = clamp_bpp;
12037 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012038 }
12039}
12040
12041static int
12042compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012043 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012044{
12045 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012046 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012047 struct drm_connector *connector;
12048 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012049 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012050
Wayne Boyer666a4532015-12-09 12:29:35 -080012051 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012052 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012053 else if (INTEL_INFO(dev)->gen >= 5)
12054 bpp = 12*3;
12055 else
12056 bpp = 8*3;
12057
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012058
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012059 pipe_config->pipe_bpp = bpp;
12060
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012061 state = pipe_config->base.state;
12062
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012063 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012064 for_each_connector_in_state(state, connector, connector_state, i) {
12065 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012066 continue;
12067
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012068 connected_sink_compute_bpp(to_intel_connector(connector),
12069 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012070 }
12071
12072 return bpp;
12073}
12074
Daniel Vetter644db712013-09-19 14:53:58 +020012075static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12076{
12077 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12078 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012079 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012080 mode->crtc_hdisplay, mode->crtc_hsync_start,
12081 mode->crtc_hsync_end, mode->crtc_htotal,
12082 mode->crtc_vdisplay, mode->crtc_vsync_start,
12083 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12084}
12085
Daniel Vetterc0b03412013-05-28 12:05:54 +020012086static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012087 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012088 const char *context)
12089{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012090 struct drm_device *dev = crtc->base.dev;
12091 struct drm_plane *plane;
12092 struct intel_plane *intel_plane;
12093 struct intel_plane_state *state;
12094 struct drm_framebuffer *fb;
12095
12096 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12097 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012098
Jani Nikulada205632016-03-15 21:51:10 +020012099 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012100 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12101 pipe_config->pipe_bpp, pipe_config->dither);
12102 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12103 pipe_config->has_pch_encoder,
12104 pipe_config->fdi_lanes,
12105 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12106 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12107 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012108 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 +030012109 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012110 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012111 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12112 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12113 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012114
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012115 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 -070012116 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012117 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012118 pipe_config->dp_m2_n2.gmch_m,
12119 pipe_config->dp_m2_n2.gmch_n,
12120 pipe_config->dp_m2_n2.link_m,
12121 pipe_config->dp_m2_n2.link_n,
12122 pipe_config->dp_m2_n2.tu);
12123
Daniel Vetter55072d12014-11-20 16:10:28 +010012124 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12125 pipe_config->has_audio,
12126 pipe_config->has_infoframe);
12127
Daniel Vetterc0b03412013-05-28 12:05:54 +020012128 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012129 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012130 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012131 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12132 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012133 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012134 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12135 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012136 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12137 crtc->num_scalers,
12138 pipe_config->scaler_state.scaler_users,
12139 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012140 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12141 pipe_config->gmch_pfit.control,
12142 pipe_config->gmch_pfit.pgm_ratios,
12143 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012144 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012145 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012146 pipe_config->pch_pfit.size,
12147 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012148 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012149 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012150
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012151 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012152 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012153 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012154 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012155 pipe_config->ddi_pll_sel,
12156 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012157 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012158 pipe_config->dpll_hw_state.pll0,
12159 pipe_config->dpll_hw_state.pll1,
12160 pipe_config->dpll_hw_state.pll2,
12161 pipe_config->dpll_hw_state.pll3,
12162 pipe_config->dpll_hw_state.pll6,
12163 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012164 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012165 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012166 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012167 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012168 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12169 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12170 pipe_config->ddi_pll_sel,
12171 pipe_config->dpll_hw_state.ctrl1,
12172 pipe_config->dpll_hw_state.cfgcr1,
12173 pipe_config->dpll_hw_state.cfgcr2);
12174 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012175 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 +010012176 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012177 pipe_config->dpll_hw_state.wrpll,
12178 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012179 } else {
12180 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12181 "fp0: 0x%x, fp1: 0x%x\n",
12182 pipe_config->dpll_hw_state.dpll,
12183 pipe_config->dpll_hw_state.dpll_md,
12184 pipe_config->dpll_hw_state.fp0,
12185 pipe_config->dpll_hw_state.fp1);
12186 }
12187
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012188 DRM_DEBUG_KMS("planes on this crtc\n");
12189 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12190 intel_plane = to_intel_plane(plane);
12191 if (intel_plane->pipe != crtc->pipe)
12192 continue;
12193
12194 state = to_intel_plane_state(plane->state);
12195 fb = state->base.fb;
12196 if (!fb) {
12197 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12198 "disabled, scaler_id = %d\n",
12199 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12200 plane->base.id, intel_plane->pipe,
12201 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12202 drm_plane_index(plane), state->scaler_id);
12203 continue;
12204 }
12205
12206 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12207 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12208 plane->base.id, intel_plane->pipe,
12209 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12210 drm_plane_index(plane));
12211 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12212 fb->base.id, fb->width, fb->height, fb->pixel_format);
12213 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12214 state->scaler_id,
12215 state->src.x1 >> 16, state->src.y1 >> 16,
12216 drm_rect_width(&state->src) >> 16,
12217 drm_rect_height(&state->src) >> 16,
12218 state->dst.x1, state->dst.y1,
12219 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12220 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012221}
12222
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012223static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012224{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012225 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012226 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012227 unsigned int used_ports = 0;
12228
12229 /*
12230 * Walk the connector list instead of the encoder
12231 * list to detect the problem on ddi platforms
12232 * where there's just one encoder per digital port.
12233 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012234 drm_for_each_connector(connector, dev) {
12235 struct drm_connector_state *connector_state;
12236 struct intel_encoder *encoder;
12237
12238 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12239 if (!connector_state)
12240 connector_state = connector->state;
12241
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012242 if (!connector_state->best_encoder)
12243 continue;
12244
12245 encoder = to_intel_encoder(connector_state->best_encoder);
12246
12247 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012248
12249 switch (encoder->type) {
12250 unsigned int port_mask;
12251 case INTEL_OUTPUT_UNKNOWN:
12252 if (WARN_ON(!HAS_DDI(dev)))
12253 break;
12254 case INTEL_OUTPUT_DISPLAYPORT:
12255 case INTEL_OUTPUT_HDMI:
12256 case INTEL_OUTPUT_EDP:
12257 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12258
12259 /* the same port mustn't appear more than once */
12260 if (used_ports & port_mask)
12261 return false;
12262
12263 used_ports |= port_mask;
12264 default:
12265 break;
12266 }
12267 }
12268
12269 return true;
12270}
12271
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012272static void
12273clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12274{
12275 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012276 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012277 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012278 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012279 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012280 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012281
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012282 /* FIXME: before the switch to atomic started, a new pipe_config was
12283 * kzalloc'd. Code that depends on any field being zero should be
12284 * fixed, so that the crtc_state can be safely duplicated. For now,
12285 * only fields that are know to not cause problems are preserved. */
12286
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012287 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012288 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012289 shared_dpll = crtc_state->shared_dpll;
12290 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012291 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012292 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012293
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012294 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012295
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012296 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012297 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012298 crtc_state->shared_dpll = shared_dpll;
12299 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012300 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012301 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012302}
12303
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012304static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012305intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012306 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012307{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012308 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012309 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012310 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012311 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012312 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012313 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012314 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012315
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012316 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012317
Daniel Vettere143a212013-07-04 12:01:15 +020012318 pipe_config->cpu_transcoder =
12319 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012320
Imre Deak2960bc92013-07-30 13:36:32 +030012321 /*
12322 * Sanitize sync polarity flags based on requested ones. If neither
12323 * positive or negative polarity is requested, treat this as meaning
12324 * negative polarity.
12325 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012326 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012327 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012328 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012329
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012330 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012331 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012332 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012333
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012334 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12335 pipe_config);
12336 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012337 goto fail;
12338
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012339 /*
12340 * Determine the real pipe dimensions. Note that stereo modes can
12341 * increase the actual pipe size due to the frame doubling and
12342 * insertion of additional space for blanks between the frame. This
12343 * is stored in the crtc timings. We use the requested mode to do this
12344 * computation to clearly distinguish it from the adjusted mode, which
12345 * can be changed by the connectors in the below retry loop.
12346 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012347 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012348 &pipe_config->pipe_src_w,
12349 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012350
Daniel Vettere29c22c2013-02-21 00:00:16 +010012351encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012352 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012353 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012354 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012355
Daniel Vetter135c81b2013-07-21 21:37:09 +020012356 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012357 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12358 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012359
Daniel Vetter7758a112012-07-08 19:40:39 +020012360 /* Pass our mode to the connectors and the CRTC to give them a chance to
12361 * adjust it according to limitations or connector properties, and also
12362 * a chance to reject the mode entirely.
12363 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012364 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012365 if (connector_state->crtc != crtc)
12366 continue;
12367
12368 encoder = to_intel_encoder(connector_state->best_encoder);
12369
Daniel Vetterefea6e82013-07-21 21:36:59 +020012370 if (!(encoder->compute_config(encoder, pipe_config))) {
12371 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012372 goto fail;
12373 }
12374 }
12375
Daniel Vetterff9a6752013-06-01 17:16:21 +020012376 /* Set default port clock if not overwritten by the encoder. Needs to be
12377 * done afterwards in case the encoder adjusts the mode. */
12378 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012379 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012380 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012381
Daniel Vettera43f6e02013-06-07 23:10:32 +020012382 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012383 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012384 DRM_DEBUG_KMS("CRTC fixup failed\n");
12385 goto fail;
12386 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012387
12388 if (ret == RETRY) {
12389 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12390 ret = -EINVAL;
12391 goto fail;
12392 }
12393
12394 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12395 retry = false;
12396 goto encoder_retry;
12397 }
12398
Daniel Vettere8fa4272015-08-12 11:43:34 +020012399 /* Dithering seems to not pass-through bits correctly when it should, so
12400 * only enable it on 6bpc panels. */
12401 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012402 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012403 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012404
Daniel Vetter7758a112012-07-08 19:40:39 +020012405fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012406 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012407}
12408
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012409static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012410intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012411{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012412 struct drm_crtc *crtc;
12413 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012414 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012415
Ville Syrjälä76688512014-01-10 11:28:06 +020012416 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012417 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012418 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012419
12420 /* Update hwmode for vblank functions */
12421 if (crtc->state->active)
12422 crtc->hwmode = crtc->state->adjusted_mode;
12423 else
12424 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012425
12426 /*
12427 * Update legacy state to satisfy fbc code. This can
12428 * be removed when fbc uses the atomic state.
12429 */
12430 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12431 struct drm_plane_state *plane_state = crtc->primary->state;
12432
12433 crtc->primary->fb = plane_state->fb;
12434 crtc->x = plane_state->src_x >> 16;
12435 crtc->y = plane_state->src_y >> 16;
12436 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012437 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012438}
12439
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012440static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012441{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012442 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012443
12444 if (clock1 == clock2)
12445 return true;
12446
12447 if (!clock1 || !clock2)
12448 return false;
12449
12450 diff = abs(clock1 - clock2);
12451
12452 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12453 return true;
12454
12455 return false;
12456}
12457
Daniel Vetter25c5b262012-07-08 22:08:04 +020012458#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12459 list_for_each_entry((intel_crtc), \
12460 &(dev)->mode_config.crtc_list, \
12461 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012462 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012463
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012464static bool
12465intel_compare_m_n(unsigned int m, unsigned int n,
12466 unsigned int m2, unsigned int n2,
12467 bool exact)
12468{
12469 if (m == m2 && n == n2)
12470 return true;
12471
12472 if (exact || !m || !n || !m2 || !n2)
12473 return false;
12474
12475 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12476
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012477 if (n > n2) {
12478 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012479 m2 <<= 1;
12480 n2 <<= 1;
12481 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012482 } else if (n < n2) {
12483 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012484 m <<= 1;
12485 n <<= 1;
12486 }
12487 }
12488
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012489 if (n != n2)
12490 return false;
12491
12492 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012493}
12494
12495static bool
12496intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12497 struct intel_link_m_n *m2_n2,
12498 bool adjust)
12499{
12500 if (m_n->tu == m2_n2->tu &&
12501 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12502 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12503 intel_compare_m_n(m_n->link_m, m_n->link_n,
12504 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12505 if (adjust)
12506 *m2_n2 = *m_n;
12507
12508 return true;
12509 }
12510
12511 return false;
12512}
12513
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012514static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012515intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012516 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012517 struct intel_crtc_state *pipe_config,
12518 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012519{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012520 bool ret = true;
12521
12522#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12523 do { \
12524 if (!adjust) \
12525 DRM_ERROR(fmt, ##__VA_ARGS__); \
12526 else \
12527 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12528 } while (0)
12529
Daniel Vetter66e985c2013-06-05 13:34:20 +020012530#define PIPE_CONF_CHECK_X(name) \
12531 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012532 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012533 "(expected 0x%08x, found 0x%08x)\n", \
12534 current_config->name, \
12535 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012536 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012537 }
12538
Daniel Vetter08a24032013-04-19 11:25:34 +020012539#define PIPE_CONF_CHECK_I(name) \
12540 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012541 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012542 "(expected %i, found %i)\n", \
12543 current_config->name, \
12544 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012545 ret = false; \
12546 }
12547
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012548#define PIPE_CONF_CHECK_P(name) \
12549 if (current_config->name != pipe_config->name) { \
12550 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12551 "(expected %p, found %p)\n", \
12552 current_config->name, \
12553 pipe_config->name); \
12554 ret = false; \
12555 }
12556
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012557#define PIPE_CONF_CHECK_M_N(name) \
12558 if (!intel_compare_link_m_n(&current_config->name, \
12559 &pipe_config->name,\
12560 adjust)) { \
12561 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12562 "(expected tu %i gmch %i/%i link %i/%i, " \
12563 "found tu %i, gmch %i/%i link %i/%i)\n", \
12564 current_config->name.tu, \
12565 current_config->name.gmch_m, \
12566 current_config->name.gmch_n, \
12567 current_config->name.link_m, \
12568 current_config->name.link_n, \
12569 pipe_config->name.tu, \
12570 pipe_config->name.gmch_m, \
12571 pipe_config->name.gmch_n, \
12572 pipe_config->name.link_m, \
12573 pipe_config->name.link_n); \
12574 ret = false; \
12575 }
12576
Daniel Vetter55c561a2016-03-30 11:34:36 +020012577/* This is required for BDW+ where there is only one set of registers for
12578 * switching between high and low RR.
12579 * This macro can be used whenever a comparison has to be made between one
12580 * hw state and multiple sw state variables.
12581 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012582#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12583 if (!intel_compare_link_m_n(&current_config->name, \
12584 &pipe_config->name, adjust) && \
12585 !intel_compare_link_m_n(&current_config->alt_name, \
12586 &pipe_config->name, adjust)) { \
12587 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12588 "(expected tu %i gmch %i/%i link %i/%i, " \
12589 "or tu %i gmch %i/%i link %i/%i, " \
12590 "found tu %i, gmch %i/%i link %i/%i)\n", \
12591 current_config->name.tu, \
12592 current_config->name.gmch_m, \
12593 current_config->name.gmch_n, \
12594 current_config->name.link_m, \
12595 current_config->name.link_n, \
12596 current_config->alt_name.tu, \
12597 current_config->alt_name.gmch_m, \
12598 current_config->alt_name.gmch_n, \
12599 current_config->alt_name.link_m, \
12600 current_config->alt_name.link_n, \
12601 pipe_config->name.tu, \
12602 pipe_config->name.gmch_m, \
12603 pipe_config->name.gmch_n, \
12604 pipe_config->name.link_m, \
12605 pipe_config->name.link_n); \
12606 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012607 }
12608
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012609#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12610 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012611 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012612 "(expected %i, found %i)\n", \
12613 current_config->name & (mask), \
12614 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012615 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012616 }
12617
Ville Syrjälä5e550652013-09-06 23:29:07 +030012618#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12619 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012620 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012621 "(expected %i, found %i)\n", \
12622 current_config->name, \
12623 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012624 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012625 }
12626
Daniel Vetterbb760062013-06-06 14:55:52 +020012627#define PIPE_CONF_QUIRK(quirk) \
12628 ((current_config->quirks | pipe_config->quirks) & (quirk))
12629
Daniel Vettereccb1402013-05-22 00:50:22 +020012630 PIPE_CONF_CHECK_I(cpu_transcoder);
12631
Daniel Vetter08a24032013-04-19 11:25:34 +020012632 PIPE_CONF_CHECK_I(has_pch_encoder);
12633 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012634 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012635
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012636 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012637 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012638
12639 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012640 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012641
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012642 if (current_config->has_drrs)
12643 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12644 } else
12645 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012646
Jani Nikulaa65347b2015-11-27 12:21:46 +020012647 PIPE_CONF_CHECK_I(has_dsi_encoder);
12648
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012649 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12650 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12651 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12652 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12653 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12654 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012655
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012656 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12657 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12658 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12659 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12660 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12661 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012662
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012663 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012664 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012665 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012666 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012667 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012668 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012669
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012670 PIPE_CONF_CHECK_I(has_audio);
12671
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012672 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012673 DRM_MODE_FLAG_INTERLACE);
12674
Daniel Vetterbb760062013-06-06 14:55:52 +020012675 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012676 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012677 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012678 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012679 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012680 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012681 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012682 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012683 DRM_MODE_FLAG_NVSYNC);
12684 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012685
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012686 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012687 /* pfit ratios are autocomputed by the hw on gen4+ */
12688 if (INTEL_INFO(dev)->gen < 4)
12689 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012690 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012691
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012692 if (!adjust) {
12693 PIPE_CONF_CHECK_I(pipe_src_w);
12694 PIPE_CONF_CHECK_I(pipe_src_h);
12695
12696 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12697 if (current_config->pch_pfit.enabled) {
12698 PIPE_CONF_CHECK_X(pch_pfit.pos);
12699 PIPE_CONF_CHECK_X(pch_pfit.size);
12700 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012701
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012702 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12703 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012704
Jesse Barnese59150d2014-01-07 13:30:45 -080012705 /* BDW+ don't expose a synchronous way to read the state */
12706 if (IS_HASWELL(dev))
12707 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012708
Ville Syrjälä282740f2013-09-04 18:30:03 +030012709 PIPE_CONF_CHECK_I(double_wide);
12710
Daniel Vetter26804af2014-06-25 22:01:55 +030012711 PIPE_CONF_CHECK_X(ddi_pll_sel);
12712
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012713 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012714 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012715 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012716 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12717 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012718 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012719 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012720 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12721 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12722 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012723
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012724 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12725 PIPE_CONF_CHECK_I(pipe_bpp);
12726
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012727 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012728 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012729
Daniel Vetter66e985c2013-06-05 13:34:20 +020012730#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012731#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012732#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012733#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012734#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012735#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012736#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012737
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012738 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012739}
12740
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012741static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12742 const struct intel_crtc_state *pipe_config)
12743{
12744 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012745 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012746 &pipe_config->fdi_m_n);
12747 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12748
12749 /*
12750 * FDI already provided one idea for the dotclock.
12751 * Yell if the encoder disagrees.
12752 */
12753 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12754 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12755 fdi_dotclock, dotclock);
12756 }
12757}
12758
Damien Lespiau08db6652014-11-04 17:06:52 +000012759static void check_wm_state(struct drm_device *dev)
12760{
12761 struct drm_i915_private *dev_priv = dev->dev_private;
12762 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12763 struct intel_crtc *intel_crtc;
12764 int plane;
12765
12766 if (INTEL_INFO(dev)->gen < 9)
12767 return;
12768
12769 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12770 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12771
12772 for_each_intel_crtc(dev, intel_crtc) {
12773 struct skl_ddb_entry *hw_entry, *sw_entry;
12774 const enum pipe pipe = intel_crtc->pipe;
12775
12776 if (!intel_crtc->active)
12777 continue;
12778
12779 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012780 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012781 hw_entry = &hw_ddb.plane[pipe][plane];
12782 sw_entry = &sw_ddb->plane[pipe][plane];
12783
12784 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12785 continue;
12786
12787 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12788 "(expected (%u,%u), found (%u,%u))\n",
12789 pipe_name(pipe), plane + 1,
12790 sw_entry->start, sw_entry->end,
12791 hw_entry->start, hw_entry->end);
12792 }
12793
12794 /* cursor */
Matt Roper4969d332015-09-24 15:53:10 -070012795 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12796 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
Damien Lespiau08db6652014-11-04 17:06:52 +000012797
12798 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12799 continue;
12800
12801 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12802 "(expected (%u,%u), found (%u,%u))\n",
12803 pipe_name(pipe),
12804 sw_entry->start, sw_entry->end,
12805 hw_entry->start, hw_entry->end);
12806 }
12807}
12808
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012809static void
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012810check_connector_state(struct drm_device *dev,
12811 struct drm_atomic_state *old_state)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012812{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012813 struct drm_connector_state *old_conn_state;
12814 struct drm_connector *connector;
12815 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012816
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012817 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12818 struct drm_encoder *encoder = connector->encoder;
12819 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012820
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012821 /* This also checks the encoder/connector hw state with the
12822 * ->get_hw_state callbacks. */
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012823 intel_connector_check_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012824
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012825 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012826 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012827 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012828}
12829
12830static void
12831check_encoder_state(struct drm_device *dev)
12832{
12833 struct intel_encoder *encoder;
12834 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012835
Damien Lespiaub2784e12014-08-05 11:29:37 +010012836 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012837 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012838 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012839
12840 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12841 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012842 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012843
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012844 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012845 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012846 continue;
12847 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012848
12849 I915_STATE_WARN(connector->base.state->crtc !=
12850 encoder->base.crtc,
12851 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012852 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012853
Rob Clarke2c719b2014-12-15 13:56:32 -050012854 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012855 "encoder's enabled state mismatch "
12856 "(expected %i, found %i)\n",
12857 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012858
12859 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012860 bool active;
12861
12862 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012863 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012864 "encoder detached but still enabled on pipe %c.\n",
12865 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012866 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012867 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012868}
12869
12870static void
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012871check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012872{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012873 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012874 struct intel_encoder *encoder;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012875 struct drm_crtc_state *old_crtc_state;
12876 struct drm_crtc *crtc;
12877 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012878
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012879 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12880 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12881 struct intel_crtc_state *pipe_config, *sw_config;
Maarten Lankhorst7b89b8d2015-08-05 12:37:03 +020012882 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012883
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012884 if (!needs_modeset(crtc->state) &&
12885 !to_intel_crtc_state(crtc->state)->update_pipe)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012886 continue;
12887
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012888 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12889 pipe_config = to_intel_crtc_state(old_crtc_state);
12890 memset(pipe_config, 0, sizeof(*pipe_config));
12891 pipe_config->base.crtc = crtc;
12892 pipe_config->base.state = old_state;
12893
12894 DRM_DEBUG_KMS("[CRTC:%d]\n",
12895 crtc->base.id);
12896
12897 active = dev_priv->display.get_pipe_config(intel_crtc,
12898 pipe_config);
12899
12900 /* hw state is inconsistent with the pipe quirk */
12901 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12902 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12903 active = crtc->state->active;
12904
12905 I915_STATE_WARN(crtc->state->active != active,
12906 "crtc active state doesn't match with hw state "
12907 "(expected %i, found %i)\n", crtc->state->active, active);
12908
12909 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
12910 "transitional active state does not match atomic hw state "
12911 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12912
12913 for_each_encoder_on_crtc(dev, crtc, encoder) {
12914 enum pipe pipe;
12915
12916 active = encoder->get_hw_state(encoder, &pipe);
12917 I915_STATE_WARN(active != crtc->state->active,
12918 "[ENCODER:%i] active %i with crtc active %i\n",
12919 encoder->base.base.id, active, crtc->state->active);
12920
12921 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12922 "Encoder connected to wrong pipe %c\n",
12923 pipe_name(pipe));
12924
12925 if (active)
12926 encoder->get_config(encoder, pipe_config);
12927 }
12928
12929 if (!crtc->state->active)
12930 continue;
12931
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012932 intel_pipe_config_sanity_check(dev_priv, pipe_config);
12933
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012934 sw_config = to_intel_crtc_state(crtc->state);
12935 if (!intel_pipe_config_compare(dev, sw_config,
12936 pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012937 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012938 intel_dump_pipe_config(intel_crtc, pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012939 "[hw state]");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012940 intel_dump_pipe_config(intel_crtc, sw_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012941 "[sw state]");
12942 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012943 }
12944}
12945
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012946static void
12947check_shared_dpll_state(struct drm_device *dev)
12948{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012949 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012950 struct intel_crtc *crtc;
12951 struct intel_dpll_hw_state dpll_hw_state;
12952 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012953
12954 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012955 struct intel_shared_dpll *pll =
12956 intel_get_shared_dpll_by_id(dev_priv, i);
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012957 unsigned enabled_crtcs = 0, active_crtcs = 0;
Daniel Vetter53589012013-06-05 13:34:16 +020012958 bool active;
12959
12960 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12961
12962 DRM_DEBUG_KMS("%s\n", pll->name);
12963
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020012964 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020012965
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012966 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
12967 "more active pll users than references: %x vs %x\n",
12968 pll->active_mask, pll->config.crtc_mask);
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020012969
12970 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012971 I915_STATE_WARN(!pll->on && pll->active_mask,
12972 "pll in active use but not on in sw tracking\n");
12973 I915_STATE_WARN(pll->on && !pll->active_mask,
12974 "pll is on but not used by any active crtc\n");
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020012975 I915_STATE_WARN(pll->on != active,
12976 "pll on state mismatch (expected %i, found %i)\n",
12977 pll->on, active);
12978 }
Daniel Vetter53589012013-06-05 13:34:16 +020012979
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012980 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012981 if (crtc->base.state->enable && crtc->config->shared_dpll == pll)
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012982 enabled_crtcs |= 1 << drm_crtc_index(&crtc->base);
12983 if (crtc->base.state->active && crtc->config->shared_dpll == pll)
12984 active_crtcs |= 1 << drm_crtc_index(&crtc->base);
Daniel Vetter53589012013-06-05 13:34:16 +020012985 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012986
12987 I915_STATE_WARN(pll->active_mask != active_crtcs,
12988 "pll active crtcs mismatch (expected %x, found %x)\n",
12989 pll->active_mask, active_crtcs);
12990 I915_STATE_WARN(pll->config.crtc_mask != enabled_crtcs,
12991 "pll enabled crtcs mismatch (expected %x, found %x)\n",
12992 pll->config.crtc_mask, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012993
Rob Clarke2c719b2014-12-15 13:56:32 -050012994 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012995 sizeof(dpll_hw_state)),
12996 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012997 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012998}
12999
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013000static void
13001intel_modeset_check_state(struct drm_device *dev,
13002 struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013003{
Damien Lespiau08db6652014-11-04 17:06:52 +000013004 check_wm_state(dev);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013005 check_connector_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013006 check_encoder_state(dev);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013007 check_crtc_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013008 check_shared_dpll_state(dev);
13009}
13010
Ville Syrjälä80715b22014-05-15 20:23:23 +030013011static void update_scanline_offset(struct intel_crtc *crtc)
13012{
13013 struct drm_device *dev = crtc->base.dev;
13014
13015 /*
13016 * The scanline counter increments at the leading edge of hsync.
13017 *
13018 * On most platforms it starts counting from vtotal-1 on the
13019 * first active line. That means the scanline counter value is
13020 * always one less than what we would expect. Ie. just after
13021 * start of vblank, which also occurs at start of hsync (on the
13022 * last active line), the scanline counter will read vblank_start-1.
13023 *
13024 * On gen2 the scanline counter starts counting from 1 instead
13025 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13026 * to keep the value positive), instead of adding one.
13027 *
13028 * On HSW+ the behaviour of the scanline counter depends on the output
13029 * type. For DP ports it behaves like most other platforms, but on HDMI
13030 * there's an extra 1 line difference. So we need to add two instead of
13031 * one to the value.
13032 */
13033 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013034 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013035 int vtotal;
13036
Ville Syrjälä124abe02015-09-08 13:40:45 +030013037 vtotal = adjusted_mode->crtc_vtotal;
13038 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013039 vtotal /= 2;
13040
13041 crtc->scanline_offset = vtotal - 1;
13042 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013043 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013044 crtc->scanline_offset = 2;
13045 } else
13046 crtc->scanline_offset = 1;
13047}
13048
Maarten Lankhorstad421372015-06-15 12:33:42 +020013049static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013050{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013051 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013052 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013053 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013054 struct drm_crtc *crtc;
13055 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013056 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013057
13058 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013059 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013060
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013061 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013062 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013063 struct intel_shared_dpll *old_dpll =
13064 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013065
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013066 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013067 continue;
13068
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013069 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013070
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013071 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013072 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013073
Maarten Lankhorstad421372015-06-15 12:33:42 +020013074 if (!shared_dpll)
13075 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13076
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013077 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013078 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013079}
13080
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013081/*
13082 * This implements the workaround described in the "notes" section of the mode
13083 * set sequence documentation. When going from no pipes or single pipe to
13084 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13085 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13086 */
13087static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13088{
13089 struct drm_crtc_state *crtc_state;
13090 struct intel_crtc *intel_crtc;
13091 struct drm_crtc *crtc;
13092 struct intel_crtc_state *first_crtc_state = NULL;
13093 struct intel_crtc_state *other_crtc_state = NULL;
13094 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13095 int i;
13096
13097 /* look at all crtc's that are going to be enabled in during modeset */
13098 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13099 intel_crtc = to_intel_crtc(crtc);
13100
13101 if (!crtc_state->active || !needs_modeset(crtc_state))
13102 continue;
13103
13104 if (first_crtc_state) {
13105 other_crtc_state = to_intel_crtc_state(crtc_state);
13106 break;
13107 } else {
13108 first_crtc_state = to_intel_crtc_state(crtc_state);
13109 first_pipe = intel_crtc->pipe;
13110 }
13111 }
13112
13113 /* No workaround needed? */
13114 if (!first_crtc_state)
13115 return 0;
13116
13117 /* w/a possibly needed, check how many crtc's are already enabled. */
13118 for_each_intel_crtc(state->dev, intel_crtc) {
13119 struct intel_crtc_state *pipe_config;
13120
13121 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13122 if (IS_ERR(pipe_config))
13123 return PTR_ERR(pipe_config);
13124
13125 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13126
13127 if (!pipe_config->base.active ||
13128 needs_modeset(&pipe_config->base))
13129 continue;
13130
13131 /* 2 or more enabled crtcs means no need for w/a */
13132 if (enabled_pipe != INVALID_PIPE)
13133 return 0;
13134
13135 enabled_pipe = intel_crtc->pipe;
13136 }
13137
13138 if (enabled_pipe != INVALID_PIPE)
13139 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13140 else if (other_crtc_state)
13141 other_crtc_state->hsw_workaround_pipe = first_pipe;
13142
13143 return 0;
13144}
13145
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013146static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13147{
13148 struct drm_crtc *crtc;
13149 struct drm_crtc_state *crtc_state;
13150 int ret = 0;
13151
13152 /* add all active pipes to the state */
13153 for_each_crtc(state->dev, crtc) {
13154 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13155 if (IS_ERR(crtc_state))
13156 return PTR_ERR(crtc_state);
13157
13158 if (!crtc_state->active || needs_modeset(crtc_state))
13159 continue;
13160
13161 crtc_state->mode_changed = true;
13162
13163 ret = drm_atomic_add_affected_connectors(state, crtc);
13164 if (ret)
13165 break;
13166
13167 ret = drm_atomic_add_affected_planes(state, crtc);
13168 if (ret)
13169 break;
13170 }
13171
13172 return ret;
13173}
13174
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013175static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013176{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013177 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13178 struct drm_i915_private *dev_priv = state->dev->dev_private;
13179 struct drm_crtc *crtc;
13180 struct drm_crtc_state *crtc_state;
13181 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013182
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013183 if (!check_digital_port_conflicts(state)) {
13184 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13185 return -EINVAL;
13186 }
13187
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013188 intel_state->modeset = true;
13189 intel_state->active_crtcs = dev_priv->active_crtcs;
13190
13191 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13192 if (crtc_state->active)
13193 intel_state->active_crtcs |= 1 << i;
13194 else
13195 intel_state->active_crtcs &= ~(1 << i);
13196 }
13197
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013198 /*
13199 * See if the config requires any additional preparation, e.g.
13200 * to adjust global state with pipes off. We need to do this
13201 * here so we can get the modeset_pipe updated config for the new
13202 * mode set on this crtc. For other crtcs we need to use the
13203 * adjusted_mode bits in the crtc directly.
13204 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013205 if (dev_priv->display.modeset_calc_cdclk) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013206 ret = dev_priv->display.modeset_calc_cdclk(state);
13207
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013208 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013209 ret = intel_modeset_all_pipes(state);
13210
13211 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013212 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013213
13214 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13215 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013216 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013217 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013218
Maarten Lankhorstad421372015-06-15 12:33:42 +020013219 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013220
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013221 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013222 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013223
Maarten Lankhorstad421372015-06-15 12:33:42 +020013224 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013225}
13226
Matt Roperaa363132015-09-24 15:53:18 -070013227/*
13228 * Handle calculation of various watermark data at the end of the atomic check
13229 * phase. The code here should be run after the per-crtc and per-plane 'check'
13230 * handlers to ensure that all derived state has been updated.
13231 */
13232static void calc_watermark_data(struct drm_atomic_state *state)
13233{
13234 struct drm_device *dev = state->dev;
13235 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13236 struct drm_crtc *crtc;
13237 struct drm_crtc_state *cstate;
13238 struct drm_plane *plane;
13239 struct drm_plane_state *pstate;
13240
13241 /*
13242 * Calculate watermark configuration details now that derived
13243 * plane/crtc state is all properly updated.
13244 */
13245 drm_for_each_crtc(crtc, dev) {
13246 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13247 crtc->state;
13248
13249 if (cstate->active)
13250 intel_state->wm_config.num_pipes_active++;
13251 }
13252 drm_for_each_legacy_plane(plane, dev) {
13253 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13254 plane->state;
13255
13256 if (!to_intel_plane_state(pstate)->visible)
13257 continue;
13258
13259 intel_state->wm_config.sprites_enabled = true;
13260 if (pstate->crtc_w != pstate->src_w >> 16 ||
13261 pstate->crtc_h != pstate->src_h >> 16)
13262 intel_state->wm_config.sprites_scaled = true;
13263 }
13264}
13265
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013266/**
13267 * intel_atomic_check - validate state object
13268 * @dev: drm device
13269 * @state: state to validate
13270 */
13271static int intel_atomic_check(struct drm_device *dev,
13272 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013273{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013274 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013275 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013276 struct drm_crtc *crtc;
13277 struct drm_crtc_state *crtc_state;
13278 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013279 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013280
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013281 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013282 if (ret)
13283 return ret;
13284
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013285 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013286 struct intel_crtc_state *pipe_config =
13287 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013288
13289 /* Catch I915_MODE_FLAG_INHERITED */
13290 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13291 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013292
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013293 if (!crtc_state->enable) {
13294 if (needs_modeset(crtc_state))
13295 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013296 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013297 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +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 Vetter26495482015-07-15 14:15:52 +020013302 /* FIXME: For only active_changed we shouldn't need to do any
13303 * state recomputation at all. */
13304
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013305 ret = drm_atomic_add_affected_connectors(state, crtc);
13306 if (ret)
13307 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013308
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013309 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013310 if (ret)
13311 return ret;
13312
Jani Nikula73831232015-11-19 10:26:30 +020013313 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013314 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013315 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013316 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013317 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013318 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013319 }
13320
13321 if (needs_modeset(crtc_state)) {
13322 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013323
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013324 ret = drm_atomic_add_affected_planes(state, crtc);
13325 if (ret)
13326 return ret;
13327 }
13328
Daniel Vetter26495482015-07-15 14:15:52 +020013329 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13330 needs_modeset(crtc_state) ?
13331 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013332 }
13333
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013334 if (any_ms) {
13335 ret = intel_modeset_checks(state);
13336
13337 if (ret)
13338 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013339 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013340 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013341
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013342 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013343 if (ret)
13344 return ret;
13345
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013346 intel_fbc_choose_crtc(dev_priv, state);
Matt Roperaa363132015-09-24 15:53:18 -070013347 calc_watermark_data(state);
13348
13349 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013350}
13351
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013352static int intel_atomic_prepare_commit(struct drm_device *dev,
13353 struct drm_atomic_state *state,
13354 bool async)
13355{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013356 struct drm_i915_private *dev_priv = dev->dev_private;
13357 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013358 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013359 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013360 struct drm_crtc *crtc;
13361 int i, ret;
13362
13363 if (async) {
13364 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13365 return -EINVAL;
13366 }
13367
13368 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13369 ret = intel_crtc_wait_for_pending_flips(crtc);
13370 if (ret)
13371 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013372
13373 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13374 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013375 }
13376
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013377 ret = mutex_lock_interruptible(&dev->struct_mutex);
13378 if (ret)
13379 return ret;
13380
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013381 ret = drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013382 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13383 u32 reset_counter;
13384
13385 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13386 mutex_unlock(&dev->struct_mutex);
13387
13388 for_each_plane_in_state(state, plane, plane_state, i) {
13389 struct intel_plane_state *intel_plane_state =
13390 to_intel_plane_state(plane_state);
13391
13392 if (!intel_plane_state->wait_req)
13393 continue;
13394
13395 ret = __i915_wait_request(intel_plane_state->wait_req,
13396 reset_counter, true,
13397 NULL, NULL);
13398
13399 /* Swallow -EIO errors to allow updates during hw lockup. */
13400 if (ret == -EIO)
13401 ret = 0;
13402
13403 if (ret)
13404 break;
13405 }
13406
13407 if (!ret)
13408 return 0;
13409
13410 mutex_lock(&dev->struct_mutex);
13411 drm_atomic_helper_cleanup_planes(dev, state);
13412 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013413
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013414 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013415 return ret;
13416}
13417
Maarten Lankhorste8861672016-02-24 11:24:26 +010013418static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13419 struct drm_i915_private *dev_priv,
13420 unsigned crtc_mask)
13421{
13422 unsigned last_vblank_count[I915_MAX_PIPES];
13423 enum pipe pipe;
13424 int ret;
13425
13426 if (!crtc_mask)
13427 return;
13428
13429 for_each_pipe(dev_priv, pipe) {
13430 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13431
13432 if (!((1 << pipe) & crtc_mask))
13433 continue;
13434
13435 ret = drm_crtc_vblank_get(crtc);
13436 if (WARN_ON(ret != 0)) {
13437 crtc_mask &= ~(1 << pipe);
13438 continue;
13439 }
13440
13441 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13442 }
13443
13444 for_each_pipe(dev_priv, pipe) {
13445 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13446 long lret;
13447
13448 if (!((1 << pipe) & crtc_mask))
13449 continue;
13450
13451 lret = wait_event_timeout(dev->vblank[pipe].queue,
13452 last_vblank_count[pipe] !=
13453 drm_crtc_vblank_count(crtc),
13454 msecs_to_jiffies(50));
13455
13456 WARN_ON(!lret);
13457
13458 drm_crtc_vblank_put(crtc);
13459 }
13460}
13461
13462static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13463{
13464 /* fb updated, need to unpin old fb */
13465 if (crtc_state->fb_changed)
13466 return true;
13467
13468 /* wm changes, need vblank before final wm's */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013469 if (crtc_state->update_wm_post)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013470 return true;
13471
13472 /*
13473 * cxsr is re-enabled after vblank.
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013474 * This is already handled by crtc_state->update_wm_post,
Maarten Lankhorste8861672016-02-24 11:24:26 +010013475 * but added for clarity.
13476 */
13477 if (crtc_state->disable_cxsr)
13478 return true;
13479
13480 return false;
13481}
13482
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013483/**
13484 * intel_atomic_commit - commit validated state object
13485 * @dev: DRM device
13486 * @state: the top-level driver state object
13487 * @async: asynchronous commit
13488 *
13489 * This function commits a top-level state object that has been validated
13490 * with drm_atomic_helper_check().
13491 *
13492 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13493 * we can only handle plane-related operations and do not yet support
13494 * asynchronous commit.
13495 *
13496 * RETURNS
13497 * Zero for success or -errno.
13498 */
13499static int intel_atomic_commit(struct drm_device *dev,
13500 struct drm_atomic_state *state,
13501 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013502{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013503 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013504 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013505 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013506 struct drm_crtc *crtc;
Matt Ropered4a6a72016-02-23 17:20:13 -080013507 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013508 int ret = 0, i;
13509 bool hw_check = intel_state->modeset;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013510 unsigned long put_domains[I915_MAX_PIPES] = {};
Maarten Lankhorste8861672016-02-24 11:24:26 +010013511 unsigned crtc_vblank_mask = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013512
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013513 ret = intel_atomic_prepare_commit(dev, state, async);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013514 if (ret) {
13515 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013516 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013517 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013518
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013519 drm_atomic_helper_swap_state(dev, state);
Maarten Lankhorsta1475e72016-03-14 09:27:53 +010013520 dev_priv->wm.config = intel_state->wm_config;
13521 intel_shared_dpll_commit(state);
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013522
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013523 if (intel_state->modeset) {
13524 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13525 sizeof(intel_state->min_pixclk));
13526 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013527 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013528
13529 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013530 }
13531
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013532 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013533 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13534
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013535 if (needs_modeset(crtc->state) ||
13536 to_intel_crtc_state(crtc->state)->update_pipe) {
13537 hw_check = true;
13538
13539 put_domains[to_intel_crtc(crtc)->pipe] =
13540 modeset_get_crtc_power_domains(crtc,
13541 to_intel_crtc_state(crtc->state));
13542 }
13543
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013544 if (!needs_modeset(crtc->state))
13545 continue;
13546
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013547 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013548
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013549 if (old_crtc_state->active) {
13550 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013551 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013552 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013553 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013554 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013555
13556 /*
13557 * Underruns don't always raise
13558 * interrupts, so check manually.
13559 */
13560 intel_check_cpu_fifo_underruns(dev_priv);
13561 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013562
13563 if (!crtc->state->active)
13564 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013565 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013566 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013567
Daniel Vetterea9d7582012-07-10 10:42:52 +020013568 /* Only after disabling all output pipelines that will be changed can we
13569 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013570 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013571
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013572 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013573 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013574
13575 if (dev_priv->display.modeset_commit_cdclk &&
13576 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13577 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013578 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013579
Daniel Vettera6778b32012-07-02 09:56:42 +020013580 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013581 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013582 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13583 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorste8861672016-02-24 11:24:26 +010013584 struct intel_crtc_state *pipe_config =
13585 to_intel_crtc_state(crtc->state);
13586 bool update_pipe = !modeset && pipe_config->update_pipe;
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013587
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013588 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013589 update_scanline_offset(to_intel_crtc(crtc));
13590 dev_priv->display.crtc_enable(crtc);
13591 }
13592
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013593 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013594 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013595
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010013596 if (crtc->state->active &&
13597 drm_atomic_get_existing_plane_state(state, crtc->primary))
Paulo Zanoni49227c42016-01-19 11:35:52 -020013598 intel_fbc_enable(intel_crtc);
13599
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013600 if (crtc->state->active &&
13601 (crtc->state->planes_changed || update_pipe))
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013602 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013603
Maarten Lankhorste8861672016-02-24 11:24:26 +010013604 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13605 crtc_vblank_mask |= 1 << i;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013606 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013607
Daniel Vettera6778b32012-07-02 09:56:42 +020013608 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013609
Maarten Lankhorste8861672016-02-24 11:24:26 +010013610 if (!state->legacy_cursor_update)
13611 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013612
Matt Ropered4a6a72016-02-23 17:20:13 -080013613 /*
13614 * Now that the vblank has passed, we can go ahead and program the
13615 * optimal watermarks on platforms that need two-step watermark
13616 * programming.
13617 *
13618 * TODO: Move this (and other cleanup) to an async worker eventually.
13619 */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013620 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Matt Ropered4a6a72016-02-23 17:20:13 -080013621 intel_cstate = to_intel_crtc_state(crtc->state);
13622
13623 if (dev_priv->display.optimize_watermarks)
13624 dev_priv->display.optimize_watermarks(intel_cstate);
13625 }
13626
Matt Roper177246a2016-03-04 15:59:39 -080013627 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13628 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13629
13630 if (put_domains[i])
13631 modeset_put_power_domains(dev_priv, put_domains[i]);
13632 }
13633
13634 if (intel_state->modeset)
13635 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13636
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013637 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013638 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013639 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013640
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013641 if (hw_check)
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013642 intel_modeset_check_state(dev, state);
13643
13644 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);
13757
13758 /* Swallow -EIO errors to allow updates during hw lockup. */
13759 if (ret && ret != -EIO)
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013760 return ret;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013761 }
13762
Alex Goins3c28ff22015-11-25 18:43:39 -080013763 /* For framebuffer backed by dmabuf, wait for fence */
13764 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013765 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080013766
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013767 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13768 false, true,
13769 MAX_SCHEDULE_TIMEOUT);
13770 if (lret == -ERESTARTSYS)
13771 return lret;
13772
13773 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080013774 }
13775
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013776 if (!obj) {
13777 ret = 0;
13778 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013779 INTEL_INFO(dev)->cursor_needs_physical) {
13780 int align = IS_I830(dev) ? 16 * 1024 : 256;
13781 ret = i915_gem_object_attach_phys(obj, align);
13782 if (ret)
13783 DRM_DEBUG_KMS("failed to attach phys object\n");
13784 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020013785 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080013786 }
13787
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013788 if (ret == 0) {
13789 if (obj) {
13790 struct intel_plane_state *plane_state =
13791 to_intel_plane_state(new_state);
13792
13793 i915_gem_request_assign(&plane_state->wait_req,
13794 obj->last_write_req);
13795 }
13796
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013797 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013798 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013799
Matt Roper6beb8c232014-12-01 15:40:14 -080013800 return ret;
13801}
13802
Matt Roper38f3ce32014-12-02 07:45:25 -080013803/**
13804 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13805 * @plane: drm plane to clean up for
13806 * @fb: old framebuffer that was on plane
13807 *
13808 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013809 *
13810 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013811 */
13812void
13813intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013814 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013815{
13816 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013817 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013818 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013819 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13820 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013821
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013822 old_intel_state = to_intel_plane_state(old_state);
13823
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013824 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013825 return;
13826
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013827 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13828 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020013829 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013830
13831 /* prepare_fb aborted? */
13832 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13833 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13834 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013835
13836 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070013837}
13838
Chandra Konduru6156a452015-04-27 13:48:39 -070013839int
13840skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13841{
13842 int max_scale;
13843 struct drm_device *dev;
13844 struct drm_i915_private *dev_priv;
13845 int crtc_clock, cdclk;
13846
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013847 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013848 return DRM_PLANE_HELPER_NO_SCALING;
13849
13850 dev = intel_crtc->base.dev;
13851 dev_priv = dev->dev_private;
13852 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013853 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013854
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010013855 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013856 return DRM_PLANE_HELPER_NO_SCALING;
13857
13858 /*
13859 * skl max scale is lower of:
13860 * close to 3 but not 3, -1 is for that purpose
13861 * or
13862 * cdclk/crtc_clock
13863 */
13864 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13865
13866 return max_scale;
13867}
13868
Matt Roper465c1202014-05-29 08:06:54 -070013869static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013870intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013871 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013872 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013873{
Matt Roper2b875c22014-12-01 15:40:13 -080013874 struct drm_crtc *crtc = state->base.crtc;
13875 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013876 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013877 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13878 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013879
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013880 if (INTEL_INFO(plane->dev)->gen >= 9) {
13881 /* use scaler when colorkey is not required */
13882 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13883 min_scale = 1;
13884 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13885 }
Sonika Jindald8106362015-04-10 14:37:28 +053013886 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013887 }
Sonika Jindald8106362015-04-10 14:37:28 +053013888
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013889 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13890 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013891 min_scale, max_scale,
13892 can_position, true,
13893 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013894}
13895
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013896static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13897 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013898{
13899 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080013900 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013901 struct intel_crtc_state *old_intel_state =
13902 to_intel_crtc_state(old_crtc_state);
13903 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013904
Matt Roperc34c9ee2014-12-23 10:41:50 -080013905 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020013906 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013907
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013908 if (modeset)
13909 return;
13910
Maarten Lankhorst20a34e72016-03-30 17:16:36 +020013911 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
13912 intel_color_set_csc(crtc->state);
13913 intel_color_load_luts(crtc->state);
13914 }
13915
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013916 if (to_intel_crtc_state(crtc->state)->update_pipe)
13917 intel_update_pipe_config(intel_crtc, old_intel_state);
13918 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020013919 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013920}
13921
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013922static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13923 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013924{
Matt Roper32b7eee2014-12-24 07:59:06 -080013925 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013926
Maarten Lankhorst62852622015-09-23 16:29:38 +020013927 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013928}
13929
Matt Ropercf4c7c12014-12-04 10:27:42 -080013930/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013931 * intel_plane_destroy - destroy a plane
13932 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013933 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013934 * Common destruction function for all types of planes (primary, cursor,
13935 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013936 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013937void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013938{
13939 struct intel_plane *intel_plane = to_intel_plane(plane);
13940 drm_plane_cleanup(plane);
13941 kfree(intel_plane);
13942}
13943
Matt Roper65a3fea2015-01-21 16:35:42 -080013944const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013945 .update_plane = drm_atomic_helper_update_plane,
13946 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013947 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013948 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013949 .atomic_get_property = intel_plane_atomic_get_property,
13950 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013951 .atomic_duplicate_state = intel_plane_duplicate_state,
13952 .atomic_destroy_state = intel_plane_destroy_state,
13953
Matt Roper465c1202014-05-29 08:06:54 -070013954};
13955
13956static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13957 int pipe)
13958{
13959 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013960 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013961 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020013962 unsigned int num_formats;
Matt Roper465c1202014-05-29 08:06:54 -070013963
13964 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13965 if (primary == NULL)
13966 return NULL;
13967
Matt Roper8e7d6882015-01-21 16:35:41 -080013968 state = intel_create_plane_state(&primary->base);
13969 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013970 kfree(primary);
13971 return NULL;
13972 }
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
14014 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014015 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070014016 intel_primary_formats, num_formats,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020014017 DRM_PLANE_TYPE_PRIMARY, NULL);
Sonika Jindal48404c12014-08-22 14:06:04 +053014018
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014019 if (INTEL_INFO(dev)->gen >= 4)
14020 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014021
Matt Roperea2c67b2014-12-23 10:41:52 -080014022 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14023
Matt Roper465c1202014-05-29 08:06:54 -070014024 return &primary->base;
14025}
14026
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014027void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14028{
14029 if (!dev->mode_config.rotation_property) {
14030 unsigned long flags = BIT(DRM_ROTATE_0) |
14031 BIT(DRM_ROTATE_180);
14032
14033 if (INTEL_INFO(dev)->gen >= 9)
14034 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14035
14036 dev->mode_config.rotation_property =
14037 drm_mode_create_rotation_property(dev, flags);
14038 }
14039 if (dev->mode_config.rotation_property)
14040 drm_object_attach_property(&plane->base.base,
14041 dev->mode_config.rotation_property,
14042 plane->base.state->rotation);
14043}
14044
Matt Roper3d7d6512014-06-10 08:28:13 -070014045static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014046intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014047 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014048 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014049{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014050 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014051 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014052 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014053 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014054 unsigned stride;
14055 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014056
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014057 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14058 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014059 DRM_PLANE_HELPER_NO_SCALING,
14060 DRM_PLANE_HELPER_NO_SCALING,
14061 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014062 if (ret)
14063 return ret;
14064
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014065 /* if we want to turn off the cursor ignore width and height */
14066 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014067 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014068
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014069 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014070 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014071 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14072 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014073 return -EINVAL;
14074 }
14075
Matt Roperea2c67b2014-12-23 10:41:52 -080014076 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14077 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014078 DRM_DEBUG_KMS("buffer is too small\n");
14079 return -ENOMEM;
14080 }
14081
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014082 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014083 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014084 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014085 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014086
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014087 /*
14088 * There's something wrong with the cursor on CHV pipe C.
14089 * If it straddles the left edge of the screen then
14090 * moving it away from the edge or disabling it often
14091 * results in a pipe underrun, and often that can lead to
14092 * dead pipe (constant underrun reported, and it scans
14093 * out just a solid color). To recover from that, the
14094 * display power well must be turned off and on again.
14095 * Refuse the put the cursor into that compromised position.
14096 */
14097 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14098 state->visible && state->base.crtc_x < 0) {
14099 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14100 return -EINVAL;
14101 }
14102
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014103 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014104}
14105
Matt Roperf4a2cf22014-12-01 15:40:12 -080014106static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014107intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014108 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014109{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14111
14112 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014113 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014114}
14115
14116static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014117intel_update_cursor_plane(struct drm_plane *plane,
14118 const struct intel_crtc_state *crtc_state,
14119 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014120{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014121 struct drm_crtc *crtc = crtc_state->base.crtc;
14122 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014123 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014124 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014125 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014126
Matt Roperf4a2cf22014-12-01 15:40:12 -080014127 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014128 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014129 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014130 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014131 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014132 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014133
Gustavo Padovana912f122014-12-01 15:40:10 -080014134 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014135 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014136}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014137
Matt Roper3d7d6512014-06-10 08:28:13 -070014138static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14139 int pipe)
14140{
14141 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014142 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014143
14144 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14145 if (cursor == NULL)
14146 return NULL;
14147
Matt Roper8e7d6882015-01-21 16:35:41 -080014148 state = intel_create_plane_state(&cursor->base);
14149 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014150 kfree(cursor);
14151 return NULL;
14152 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014153 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014154
Matt Roper3d7d6512014-06-10 08:28:13 -070014155 cursor->can_scale = false;
14156 cursor->max_downscale = 1;
14157 cursor->pipe = pipe;
14158 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014159 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014160 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014161 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014162 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014163
14164 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014165 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014166 intel_cursor_formats,
14167 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020014168 DRM_PLANE_TYPE_CURSOR, NULL);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014169
14170 if (INTEL_INFO(dev)->gen >= 4) {
14171 if (!dev->mode_config.rotation_property)
14172 dev->mode_config.rotation_property =
14173 drm_mode_create_rotation_property(dev,
14174 BIT(DRM_ROTATE_0) |
14175 BIT(DRM_ROTATE_180));
14176 if (dev->mode_config.rotation_property)
14177 drm_object_attach_property(&cursor->base.base,
14178 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014179 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014180 }
14181
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014182 if (INTEL_INFO(dev)->gen >=9)
14183 state->scaler_id = -1;
14184
Matt Roperea2c67b2014-12-23 10:41:52 -080014185 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14186
Matt Roper3d7d6512014-06-10 08:28:13 -070014187 return &cursor->base;
14188}
14189
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014190static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14191 struct intel_crtc_state *crtc_state)
14192{
14193 int i;
14194 struct intel_scaler *intel_scaler;
14195 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14196
14197 for (i = 0; i < intel_crtc->num_scalers; i++) {
14198 intel_scaler = &scaler_state->scalers[i];
14199 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014200 intel_scaler->mode = PS_SCALER_MODE_DYN;
14201 }
14202
14203 scaler_state->scaler_id = -1;
14204}
14205
Hannes Ederb358d0a2008-12-18 21:18:47 +010014206static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014207{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014208 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014209 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014210 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014211 struct drm_plane *primary = NULL;
14212 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014213 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014214
Daniel Vetter955382f2013-09-19 14:05:45 +020014215 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014216 if (intel_crtc == NULL)
14217 return;
14218
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014219 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14220 if (!crtc_state)
14221 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014222 intel_crtc->config = crtc_state;
14223 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014224 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014225
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014226 /* initialize shared scalers */
14227 if (INTEL_INFO(dev)->gen >= 9) {
14228 if (pipe == PIPE_C)
14229 intel_crtc->num_scalers = 1;
14230 else
14231 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14232
14233 skl_init_scalers(dev, intel_crtc, crtc_state);
14234 }
14235
Matt Roper465c1202014-05-29 08:06:54 -070014236 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014237 if (!primary)
14238 goto fail;
14239
14240 cursor = intel_cursor_plane_create(dev, pipe);
14241 if (!cursor)
14242 goto fail;
14243
Matt Roper465c1202014-05-29 08:06:54 -070014244 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020014245 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070014246 if (ret)
14247 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014248
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014249 /*
14250 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014251 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014252 */
Jesse Barnes80824002009-09-10 15:28:06 -070014253 intel_crtc->pipe = pipe;
14254 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014255 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014256 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014257 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014258 }
14259
Chris Wilson4b0e3332014-05-30 16:35:26 +030014260 intel_crtc->cursor_base = ~0;
14261 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014262 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014263
Ville Syrjälä852eb002015-06-24 22:00:07 +030014264 intel_crtc->wm.cxsr_allowed = true;
14265
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014266 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14267 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14268 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14269 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14270
Jesse Barnes79e53942008-11-07 14:24:08 -080014271 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014272
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014273 intel_color_init(&intel_crtc->base);
14274
Daniel Vetter87b6b102014-05-15 15:33:46 +020014275 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014276 return;
14277
14278fail:
14279 if (primary)
14280 drm_plane_cleanup(primary);
14281 if (cursor)
14282 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014283 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014284 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014285}
14286
Jesse Barnes752aa882013-10-31 18:55:49 +020014287enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14288{
14289 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014290 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014291
Rob Clark51fd3712013-11-19 12:10:12 -050014292 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014293
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014294 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014295 return INVALID_PIPE;
14296
14297 return to_intel_crtc(encoder->crtc)->pipe;
14298}
14299
Carl Worth08d7b3d2009-04-29 14:43:54 -070014300int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014301 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014302{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014303 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014304 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014305 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014306
Rob Clark7707e652014-07-17 23:30:04 -040014307 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014308
Rob Clark7707e652014-07-17 23:30:04 -040014309 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014310 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014311 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014312 }
14313
Rob Clark7707e652014-07-17 23:30:04 -040014314 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014315 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014316
Daniel Vetterc05422d2009-08-11 16:05:30 +020014317 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014318}
14319
Daniel Vetter66a92782012-07-12 20:08:18 +020014320static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014321{
Daniel Vetter66a92782012-07-12 20:08:18 +020014322 struct drm_device *dev = encoder->base.dev;
14323 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014324 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014325 int entry = 0;
14326
Damien Lespiaub2784e12014-08-05 11:29:37 +010014327 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014328 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014329 index_mask |= (1 << entry);
14330
Jesse Barnes79e53942008-11-07 14:24:08 -080014331 entry++;
14332 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014333
Jesse Barnes79e53942008-11-07 14:24:08 -080014334 return index_mask;
14335}
14336
Chris Wilson4d302442010-12-14 19:21:29 +000014337static bool has_edp_a(struct drm_device *dev)
14338{
14339 struct drm_i915_private *dev_priv = dev->dev_private;
14340
14341 if (!IS_MOBILE(dev))
14342 return false;
14343
14344 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14345 return false;
14346
Damien Lespiaue3589902014-02-07 19:12:50 +000014347 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014348 return false;
14349
14350 return true;
14351}
14352
Jesse Barnes84b4e042014-06-25 08:24:29 -070014353static bool intel_crt_present(struct drm_device *dev)
14354{
14355 struct drm_i915_private *dev_priv = dev->dev_private;
14356
Damien Lespiau884497e2013-12-03 13:56:23 +000014357 if (INTEL_INFO(dev)->gen >= 9)
14358 return false;
14359
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014360 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014361 return false;
14362
14363 if (IS_CHERRYVIEW(dev))
14364 return false;
14365
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014366 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14367 return false;
14368
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014369 /* DDI E can't be used if DDI A requires 4 lanes */
14370 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14371 return false;
14372
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014373 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014374 return false;
14375
14376 return true;
14377}
14378
Jesse Barnes79e53942008-11-07 14:24:08 -080014379static void intel_setup_outputs(struct drm_device *dev)
14380{
Eric Anholt725e30a2009-01-22 13:01:02 -080014381 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014382 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014383 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014384
Daniel Vetterc9093352013-06-06 22:22:47 +020014385 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014386
Jesse Barnes84b4e042014-06-25 08:24:29 -070014387 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014388 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014389
Vandana Kannanc776eb22014-08-19 12:05:01 +053014390 if (IS_BROXTON(dev)) {
14391 /*
14392 * FIXME: Broxton doesn't support port detection via the
14393 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14394 * detect the ports.
14395 */
14396 intel_ddi_init(dev, PORT_A);
14397 intel_ddi_init(dev, PORT_B);
14398 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014399
14400 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014401 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014402 int found;
14403
Jesse Barnesde31fac2015-03-06 15:53:32 -080014404 /*
14405 * Haswell uses DDI functions to detect digital outputs.
14406 * On SKL pre-D0 the strap isn't connected, so we assume
14407 * it's there.
14408 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014409 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014410 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014411 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014412 intel_ddi_init(dev, PORT_A);
14413
14414 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14415 * register */
14416 found = I915_READ(SFUSE_STRAP);
14417
14418 if (found & SFUSE_STRAP_DDIB_DETECTED)
14419 intel_ddi_init(dev, PORT_B);
14420 if (found & SFUSE_STRAP_DDIC_DETECTED)
14421 intel_ddi_init(dev, PORT_C);
14422 if (found & SFUSE_STRAP_DDID_DETECTED)
14423 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014424 /*
14425 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14426 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014427 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014428 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14429 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14430 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14431 intel_ddi_init(dev, PORT_E);
14432
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014433 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014434 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014435 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014436
14437 if (has_edp_a(dev))
14438 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014439
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014440 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014441 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014442 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014443 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014444 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014445 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014446 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014447 }
14448
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014449 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014450 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014451
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014452 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014453 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014454
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014455 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014456 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014457
Daniel Vetter270b3042012-10-27 15:52:05 +020014458 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014459 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014460 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014461 /*
14462 * The DP_DETECTED bit is the latched state of the DDC
14463 * SDA pin at boot. However since eDP doesn't require DDC
14464 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14465 * eDP ports may have been muxed to an alternate function.
14466 * Thus we can't rely on the DP_DETECTED bit alone to detect
14467 * eDP ports. Consult the VBT as well as DP_DETECTED to
14468 * detect eDP ports.
14469 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014470 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014471 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014472 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14473 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014474 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014475 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014476
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014477 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014478 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014479 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14480 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014481 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014482 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014483
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014484 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014485 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014486 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14487 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14488 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14489 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014490 }
14491
Jani Nikula3cfca972013-08-27 15:12:26 +030014492 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014493 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014494 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014495
Paulo Zanonie2debe92013-02-18 19:00:27 -030014496 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014497 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014498 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014499 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014500 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014501 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014502 }
Ma Ling27185ae2009-08-24 13:50:23 +080014503
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014504 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014505 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014506 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014507
14508 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014509
Paulo Zanonie2debe92013-02-18 19:00:27 -030014510 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014511 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014512 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014513 }
Ma Ling27185ae2009-08-24 13:50:23 +080014514
Paulo Zanonie2debe92013-02-18 19:00:27 -030014515 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014516
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014517 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014518 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014519 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014520 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014521 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014522 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014523 }
Ma Ling27185ae2009-08-24 13:50:23 +080014524
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014525 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014526 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014527 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014528 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014529 intel_dvo_init(dev);
14530
Zhenyu Wang103a1962009-11-27 11:44:36 +080014531 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014532 intel_tv_init(dev);
14533
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014534 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014535
Damien Lespiaub2784e12014-08-05 11:29:37 +010014536 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014537 encoder->base.possible_crtcs = encoder->crtc_mask;
14538 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014539 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014540 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014541
Paulo Zanonidde86e22012-12-01 12:04:25 -020014542 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014543
14544 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014545}
14546
14547static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14548{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014549 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014550 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014551
Daniel Vetteref2d6332014-02-10 18:00:38 +010014552 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014553 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014554 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014555 drm_gem_object_unreference(&intel_fb->obj->base);
14556 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014557 kfree(intel_fb);
14558}
14559
14560static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014561 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014562 unsigned int *handle)
14563{
14564 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014565 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014566
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014567 if (obj->userptr.mm) {
14568 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14569 return -EINVAL;
14570 }
14571
Chris Wilson05394f32010-11-08 19:18:58 +000014572 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014573}
14574
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014575static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14576 struct drm_file *file,
14577 unsigned flags, unsigned color,
14578 struct drm_clip_rect *clips,
14579 unsigned num_clips)
14580{
14581 struct drm_device *dev = fb->dev;
14582 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14583 struct drm_i915_gem_object *obj = intel_fb->obj;
14584
14585 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014586 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014587 mutex_unlock(&dev->struct_mutex);
14588
14589 return 0;
14590}
14591
Jesse Barnes79e53942008-11-07 14:24:08 -080014592static const struct drm_framebuffer_funcs intel_fb_funcs = {
14593 .destroy = intel_user_framebuffer_destroy,
14594 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014595 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014596};
14597
Damien Lespiaub3218032015-02-27 11:15:18 +000014598static
14599u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14600 uint32_t pixel_format)
14601{
14602 u32 gen = INTEL_INFO(dev)->gen;
14603
14604 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014605 int cpp = drm_format_plane_cpp(pixel_format, 0);
14606
Damien Lespiaub3218032015-02-27 11:15:18 +000014607 /* "The stride in bytes must not exceed the of the size of 8K
14608 * pixels and 32K bytes."
14609 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014610 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014611 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014612 return 32*1024;
14613 } else if (gen >= 4) {
14614 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14615 return 16*1024;
14616 else
14617 return 32*1024;
14618 } else if (gen >= 3) {
14619 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14620 return 8*1024;
14621 else
14622 return 16*1024;
14623 } else {
14624 /* XXX DSPC is limited to 4k tiled */
14625 return 8*1024;
14626 }
14627}
14628
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014629static int intel_framebuffer_init(struct drm_device *dev,
14630 struct intel_framebuffer *intel_fb,
14631 struct drm_mode_fb_cmd2 *mode_cmd,
14632 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014633{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014634 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014635 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014636 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014637 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014638
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014639 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14640
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014641 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14642 /* Enforce that fb modifier and tiling mode match, but only for
14643 * X-tiled. This is needed for FBC. */
14644 if (!!(obj->tiling_mode == I915_TILING_X) !=
14645 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14646 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14647 return -EINVAL;
14648 }
14649 } else {
14650 if (obj->tiling_mode == I915_TILING_X)
14651 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14652 else if (obj->tiling_mode == I915_TILING_Y) {
14653 DRM_DEBUG("No Y tiling for legacy addfb\n");
14654 return -EINVAL;
14655 }
14656 }
14657
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014658 /* Passed in modifier sanity checking. */
14659 switch (mode_cmd->modifier[0]) {
14660 case I915_FORMAT_MOD_Y_TILED:
14661 case I915_FORMAT_MOD_Yf_TILED:
14662 if (INTEL_INFO(dev)->gen < 9) {
14663 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14664 mode_cmd->modifier[0]);
14665 return -EINVAL;
14666 }
14667 case DRM_FORMAT_MOD_NONE:
14668 case I915_FORMAT_MOD_X_TILED:
14669 break;
14670 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014671 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14672 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014673 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014674 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014675
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014676 stride_alignment = intel_fb_stride_alignment(dev_priv,
14677 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014678 mode_cmd->pixel_format);
14679 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14680 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14681 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014682 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014683 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014684
Damien Lespiaub3218032015-02-27 11:15:18 +000014685 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14686 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014687 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014688 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14689 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014690 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014691 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014692 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014693 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014694
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014695 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014696 mode_cmd->pitches[0] != obj->stride) {
14697 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14698 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014699 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014700 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014701
Ville Syrjälä57779d02012-10-31 17:50:14 +020014702 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014703 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014704 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014705 case DRM_FORMAT_RGB565:
14706 case DRM_FORMAT_XRGB8888:
14707 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014708 break;
14709 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014710 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014711 DRM_DEBUG("unsupported pixel format: %s\n",
14712 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014713 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014714 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014715 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014716 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014717 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14718 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014719 DRM_DEBUG("unsupported pixel format: %s\n",
14720 drm_get_format_name(mode_cmd->pixel_format));
14721 return -EINVAL;
14722 }
14723 break;
14724 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014725 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014726 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014727 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014728 DRM_DEBUG("unsupported pixel format: %s\n",
14729 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014730 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014731 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014732 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014733 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014734 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014735 DRM_DEBUG("unsupported pixel format: %s\n",
14736 drm_get_format_name(mode_cmd->pixel_format));
14737 return -EINVAL;
14738 }
14739 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014740 case DRM_FORMAT_YUYV:
14741 case DRM_FORMAT_UYVY:
14742 case DRM_FORMAT_YVYU:
14743 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014744 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014745 DRM_DEBUG("unsupported pixel format: %s\n",
14746 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014747 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014748 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014749 break;
14750 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014751 DRM_DEBUG("unsupported pixel format: %s\n",
14752 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014753 return -EINVAL;
14754 }
14755
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014756 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14757 if (mode_cmd->offsets[0] != 0)
14758 return -EINVAL;
14759
Damien Lespiauec2c9812015-01-20 12:51:45 +000014760 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014761 mode_cmd->pixel_format,
14762 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014763 /* FIXME drm helper for size checks (especially planar formats)? */
14764 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14765 return -EINVAL;
14766
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014767 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14768 intel_fb->obj = obj;
14769
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014770 intel_fill_fb_info(dev_priv, &intel_fb->base);
14771
Jesse Barnes79e53942008-11-07 14:24:08 -080014772 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14773 if (ret) {
14774 DRM_ERROR("framebuffer init failed %d\n", ret);
14775 return ret;
14776 }
14777
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020014778 intel_fb->obj->framebuffer_references++;
14779
Jesse Barnes79e53942008-11-07 14:24:08 -080014780 return 0;
14781}
14782
Jesse Barnes79e53942008-11-07 14:24:08 -080014783static struct drm_framebuffer *
14784intel_user_framebuffer_create(struct drm_device *dev,
14785 struct drm_file *filp,
Ville Syrjälä1eb834512015-11-11 19:11:29 +020014786 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014787{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014788 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014789 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014790 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014791
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014792 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014793 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014794 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014795 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014796
Daniel Vetter92907cb2015-11-23 09:04:05 +010014797 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014798 if (IS_ERR(fb))
14799 drm_gem_object_unreference_unlocked(&obj->base);
14800
14801 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014802}
14803
Daniel Vetter06957262015-08-10 13:34:08 +020014804#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014805static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014806{
14807}
14808#endif
14809
Jesse Barnes79e53942008-11-07 14:24:08 -080014810static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014811 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014812 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014813 .atomic_check = intel_atomic_check,
14814 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014815 .atomic_state_alloc = intel_atomic_state_alloc,
14816 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014817};
14818
Imre Deak88212942016-03-16 13:38:53 +020014819/**
14820 * intel_init_display_hooks - initialize the display modesetting hooks
14821 * @dev_priv: device private
14822 */
14823void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014824{
Imre Deak88212942016-03-16 13:38:53 +020014825 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014826 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014827 dev_priv->display.get_initial_plane_config =
14828 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014829 dev_priv->display.crtc_compute_clock =
14830 haswell_crtc_compute_clock;
14831 dev_priv->display.crtc_enable = haswell_crtc_enable;
14832 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014833 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014834 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014835 dev_priv->display.get_initial_plane_config =
14836 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014837 dev_priv->display.crtc_compute_clock =
14838 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014839 dev_priv->display.crtc_enable = haswell_crtc_enable;
14840 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014841 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014842 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014843 dev_priv->display.get_initial_plane_config =
14844 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014845 dev_priv->display.crtc_compute_clock =
14846 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014847 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14848 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014849 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014850 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014851 dev_priv->display.get_initial_plane_config =
14852 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014853 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14854 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14855 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14856 } else if (IS_VALLEYVIEW(dev_priv)) {
14857 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14858 dev_priv->display.get_initial_plane_config =
14859 i9xx_get_initial_plane_config;
14860 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014861 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14862 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020014863 } else if (IS_G4X(dev_priv)) {
14864 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14865 dev_priv->display.get_initial_plane_config =
14866 i9xx_get_initial_plane_config;
14867 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14868 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14869 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020014870 } else if (IS_PINEVIEW(dev_priv)) {
14871 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14872 dev_priv->display.get_initial_plane_config =
14873 i9xx_get_initial_plane_config;
14874 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14875 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14876 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014877 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014878 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014879 dev_priv->display.get_initial_plane_config =
14880 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014881 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014882 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14883 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014884 } else {
14885 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14886 dev_priv->display.get_initial_plane_config =
14887 i9xx_get_initial_plane_config;
14888 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14889 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14890 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014891 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014892
Jesse Barnese70236a2009-09-21 10:42:27 -070014893 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020014894 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014895 dev_priv->display.get_display_clock_speed =
14896 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014897 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014898 dev_priv->display.get_display_clock_speed =
14899 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014900 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014901 dev_priv->display.get_display_clock_speed =
14902 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014903 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014904 dev_priv->display.get_display_clock_speed =
14905 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014906 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014907 dev_priv->display.get_display_clock_speed =
14908 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014909 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014910 dev_priv->display.get_display_clock_speed =
14911 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014912 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14913 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014914 dev_priv->display.get_display_clock_speed =
14915 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014916 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014917 dev_priv->display.get_display_clock_speed =
14918 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014919 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014920 dev_priv->display.get_display_clock_speed =
14921 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014922 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014923 dev_priv->display.get_display_clock_speed =
14924 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014925 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014926 dev_priv->display.get_display_clock_speed =
14927 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014928 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014929 dev_priv->display.get_display_clock_speed =
14930 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014931 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014932 dev_priv->display.get_display_clock_speed =
14933 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014934 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014935 dev_priv->display.get_display_clock_speed =
14936 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014937 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014938 dev_priv->display.get_display_clock_speed =
14939 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014940 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014941 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014942 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014943 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020014944 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014945 dev_priv->display.get_display_clock_speed =
14946 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014947 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014948
Imre Deak88212942016-03-16 13:38:53 +020014949 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014950 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014951 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014952 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014953 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014954 /* FIXME: detect B0+ stepping and use auto training */
14955 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014956 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014957 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014958 if (IS_BROADWELL(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014959 dev_priv->display.modeset_commit_cdclk =
14960 broadwell_modeset_commit_cdclk;
14961 dev_priv->display.modeset_calc_cdclk =
14962 broadwell_modeset_calc_cdclk;
14963 }
Imre Deak88212942016-03-16 13:38:53 +020014964 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014965 dev_priv->display.modeset_commit_cdclk =
14966 valleyview_modeset_commit_cdclk;
14967 dev_priv->display.modeset_calc_cdclk =
14968 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020014969 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014970 dev_priv->display.modeset_commit_cdclk =
14971 broxton_modeset_commit_cdclk;
14972 dev_priv->display.modeset_calc_cdclk =
14973 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014974 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014975
Imre Deak88212942016-03-16 13:38:53 +020014976 switch (INTEL_INFO(dev_priv)->gen) {
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014977 case 2:
14978 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14979 break;
14980
14981 case 3:
14982 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14983 break;
14984
14985 case 4:
14986 case 5:
14987 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14988 break;
14989
14990 case 6:
14991 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14992 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014993 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014994 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014995 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14996 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014997 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014998 /* Drop through - unsupported since execlist only. */
14999 default:
15000 /* Default just returns -ENODEV to indicate unsupported */
15001 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015002 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015003}
15004
Jesse Barnesb690e962010-07-19 13:53:12 -070015005/*
15006 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15007 * resume, or other times. This quirk makes sure that's the case for
15008 * affected systems.
15009 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015010static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015011{
15012 struct drm_i915_private *dev_priv = dev->dev_private;
15013
15014 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015015 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015016}
15017
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015018static void quirk_pipeb_force(struct drm_device *dev)
15019{
15020 struct drm_i915_private *dev_priv = dev->dev_private;
15021
15022 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15023 DRM_INFO("applying pipe b force quirk\n");
15024}
15025
Keith Packard435793d2011-07-12 14:56:22 -070015026/*
15027 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15028 */
15029static void quirk_ssc_force_disable(struct drm_device *dev)
15030{
15031 struct drm_i915_private *dev_priv = dev->dev_private;
15032 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015033 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015034}
15035
Carsten Emde4dca20e2012-03-15 15:56:26 +010015036/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015037 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15038 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015039 */
15040static void quirk_invert_brightness(struct drm_device *dev)
15041{
15042 struct drm_i915_private *dev_priv = dev->dev_private;
15043 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015044 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015045}
15046
Scot Doyle9c72cc62014-07-03 23:27:50 +000015047/* Some VBT's incorrectly indicate no backlight is present */
15048static void quirk_backlight_present(struct drm_device *dev)
15049{
15050 struct drm_i915_private *dev_priv = dev->dev_private;
15051 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15052 DRM_INFO("applying backlight present quirk\n");
15053}
15054
Jesse Barnesb690e962010-07-19 13:53:12 -070015055struct intel_quirk {
15056 int device;
15057 int subsystem_vendor;
15058 int subsystem_device;
15059 void (*hook)(struct drm_device *dev);
15060};
15061
Egbert Eich5f85f172012-10-14 15:46:38 +020015062/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15063struct intel_dmi_quirk {
15064 void (*hook)(struct drm_device *dev);
15065 const struct dmi_system_id (*dmi_id_list)[];
15066};
15067
15068static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15069{
15070 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15071 return 1;
15072}
15073
15074static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15075 {
15076 .dmi_id_list = &(const struct dmi_system_id[]) {
15077 {
15078 .callback = intel_dmi_reverse_brightness,
15079 .ident = "NCR Corporation",
15080 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15081 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15082 },
15083 },
15084 { } /* terminating entry */
15085 },
15086 .hook = quirk_invert_brightness,
15087 },
15088};
15089
Ben Widawskyc43b5632012-04-16 14:07:40 -070015090static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015091 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15092 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15093
Jesse Barnesb690e962010-07-19 13:53:12 -070015094 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15095 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15096
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015097 /* 830 needs to leave pipe A & dpll A up */
15098 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15099
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015100 /* 830 needs to leave pipe B & dpll B up */
15101 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15102
Keith Packard435793d2011-07-12 14:56:22 -070015103 /* Lenovo U160 cannot use SSC on LVDS */
15104 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015105
15106 /* Sony Vaio Y cannot use SSC on LVDS */
15107 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015108
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015109 /* Acer Aspire 5734Z must invert backlight brightness */
15110 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15111
15112 /* Acer/eMachines G725 */
15113 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15114
15115 /* Acer/eMachines e725 */
15116 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15117
15118 /* Acer/Packard Bell NCL20 */
15119 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15120
15121 /* Acer Aspire 4736Z */
15122 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015123
15124 /* Acer Aspire 5336 */
15125 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015126
15127 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15128 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015129
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015130 /* Acer C720 Chromebook (Core i3 4005U) */
15131 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15132
jens steinb2a96012014-10-28 20:25:53 +010015133 /* Apple Macbook 2,1 (Core 2 T7400) */
15134 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15135
Jani Nikula1b9448b02015-11-05 11:49:59 +020015136 /* Apple Macbook 4,1 */
15137 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15138
Scot Doyled4967d82014-07-03 23:27:52 +000015139 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15140 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015141
15142 /* HP Chromebook 14 (Celeron 2955U) */
15143 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015144
15145 /* Dell Chromebook 11 */
15146 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015147
15148 /* Dell Chromebook 11 (2015 version) */
15149 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015150};
15151
15152static void intel_init_quirks(struct drm_device *dev)
15153{
15154 struct pci_dev *d = dev->pdev;
15155 int i;
15156
15157 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15158 struct intel_quirk *q = &intel_quirks[i];
15159
15160 if (d->device == q->device &&
15161 (d->subsystem_vendor == q->subsystem_vendor ||
15162 q->subsystem_vendor == PCI_ANY_ID) &&
15163 (d->subsystem_device == q->subsystem_device ||
15164 q->subsystem_device == PCI_ANY_ID))
15165 q->hook(dev);
15166 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015167 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15168 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15169 intel_dmi_quirks[i].hook(dev);
15170 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015171}
15172
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015173/* Disable the VGA plane that we never use */
15174static void i915_disable_vga(struct drm_device *dev)
15175{
15176 struct drm_i915_private *dev_priv = dev->dev_private;
15177 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015178 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015179
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015180 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015181 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015182 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015183 sr1 = inb(VGA_SR_DATA);
15184 outb(sr1 | 1<<5, VGA_SR_DATA);
15185 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15186 udelay(300);
15187
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015188 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015189 POSTING_READ(vga_reg);
15190}
15191
Daniel Vetterf8175862012-04-10 15:50:11 +020015192void intel_modeset_init_hw(struct drm_device *dev)
15193{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015194 struct drm_i915_private *dev_priv = dev->dev_private;
15195
Ville Syrjäläb6283052015-06-03 15:45:07 +030015196 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015197
15198 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15199
Daniel Vetterf8175862012-04-10 15:50:11 +020015200 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015201 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015202}
15203
Matt Roperd93c0372015-12-03 11:37:41 -080015204/*
15205 * Calculate what we think the watermarks should be for the state we've read
15206 * out of the hardware and then immediately program those watermarks so that
15207 * we ensure the hardware settings match our internal state.
15208 *
15209 * We can calculate what we think WM's should be by creating a duplicate of the
15210 * current state (which was constructed during hardware readout) and running it
15211 * through the atomic check code to calculate new watermark values in the
15212 * state object.
15213 */
15214static void sanitize_watermarks(struct drm_device *dev)
15215{
15216 struct drm_i915_private *dev_priv = to_i915(dev);
15217 struct drm_atomic_state *state;
15218 struct drm_crtc *crtc;
15219 struct drm_crtc_state *cstate;
15220 struct drm_modeset_acquire_ctx ctx;
15221 int ret;
15222 int i;
15223
15224 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015225 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015226 return;
15227
15228 /*
15229 * We need to hold connection_mutex before calling duplicate_state so
15230 * that the connector loop is protected.
15231 */
15232 drm_modeset_acquire_init(&ctx, 0);
15233retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015234 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015235 if (ret == -EDEADLK) {
15236 drm_modeset_backoff(&ctx);
15237 goto retry;
15238 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015239 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015240 }
15241
15242 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15243 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015244 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015245
Matt Ropered4a6a72016-02-23 17:20:13 -080015246 /*
15247 * Hardware readout is the only time we don't want to calculate
15248 * intermediate watermarks (since we don't trust the current
15249 * watermarks).
15250 */
15251 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15252
Matt Roperd93c0372015-12-03 11:37:41 -080015253 ret = intel_atomic_check(dev, state);
15254 if (ret) {
15255 /*
15256 * If we fail here, it means that the hardware appears to be
15257 * programmed in a way that shouldn't be possible, given our
15258 * understanding of watermark requirements. This might mean a
15259 * mistake in the hardware readout code or a mistake in the
15260 * watermark calculations for a given platform. Raise a WARN
15261 * so that this is noticeable.
15262 *
15263 * If this actually happens, we'll have to just leave the
15264 * BIOS-programmed watermarks untouched and hope for the best.
15265 */
15266 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015267 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015268 }
15269
15270 /* Write calculated watermark values back */
15271 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15272 for_each_crtc_in_state(state, crtc, cstate, i) {
15273 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15274
Matt Ropered4a6a72016-02-23 17:20:13 -080015275 cs->wm.need_postvbl_update = true;
15276 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015277 }
15278
15279 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015280fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015281 drm_modeset_drop_locks(&ctx);
15282 drm_modeset_acquire_fini(&ctx);
15283}
15284
Jesse Barnes79e53942008-11-07 14:24:08 -080015285void intel_modeset_init(struct drm_device *dev)
15286{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015287 struct drm_i915_private *dev_priv = to_i915(dev);
15288 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015289 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015290 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015291 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015292
15293 drm_mode_config_init(dev);
15294
15295 dev->mode_config.min_width = 0;
15296 dev->mode_config.min_height = 0;
15297
Dave Airlie019d96c2011-09-29 16:20:42 +010015298 dev->mode_config.preferred_depth = 24;
15299 dev->mode_config.prefer_shadow = 1;
15300
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015301 dev->mode_config.allow_fb_modifiers = true;
15302
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015303 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015304
Jesse Barnesb690e962010-07-19 13:53:12 -070015305 intel_init_quirks(dev);
15306
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015307 intel_init_pm(dev);
15308
Ben Widawskye3c74752013-04-05 13:12:39 -070015309 if (INTEL_INFO(dev)->num_pipes == 0)
15310 return;
15311
Lukas Wunner69f92f62015-07-15 13:57:35 +020015312 /*
15313 * There may be no VBT; and if the BIOS enabled SSC we can
15314 * just keep using it to avoid unnecessary flicker. Whereas if the
15315 * BIOS isn't using it, don't assume it will work even if the VBT
15316 * indicates as much.
15317 */
15318 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15319 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15320 DREF_SSC1_ENABLE);
15321
15322 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15323 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15324 bios_lvds_use_ssc ? "en" : "dis",
15325 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15326 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15327 }
15328 }
15329
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015330 if (IS_GEN2(dev)) {
15331 dev->mode_config.max_width = 2048;
15332 dev->mode_config.max_height = 2048;
15333 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015334 dev->mode_config.max_width = 4096;
15335 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015336 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015337 dev->mode_config.max_width = 8192;
15338 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015339 }
Damien Lespiau068be562014-03-28 14:17:49 +000015340
Ville Syrjälädc41c152014-08-13 11:57:05 +030015341 if (IS_845G(dev) || IS_I865G(dev)) {
15342 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15343 dev->mode_config.cursor_height = 1023;
15344 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015345 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15346 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15347 } else {
15348 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15349 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15350 }
15351
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015352 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015353
Zhao Yakui28c97732009-10-09 11:39:41 +080015354 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015355 INTEL_INFO(dev)->num_pipes,
15356 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015357
Damien Lespiau055e3932014-08-18 13:49:10 +010015358 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015359 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015360 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015361 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015362 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015363 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015364 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015365 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015366 }
15367
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015368 intel_update_czclk(dev_priv);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +020015369 intel_update_rawclk(dev_priv);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015370 intel_update_cdclk(dev);
15371
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015372 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015373
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015374 /* Just disable it once at startup */
15375 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015376 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015377
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015378 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015379 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015380 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015381
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015382 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015383 struct intel_initial_plane_config plane_config = {};
15384
Jesse Barnes46f297f2014-03-07 08:57:48 -080015385 if (!crtc->active)
15386 continue;
15387
Jesse Barnes46f297f2014-03-07 08:57:48 -080015388 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015389 * Note that reserving the BIOS fb up front prevents us
15390 * from stuffing other stolen allocations like the ring
15391 * on top. This prevents some ugliness at boot time, and
15392 * can even allow for smooth boot transitions if the BIOS
15393 * fb is large enough for the active pipe configuration.
15394 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015395 dev_priv->display.get_initial_plane_config(crtc,
15396 &plane_config);
15397
15398 /*
15399 * If the fb is shared between multiple heads, we'll
15400 * just get the first one.
15401 */
15402 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015403 }
Matt Roperd93c0372015-12-03 11:37:41 -080015404
15405 /*
15406 * Make sure hardware watermarks really match the state we read out.
15407 * Note that we need to do this after reconstructing the BIOS fb's
15408 * since the watermark calculation done here will use pstate->fb.
15409 */
15410 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015411}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015412
Daniel Vetter7fad7982012-07-04 17:51:47 +020015413static void intel_enable_pipe_a(struct drm_device *dev)
15414{
15415 struct intel_connector *connector;
15416 struct drm_connector *crt = NULL;
15417 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015418 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015419
15420 /* We can't just switch on the pipe A, we need to set things up with a
15421 * proper mode and output configuration. As a gross hack, enable pipe A
15422 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015423 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015424 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15425 crt = &connector->base;
15426 break;
15427 }
15428 }
15429
15430 if (!crt)
15431 return;
15432
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015433 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015434 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015435}
15436
Daniel Vetterfa555832012-10-10 23:14:00 +020015437static bool
15438intel_check_plane_mapping(struct intel_crtc *crtc)
15439{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015440 struct drm_device *dev = crtc->base.dev;
15441 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015442 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015443
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015444 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015445 return true;
15446
Ville Syrjälä649636e2015-09-22 19:50:01 +030015447 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015448
15449 if ((val & DISPLAY_PLANE_ENABLE) &&
15450 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15451 return false;
15452
15453 return true;
15454}
15455
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015456static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15457{
15458 struct drm_device *dev = crtc->base.dev;
15459 struct intel_encoder *encoder;
15460
15461 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15462 return true;
15463
15464 return false;
15465}
15466
Ville Syrjälädd756192016-02-17 21:28:45 +020015467static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15468{
15469 struct drm_device *dev = encoder->base.dev;
15470 struct intel_connector *connector;
15471
15472 for_each_connector_on_encoder(dev, &encoder->base, connector)
15473 return true;
15474
15475 return false;
15476}
15477
Daniel Vetter24929352012-07-02 20:28:59 +020015478static void intel_sanitize_crtc(struct intel_crtc *crtc)
15479{
15480 struct drm_device *dev = crtc->base.dev;
15481 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015482 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015483
Daniel Vetter24929352012-07-02 20:28:59 +020015484 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015485 if (!transcoder_is_dsi(cpu_transcoder)) {
15486 i915_reg_t reg = PIPECONF(cpu_transcoder);
15487
15488 I915_WRITE(reg,
15489 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15490 }
Daniel Vetter24929352012-07-02 20:28:59 +020015491
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015492 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015493 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015494 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015495 struct intel_plane *plane;
15496
Daniel Vetter96256042015-02-13 21:03:42 +010015497 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015498
15499 /* Disable everything but the primary plane */
15500 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15501 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15502 continue;
15503
15504 plane->disable_plane(&plane->base, &crtc->base);
15505 }
Daniel Vetter96256042015-02-13 21:03:42 +010015506 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015507
Daniel Vetter24929352012-07-02 20:28:59 +020015508 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015509 * disable the crtc (and hence change the state) if it is wrong. Note
15510 * that gen4+ has a fixed plane -> pipe mapping. */
15511 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015512 bool plane;
15513
Daniel Vetter24929352012-07-02 20:28:59 +020015514 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15515 crtc->base.base.id);
15516
15517 /* Pipe has the wrong plane attached and the plane is active.
15518 * Temporarily change the plane mapping and disable everything
15519 * ... */
15520 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015521 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015522 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015523 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015524 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015525 }
Daniel Vetter24929352012-07-02 20:28:59 +020015526
Daniel Vetter7fad7982012-07-04 17:51:47 +020015527 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15528 crtc->pipe == PIPE_A && !crtc->active) {
15529 /* BIOS forgot to enable pipe A, this mostly happens after
15530 * resume. Force-enable the pipe to fix this, the update_dpms
15531 * call below we restore the pipe to the right state, but leave
15532 * the required bits on. */
15533 intel_enable_pipe_a(dev);
15534 }
15535
Daniel Vetter24929352012-07-02 20:28:59 +020015536 /* Adjust the state of the output pipe according to whether we
15537 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015538 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015539 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015540
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015541 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015542 /*
15543 * We start out with underrun reporting disabled to avoid races.
15544 * For correct bookkeeping mark this on active crtcs.
15545 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015546 * Also on gmch platforms we dont have any hardware bits to
15547 * disable the underrun reporting. Which means we need to start
15548 * out with underrun reporting disabled also on inactive pipes,
15549 * since otherwise we'll complain about the garbage we read when
15550 * e.g. coming up after runtime pm.
15551 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015552 * No protection against concurrent access is required - at
15553 * worst a fifo underrun happens which also sets this to false.
15554 */
15555 crtc->cpu_fifo_underrun_disabled = true;
15556 crtc->pch_fifo_underrun_disabled = true;
15557 }
Daniel Vetter24929352012-07-02 20:28:59 +020015558}
15559
15560static void intel_sanitize_encoder(struct intel_encoder *encoder)
15561{
15562 struct intel_connector *connector;
15563 struct drm_device *dev = encoder->base.dev;
15564
15565 /* We need to check both for a crtc link (meaning that the
15566 * encoder is active and trying to read from a pipe) and the
15567 * pipe itself being active. */
15568 bool has_active_crtc = encoder->base.crtc &&
15569 to_intel_crtc(encoder->base.crtc)->active;
15570
Ville Syrjälädd756192016-02-17 21:28:45 +020015571 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015572 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15573 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015574 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015575
15576 /* Connector is active, but has no active pipe. This is
15577 * fallout from our resume register restoring. Disable
15578 * the encoder manually again. */
15579 if (encoder->base.crtc) {
15580 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15581 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015582 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015583 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015584 if (encoder->post_disable)
15585 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015586 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015587 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015588
15589 /* Inconsistent output/port/pipe state happens presumably due to
15590 * a bug in one of the get_hw_state functions. Or someplace else
15591 * in our code, like the register restore mess on resume. Clamp
15592 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015593 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015594 if (connector->encoder != encoder)
15595 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015596 connector->base.dpms = DRM_MODE_DPMS_OFF;
15597 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015598 }
15599 }
15600 /* Enabled encoders without active connectors will be fixed in
15601 * the crtc fixup. */
15602}
15603
Imre Deak04098752014-02-18 00:02:16 +020015604void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015605{
15606 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015607 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015608
Imre Deak04098752014-02-18 00:02:16 +020015609 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15610 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15611 i915_disable_vga(dev);
15612 }
15613}
15614
15615void i915_redisable_vga(struct drm_device *dev)
15616{
15617 struct drm_i915_private *dev_priv = dev->dev_private;
15618
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015619 /* This function can be called both from intel_modeset_setup_hw_state or
15620 * at a very early point in our resume sequence, where the power well
15621 * structures are not yet restored. Since this function is at a very
15622 * paranoid "someone might have enabled VGA while we were not looking"
15623 * level, just check if the power well is enabled instead of trying to
15624 * follow the "don't touch the power well if we don't need it" policy
15625 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015626 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015627 return;
15628
Imre Deak04098752014-02-18 00:02:16 +020015629 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015630
15631 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015632}
15633
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015634static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015635{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015636 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015637
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015638 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015639}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015640
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015641/* FIXME read out full plane state for all planes */
15642static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015643{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015644 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015645 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015646 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015647
Matt Roper19b8d382015-09-24 15:53:17 -070015648 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015649 primary_get_hw_state(to_intel_plane(primary));
15650
15651 if (plane_state->visible)
15652 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015653}
15654
Daniel Vetter30e984d2013-06-05 13:34:17 +020015655static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015656{
15657 struct drm_i915_private *dev_priv = dev->dev_private;
15658 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015659 struct intel_crtc *crtc;
15660 struct intel_encoder *encoder;
15661 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015662 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015663
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015664 dev_priv->active_crtcs = 0;
15665
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015666 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015667 struct intel_crtc_state *crtc_state = crtc->config;
15668 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015669
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015670 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15671 memset(crtc_state, 0, sizeof(*crtc_state));
15672 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015673
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015674 crtc_state->base.active = crtc_state->base.enable =
15675 dev_priv->display.get_pipe_config(crtc, crtc_state);
15676
15677 crtc->base.enabled = crtc_state->base.enable;
15678 crtc->active = crtc_state->base.active;
15679
15680 if (crtc_state->base.active) {
15681 dev_priv->active_crtcs |= 1 << crtc->pipe;
15682
15683 if (IS_BROADWELL(dev_priv)) {
15684 pixclk = ilk_pipe_pixel_rate(crtc_state);
15685
15686 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15687 if (crtc_state->ips_enabled)
15688 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15689 } else if (IS_VALLEYVIEW(dev_priv) ||
15690 IS_CHERRYVIEW(dev_priv) ||
15691 IS_BROXTON(dev_priv))
15692 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15693 else
15694 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15695 }
15696
15697 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015698
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015699 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015700
15701 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15702 crtc->base.base.id,
15703 crtc->active ? "enabled" : "disabled");
15704 }
15705
Daniel Vetter53589012013-06-05 13:34:16 +020015706 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15707 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15708
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015709 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15710 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015711 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015712 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015713 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015714 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015715 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015716 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015717
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015718 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015719 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015720 }
15721
Damien Lespiaub2784e12014-08-05 11:29:37 +010015722 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015723 pipe = 0;
15724
15725 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015726 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15727 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015728 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015729 } else {
15730 encoder->base.crtc = NULL;
15731 }
15732
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015733 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015734 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015735 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015736 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015737 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015738 }
15739
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015740 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015741 if (connector->get_hw_state(connector)) {
15742 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015743
15744 encoder = connector->encoder;
15745 connector->base.encoder = &encoder->base;
15746
15747 if (encoder->base.crtc &&
15748 encoder->base.crtc->state->active) {
15749 /*
15750 * This has to be done during hardware readout
15751 * because anything calling .crtc_disable may
15752 * rely on the connector_mask being accurate.
15753 */
15754 encoder->base.crtc->state->connector_mask |=
15755 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015756 encoder->base.crtc->state->encoder_mask |=
15757 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015758 }
15759
Daniel Vetter24929352012-07-02 20:28:59 +020015760 } else {
15761 connector->base.dpms = DRM_MODE_DPMS_OFF;
15762 connector->base.encoder = NULL;
15763 }
15764 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15765 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015766 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015767 connector->base.encoder ? "enabled" : "disabled");
15768 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015769
15770 for_each_intel_crtc(dev, crtc) {
15771 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15772
15773 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15774 if (crtc->base.state->active) {
15775 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15776 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15777 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15778
15779 /*
15780 * The initial mode needs to be set in order to keep
15781 * the atomic core happy. It wants a valid mode if the
15782 * crtc's enabled, so we do the above call.
15783 *
15784 * At this point some state updated by the connectors
15785 * in their ->detect() callback has not run yet, so
15786 * no recalculation can be done yet.
15787 *
15788 * Even if we could do a recalculation and modeset
15789 * right now it would cause a double modeset if
15790 * fbdev or userspace chooses a different initial mode.
15791 *
15792 * If that happens, someone indicated they wanted a
15793 * mode change, which means it's safe to do a full
15794 * recalculation.
15795 */
15796 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015797
15798 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15799 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015800 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015801
15802 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015803 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015804}
15805
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015806/* Scan out the current hw modeset state,
15807 * and sanitizes it to the current state
15808 */
15809static void
15810intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015811{
15812 struct drm_i915_private *dev_priv = dev->dev_private;
15813 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015814 struct intel_crtc *crtc;
15815 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015816 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015817
15818 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015819
15820 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015821 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015822 intel_sanitize_encoder(encoder);
15823 }
15824
Damien Lespiau055e3932014-08-18 13:49:10 +010015825 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015826 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15827 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015828 intel_dump_pipe_config(crtc, crtc->config,
15829 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015830 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015831
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015832 intel_modeset_update_connector_atomic_state(dev);
15833
Daniel Vetter35c95372013-07-17 06:55:04 +020015834 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15835 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15836
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015837 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015838 continue;
15839
15840 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15841
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015842 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015843 pll->on = false;
15844 }
15845
Wayne Boyer666a4532015-12-09 12:29:35 -080015846 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015847 vlv_wm_get_hw_state(dev);
15848 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015849 skl_wm_get_hw_state(dev);
15850 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015851 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015852
15853 for_each_intel_crtc(dev, crtc) {
15854 unsigned long put_domains;
15855
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015856 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015857 if (WARN_ON(put_domains))
15858 modeset_put_power_domains(dev_priv, put_domains);
15859 }
15860 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015861
15862 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015863}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015864
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015865void intel_display_resume(struct drm_device *dev)
15866{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015867 struct drm_i915_private *dev_priv = to_i915(dev);
15868 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15869 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015870 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015871 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020015872
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015873 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015874
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015875 /*
15876 * This is a cludge because with real atomic modeset mode_config.mutex
15877 * won't be taken. Unfortunately some probed state like
15878 * audio_codec_enable is still protected by mode_config.mutex, so lock
15879 * it here for now.
15880 */
15881 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015882 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015883
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015884retry:
15885 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015886
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015887 if (ret == 0 && !setup) {
15888 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015889
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015890 intel_modeset_setup_hw_state(dev);
15891 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015892 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015893
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015894 if (ret == 0 && state) {
15895 struct drm_crtc_state *crtc_state;
15896 struct drm_crtc *crtc;
15897 int i;
15898
15899 state->acquire_ctx = &ctx;
15900
15901 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15902 /*
15903 * Force recalculation even if we restore
15904 * current state. With fast modeset this may not result
15905 * in a modeset when the state is compatible.
15906 */
15907 crtc_state->mode_changed = true;
15908 }
15909
15910 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015911 }
15912
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015913 if (ret == -EDEADLK) {
15914 drm_modeset_backoff(&ctx);
15915 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015916 }
15917
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015918 drm_modeset_drop_locks(&ctx);
15919 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015920 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015921
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015922 if (ret) {
15923 DRM_ERROR("Restoring old state failed with %i\n", ret);
15924 drm_atomic_state_free(state);
15925 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015926}
15927
15928void intel_modeset_gem_init(struct drm_device *dev)
15929{
Jesse Barnes484b41d2014-03-07 08:57:55 -080015930 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015931 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015932 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015933
Imre Deakae484342014-03-31 15:10:44 +030015934 intel_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +030015935
Chris Wilson1833b132012-05-09 11:56:28 +010015936 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015937
15938 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015939
15940 /*
15941 * Make sure any fbs we allocated at startup are properly
15942 * pinned & fenced. When we do the allocation it's too early
15943 * for this.
15944 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015945 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015946 obj = intel_fb_obj(c->primary->fb);
15947 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015948 continue;
15949
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015950 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020015951 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
15952 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015953 mutex_unlock(&dev->struct_mutex);
15954 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015955 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15956 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015957 drm_framebuffer_unreference(c->primary->fb);
15958 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015959 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015960 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015961 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015962 }
15963 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015964
15965 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015966}
15967
Imre Deak4932e2c2014-02-11 17:12:48 +020015968void intel_connector_unregister(struct intel_connector *intel_connector)
15969{
15970 struct drm_connector *connector = &intel_connector->base;
15971
15972 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015973 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015974}
15975
Jesse Barnes79e53942008-11-07 14:24:08 -080015976void intel_modeset_cleanup(struct drm_device *dev)
15977{
Jesse Barnes652c3932009-08-17 13:31:43 -070015978 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020015979 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015980
Imre Deak2eb52522014-11-19 15:30:05 +020015981 intel_disable_gt_powersave(dev);
15982
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015983 intel_backlight_unregister(dev);
15984
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015985 /*
15986 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015987 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015988 * experience fancy races otherwise.
15989 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015990 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015991
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015992 /*
15993 * Due to the hpd irq storm handling the hotplug work can re-arm the
15994 * poll handlers. Hence disable polling after hpd handling is shut down.
15995 */
Keith Packardf87ea762010-10-03 19:36:26 -070015996 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015997
Jesse Barnes723bfd72010-10-07 16:01:13 -070015998 intel_unregister_dsm_handler();
15999
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016000 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016001
Chris Wilson1630fe72011-07-08 12:22:42 +010016002 /* flush any delayed tasks or pending work */
16003 flush_scheduled_work();
16004
Jani Nikuladb31af1d2013-11-08 16:48:53 +020016005 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020016006 for_each_intel_connector(dev, connector)
16007 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030016008
Jesse Barnes79e53942008-11-07 14:24:08 -080016009 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016010
16011 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030016012
Imre Deakae484342014-03-31 15:10:44 +030016013 intel_cleanup_gt_powersave(dev);
Daniel Vetterf5949142016-01-13 11:55:28 +010016014
16015 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016016}
16017
Dave Airlie28d52042009-09-21 14:33:58 +100016018/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080016019 * Return which encoder is currently attached for connector.
16020 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010016021struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080016022{
Chris Wilsondf0e9242010-09-09 16:20:55 +010016023 return &intel_attached_encoder(connector)->base;
16024}
Jesse Barnes79e53942008-11-07 14:24:08 -080016025
Chris Wilsondf0e9242010-09-09 16:20:55 +010016026void intel_connector_attach_encoder(struct intel_connector *connector,
16027 struct intel_encoder *encoder)
16028{
16029 connector->encoder = encoder;
16030 drm_mode_connector_attach_encoder(&connector->base,
16031 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016032}
Dave Airlie28d52042009-09-21 14:33:58 +100016033
16034/*
16035 * set vga decode state - true == enable VGA decode
16036 */
16037int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16038{
16039 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016040 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016041 u16 gmch_ctrl;
16042
Chris Wilson75fa0412014-02-07 18:37:02 -020016043 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16044 DRM_ERROR("failed to read control word\n");
16045 return -EIO;
16046 }
16047
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016048 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16049 return 0;
16050
Dave Airlie28d52042009-09-21 14:33:58 +100016051 if (state)
16052 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16053 else
16054 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016055
16056 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16057 DRM_ERROR("failed to write control word\n");
16058 return -EIO;
16059 }
16060
Dave Airlie28d52042009-09-21 14:33:58 +100016061 return 0;
16062}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016063
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016064struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016065
16066 u32 power_well_driver;
16067
Chris Wilson63b66e52013-08-08 15:12:06 +020016068 int num_transcoders;
16069
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016070 struct intel_cursor_error_state {
16071 u32 control;
16072 u32 position;
16073 u32 base;
16074 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016075 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016076
16077 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016078 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016079 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030016080 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016081 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016082
16083 struct intel_plane_error_state {
16084 u32 control;
16085 u32 stride;
16086 u32 size;
16087 u32 pos;
16088 u32 addr;
16089 u32 surface;
16090 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016091 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016092
16093 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016094 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016095 enum transcoder cpu_transcoder;
16096
16097 u32 conf;
16098
16099 u32 htotal;
16100 u32 hblank;
16101 u32 hsync;
16102 u32 vtotal;
16103 u32 vblank;
16104 u32 vsync;
16105 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016106};
16107
16108struct intel_display_error_state *
16109intel_display_capture_error_state(struct drm_device *dev)
16110{
Jani Nikulafbee40d2014-03-31 14:27:18 +030016111 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016112 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016113 int transcoders[] = {
16114 TRANSCODER_A,
16115 TRANSCODER_B,
16116 TRANSCODER_C,
16117 TRANSCODER_EDP,
16118 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016119 int i;
16120
Chris Wilson63b66e52013-08-08 15:12:06 +020016121 if (INTEL_INFO(dev)->num_pipes == 0)
16122 return NULL;
16123
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016124 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016125 if (error == NULL)
16126 return NULL;
16127
Imre Deak190be112013-11-25 17:15:31 +020016128 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016129 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16130
Damien Lespiau055e3932014-08-18 13:49:10 +010016131 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016132 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016133 __intel_display_power_is_enabled(dev_priv,
16134 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016135 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016136 continue;
16137
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016138 error->cursor[i].control = I915_READ(CURCNTR(i));
16139 error->cursor[i].position = I915_READ(CURPOS(i));
16140 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016141
16142 error->plane[i].control = I915_READ(DSPCNTR(i));
16143 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016144 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016145 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016146 error->plane[i].pos = I915_READ(DSPPOS(i));
16147 }
Paulo Zanonica291362013-03-06 20:03:14 -030016148 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16149 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016150 if (INTEL_INFO(dev)->gen >= 4) {
16151 error->plane[i].surface = I915_READ(DSPSURF(i));
16152 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16153 }
16154
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016155 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030016156
Sonika Jindal3abfce72014-07-21 15:23:43 +053016157 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e12014-04-18 15:55:04 +030016158 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016159 }
16160
Jani Nikula4d1de972016-03-18 17:05:42 +020016161 /* Note: this does not include DSI transcoders. */
Chris Wilson63b66e52013-08-08 15:12:06 +020016162 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16163 if (HAS_DDI(dev_priv->dev))
16164 error->num_transcoders++; /* Account for eDP. */
16165
16166 for (i = 0; i < error->num_transcoders; i++) {
16167 enum transcoder cpu_transcoder = transcoders[i];
16168
Imre Deakddf9c532013-11-27 22:02:02 +020016169 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016170 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016171 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016172 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016173 continue;
16174
Chris Wilson63b66e52013-08-08 15:12:06 +020016175 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16176
16177 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16178 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16179 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16180 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16181 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16182 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16183 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016184 }
16185
16186 return error;
16187}
16188
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016189#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16190
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016191void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016192intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016193 struct drm_device *dev,
16194 struct intel_display_error_state *error)
16195{
Damien Lespiau055e3932014-08-18 13:49:10 +010016196 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016197 int i;
16198
Chris Wilson63b66e52013-08-08 15:12:06 +020016199 if (!error)
16200 return;
16201
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016202 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016203 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016204 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016205 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016206 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016207 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016208 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016209 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016210 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030016211 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016212
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016213 err_printf(m, "Plane [%d]:\n", i);
16214 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16215 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016216 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016217 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16218 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016219 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016220 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016221 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016222 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016223 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16224 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016225 }
16226
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016227 err_printf(m, "Cursor [%d]:\n", i);
16228 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16229 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16230 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016231 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016232
16233 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016234 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016235 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016236 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016237 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016238 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16239 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16240 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16241 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16242 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16243 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16244 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16245 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016246}