blob: fec6392dfc02851c2980226573b72e032be37367 [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äf0f59a02015-11-18 15:33:26 +02001531 i915_reg_t reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001532 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001533
Daniel Vetter426115c2013-07-11 22:13:42 +02001534 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001535
Daniel Vetter87442f72013-06-06 00:52:17 +02001536 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001537 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001538 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001539
Daniel Vetter426115c2013-07-11 22:13:42 +02001540 I915_WRITE(reg, dpll);
1541 POSTING_READ(reg);
1542 udelay(150);
1543
1544 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1545 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1546
Ville Syrjäläd288f652014-10-28 13:20:22 +02001547 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001548 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001549
1550 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001551 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001552 POSTING_READ(reg);
1553 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001554 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001555 POSTING_READ(reg);
1556 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001557 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001558 POSTING_READ(reg);
1559 udelay(150); /* wait for warmup */
1560}
1561
Ville Syrjäläd288f652014-10-28 13:20:22 +02001562static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001563 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001564{
1565 struct drm_device *dev = crtc->base.dev;
1566 struct drm_i915_private *dev_priv = dev->dev_private;
1567 int pipe = crtc->pipe;
1568 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001569 u32 tmp;
1570
1571 assert_pipe_disabled(dev_priv, crtc->pipe);
1572
Ville Syrjäläa5805162015-05-26 20:42:30 +03001573 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001574
1575 /* Enable back the 10bit clock to display controller */
1576 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1577 tmp |= DPIO_DCLKP_EN;
1578 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1579
Ville Syrjälä54433e92015-05-26 20:42:31 +03001580 mutex_unlock(&dev_priv->sb_lock);
1581
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001582 /*
1583 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1584 */
1585 udelay(1);
1586
1587 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001588 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001589
1590 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001591 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001592 DRM_ERROR("PLL %d failed to lock\n", pipe);
1593
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001594 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001595 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001596 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001597}
1598
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001599static int intel_num_dvo_pipes(struct drm_device *dev)
1600{
1601 struct intel_crtc *crtc;
1602 int count = 0;
1603
1604 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001605 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001606 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001607
1608 return count;
1609}
1610
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001611static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001612{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001613 struct drm_device *dev = crtc->base.dev;
1614 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001615 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001616 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001617
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001618 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001619
1620 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001621 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001622
1623 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001624 if (IS_MOBILE(dev) && !IS_I830(dev))
1625 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001626
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001627 /* Enable DVO 2x clock on both PLLs if necessary */
1628 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1629 /*
1630 * It appears to be important that we don't enable this
1631 * for the current pipe before otherwise configuring the
1632 * PLL. No idea how this should be handled if multiple
1633 * DVO outputs are enabled simultaneosly.
1634 */
1635 dpll |= DPLL_DVO_2X_MODE;
1636 I915_WRITE(DPLL(!crtc->pipe),
1637 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1638 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001639
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001640 /*
1641 * Apparently we need to have VGA mode enabled prior to changing
1642 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1643 * dividers, even though the register value does change.
1644 */
1645 I915_WRITE(reg, 0);
1646
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001647 I915_WRITE(reg, dpll);
1648
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001649 /* Wait for the clocks to stabilize. */
1650 POSTING_READ(reg);
1651 udelay(150);
1652
1653 if (INTEL_INFO(dev)->gen >= 4) {
1654 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001655 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001656 } else {
1657 /* The pixel multiplier can only be updated once the
1658 * DPLL is enabled and the clocks are stable.
1659 *
1660 * So write it again.
1661 */
1662 I915_WRITE(reg, dpll);
1663 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001664
1665 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001666 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001667 POSTING_READ(reg);
1668 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001669 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001670 POSTING_READ(reg);
1671 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001672 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001673 POSTING_READ(reg);
1674 udelay(150); /* wait for warmup */
1675}
1676
1677/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001678 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001679 * @dev_priv: i915 private structure
1680 * @pipe: pipe PLL to disable
1681 *
1682 * Disable the PLL for @pipe, making sure the pipe is off first.
1683 *
1684 * Note! This is for pre-ILK only.
1685 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001686static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001687{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001688 struct drm_device *dev = crtc->base.dev;
1689 struct drm_i915_private *dev_priv = dev->dev_private;
1690 enum pipe pipe = crtc->pipe;
1691
1692 /* Disable DVO 2x clock on both PLLs if necessary */
1693 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001694 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001695 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001696 I915_WRITE(DPLL(PIPE_B),
1697 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1698 I915_WRITE(DPLL(PIPE_A),
1699 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1700 }
1701
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001702 /* Don't disable pipe or pipe PLLs if needed */
1703 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1704 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001705 return;
1706
1707 /* Make sure the pipe isn't still relying on us */
1708 assert_pipe_disabled(dev_priv, pipe);
1709
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001710 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001711 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001712}
1713
Jesse Barnesf6071162013-10-01 10:41:38 -07001714static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1715{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001716 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001717
1718 /* Make sure the pipe isn't still relying on us */
1719 assert_pipe_disabled(dev_priv, pipe);
1720
Imre Deake5cbfbf2014-01-09 17:08:16 +02001721 /*
1722 * Leave integrated clock source and reference clock enabled for pipe B.
1723 * The latter is needed for VGA hotplug / manual detection.
1724 */
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001725 val = DPLL_VGA_MODE_DIS;
Jesse Barnesf6071162013-10-01 10:41:38 -07001726 if (pipe == PIPE_B)
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001727 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001728 I915_WRITE(DPLL(pipe), val);
1729 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001730
1731}
1732
1733static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1734{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001735 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001736 u32 val;
1737
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001738 /* Make sure the pipe isn't still relying on us */
1739 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001740
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001741 /* Set PLL en = 0 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001742 val = DPLL_SSC_REF_CLK_CHV |
1743 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001744 if (pipe != PIPE_A)
1745 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1746 I915_WRITE(DPLL(pipe), val);
1747 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001748
Ville Syrjäläa5805162015-05-26 20:42:30 +03001749 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001750
1751 /* Disable 10bit clock to display controller */
1752 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1753 val &= ~DPIO_DCLKP_EN;
1754 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1755
Ville Syrjäläa5805162015-05-26 20:42:30 +03001756 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001757}
1758
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001759void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001760 struct intel_digital_port *dport,
1761 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001762{
1763 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001764 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001765
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001766 switch (dport->port) {
1767 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001768 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001769 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001770 break;
1771 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001772 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001773 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001774 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001775 break;
1776 case PORT_D:
1777 port_mask = DPLL_PORTD_READY_MASK;
1778 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001779 break;
1780 default:
1781 BUG();
1782 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001783
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001784 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1785 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1786 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001787}
1788
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001789static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1790 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001791{
Daniel Vetter23670b322012-11-01 09:15:30 +01001792 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001793 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001794 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001795 i915_reg_t reg;
1796 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001797
1798 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001799 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001800
1801 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001802 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001803
1804 /* FDI must be feeding us bits for PCH ports */
1805 assert_fdi_tx_enabled(dev_priv, pipe);
1806 assert_fdi_rx_enabled(dev_priv, pipe);
1807
Daniel Vetter23670b322012-11-01 09:15:30 +01001808 if (HAS_PCH_CPT(dev)) {
1809 /* Workaround: Set the timing override bit before enabling the
1810 * pch transcoder. */
1811 reg = TRANS_CHICKEN2(pipe);
1812 val = I915_READ(reg);
1813 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1814 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001815 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001816
Daniel Vetterab9412b2013-05-03 11:49:46 +02001817 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001818 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001819 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001820
1821 if (HAS_PCH_IBX(dev_priv->dev)) {
1822 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001823 * Make the BPC in transcoder be consistent with
1824 * that in pipeconf reg. For HDMI we must use 8bpc
1825 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001826 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001827 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001828 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1829 val |= PIPECONF_8BPC;
1830 else
1831 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001832 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001833
1834 val &= ~TRANS_INTERLACE_MASK;
1835 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001836 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001837 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001838 val |= TRANS_LEGACY_INTERLACED_ILK;
1839 else
1840 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001841 else
1842 val |= TRANS_PROGRESSIVE;
1843
Jesse Barnes040484a2011-01-03 12:14:26 -08001844 I915_WRITE(reg, val | TRANS_ENABLE);
1845 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001846 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001847}
1848
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001849static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001850 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001851{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001852 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001853
1854 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001855 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001856
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001857 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001858 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001859 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001860
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001861 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001862 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001863 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001864 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001865
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001866 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001867 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001868
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001869 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1870 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001871 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001872 else
1873 val |= TRANS_PROGRESSIVE;
1874
Daniel Vetterab9412b2013-05-03 11:49:46 +02001875 I915_WRITE(LPT_TRANSCONF, val);
1876 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001877 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001878}
1879
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001880static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1881 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001882{
Daniel Vetter23670b322012-11-01 09:15:30 +01001883 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001884 i915_reg_t reg;
1885 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001886
1887 /* FDI relies on the transcoder */
1888 assert_fdi_tx_disabled(dev_priv, pipe);
1889 assert_fdi_rx_disabled(dev_priv, pipe);
1890
Jesse Barnes291906f2011-02-02 12:28:03 -08001891 /* Ports must be off as well */
1892 assert_pch_ports_disabled(dev_priv, pipe);
1893
Daniel Vetterab9412b2013-05-03 11:49:46 +02001894 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001895 val = I915_READ(reg);
1896 val &= ~TRANS_ENABLE;
1897 I915_WRITE(reg, val);
1898 /* wait for PCH transcoder off, transcoder state */
1899 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001900 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001901
Ville Syrjäläc4656132015-10-29 21:25:56 +02001902 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001903 /* Workaround: Clear the timing override chicken bit again. */
1904 reg = TRANS_CHICKEN2(pipe);
1905 val = I915_READ(reg);
1906 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1907 I915_WRITE(reg, val);
1908 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001909}
1910
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001911static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001912{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001913 u32 val;
1914
Daniel Vetterab9412b2013-05-03 11:49:46 +02001915 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001916 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001917 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001918 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001919 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001920 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001921
1922 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001923 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001924 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001925 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001926}
1927
1928/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001929 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001930 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001931 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001932 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001933 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001934 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001935static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001936{
Paulo Zanoni03722642014-01-17 13:51:09 -02001937 struct drm_device *dev = crtc->base.dev;
1938 struct drm_i915_private *dev_priv = dev->dev_private;
1939 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001940 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001941 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001942 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001943 u32 val;
1944
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001945 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1946
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001947 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001948 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001949 assert_sprites_disabled(dev_priv, pipe);
1950
Paulo Zanoni681e5812012-12-06 11:12:38 -02001951 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001952 pch_transcoder = TRANSCODER_A;
1953 else
1954 pch_transcoder = pipe;
1955
Jesse Barnesb24e7172011-01-04 15:09:30 -08001956 /*
1957 * A pipe without a PLL won't actually be able to drive bits from
1958 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1959 * need the check.
1960 */
Imre Deak50360402015-01-16 00:55:16 -08001961 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Jani Nikulaa65347b2015-11-27 12:21:46 +02001962 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03001963 assert_dsi_pll_enabled(dev_priv);
1964 else
1965 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001966 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001967 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001968 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001969 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001970 assert_fdi_tx_pll_enabled(dev_priv,
1971 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001972 }
1973 /* FIXME: assert CPU port conditions for SNB+ */
1974 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001975
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001976 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001977 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001978 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001979 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1980 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00001981 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001982 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001983
1984 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001985 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001986
1987 /*
1988 * Until the pipe starts DSL will read as 0, which would cause
1989 * an apparent vblank timestamp jump, which messes up also the
1990 * frame count when it's derived from the timestamps. So let's
1991 * wait for the pipe to start properly before we call
1992 * drm_crtc_vblank_on()
1993 */
1994 if (dev->max_vblank_count == 0 &&
1995 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1996 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001997}
1998
1999/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002000 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002001 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002002 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002003 * Disable the pipe of @crtc, making sure that various hardware
2004 * specific requirements are met, if applicable, e.g. plane
2005 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002006 *
2007 * Will wait until the pipe has shut down before returning.
2008 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002009static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002010{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002011 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002012 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002013 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002014 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002015 u32 val;
2016
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002017 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2018
Jesse Barnesb24e7172011-01-04 15:09:30 -08002019 /*
2020 * Make sure planes won't keep trying to pump pixels to us,
2021 * or we might hang the display.
2022 */
2023 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002024 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002025 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002026
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002027 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002028 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002029 if ((val & PIPECONF_ENABLE) == 0)
2030 return;
2031
Ville Syrjälä67adc642014-08-15 01:21:57 +03002032 /*
2033 * Double wide has implications for planes
2034 * so best keep it disabled when not needed.
2035 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002036 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002037 val &= ~PIPECONF_DOUBLE_WIDE;
2038
2039 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002040 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2041 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002042 val &= ~PIPECONF_ENABLE;
2043
2044 I915_WRITE(reg, val);
2045 if ((val & PIPECONF_ENABLE) == 0)
2046 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002047}
2048
Chris Wilson693db182013-03-05 14:52:39 +00002049static bool need_vtd_wa(struct drm_device *dev)
2050{
2051#ifdef CONFIG_INTEL_IOMMU
2052 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2053 return true;
2054#endif
2055 return false;
2056}
2057
Ville Syrjälä832be822016-01-12 21:08:33 +02002058static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2059{
2060 return IS_GEN2(dev_priv) ? 2048 : 4096;
2061}
2062
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002063static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2064 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002065{
2066 switch (fb_modifier) {
2067 case DRM_FORMAT_MOD_NONE:
2068 return cpp;
2069 case I915_FORMAT_MOD_X_TILED:
2070 if (IS_GEN2(dev_priv))
2071 return 128;
2072 else
2073 return 512;
2074 case I915_FORMAT_MOD_Y_TILED:
2075 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2076 return 128;
2077 else
2078 return 512;
2079 case I915_FORMAT_MOD_Yf_TILED:
2080 switch (cpp) {
2081 case 1:
2082 return 64;
2083 case 2:
2084 case 4:
2085 return 128;
2086 case 8:
2087 case 16:
2088 return 256;
2089 default:
2090 MISSING_CASE(cpp);
2091 return cpp;
2092 }
2093 break;
2094 default:
2095 MISSING_CASE(fb_modifier);
2096 return cpp;
2097 }
2098}
2099
Ville Syrjälä832be822016-01-12 21:08:33 +02002100unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2101 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002102{
Ville Syrjälä832be822016-01-12 21:08:33 +02002103 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2104 return 1;
2105 else
2106 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002107 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002108}
2109
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002110/* Return the tile dimensions in pixel units */
2111static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2112 unsigned int *tile_width,
2113 unsigned int *tile_height,
2114 uint64_t fb_modifier,
2115 unsigned int cpp)
2116{
2117 unsigned int tile_width_bytes =
2118 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2119
2120 *tile_width = tile_width_bytes / cpp;
2121 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2122}
2123
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002124unsigned int
2125intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002126 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002127{
Ville Syrjälä832be822016-01-12 21:08:33 +02002128 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2129 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2130
2131 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002132}
2133
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002134unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2135{
2136 unsigned int size = 0;
2137 int i;
2138
2139 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2140 size += rot_info->plane[i].width * rot_info->plane[i].height;
2141
2142 return size;
2143}
2144
Daniel Vetter75c82a52015-10-14 16:51:04 +02002145static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002146intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2147 const struct drm_framebuffer *fb,
2148 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002149{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002150 if (intel_rotation_90_or_270(rotation)) {
2151 *view = i915_ggtt_view_rotated;
2152 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2153 } else {
2154 *view = i915_ggtt_view_normal;
2155 }
2156}
2157
2158static void
2159intel_fill_fb_info(struct drm_i915_private *dev_priv,
2160 struct drm_framebuffer *fb)
2161{
2162 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002163 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002164
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002165 tile_size = intel_tile_size(dev_priv);
2166
2167 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002168 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2169 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002170
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002171 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2172 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002173
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002174 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002175 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002176 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2177 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002178
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002179 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002180 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2181 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002182 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002183}
2184
Ville Syrjälä603525d2016-01-12 21:08:37 +02002185static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002186{
2187 if (INTEL_INFO(dev_priv)->gen >= 9)
2188 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002189 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002190 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002191 return 128 * 1024;
2192 else if (INTEL_INFO(dev_priv)->gen >= 4)
2193 return 4 * 1024;
2194 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002195 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002196}
2197
Ville Syrjälä603525d2016-01-12 21:08:37 +02002198static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2199 uint64_t fb_modifier)
2200{
2201 switch (fb_modifier) {
2202 case DRM_FORMAT_MOD_NONE:
2203 return intel_linear_alignment(dev_priv);
2204 case I915_FORMAT_MOD_X_TILED:
2205 if (INTEL_INFO(dev_priv)->gen >= 9)
2206 return 256 * 1024;
2207 return 0;
2208 case I915_FORMAT_MOD_Y_TILED:
2209 case I915_FORMAT_MOD_Yf_TILED:
2210 return 1 * 1024 * 1024;
2211 default:
2212 MISSING_CASE(fb_modifier);
2213 return 0;
2214 }
2215}
2216
Chris Wilson127bd2a2010-07-23 23:32:05 +01002217int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002218intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2219 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002220{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002221 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002222 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002223 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002224 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002225 u32 alignment;
2226 int ret;
2227
Matt Roperebcdd392014-07-09 16:22:11 -07002228 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2229
Ville Syrjälä603525d2016-01-12 21:08:37 +02002230 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002231
Ville Syrjälä3465c582016-02-15 22:54:43 +02002232 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002233
Chris Wilson693db182013-03-05 14:52:39 +00002234 /* Note that the w/a also requires 64 PTE of padding following the
2235 * bo. We currently fill all unused PTE with the shadow page and so
2236 * we should always have valid PTE following the scanout preventing
2237 * the VT-d warning.
2238 */
2239 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2240 alignment = 256 * 1024;
2241
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002242 /*
2243 * Global gtt pte registers are special registers which actually forward
2244 * writes to a chunk of system memory. Which means that there is no risk
2245 * that the register values disappear as soon as we call
2246 * intel_runtime_pm_put(), so it is correct to wrap only the
2247 * pin/unpin/fence and not more.
2248 */
2249 intel_runtime_pm_get(dev_priv);
2250
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002251 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2252 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002253 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002254 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002255
2256 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2257 * fence, whereas 965+ only requires a fence if using
2258 * framebuffer compression. For simplicity, we always install
2259 * a fence as the cost is not that onerous.
2260 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002261 if (view.type == I915_GGTT_VIEW_NORMAL) {
2262 ret = i915_gem_object_get_fence(obj);
2263 if (ret == -EDEADLK) {
2264 /*
2265 * -EDEADLK means there are no free fences
2266 * no pending flips.
2267 *
2268 * This is propagated to atomic, but it uses
2269 * -EDEADLK to force a locking recovery, so
2270 * change the returned error to -EBUSY.
2271 */
2272 ret = -EBUSY;
2273 goto err_unpin;
2274 } else if (ret)
2275 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002276
Vivek Kasireddy98072162015-10-29 18:54:38 -07002277 i915_gem_object_pin_fence(obj);
2278 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002279
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002280 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002281 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002282
2283err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002284 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002285err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002286 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002287 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002288}
2289
Ville Syrjälä3465c582016-02-15 22:54:43 +02002290static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002291{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002292 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002293 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002294
Matt Roperebcdd392014-07-09 16:22:11 -07002295 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2296
Ville Syrjälä3465c582016-02-15 22:54:43 +02002297 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002298
Vivek Kasireddy98072162015-10-29 18:54:38 -07002299 if (view.type == I915_GGTT_VIEW_NORMAL)
2300 i915_gem_object_unpin_fence(obj);
2301
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002302 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002303}
2304
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002305/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002306 * Adjust the tile offset by moving the difference into
2307 * the x/y offsets.
2308 *
2309 * Input tile dimensions and pitch must already be
2310 * rotated to match x and y, and in pixel units.
2311 */
2312static u32 intel_adjust_tile_offset(int *x, int *y,
2313 unsigned int tile_width,
2314 unsigned int tile_height,
2315 unsigned int tile_size,
2316 unsigned int pitch_tiles,
2317 u32 old_offset,
2318 u32 new_offset)
2319{
2320 unsigned int tiles;
2321
2322 WARN_ON(old_offset & (tile_size - 1));
2323 WARN_ON(new_offset & (tile_size - 1));
2324 WARN_ON(new_offset > old_offset);
2325
2326 tiles = (old_offset - new_offset) / tile_size;
2327
2328 *y += tiles / pitch_tiles * tile_height;
2329 *x += tiles % pitch_tiles * tile_width;
2330
2331 return new_offset;
2332}
2333
2334/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002335 * Computes the linear offset to the base tile and adjusts
2336 * x, y. bytes per pixel is assumed to be a power-of-two.
2337 *
2338 * In the 90/270 rotated case, x and y are assumed
2339 * to be already rotated to match the rotated GTT view, and
2340 * pitch is the tile_height aligned framebuffer height.
2341 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002342u32 intel_compute_tile_offset(int *x, int *y,
2343 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002344 unsigned int pitch,
2345 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002346{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002347 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2348 uint64_t fb_modifier = fb->modifier[plane];
2349 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002350 u32 offset, offset_aligned, alignment;
2351
2352 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2353 if (alignment)
2354 alignment--;
2355
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002356 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002357 unsigned int tile_size, tile_width, tile_height;
2358 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002359
Ville Syrjäläd8433102016-01-12 21:08:35 +02002360 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002361 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2362 fb_modifier, cpp);
2363
2364 if (intel_rotation_90_or_270(rotation)) {
2365 pitch_tiles = pitch / tile_height;
2366 swap(tile_width, tile_height);
2367 } else {
2368 pitch_tiles = pitch / (tile_width * cpp);
2369 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002370
Ville Syrjäläd8433102016-01-12 21:08:35 +02002371 tile_rows = *y / tile_height;
2372 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002373
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002374 tiles = *x / tile_width;
2375 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002376
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002377 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2378 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002379
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002380 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2381 tile_size, pitch_tiles,
2382 offset, offset_aligned);
2383 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002384 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002385 offset_aligned = offset & ~alignment;
2386
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002387 *y = (offset & alignment) / pitch;
2388 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002389 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002390
2391 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002392}
2393
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002394static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002395{
2396 switch (format) {
2397 case DISPPLANE_8BPP:
2398 return DRM_FORMAT_C8;
2399 case DISPPLANE_BGRX555:
2400 return DRM_FORMAT_XRGB1555;
2401 case DISPPLANE_BGRX565:
2402 return DRM_FORMAT_RGB565;
2403 default:
2404 case DISPPLANE_BGRX888:
2405 return DRM_FORMAT_XRGB8888;
2406 case DISPPLANE_RGBX888:
2407 return DRM_FORMAT_XBGR8888;
2408 case DISPPLANE_BGRX101010:
2409 return DRM_FORMAT_XRGB2101010;
2410 case DISPPLANE_RGBX101010:
2411 return DRM_FORMAT_XBGR2101010;
2412 }
2413}
2414
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002415static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2416{
2417 switch (format) {
2418 case PLANE_CTL_FORMAT_RGB_565:
2419 return DRM_FORMAT_RGB565;
2420 default:
2421 case PLANE_CTL_FORMAT_XRGB_8888:
2422 if (rgb_order) {
2423 if (alpha)
2424 return DRM_FORMAT_ABGR8888;
2425 else
2426 return DRM_FORMAT_XBGR8888;
2427 } else {
2428 if (alpha)
2429 return DRM_FORMAT_ARGB8888;
2430 else
2431 return DRM_FORMAT_XRGB8888;
2432 }
2433 case PLANE_CTL_FORMAT_XRGB_2101010:
2434 if (rgb_order)
2435 return DRM_FORMAT_XBGR2101010;
2436 else
2437 return DRM_FORMAT_XRGB2101010;
2438 }
2439}
2440
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002441static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002442intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2443 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002444{
2445 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002446 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002447 struct drm_i915_gem_object *obj = NULL;
2448 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002449 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002450 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2451 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2452 PAGE_SIZE);
2453
2454 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002455
Chris Wilsonff2652e2014-03-10 08:07:02 +00002456 if (plane_config->size == 0)
2457 return false;
2458
Paulo Zanoni3badb492015-09-23 12:52:23 -03002459 /* If the FB is too big, just don't use it since fbdev is not very
2460 * important and we should probably use that space with FBC or other
2461 * features. */
Joonas Lahtinen62106b42016-03-18 10:42:57 +02002462 if (size_aligned * 2 > dev_priv->ggtt.stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002463 return false;
2464
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002465 mutex_lock(&dev->struct_mutex);
2466
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002467 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2468 base_aligned,
2469 base_aligned,
2470 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002471 if (!obj) {
2472 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002473 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002474 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002475
Damien Lespiau49af4492015-01-20 12:51:44 +00002476 obj->tiling_mode = plane_config->tiling;
2477 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002478 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002479
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002480 mode_cmd.pixel_format = fb->pixel_format;
2481 mode_cmd.width = fb->width;
2482 mode_cmd.height = fb->height;
2483 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002484 mode_cmd.modifier[0] = fb->modifier[0];
2485 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002486
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002487 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002488 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002489 DRM_DEBUG_KMS("intel fb init failed\n");
2490 goto out_unref_obj;
2491 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002492
Jesse Barnes46f297f2014-03-07 08:57:48 -08002493 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002494
Daniel Vetterf6936e22015-03-26 12:17:05 +01002495 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002496 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002497
2498out_unref_obj:
2499 drm_gem_object_unreference(&obj->base);
2500 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002501 return false;
2502}
2503
Matt Roperafd65eb2015-02-03 13:10:04 -08002504/* Update plane->state->fb to match plane->fb after driver-internal updates */
2505static void
2506update_state_fb(struct drm_plane *plane)
2507{
2508 if (plane->fb == plane->state->fb)
2509 return;
2510
2511 if (plane->state->fb)
2512 drm_framebuffer_unreference(plane->state->fb);
2513 plane->state->fb = plane->fb;
2514 if (plane->state->fb)
2515 drm_framebuffer_reference(plane->state->fb);
2516}
2517
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002518static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002519intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2520 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002521{
2522 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002523 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002524 struct drm_crtc *c;
2525 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002526 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002527 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002528 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002529 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2530 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002531 struct intel_plane_state *intel_state =
2532 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002533 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002534
Damien Lespiau2d140302015-02-05 17:22:18 +00002535 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002536 return;
2537
Daniel Vetterf6936e22015-03-26 12:17:05 +01002538 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002539 fb = &plane_config->fb->base;
2540 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002541 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002542
Damien Lespiau2d140302015-02-05 17:22:18 +00002543 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002544
2545 /*
2546 * Failed to alloc the obj, check to see if we should share
2547 * an fb with another CRTC instead
2548 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002549 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002550 i = to_intel_crtc(c);
2551
2552 if (c == &intel_crtc->base)
2553 continue;
2554
Matt Roper2ff8fde2014-07-08 07:50:07 -07002555 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002556 continue;
2557
Daniel Vetter88595ac2015-03-26 12:42:24 +01002558 fb = c->primary->fb;
2559 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002560 continue;
2561
Daniel Vetter88595ac2015-03-26 12:42:24 +01002562 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002563 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002564 drm_framebuffer_reference(fb);
2565 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002566 }
2567 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002568
Matt Roper200757f2015-12-03 11:37:36 -08002569 /*
2570 * We've failed to reconstruct the BIOS FB. Current display state
2571 * indicates that the primary plane is visible, but has a NULL FB,
2572 * which will lead to problems later if we don't fix it up. The
2573 * simplest solution is to just disable the primary plane now and
2574 * pretend the BIOS never had it enabled.
2575 */
2576 to_intel_plane_state(plane_state)->visible = false;
2577 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002578 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002579 intel_plane->disable_plane(primary, &intel_crtc->base);
2580
Daniel Vetter88595ac2015-03-26 12:42:24 +01002581 return;
2582
2583valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002584 plane_state->src_x = 0;
2585 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002586 plane_state->src_w = fb->width << 16;
2587 plane_state->src_h = fb->height << 16;
2588
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002589 plane_state->crtc_x = 0;
2590 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002591 plane_state->crtc_w = fb->width;
2592 plane_state->crtc_h = fb->height;
2593
Matt Roper0a8d8a82015-12-03 11:37:38 -08002594 intel_state->src.x1 = plane_state->src_x;
2595 intel_state->src.y1 = plane_state->src_y;
2596 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2597 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2598 intel_state->dst.x1 = plane_state->crtc_x;
2599 intel_state->dst.y1 = plane_state->crtc_y;
2600 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2601 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2602
Daniel Vetter88595ac2015-03-26 12:42:24 +01002603 obj = intel_fb_obj(fb);
2604 if (obj->tiling_mode != I915_TILING_NONE)
2605 dev_priv->preserve_bios_swizzle = true;
2606
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002607 drm_framebuffer_reference(fb);
2608 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002609 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002610 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002611 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002612}
2613
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002614static void i9xx_update_primary_plane(struct drm_plane *primary,
2615 const struct intel_crtc_state *crtc_state,
2616 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002617{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002618 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002619 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002620 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2621 struct drm_framebuffer *fb = plane_state->base.fb;
2622 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002623 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002624 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002625 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002626 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002627 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002628 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002629 int x = plane_state->src.x1 >> 16;
2630 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002631
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002632 dspcntr = DISPPLANE_GAMMA_ENABLE;
2633
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002634 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002635
2636 if (INTEL_INFO(dev)->gen < 4) {
2637 if (intel_crtc->pipe == PIPE_B)
2638 dspcntr |= DISPPLANE_SEL_PIPE_B;
2639
2640 /* pipesrc and dspsize control the size that is scaled from,
2641 * which should always be the user's requested size.
2642 */
2643 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002644 ((crtc_state->pipe_src_h - 1) << 16) |
2645 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002646 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002647 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2648 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002649 ((crtc_state->pipe_src_h - 1) << 16) |
2650 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002651 I915_WRITE(PRIMPOS(plane), 0);
2652 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002653 }
2654
Ville Syrjälä57779d02012-10-31 17:50:14 +02002655 switch (fb->pixel_format) {
2656 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002657 dspcntr |= DISPPLANE_8BPP;
2658 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002659 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002660 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002661 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002662 case DRM_FORMAT_RGB565:
2663 dspcntr |= DISPPLANE_BGRX565;
2664 break;
2665 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002666 dspcntr |= DISPPLANE_BGRX888;
2667 break;
2668 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002669 dspcntr |= DISPPLANE_RGBX888;
2670 break;
2671 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002672 dspcntr |= DISPPLANE_BGRX101010;
2673 break;
2674 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002675 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002676 break;
2677 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002678 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002679 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002680
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002681 if (INTEL_INFO(dev)->gen >= 4 &&
2682 obj->tiling_mode != I915_TILING_NONE)
2683 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002684
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002685 if (IS_G4X(dev))
2686 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2687
Ville Syrjäläac484962016-01-20 21:05:26 +02002688 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002689
Daniel Vetterc2c75132012-07-05 12:17:30 +02002690 if (INTEL_INFO(dev)->gen >= 4) {
2691 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002692 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002693 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002694 linear_offset -= intel_crtc->dspaddr_offset;
2695 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002696 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002697 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002698
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002699 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302700 dspcntr |= DISPPLANE_ROTATE_180;
2701
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002702 x += (crtc_state->pipe_src_w - 1);
2703 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302704
2705 /* Finding the last pixel of the last line of the display
2706 data and adding to linear_offset*/
2707 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002708 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002709 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302710 }
2711
Paulo Zanoni2db33662015-09-14 15:20:03 -03002712 intel_crtc->adjusted_x = x;
2713 intel_crtc->adjusted_y = y;
2714
Sonika Jindal48404c12014-08-22 14:06:04 +05302715 I915_WRITE(reg, dspcntr);
2716
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002717 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002718 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002719 I915_WRITE(DSPSURF(plane),
2720 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002721 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002722 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002723 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002724 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002725 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002726}
2727
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002728static void i9xx_disable_primary_plane(struct drm_plane *primary,
2729 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002730{
2731 struct drm_device *dev = crtc->dev;
2732 struct drm_i915_private *dev_priv = dev->dev_private;
2733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002734 int plane = intel_crtc->plane;
2735
2736 I915_WRITE(DSPCNTR(plane), 0);
2737 if (INTEL_INFO(dev_priv)->gen >= 4)
2738 I915_WRITE(DSPSURF(plane), 0);
2739 else
2740 I915_WRITE(DSPADDR(plane), 0);
2741 POSTING_READ(DSPCNTR(plane));
2742}
2743
2744static void ironlake_update_primary_plane(struct drm_plane *primary,
2745 const struct intel_crtc_state *crtc_state,
2746 const struct intel_plane_state *plane_state)
2747{
2748 struct drm_device *dev = primary->dev;
2749 struct drm_i915_private *dev_priv = dev->dev_private;
2750 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2751 struct drm_framebuffer *fb = plane_state->base.fb;
2752 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002753 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002754 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002755 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002756 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002757 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002758 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002759 int x = plane_state->src.x1 >> 16;
2760 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002761
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002762 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002763 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002764
2765 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2766 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2767
Ville Syrjälä57779d02012-10-31 17:50:14 +02002768 switch (fb->pixel_format) {
2769 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002770 dspcntr |= DISPPLANE_8BPP;
2771 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002772 case DRM_FORMAT_RGB565:
2773 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002774 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002775 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002776 dspcntr |= DISPPLANE_BGRX888;
2777 break;
2778 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002779 dspcntr |= DISPPLANE_RGBX888;
2780 break;
2781 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002782 dspcntr |= DISPPLANE_BGRX101010;
2783 break;
2784 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002785 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002786 break;
2787 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002788 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002789 }
2790
2791 if (obj->tiling_mode != I915_TILING_NONE)
2792 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002793
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002794 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002795 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002796
Ville Syrjäläac484962016-01-20 21:05:26 +02002797 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002798 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002799 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002800 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002801 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002802 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302803 dspcntr |= DISPPLANE_ROTATE_180;
2804
2805 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002806 x += (crtc_state->pipe_src_w - 1);
2807 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302808
2809 /* Finding the last pixel of the last line of the display
2810 data and adding to linear_offset*/
2811 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002812 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002813 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302814 }
2815 }
2816
Paulo Zanoni2db33662015-09-14 15:20:03 -03002817 intel_crtc->adjusted_x = x;
2818 intel_crtc->adjusted_y = y;
2819
Sonika Jindal48404c12014-08-22 14:06:04 +05302820 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002821
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002822 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002823 I915_WRITE(DSPSURF(plane),
2824 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002825 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002826 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2827 } else {
2828 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2829 I915_WRITE(DSPLINOFF(plane), linear_offset);
2830 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002831 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002832}
2833
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002834u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2835 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002836{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002837 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2838 return 64;
2839 } else {
2840 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002841
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002842 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002843 }
2844}
2845
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002846u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2847 struct drm_i915_gem_object *obj,
2848 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002849{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002850 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002851 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002852 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002853
Ville Syrjäläe7941292016-01-19 18:23:17 +02002854 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002855 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002856
Daniel Vetterce7f1722015-10-14 16:51:06 +02002857 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002858 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002859 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002860 return -1;
2861
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002862 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002863
2864 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002865 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002866 PAGE_SIZE;
2867 }
2868
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002869 WARN_ON(upper_32_bits(offset));
2870
2871 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002872}
2873
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002874static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2875{
2876 struct drm_device *dev = intel_crtc->base.dev;
2877 struct drm_i915_private *dev_priv = dev->dev_private;
2878
2879 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2880 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2881 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002882}
2883
Chandra Kondurua1b22782015-04-07 15:28:45 -07002884/*
2885 * This function detaches (aka. unbinds) unused scalers in hardware
2886 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002887static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002888{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002889 struct intel_crtc_scaler_state *scaler_state;
2890 int i;
2891
Chandra Kondurua1b22782015-04-07 15:28:45 -07002892 scaler_state = &intel_crtc->config->scaler_state;
2893
2894 /* loop through and disable scalers that aren't in use */
2895 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002896 if (!scaler_state->scalers[i].in_use)
2897 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002898 }
2899}
2900
Chandra Konduru6156a452015-04-27 13:48:39 -07002901u32 skl_plane_ctl_format(uint32_t pixel_format)
2902{
Chandra Konduru6156a452015-04-27 13:48:39 -07002903 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002904 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002905 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002906 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002907 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002908 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002909 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002910 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002911 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002912 /*
2913 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2914 * to be already pre-multiplied. We need to add a knob (or a different
2915 * DRM_FORMAT) for user-space to configure that.
2916 */
2917 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002918 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002919 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002920 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002921 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002922 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002923 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002924 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002925 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002926 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002927 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002928 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002929 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002930 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002931 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002932 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002933 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002934 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002935 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002936 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002937 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002938
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002939 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002940}
2941
2942u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2943{
Chandra Konduru6156a452015-04-27 13:48:39 -07002944 switch (fb_modifier) {
2945 case DRM_FORMAT_MOD_NONE:
2946 break;
2947 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002948 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002949 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002950 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002951 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002952 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002953 default:
2954 MISSING_CASE(fb_modifier);
2955 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002956
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002957 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958}
2959
2960u32 skl_plane_ctl_rotation(unsigned int rotation)
2961{
Chandra Konduru6156a452015-04-27 13:48:39 -07002962 switch (rotation) {
2963 case BIT(DRM_ROTATE_0):
2964 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302965 /*
2966 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2967 * while i915 HW rotation is clockwise, thats why this swapping.
2968 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002969 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302970 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002971 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002972 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002973 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302974 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002975 default:
2976 MISSING_CASE(rotation);
2977 }
2978
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002979 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002980}
2981
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002982static void skylake_update_primary_plane(struct drm_plane *plane,
2983 const struct intel_crtc_state *crtc_state,
2984 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01002985{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002986 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002987 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002988 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2989 struct drm_framebuffer *fb = plane_state->base.fb;
2990 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002991 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302992 u32 plane_ctl, stride_div, stride;
2993 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002994 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302995 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002996 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002997 int scaler_id = plane_state->scaler_id;
2998 int src_x = plane_state->src.x1 >> 16;
2999 int src_y = plane_state->src.y1 >> 16;
3000 int src_w = drm_rect_width(&plane_state->src) >> 16;
3001 int src_h = drm_rect_height(&plane_state->src) >> 16;
3002 int dst_x = plane_state->dst.x1;
3003 int dst_y = plane_state->dst.y1;
3004 int dst_w = drm_rect_width(&plane_state->dst);
3005 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003006
3007 plane_ctl = PLANE_CTL_ENABLE |
3008 PLANE_CTL_PIPE_GAMMA_ENABLE |
3009 PLANE_CTL_PIPE_CSC_ENABLE;
3010
Chandra Konduru6156a452015-04-27 13:48:39 -07003011 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3012 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003013 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003014 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003015
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003016 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003017 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003018 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303019
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003020 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003021
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303022 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003023 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3024
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303025 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003026 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303027 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003028 x_offset = stride * tile_height - src_y - src_h;
3029 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003030 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303031 } else {
3032 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003033 x_offset = src_x;
3034 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003035 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303036 }
3037 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003038
Paulo Zanoni2db33662015-09-14 15:20:03 -03003039 intel_crtc->adjusted_x = x_offset;
3040 intel_crtc->adjusted_y = y_offset;
3041
Damien Lespiau70d21f02013-07-03 21:06:04 +01003042 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303043 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3044 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3045 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003046
3047 if (scaler_id >= 0) {
3048 uint32_t ps_ctrl = 0;
3049
3050 WARN_ON(!dst_w || !dst_h);
3051 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3052 crtc_state->scaler_state.scalers[scaler_id].mode;
3053 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3054 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3055 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3056 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3057 I915_WRITE(PLANE_POS(pipe, 0), 0);
3058 } else {
3059 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3060 }
3061
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003062 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003063
3064 POSTING_READ(PLANE_SURF(pipe, 0));
3065}
3066
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003067static void skylake_disable_primary_plane(struct drm_plane *primary,
3068 struct drm_crtc *crtc)
3069{
3070 struct drm_device *dev = crtc->dev;
3071 struct drm_i915_private *dev_priv = dev->dev_private;
3072 int pipe = to_intel_crtc(crtc)->pipe;
3073
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003074 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3075 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3076 POSTING_READ(PLANE_SURF(pipe, 0));
3077}
3078
Jesse Barnes17638cd2011-06-24 12:19:23 -07003079/* Assume fb object is pinned & idle & fenced and just update base pointers */
3080static int
3081intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3082 int x, int y, enum mode_set_atomic state)
3083{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003084 /* Support for kgdboc is disabled, this needs a major rework. */
3085 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003086
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003087 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003088}
3089
Ville Syrjälä75147472014-11-24 18:28:11 +02003090static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003091{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003092 struct drm_crtc *crtc;
3093
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003094 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3096 enum plane plane = intel_crtc->plane;
3097
3098 intel_prepare_page_flip(dev, plane);
3099 intel_finish_page_flip_plane(dev, plane);
3100 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003101}
3102
3103static void intel_update_primary_planes(struct drm_device *dev)
3104{
Ville Syrjälä75147472014-11-24 18:28:11 +02003105 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003106
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003107 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003108 struct intel_plane *plane = to_intel_plane(crtc->primary);
3109 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003110
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003111 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003112 plane_state = to_intel_plane_state(plane->base.state);
3113
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003114 if (plane_state->visible)
3115 plane->update_plane(&plane->base,
3116 to_intel_crtc_state(crtc->state),
3117 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003118
3119 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003120 }
3121}
3122
Ville Syrjälä75147472014-11-24 18:28:11 +02003123void intel_prepare_reset(struct drm_device *dev)
3124{
3125 /* no reset support for gen2 */
3126 if (IS_GEN2(dev))
3127 return;
3128
3129 /* reset doesn't touch the display */
3130 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3131 return;
3132
3133 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003134 /*
3135 * Disabling the crtcs gracefully seems nicer. Also the
3136 * g33 docs say we should at least disable all the planes.
3137 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003138 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003139}
3140
3141void intel_finish_reset(struct drm_device *dev)
3142{
3143 struct drm_i915_private *dev_priv = to_i915(dev);
3144
3145 /*
3146 * Flips in the rings will be nuked by the reset,
3147 * so complete all pending flips so that user space
3148 * will get its events and not get stuck.
3149 */
3150 intel_complete_page_flips(dev);
3151
3152 /* no reset support for gen2 */
3153 if (IS_GEN2(dev))
3154 return;
3155
3156 /* reset doesn't touch the display */
3157 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3158 /*
3159 * Flips in the rings have been nuked by the reset,
3160 * so update the base address of all primary
3161 * planes to the the last fb to make sure we're
3162 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003163 *
3164 * FIXME: Atomic will make this obsolete since we won't schedule
3165 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003166 */
3167 intel_update_primary_planes(dev);
3168 return;
3169 }
3170
3171 /*
3172 * The display has been reset as well,
3173 * so need a full re-initialization.
3174 */
3175 intel_runtime_pm_disable_interrupts(dev_priv);
3176 intel_runtime_pm_enable_interrupts(dev_priv);
3177
3178 intel_modeset_init_hw(dev);
3179
3180 spin_lock_irq(&dev_priv->irq_lock);
3181 if (dev_priv->display.hpd_irq_setup)
3182 dev_priv->display.hpd_irq_setup(dev);
3183 spin_unlock_irq(&dev_priv->irq_lock);
3184
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003185 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003186
3187 intel_hpd_init(dev_priv);
3188
3189 drm_modeset_unlock_all(dev);
3190}
3191
Chris Wilson7d5e3792014-03-04 13:15:08 +00003192static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3193{
3194 struct drm_device *dev = crtc->dev;
3195 struct drm_i915_private *dev_priv = dev->dev_private;
3196 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003197 bool pending;
3198
3199 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3200 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3201 return false;
3202
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003203 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003204 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003205 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003206
3207 return pending;
3208}
3209
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003210static void intel_update_pipe_config(struct intel_crtc *crtc,
3211 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003212{
3213 struct drm_device *dev = crtc->base.dev;
3214 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003215 struct intel_crtc_state *pipe_config =
3216 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003217
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003218 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3219 crtc->base.mode = crtc->base.state->mode;
3220
3221 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3222 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3223 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003224
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003225 if (HAS_DDI(dev))
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00003226 intel_color_set_csc(&crtc->base);
Maarten Lankhorst44522d82015-08-27 15:44:02 +02003227
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003228 /*
3229 * Update pipe size and adjust fitter if needed: the reason for this is
3230 * that in compute_mode_changes we check the native mode (not the pfit
3231 * mode) to see if we can flip rather than do a full mode set. In the
3232 * fastboot case, we'll flip, but if we don't update the pipesrc and
3233 * pfit state, we'll end up with a big fb scanned out into the wrong
3234 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003235 */
3236
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003237 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003238 ((pipe_config->pipe_src_w - 1) << 16) |
3239 (pipe_config->pipe_src_h - 1));
3240
3241 /* on skylake this is done by detaching scalers */
3242 if (INTEL_INFO(dev)->gen >= 9) {
3243 skl_detach_scalers(crtc);
3244
3245 if (pipe_config->pch_pfit.enabled)
3246 skylake_pfit_enable(crtc);
3247 } else if (HAS_PCH_SPLIT(dev)) {
3248 if (pipe_config->pch_pfit.enabled)
3249 ironlake_pfit_enable(crtc);
3250 else if (old_crtc_state->pch_pfit.enabled)
3251 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003252 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003253}
3254
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003255static void intel_fdi_normal_train(struct drm_crtc *crtc)
3256{
3257 struct drm_device *dev = crtc->dev;
3258 struct drm_i915_private *dev_priv = dev->dev_private;
3259 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3260 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003261 i915_reg_t reg;
3262 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003263
3264 /* enable normal train */
3265 reg = FDI_TX_CTL(pipe);
3266 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003267 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003268 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3269 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003270 } else {
3271 temp &= ~FDI_LINK_TRAIN_NONE;
3272 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003273 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003274 I915_WRITE(reg, temp);
3275
3276 reg = FDI_RX_CTL(pipe);
3277 temp = I915_READ(reg);
3278 if (HAS_PCH_CPT(dev)) {
3279 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3280 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3281 } else {
3282 temp &= ~FDI_LINK_TRAIN_NONE;
3283 temp |= FDI_LINK_TRAIN_NONE;
3284 }
3285 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3286
3287 /* wait one idle pattern time */
3288 POSTING_READ(reg);
3289 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003290
3291 /* IVB wants error correction enabled */
3292 if (IS_IVYBRIDGE(dev))
3293 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3294 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003295}
3296
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003297/* The FDI link training functions for ILK/Ibexpeak. */
3298static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3299{
3300 struct drm_device *dev = crtc->dev;
3301 struct drm_i915_private *dev_priv = dev->dev_private;
3302 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3303 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003304 i915_reg_t reg;
3305 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003306
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003307 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003308 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003309
Adam Jacksone1a44742010-06-25 15:32:14 -04003310 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3311 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003312 reg = FDI_RX_IMR(pipe);
3313 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003314 temp &= ~FDI_RX_SYMBOL_LOCK;
3315 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003316 I915_WRITE(reg, temp);
3317 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003318 udelay(150);
3319
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003320 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003321 reg = FDI_TX_CTL(pipe);
3322 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003323 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003324 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003325 temp &= ~FDI_LINK_TRAIN_NONE;
3326 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003327 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003328
Chris Wilson5eddb702010-09-11 13:48:45 +01003329 reg = FDI_RX_CTL(pipe);
3330 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003331 temp &= ~FDI_LINK_TRAIN_NONE;
3332 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003333 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3334
3335 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003336 udelay(150);
3337
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003338 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003339 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3340 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3341 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003342
Chris Wilson5eddb702010-09-11 13:48:45 +01003343 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003344 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003345 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003346 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3347
3348 if ((temp & FDI_RX_BIT_LOCK)) {
3349 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003350 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003351 break;
3352 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003353 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003354 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003355 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003356
3357 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003358 reg = FDI_TX_CTL(pipe);
3359 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003360 temp &= ~FDI_LINK_TRAIN_NONE;
3361 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003362 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003363
Chris Wilson5eddb702010-09-11 13:48:45 +01003364 reg = FDI_RX_CTL(pipe);
3365 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003366 temp &= ~FDI_LINK_TRAIN_NONE;
3367 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003368 I915_WRITE(reg, temp);
3369
3370 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003371 udelay(150);
3372
Chris Wilson5eddb702010-09-11 13:48:45 +01003373 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003374 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003375 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003376 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3377
3378 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003379 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003380 DRM_DEBUG_KMS("FDI train 2 done.\n");
3381 break;
3382 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003383 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003384 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003385 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003386
3387 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003388
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003389}
3390
Akshay Joshi0206e352011-08-16 15:34:10 -04003391static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003392 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3393 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3394 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3395 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3396};
3397
3398/* The FDI link training functions for SNB/Cougarpoint. */
3399static void gen6_fdi_link_train(struct drm_crtc *crtc)
3400{
3401 struct drm_device *dev = crtc->dev;
3402 struct drm_i915_private *dev_priv = dev->dev_private;
3403 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3404 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003405 i915_reg_t reg;
3406 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003407
Adam Jacksone1a44742010-06-25 15:32:14 -04003408 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3409 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003410 reg = FDI_RX_IMR(pipe);
3411 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003412 temp &= ~FDI_RX_SYMBOL_LOCK;
3413 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003414 I915_WRITE(reg, temp);
3415
3416 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003417 udelay(150);
3418
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003419 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003420 reg = FDI_TX_CTL(pipe);
3421 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003422 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003423 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003424 temp &= ~FDI_LINK_TRAIN_NONE;
3425 temp |= FDI_LINK_TRAIN_PATTERN_1;
3426 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3427 /* SNB-B */
3428 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003429 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003430
Daniel Vetterd74cf322012-10-26 10:58:13 +02003431 I915_WRITE(FDI_RX_MISC(pipe),
3432 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3433
Chris Wilson5eddb702010-09-11 13:48:45 +01003434 reg = FDI_RX_CTL(pipe);
3435 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003436 if (HAS_PCH_CPT(dev)) {
3437 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3438 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3439 } else {
3440 temp &= ~FDI_LINK_TRAIN_NONE;
3441 temp |= FDI_LINK_TRAIN_PATTERN_1;
3442 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003443 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3444
3445 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003446 udelay(150);
3447
Akshay Joshi0206e352011-08-16 15:34:10 -04003448 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003449 reg = FDI_TX_CTL(pipe);
3450 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003451 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3452 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003453 I915_WRITE(reg, temp);
3454
3455 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003456 udelay(500);
3457
Sean Paulfa37d392012-03-02 12:53:39 -05003458 for (retry = 0; retry < 5; retry++) {
3459 reg = FDI_RX_IIR(pipe);
3460 temp = I915_READ(reg);
3461 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3462 if (temp & FDI_RX_BIT_LOCK) {
3463 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3464 DRM_DEBUG_KMS("FDI train 1 done.\n");
3465 break;
3466 }
3467 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003468 }
Sean Paulfa37d392012-03-02 12:53:39 -05003469 if (retry < 5)
3470 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003471 }
3472 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003473 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003474
3475 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003476 reg = FDI_TX_CTL(pipe);
3477 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003478 temp &= ~FDI_LINK_TRAIN_NONE;
3479 temp |= FDI_LINK_TRAIN_PATTERN_2;
3480 if (IS_GEN6(dev)) {
3481 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3482 /* SNB-B */
3483 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3484 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003485 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003486
Chris Wilson5eddb702010-09-11 13:48:45 +01003487 reg = FDI_RX_CTL(pipe);
3488 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003489 if (HAS_PCH_CPT(dev)) {
3490 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3491 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3492 } else {
3493 temp &= ~FDI_LINK_TRAIN_NONE;
3494 temp |= FDI_LINK_TRAIN_PATTERN_2;
3495 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003496 I915_WRITE(reg, temp);
3497
3498 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003499 udelay(150);
3500
Akshay Joshi0206e352011-08-16 15:34:10 -04003501 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003502 reg = FDI_TX_CTL(pipe);
3503 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003504 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3505 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003506 I915_WRITE(reg, temp);
3507
3508 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003509 udelay(500);
3510
Sean Paulfa37d392012-03-02 12:53:39 -05003511 for (retry = 0; retry < 5; retry++) {
3512 reg = FDI_RX_IIR(pipe);
3513 temp = I915_READ(reg);
3514 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3515 if (temp & FDI_RX_SYMBOL_LOCK) {
3516 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3517 DRM_DEBUG_KMS("FDI train 2 done.\n");
3518 break;
3519 }
3520 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003521 }
Sean Paulfa37d392012-03-02 12:53:39 -05003522 if (retry < 5)
3523 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003524 }
3525 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003526 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003527
3528 DRM_DEBUG_KMS("FDI train done.\n");
3529}
3530
Jesse Barnes357555c2011-04-28 15:09:55 -07003531/* Manual link training for Ivy Bridge A0 parts */
3532static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3533{
3534 struct drm_device *dev = crtc->dev;
3535 struct drm_i915_private *dev_priv = dev->dev_private;
3536 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3537 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003538 i915_reg_t reg;
3539 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003540
3541 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3542 for train result */
3543 reg = FDI_RX_IMR(pipe);
3544 temp = I915_READ(reg);
3545 temp &= ~FDI_RX_SYMBOL_LOCK;
3546 temp &= ~FDI_RX_BIT_LOCK;
3547 I915_WRITE(reg, temp);
3548
3549 POSTING_READ(reg);
3550 udelay(150);
3551
Daniel Vetter01a415f2012-10-27 15:58:40 +02003552 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3553 I915_READ(FDI_RX_IIR(pipe)));
3554
Jesse Barnes139ccd32013-08-19 11:04:55 -07003555 /* Try each vswing and preemphasis setting twice before moving on */
3556 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3557 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003558 reg = FDI_TX_CTL(pipe);
3559 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003560 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3561 temp &= ~FDI_TX_ENABLE;
3562 I915_WRITE(reg, temp);
3563
3564 reg = FDI_RX_CTL(pipe);
3565 temp = I915_READ(reg);
3566 temp &= ~FDI_LINK_TRAIN_AUTO;
3567 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3568 temp &= ~FDI_RX_ENABLE;
3569 I915_WRITE(reg, temp);
3570
3571 /* enable CPU FDI TX and PCH FDI RX */
3572 reg = FDI_TX_CTL(pipe);
3573 temp = I915_READ(reg);
3574 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003575 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003576 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003577 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003578 temp |= snb_b_fdi_train_param[j/2];
3579 temp |= FDI_COMPOSITE_SYNC;
3580 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3581
3582 I915_WRITE(FDI_RX_MISC(pipe),
3583 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3584
3585 reg = FDI_RX_CTL(pipe);
3586 temp = I915_READ(reg);
3587 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3588 temp |= FDI_COMPOSITE_SYNC;
3589 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3590
3591 POSTING_READ(reg);
3592 udelay(1); /* should be 0.5us */
3593
3594 for (i = 0; i < 4; i++) {
3595 reg = FDI_RX_IIR(pipe);
3596 temp = I915_READ(reg);
3597 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3598
3599 if (temp & FDI_RX_BIT_LOCK ||
3600 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3601 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3602 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3603 i);
3604 break;
3605 }
3606 udelay(1); /* should be 0.5us */
3607 }
3608 if (i == 4) {
3609 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3610 continue;
3611 }
3612
3613 /* Train 2 */
3614 reg = FDI_TX_CTL(pipe);
3615 temp = I915_READ(reg);
3616 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3617 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3618 I915_WRITE(reg, temp);
3619
3620 reg = FDI_RX_CTL(pipe);
3621 temp = I915_READ(reg);
3622 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3623 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003624 I915_WRITE(reg, temp);
3625
3626 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003627 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003628
Jesse Barnes139ccd32013-08-19 11:04:55 -07003629 for (i = 0; i < 4; i++) {
3630 reg = FDI_RX_IIR(pipe);
3631 temp = I915_READ(reg);
3632 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003633
Jesse Barnes139ccd32013-08-19 11:04:55 -07003634 if (temp & FDI_RX_SYMBOL_LOCK ||
3635 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3636 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3637 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3638 i);
3639 goto train_done;
3640 }
3641 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003642 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003643 if (i == 4)
3644 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003645 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003646
Jesse Barnes139ccd32013-08-19 11:04:55 -07003647train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003648 DRM_DEBUG_KMS("FDI train done.\n");
3649}
3650
Daniel Vetter88cefb62012-08-12 19:27:14 +02003651static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003652{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003653 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003654 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003655 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003656 i915_reg_t reg;
3657 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003658
Jesse Barnes0e23b992010-09-10 11:10:00 -07003659 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003660 reg = FDI_RX_CTL(pipe);
3661 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003662 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003663 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003664 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003665 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3666
3667 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003668 udelay(200);
3669
3670 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003671 temp = I915_READ(reg);
3672 I915_WRITE(reg, temp | FDI_PCDCLK);
3673
3674 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003675 udelay(200);
3676
Paulo Zanoni20749732012-11-23 15:30:38 -02003677 /* Enable CPU FDI TX PLL, always on for Ironlake */
3678 reg = FDI_TX_CTL(pipe);
3679 temp = I915_READ(reg);
3680 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3681 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003682
Paulo Zanoni20749732012-11-23 15:30:38 -02003683 POSTING_READ(reg);
3684 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003685 }
3686}
3687
Daniel Vetter88cefb62012-08-12 19:27:14 +02003688static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3689{
3690 struct drm_device *dev = intel_crtc->base.dev;
3691 struct drm_i915_private *dev_priv = dev->dev_private;
3692 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003693 i915_reg_t reg;
3694 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003695
3696 /* Switch from PCDclk to Rawclk */
3697 reg = FDI_RX_CTL(pipe);
3698 temp = I915_READ(reg);
3699 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3700
3701 /* Disable CPU FDI TX PLL */
3702 reg = FDI_TX_CTL(pipe);
3703 temp = I915_READ(reg);
3704 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3705
3706 POSTING_READ(reg);
3707 udelay(100);
3708
3709 reg = FDI_RX_CTL(pipe);
3710 temp = I915_READ(reg);
3711 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3712
3713 /* Wait for the clocks to turn off. */
3714 POSTING_READ(reg);
3715 udelay(100);
3716}
3717
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003718static void ironlake_fdi_disable(struct drm_crtc *crtc)
3719{
3720 struct drm_device *dev = crtc->dev;
3721 struct drm_i915_private *dev_priv = dev->dev_private;
3722 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3723 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003724 i915_reg_t reg;
3725 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003726
3727 /* disable CPU FDI tx and PCH FDI rx */
3728 reg = FDI_TX_CTL(pipe);
3729 temp = I915_READ(reg);
3730 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3731 POSTING_READ(reg);
3732
3733 reg = FDI_RX_CTL(pipe);
3734 temp = I915_READ(reg);
3735 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003736 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003737 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3738
3739 POSTING_READ(reg);
3740 udelay(100);
3741
3742 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003743 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003744 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003745
3746 /* still set train pattern 1 */
3747 reg = FDI_TX_CTL(pipe);
3748 temp = I915_READ(reg);
3749 temp &= ~FDI_LINK_TRAIN_NONE;
3750 temp |= FDI_LINK_TRAIN_PATTERN_1;
3751 I915_WRITE(reg, temp);
3752
3753 reg = FDI_RX_CTL(pipe);
3754 temp = I915_READ(reg);
3755 if (HAS_PCH_CPT(dev)) {
3756 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3757 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3758 } else {
3759 temp &= ~FDI_LINK_TRAIN_NONE;
3760 temp |= FDI_LINK_TRAIN_PATTERN_1;
3761 }
3762 /* BPC in FDI rx is consistent with that in PIPECONF */
3763 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003764 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003765 I915_WRITE(reg, temp);
3766
3767 POSTING_READ(reg);
3768 udelay(100);
3769}
3770
Chris Wilson5dce5b932014-01-20 10:17:36 +00003771bool intel_has_pending_fb_unpin(struct drm_device *dev)
3772{
3773 struct intel_crtc *crtc;
3774
3775 /* Note that we don't need to be called with mode_config.lock here
3776 * as our list of CRTC objects is static for the lifetime of the
3777 * device and so cannot disappear as we iterate. Similarly, we can
3778 * happily treat the predicates as racy, atomic checks as userspace
3779 * cannot claim and pin a new fb without at least acquring the
3780 * struct_mutex and so serialising with us.
3781 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003782 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003783 if (atomic_read(&crtc->unpin_work_count) == 0)
3784 continue;
3785
3786 if (crtc->unpin_work)
3787 intel_wait_for_vblank(dev, crtc->pipe);
3788
3789 return true;
3790 }
3791
3792 return false;
3793}
3794
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003795static void page_flip_completed(struct intel_crtc *intel_crtc)
3796{
3797 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3798 struct intel_unpin_work *work = intel_crtc->unpin_work;
3799
3800 /* ensure that the unpin work is consistent wrt ->pending. */
3801 smp_rmb();
3802 intel_crtc->unpin_work = NULL;
3803
3804 if (work->event)
3805 drm_send_vblank_event(intel_crtc->base.dev,
3806 intel_crtc->pipe,
3807 work->event);
3808
3809 drm_crtc_vblank_put(&intel_crtc->base);
3810
3811 wake_up_all(&dev_priv->pending_flip_queue);
3812 queue_work(dev_priv->wq, &work->work);
3813
3814 trace_i915_flip_complete(intel_crtc->plane,
3815 work->pending_flip_obj);
3816}
3817
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003818static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003819{
Chris Wilson0f911282012-04-17 10:05:38 +01003820 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003821 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003822 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003823
Daniel Vetter2c10d572012-12-20 21:24:07 +01003824 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003825
3826 ret = wait_event_interruptible_timeout(
3827 dev_priv->pending_flip_queue,
3828 !intel_crtc_has_pending_flip(crtc),
3829 60*HZ);
3830
3831 if (ret < 0)
3832 return ret;
3833
3834 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01003835 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003836
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003837 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003838 if (intel_crtc->unpin_work) {
3839 WARN_ONCE(1, "Removing stuck page flip\n");
3840 page_flip_completed(intel_crtc);
3841 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003842 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003843 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003844
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003845 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003846}
3847
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003848static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3849{
3850 u32 temp;
3851
3852 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3853
3854 mutex_lock(&dev_priv->sb_lock);
3855
3856 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3857 temp |= SBI_SSCCTL_DISABLE;
3858 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3859
3860 mutex_unlock(&dev_priv->sb_lock);
3861}
3862
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003863/* Program iCLKIP clock to the desired frequency */
3864static void lpt_program_iclkip(struct drm_crtc *crtc)
3865{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003866 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003867 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003868 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3869 u32 temp;
3870
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003871 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003872
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003873 /* The iCLK virtual clock root frequency is in MHz,
3874 * but the adjusted_mode->crtc_clock in in KHz. To get the
3875 * divisors, it is necessary to divide one by another, so we
3876 * convert the virtual clock precision to KHz here for higher
3877 * precision.
3878 */
3879 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003880 u32 iclk_virtual_root_freq = 172800 * 1000;
3881 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003882 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003883
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003884 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3885 clock << auxdiv);
3886 divsel = (desired_divisor / iclk_pi_range) - 2;
3887 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003888
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003889 /*
3890 * Near 20MHz is a corner case which is
3891 * out of range for the 7-bit divisor
3892 */
3893 if (divsel <= 0x7f)
3894 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003895 }
3896
3897 /* This should not happen with any sane values */
3898 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3899 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3900 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3901 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3902
3903 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 +03003904 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003905 auxdiv,
3906 divsel,
3907 phasedir,
3908 phaseinc);
3909
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003910 mutex_lock(&dev_priv->sb_lock);
3911
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003912 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003913 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003914 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3915 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3916 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3917 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3918 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3919 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003920 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003921
3922 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003923 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003924 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3925 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003926 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003927
3928 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003929 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003930 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003931 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003932
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003933 mutex_unlock(&dev_priv->sb_lock);
3934
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003935 /* Wait for initialization time */
3936 udelay(24);
3937
3938 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3939}
3940
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003941int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3942{
3943 u32 divsel, phaseinc, auxdiv;
3944 u32 iclk_virtual_root_freq = 172800 * 1000;
3945 u32 iclk_pi_range = 64;
3946 u32 desired_divisor;
3947 u32 temp;
3948
3949 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3950 return 0;
3951
3952 mutex_lock(&dev_priv->sb_lock);
3953
3954 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3955 if (temp & SBI_SSCCTL_DISABLE) {
3956 mutex_unlock(&dev_priv->sb_lock);
3957 return 0;
3958 }
3959
3960 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3961 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3962 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3963 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3964 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3965
3966 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3967 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3968 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3969
3970 mutex_unlock(&dev_priv->sb_lock);
3971
3972 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3973
3974 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3975 desired_divisor << auxdiv);
3976}
3977
Daniel Vetter275f01b22013-05-03 11:49:47 +02003978static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3979 enum pipe pch_transcoder)
3980{
3981 struct drm_device *dev = crtc->base.dev;
3982 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003983 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003984
3985 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3986 I915_READ(HTOTAL(cpu_transcoder)));
3987 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3988 I915_READ(HBLANK(cpu_transcoder)));
3989 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3990 I915_READ(HSYNC(cpu_transcoder)));
3991
3992 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3993 I915_READ(VTOTAL(cpu_transcoder)));
3994 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3995 I915_READ(VBLANK(cpu_transcoder)));
3996 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3997 I915_READ(VSYNC(cpu_transcoder)));
3998 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3999 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4000}
4001
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004002static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004003{
4004 struct drm_i915_private *dev_priv = dev->dev_private;
4005 uint32_t temp;
4006
4007 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004008 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004009 return;
4010
4011 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4012 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4013
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004014 temp &= ~FDI_BC_BIFURCATION_SELECT;
4015 if (enable)
4016 temp |= FDI_BC_BIFURCATION_SELECT;
4017
4018 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004019 I915_WRITE(SOUTH_CHICKEN1, temp);
4020 POSTING_READ(SOUTH_CHICKEN1);
4021}
4022
4023static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4024{
4025 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004026
4027 switch (intel_crtc->pipe) {
4028 case PIPE_A:
4029 break;
4030 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004031 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004032 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004033 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004034 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004035
4036 break;
4037 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004038 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004039
4040 break;
4041 default:
4042 BUG();
4043 }
4044}
4045
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004046/* Return which DP Port should be selected for Transcoder DP control */
4047static enum port
4048intel_trans_dp_port_sel(struct drm_crtc *crtc)
4049{
4050 struct drm_device *dev = crtc->dev;
4051 struct intel_encoder *encoder;
4052
4053 for_each_encoder_on_crtc(dev, crtc, encoder) {
4054 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4055 encoder->type == INTEL_OUTPUT_EDP)
4056 return enc_to_dig_port(&encoder->base)->port;
4057 }
4058
4059 return -1;
4060}
4061
Jesse Barnesf67a5592011-01-05 10:31:48 -08004062/*
4063 * Enable PCH resources required for PCH ports:
4064 * - PCH PLLs
4065 * - FDI training & RX/TX
4066 * - update transcoder timings
4067 * - DP transcoding bits
4068 * - transcoder
4069 */
4070static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004071{
4072 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004073 struct drm_i915_private *dev_priv = dev->dev_private;
4074 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4075 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004076 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004077
Daniel Vetterab9412b2013-05-03 11:49:46 +02004078 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004079
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004080 if (IS_IVYBRIDGE(dev))
4081 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4082
Daniel Vettercd986ab2012-10-26 10:58:12 +02004083 /* Write the TU size bits before fdi link training, so that error
4084 * detection works. */
4085 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4086 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4087
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004088 /*
4089 * Sometimes spurious CPU pipe underruns happen during FDI
4090 * training, at least with VGA+HDMI cloning. Suppress them.
4091 */
4092 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4093
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004094 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004095 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004096
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004097 /* We need to program the right clock selection before writing the pixel
4098 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004099 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004100 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004101
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004102 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004103 temp |= TRANS_DPLL_ENABLE(pipe);
4104 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004105 if (intel_crtc->config->shared_dpll ==
4106 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004107 temp |= sel;
4108 else
4109 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004110 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004111 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004112
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004113 /* XXX: pch pll's can be enabled any time before we enable the PCH
4114 * transcoder, and we actually should do this to not upset any PCH
4115 * transcoder that already use the clock when we share it.
4116 *
4117 * Note that enable_shared_dpll tries to do the right thing, but
4118 * get_shared_dpll unconditionally resets the pll - we need that to have
4119 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004120 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004121
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004122 /* set transcoder timing, panel must allow it */
4123 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004124 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004125
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004126 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004127
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004128 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4129
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004130 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004131 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004132 const struct drm_display_mode *adjusted_mode =
4133 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004134 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004135 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004136 temp = I915_READ(reg);
4137 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004138 TRANS_DP_SYNC_MASK |
4139 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004140 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004141 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004142
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004143 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004144 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004145 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004146 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004147
4148 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004149 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004150 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004151 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004152 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004153 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004154 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004155 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004156 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004157 break;
4158 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004159 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004160 }
4161
Chris Wilson5eddb702010-09-11 13:48:45 +01004162 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004163 }
4164
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004165 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004166}
4167
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004168static void lpt_pch_enable(struct drm_crtc *crtc)
4169{
4170 struct drm_device *dev = crtc->dev;
4171 struct drm_i915_private *dev_priv = dev->dev_private;
4172 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004173 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004174
Daniel Vetterab9412b2013-05-03 11:49:46 +02004175 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004176
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004177 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004178
Paulo Zanoni0540e482012-10-31 18:12:40 -02004179 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004180 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004181
Paulo Zanoni937bb612012-10-31 18:12:47 -02004182 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004183}
4184
Daniel Vettera1520312013-05-03 11:49:50 +02004185static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004186{
4187 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004188 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004189 u32 temp;
4190
4191 temp = I915_READ(dslreg);
4192 udelay(500);
4193 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004194 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004195 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004196 }
4197}
4198
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004199static int
4200skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4201 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4202 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004203{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004204 struct intel_crtc_scaler_state *scaler_state =
4205 &crtc_state->scaler_state;
4206 struct intel_crtc *intel_crtc =
4207 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004208 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004209
4210 need_scaling = intel_rotation_90_or_270(rotation) ?
4211 (src_h != dst_w || src_w != dst_h):
4212 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004213
4214 /*
4215 * if plane is being disabled or scaler is no more required or force detach
4216 * - free scaler binded to this plane/crtc
4217 * - in order to do this, update crtc->scaler_usage
4218 *
4219 * Here scaler state in crtc_state is set free so that
4220 * scaler can be assigned to other user. Actual register
4221 * update to free the scaler is done in plane/panel-fit programming.
4222 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4223 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004224 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004225 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004226 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004227 scaler_state->scalers[*scaler_id].in_use = 0;
4228
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004229 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4230 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4231 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004232 scaler_state->scaler_users);
4233 *scaler_id = -1;
4234 }
4235 return 0;
4236 }
4237
4238 /* range checks */
4239 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4240 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4241
4242 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4243 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004244 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004245 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004246 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004247 return -EINVAL;
4248 }
4249
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004250 /* mark this plane as a scaler user in crtc_state */
4251 scaler_state->scaler_users |= (1 << scaler_user);
4252 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4253 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4254 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4255 scaler_state->scaler_users);
4256
4257 return 0;
4258}
4259
4260/**
4261 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4262 *
4263 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004264 *
4265 * Return
4266 * 0 - scaler_usage updated successfully
4267 * error - requested scaling cannot be supported or other error condition
4268 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004269int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004270{
4271 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004272 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004273
4274 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4275 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4276
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004277 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004278 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004279 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004280 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004281}
4282
4283/**
4284 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4285 *
4286 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004287 * @plane_state: atomic plane state to update
4288 *
4289 * Return
4290 * 0 - scaler_usage updated successfully
4291 * error - requested scaling cannot be supported or other error condition
4292 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004293static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4294 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004295{
4296
4297 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004298 struct intel_plane *intel_plane =
4299 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004300 struct drm_framebuffer *fb = plane_state->base.fb;
4301 int ret;
4302
4303 bool force_detach = !fb || !plane_state->visible;
4304
4305 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4306 intel_plane->base.base.id, intel_crtc->pipe,
4307 drm_plane_index(&intel_plane->base));
4308
4309 ret = skl_update_scaler(crtc_state, force_detach,
4310 drm_plane_index(&intel_plane->base),
4311 &plane_state->scaler_id,
4312 plane_state->base.rotation,
4313 drm_rect_width(&plane_state->src) >> 16,
4314 drm_rect_height(&plane_state->src) >> 16,
4315 drm_rect_width(&plane_state->dst),
4316 drm_rect_height(&plane_state->dst));
4317
4318 if (ret || plane_state->scaler_id < 0)
4319 return ret;
4320
Chandra Kondurua1b22782015-04-07 15:28:45 -07004321 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004322 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004323 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004324 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004325 return -EINVAL;
4326 }
4327
4328 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004329 switch (fb->pixel_format) {
4330 case DRM_FORMAT_RGB565:
4331 case DRM_FORMAT_XBGR8888:
4332 case DRM_FORMAT_XRGB8888:
4333 case DRM_FORMAT_ABGR8888:
4334 case DRM_FORMAT_ARGB8888:
4335 case DRM_FORMAT_XRGB2101010:
4336 case DRM_FORMAT_XBGR2101010:
4337 case DRM_FORMAT_YUYV:
4338 case DRM_FORMAT_YVYU:
4339 case DRM_FORMAT_UYVY:
4340 case DRM_FORMAT_VYUY:
4341 break;
4342 default:
4343 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4344 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4345 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004346 }
4347
Chandra Kondurua1b22782015-04-07 15:28:45 -07004348 return 0;
4349}
4350
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004351static void skylake_scaler_disable(struct intel_crtc *crtc)
4352{
4353 int i;
4354
4355 for (i = 0; i < crtc->num_scalers; i++)
4356 skl_detach_scaler(crtc, i);
4357}
4358
4359static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004360{
4361 struct drm_device *dev = crtc->base.dev;
4362 struct drm_i915_private *dev_priv = dev->dev_private;
4363 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004364 struct intel_crtc_scaler_state *scaler_state =
4365 &crtc->config->scaler_state;
4366
4367 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4368
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004369 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004370 int id;
4371
4372 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4373 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4374 return;
4375 }
4376
4377 id = scaler_state->scaler_id;
4378 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4379 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4380 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4381 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4382
4383 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004384 }
4385}
4386
Jesse Barnesb074cec2013-04-25 12:55:02 -07004387static void ironlake_pfit_enable(struct intel_crtc *crtc)
4388{
4389 struct drm_device *dev = crtc->base.dev;
4390 struct drm_i915_private *dev_priv = dev->dev_private;
4391 int pipe = crtc->pipe;
4392
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004393 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004394 /* Force use of hard-coded filter coefficients
4395 * as some pre-programmed values are broken,
4396 * e.g. x201.
4397 */
4398 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4399 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4400 PF_PIPE_SEL_IVB(pipe));
4401 else
4402 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004403 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4404 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004405 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004406}
4407
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004408void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004409{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004410 struct drm_device *dev = crtc->base.dev;
4411 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004412
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004413 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004414 return;
4415
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004416 /*
4417 * We can only enable IPS after we enable a plane and wait for a vblank
4418 * This function is called from post_plane_update, which is run after
4419 * a vblank wait.
4420 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004421
Paulo Zanonid77e4532013-09-24 13:52:55 -03004422 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004423 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004424 mutex_lock(&dev_priv->rps.hw_lock);
4425 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4426 mutex_unlock(&dev_priv->rps.hw_lock);
4427 /* Quoting Art Runyan: "its not safe to expect any particular
4428 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004429 * mailbox." Moreover, the mailbox may return a bogus state,
4430 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004431 */
4432 } else {
4433 I915_WRITE(IPS_CTL, IPS_ENABLE);
4434 /* The bit only becomes 1 in the next vblank, so this wait here
4435 * is essentially intel_wait_for_vblank. If we don't have this
4436 * and don't wait for vblanks until the end of crtc_enable, then
4437 * the HW state readout code will complain that the expected
4438 * IPS_CTL value is not the one we read. */
4439 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4440 DRM_ERROR("Timed out waiting for IPS enable\n");
4441 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004442}
4443
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004444void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004445{
4446 struct drm_device *dev = crtc->base.dev;
4447 struct drm_i915_private *dev_priv = dev->dev_private;
4448
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004449 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004450 return;
4451
4452 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004453 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004454 mutex_lock(&dev_priv->rps.hw_lock);
4455 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4456 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004457 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4458 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4459 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004460 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004461 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004462 POSTING_READ(IPS_CTL);
4463 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004464
4465 /* We need to wait for a vblank before we can disable the plane. */
4466 intel_wait_for_vblank(dev, crtc->pipe);
4467}
4468
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004469static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004470{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004471 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004472 struct drm_device *dev = intel_crtc->base.dev;
4473 struct drm_i915_private *dev_priv = dev->dev_private;
4474
4475 mutex_lock(&dev->struct_mutex);
4476 dev_priv->mm.interruptible = false;
4477 (void) intel_overlay_switch_off(intel_crtc->overlay);
4478 dev_priv->mm.interruptible = true;
4479 mutex_unlock(&dev->struct_mutex);
4480 }
4481
4482 /* Let userspace switch the overlay on again. In most cases userspace
4483 * has to recompute where to put it anyway.
4484 */
4485}
4486
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004487/**
4488 * intel_post_enable_primary - Perform operations after enabling primary plane
4489 * @crtc: the CRTC whose primary plane was just enabled
4490 *
4491 * Performs potentially sleeping operations that must be done after the primary
4492 * plane is enabled, such as updating FBC and IPS. Note that this may be
4493 * called due to an explicit primary plane update, or due to an implicit
4494 * re-enable that is caused when a sprite plane is updated to no longer
4495 * completely hide the primary plane.
4496 */
4497static void
4498intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004499{
4500 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004501 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004502 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4503 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004504
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004505 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004506 * FIXME IPS should be fine as long as one plane is
4507 * enabled, but in practice it seems to have problems
4508 * when going from primary only to sprite only and vice
4509 * versa.
4510 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004511 hsw_enable_ips(intel_crtc);
4512
Daniel Vetterf99d7062014-06-19 16:01:59 +02004513 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004514 * Gen2 reports pipe underruns whenever all planes are disabled.
4515 * So don't enable underrun reporting before at least some planes
4516 * are enabled.
4517 * FIXME: Need to fix the logic to work when we turn off all planes
4518 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004519 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004520 if (IS_GEN2(dev))
4521 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4522
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004523 /* Underruns don't always raise interrupts, so check manually. */
4524 intel_check_cpu_fifo_underruns(dev_priv);
4525 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004526}
4527
Ville Syrjälä2622a082016-03-09 19:07:26 +02004528/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004529static void
4530intel_pre_disable_primary(struct drm_crtc *crtc)
4531{
4532 struct drm_device *dev = crtc->dev;
4533 struct drm_i915_private *dev_priv = dev->dev_private;
4534 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4535 int pipe = intel_crtc->pipe;
4536
4537 /*
4538 * Gen2 reports pipe underruns whenever all planes are disabled.
4539 * So diasble underrun reporting before all the planes get disabled.
4540 * FIXME: Need to fix the logic to work when we turn off all planes
4541 * but leave the pipe running.
4542 */
4543 if (IS_GEN2(dev))
4544 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4545
4546 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004547 * FIXME IPS should be fine as long as one plane is
4548 * enabled, but in practice it seems to have problems
4549 * when going from primary only to sprite only and vice
4550 * versa.
4551 */
4552 hsw_disable_ips(intel_crtc);
4553}
4554
4555/* FIXME get rid of this and use pre_plane_update */
4556static void
4557intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4558{
4559 struct drm_device *dev = crtc->dev;
4560 struct drm_i915_private *dev_priv = dev->dev_private;
4561 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4562 int pipe = intel_crtc->pipe;
4563
4564 intel_pre_disable_primary(crtc);
4565
4566 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004567 * Vblank time updates from the shadow to live plane control register
4568 * are blocked if the memory self-refresh mode is active at that
4569 * moment. So to make sure the plane gets truly disabled, disable
4570 * first the self-refresh mode. The self-refresh enable bit in turn
4571 * will be checked/applied by the HW only at the next frame start
4572 * event which is after the vblank start event, so we need to have a
4573 * wait-for-vblank between disabling the plane and the pipe.
4574 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004575 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004576 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004577 dev_priv->wm.vlv.cxsr = false;
4578 intel_wait_for_vblank(dev, pipe);
4579 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004580}
4581
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004582static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004583{
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004584 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4585 struct drm_atomic_state *old_state = old_crtc_state->base.state;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004586 struct intel_crtc_state *pipe_config =
4587 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004588 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004589 struct drm_plane *primary = crtc->base.primary;
4590 struct drm_plane_state *old_pri_state =
4591 drm_atomic_get_existing_plane_state(old_state, primary);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004592
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004593 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004594
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004595 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004596
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004597 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004598 intel_update_watermarks(&crtc->base);
4599
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004600 if (old_pri_state) {
4601 struct intel_plane_state *primary_state =
4602 to_intel_plane_state(primary->state);
4603 struct intel_plane_state *old_primary_state =
4604 to_intel_plane_state(old_pri_state);
4605
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004606 intel_fbc_post_update(crtc);
4607
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004608 if (primary_state->visible &&
4609 (needs_modeset(&pipe_config->base) ||
4610 !old_primary_state->visible))
4611 intel_post_enable_primary(&crtc->base);
4612 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004613}
4614
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004615static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004616{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004617 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004618 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004619 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004620 struct intel_crtc_state *pipe_config =
4621 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004622 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4623 struct drm_plane *primary = crtc->base.primary;
4624 struct drm_plane_state *old_pri_state =
4625 drm_atomic_get_existing_plane_state(old_state, primary);
4626 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004627
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004628 if (old_pri_state) {
4629 struct intel_plane_state *primary_state =
4630 to_intel_plane_state(primary->state);
4631 struct intel_plane_state *old_primary_state =
4632 to_intel_plane_state(old_pri_state);
4633
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004634 intel_fbc_pre_update(crtc);
4635
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004636 if (old_primary_state->visible &&
4637 (modeset || !primary_state->visible))
4638 intel_pre_disable_primary(&crtc->base);
4639 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004640
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004641 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004642 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004643
Ville Syrjälä2622a082016-03-09 19:07:26 +02004644 /*
4645 * Vblank time updates from the shadow to live plane control register
4646 * are blocked if the memory self-refresh mode is active at that
4647 * moment. So to make sure the plane gets truly disabled, disable
4648 * first the self-refresh mode. The self-refresh enable bit in turn
4649 * will be checked/applied by the HW only at the next frame start
4650 * event which is after the vblank start event, so we need to have a
4651 * wait-for-vblank between disabling the plane and the pipe.
4652 */
4653 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004654 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004655 dev_priv->wm.vlv.cxsr = false;
4656 intel_wait_for_vblank(dev, crtc->pipe);
4657 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004658 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004659
Matt Ropered4a6a72016-02-23 17:20:13 -08004660 /*
4661 * IVB workaround: must disable low power watermarks for at least
4662 * one frame before enabling scaling. LP watermarks can be re-enabled
4663 * when scaling is disabled.
4664 *
4665 * WaCxSRDisabledForSpriteScaling:ivb
4666 */
4667 if (pipe_config->disable_lp_wm) {
4668 ilk_disable_lp_wm(dev);
4669 intel_wait_for_vblank(dev, crtc->pipe);
4670 }
4671
4672 /*
4673 * If we're doing a modeset, we're done. No need to do any pre-vblank
4674 * watermark programming here.
4675 */
4676 if (needs_modeset(&pipe_config->base))
4677 return;
4678
4679 /*
4680 * For platforms that support atomic watermarks, program the
4681 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4682 * will be the intermediate values that are safe for both pre- and
4683 * post- vblank; when vblank happens, the 'active' values will be set
4684 * to the final 'target' values and we'll do this again to get the
4685 * optimal watermarks. For gen9+ platforms, the values we program here
4686 * will be the final target values which will get automatically latched
4687 * at vblank time; no further programming will be necessary.
4688 *
4689 * If a platform hasn't been transitioned to atomic watermarks yet,
4690 * we'll continue to update watermarks the old way, if flags tell
4691 * us to.
4692 */
4693 if (dev_priv->display.initial_watermarks != NULL)
4694 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004695 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004696 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004697}
4698
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004699static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004700{
4701 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004702 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004703 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004704 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004705
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004706 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004707
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004708 drm_for_each_plane_mask(p, dev, plane_mask)
4709 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004710
Daniel Vetterf99d7062014-06-19 16:01:59 +02004711 /*
4712 * FIXME: Once we grow proper nuclear flip support out of this we need
4713 * to compute the mask of flip planes precisely. For the time being
4714 * consider this a flip to a NULL plane.
4715 */
4716 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004717}
4718
Jesse Barnesf67a5592011-01-05 10:31:48 -08004719static void ironlake_crtc_enable(struct drm_crtc *crtc)
4720{
4721 struct drm_device *dev = crtc->dev;
4722 struct drm_i915_private *dev_priv = dev->dev_private;
4723 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004724 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004725 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004726
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004727 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004728 return;
4729
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004730 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004731 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4732
4733 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004734 intel_prepare_shared_dpll(intel_crtc);
4735
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004736 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304737 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004738
4739 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004740 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004741
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004742 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004743 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004744 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004745 }
4746
4747 ironlake_set_pipeconf(crtc);
4748
Jesse Barnesf67a5592011-01-05 10:31:48 -08004749 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004750
Daniel Vettera72e4c92014-09-30 10:56:47 +02004751 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004752
Daniel Vetterf6736a12013-06-05 13:34:30 +02004753 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004754 if (encoder->pre_enable)
4755 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004756
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004757 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004758 /* Note: FDI PLL enabling _must_ be done before we enable the
4759 * cpu pipes, hence this is separate from all the other fdi/pch
4760 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004761 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004762 } else {
4763 assert_fdi_tx_disabled(dev_priv, pipe);
4764 assert_fdi_rx_disabled(dev_priv, pipe);
4765 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004766
Jesse Barnesb074cec2013-04-25 12:55:02 -07004767 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004768
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004769 /*
4770 * On ILK+ LUT must be loaded before the pipe is running but with
4771 * clocks enabled
4772 */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004773 intel_color_load_luts(crtc);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004774
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004775 if (dev_priv->display.initial_watermarks != NULL)
4776 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004777 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004778
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004779 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004780 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004781
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004782 assert_vblank_disabled(crtc);
4783 drm_crtc_vblank_on(crtc);
4784
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004785 for_each_encoder_on_crtc(dev, crtc, encoder)
4786 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004787
4788 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004789 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004790
4791 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4792 if (intel_crtc->config->has_pch_encoder)
4793 intel_wait_for_vblank(dev, pipe);
4794 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004795}
4796
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004797/* IPS only exists on ULT machines and is tied to pipe A. */
4798static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4799{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004800 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004801}
4802
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004803static void haswell_crtc_enable(struct drm_crtc *crtc)
4804{
4805 struct drm_device *dev = crtc->dev;
4806 struct drm_i915_private *dev_priv = dev->dev_private;
4807 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4808 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004809 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004810 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004811 struct intel_crtc_state *pipe_config =
4812 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004813
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004814 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004815 return;
4816
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004817 if (intel_crtc->config->has_pch_encoder)
4818 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4819 false);
4820
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004821 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004822 intel_enable_shared_dpll(intel_crtc);
4823
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004824 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304825 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004826
Jani Nikula4d1de972016-03-18 17:05:42 +02004827 if (!intel_crtc->config->has_dsi_encoder)
4828 intel_set_pipe_timings(intel_crtc);
4829
Jani Nikulabc58be62016-03-18 17:05:39 +02004830 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004831
Jani Nikula4d1de972016-03-18 17:05:42 +02004832 if (cpu_transcoder != TRANSCODER_EDP &&
4833 !transcoder_is_dsi(cpu_transcoder)) {
4834 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004835 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004836 }
4837
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004838 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004839 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004840 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004841 }
4842
Jani Nikula4d1de972016-03-18 17:05:42 +02004843 if (!intel_crtc->config->has_dsi_encoder)
4844 haswell_set_pipeconf(crtc);
4845
Jani Nikula391bf042016-03-18 17:05:40 +02004846 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004847
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004848 intel_color_set_csc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004849
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004850 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004851
Daniel Vetter6b698512015-11-28 11:05:39 +01004852 if (intel_crtc->config->has_pch_encoder)
4853 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4854 else
4855 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4856
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304857 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004858 if (encoder->pre_enable)
4859 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304860 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004861
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004862 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004863 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004864
Jani Nikulaa65347b2015-11-27 12:21:46 +02004865 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304866 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004867
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004868 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004869 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004870 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004871 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004872
4873 /*
4874 * On ILK+ LUT must be loaded before the pipe is running but with
4875 * clocks enabled
4876 */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00004877 intel_color_load_luts(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004878
Paulo Zanoni1f544382012-10-24 11:32:00 -02004879 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004880 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304881 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004882
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004883 if (dev_priv->display.initial_watermarks != NULL)
4884 dev_priv->display.initial_watermarks(pipe_config);
4885 else
4886 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004887
4888 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4889 if (!intel_crtc->config->has_dsi_encoder)
4890 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004891
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004892 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004893 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004894
Jani Nikulaa65347b2015-11-27 12:21:46 +02004895 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004896 intel_ddi_set_vc_payload_alloc(crtc, true);
4897
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004898 assert_vblank_disabled(crtc);
4899 drm_crtc_vblank_on(crtc);
4900
Jani Nikula8807e552013-08-30 19:40:32 +03004901 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004902 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004903 intel_opregion_notify_encoder(encoder, true);
4904 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004905
Daniel Vetter6b698512015-11-28 11:05:39 +01004906 if (intel_crtc->config->has_pch_encoder) {
4907 intel_wait_for_vblank(dev, pipe);
4908 intel_wait_for_vblank(dev, pipe);
4909 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004910 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4911 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004912 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004913
Paulo Zanonie4916942013-09-20 16:21:19 -03004914 /* If we change the relative order between pipe/planes enabling, we need
4915 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004916 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4917 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4918 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4919 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4920 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004921}
4922
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004923static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004924{
4925 struct drm_device *dev = crtc->base.dev;
4926 struct drm_i915_private *dev_priv = dev->dev_private;
4927 int pipe = crtc->pipe;
4928
4929 /* To avoid upsetting the power well on haswell only disable the pfit if
4930 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004931 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004932 I915_WRITE(PF_CTL(pipe), 0);
4933 I915_WRITE(PF_WIN_POS(pipe), 0);
4934 I915_WRITE(PF_WIN_SZ(pipe), 0);
4935 }
4936}
4937
Jesse Barnes6be4a602010-09-10 10:26:01 -07004938static void ironlake_crtc_disable(struct drm_crtc *crtc)
4939{
4940 struct drm_device *dev = crtc->dev;
4941 struct drm_i915_private *dev_priv = dev->dev_private;
4942 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004943 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004944 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004945
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004946 if (intel_crtc->config->has_pch_encoder)
4947 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4948
Daniel Vetterea9d7582012-07-10 10:42:52 +02004949 for_each_encoder_on_crtc(dev, crtc, encoder)
4950 encoder->disable(encoder);
4951
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004952 drm_crtc_vblank_off(crtc);
4953 assert_vblank_disabled(crtc);
4954
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004955 /*
4956 * Sometimes spurious CPU pipe underruns happen when the
4957 * pipe is already disabled, but FDI RX/TX is still enabled.
4958 * Happens at least with VGA+HDMI cloning. Suppress them.
4959 */
4960 if (intel_crtc->config->has_pch_encoder)
4961 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4962
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004963 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004964
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004965 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004966
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004967 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004968 ironlake_fdi_disable(crtc);
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004969 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4970 }
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004971
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004972 for_each_encoder_on_crtc(dev, crtc, encoder)
4973 if (encoder->post_disable)
4974 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004975
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004976 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004977 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004978
Daniel Vetterd925c592013-06-05 13:34:04 +02004979 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004980 i915_reg_t reg;
4981 u32 temp;
4982
Daniel Vetterd925c592013-06-05 13:34:04 +02004983 /* disable TRANS_DP_CTL */
4984 reg = TRANS_DP_CTL(pipe);
4985 temp = I915_READ(reg);
4986 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4987 TRANS_DP_PORT_SEL_MASK);
4988 temp |= TRANS_DP_PORT_SEL_NONE;
4989 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004990
Daniel Vetterd925c592013-06-05 13:34:04 +02004991 /* disable DPLL_SEL */
4992 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004993 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004994 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004995 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004996
Daniel Vetterd925c592013-06-05 13:34:04 +02004997 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004998 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004999
5000 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005001}
5002
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005003static void haswell_crtc_disable(struct drm_crtc *crtc)
5004{
5005 struct drm_device *dev = crtc->dev;
5006 struct drm_i915_private *dev_priv = dev->dev_private;
5007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5008 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005009 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005010
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005011 if (intel_crtc->config->has_pch_encoder)
5012 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5013 false);
5014
Jani Nikula8807e552013-08-30 19:40:32 +03005015 for_each_encoder_on_crtc(dev, crtc, encoder) {
5016 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005017 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005018 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005019
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005020 drm_crtc_vblank_off(crtc);
5021 assert_vblank_disabled(crtc);
5022
Jani Nikula4d1de972016-03-18 17:05:42 +02005023 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5024 if (!intel_crtc->config->has_dsi_encoder)
5025 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005026
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005027 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005028 intel_ddi_set_vc_payload_alloc(crtc, false);
5029
Jani Nikulaa65347b2015-11-27 12:21:46 +02005030 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305031 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005032
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005033 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005034 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005035 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005036 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005037
Jani Nikulaa65347b2015-11-27 12:21:46 +02005038 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305039 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005040
Imre Deak97b040a2014-06-25 22:01:50 +03005041 for_each_encoder_on_crtc(dev, crtc, encoder)
5042 if (encoder->post_disable)
5043 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005044
Ville Syrjälä92966a32015-12-08 16:05:48 +02005045 if (intel_crtc->config->has_pch_encoder) {
5046 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005047 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005048 intel_ddi_fdi_disable(crtc);
5049
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005050 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5051 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005052 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005053}
5054
Jesse Barnes2dd24552013-04-25 12:55:01 -07005055static void i9xx_pfit_enable(struct intel_crtc *crtc)
5056{
5057 struct drm_device *dev = crtc->base.dev;
5058 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005059 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005060
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005061 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005062 return;
5063
Daniel Vetterc0b03412013-05-28 12:05:54 +02005064 /*
5065 * The panel fitter should only be adjusted whilst the pipe is disabled,
5066 * according to register description and PRM.
5067 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005068 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5069 assert_pipe_disabled(dev_priv, crtc->pipe);
5070
Jesse Barnesb074cec2013-04-25 12:55:02 -07005071 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5072 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005073
5074 /* Border color in case we don't scale up to the full screen. Black by
5075 * default, change to something else for debugging. */
5076 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005077}
5078
Dave Airlied05410f2014-06-05 13:22:59 +10005079static enum intel_display_power_domain port_to_power_domain(enum port port)
5080{
5081 switch (port) {
5082 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005083 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005084 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005085 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005086 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005087 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005088 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005089 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005090 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005091 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005092 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005093 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005094 return POWER_DOMAIN_PORT_OTHER;
5095 }
5096}
5097
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005098static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5099{
5100 switch (port) {
5101 case PORT_A:
5102 return POWER_DOMAIN_AUX_A;
5103 case PORT_B:
5104 return POWER_DOMAIN_AUX_B;
5105 case PORT_C:
5106 return POWER_DOMAIN_AUX_C;
5107 case PORT_D:
5108 return POWER_DOMAIN_AUX_D;
5109 case PORT_E:
5110 /* FIXME: Check VBT for actual wiring of PORT E */
5111 return POWER_DOMAIN_AUX_D;
5112 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005113 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005114 return POWER_DOMAIN_AUX_A;
5115 }
5116}
5117
Imre Deak319be8a2014-03-04 19:22:57 +02005118enum intel_display_power_domain
5119intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005120{
Imre Deak319be8a2014-03-04 19:22:57 +02005121 struct drm_device *dev = intel_encoder->base.dev;
5122 struct intel_digital_port *intel_dig_port;
5123
5124 switch (intel_encoder->type) {
5125 case INTEL_OUTPUT_UNKNOWN:
5126 /* Only DDI platforms should ever use this output type */
5127 WARN_ON_ONCE(!HAS_DDI(dev));
5128 case INTEL_OUTPUT_DISPLAYPORT:
5129 case INTEL_OUTPUT_HDMI:
5130 case INTEL_OUTPUT_EDP:
5131 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005132 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005133 case INTEL_OUTPUT_DP_MST:
5134 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5135 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005136 case INTEL_OUTPUT_ANALOG:
5137 return POWER_DOMAIN_PORT_CRT;
5138 case INTEL_OUTPUT_DSI:
5139 return POWER_DOMAIN_PORT_DSI;
5140 default:
5141 return POWER_DOMAIN_PORT_OTHER;
5142 }
5143}
5144
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005145enum intel_display_power_domain
5146intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5147{
5148 struct drm_device *dev = intel_encoder->base.dev;
5149 struct intel_digital_port *intel_dig_port;
5150
5151 switch (intel_encoder->type) {
5152 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005153 case INTEL_OUTPUT_HDMI:
5154 /*
5155 * Only DDI platforms should ever use these output types.
5156 * We can get here after the HDMI detect code has already set
5157 * the type of the shared encoder. Since we can't be sure
5158 * what's the status of the given connectors, play safe and
5159 * run the DP detection too.
5160 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005161 WARN_ON_ONCE(!HAS_DDI(dev));
5162 case INTEL_OUTPUT_DISPLAYPORT:
5163 case INTEL_OUTPUT_EDP:
5164 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5165 return port_to_aux_power_domain(intel_dig_port->port);
5166 case INTEL_OUTPUT_DP_MST:
5167 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5168 return port_to_aux_power_domain(intel_dig_port->port);
5169 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005170 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005171 return POWER_DOMAIN_AUX_A;
5172 }
5173}
5174
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005175static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5176 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005177{
5178 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005179 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5181 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005182 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005183 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005184
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005185 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005186 return 0;
5187
Imre Deak77d22dc2014-03-05 16:20:52 +02005188 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5189 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005190 if (crtc_state->pch_pfit.enabled ||
5191 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005192 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5193
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005194 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5195 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5196
Imre Deak319be8a2014-03-04 19:22:57 +02005197 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005198 }
Imre Deak319be8a2014-03-04 19:22:57 +02005199
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005200 if (crtc_state->shared_dpll)
5201 mask |= BIT(POWER_DOMAIN_PLLS);
5202
Imre Deak77d22dc2014-03-05 16:20:52 +02005203 return mask;
5204}
5205
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005206static unsigned long
5207modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5208 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005209{
5210 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5211 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5212 enum intel_display_power_domain domain;
5213 unsigned long domains, new_domains, old_domains;
5214
5215 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005216 intel_crtc->enabled_power_domains = new_domains =
5217 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005218
5219 domains = new_domains & ~old_domains;
5220
5221 for_each_power_domain(domain, domains)
5222 intel_display_power_get(dev_priv, domain);
5223
5224 return old_domains & ~new_domains;
5225}
5226
5227static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5228 unsigned long domains)
5229{
5230 enum intel_display_power_domain domain;
5231
5232 for_each_power_domain(domain, domains)
5233 intel_display_power_put(dev_priv, domain);
5234}
5235
Mika Kaholaadafdc62015-08-18 14:36:59 +03005236static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5237{
5238 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5239
5240 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5241 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5242 return max_cdclk_freq;
5243 else if (IS_CHERRYVIEW(dev_priv))
5244 return max_cdclk_freq*95/100;
5245 else if (INTEL_INFO(dev_priv)->gen < 4)
5246 return 2*max_cdclk_freq*90/100;
5247 else
5248 return max_cdclk_freq*90/100;
5249}
5250
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005251static void intel_update_max_cdclk(struct drm_device *dev)
5252{
5253 struct drm_i915_private *dev_priv = dev->dev_private;
5254
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005255 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005256 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5257
5258 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5259 dev_priv->max_cdclk_freq = 675000;
5260 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5261 dev_priv->max_cdclk_freq = 540000;
5262 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5263 dev_priv->max_cdclk_freq = 450000;
5264 else
5265 dev_priv->max_cdclk_freq = 337500;
5266 } else if (IS_BROADWELL(dev)) {
5267 /*
5268 * FIXME with extra cooling we can allow
5269 * 540 MHz for ULX and 675 Mhz for ULT.
5270 * How can we know if extra cooling is
5271 * available? PCI ID, VTB, something else?
5272 */
5273 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5274 dev_priv->max_cdclk_freq = 450000;
5275 else if (IS_BDW_ULX(dev))
5276 dev_priv->max_cdclk_freq = 450000;
5277 else if (IS_BDW_ULT(dev))
5278 dev_priv->max_cdclk_freq = 540000;
5279 else
5280 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005281 } else if (IS_CHERRYVIEW(dev)) {
5282 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005283 } else if (IS_VALLEYVIEW(dev)) {
5284 dev_priv->max_cdclk_freq = 400000;
5285 } else {
5286 /* otherwise assume cdclk is fixed */
5287 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5288 }
5289
Mika Kaholaadafdc62015-08-18 14:36:59 +03005290 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5291
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005292 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5293 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005294
5295 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5296 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005297}
5298
5299static void intel_update_cdclk(struct drm_device *dev)
5300{
5301 struct drm_i915_private *dev_priv = dev->dev_private;
5302
5303 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5304 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5305 dev_priv->cdclk_freq);
5306
5307 /*
5308 * Program the gmbus_freq based on the cdclk frequency.
5309 * BSpec erroneously claims we should aim for 4MHz, but
5310 * in fact 1MHz is the correct frequency.
5311 */
Wayne Boyer666a4532015-12-09 12:29:35 -08005312 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005313 /*
5314 * Program the gmbus_freq based on the cdclk frequency.
5315 * BSpec erroneously claims we should aim for 4MHz, but
5316 * in fact 1MHz is the correct frequency.
5317 */
5318 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5319 }
5320
5321 if (dev_priv->max_cdclk_freq == 0)
5322 intel_update_max_cdclk(dev);
5323}
5324
Damien Lespiau70d0c572015-06-04 18:21:29 +01005325static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305326{
5327 struct drm_i915_private *dev_priv = dev->dev_private;
5328 uint32_t divider;
5329 uint32_t ratio;
5330 uint32_t current_freq;
5331 int ret;
5332
5333 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5334 switch (frequency) {
5335 case 144000:
5336 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5337 ratio = BXT_DE_PLL_RATIO(60);
5338 break;
5339 case 288000:
5340 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5341 ratio = BXT_DE_PLL_RATIO(60);
5342 break;
5343 case 384000:
5344 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5345 ratio = BXT_DE_PLL_RATIO(60);
5346 break;
5347 case 576000:
5348 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5349 ratio = BXT_DE_PLL_RATIO(60);
5350 break;
5351 case 624000:
5352 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5353 ratio = BXT_DE_PLL_RATIO(65);
5354 break;
5355 case 19200:
5356 /*
5357 * Bypass frequency with DE PLL disabled. Init ratio, divider
5358 * to suppress GCC warning.
5359 */
5360 ratio = 0;
5361 divider = 0;
5362 break;
5363 default:
5364 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5365
5366 return;
5367 }
5368
5369 mutex_lock(&dev_priv->rps.hw_lock);
5370 /* Inform power controller of upcoming frequency change */
5371 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5372 0x80000000);
5373 mutex_unlock(&dev_priv->rps.hw_lock);
5374
5375 if (ret) {
5376 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5377 ret, frequency);
5378 return;
5379 }
5380
5381 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5382 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5383 current_freq = current_freq * 500 + 1000;
5384
5385 /*
5386 * DE PLL has to be disabled when
5387 * - setting to 19.2MHz (bypass, PLL isn't used)
5388 * - before setting to 624MHz (PLL needs toggling)
5389 * - before setting to any frequency from 624MHz (PLL needs toggling)
5390 */
5391 if (frequency == 19200 || frequency == 624000 ||
5392 current_freq == 624000) {
5393 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5394 /* Timeout 200us */
5395 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5396 1))
5397 DRM_ERROR("timout waiting for DE PLL unlock\n");
5398 }
5399
5400 if (frequency != 19200) {
5401 uint32_t val;
5402
5403 val = I915_READ(BXT_DE_PLL_CTL);
5404 val &= ~BXT_DE_PLL_RATIO_MASK;
5405 val |= ratio;
5406 I915_WRITE(BXT_DE_PLL_CTL, val);
5407
5408 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5409 /* Timeout 200us */
5410 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5411 DRM_ERROR("timeout waiting for DE PLL lock\n");
5412
5413 val = I915_READ(CDCLK_CTL);
5414 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5415 val |= divider;
5416 /*
5417 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5418 * enable otherwise.
5419 */
5420 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5421 if (frequency >= 500000)
5422 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5423
5424 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5425 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5426 val |= (frequency - 1000) / 500;
5427 I915_WRITE(CDCLK_CTL, val);
5428 }
5429
5430 mutex_lock(&dev_priv->rps.hw_lock);
5431 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5432 DIV_ROUND_UP(frequency, 25000));
5433 mutex_unlock(&dev_priv->rps.hw_lock);
5434
5435 if (ret) {
5436 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5437 ret, frequency);
5438 return;
5439 }
5440
Damien Lespiaua47871b2015-06-04 18:21:34 +01005441 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305442}
5443
5444void broxton_init_cdclk(struct drm_device *dev)
5445{
5446 struct drm_i915_private *dev_priv = dev->dev_private;
5447 uint32_t val;
5448
5449 /*
5450 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5451 * or else the reset will hang because there is no PCH to respond.
5452 * Move the handshake programming to initialization sequence.
5453 * Previously was left up to BIOS.
5454 */
5455 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5456 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5457 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5458
5459 /* Enable PG1 for cdclk */
5460 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5461
5462 /* check if cd clock is enabled */
5463 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5464 DRM_DEBUG_KMS("Display already initialized\n");
5465 return;
5466 }
5467
5468 /*
5469 * FIXME:
5470 * - The initial CDCLK needs to be read from VBT.
5471 * Need to make this change after VBT has changes for BXT.
5472 * - check if setting the max (or any) cdclk freq is really necessary
5473 * here, it belongs to modeset time
5474 */
5475 broxton_set_cdclk(dev, 624000);
5476
5477 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005478 POSTING_READ(DBUF_CTL);
5479
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305480 udelay(10);
5481
5482 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5483 DRM_ERROR("DBuf power enable timeout!\n");
5484}
5485
5486void broxton_uninit_cdclk(struct drm_device *dev)
5487{
5488 struct drm_i915_private *dev_priv = dev->dev_private;
5489
5490 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005491 POSTING_READ(DBUF_CTL);
5492
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305493 udelay(10);
5494
5495 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5496 DRM_ERROR("DBuf power disable timeout!\n");
5497
5498 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5499 broxton_set_cdclk(dev, 19200);
5500
5501 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5502}
5503
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005504static const struct skl_cdclk_entry {
5505 unsigned int freq;
5506 unsigned int vco;
5507} skl_cdclk_frequencies[] = {
5508 { .freq = 308570, .vco = 8640 },
5509 { .freq = 337500, .vco = 8100 },
5510 { .freq = 432000, .vco = 8640 },
5511 { .freq = 450000, .vco = 8100 },
5512 { .freq = 540000, .vco = 8100 },
5513 { .freq = 617140, .vco = 8640 },
5514 { .freq = 675000, .vco = 8100 },
5515};
5516
5517static unsigned int skl_cdclk_decimal(unsigned int freq)
5518{
5519 return (freq - 1000) / 500;
5520}
5521
5522static unsigned int skl_cdclk_get_vco(unsigned int freq)
5523{
5524 unsigned int i;
5525
5526 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5527 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5528
5529 if (e->freq == freq)
5530 return e->vco;
5531 }
5532
5533 return 8100;
5534}
5535
5536static void
5537skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5538{
5539 unsigned int min_freq;
5540 u32 val;
5541
5542 /* select the minimum CDCLK before enabling DPLL 0 */
5543 val = I915_READ(CDCLK_CTL);
5544 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5545 val |= CDCLK_FREQ_337_308;
5546
5547 if (required_vco == 8640)
5548 min_freq = 308570;
5549 else
5550 min_freq = 337500;
5551
5552 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5553
5554 I915_WRITE(CDCLK_CTL, val);
5555 POSTING_READ(CDCLK_CTL);
5556
5557 /*
5558 * We always enable DPLL0 with the lowest link rate possible, but still
5559 * taking into account the VCO required to operate the eDP panel at the
5560 * desired frequency. The usual DP link rates operate with a VCO of
5561 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5562 * The modeset code is responsible for the selection of the exact link
5563 * rate later on, with the constraint of choosing a frequency that
5564 * works with required_vco.
5565 */
5566 val = I915_READ(DPLL_CTRL1);
5567
5568 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5569 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5570 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5571 if (required_vco == 8640)
5572 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5573 SKL_DPLL0);
5574 else
5575 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5576 SKL_DPLL0);
5577
5578 I915_WRITE(DPLL_CTRL1, val);
5579 POSTING_READ(DPLL_CTRL1);
5580
5581 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5582
5583 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5584 DRM_ERROR("DPLL0 not locked\n");
5585}
5586
5587static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5588{
5589 int ret;
5590 u32 val;
5591
5592 /* inform PCU we want to change CDCLK */
5593 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5594 mutex_lock(&dev_priv->rps.hw_lock);
5595 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5596 mutex_unlock(&dev_priv->rps.hw_lock);
5597
5598 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5599}
5600
5601static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5602{
5603 unsigned int i;
5604
5605 for (i = 0; i < 15; i++) {
5606 if (skl_cdclk_pcu_ready(dev_priv))
5607 return true;
5608 udelay(10);
5609 }
5610
5611 return false;
5612}
5613
5614static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5615{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005616 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005617 u32 freq_select, pcu_ack;
5618
5619 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5620
5621 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5622 DRM_ERROR("failed to inform PCU about cdclk change\n");
5623 return;
5624 }
5625
5626 /* set CDCLK_CTL */
5627 switch(freq) {
5628 case 450000:
5629 case 432000:
5630 freq_select = CDCLK_FREQ_450_432;
5631 pcu_ack = 1;
5632 break;
5633 case 540000:
5634 freq_select = CDCLK_FREQ_540;
5635 pcu_ack = 2;
5636 break;
5637 case 308570:
5638 case 337500:
5639 default:
5640 freq_select = CDCLK_FREQ_337_308;
5641 pcu_ack = 0;
5642 break;
5643 case 617140:
5644 case 675000:
5645 freq_select = CDCLK_FREQ_675_617;
5646 pcu_ack = 3;
5647 break;
5648 }
5649
5650 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5651 POSTING_READ(CDCLK_CTL);
5652
5653 /* inform PCU of the change */
5654 mutex_lock(&dev_priv->rps.hw_lock);
5655 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5656 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005657
5658 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005659}
5660
5661void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5662{
5663 /* disable DBUF power */
5664 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5665 POSTING_READ(DBUF_CTL);
5666
5667 udelay(10);
5668
5669 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5670 DRM_ERROR("DBuf power disable timeout\n");
5671
Imre Deakab96c1ee2015-11-04 19:24:18 +02005672 /* disable DPLL0 */
5673 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5674 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5675 DRM_ERROR("Couldn't disable DPLL0\n");
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005676}
5677
5678void skl_init_cdclk(struct drm_i915_private *dev_priv)
5679{
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005680 unsigned int required_vco;
5681
Gary Wang39d9b852015-08-28 16:40:34 +08005682 /* DPLL0 not enabled (happens on early BIOS versions) */
5683 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5684 /* enable DPLL0 */
5685 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5686 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005687 }
5688
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005689 /* set CDCLK to the frequency the BIOS chose */
5690 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5691
5692 /* enable DBUF power */
5693 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5694 POSTING_READ(DBUF_CTL);
5695
5696 udelay(10);
5697
5698 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5699 DRM_ERROR("DBuf power enable timeout\n");
5700}
5701
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305702int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5703{
5704 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5705 uint32_t cdctl = I915_READ(CDCLK_CTL);
5706 int freq = dev_priv->skl_boot_cdclk;
5707
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305708 /*
5709 * check if the pre-os intialized the display
5710 * There is SWF18 scratchpad register defined which is set by the
5711 * pre-os which can be used by the OS drivers to check the status
5712 */
5713 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5714 goto sanitize;
5715
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305716 /* Is PLL enabled and locked ? */
5717 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5718 goto sanitize;
5719
5720 /* DPLL okay; verify the cdclock
5721 *
5722 * Noticed in some instances that the freq selection is correct but
5723 * decimal part is programmed wrong from BIOS where pre-os does not
5724 * enable display. Verify the same as well.
5725 */
5726 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5727 /* All well; nothing to sanitize */
5728 return false;
5729sanitize:
5730 /*
5731 * As of now initialize with max cdclk till
5732 * we get dynamic cdclk support
5733 * */
5734 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5735 skl_init_cdclk(dev_priv);
5736
5737 /* we did have to sanitize */
5738 return true;
5739}
5740
Jesse Barnes30a970c2013-11-04 13:48:12 -08005741/* Adjust CDclk dividers to allow high res or save power if possible */
5742static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5743{
5744 struct drm_i915_private *dev_priv = dev->dev_private;
5745 u32 val, cmd;
5746
Vandana Kannan164dfd22014-11-24 13:37:41 +05305747 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5748 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005749
Ville Syrjälädfcab172014-06-13 13:37:47 +03005750 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005751 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005752 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005753 cmd = 1;
5754 else
5755 cmd = 0;
5756
5757 mutex_lock(&dev_priv->rps.hw_lock);
5758 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5759 val &= ~DSPFREQGUAR_MASK;
5760 val |= (cmd << DSPFREQGUAR_SHIFT);
5761 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5762 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5763 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5764 50)) {
5765 DRM_ERROR("timed out waiting for CDclk change\n");
5766 }
5767 mutex_unlock(&dev_priv->rps.hw_lock);
5768
Ville Syrjälä54433e92015-05-26 20:42:31 +03005769 mutex_lock(&dev_priv->sb_lock);
5770
Ville Syrjälädfcab172014-06-13 13:37:47 +03005771 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005772 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005773
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005774 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005775
Jesse Barnes30a970c2013-11-04 13:48:12 -08005776 /* adjust cdclk divider */
5777 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005778 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005779 val |= divider;
5780 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005781
5782 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005783 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005784 50))
5785 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005786 }
5787
Jesse Barnes30a970c2013-11-04 13:48:12 -08005788 /* adjust self-refresh exit latency value */
5789 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5790 val &= ~0x7f;
5791
5792 /*
5793 * For high bandwidth configs, we set a higher latency in the bunit
5794 * so that the core display fetch happens in time to avoid underruns.
5795 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005796 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005797 val |= 4500 / 250; /* 4.5 usec */
5798 else
5799 val |= 3000 / 250; /* 3.0 usec */
5800 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005801
Ville Syrjäläa5805162015-05-26 20:42:30 +03005802 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005803
Ville Syrjäläb6283052015-06-03 15:45:07 +03005804 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005805}
5806
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005807static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5808{
5809 struct drm_i915_private *dev_priv = dev->dev_private;
5810 u32 val, cmd;
5811
Vandana Kannan164dfd22014-11-24 13:37:41 +05305812 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5813 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005814
5815 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005816 case 333333:
5817 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005818 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005819 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005820 break;
5821 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005822 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005823 return;
5824 }
5825
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005826 /*
5827 * Specs are full of misinformation, but testing on actual
5828 * hardware has shown that we just need to write the desired
5829 * CCK divider into the Punit register.
5830 */
5831 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5832
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005833 mutex_lock(&dev_priv->rps.hw_lock);
5834 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5835 val &= ~DSPFREQGUAR_MASK_CHV;
5836 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5837 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5838 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5839 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5840 50)) {
5841 DRM_ERROR("timed out waiting for CDclk change\n");
5842 }
5843 mutex_unlock(&dev_priv->rps.hw_lock);
5844
Ville Syrjäläb6283052015-06-03 15:45:07 +03005845 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005846}
5847
Jesse Barnes30a970c2013-11-04 13:48:12 -08005848static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5849 int max_pixclk)
5850{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005851 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005852 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005853
Jesse Barnes30a970c2013-11-04 13:48:12 -08005854 /*
5855 * Really only a few cases to deal with, as only 4 CDclks are supported:
5856 * 200MHz
5857 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005858 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005859 * 400MHz (VLV only)
5860 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5861 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005862 *
5863 * We seem to get an unstable or solid color picture at 200MHz.
5864 * Not sure what's wrong. For now use 200MHz only when all pipes
5865 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005866 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005867 if (!IS_CHERRYVIEW(dev_priv) &&
5868 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005869 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005870 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005871 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005872 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005873 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005874 else
5875 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005876}
5877
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305878static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5879 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005880{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305881 /*
5882 * FIXME:
5883 * - remove the guardband, it's not needed on BXT
5884 * - set 19.2MHz bypass frequency if there are no active pipes
5885 */
5886 if (max_pixclk > 576000*9/10)
5887 return 624000;
5888 else if (max_pixclk > 384000*9/10)
5889 return 576000;
5890 else if (max_pixclk > 288000*9/10)
5891 return 384000;
5892 else if (max_pixclk > 144000*9/10)
5893 return 288000;
5894 else
5895 return 144000;
5896}
5897
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005898/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005899static int intel_mode_max_pixclk(struct drm_device *dev,
5900 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005901{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005902 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5903 struct drm_i915_private *dev_priv = dev->dev_private;
5904 struct drm_crtc *crtc;
5905 struct drm_crtc_state *crtc_state;
5906 unsigned max_pixclk = 0, i;
5907 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005908
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005909 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5910 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005911
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005912 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5913 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005914
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005915 if (crtc_state->enable)
5916 pixclk = crtc_state->adjusted_mode.crtc_clock;
5917
5918 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005919 }
5920
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005921 for_each_pipe(dev_priv, pipe)
5922 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5923
Jesse Barnes30a970c2013-11-04 13:48:12 -08005924 return max_pixclk;
5925}
5926
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005927static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005928{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005929 struct drm_device *dev = state->dev;
5930 struct drm_i915_private *dev_priv = dev->dev_private;
5931 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005932 struct intel_atomic_state *intel_state =
5933 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005934
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005935 if (max_pixclk < 0)
5936 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005937
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005938 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005939 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305940
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005941 if (!intel_state->active_crtcs)
5942 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5943
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005944 return 0;
5945}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005946
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005947static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5948{
5949 struct drm_device *dev = state->dev;
5950 struct drm_i915_private *dev_priv = dev->dev_private;
5951 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005952 struct intel_atomic_state *intel_state =
5953 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005954
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005955 if (max_pixclk < 0)
5956 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005957
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005958 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005959 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005960
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005961 if (!intel_state->active_crtcs)
5962 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
5963
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005964 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005965}
5966
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005967static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5968{
5969 unsigned int credits, default_credits;
5970
5971 if (IS_CHERRYVIEW(dev_priv))
5972 default_credits = PFI_CREDIT(12);
5973 else
5974 default_credits = PFI_CREDIT(8);
5975
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005976 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005977 /* CHV suggested value is 31 or 63 */
5978 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005979 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005980 else
5981 credits = PFI_CREDIT(15);
5982 } else {
5983 credits = default_credits;
5984 }
5985
5986 /*
5987 * WA - write default credits before re-programming
5988 * FIXME: should we also set the resend bit here?
5989 */
5990 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5991 default_credits);
5992
5993 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5994 credits | PFI_CREDIT_RESEND);
5995
5996 /*
5997 * FIXME is this guaranteed to clear
5998 * immediately or should we poll for it?
5999 */
6000 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6001}
6002
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006003static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006004{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006005 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006006 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006007 struct intel_atomic_state *old_intel_state =
6008 to_intel_atomic_state(old_state);
6009 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006010
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006011 /*
6012 * FIXME: We can end up here with all power domains off, yet
6013 * with a CDCLK frequency other than the minimum. To account
6014 * for this take the PIPE-A power domain, which covers the HW
6015 * blocks needed for the following programming. This can be
6016 * removed once it's guaranteed that we get here either with
6017 * the minimum CDCLK set, or the required power domains
6018 * enabled.
6019 */
6020 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006021
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006022 if (IS_CHERRYVIEW(dev))
6023 cherryview_set_cdclk(dev, req_cdclk);
6024 else
6025 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006026
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006027 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006028
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006029 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006030}
6031
Jesse Barnes89b667f2013-04-18 14:51:36 -07006032static void valleyview_crtc_enable(struct drm_crtc *crtc)
6033{
6034 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006035 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006036 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6037 struct intel_encoder *encoder;
6038 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006039
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006040 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006041 return;
6042
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006043 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306044 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006045
6046 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006047 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006048
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006049 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6050 struct drm_i915_private *dev_priv = dev->dev_private;
6051
6052 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6053 I915_WRITE(CHV_CANVAS(pipe), 0);
6054 }
6055
Daniel Vetter5b18e572014-04-24 23:55:06 +02006056 i9xx_set_pipeconf(intel_crtc);
6057
Jesse Barnes89b667f2013-04-18 14:51:36 -07006058 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006059
Daniel Vettera72e4c92014-09-30 10:56:47 +02006060 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006061
Jesse Barnes89b667f2013-04-18 14:51:36 -07006062 for_each_encoder_on_crtc(dev, crtc, encoder)
6063 if (encoder->pre_pll_enable)
6064 encoder->pre_pll_enable(encoder);
6065
Jani Nikulaa65347b2015-11-27 12:21:46 +02006066 if (!intel_crtc->config->has_dsi_encoder) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006067 if (IS_CHERRYVIEW(dev)) {
6068 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006069 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006070 } else {
6071 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006072 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006073 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006074 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006075
6076 for_each_encoder_on_crtc(dev, crtc, encoder)
6077 if (encoder->pre_enable)
6078 encoder->pre_enable(encoder);
6079
Jesse Barnes2dd24552013-04-25 12:55:01 -07006080 i9xx_pfit_enable(intel_crtc);
6081
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00006082 intel_color_load_luts(crtc);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006083
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006084 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006085 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006086
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006087 assert_vblank_disabled(crtc);
6088 drm_crtc_vblank_on(crtc);
6089
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006090 for_each_encoder_on_crtc(dev, crtc, encoder)
6091 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006092}
6093
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006094static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6095{
6096 struct drm_device *dev = crtc->base.dev;
6097 struct drm_i915_private *dev_priv = dev->dev_private;
6098
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006099 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6100 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006101}
6102
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006103static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006104{
6105 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006106 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006107 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006108 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006109 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006110
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006111 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006112 return;
6113
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006114 i9xx_set_pll_dividers(intel_crtc);
6115
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006116 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306117 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006118
6119 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006120 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006121
Daniel Vetter5b18e572014-04-24 23:55:06 +02006122 i9xx_set_pipeconf(intel_crtc);
6123
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006124 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006125
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006126 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006127 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006128
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006129 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006130 if (encoder->pre_enable)
6131 encoder->pre_enable(encoder);
6132
Daniel Vetterf6736a12013-06-05 13:34:30 +02006133 i9xx_enable_pll(intel_crtc);
6134
Jesse Barnes2dd24552013-04-25 12:55:01 -07006135 i9xx_pfit_enable(intel_crtc);
6136
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00006137 intel_color_load_luts(crtc);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006138
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006139 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006140 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006141
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006142 assert_vblank_disabled(crtc);
6143 drm_crtc_vblank_on(crtc);
6144
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006145 for_each_encoder_on_crtc(dev, crtc, encoder)
6146 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006147}
6148
Daniel Vetter87476d62013-04-11 16:29:06 +02006149static void i9xx_pfit_disable(struct intel_crtc *crtc)
6150{
6151 struct drm_device *dev = crtc->base.dev;
6152 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006153
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006154 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006155 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006156
6157 assert_pipe_disabled(dev_priv, crtc->pipe);
6158
Daniel Vetter328d8e82013-05-08 10:36:31 +02006159 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6160 I915_READ(PFIT_CONTROL));
6161 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006162}
6163
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006164static void i9xx_crtc_disable(struct drm_crtc *crtc)
6165{
6166 struct drm_device *dev = crtc->dev;
6167 struct drm_i915_private *dev_priv = dev->dev_private;
6168 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006169 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006170 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006171
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006172 /*
6173 * On gen2 planes are double buffered but the pipe isn't, so we must
6174 * wait for planes to fully turn off before disabling the pipe.
6175 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006176 if (IS_GEN2(dev))
6177 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006178
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006179 for_each_encoder_on_crtc(dev, crtc, encoder)
6180 encoder->disable(encoder);
6181
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006182 drm_crtc_vblank_off(crtc);
6183 assert_vblank_disabled(crtc);
6184
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006185 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006186
Daniel Vetter87476d62013-04-11 16:29:06 +02006187 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006188
Jesse Barnes89b667f2013-04-18 14:51:36 -07006189 for_each_encoder_on_crtc(dev, crtc, encoder)
6190 if (encoder->post_disable)
6191 encoder->post_disable(encoder);
6192
Jani Nikulaa65347b2015-11-27 12:21:46 +02006193 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006194 if (IS_CHERRYVIEW(dev))
6195 chv_disable_pll(dev_priv, pipe);
6196 else if (IS_VALLEYVIEW(dev))
6197 vlv_disable_pll(dev_priv, pipe);
6198 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006199 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006200 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006201
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006202 for_each_encoder_on_crtc(dev, crtc, encoder)
6203 if (encoder->post_pll_disable)
6204 encoder->post_pll_disable(encoder);
6205
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006206 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006207 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006208}
6209
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006210static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006211{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006212 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006213 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006214 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006215 enum intel_display_power_domain domain;
6216 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006217
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006218 if (!intel_crtc->active)
6219 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006220
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006221 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006222 WARN_ON(intel_crtc->unpin_work);
6223
Ville Syrjälä2622a082016-03-09 19:07:26 +02006224 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006225
6226 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6227 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006228 }
6229
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006230 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006231
6232 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6233 crtc->base.id);
6234
6235 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6236 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006237 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006238 crtc->enabled = false;
6239 crtc->state->connector_mask = 0;
6240 crtc->state->encoder_mask = 0;
6241
6242 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6243 encoder->base.crtc = NULL;
6244
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006245 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006246 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006247 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006248
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006249 domains = intel_crtc->enabled_power_domains;
6250 for_each_power_domain(domain, domains)
6251 intel_display_power_put(dev_priv, domain);
6252 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006253
6254 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6255 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006256}
6257
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006258/*
6259 * turn all crtc's off, but do not adjust state
6260 * This has to be paired with a call to intel_modeset_setup_hw_state.
6261 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006262int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006263{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006264 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006265 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006266 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006267
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006268 state = drm_atomic_helper_suspend(dev);
6269 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006270 if (ret)
6271 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006272 else
6273 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006274 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006275}
6276
Chris Wilsonea5b2132010-08-04 13:50:23 +01006277void intel_encoder_destroy(struct drm_encoder *encoder)
6278{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006279 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006280
Chris Wilsonea5b2132010-08-04 13:50:23 +01006281 drm_encoder_cleanup(encoder);
6282 kfree(intel_encoder);
6283}
6284
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006285/* Cross check the actual hw state with our own modeset state tracking (and it's
6286 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006287static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006288{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006289 struct drm_crtc *crtc = connector->base.state->crtc;
6290
6291 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6292 connector->base.base.id,
6293 connector->base.name);
6294
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006295 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006296 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006297 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006298
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006299 I915_STATE_WARN(!crtc,
6300 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006301
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006302 if (!crtc)
6303 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006304
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006305 I915_STATE_WARN(!crtc->state->active,
6306 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006307
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006308 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006309 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006310
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006311 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006312 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006313
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006314 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006315 "attached encoder crtc differs from connector crtc\n");
6316 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006317 I915_STATE_WARN(crtc && crtc->state->active,
6318 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006319 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6320 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006321 }
6322}
6323
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006324int intel_connector_init(struct intel_connector *connector)
6325{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006326 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006327
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006328 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006329 return -ENOMEM;
6330
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006331 return 0;
6332}
6333
6334struct intel_connector *intel_connector_alloc(void)
6335{
6336 struct intel_connector *connector;
6337
6338 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6339 if (!connector)
6340 return NULL;
6341
6342 if (intel_connector_init(connector) < 0) {
6343 kfree(connector);
6344 return NULL;
6345 }
6346
6347 return connector;
6348}
6349
Daniel Vetterf0947c32012-07-02 13:10:34 +02006350/* Simple connector->get_hw_state implementation for encoders that support only
6351 * one connector and no cloning and hence the encoder state determines the state
6352 * of the connector. */
6353bool intel_connector_get_hw_state(struct intel_connector *connector)
6354{
Daniel Vetter24929352012-07-02 20:28:59 +02006355 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006356 struct intel_encoder *encoder = connector->encoder;
6357
6358 return encoder->get_hw_state(encoder, &pipe);
6359}
6360
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006361static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006362{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006363 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6364 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006365
6366 return 0;
6367}
6368
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006369static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006370 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006371{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006372 struct drm_atomic_state *state = pipe_config->base.state;
6373 struct intel_crtc *other_crtc;
6374 struct intel_crtc_state *other_crtc_state;
6375
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006376 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6377 pipe_name(pipe), pipe_config->fdi_lanes);
6378 if (pipe_config->fdi_lanes > 4) {
6379 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6380 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006381 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006382 }
6383
Paulo Zanonibafb6552013-11-02 21:07:44 -07006384 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006385 if (pipe_config->fdi_lanes > 2) {
6386 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6387 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006388 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006389 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006390 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006391 }
6392 }
6393
6394 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006395 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006396
6397 /* Ivybridge 3 pipe is really complicated */
6398 switch (pipe) {
6399 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006400 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006401 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006402 if (pipe_config->fdi_lanes <= 2)
6403 return 0;
6404
6405 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6406 other_crtc_state =
6407 intel_atomic_get_crtc_state(state, other_crtc);
6408 if (IS_ERR(other_crtc_state))
6409 return PTR_ERR(other_crtc_state);
6410
6411 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006412 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6413 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006414 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006415 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006416 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006417 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006418 if (pipe_config->fdi_lanes > 2) {
6419 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6420 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006421 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006422 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006423
6424 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6425 other_crtc_state =
6426 intel_atomic_get_crtc_state(state, other_crtc);
6427 if (IS_ERR(other_crtc_state))
6428 return PTR_ERR(other_crtc_state);
6429
6430 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006431 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006432 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006433 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006434 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006435 default:
6436 BUG();
6437 }
6438}
6439
Daniel Vettere29c22c2013-02-21 00:00:16 +01006440#define RETRY 1
6441static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006442 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006443{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006444 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006445 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006446 int lane, link_bw, fdi_dotclock, ret;
6447 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006448
Daniel Vettere29c22c2013-02-21 00:00:16 +01006449retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006450 /* FDI is a binary signal running at ~2.7GHz, encoding
6451 * each output octet as 10 bits. The actual frequency
6452 * is stored as a divider into a 100MHz clock, and the
6453 * mode pixel clock is stored in units of 1KHz.
6454 * Hence the bw of each lane in terms of the mode signal
6455 * is:
6456 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006457 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006458
Damien Lespiau241bfc32013-09-25 16:45:37 +01006459 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006460
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006461 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006462 pipe_config->pipe_bpp);
6463
6464 pipe_config->fdi_lanes = lane;
6465
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006466 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006467 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006468
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006469 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006470 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006471 pipe_config->pipe_bpp -= 2*3;
6472 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6473 pipe_config->pipe_bpp);
6474 needs_recompute = true;
6475 pipe_config->bw_constrained = true;
6476
6477 goto retry;
6478 }
6479
6480 if (needs_recompute)
6481 return RETRY;
6482
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006483 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006484}
6485
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006486static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6487 struct intel_crtc_state *pipe_config)
6488{
6489 if (pipe_config->pipe_bpp > 24)
6490 return false;
6491
6492 /* HSW can handle pixel rate up to cdclk? */
6493 if (IS_HASWELL(dev_priv->dev))
6494 return true;
6495
6496 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006497 * We compare against max which means we must take
6498 * the increased cdclk requirement into account when
6499 * calculating the new cdclk.
6500 *
6501 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006502 */
6503 return ilk_pipe_pixel_rate(pipe_config) <=
6504 dev_priv->max_cdclk_freq * 95 / 100;
6505}
6506
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006507static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006508 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006509{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006510 struct drm_device *dev = crtc->base.dev;
6511 struct drm_i915_private *dev_priv = dev->dev_private;
6512
Jani Nikulad330a952014-01-21 11:24:25 +02006513 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006514 hsw_crtc_supports_ips(crtc) &&
6515 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006516}
6517
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006518static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6519{
6520 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6521
6522 /* GDG double wide on either pipe, otherwise pipe A only */
6523 return INTEL_INFO(dev_priv)->gen < 4 &&
6524 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6525}
6526
Daniel Vettera43f6e02013-06-07 23:10:32 +02006527static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006528 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006529{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006530 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006531 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006532 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006533
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006534 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006535 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006536 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006537
6538 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006539 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006540 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006541 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006542 if (intel_crtc_supports_double_wide(crtc) &&
6543 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006544 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006545 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006546 }
6547
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006548 if (adjusted_mode->crtc_clock > clock_limit) {
6549 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6550 adjusted_mode->crtc_clock, clock_limit,
6551 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006552 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006553 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006554 }
Chris Wilson89749352010-09-12 18:25:19 +01006555
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006556 /*
6557 * Pipe horizontal size must be even in:
6558 * - DVO ganged mode
6559 * - LVDS dual channel mode
6560 * - Double wide pipe
6561 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006562 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006563 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6564 pipe_config->pipe_src_w &= ~1;
6565
Damien Lespiau8693a822013-05-03 18:48:11 +01006566 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6567 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006568 */
6569 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006570 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006571 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006572
Damien Lespiauf5adf942013-06-24 18:29:34 +01006573 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006574 hsw_compute_ips_config(crtc, pipe_config);
6575
Daniel Vetter877d48d2013-04-19 11:24:43 +02006576 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006577 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006578
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006579 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006580}
6581
Ville Syrjälä1652d192015-03-31 14:12:01 +03006582static int skylake_get_display_clock_speed(struct drm_device *dev)
6583{
6584 struct drm_i915_private *dev_priv = to_i915(dev);
6585 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6586 uint32_t cdctl = I915_READ(CDCLK_CTL);
6587 uint32_t linkrate;
6588
Damien Lespiau414355a2015-06-04 18:21:31 +01006589 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006590 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006591
6592 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6593 return 540000;
6594
6595 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006596 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006597
Damien Lespiau71cd8422015-04-30 16:39:17 +01006598 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6599 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006600 /* vco 8640 */
6601 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6602 case CDCLK_FREQ_450_432:
6603 return 432000;
6604 case CDCLK_FREQ_337_308:
6605 return 308570;
6606 case CDCLK_FREQ_675_617:
6607 return 617140;
6608 default:
6609 WARN(1, "Unknown cd freq selection\n");
6610 }
6611 } else {
6612 /* vco 8100 */
6613 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6614 case CDCLK_FREQ_450_432:
6615 return 450000;
6616 case CDCLK_FREQ_337_308:
6617 return 337500;
6618 case CDCLK_FREQ_675_617:
6619 return 675000;
6620 default:
6621 WARN(1, "Unknown cd freq selection\n");
6622 }
6623 }
6624
6625 /* error case, do as if DPLL0 isn't enabled */
6626 return 24000;
6627}
6628
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006629static int broxton_get_display_clock_speed(struct drm_device *dev)
6630{
6631 struct drm_i915_private *dev_priv = to_i915(dev);
6632 uint32_t cdctl = I915_READ(CDCLK_CTL);
6633 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6634 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6635 int cdclk;
6636
6637 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6638 return 19200;
6639
6640 cdclk = 19200 * pll_ratio / 2;
6641
6642 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6643 case BXT_CDCLK_CD2X_DIV_SEL_1:
6644 return cdclk; /* 576MHz or 624MHz */
6645 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6646 return cdclk * 2 / 3; /* 384MHz */
6647 case BXT_CDCLK_CD2X_DIV_SEL_2:
6648 return cdclk / 2; /* 288MHz */
6649 case BXT_CDCLK_CD2X_DIV_SEL_4:
6650 return cdclk / 4; /* 144MHz */
6651 }
6652
6653 /* error case, do as if DE PLL isn't enabled */
6654 return 19200;
6655}
6656
Ville Syrjälä1652d192015-03-31 14:12:01 +03006657static int broadwell_get_display_clock_speed(struct drm_device *dev)
6658{
6659 struct drm_i915_private *dev_priv = dev->dev_private;
6660 uint32_t lcpll = I915_READ(LCPLL_CTL);
6661 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6662
6663 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6664 return 800000;
6665 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6666 return 450000;
6667 else if (freq == LCPLL_CLK_FREQ_450)
6668 return 450000;
6669 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6670 return 540000;
6671 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6672 return 337500;
6673 else
6674 return 675000;
6675}
6676
6677static int haswell_get_display_clock_speed(struct drm_device *dev)
6678{
6679 struct drm_i915_private *dev_priv = dev->dev_private;
6680 uint32_t lcpll = I915_READ(LCPLL_CTL);
6681 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6682
6683 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6684 return 800000;
6685 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6686 return 450000;
6687 else if (freq == LCPLL_CLK_FREQ_450)
6688 return 450000;
6689 else if (IS_HSW_ULT(dev))
6690 return 337500;
6691 else
6692 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006693}
6694
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006695static int valleyview_get_display_clock_speed(struct drm_device *dev)
6696{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006697 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6698 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006699}
6700
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006701static int ilk_get_display_clock_speed(struct drm_device *dev)
6702{
6703 return 450000;
6704}
6705
Jesse Barnese70236a2009-09-21 10:42:27 -07006706static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006707{
Jesse Barnese70236a2009-09-21 10:42:27 -07006708 return 400000;
6709}
Jesse Barnes79e53942008-11-07 14:24:08 -08006710
Jesse Barnese70236a2009-09-21 10:42:27 -07006711static int i915_get_display_clock_speed(struct drm_device *dev)
6712{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006713 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006714}
Jesse Barnes79e53942008-11-07 14:24:08 -08006715
Jesse Barnese70236a2009-09-21 10:42:27 -07006716static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6717{
6718 return 200000;
6719}
Jesse Barnes79e53942008-11-07 14:24:08 -08006720
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006721static int pnv_get_display_clock_speed(struct drm_device *dev)
6722{
6723 u16 gcfgc = 0;
6724
6725 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6726
6727 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6728 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006729 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006730 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006731 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006732 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006733 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006734 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6735 return 200000;
6736 default:
6737 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6738 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006739 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006740 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006741 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006742 }
6743}
6744
Jesse Barnese70236a2009-09-21 10:42:27 -07006745static int i915gm_get_display_clock_speed(struct drm_device *dev)
6746{
6747 u16 gcfgc = 0;
6748
6749 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6750
6751 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006752 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006753 else {
6754 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6755 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006756 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006757 default:
6758 case GC_DISPLAY_CLOCK_190_200_MHZ:
6759 return 190000;
6760 }
6761 }
6762}
Jesse Barnes79e53942008-11-07 14:24:08 -08006763
Jesse Barnese70236a2009-09-21 10:42:27 -07006764static int i865_get_display_clock_speed(struct drm_device *dev)
6765{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006766 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006767}
6768
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006769static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006770{
6771 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006772
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006773 /*
6774 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6775 * encoding is different :(
6776 * FIXME is this the right way to detect 852GM/852GMV?
6777 */
6778 if (dev->pdev->revision == 0x1)
6779 return 133333;
6780
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006781 pci_bus_read_config_word(dev->pdev->bus,
6782 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6783
Jesse Barnese70236a2009-09-21 10:42:27 -07006784 /* Assume that the hardware is in the high speed state. This
6785 * should be the default.
6786 */
6787 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6788 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006789 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006790 case GC_CLOCK_100_200:
6791 return 200000;
6792 case GC_CLOCK_166_250:
6793 return 250000;
6794 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006795 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006796 case GC_CLOCK_133_266:
6797 case GC_CLOCK_133_266_2:
6798 case GC_CLOCK_166_266:
6799 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006800 }
6801
6802 /* Shouldn't happen */
6803 return 0;
6804}
6805
6806static int i830_get_display_clock_speed(struct drm_device *dev)
6807{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006808 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006809}
6810
Ville Syrjälä34edce22015-05-22 11:22:33 +03006811static unsigned int intel_hpll_vco(struct drm_device *dev)
6812{
6813 struct drm_i915_private *dev_priv = dev->dev_private;
6814 static const unsigned int blb_vco[8] = {
6815 [0] = 3200000,
6816 [1] = 4000000,
6817 [2] = 5333333,
6818 [3] = 4800000,
6819 [4] = 6400000,
6820 };
6821 static const unsigned int pnv_vco[8] = {
6822 [0] = 3200000,
6823 [1] = 4000000,
6824 [2] = 5333333,
6825 [3] = 4800000,
6826 [4] = 2666667,
6827 };
6828 static const unsigned int cl_vco[8] = {
6829 [0] = 3200000,
6830 [1] = 4000000,
6831 [2] = 5333333,
6832 [3] = 6400000,
6833 [4] = 3333333,
6834 [5] = 3566667,
6835 [6] = 4266667,
6836 };
6837 static const unsigned int elk_vco[8] = {
6838 [0] = 3200000,
6839 [1] = 4000000,
6840 [2] = 5333333,
6841 [3] = 4800000,
6842 };
6843 static const unsigned int ctg_vco[8] = {
6844 [0] = 3200000,
6845 [1] = 4000000,
6846 [2] = 5333333,
6847 [3] = 6400000,
6848 [4] = 2666667,
6849 [5] = 4266667,
6850 };
6851 const unsigned int *vco_table;
6852 unsigned int vco;
6853 uint8_t tmp = 0;
6854
6855 /* FIXME other chipsets? */
6856 if (IS_GM45(dev))
6857 vco_table = ctg_vco;
6858 else if (IS_G4X(dev))
6859 vco_table = elk_vco;
6860 else if (IS_CRESTLINE(dev))
6861 vco_table = cl_vco;
6862 else if (IS_PINEVIEW(dev))
6863 vco_table = pnv_vco;
6864 else if (IS_G33(dev))
6865 vco_table = blb_vco;
6866 else
6867 return 0;
6868
6869 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6870
6871 vco = vco_table[tmp & 0x7];
6872 if (vco == 0)
6873 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6874 else
6875 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6876
6877 return vco;
6878}
6879
6880static int gm45_get_display_clock_speed(struct drm_device *dev)
6881{
6882 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6883 uint16_t tmp = 0;
6884
6885 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6886
6887 cdclk_sel = (tmp >> 12) & 0x1;
6888
6889 switch (vco) {
6890 case 2666667:
6891 case 4000000:
6892 case 5333333:
6893 return cdclk_sel ? 333333 : 222222;
6894 case 3200000:
6895 return cdclk_sel ? 320000 : 228571;
6896 default:
6897 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6898 return 222222;
6899 }
6900}
6901
6902static int i965gm_get_display_clock_speed(struct drm_device *dev)
6903{
6904 static const uint8_t div_3200[] = { 16, 10, 8 };
6905 static const uint8_t div_4000[] = { 20, 12, 10 };
6906 static const uint8_t div_5333[] = { 24, 16, 14 };
6907 const uint8_t *div_table;
6908 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6909 uint16_t tmp = 0;
6910
6911 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6912
6913 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6914
6915 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6916 goto fail;
6917
6918 switch (vco) {
6919 case 3200000:
6920 div_table = div_3200;
6921 break;
6922 case 4000000:
6923 div_table = div_4000;
6924 break;
6925 case 5333333:
6926 div_table = div_5333;
6927 break;
6928 default:
6929 goto fail;
6930 }
6931
6932 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6933
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006934fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006935 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6936 return 200000;
6937}
6938
6939static int g33_get_display_clock_speed(struct drm_device *dev)
6940{
6941 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6942 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6943 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6944 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6945 const uint8_t *div_table;
6946 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6947 uint16_t tmp = 0;
6948
6949 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6950
6951 cdclk_sel = (tmp >> 4) & 0x7;
6952
6953 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6954 goto fail;
6955
6956 switch (vco) {
6957 case 3200000:
6958 div_table = div_3200;
6959 break;
6960 case 4000000:
6961 div_table = div_4000;
6962 break;
6963 case 4800000:
6964 div_table = div_4800;
6965 break;
6966 case 5333333:
6967 div_table = div_5333;
6968 break;
6969 default:
6970 goto fail;
6971 }
6972
6973 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6974
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006975fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006976 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6977 return 190476;
6978}
6979
Zhenyu Wang2c072452009-06-05 15:38:42 +08006980static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006981intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006982{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006983 while (*num > DATA_LINK_M_N_MASK ||
6984 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006985 *num >>= 1;
6986 *den >>= 1;
6987 }
6988}
6989
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006990static void compute_m_n(unsigned int m, unsigned int n,
6991 uint32_t *ret_m, uint32_t *ret_n)
6992{
6993 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6994 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6995 intel_reduce_m_n_ratio(ret_m, ret_n);
6996}
6997
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006998void
6999intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7000 int pixel_clock, int link_clock,
7001 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007002{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007003 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007004
7005 compute_m_n(bits_per_pixel * pixel_clock,
7006 link_clock * nlanes * 8,
7007 &m_n->gmch_m, &m_n->gmch_n);
7008
7009 compute_m_n(pixel_clock, link_clock,
7010 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007011}
7012
Chris Wilsona7615032011-01-12 17:04:08 +00007013static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7014{
Jani Nikulad330a952014-01-21 11:24:25 +02007015 if (i915.panel_use_ssc >= 0)
7016 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007017 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007018 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007019}
7020
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007021static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007022{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007023 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007024}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007025
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007026static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7027{
7028 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007029}
7030
Daniel Vetterf47709a2013-03-28 10:42:02 +01007031static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007032 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007033 intel_clock_t *reduced_clock)
7034{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007035 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007036 u32 fp, fp2 = 0;
7037
7038 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007039 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007040 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007041 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007042 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007043 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007044 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007045 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007046 }
7047
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007048 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007049
Daniel Vetterf47709a2013-03-28 10:42:02 +01007050 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007051 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007052 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007053 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007054 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007055 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007056 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007057 }
7058}
7059
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007060static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7061 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007062{
7063 u32 reg_val;
7064
7065 /*
7066 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7067 * and set it to a reasonable value instead.
7068 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007069 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007070 reg_val &= 0xffffff00;
7071 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007072 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007073
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007074 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007075 reg_val &= 0x8cffffff;
7076 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007077 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007078
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007079 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007080 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007081 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007082
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007083 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007084 reg_val &= 0x00ffffff;
7085 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007086 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007087}
7088
Daniel Vetterb5518422013-05-03 11:49:48 +02007089static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7090 struct intel_link_m_n *m_n)
7091{
7092 struct drm_device *dev = crtc->base.dev;
7093 struct drm_i915_private *dev_priv = dev->dev_private;
7094 int pipe = crtc->pipe;
7095
Daniel Vettere3b95f12013-05-03 11:49:49 +02007096 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7097 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7098 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7099 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007100}
7101
7102static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007103 struct intel_link_m_n *m_n,
7104 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007105{
7106 struct drm_device *dev = crtc->base.dev;
7107 struct drm_i915_private *dev_priv = dev->dev_private;
7108 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007109 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007110
7111 if (INTEL_INFO(dev)->gen >= 5) {
7112 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7113 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7114 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7115 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007116 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7117 * for gen < 8) and if DRRS is supported (to make sure the
7118 * registers are not unnecessarily accessed).
7119 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307120 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007121 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007122 I915_WRITE(PIPE_DATA_M2(transcoder),
7123 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7124 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7125 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7126 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7127 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007128 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007129 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7130 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7131 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7132 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007133 }
7134}
7135
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307136void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007137{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307138 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7139
7140 if (m_n == M1_N1) {
7141 dp_m_n = &crtc->config->dp_m_n;
7142 dp_m2_n2 = &crtc->config->dp_m2_n2;
7143 } else if (m_n == M2_N2) {
7144
7145 /*
7146 * M2_N2 registers are not supported. Hence m2_n2 divider value
7147 * needs to be programmed into M1_N1.
7148 */
7149 dp_m_n = &crtc->config->dp_m2_n2;
7150 } else {
7151 DRM_ERROR("Unsupported divider value\n");
7152 return;
7153 }
7154
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007155 if (crtc->config->has_pch_encoder)
7156 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007157 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307158 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007159}
7160
Daniel Vetter251ac862015-06-18 10:30:24 +02007161static void vlv_compute_dpll(struct intel_crtc *crtc,
7162 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007163{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007164 u32 dpll, dpll_md;
7165
7166 /*
7167 * Enable DPIO clock input. We should never disable the reference
7168 * clock for pipe B, since VGA hotplug / manual detection depends
7169 * on it.
7170 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007171 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7172 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007173 /* We should never disable this, set it here for state tracking */
7174 if (crtc->pipe == PIPE_B)
7175 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7176 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007177 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007178
Ville Syrjäläd288f652014-10-28 13:20:22 +02007179 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007180 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007181 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007182}
7183
Ville Syrjäläd288f652014-10-28 13:20:22 +02007184static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007185 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007186{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007187 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007188 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007189 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007190 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007191 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007192 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007193
Ville Syrjäläa5805162015-05-26 20:42:30 +03007194 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007195
Ville Syrjäläd288f652014-10-28 13:20:22 +02007196 bestn = pipe_config->dpll.n;
7197 bestm1 = pipe_config->dpll.m1;
7198 bestm2 = pipe_config->dpll.m2;
7199 bestp1 = pipe_config->dpll.p1;
7200 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007201
Jesse Barnes89b667f2013-04-18 14:51:36 -07007202 /* See eDP HDMI DPIO driver vbios notes doc */
7203
7204 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007205 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007206 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007207
7208 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007209 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007210
7211 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007212 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007213 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007214 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007215
7216 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007217 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007218
7219 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007220 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7221 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7222 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007223 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007224
7225 /*
7226 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7227 * but we don't support that).
7228 * Note: don't use the DAC post divider as it seems unstable.
7229 */
7230 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007231 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007232
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007233 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007234 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007235
Jesse Barnes89b667f2013-04-18 14:51:36 -07007236 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007237 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007238 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7239 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007240 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007241 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007242 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007243 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007244 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007245
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007246 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007247 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007248 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007249 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007250 0x0df40000);
7251 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007252 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007253 0x0df70000);
7254 } else { /* HDMI or VGA */
7255 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007256 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007257 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007258 0x0df70000);
7259 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007260 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007261 0x0df40000);
7262 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007263
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007264 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007265 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007266 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7267 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007268 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007269 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007270
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007271 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007272 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007273}
7274
Daniel Vetter251ac862015-06-18 10:30:24 +02007275static void chv_compute_dpll(struct intel_crtc *crtc,
7276 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007277{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007278 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7279 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007280 DPLL_VCO_ENABLE;
7281 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007282 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007283
Ville Syrjäläd288f652014-10-28 13:20:22 +02007284 pipe_config->dpll_hw_state.dpll_md =
7285 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007286}
7287
Ville Syrjäläd288f652014-10-28 13:20:22 +02007288static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007289 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007290{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007291 struct drm_device *dev = crtc->base.dev;
7292 struct drm_i915_private *dev_priv = dev->dev_private;
7293 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02007294 i915_reg_t dpll_reg = DPLL(crtc->pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007295 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307296 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007297 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307298 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307299 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007300
Ville Syrjäläd288f652014-10-28 13:20:22 +02007301 bestn = pipe_config->dpll.n;
7302 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7303 bestm1 = pipe_config->dpll.m1;
7304 bestm2 = pipe_config->dpll.m2 >> 22;
7305 bestp1 = pipe_config->dpll.p1;
7306 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307307 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307308 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307309 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007310
7311 /*
7312 * Enable Refclk and SSC
7313 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007314 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007315 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007316
Ville Syrjäläa5805162015-05-26 20:42:30 +03007317 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007318
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007319 /* p1 and p2 divider */
7320 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7321 5 << DPIO_CHV_S1_DIV_SHIFT |
7322 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7323 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7324 1 << DPIO_CHV_K_DIV_SHIFT);
7325
7326 /* Feedback post-divider - m2 */
7327 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7328
7329 /* Feedback refclk divider - n and m1 */
7330 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7331 DPIO_CHV_M1_DIV_BY_2 |
7332 1 << DPIO_CHV_N_DIV_SHIFT);
7333
7334 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007335 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007336
7337 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307338 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7339 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7340 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7341 if (bestm2_frac)
7342 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7343 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007344
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307345 /* Program digital lock detect threshold */
7346 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7347 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7348 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7349 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7350 if (!bestm2_frac)
7351 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7352 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7353
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007354 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307355 if (vco == 5400000) {
7356 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7357 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7358 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7359 tribuf_calcntr = 0x9;
7360 } else if (vco <= 6200000) {
7361 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7362 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7363 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7364 tribuf_calcntr = 0x9;
7365 } else if (vco <= 6480000) {
7366 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7367 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7368 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7369 tribuf_calcntr = 0x8;
7370 } else {
7371 /* Not supported. Apply the same limits as in the max case */
7372 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7373 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7374 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7375 tribuf_calcntr = 0;
7376 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007377 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7378
Ville Syrjälä968040b2015-03-11 22:52:08 +02007379 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307380 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7381 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7382 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7383
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007384 /* AFC Recal */
7385 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7386 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7387 DPIO_AFC_RECAL);
7388
Ville Syrjäläa5805162015-05-26 20:42:30 +03007389 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007390}
7391
Ville Syrjäläd288f652014-10-28 13:20:22 +02007392/**
7393 * vlv_force_pll_on - forcibly enable just the PLL
7394 * @dev_priv: i915 private structure
7395 * @pipe: pipe PLL to enable
7396 * @dpll: PLL configuration
7397 *
7398 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7399 * in cases where we need the PLL enabled even when @pipe is not going to
7400 * be enabled.
7401 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007402int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7403 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007404{
7405 struct intel_crtc *crtc =
7406 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007407 struct intel_crtc_state *pipe_config;
7408
7409 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7410 if (!pipe_config)
7411 return -ENOMEM;
7412
7413 pipe_config->base.crtc = &crtc->base;
7414 pipe_config->pixel_multiplier = 1;
7415 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007416
7417 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007418 chv_compute_dpll(crtc, pipe_config);
7419 chv_prepare_pll(crtc, pipe_config);
7420 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007421 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007422 vlv_compute_dpll(crtc, pipe_config);
7423 vlv_prepare_pll(crtc, pipe_config);
7424 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007425 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007426
7427 kfree(pipe_config);
7428
7429 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007430}
7431
7432/**
7433 * vlv_force_pll_off - forcibly disable just the PLL
7434 * @dev_priv: i915 private structure
7435 * @pipe: pipe PLL to disable
7436 *
7437 * Disable the PLL for @pipe. To be used in cases where we need
7438 * the PLL enabled even when @pipe is not going to be enabled.
7439 */
7440void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7441{
7442 if (IS_CHERRYVIEW(dev))
7443 chv_disable_pll(to_i915(dev), pipe);
7444 else
7445 vlv_disable_pll(to_i915(dev), pipe);
7446}
7447
Daniel Vetter251ac862015-06-18 10:30:24 +02007448static void i9xx_compute_dpll(struct intel_crtc *crtc,
7449 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007450 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007451{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007452 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007453 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007454 u32 dpll;
7455 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007456 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007457
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007458 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307459
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007460 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7461 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007462
7463 dpll = DPLL_VGA_MODE_DIS;
7464
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007465 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007466 dpll |= DPLLB_MODE_LVDS;
7467 else
7468 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007469
Daniel Vetteref1b4602013-06-01 17:17:04 +02007470 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007471 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007472 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007473 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007474
7475 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007476 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007477
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007478 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007479 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007480
7481 /* compute bitmask from p1 value */
7482 if (IS_PINEVIEW(dev))
7483 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7484 else {
7485 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7486 if (IS_G4X(dev) && reduced_clock)
7487 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7488 }
7489 switch (clock->p2) {
7490 case 5:
7491 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7492 break;
7493 case 7:
7494 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7495 break;
7496 case 10:
7497 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7498 break;
7499 case 14:
7500 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7501 break;
7502 }
7503 if (INTEL_INFO(dev)->gen >= 4)
7504 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7505
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007506 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007507 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007508 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007509 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007510 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7511 else
7512 dpll |= PLL_REF_INPUT_DREFCLK;
7513
7514 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007515 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007516
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007517 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007518 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007519 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007520 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007521 }
7522}
7523
Daniel Vetter251ac862015-06-18 10:30:24 +02007524static void i8xx_compute_dpll(struct intel_crtc *crtc,
7525 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007526 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007527{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007528 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007529 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007530 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007531 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007532
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007533 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307534
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007535 dpll = DPLL_VGA_MODE_DIS;
7536
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007537 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007538 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7539 } else {
7540 if (clock->p1 == 2)
7541 dpll |= PLL_P1_DIVIDE_BY_TWO;
7542 else
7543 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7544 if (clock->p2 == 4)
7545 dpll |= PLL_P2_DIVIDE_BY_4;
7546 }
7547
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007548 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007549 dpll |= DPLL_DVO_2X_MODE;
7550
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007551 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007552 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007553 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7554 else
7555 dpll |= PLL_REF_INPUT_DREFCLK;
7556
7557 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007558 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007559}
7560
Daniel Vetter8a654f32013-06-01 17:16:22 +02007561static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007562{
7563 struct drm_device *dev = intel_crtc->base.dev;
7564 struct drm_i915_private *dev_priv = dev->dev_private;
7565 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007566 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007567 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007568 uint32_t crtc_vtotal, crtc_vblank_end;
7569 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007570
7571 /* We need to be careful not to changed the adjusted mode, for otherwise
7572 * the hw state checker will get angry at the mismatch. */
7573 crtc_vtotal = adjusted_mode->crtc_vtotal;
7574 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007575
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007576 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007577 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007578 crtc_vtotal -= 1;
7579 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007580
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007581 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007582 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7583 else
7584 vsyncshift = adjusted_mode->crtc_hsync_start -
7585 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007586 if (vsyncshift < 0)
7587 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007588 }
7589
7590 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007591 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007592
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007593 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007594 (adjusted_mode->crtc_hdisplay - 1) |
7595 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007596 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007597 (adjusted_mode->crtc_hblank_start - 1) |
7598 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007599 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007600 (adjusted_mode->crtc_hsync_start - 1) |
7601 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7602
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007603 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007604 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007605 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007606 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007607 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007608 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007609 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007610 (adjusted_mode->crtc_vsync_start - 1) |
7611 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7612
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007613 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7614 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7615 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7616 * bits. */
7617 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7618 (pipe == PIPE_B || pipe == PIPE_C))
7619 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7620
Jani Nikulabc58be62016-03-18 17:05:39 +02007621}
7622
7623static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7624{
7625 struct drm_device *dev = intel_crtc->base.dev;
7626 struct drm_i915_private *dev_priv = dev->dev_private;
7627 enum pipe pipe = intel_crtc->pipe;
7628
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007629 /* pipesrc controls the size that is scaled from, which should
7630 * always be the user's requested size.
7631 */
7632 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007633 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7634 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007635}
7636
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007637static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007638 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007639{
7640 struct drm_device *dev = crtc->base.dev;
7641 struct drm_i915_private *dev_priv = dev->dev_private;
7642 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7643 uint32_t tmp;
7644
7645 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007646 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7647 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007648 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007649 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7650 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007651 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007652 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7653 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007654
7655 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007656 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7657 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007658 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007659 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7660 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007661 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007662 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7663 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007664
7665 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007666 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7667 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7668 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007669 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007670}
7671
7672static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7673 struct intel_crtc_state *pipe_config)
7674{
7675 struct drm_device *dev = crtc->base.dev;
7676 struct drm_i915_private *dev_priv = dev->dev_private;
7677 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007678
7679 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007680 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7681 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7682
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007683 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7684 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007685}
7686
Daniel Vetterf6a83282014-02-11 15:28:57 -08007687void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007688 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007689{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007690 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7691 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7692 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7693 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007694
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007695 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7696 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7697 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7698 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007699
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007700 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007701 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007702
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007703 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7704 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007705
7706 mode->hsync = drm_mode_hsync(mode);
7707 mode->vrefresh = drm_mode_vrefresh(mode);
7708 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007709}
7710
Daniel Vetter84b046f2013-02-19 18:48:54 +01007711static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7712{
7713 struct drm_device *dev = intel_crtc->base.dev;
7714 struct drm_i915_private *dev_priv = dev->dev_private;
7715 uint32_t pipeconf;
7716
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007717 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007718
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007719 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7720 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7721 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007722
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007723 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007724 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007725
Daniel Vetterff9ce462013-04-24 14:57:17 +02007726 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007727 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007728 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007729 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007730 pipeconf |= PIPECONF_DITHER_EN |
7731 PIPECONF_DITHER_TYPE_SP;
7732
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007733 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007734 case 18:
7735 pipeconf |= PIPECONF_6BPC;
7736 break;
7737 case 24:
7738 pipeconf |= PIPECONF_8BPC;
7739 break;
7740 case 30:
7741 pipeconf |= PIPECONF_10BPC;
7742 break;
7743 default:
7744 /* Case prevented by intel_choose_pipe_bpp_dither. */
7745 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007746 }
7747 }
7748
7749 if (HAS_PIPE_CXSR(dev)) {
7750 if (intel_crtc->lowfreq_avail) {
7751 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7752 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7753 } else {
7754 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007755 }
7756 }
7757
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007758 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007759 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007760 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007761 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7762 else
7763 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7764 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007765 pipeconf |= PIPECONF_PROGRESSIVE;
7766
Wayne Boyer666a4532015-12-09 12:29:35 -08007767 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7768 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007769 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007770
Daniel Vetter84b046f2013-02-19 18:48:54 +01007771 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7772 POSTING_READ(PIPECONF(intel_crtc->pipe));
7773}
7774
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007775static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7776 struct intel_crtc_state *crtc_state)
7777{
7778 struct drm_device *dev = crtc->base.dev;
7779 struct drm_i915_private *dev_priv = dev->dev_private;
7780 const intel_limit_t *limit;
7781 int refclk = 48000;
7782
7783 memset(&crtc_state->dpll_hw_state, 0,
7784 sizeof(crtc_state->dpll_hw_state));
7785
7786 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7787 if (intel_panel_use_ssc(dev_priv)) {
7788 refclk = dev_priv->vbt.lvds_ssc_freq;
7789 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7790 }
7791
7792 limit = &intel_limits_i8xx_lvds;
7793 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7794 limit = &intel_limits_i8xx_dvo;
7795 } else {
7796 limit = &intel_limits_i8xx_dac;
7797 }
7798
7799 if (!crtc_state->clock_set &&
7800 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7801 refclk, NULL, &crtc_state->dpll)) {
7802 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7803 return -EINVAL;
7804 }
7805
7806 i8xx_compute_dpll(crtc, crtc_state, NULL);
7807
7808 return 0;
7809}
7810
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007811static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7812 struct intel_crtc_state *crtc_state)
7813{
7814 struct drm_device *dev = crtc->base.dev;
7815 struct drm_i915_private *dev_priv = dev->dev_private;
7816 const intel_limit_t *limit;
7817 int refclk = 96000;
7818
7819 memset(&crtc_state->dpll_hw_state, 0,
7820 sizeof(crtc_state->dpll_hw_state));
7821
7822 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7823 if (intel_panel_use_ssc(dev_priv)) {
7824 refclk = dev_priv->vbt.lvds_ssc_freq;
7825 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7826 }
7827
7828 if (intel_is_dual_link_lvds(dev))
7829 limit = &intel_limits_g4x_dual_channel_lvds;
7830 else
7831 limit = &intel_limits_g4x_single_channel_lvds;
7832 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7833 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7834 limit = &intel_limits_g4x_hdmi;
7835 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7836 limit = &intel_limits_g4x_sdvo;
7837 } else {
7838 /* The option is for other outputs */
7839 limit = &intel_limits_i9xx_sdvo;
7840 }
7841
7842 if (!crtc_state->clock_set &&
7843 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7844 refclk, NULL, &crtc_state->dpll)) {
7845 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7846 return -EINVAL;
7847 }
7848
7849 i9xx_compute_dpll(crtc, crtc_state, NULL);
7850
7851 return 0;
7852}
7853
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007854static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7855 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007856{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007857 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007858 struct drm_i915_private *dev_priv = dev->dev_private;
Ma Lingd4906092009-03-18 20:13:27 +08007859 const intel_limit_t *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007860 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007861
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007862 memset(&crtc_state->dpll_hw_state, 0,
7863 sizeof(crtc_state->dpll_hw_state));
7864
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007865 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7866 if (intel_panel_use_ssc(dev_priv)) {
7867 refclk = dev_priv->vbt.lvds_ssc_freq;
7868 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7869 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007870
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007871 limit = &intel_limits_pineview_lvds;
7872 } else {
7873 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007874 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007875
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007876 if (!crtc_state->clock_set &&
7877 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7878 refclk, NULL, &crtc_state->dpll)) {
7879 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7880 return -EINVAL;
7881 }
7882
7883 i9xx_compute_dpll(crtc, crtc_state, NULL);
7884
7885 return 0;
7886}
7887
7888static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7889 struct intel_crtc_state *crtc_state)
7890{
7891 struct drm_device *dev = crtc->base.dev;
7892 struct drm_i915_private *dev_priv = dev->dev_private;
7893 const intel_limit_t *limit;
7894 int refclk = 96000;
7895
7896 memset(&crtc_state->dpll_hw_state, 0,
7897 sizeof(crtc_state->dpll_hw_state));
7898
7899 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7900 if (intel_panel_use_ssc(dev_priv)) {
7901 refclk = dev_priv->vbt.lvds_ssc_freq;
7902 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007903 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007904
7905 limit = &intel_limits_i9xx_lvds;
7906 } else {
7907 limit = &intel_limits_i9xx_sdvo;
7908 }
7909
7910 if (!crtc_state->clock_set &&
7911 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7912 refclk, NULL, &crtc_state->dpll)) {
7913 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7914 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007915 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007916
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007917 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007918
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007919 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007920}
7921
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007922static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7923 struct intel_crtc_state *crtc_state)
7924{
7925 int refclk = 100000;
7926 const intel_limit_t *limit = &intel_limits_chv;
7927
7928 memset(&crtc_state->dpll_hw_state, 0,
7929 sizeof(crtc_state->dpll_hw_state));
7930
7931 if (crtc_state->has_dsi_encoder)
7932 return 0;
7933
7934 if (!crtc_state->clock_set &&
7935 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7936 refclk, NULL, &crtc_state->dpll)) {
7937 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7938 return -EINVAL;
7939 }
7940
7941 chv_compute_dpll(crtc, crtc_state);
7942
7943 return 0;
7944}
7945
7946static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7947 struct intel_crtc_state *crtc_state)
7948{
7949 int refclk = 100000;
7950 const intel_limit_t *limit = &intel_limits_vlv;
7951
7952 memset(&crtc_state->dpll_hw_state, 0,
7953 sizeof(crtc_state->dpll_hw_state));
7954
7955 if (crtc_state->has_dsi_encoder)
7956 return 0;
7957
7958 if (!crtc_state->clock_set &&
7959 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7960 refclk, NULL, &crtc_state->dpll)) {
7961 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7962 return -EINVAL;
7963 }
7964
7965 vlv_compute_dpll(crtc, crtc_state);
7966
7967 return 0;
7968}
7969
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007970static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007971 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007972{
7973 struct drm_device *dev = crtc->base.dev;
7974 struct drm_i915_private *dev_priv = dev->dev_private;
7975 uint32_t tmp;
7976
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007977 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7978 return;
7979
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007980 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007981 if (!(tmp & PFIT_ENABLE))
7982 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007983
Daniel Vetter06922822013-07-11 13:35:40 +02007984 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007985 if (INTEL_INFO(dev)->gen < 4) {
7986 if (crtc->pipe != PIPE_B)
7987 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007988 } else {
7989 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7990 return;
7991 }
7992
Daniel Vetter06922822013-07-11 13:35:40 +02007993 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007994 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7995 if (INTEL_INFO(dev)->gen < 5)
7996 pipe_config->gmch_pfit.lvds_border_bits =
7997 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7998}
7999
Jesse Barnesacbec812013-09-20 11:29:32 -07008000static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008001 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008002{
8003 struct drm_device *dev = crtc->base.dev;
8004 struct drm_i915_private *dev_priv = dev->dev_private;
8005 int pipe = pipe_config->cpu_transcoder;
8006 intel_clock_t clock;
8007 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008008 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008009
Shobhit Kumarf573de52014-07-30 20:32:37 +05308010 /* In case of MIPI DPLL will not even be used */
8011 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8012 return;
8013
Ville Syrjäläa5805162015-05-26 20:42:30 +03008014 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008015 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008016 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008017
8018 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8019 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8020 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8021 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8022 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8023
Imre Deakdccbea32015-06-22 23:35:51 +03008024 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008025}
8026
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008027static void
8028i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8029 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008030{
8031 struct drm_device *dev = crtc->base.dev;
8032 struct drm_i915_private *dev_priv = dev->dev_private;
8033 u32 val, base, offset;
8034 int pipe = crtc->pipe, plane = crtc->plane;
8035 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008036 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008037 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008038 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008039
Damien Lespiau42a7b082015-02-05 19:35:13 +00008040 val = I915_READ(DSPCNTR(plane));
8041 if (!(val & DISPLAY_PLANE_ENABLE))
8042 return;
8043
Damien Lespiaud9806c92015-01-21 14:07:19 +00008044 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008045 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008046 DRM_DEBUG_KMS("failed to alloc fb\n");
8047 return;
8048 }
8049
Damien Lespiau1b842c82015-01-21 13:50:54 +00008050 fb = &intel_fb->base;
8051
Daniel Vetter18c52472015-02-10 17:16:09 +00008052 if (INTEL_INFO(dev)->gen >= 4) {
8053 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008054 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008055 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8056 }
8057 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008058
8059 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008060 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008061 fb->pixel_format = fourcc;
8062 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008063
8064 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008065 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008066 offset = I915_READ(DSPTILEOFF(plane));
8067 else
8068 offset = I915_READ(DSPLINOFF(plane));
8069 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8070 } else {
8071 base = I915_READ(DSPADDR(plane));
8072 }
8073 plane_config->base = base;
8074
8075 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008076 fb->width = ((val >> 16) & 0xfff) + 1;
8077 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008078
8079 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008080 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008081
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008082 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008083 fb->pixel_format,
8084 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008085
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008086 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008087
Damien Lespiau2844a922015-01-20 12:51:48 +00008088 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8089 pipe_name(pipe), plane, fb->width, fb->height,
8090 fb->bits_per_pixel, base, fb->pitches[0],
8091 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008092
Damien Lespiau2d140302015-02-05 17:22:18 +00008093 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008094}
8095
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008096static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008097 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008098{
8099 struct drm_device *dev = crtc->base.dev;
8100 struct drm_i915_private *dev_priv = dev->dev_private;
8101 int pipe = pipe_config->cpu_transcoder;
8102 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8103 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008104 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008105 int refclk = 100000;
8106
Ville Syrjäläa5805162015-05-26 20:42:30 +03008107 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008108 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8109 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8110 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8111 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008112 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008113 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008114
8115 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008116 clock.m2 = (pll_dw0 & 0xff) << 22;
8117 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8118 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008119 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8120 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8121 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8122
Imre Deakdccbea32015-06-22 23:35:51 +03008123 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008124}
8125
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008126static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008127 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008128{
8129 struct drm_device *dev = crtc->base.dev;
8130 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008131 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008132 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008133 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008134
Imre Deak17290502016-02-12 18:55:11 +02008135 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8136 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008137 return false;
8138
Daniel Vettere143a212013-07-04 12:01:15 +02008139 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008140 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008141
Imre Deak17290502016-02-12 18:55:11 +02008142 ret = false;
8143
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008144 tmp = I915_READ(PIPECONF(crtc->pipe));
8145 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008146 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008147
Wayne Boyer666a4532015-12-09 12:29:35 -08008148 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008149 switch (tmp & PIPECONF_BPC_MASK) {
8150 case PIPECONF_6BPC:
8151 pipe_config->pipe_bpp = 18;
8152 break;
8153 case PIPECONF_8BPC:
8154 pipe_config->pipe_bpp = 24;
8155 break;
8156 case PIPECONF_10BPC:
8157 pipe_config->pipe_bpp = 30;
8158 break;
8159 default:
8160 break;
8161 }
8162 }
8163
Wayne Boyer666a4532015-12-09 12:29:35 -08008164 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8165 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008166 pipe_config->limited_color_range = true;
8167
Ville Syrjälä282740f2013-09-04 18:30:03 +03008168 if (INTEL_INFO(dev)->gen < 4)
8169 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8170
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008171 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008172 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008173
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008174 i9xx_get_pfit_config(crtc, pipe_config);
8175
Daniel Vetter6c49f242013-06-06 12:45:25 +02008176 if (INTEL_INFO(dev)->gen >= 4) {
8177 tmp = I915_READ(DPLL_MD(crtc->pipe));
8178 pipe_config->pixel_multiplier =
8179 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8180 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008181 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008182 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8183 tmp = I915_READ(DPLL(crtc->pipe));
8184 pipe_config->pixel_multiplier =
8185 ((tmp & SDVO_MULTIPLIER_MASK)
8186 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8187 } else {
8188 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8189 * port and will be fixed up in the encoder->get_config
8190 * function. */
8191 pipe_config->pixel_multiplier = 1;
8192 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008193 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008194 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008195 /*
8196 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8197 * on 830. Filter it out here so that we don't
8198 * report errors due to that.
8199 */
8200 if (IS_I830(dev))
8201 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8202
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008203 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8204 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008205 } else {
8206 /* Mask out read-only status bits. */
8207 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8208 DPLL_PORTC_READY_MASK |
8209 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008210 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008211
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008212 if (IS_CHERRYVIEW(dev))
8213 chv_crtc_clock_get(crtc, pipe_config);
8214 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008215 vlv_crtc_clock_get(crtc, pipe_config);
8216 else
8217 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008218
Ville Syrjälä0f646142015-08-26 19:39:18 +03008219 /*
8220 * Normally the dotclock is filled in by the encoder .get_config()
8221 * but in case the pipe is enabled w/o any ports we need a sane
8222 * default.
8223 */
8224 pipe_config->base.adjusted_mode.crtc_clock =
8225 pipe_config->port_clock / pipe_config->pixel_multiplier;
8226
Imre Deak17290502016-02-12 18:55:11 +02008227 ret = true;
8228
8229out:
8230 intel_display_power_put(dev_priv, power_domain);
8231
8232 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008233}
8234
Paulo Zanonidde86e22012-12-01 12:04:25 -02008235static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008236{
8237 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008238 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008239 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008240 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008241 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008242 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008243 bool has_ck505 = false;
8244 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008245
8246 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008247 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008248 switch (encoder->type) {
8249 case INTEL_OUTPUT_LVDS:
8250 has_panel = true;
8251 has_lvds = true;
8252 break;
8253 case INTEL_OUTPUT_EDP:
8254 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008255 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008256 has_cpu_edp = true;
8257 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008258 default:
8259 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008260 }
8261 }
8262
Keith Packard99eb6a02011-09-26 14:29:12 -07008263 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008264 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008265 can_ssc = has_ck505;
8266 } else {
8267 has_ck505 = false;
8268 can_ssc = true;
8269 }
8270
Imre Deak2de69052013-05-08 13:14:04 +03008271 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8272 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008273
8274 /* Ironlake: try to setup display ref clock before DPLL
8275 * enabling. This is only under driver's control after
8276 * PCH B stepping, previous chipset stepping should be
8277 * ignoring this setting.
8278 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008279 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008280
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008281 /* As we must carefully and slowly disable/enable each source in turn,
8282 * compute the final state we want first and check if we need to
8283 * make any changes at all.
8284 */
8285 final = val;
8286 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008287 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008288 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008289 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008290 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8291
8292 final &= ~DREF_SSC_SOURCE_MASK;
8293 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8294 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008295
Keith Packard199e5d72011-09-22 12:01:57 -07008296 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008297 final |= DREF_SSC_SOURCE_ENABLE;
8298
8299 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8300 final |= DREF_SSC1_ENABLE;
8301
8302 if (has_cpu_edp) {
8303 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8304 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8305 else
8306 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8307 } else
8308 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8309 } else {
8310 final |= DREF_SSC_SOURCE_DISABLE;
8311 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8312 }
8313
8314 if (final == val)
8315 return;
8316
8317 /* Always enable nonspread source */
8318 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8319
8320 if (has_ck505)
8321 val |= DREF_NONSPREAD_CK505_ENABLE;
8322 else
8323 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8324
8325 if (has_panel) {
8326 val &= ~DREF_SSC_SOURCE_MASK;
8327 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008328
Keith Packard199e5d72011-09-22 12:01:57 -07008329 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008330 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008331 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008332 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008333 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008334 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008335
8336 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008337 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008338 POSTING_READ(PCH_DREF_CONTROL);
8339 udelay(200);
8340
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008341 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008342
8343 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008344 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008345 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008346 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008347 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008348 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008349 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008350 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008351 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008352
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008353 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008354 POSTING_READ(PCH_DREF_CONTROL);
8355 udelay(200);
8356 } else {
8357 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8358
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008359 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008360
8361 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008362 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008363
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008364 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008365 POSTING_READ(PCH_DREF_CONTROL);
8366 udelay(200);
8367
8368 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008369 val &= ~DREF_SSC_SOURCE_MASK;
8370 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008371
8372 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008373 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008374
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008375 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008376 POSTING_READ(PCH_DREF_CONTROL);
8377 udelay(200);
8378 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008379
8380 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008381}
8382
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008383static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008384{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008385 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008386
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008387 tmp = I915_READ(SOUTH_CHICKEN2);
8388 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8389 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008390
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008391 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8392 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8393 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008394
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008395 tmp = I915_READ(SOUTH_CHICKEN2);
8396 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8397 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008398
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008399 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8400 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8401 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008402}
8403
8404/* WaMPhyProgramming:hsw */
8405static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8406{
8407 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008408
8409 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8410 tmp &= ~(0xFF << 24);
8411 tmp |= (0x12 << 24);
8412 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8413
Paulo Zanonidde86e22012-12-01 12:04:25 -02008414 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8415 tmp |= (1 << 11);
8416 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8417
8418 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8419 tmp |= (1 << 11);
8420 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8421
Paulo Zanonidde86e22012-12-01 12:04:25 -02008422 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8423 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8424 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8425
8426 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8427 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8428 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8429
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008430 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8431 tmp &= ~(7 << 13);
8432 tmp |= (5 << 13);
8433 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008434
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008435 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8436 tmp &= ~(7 << 13);
8437 tmp |= (5 << 13);
8438 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008439
8440 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8441 tmp &= ~0xFF;
8442 tmp |= 0x1C;
8443 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8444
8445 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8446 tmp &= ~0xFF;
8447 tmp |= 0x1C;
8448 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8449
8450 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8451 tmp &= ~(0xFF << 16);
8452 tmp |= (0x1C << 16);
8453 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8454
8455 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8456 tmp &= ~(0xFF << 16);
8457 tmp |= (0x1C << 16);
8458 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8459
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008460 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8461 tmp |= (1 << 27);
8462 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008463
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008464 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8465 tmp |= (1 << 27);
8466 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008467
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008468 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8469 tmp &= ~(0xF << 28);
8470 tmp |= (4 << 28);
8471 intel_sbi_write(dev_priv, 0x20EC, 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, 0x21EC, SBI_MPHY);
8474 tmp &= ~(0xF << 28);
8475 tmp |= (4 << 28);
8476 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008477}
8478
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008479/* Implements 3 different sequences from BSpec chapter "Display iCLK
8480 * Programming" based on the parameters passed:
8481 * - Sequence to enable CLKOUT_DP
8482 * - Sequence to enable CLKOUT_DP without spread
8483 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8484 */
8485static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8486 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008487{
8488 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008489 uint32_t reg, tmp;
8490
8491 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8492 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008493 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008494 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008495
Ville Syrjäläa5805162015-05-26 20:42:30 +03008496 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008497
8498 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8499 tmp &= ~SBI_SSCCTL_DISABLE;
8500 tmp |= SBI_SSCCTL_PATHALT;
8501 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8502
8503 udelay(24);
8504
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008505 if (with_spread) {
8506 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8507 tmp &= ~SBI_SSCCTL_PATHALT;
8508 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008509
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008510 if (with_fdi) {
8511 lpt_reset_fdi_mphy(dev_priv);
8512 lpt_program_fdi_mphy(dev_priv);
8513 }
8514 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008515
Ville Syrjäläc2699522015-08-27 23:55:59 +03008516 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008517 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8518 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8519 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008520
Ville Syrjäläa5805162015-05-26 20:42:30 +03008521 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008522}
8523
Paulo Zanoni47701c32013-07-23 11:19:25 -03008524/* Sequence to disable CLKOUT_DP */
8525static void lpt_disable_clkout_dp(struct drm_device *dev)
8526{
8527 struct drm_i915_private *dev_priv = dev->dev_private;
8528 uint32_t reg, tmp;
8529
Ville Syrjäläa5805162015-05-26 20:42:30 +03008530 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008531
Ville Syrjäläc2699522015-08-27 23:55:59 +03008532 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008533 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8534 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8535 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8536
8537 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8538 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8539 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8540 tmp |= SBI_SSCCTL_PATHALT;
8541 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8542 udelay(32);
8543 }
8544 tmp |= SBI_SSCCTL_DISABLE;
8545 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8546 }
8547
Ville Syrjäläa5805162015-05-26 20:42:30 +03008548 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008549}
8550
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008551#define BEND_IDX(steps) ((50 + (steps)) / 5)
8552
8553static const uint16_t sscdivintphase[] = {
8554 [BEND_IDX( 50)] = 0x3B23,
8555 [BEND_IDX( 45)] = 0x3B23,
8556 [BEND_IDX( 40)] = 0x3C23,
8557 [BEND_IDX( 35)] = 0x3C23,
8558 [BEND_IDX( 30)] = 0x3D23,
8559 [BEND_IDX( 25)] = 0x3D23,
8560 [BEND_IDX( 20)] = 0x3E23,
8561 [BEND_IDX( 15)] = 0x3E23,
8562 [BEND_IDX( 10)] = 0x3F23,
8563 [BEND_IDX( 5)] = 0x3F23,
8564 [BEND_IDX( 0)] = 0x0025,
8565 [BEND_IDX( -5)] = 0x0025,
8566 [BEND_IDX(-10)] = 0x0125,
8567 [BEND_IDX(-15)] = 0x0125,
8568 [BEND_IDX(-20)] = 0x0225,
8569 [BEND_IDX(-25)] = 0x0225,
8570 [BEND_IDX(-30)] = 0x0325,
8571 [BEND_IDX(-35)] = 0x0325,
8572 [BEND_IDX(-40)] = 0x0425,
8573 [BEND_IDX(-45)] = 0x0425,
8574 [BEND_IDX(-50)] = 0x0525,
8575};
8576
8577/*
8578 * Bend CLKOUT_DP
8579 * steps -50 to 50 inclusive, in steps of 5
8580 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8581 * change in clock period = -(steps / 10) * 5.787 ps
8582 */
8583static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8584{
8585 uint32_t tmp;
8586 int idx = BEND_IDX(steps);
8587
8588 if (WARN_ON(steps % 5 != 0))
8589 return;
8590
8591 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8592 return;
8593
8594 mutex_lock(&dev_priv->sb_lock);
8595
8596 if (steps % 10 != 0)
8597 tmp = 0xAAAAAAAB;
8598 else
8599 tmp = 0x00000000;
8600 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8601
8602 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8603 tmp &= 0xffff0000;
8604 tmp |= sscdivintphase[idx];
8605 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8606
8607 mutex_unlock(&dev_priv->sb_lock);
8608}
8609
8610#undef BEND_IDX
8611
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008612static void lpt_init_pch_refclk(struct drm_device *dev)
8613{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008614 struct intel_encoder *encoder;
8615 bool has_vga = false;
8616
Damien Lespiaub2784e12014-08-05 11:29:37 +01008617 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008618 switch (encoder->type) {
8619 case INTEL_OUTPUT_ANALOG:
8620 has_vga = true;
8621 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008622 default:
8623 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008624 }
8625 }
8626
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008627 if (has_vga) {
8628 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008629 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008630 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008631 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008632 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008633}
8634
Paulo Zanonidde86e22012-12-01 12:04:25 -02008635/*
8636 * Initialize reference clocks when the driver loads
8637 */
8638void intel_init_pch_refclk(struct drm_device *dev)
8639{
8640 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8641 ironlake_init_pch_refclk(dev);
8642 else if (HAS_PCH_LPT(dev))
8643 lpt_init_pch_refclk(dev);
8644}
8645
Daniel Vetter6ff93602013-04-19 11:24:36 +02008646static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008647{
8648 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8649 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8650 int pipe = intel_crtc->pipe;
8651 uint32_t val;
8652
Daniel Vetter78114072013-06-13 00:54:57 +02008653 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008654
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008655 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008656 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008657 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008658 break;
8659 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008660 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008661 break;
8662 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008663 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008664 break;
8665 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008666 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008667 break;
8668 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008669 /* Case prevented by intel_choose_pipe_bpp_dither. */
8670 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008671 }
8672
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008673 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008674 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8675
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008676 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008677 val |= PIPECONF_INTERLACED_ILK;
8678 else
8679 val |= PIPECONF_PROGRESSIVE;
8680
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008681 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008682 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008683
Paulo Zanonic8203562012-09-12 10:06:29 -03008684 I915_WRITE(PIPECONF(pipe), val);
8685 POSTING_READ(PIPECONF(pipe));
8686}
8687
Daniel Vetter6ff93602013-04-19 11:24:36 +02008688static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008689{
Jani Nikula391bf042016-03-18 17:05:40 +02008690 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008691 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008692 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008693 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008694
Jani Nikula391bf042016-03-18 17:05:40 +02008695 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008696 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8697
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008698 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008699 val |= PIPECONF_INTERLACED_ILK;
8700 else
8701 val |= PIPECONF_PROGRESSIVE;
8702
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008703 I915_WRITE(PIPECONF(cpu_transcoder), val);
8704 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008705}
8706
Jani Nikula391bf042016-03-18 17:05:40 +02008707static void haswell_set_pipemisc(struct drm_crtc *crtc)
8708{
8709 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8710 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8711
8712 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8713 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008714
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008715 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008716 case 18:
8717 val |= PIPEMISC_DITHER_6_BPC;
8718 break;
8719 case 24:
8720 val |= PIPEMISC_DITHER_8_BPC;
8721 break;
8722 case 30:
8723 val |= PIPEMISC_DITHER_10_BPC;
8724 break;
8725 case 36:
8726 val |= PIPEMISC_DITHER_12_BPC;
8727 break;
8728 default:
8729 /* Case prevented by pipe_config_set_bpp. */
8730 BUG();
8731 }
8732
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008733 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008734 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8735
Jani Nikula391bf042016-03-18 17:05:40 +02008736 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008737 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008738}
8739
Paulo Zanonid4b19312012-11-29 11:29:32 -02008740int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8741{
8742 /*
8743 * Account for spread spectrum to avoid
8744 * oversubscribing the link. Max center spread
8745 * is 2.5%; use 5% for safety's sake.
8746 */
8747 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008748 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008749}
8750
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008751static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008752{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008753 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008754}
8755
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008756static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8757 struct intel_crtc_state *crtc_state,
8758 intel_clock_t *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008759{
8760 struct drm_crtc *crtc = &intel_crtc->base;
8761 struct drm_device *dev = crtc->dev;
8762 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008763 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008764 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008765 struct drm_connector_state *connector_state;
8766 struct intel_encoder *encoder;
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008767 u32 dpll, fp, fp2;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008768 int factor, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008769 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008770
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008771 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008772 if (connector_state->crtc != crtc_state->base.crtc)
8773 continue;
8774
8775 encoder = to_intel_encoder(connector_state->best_encoder);
8776
8777 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008778 case INTEL_OUTPUT_LVDS:
8779 is_lvds = true;
8780 break;
8781 case INTEL_OUTPUT_SDVO:
8782 case INTEL_OUTPUT_HDMI:
8783 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008784 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008785 default:
8786 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008787 }
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008788 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008789
Chris Wilsonc1858122010-12-03 21:35:48 +00008790 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008791 factor = 21;
8792 if (is_lvds) {
8793 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008794 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008795 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008796 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008797 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008798 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008799
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008800 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008801
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008802 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8803 fp |= FP_CB_TUNE;
8804
8805 if (reduced_clock) {
8806 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8807
8808 if (reduced_clock->m < factor * reduced_clock->n)
8809 fp2 |= FP_CB_TUNE;
8810 } else {
8811 fp2 = fp;
8812 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008813
Chris Wilson5eddb702010-09-11 13:48:45 +01008814 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008815
Eric Anholta07d6782011-03-30 13:01:08 -07008816 if (is_lvds)
8817 dpll |= DPLLB_MODE_LVDS;
8818 else
8819 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008820
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008821 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008822 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008823
8824 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008825 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008826 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008827 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008828
Eric Anholta07d6782011-03-30 13:01:08 -07008829 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008830 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008831 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008832 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008833
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008834 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008835 case 5:
8836 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8837 break;
8838 case 7:
8839 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8840 break;
8841 case 10:
8842 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8843 break;
8844 case 14:
8845 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8846 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008847 }
8848
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008849 if (is_lvds && intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008850 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008851 else
8852 dpll |= PLL_REF_INPUT_DREFCLK;
8853
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008854 dpll |= DPLL_VCO_ENABLE;
8855
8856 crtc_state->dpll_hw_state.dpll = dpll;
8857 crtc_state->dpll_hw_state.fp0 = fp;
8858 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008859}
8860
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008861static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8862 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008863{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008864 struct drm_device *dev = crtc->base.dev;
8865 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008866 intel_clock_t reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008867 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008868 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008869 const intel_limit_t *limit;
8870 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008871
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008872 memset(&crtc_state->dpll_hw_state, 0,
8873 sizeof(crtc_state->dpll_hw_state));
8874
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008875 crtc->lowfreq_avail = false;
8876
8877 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8878 if (!crtc_state->has_pch_encoder)
8879 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008880
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008881 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8882 if (intel_panel_use_ssc(dev_priv)) {
8883 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8884 dev_priv->vbt.lvds_ssc_freq);
8885 refclk = dev_priv->vbt.lvds_ssc_freq;
8886 }
8887
8888 if (intel_is_dual_link_lvds(dev)) {
8889 if (refclk == 100000)
8890 limit = &intel_limits_ironlake_dual_lvds_100m;
8891 else
8892 limit = &intel_limits_ironlake_dual_lvds;
8893 } else {
8894 if (refclk == 100000)
8895 limit = &intel_limits_ironlake_single_lvds_100m;
8896 else
8897 limit = &intel_limits_ironlake_single_lvds;
8898 }
8899 } else {
8900 limit = &intel_limits_ironlake_dac;
8901 }
8902
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008903 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008904 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8905 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008906 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8907 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008908 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008909
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008910 ironlake_compute_dpll(crtc, crtc_state,
8911 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008912
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008913 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8914 if (pll == NULL) {
8915 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8916 pipe_name(crtc->pipe));
8917 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008918 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008919
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008920 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8921 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008922 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02008923
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008924 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008925}
8926
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008927static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8928 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008929{
8930 struct drm_device *dev = crtc->base.dev;
8931 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008932 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008933
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008934 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8935 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8936 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8937 & ~TU_SIZE_MASK;
8938 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8939 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8940 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8941}
8942
8943static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8944 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008945 struct intel_link_m_n *m_n,
8946 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008947{
8948 struct drm_device *dev = crtc->base.dev;
8949 struct drm_i915_private *dev_priv = dev->dev_private;
8950 enum pipe pipe = crtc->pipe;
8951
8952 if (INTEL_INFO(dev)->gen >= 5) {
8953 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8954 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8955 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8956 & ~TU_SIZE_MASK;
8957 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8958 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8959 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008960 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8961 * gen < 8) and if DRRS is supported (to make sure the
8962 * registers are not unnecessarily read).
8963 */
8964 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008965 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008966 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8967 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8968 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8969 & ~TU_SIZE_MASK;
8970 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8971 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8972 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8973 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008974 } else {
8975 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8976 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8977 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8978 & ~TU_SIZE_MASK;
8979 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8980 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8981 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8982 }
8983}
8984
8985void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008986 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008987{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008988 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008989 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8990 else
8991 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008992 &pipe_config->dp_m_n,
8993 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008994}
8995
Daniel Vetter72419202013-04-04 13:28:53 +02008996static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008997 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008998{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008999 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009000 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009001}
9002
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009003static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009004 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009005{
9006 struct drm_device *dev = crtc->base.dev;
9007 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009008 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9009 uint32_t ps_ctrl = 0;
9010 int id = -1;
9011 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009012
Chandra Kondurua1b22782015-04-07 15:28:45 -07009013 /* find scaler attached to this pipe */
9014 for (i = 0; i < crtc->num_scalers; i++) {
9015 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9016 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9017 id = i;
9018 pipe_config->pch_pfit.enabled = true;
9019 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9020 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9021 break;
9022 }
9023 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009024
Chandra Kondurua1b22782015-04-07 15:28:45 -07009025 scaler_state->scaler_id = id;
9026 if (id >= 0) {
9027 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9028 } else {
9029 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009030 }
9031}
9032
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009033static void
9034skylake_get_initial_plane_config(struct intel_crtc *crtc,
9035 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009036{
9037 struct drm_device *dev = crtc->base.dev;
9038 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009039 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009040 int pipe = crtc->pipe;
9041 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009042 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009043 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009044 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009045
Damien Lespiaud9806c92015-01-21 14:07:19 +00009046 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009047 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009048 DRM_DEBUG_KMS("failed to alloc fb\n");
9049 return;
9050 }
9051
Damien Lespiau1b842c82015-01-21 13:50:54 +00009052 fb = &intel_fb->base;
9053
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009054 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009055 if (!(val & PLANE_CTL_ENABLE))
9056 goto error;
9057
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009058 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9059 fourcc = skl_format_to_fourcc(pixel_format,
9060 val & PLANE_CTL_ORDER_RGBX,
9061 val & PLANE_CTL_ALPHA_MASK);
9062 fb->pixel_format = fourcc;
9063 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9064
Damien Lespiau40f46282015-02-27 11:15:21 +00009065 tiling = val & PLANE_CTL_TILED_MASK;
9066 switch (tiling) {
9067 case PLANE_CTL_TILED_LINEAR:
9068 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9069 break;
9070 case PLANE_CTL_TILED_X:
9071 plane_config->tiling = I915_TILING_X;
9072 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9073 break;
9074 case PLANE_CTL_TILED_Y:
9075 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9076 break;
9077 case PLANE_CTL_TILED_YF:
9078 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9079 break;
9080 default:
9081 MISSING_CASE(tiling);
9082 goto error;
9083 }
9084
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009085 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9086 plane_config->base = base;
9087
9088 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9089
9090 val = I915_READ(PLANE_SIZE(pipe, 0));
9091 fb->height = ((val >> 16) & 0xfff) + 1;
9092 fb->width = ((val >> 0) & 0x1fff) + 1;
9093
9094 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009095 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009096 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009097 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9098
9099 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009100 fb->pixel_format,
9101 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009102
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009103 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009104
9105 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9106 pipe_name(pipe), fb->width, fb->height,
9107 fb->bits_per_pixel, base, fb->pitches[0],
9108 plane_config->size);
9109
Damien Lespiau2d140302015-02-05 17:22:18 +00009110 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009111 return;
9112
9113error:
9114 kfree(fb);
9115}
9116
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009117static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009118 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009119{
9120 struct drm_device *dev = crtc->base.dev;
9121 struct drm_i915_private *dev_priv = dev->dev_private;
9122 uint32_t tmp;
9123
9124 tmp = I915_READ(PF_CTL(crtc->pipe));
9125
9126 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009127 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009128 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9129 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009130
9131 /* We currently do not free assignements of panel fitters on
9132 * ivb/hsw (since we don't use the higher upscaling modes which
9133 * differentiates them) so just WARN about this case for now. */
9134 if (IS_GEN7(dev)) {
9135 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9136 PF_PIPE_SEL_IVB(crtc->pipe));
9137 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009138 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009139}
9140
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009141static void
9142ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9143 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009144{
9145 struct drm_device *dev = crtc->base.dev;
9146 struct drm_i915_private *dev_priv = dev->dev_private;
9147 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009148 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009149 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009150 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009151 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009152 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009153
Damien Lespiau42a7b082015-02-05 19:35:13 +00009154 val = I915_READ(DSPCNTR(pipe));
9155 if (!(val & DISPLAY_PLANE_ENABLE))
9156 return;
9157
Damien Lespiaud9806c92015-01-21 14:07:19 +00009158 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009159 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009160 DRM_DEBUG_KMS("failed to alloc fb\n");
9161 return;
9162 }
9163
Damien Lespiau1b842c82015-01-21 13:50:54 +00009164 fb = &intel_fb->base;
9165
Daniel Vetter18c52472015-02-10 17:16:09 +00009166 if (INTEL_INFO(dev)->gen >= 4) {
9167 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009168 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009169 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9170 }
9171 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009172
9173 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009174 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009175 fb->pixel_format = fourcc;
9176 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009177
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009178 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009179 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009180 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009181 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009182 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009183 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009184 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009185 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009186 }
9187 plane_config->base = base;
9188
9189 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009190 fb->width = ((val >> 16) & 0xfff) + 1;
9191 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009192
9193 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009194 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009195
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009196 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009197 fb->pixel_format,
9198 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009199
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009200 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009201
Damien Lespiau2844a922015-01-20 12:51:48 +00009202 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9203 pipe_name(pipe), fb->width, fb->height,
9204 fb->bits_per_pixel, base, fb->pitches[0],
9205 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009206
Damien Lespiau2d140302015-02-05 17:22:18 +00009207 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009208}
9209
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009210static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009211 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009212{
9213 struct drm_device *dev = crtc->base.dev;
9214 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009215 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009216 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009217 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009218
Imre Deak17290502016-02-12 18:55:11 +02009219 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9220 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009221 return false;
9222
Daniel Vettere143a212013-07-04 12:01:15 +02009223 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009224 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009225
Imre Deak17290502016-02-12 18:55:11 +02009226 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009227 tmp = I915_READ(PIPECONF(crtc->pipe));
9228 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009229 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009230
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009231 switch (tmp & PIPECONF_BPC_MASK) {
9232 case PIPECONF_6BPC:
9233 pipe_config->pipe_bpp = 18;
9234 break;
9235 case PIPECONF_8BPC:
9236 pipe_config->pipe_bpp = 24;
9237 break;
9238 case PIPECONF_10BPC:
9239 pipe_config->pipe_bpp = 30;
9240 break;
9241 case PIPECONF_12BPC:
9242 pipe_config->pipe_bpp = 36;
9243 break;
9244 default:
9245 break;
9246 }
9247
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009248 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9249 pipe_config->limited_color_range = true;
9250
Daniel Vetterab9412b2013-05-03 11:49:46 +02009251 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009252 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009253 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009254
Daniel Vetter88adfff2013-03-28 10:42:01 +01009255 pipe_config->has_pch_encoder = true;
9256
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009257 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9258 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9259 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009260
9261 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009262
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009263 if (HAS_PCH_IBX(dev_priv->dev)) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009264 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009265 } else {
9266 tmp = I915_READ(PCH_DPLL_SEL);
9267 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009268 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009269 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009270 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009271 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009272
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009273 pipe_config->shared_dpll =
9274 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9275 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009276
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009277 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9278 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009279
9280 tmp = pipe_config->dpll_hw_state.dpll;
9281 pipe_config->pixel_multiplier =
9282 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9283 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009284
9285 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009286 } else {
9287 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009288 }
9289
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009290 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009291 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009292
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009293 ironlake_get_pfit_config(crtc, pipe_config);
9294
Imre Deak17290502016-02-12 18:55:11 +02009295 ret = true;
9296
9297out:
9298 intel_display_power_put(dev_priv, power_domain);
9299
9300 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009301}
9302
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009303static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9304{
9305 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009306 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009307
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009308 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009309 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009310 pipe_name(crtc->pipe));
9311
Rob Clarke2c719b2014-12-15 13:56:32 -05009312 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9313 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009314 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9315 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009316 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9317 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009318 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009319 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009320 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009321 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009322 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009323 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009324 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009325 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009326 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009327
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009328 /*
9329 * In theory we can still leave IRQs enabled, as long as only the HPD
9330 * interrupts remain enabled. We used to check for that, but since it's
9331 * gen-specific and since we only disable LCPLL after we fully disable
9332 * the interrupts, the check below should be enough.
9333 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009334 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009335}
9336
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009337static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9338{
9339 struct drm_device *dev = dev_priv->dev;
9340
9341 if (IS_HASWELL(dev))
9342 return I915_READ(D_COMP_HSW);
9343 else
9344 return I915_READ(D_COMP_BDW);
9345}
9346
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009347static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9348{
9349 struct drm_device *dev = dev_priv->dev;
9350
9351 if (IS_HASWELL(dev)) {
9352 mutex_lock(&dev_priv->rps.hw_lock);
9353 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9354 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009355 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009356 mutex_unlock(&dev_priv->rps.hw_lock);
9357 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009358 I915_WRITE(D_COMP_BDW, val);
9359 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009360 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009361}
9362
9363/*
9364 * This function implements pieces of two sequences from BSpec:
9365 * - Sequence for display software to disable LCPLL
9366 * - Sequence for display software to allow package C8+
9367 * The steps implemented here are just the steps that actually touch the LCPLL
9368 * register. Callers should take care of disabling all the display engine
9369 * functions, doing the mode unset, fixing interrupts, etc.
9370 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009371static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9372 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009373{
9374 uint32_t val;
9375
9376 assert_can_disable_lcpll(dev_priv);
9377
9378 val = I915_READ(LCPLL_CTL);
9379
9380 if (switch_to_fclk) {
9381 val |= LCPLL_CD_SOURCE_FCLK;
9382 I915_WRITE(LCPLL_CTL, val);
9383
9384 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9385 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9386 DRM_ERROR("Switching to FCLK failed\n");
9387
9388 val = I915_READ(LCPLL_CTL);
9389 }
9390
9391 val |= LCPLL_PLL_DISABLE;
9392 I915_WRITE(LCPLL_CTL, val);
9393 POSTING_READ(LCPLL_CTL);
9394
9395 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9396 DRM_ERROR("LCPLL still locked\n");
9397
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009398 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009399 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009400 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009401 ndelay(100);
9402
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009403 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9404 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009405 DRM_ERROR("D_COMP RCOMP still in progress\n");
9406
9407 if (allow_power_down) {
9408 val = I915_READ(LCPLL_CTL);
9409 val |= LCPLL_POWER_DOWN_ALLOW;
9410 I915_WRITE(LCPLL_CTL, val);
9411 POSTING_READ(LCPLL_CTL);
9412 }
9413}
9414
9415/*
9416 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9417 * source.
9418 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009419static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009420{
9421 uint32_t val;
9422
9423 val = I915_READ(LCPLL_CTL);
9424
9425 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9426 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9427 return;
9428
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009429 /*
9430 * Make sure we're not on PC8 state before disabling PC8, otherwise
9431 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009432 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009433 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009434
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009435 if (val & LCPLL_POWER_DOWN_ALLOW) {
9436 val &= ~LCPLL_POWER_DOWN_ALLOW;
9437 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009438 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009439 }
9440
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009441 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009442 val |= D_COMP_COMP_FORCE;
9443 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009444 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009445
9446 val = I915_READ(LCPLL_CTL);
9447 val &= ~LCPLL_PLL_DISABLE;
9448 I915_WRITE(LCPLL_CTL, val);
9449
9450 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9451 DRM_ERROR("LCPLL not locked yet\n");
9452
9453 if (val & LCPLL_CD_SOURCE_FCLK) {
9454 val = I915_READ(LCPLL_CTL);
9455 val &= ~LCPLL_CD_SOURCE_FCLK;
9456 I915_WRITE(LCPLL_CTL, val);
9457
9458 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9459 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9460 DRM_ERROR("Switching back to LCPLL failed\n");
9461 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009462
Mika Kuoppala59bad942015-01-16 11:34:40 +02009463 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009464 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009465}
9466
Paulo Zanoni765dab672014-03-07 20:08:18 -03009467/*
9468 * Package states C8 and deeper are really deep PC states that can only be
9469 * reached when all the devices on the system allow it, so even if the graphics
9470 * device allows PC8+, it doesn't mean the system will actually get to these
9471 * states. Our driver only allows PC8+ when going into runtime PM.
9472 *
9473 * The requirements for PC8+ are that all the outputs are disabled, the power
9474 * well is disabled and most interrupts are disabled, and these are also
9475 * requirements for runtime PM. When these conditions are met, we manually do
9476 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9477 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9478 * hang the machine.
9479 *
9480 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9481 * the state of some registers, so when we come back from PC8+ we need to
9482 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9483 * need to take care of the registers kept by RC6. Notice that this happens even
9484 * if we don't put the device in PCI D3 state (which is what currently happens
9485 * because of the runtime PM support).
9486 *
9487 * For more, read "Display Sequences for Package C8" on the hardware
9488 * documentation.
9489 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009490void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009491{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009492 struct drm_device *dev = dev_priv->dev;
9493 uint32_t val;
9494
Paulo Zanonic67a4702013-08-19 13:18:09 -03009495 DRM_DEBUG_KMS("Enabling package C8+\n");
9496
Ville Syrjäläc2699522015-08-27 23:55:59 +03009497 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009498 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9499 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9500 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9501 }
9502
9503 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009504 hsw_disable_lcpll(dev_priv, true, true);
9505}
9506
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009507void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009508{
9509 struct drm_device *dev = dev_priv->dev;
9510 uint32_t val;
9511
Paulo Zanonic67a4702013-08-19 13:18:09 -03009512 DRM_DEBUG_KMS("Disabling package C8+\n");
9513
9514 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009515 lpt_init_pch_refclk(dev);
9516
Ville Syrjäläc2699522015-08-27 23:55:59 +03009517 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009518 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9519 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9520 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9521 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009522}
9523
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009524static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309525{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009526 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009527 struct intel_atomic_state *old_intel_state =
9528 to_intel_atomic_state(old_state);
9529 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309530
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009531 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309532}
9533
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009534/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009535static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009536{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009537 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9538 struct drm_i915_private *dev_priv = state->dev->dev_private;
9539 struct drm_crtc *crtc;
9540 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009541 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009542 unsigned max_pixel_rate = 0, i;
9543 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009544
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009545 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9546 sizeof(intel_state->min_pixclk));
9547
9548 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009549 int pixel_rate;
9550
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009551 crtc_state = to_intel_crtc_state(cstate);
9552 if (!crtc_state->base.enable) {
9553 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009554 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009555 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009556
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009557 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009558
9559 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009560 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009561 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9562
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009563 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009564 }
9565
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009566 for_each_pipe(dev_priv, pipe)
9567 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9568
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009569 return max_pixel_rate;
9570}
9571
9572static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9573{
9574 struct drm_i915_private *dev_priv = dev->dev_private;
9575 uint32_t val, data;
9576 int ret;
9577
9578 if (WARN((I915_READ(LCPLL_CTL) &
9579 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9580 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9581 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9582 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9583 "trying to change cdclk frequency with cdclk not enabled\n"))
9584 return;
9585
9586 mutex_lock(&dev_priv->rps.hw_lock);
9587 ret = sandybridge_pcode_write(dev_priv,
9588 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9589 mutex_unlock(&dev_priv->rps.hw_lock);
9590 if (ret) {
9591 DRM_ERROR("failed to inform pcode about cdclk change\n");
9592 return;
9593 }
9594
9595 val = I915_READ(LCPLL_CTL);
9596 val |= LCPLL_CD_SOURCE_FCLK;
9597 I915_WRITE(LCPLL_CTL, val);
9598
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009599 if (wait_for_us(I915_READ(LCPLL_CTL) &
9600 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009601 DRM_ERROR("Switching to FCLK failed\n");
9602
9603 val = I915_READ(LCPLL_CTL);
9604 val &= ~LCPLL_CLK_FREQ_MASK;
9605
9606 switch (cdclk) {
9607 case 450000:
9608 val |= LCPLL_CLK_FREQ_450;
9609 data = 0;
9610 break;
9611 case 540000:
9612 val |= LCPLL_CLK_FREQ_54O_BDW;
9613 data = 1;
9614 break;
9615 case 337500:
9616 val |= LCPLL_CLK_FREQ_337_5_BDW;
9617 data = 2;
9618 break;
9619 case 675000:
9620 val |= LCPLL_CLK_FREQ_675_BDW;
9621 data = 3;
9622 break;
9623 default:
9624 WARN(1, "invalid cdclk frequency\n");
9625 return;
9626 }
9627
9628 I915_WRITE(LCPLL_CTL, val);
9629
9630 val = I915_READ(LCPLL_CTL);
9631 val &= ~LCPLL_CD_SOURCE_FCLK;
9632 I915_WRITE(LCPLL_CTL, val);
9633
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009634 if (wait_for_us((I915_READ(LCPLL_CTL) &
9635 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009636 DRM_ERROR("Switching back to LCPLL failed\n");
9637
9638 mutex_lock(&dev_priv->rps.hw_lock);
9639 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9640 mutex_unlock(&dev_priv->rps.hw_lock);
9641
9642 intel_update_cdclk(dev);
9643
9644 WARN(cdclk != dev_priv->cdclk_freq,
9645 "cdclk requested %d kHz but got %d kHz\n",
9646 cdclk, dev_priv->cdclk_freq);
9647}
9648
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009649static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009650{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009651 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009652 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009653 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009654 int cdclk;
9655
9656 /*
9657 * FIXME should also account for plane ratio
9658 * once 64bpp pixel formats are supported.
9659 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009660 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009661 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009662 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009663 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009664 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009665 cdclk = 450000;
9666 else
9667 cdclk = 337500;
9668
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009669 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009670 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9671 cdclk, dev_priv->max_cdclk_freq);
9672 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009673 }
9674
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009675 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9676 if (!intel_state->active_crtcs)
9677 intel_state->dev_cdclk = 337500;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009678
9679 return 0;
9680}
9681
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009682static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009683{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009684 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009685 struct intel_atomic_state *old_intel_state =
9686 to_intel_atomic_state(old_state);
9687 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009688
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009689 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009690}
9691
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009692static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9693 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009694{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009695 struct intel_encoder *intel_encoder =
9696 intel_ddi_get_crtc_new_encoder(crtc_state);
9697
9698 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9699 if (!intel_ddi_pll_select(crtc, crtc_state))
9700 return -EINVAL;
9701 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009702
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009703 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009704
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009705 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009706}
9707
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309708static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9709 enum port port,
9710 struct intel_crtc_state *pipe_config)
9711{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009712 enum intel_dpll_id id;
9713
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309714 switch (port) {
9715 case PORT_A:
9716 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009717 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309718 break;
9719 case PORT_B:
9720 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009721 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309722 break;
9723 case PORT_C:
9724 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009725 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309726 break;
9727 default:
9728 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009729 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309730 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009731
9732 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309733}
9734
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009735static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9736 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009737 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009738{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009739 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009740 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009741
9742 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9743 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9744
9745 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009746 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009747 id = DPLL_ID_SKL_DPLL0;
9748 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009749 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009750 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009751 break;
9752 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009753 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009754 break;
9755 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009756 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009757 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009758 default:
9759 MISSING_CASE(pipe_config->ddi_pll_sel);
9760 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009761 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009762
9763 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009764}
9765
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009766static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9767 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009768 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009769{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009770 enum intel_dpll_id id;
9771
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009772 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9773
9774 switch (pipe_config->ddi_pll_sel) {
9775 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009776 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009777 break;
9778 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009779 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009780 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009781 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009782 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009783 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009784 case PORT_CLK_SEL_LCPLL_810:
9785 id = DPLL_ID_LCPLL_810;
9786 break;
9787 case PORT_CLK_SEL_LCPLL_1350:
9788 id = DPLL_ID_LCPLL_1350;
9789 break;
9790 case PORT_CLK_SEL_LCPLL_2700:
9791 id = DPLL_ID_LCPLL_2700;
9792 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009793 default:
9794 MISSING_CASE(pipe_config->ddi_pll_sel);
9795 /* fall through */
9796 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009797 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009798 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009799
9800 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009801}
9802
Jani Nikulacf304292016-03-18 17:05:41 +02009803static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9804 struct intel_crtc_state *pipe_config,
9805 unsigned long *power_domain_mask)
9806{
9807 struct drm_device *dev = crtc->base.dev;
9808 struct drm_i915_private *dev_priv = dev->dev_private;
9809 enum intel_display_power_domain power_domain;
9810 u32 tmp;
9811
9812 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9813
9814 /*
9815 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9816 * consistency and less surprising code; it's in always on power).
9817 */
9818 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9819 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9820 enum pipe trans_edp_pipe;
9821 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9822 default:
9823 WARN(1, "unknown pipe linked to edp transcoder\n");
9824 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9825 case TRANS_DDI_EDP_INPUT_A_ON:
9826 trans_edp_pipe = PIPE_A;
9827 break;
9828 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9829 trans_edp_pipe = PIPE_B;
9830 break;
9831 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9832 trans_edp_pipe = PIPE_C;
9833 break;
9834 }
9835
9836 if (trans_edp_pipe == crtc->pipe)
9837 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9838 }
9839
9840 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9841 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9842 return false;
9843 *power_domain_mask |= BIT(power_domain);
9844
9845 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9846
9847 return tmp & PIPECONF_ENABLE;
9848}
9849
Jani Nikula4d1de972016-03-18 17:05:42 +02009850static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9851 struct intel_crtc_state *pipe_config,
9852 unsigned long *power_domain_mask)
9853{
9854 struct drm_device *dev = crtc->base.dev;
9855 struct drm_i915_private *dev_priv = dev->dev_private;
9856 enum intel_display_power_domain power_domain;
9857 enum port port;
9858 enum transcoder cpu_transcoder;
9859 u32 tmp;
9860
9861 pipe_config->has_dsi_encoder = false;
9862
9863 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9864 if (port == PORT_A)
9865 cpu_transcoder = TRANSCODER_DSI_A;
9866 else
9867 cpu_transcoder = TRANSCODER_DSI_C;
9868
9869 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9870 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9871 continue;
9872 *power_domain_mask |= BIT(power_domain);
9873
Imre Deakdb18b6a2016-03-24 12:41:40 +02009874 /*
9875 * The PLL needs to be enabled with a valid divider
9876 * configuration, otherwise accessing DSI registers will hang
9877 * the machine. See BSpec North Display Engine
9878 * registers/MIPI[BXT]. We can break out here early, since we
9879 * need the same DSI PLL to be enabled for both DSI ports.
9880 */
9881 if (!intel_dsi_pll_is_enabled(dev_priv))
9882 break;
9883
Jani Nikula4d1de972016-03-18 17:05:42 +02009884 /* XXX: this works for video mode only */
9885 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9886 if (!(tmp & DPI_ENABLE))
9887 continue;
9888
9889 tmp = I915_READ(MIPI_CTRL(port));
9890 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9891 continue;
9892
9893 pipe_config->cpu_transcoder = cpu_transcoder;
9894 pipe_config->has_dsi_encoder = true;
9895 break;
9896 }
9897
9898 return pipe_config->has_dsi_encoder;
9899}
9900
Daniel Vetter26804af2014-06-25 22:01:55 +03009901static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009902 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009903{
9904 struct drm_device *dev = crtc->base.dev;
9905 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009906 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009907 enum port port;
9908 uint32_t tmp;
9909
9910 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9911
9912 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9913
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07009914 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009915 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309916 else if (IS_BROXTON(dev))
9917 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009918 else
9919 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009920
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009921 pll = pipe_config->shared_dpll;
9922 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009923 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9924 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009925 }
9926
Daniel Vetter26804af2014-06-25 22:01:55 +03009927 /*
9928 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9929 * DDI E. So just check whether this pipe is wired to DDI E and whether
9930 * the PCH transcoder is on.
9931 */
Damien Lespiauca370452013-12-03 13:56:24 +00009932 if (INTEL_INFO(dev)->gen < 9 &&
9933 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009934 pipe_config->has_pch_encoder = true;
9935
9936 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9937 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9938 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9939
9940 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9941 }
9942}
9943
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009944static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009945 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009946{
9947 struct drm_device *dev = crtc->base.dev;
9948 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009949 enum intel_display_power_domain power_domain;
9950 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009951 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009952
Imre Deak17290502016-02-12 18:55:11 +02009953 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9954 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009955 return false;
Imre Deak17290502016-02-12 18:55:11 +02009956 power_domain_mask = BIT(power_domain);
9957
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009958 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009959
Jani Nikulacf304292016-03-18 17:05:41 +02009960 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +02009961
Jani Nikula4d1de972016-03-18 17:05:42 +02009962 if (IS_BROXTON(dev_priv)) {
9963 bxt_get_dsi_transcoder_state(crtc, pipe_config,
9964 &power_domain_mask);
9965 WARN_ON(active && pipe_config->has_dsi_encoder);
9966 if (pipe_config->has_dsi_encoder)
9967 active = true;
9968 }
9969
Jani Nikulacf304292016-03-18 17:05:41 +02009970 if (!active)
Imre Deak17290502016-02-12 18:55:11 +02009971 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009972
Jani Nikula4d1de972016-03-18 17:05:42 +02009973 if (!pipe_config->has_dsi_encoder) {
9974 haswell_get_ddi_port_state(crtc, pipe_config);
9975 intel_get_pipe_timings(crtc, pipe_config);
9976 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009977
Jani Nikulabc58be62016-03-18 17:05:39 +02009978 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009979
Lionel Landwerlin05dc6982016-03-16 10:57:15 +00009980 pipe_config->gamma_mode =
9981 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9982
Chandra Kondurua1b22782015-04-07 15:28:45 -07009983 if (INTEL_INFO(dev)->gen >= 9) {
9984 skl_init_scalers(dev, crtc, pipe_config);
9985 }
9986
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009987 if (INTEL_INFO(dev)->gen >= 9) {
9988 pipe_config->scaler_state.scaler_id = -1;
9989 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9990 }
9991
Imre Deak17290502016-02-12 18:55:11 +02009992 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9993 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
9994 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009995 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009996 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009997 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009998 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009999 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010000
Jesse Barnese59150d2014-01-07 13:30:45 -080010001 if (IS_HASWELL(dev))
10002 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10003 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010004
Jani Nikula4d1de972016-03-18 17:05:42 +020010005 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10006 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010007 pipe_config->pixel_multiplier =
10008 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10009 } else {
10010 pipe_config->pixel_multiplier = 1;
10011 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010012
Imre Deak17290502016-02-12 18:55:11 +020010013out:
10014 for_each_power_domain(power_domain, power_domain_mask)
10015 intel_display_power_put(dev_priv, power_domain);
10016
Jani Nikulacf304292016-03-18 17:05:41 +020010017 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010018}
10019
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010020static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10021 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010022{
10023 struct drm_device *dev = crtc->dev;
10024 struct drm_i915_private *dev_priv = dev->dev_private;
10025 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010026 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010027
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010028 if (plane_state && plane_state->visible) {
10029 unsigned int width = plane_state->base.crtc_w;
10030 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010031 unsigned int stride = roundup_pow_of_two(width) * 4;
10032
10033 switch (stride) {
10034 default:
10035 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10036 width, stride);
10037 stride = 256;
10038 /* fallthrough */
10039 case 256:
10040 case 512:
10041 case 1024:
10042 case 2048:
10043 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010044 }
10045
Ville Syrjälädc41c152014-08-13 11:57:05 +030010046 cntl |= CURSOR_ENABLE |
10047 CURSOR_GAMMA_ENABLE |
10048 CURSOR_FORMAT_ARGB |
10049 CURSOR_STRIDE(stride);
10050
10051 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010052 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010053
Ville Syrjälädc41c152014-08-13 11:57:05 +030010054 if (intel_crtc->cursor_cntl != 0 &&
10055 (intel_crtc->cursor_base != base ||
10056 intel_crtc->cursor_size != size ||
10057 intel_crtc->cursor_cntl != cntl)) {
10058 /* On these chipsets we can only modify the base/size/stride
10059 * whilst the cursor is disabled.
10060 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010061 I915_WRITE(CURCNTR(PIPE_A), 0);
10062 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010063 intel_crtc->cursor_cntl = 0;
10064 }
10065
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010066 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010067 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010068 intel_crtc->cursor_base = base;
10069 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010070
10071 if (intel_crtc->cursor_size != size) {
10072 I915_WRITE(CURSIZE, size);
10073 intel_crtc->cursor_size = size;
10074 }
10075
Chris Wilson4b0e3332014-05-30 16:35:26 +030010076 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010077 I915_WRITE(CURCNTR(PIPE_A), cntl);
10078 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010079 intel_crtc->cursor_cntl = cntl;
10080 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010081}
10082
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010083static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10084 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010085{
10086 struct drm_device *dev = crtc->dev;
10087 struct drm_i915_private *dev_priv = dev->dev_private;
10088 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10089 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010090 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010091
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010092 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010093 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010094 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010095 case 64:
10096 cntl |= CURSOR_MODE_64_ARGB_AX;
10097 break;
10098 case 128:
10099 cntl |= CURSOR_MODE_128_ARGB_AX;
10100 break;
10101 case 256:
10102 cntl |= CURSOR_MODE_256_ARGB_AX;
10103 break;
10104 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010105 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010106 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010107 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010108 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010109
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010110 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010111 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010112
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010113 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10114 cntl |= CURSOR_ROTATE_180;
10115 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010116
Chris Wilson4b0e3332014-05-30 16:35:26 +030010117 if (intel_crtc->cursor_cntl != cntl) {
10118 I915_WRITE(CURCNTR(pipe), cntl);
10119 POSTING_READ(CURCNTR(pipe));
10120 intel_crtc->cursor_cntl = cntl;
10121 }
10122
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010123 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010124 I915_WRITE(CURBASE(pipe), base);
10125 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010126
10127 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010128}
10129
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010130/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010131static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010132 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010133{
10134 struct drm_device *dev = crtc->dev;
10135 struct drm_i915_private *dev_priv = dev->dev_private;
10136 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10137 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010138 u32 base = intel_crtc->cursor_addr;
10139 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010140
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010141 if (plane_state) {
10142 int x = plane_state->base.crtc_x;
10143 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010144
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010145 if (x < 0) {
10146 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10147 x = -x;
10148 }
10149 pos |= x << CURSOR_X_SHIFT;
10150
10151 if (y < 0) {
10152 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10153 y = -y;
10154 }
10155 pos |= y << CURSOR_Y_SHIFT;
10156
10157 /* ILK+ do this automagically */
10158 if (HAS_GMCH_DISPLAY(dev) &&
10159 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10160 base += (plane_state->base.crtc_h *
10161 plane_state->base.crtc_w - 1) * 4;
10162 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010163 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010164
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010165 I915_WRITE(CURPOS(pipe), pos);
10166
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010167 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010168 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010169 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010170 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010171}
10172
Ville Syrjälädc41c152014-08-13 11:57:05 +030010173static bool cursor_size_ok(struct drm_device *dev,
10174 uint32_t width, uint32_t height)
10175{
10176 if (width == 0 || height == 0)
10177 return false;
10178
10179 /*
10180 * 845g/865g are special in that they are only limited by
10181 * the width of their cursors, the height is arbitrary up to
10182 * the precision of the register. Everything else requires
10183 * square cursors, limited to a few power-of-two sizes.
10184 */
10185 if (IS_845G(dev) || IS_I865G(dev)) {
10186 if ((width & 63) != 0)
10187 return false;
10188
10189 if (width > (IS_845G(dev) ? 64 : 512))
10190 return false;
10191
10192 if (height > 1023)
10193 return false;
10194 } else {
10195 switch (width | height) {
10196 case 256:
10197 case 128:
10198 if (IS_GEN2(dev))
10199 return false;
10200 case 64:
10201 break;
10202 default:
10203 return false;
10204 }
10205 }
10206
10207 return true;
10208}
10209
Jesse Barnes79e53942008-11-07 14:24:08 -080010210/* VESA 640x480x72Hz mode to set on the pipe */
10211static struct drm_display_mode load_detect_mode = {
10212 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10213 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10214};
10215
Daniel Vettera8bb6812014-02-10 18:00:39 +010010216struct drm_framebuffer *
10217__intel_framebuffer_create(struct drm_device *dev,
10218 struct drm_mode_fb_cmd2 *mode_cmd,
10219 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010220{
10221 struct intel_framebuffer *intel_fb;
10222 int ret;
10223
10224 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010225 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010226 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010227
10228 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010229 if (ret)
10230 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010231
10232 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010233
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010234err:
10235 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010236 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010237}
10238
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010239static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010240intel_framebuffer_create(struct drm_device *dev,
10241 struct drm_mode_fb_cmd2 *mode_cmd,
10242 struct drm_i915_gem_object *obj)
10243{
10244 struct drm_framebuffer *fb;
10245 int ret;
10246
10247 ret = i915_mutex_lock_interruptible(dev);
10248 if (ret)
10249 return ERR_PTR(ret);
10250 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10251 mutex_unlock(&dev->struct_mutex);
10252
10253 return fb;
10254}
10255
Chris Wilsond2dff872011-04-19 08:36:26 +010010256static u32
10257intel_framebuffer_pitch_for_width(int width, int bpp)
10258{
10259 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10260 return ALIGN(pitch, 64);
10261}
10262
10263static u32
10264intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10265{
10266 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010267 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010268}
10269
10270static struct drm_framebuffer *
10271intel_framebuffer_create_for_mode(struct drm_device *dev,
10272 struct drm_display_mode *mode,
10273 int depth, int bpp)
10274{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010275 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010276 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010277 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010278
10279 obj = i915_gem_alloc_object(dev,
10280 intel_framebuffer_size_for_mode(mode, bpp));
10281 if (obj == NULL)
10282 return ERR_PTR(-ENOMEM);
10283
10284 mode_cmd.width = mode->hdisplay;
10285 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010286 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10287 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010288 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010289
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010290 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10291 if (IS_ERR(fb))
10292 drm_gem_object_unreference_unlocked(&obj->base);
10293
10294 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010295}
10296
10297static struct drm_framebuffer *
10298mode_fits_in_fbdev(struct drm_device *dev,
10299 struct drm_display_mode *mode)
10300{
Daniel Vetter06957262015-08-10 13:34:08 +020010301#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010302 struct drm_i915_private *dev_priv = dev->dev_private;
10303 struct drm_i915_gem_object *obj;
10304 struct drm_framebuffer *fb;
10305
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010306 if (!dev_priv->fbdev)
10307 return NULL;
10308
10309 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010310 return NULL;
10311
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010312 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010313 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010314
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010315 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010316 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10317 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010318 return NULL;
10319
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010320 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010321 return NULL;
10322
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010323 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010324 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010325#else
10326 return NULL;
10327#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010328}
10329
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010330static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10331 struct drm_crtc *crtc,
10332 struct drm_display_mode *mode,
10333 struct drm_framebuffer *fb,
10334 int x, int y)
10335{
10336 struct drm_plane_state *plane_state;
10337 int hdisplay, vdisplay;
10338 int ret;
10339
10340 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10341 if (IS_ERR(plane_state))
10342 return PTR_ERR(plane_state);
10343
10344 if (mode)
10345 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10346 else
10347 hdisplay = vdisplay = 0;
10348
10349 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10350 if (ret)
10351 return ret;
10352 drm_atomic_set_fb_for_plane(plane_state, fb);
10353 plane_state->crtc_x = 0;
10354 plane_state->crtc_y = 0;
10355 plane_state->crtc_w = hdisplay;
10356 plane_state->crtc_h = vdisplay;
10357 plane_state->src_x = x << 16;
10358 plane_state->src_y = y << 16;
10359 plane_state->src_w = hdisplay << 16;
10360 plane_state->src_h = vdisplay << 16;
10361
10362 return 0;
10363}
10364
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010365bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010366 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010367 struct intel_load_detect_pipe *old,
10368 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010369{
10370 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010371 struct intel_encoder *intel_encoder =
10372 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010373 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010374 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010375 struct drm_crtc *crtc = NULL;
10376 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010377 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010378 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010379 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010380 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010381 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010382 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010383
Chris Wilsond2dff872011-04-19 08:36:26 +010010384 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010385 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010386 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010387
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010388 old->restore_state = NULL;
10389
Rob Clark51fd3712013-11-19 12:10:12 -050010390retry:
10391 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10392 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010393 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010394
Jesse Barnes79e53942008-11-07 14:24:08 -080010395 /*
10396 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010397 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010398 * - if the connector already has an assigned crtc, use it (but make
10399 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010400 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010401 * - try to find the first unused crtc that can drive this connector,
10402 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010403 */
10404
10405 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010406 if (connector->state->crtc) {
10407 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010408
Rob Clark51fd3712013-11-19 12:10:12 -050010409 ret = drm_modeset_lock(&crtc->mutex, ctx);
10410 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010411 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010412
10413 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010414 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010415 }
10416
10417 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010418 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010419 i++;
10420 if (!(encoder->possible_crtcs & (1 << i)))
10421 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010422
10423 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10424 if (ret)
10425 goto fail;
10426
10427 if (possible_crtc->state->enable) {
10428 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010429 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010430 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010431
10432 crtc = possible_crtc;
10433 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010434 }
10435
10436 /*
10437 * If we didn't find an unused CRTC, don't use any.
10438 */
10439 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010440 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010441 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010442 }
10443
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010444found:
10445 intel_crtc = to_intel_crtc(crtc);
10446
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010447 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10448 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010449 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010450
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010451 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010452 restore_state = drm_atomic_state_alloc(dev);
10453 if (!state || !restore_state) {
10454 ret = -ENOMEM;
10455 goto fail;
10456 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010457
10458 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010459 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010460
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010461 connector_state = drm_atomic_get_connector_state(state, connector);
10462 if (IS_ERR(connector_state)) {
10463 ret = PTR_ERR(connector_state);
10464 goto fail;
10465 }
10466
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010467 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10468 if (ret)
10469 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010470
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010471 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10472 if (IS_ERR(crtc_state)) {
10473 ret = PTR_ERR(crtc_state);
10474 goto fail;
10475 }
10476
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010477 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010478
Chris Wilson64927112011-04-20 07:25:26 +010010479 if (!mode)
10480 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010481
Chris Wilsond2dff872011-04-19 08:36:26 +010010482 /* We need a framebuffer large enough to accommodate all accesses
10483 * that the plane may generate whilst we perform load detection.
10484 * We can not rely on the fbcon either being present (we get called
10485 * during its initialisation to detect all boot displays, or it may
10486 * not even exist) or that it is large enough to satisfy the
10487 * requested mode.
10488 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010489 fb = mode_fits_in_fbdev(dev, mode);
10490 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010491 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010492 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010493 } else
10494 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010495 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010496 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010497 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010498 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010499
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010500 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10501 if (ret)
10502 goto fail;
10503
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010504 drm_framebuffer_unreference(fb);
10505
10506 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10507 if (ret)
10508 goto fail;
10509
10510 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10511 if (!ret)
10512 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10513 if (!ret)
10514 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10515 if (ret) {
10516 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10517 goto fail;
10518 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010519
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010520 ret = drm_atomic_commit(state);
10521 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010522 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010523 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010524 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010525
10526 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010527
Jesse Barnes79e53942008-11-07 14:24:08 -080010528 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010529 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010530 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010531
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010532fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010533 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010534 drm_atomic_state_free(restore_state);
10535 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010536
Rob Clark51fd3712013-11-19 12:10:12 -050010537 if (ret == -EDEADLK) {
10538 drm_modeset_backoff(ctx);
10539 goto retry;
10540 }
10541
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010542 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010543}
10544
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010545void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010546 struct intel_load_detect_pipe *old,
10547 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010548{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010549 struct intel_encoder *intel_encoder =
10550 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010551 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010552 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010553 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010554
Chris Wilsond2dff872011-04-19 08:36:26 +010010555 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010556 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010557 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010558
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010559 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010560 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010561
10562 ret = drm_atomic_commit(state);
10563 if (ret) {
10564 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10565 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010566 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010567}
10568
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010569static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010570 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010571{
10572 struct drm_i915_private *dev_priv = dev->dev_private;
10573 u32 dpll = pipe_config->dpll_hw_state.dpll;
10574
10575 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010576 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010577 else if (HAS_PCH_SPLIT(dev))
10578 return 120000;
10579 else if (!IS_GEN2(dev))
10580 return 96000;
10581 else
10582 return 48000;
10583}
10584
Jesse Barnes79e53942008-11-07 14:24:08 -080010585/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010586static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010587 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010588{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010589 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010590 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010591 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010592 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010593 u32 fp;
10594 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010595 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010596 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010597
10598 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010599 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010600 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010601 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010602
10603 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010604 if (IS_PINEVIEW(dev)) {
10605 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10606 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010607 } else {
10608 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10609 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10610 }
10611
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010612 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010613 if (IS_PINEVIEW(dev))
10614 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10615 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010616 else
10617 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010618 DPLL_FPA01_P1_POST_DIV_SHIFT);
10619
10620 switch (dpll & DPLL_MODE_MASK) {
10621 case DPLLB_MODE_DAC_SERIAL:
10622 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10623 5 : 10;
10624 break;
10625 case DPLLB_MODE_LVDS:
10626 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10627 7 : 14;
10628 break;
10629 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010630 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010631 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010632 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010633 }
10634
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010635 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010636 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010637 else
Imre Deakdccbea32015-06-22 23:35:51 +030010638 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010639 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010640 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010641 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010642
10643 if (is_lvds) {
10644 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10645 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010646
10647 if (lvds & LVDS_CLKB_POWER_UP)
10648 clock.p2 = 7;
10649 else
10650 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010651 } else {
10652 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10653 clock.p1 = 2;
10654 else {
10655 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10656 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10657 }
10658 if (dpll & PLL_P2_DIVIDE_BY_4)
10659 clock.p2 = 4;
10660 else
10661 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010662 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010663
Imre Deakdccbea32015-06-22 23:35:51 +030010664 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010665 }
10666
Ville Syrjälä18442d02013-09-13 16:00:08 +030010667 /*
10668 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010669 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010670 * encoder's get_config() function.
10671 */
Imre Deakdccbea32015-06-22 23:35:51 +030010672 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010673}
10674
Ville Syrjälä6878da02013-09-13 15:59:11 +030010675int intel_dotclock_calculate(int link_freq,
10676 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010677{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010678 /*
10679 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010680 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010681 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010682 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010683 *
10684 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010685 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010686 */
10687
Ville Syrjälä6878da02013-09-13 15:59:11 +030010688 if (!m_n->link_n)
10689 return 0;
10690
10691 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10692}
10693
Ville Syrjälä18442d02013-09-13 16:00:08 +030010694static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010695 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010696{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010697 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010698
10699 /* read out port_clock from the DPLL */
10700 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010701
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010702 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010703 * In case there is an active pipe without active ports,
10704 * we may need some idea for the dotclock anyway.
10705 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010706 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010707 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010708 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010709 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010710}
10711
10712/** Returns the currently programmed mode of the given pipe. */
10713struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10714 struct drm_crtc *crtc)
10715{
Jesse Barnes548f2452011-02-17 10:40:53 -080010716 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010717 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010718 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010719 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010720 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010721 int htot = I915_READ(HTOTAL(cpu_transcoder));
10722 int hsync = I915_READ(HSYNC(cpu_transcoder));
10723 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10724 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010725 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010726
10727 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10728 if (!mode)
10729 return NULL;
10730
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010731 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10732 if (!pipe_config) {
10733 kfree(mode);
10734 return NULL;
10735 }
10736
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010737 /*
10738 * Construct a pipe_config sufficient for getting the clock info
10739 * back out of crtc_clock_get.
10740 *
10741 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10742 * to use a real value here instead.
10743 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010744 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10745 pipe_config->pixel_multiplier = 1;
10746 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10747 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10748 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10749 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010750
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010751 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010752 mode->hdisplay = (htot & 0xffff) + 1;
10753 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10754 mode->hsync_start = (hsync & 0xffff) + 1;
10755 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10756 mode->vdisplay = (vtot & 0xffff) + 1;
10757 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10758 mode->vsync_start = (vsync & 0xffff) + 1;
10759 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10760
10761 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010762
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010763 kfree(pipe_config);
10764
Jesse Barnes79e53942008-11-07 14:24:08 -080010765 return mode;
10766}
10767
Chris Wilsonf047e392012-07-21 12:31:41 +010010768void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010769{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010770 struct drm_i915_private *dev_priv = dev->dev_private;
10771
Chris Wilsonf62a0072014-02-21 17:55:39 +000010772 if (dev_priv->mm.busy)
10773 return;
10774
Paulo Zanoni43694d62014-03-07 20:08:08 -030010775 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010776 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010777 if (INTEL_INFO(dev)->gen >= 6)
10778 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010779 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010780}
10781
10782void intel_mark_idle(struct drm_device *dev)
10783{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010784 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010785
Chris Wilsonf62a0072014-02-21 17:55:39 +000010786 if (!dev_priv->mm.busy)
10787 return;
10788
10789 dev_priv->mm.busy = false;
10790
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010791 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010792 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010793
Paulo Zanoni43694d62014-03-07 20:08:08 -030010794 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010795}
10796
Jesse Barnes79e53942008-11-07 14:24:08 -080010797static void intel_crtc_destroy(struct drm_crtc *crtc)
10798{
10799 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010800 struct drm_device *dev = crtc->dev;
10801 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010802
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010803 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010804 work = intel_crtc->unpin_work;
10805 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010806 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010807
10808 if (work) {
10809 cancel_work_sync(&work->work);
10810 kfree(work);
10811 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010812
10813 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010814
Jesse Barnes79e53942008-11-07 14:24:08 -080010815 kfree(intel_crtc);
10816}
10817
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010818static void intel_unpin_work_fn(struct work_struct *__work)
10819{
10820 struct intel_unpin_work *work =
10821 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010822 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10823 struct drm_device *dev = crtc->base.dev;
10824 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010825
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010826 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020010827 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilson05394f32010-11-08 19:18:58 +000010828 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010829
John Harrisonf06cc1b2014-11-24 18:49:37 +000010830 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010831 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010832 mutex_unlock(&dev->struct_mutex);
10833
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010834 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanoni1eb52232016-01-19 11:35:44 -020010835 intel_fbc_post_update(crtc);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010836 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010837
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010838 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10839 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010840
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010841 kfree(work);
10842}
10843
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010844static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010845 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010846{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010847 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10848 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010849 unsigned long flags;
10850
10851 /* Ignore early vblank irqs */
10852 if (intel_crtc == NULL)
10853 return;
10854
Daniel Vetterf3260382014-09-15 14:55:23 +020010855 /*
10856 * This is called both by irq handlers and the reset code (to complete
10857 * lost pageflips) so needs the full irqsave spinlocks.
10858 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010859 spin_lock_irqsave(&dev->event_lock, flags);
10860 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010861
10862 /* Ensure we don't miss a work->pending update ... */
10863 smp_rmb();
10864
10865 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010866 spin_unlock_irqrestore(&dev->event_lock, flags);
10867 return;
10868 }
10869
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010870 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010871
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010872 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010873}
10874
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010875void intel_finish_page_flip(struct drm_device *dev, int pipe)
10876{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010877 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010878 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10879
Mario Kleiner49b14a52010-12-09 07:00:07 +010010880 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010881}
10882
10883void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10884{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010885 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010886 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10887
Mario Kleiner49b14a52010-12-09 07:00:07 +010010888 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010889}
10890
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010891/* Is 'a' after or equal to 'b'? */
10892static bool g4x_flip_count_after_eq(u32 a, u32 b)
10893{
10894 return !((a - b) & 0x80000000);
10895}
10896
10897static bool page_flip_finished(struct intel_crtc *crtc)
10898{
10899 struct drm_device *dev = crtc->base.dev;
10900 struct drm_i915_private *dev_priv = dev->dev_private;
10901
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010902 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10903 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10904 return true;
10905
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010906 /*
10907 * The relevant registers doen't exist on pre-ctg.
10908 * As the flip done interrupt doesn't trigger for mmio
10909 * flips on gmch platforms, a flip count check isn't
10910 * really needed there. But since ctg has the registers,
10911 * include it in the check anyway.
10912 */
10913 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10914 return true;
10915
10916 /*
Maarten Lankhorste8861672016-02-24 11:24:26 +010010917 * BDW signals flip done immediately if the plane
10918 * is disabled, even if the plane enable is already
10919 * armed to occur at the next vblank :(
10920 */
10921
10922 /*
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010923 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10924 * used the same base address. In that case the mmio flip might
10925 * have completed, but the CS hasn't even executed the flip yet.
10926 *
10927 * A flip count check isn't enough as the CS might have updated
10928 * the base address just after start of vblank, but before we
10929 * managed to process the interrupt. This means we'd complete the
10930 * CS flip too soon.
10931 *
10932 * Combining both checks should get us a good enough result. It may
10933 * still happen that the CS flip has been executed, but has not
10934 * yet actually completed. But in case the base address is the same
10935 * anyway, we don't really care.
10936 */
10937 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10938 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030010939 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010940 crtc->unpin_work->flip_count);
10941}
10942
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010943void intel_prepare_page_flip(struct drm_device *dev, int plane)
10944{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010945 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010946 struct intel_crtc *intel_crtc =
10947 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10948 unsigned long flags;
10949
Daniel Vetterf3260382014-09-15 14:55:23 +020010950
10951 /*
10952 * This is called both by irq handlers and the reset code (to complete
10953 * lost pageflips) so needs the full irqsave spinlocks.
10954 *
10955 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010956 * generate a page-flip completion irq, i.e. every modeset
10957 * is also accompanied by a spurious intel_prepare_page_flip().
10958 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010959 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010960 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010961 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010962 spin_unlock_irqrestore(&dev->event_lock, flags);
10963}
10964
Chris Wilson60426392015-10-10 10:44:32 +010010965static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010966{
10967 /* Ensure that the work item is consistent when activating it ... */
10968 smp_wmb();
Chris Wilson60426392015-10-10 10:44:32 +010010969 atomic_set(&work->pending, INTEL_FLIP_PENDING);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010970 /* and that it is marked active as soon as the irq could fire. */
10971 smp_wmb();
10972}
10973
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010974static int intel_gen2_queue_flip(struct drm_device *dev,
10975 struct drm_crtc *crtc,
10976 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010977 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010978 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010979 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010980{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010981 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010983 u32 flip_mask;
10984 int ret;
10985
John Harrison5fb9de12015-05-29 17:44:07 +010010986 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010987 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010988 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010989
10990 /* Can't queue multiple flips, so wait for the previous
10991 * one to finish before executing the next.
10992 */
10993 if (intel_crtc->plane)
10994 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10995 else
10996 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010997 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
10998 intel_ring_emit(engine, MI_NOOP);
10999 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011000 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011001 intel_ring_emit(engine, fb->pitches[0]);
11002 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11003 intel_ring_emit(engine, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000011004
Chris Wilson60426392015-10-10 10:44:32 +010011005 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011006 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011007}
11008
11009static int intel_gen3_queue_flip(struct drm_device *dev,
11010 struct drm_crtc *crtc,
11011 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011012 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011013 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011014 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011015{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011016 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011017 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011018 u32 flip_mask;
11019 int ret;
11020
John Harrison5fb9de12015-05-29 17:44:07 +010011021 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011022 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011023 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011024
11025 if (intel_crtc->plane)
11026 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11027 else
11028 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011029 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11030 intel_ring_emit(engine, MI_NOOP);
11031 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011032 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011033 intel_ring_emit(engine, fb->pitches[0]);
11034 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11035 intel_ring_emit(engine, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011036
Chris Wilson60426392015-10-10 10:44:32 +010011037 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011038 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011039}
11040
11041static int intel_gen4_queue_flip(struct drm_device *dev,
11042 struct drm_crtc *crtc,
11043 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011044 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011045 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011046 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011047{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011048 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011049 struct drm_i915_private *dev_priv = dev->dev_private;
11050 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11051 uint32_t pf, pipesrc;
11052 int ret;
11053
John Harrison5fb9de12015-05-29 17:44:07 +010011054 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011055 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011056 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011057
11058 /* i965+ uses the linear or tiled offsets from the
11059 * Display Registers (which do not change across a page-flip)
11060 * so we need only reprogram the base address.
11061 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011062 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011063 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011064 intel_ring_emit(engine, fb->pitches[0]);
11065 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011066 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011067
11068 /* XXX Enabling the panel-fitter across page-flip is so far
11069 * untested on non-native modes, so ignore it for now.
11070 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11071 */
11072 pf = 0;
11073 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011074 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011075
Chris Wilson60426392015-10-10 10:44:32 +010011076 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011077 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011078}
11079
11080static int intel_gen6_queue_flip(struct drm_device *dev,
11081 struct drm_crtc *crtc,
11082 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011083 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011084 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011085 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011086{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011087 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011088 struct drm_i915_private *dev_priv = dev->dev_private;
11089 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11090 uint32_t pf, pipesrc;
11091 int ret;
11092
John Harrison5fb9de12015-05-29 17:44:07 +010011093 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011094 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011095 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011096
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011097 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011098 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011099 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11100 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011101
Chris Wilson99d9acd2012-04-17 20:37:00 +010011102 /* Contrary to the suggestions in the documentation,
11103 * "Enable Panel Fitter" does not seem to be required when page
11104 * flipping with a non-native mode, and worse causes a normal
11105 * modeset to fail.
11106 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11107 */
11108 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011109 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011110 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011111
Chris Wilson60426392015-10-10 10:44:32 +010011112 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011113 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011114}
11115
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011116static int intel_gen7_queue_flip(struct drm_device *dev,
11117 struct drm_crtc *crtc,
11118 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011119 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011120 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011121 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011122{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011123 struct intel_engine_cs *engine = req->engine;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011124 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011125 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011126 int len, ret;
11127
Robin Schroereba905b2014-05-18 02:24:50 +020011128 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011129 case PLANE_A:
11130 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11131 break;
11132 case PLANE_B:
11133 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11134 break;
11135 case PLANE_C:
11136 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11137 break;
11138 default:
11139 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011140 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011141 }
11142
Chris Wilsonffe74d72013-08-26 20:58:12 +010011143 len = 4;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011144 if (engine->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011145 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011146 /*
11147 * On Gen 8, SRM is now taking an extra dword to accommodate
11148 * 48bits addresses, and we need a NOOP for the batch size to
11149 * stay even.
11150 */
11151 if (IS_GEN8(dev))
11152 len += 2;
11153 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011154
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011155 /*
11156 * BSpec MI_DISPLAY_FLIP for IVB:
11157 * "The full packet must be contained within the same cache line."
11158 *
11159 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11160 * cacheline, if we ever start emitting more commands before
11161 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11162 * then do the cacheline alignment, and finally emit the
11163 * MI_DISPLAY_FLIP.
11164 */
John Harrisonbba09b12015-05-29 17:44:06 +010011165 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011166 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011167 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011168
John Harrison5fb9de12015-05-29 17:44:07 +010011169 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011170 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011171 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011172
Chris Wilsonffe74d72013-08-26 20:58:12 +010011173 /* Unmask the flip-done completion message. Note that the bspec says that
11174 * we should do this for both the BCS and RCS, and that we must not unmask
11175 * more than one flip event at any time (or ensure that one flip message
11176 * can be sent by waiting for flip-done prior to queueing new flips).
11177 * Experimentation says that BCS works despite DERRMR masking all
11178 * flip-done completion events and that unmasking all planes at once
11179 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11180 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11181 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011182 if (engine->id == RCS) {
11183 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11184 intel_ring_emit_reg(engine, DERRMR);
11185 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11186 DERRMR_PIPEB_PRI_FLIP_DONE |
11187 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011188 if (IS_GEN8(dev))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011189 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011190 MI_SRM_LRM_GLOBAL_GTT);
11191 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011192 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011193 MI_SRM_LRM_GLOBAL_GTT);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011194 intel_ring_emit_reg(engine, DERRMR);
11195 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011196 if (IS_GEN8(dev)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011197 intel_ring_emit(engine, 0);
11198 intel_ring_emit(engine, MI_NOOP);
Damien Lespiauf4768282014-04-07 20:24:34 +010011199 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011200 }
11201
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011202 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11203 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11204 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11205 intel_ring_emit(engine, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011206
Chris Wilson60426392015-10-10 10:44:32 +010011207 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011208 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011209}
11210
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011211static bool use_mmio_flip(struct intel_engine_cs *engine,
Sourab Gupta84c33a62014-06-02 16:47:17 +053011212 struct drm_i915_gem_object *obj)
11213{
11214 /*
11215 * This is not being used for older platforms, because
11216 * non-availability of flip done interrupt forces us to use
11217 * CS flips. Older platforms derive flip done using some clever
11218 * tricks involving the flip_pending status bits and vblank irqs.
11219 * So using MMIO flips there would disrupt this mechanism.
11220 */
11221
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011222 if (engine == NULL)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011223 return true;
11224
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011225 if (INTEL_INFO(engine->dev)->gen < 5)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011226 return false;
11227
11228 if (i915.use_mmio_flip < 0)
11229 return false;
11230 else if (i915.use_mmio_flip > 0)
11231 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011232 else if (i915.enable_execlists)
11233 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011234 else if (obj->base.dma_buf &&
11235 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11236 false))
11237 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011238 else
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011239 return engine != i915_gem_request_get_engine(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011240}
11241
Chris Wilson60426392015-10-10 10:44:32 +010011242static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011243 unsigned int rotation,
Chris Wilson60426392015-10-10 10:44:32 +010011244 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011245{
11246 struct drm_device *dev = intel_crtc->base.dev;
11247 struct drm_i915_private *dev_priv = dev->dev_private;
11248 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011249 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011250 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011251
11252 ctl = I915_READ(PLANE_CTL(pipe, 0));
11253 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011254 switch (fb->modifier[0]) {
11255 case DRM_FORMAT_MOD_NONE:
11256 break;
11257 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011258 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011259 break;
11260 case I915_FORMAT_MOD_Y_TILED:
11261 ctl |= PLANE_CTL_TILED_Y;
11262 break;
11263 case I915_FORMAT_MOD_Yf_TILED:
11264 ctl |= PLANE_CTL_TILED_YF;
11265 break;
11266 default:
11267 MISSING_CASE(fb->modifier[0]);
11268 }
Damien Lespiauff944562014-11-20 14:58:16 +000011269
11270 /*
11271 * The stride is either expressed as a multiple of 64 bytes chunks for
11272 * linear buffers or in number of tiles for tiled buffers.
11273 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011274 if (intel_rotation_90_or_270(rotation)) {
11275 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +020011276 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011277 stride = DIV_ROUND_UP(fb->height, tile_height);
11278 } else {
11279 stride = fb->pitches[0] /
Ville Syrjälä7b49f942016-01-12 21:08:32 +020011280 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11281 fb->pixel_format);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011282 }
Damien Lespiauff944562014-11-20 14:58:16 +000011283
11284 /*
11285 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11286 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11287 */
11288 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11289 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11290
Chris Wilson60426392015-10-10 10:44:32 +010011291 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011292 POSTING_READ(PLANE_SURF(pipe, 0));
11293}
11294
Chris Wilson60426392015-10-10 10:44:32 +010011295static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11296 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011297{
11298 struct drm_device *dev = intel_crtc->base.dev;
11299 struct drm_i915_private *dev_priv = dev->dev_private;
11300 struct intel_framebuffer *intel_fb =
11301 to_intel_framebuffer(intel_crtc->base.primary->fb);
11302 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011303 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011304 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011305
Sourab Gupta84c33a62014-06-02 16:47:17 +053011306 dspcntr = I915_READ(reg);
11307
Damien Lespiauc5d97472014-10-25 00:11:11 +010011308 if (obj->tiling_mode != I915_TILING_NONE)
11309 dspcntr |= DISPPLANE_TILED;
11310 else
11311 dspcntr &= ~DISPPLANE_TILED;
11312
Sourab Gupta84c33a62014-06-02 16:47:17 +053011313 I915_WRITE(reg, dspcntr);
11314
Chris Wilson60426392015-10-10 10:44:32 +010011315 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011316 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011317}
11318
11319/*
11320 * XXX: This is the temporary way to update the plane registers until we get
11321 * around to using the usual plane update functions for MMIO flips
11322 */
Chris Wilson60426392015-10-10 10:44:32 +010011323static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011324{
Chris Wilson60426392015-10-10 10:44:32 +010011325 struct intel_crtc *crtc = mmio_flip->crtc;
11326 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011327
Chris Wilson60426392015-10-10 10:44:32 +010011328 spin_lock_irq(&crtc->base.dev->event_lock);
11329 work = crtc->unpin_work;
11330 spin_unlock_irq(&crtc->base.dev->event_lock);
11331 if (work == NULL)
11332 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011333
Chris Wilson60426392015-10-10 10:44:32 +010011334 intel_mark_page_flip_active(work);
Damien Lespiauff944562014-11-20 14:58:16 +000011335
Chris Wilson60426392015-10-10 10:44:32 +010011336 intel_pipe_update_start(crtc);
11337
11338 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011339 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011340 else
11341 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011342 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011343
Chris Wilson60426392015-10-10 10:44:32 +010011344 intel_pipe_update_end(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011345}
11346
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011347static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011348{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011349 struct intel_mmio_flip *mmio_flip =
11350 container_of(work, struct intel_mmio_flip, work);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011351 struct intel_framebuffer *intel_fb =
11352 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11353 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011354
Chris Wilson60426392015-10-10 10:44:32 +010011355 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011356 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011357 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011358 false, NULL,
11359 &mmio_flip->i915->rps.mmioflips));
Chris Wilson60426392015-10-10 10:44:32 +010011360 i915_gem_request_unreference__unlocked(mmio_flip->req);
11361 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011362
Alex Goinsfd8e0582015-11-25 18:43:38 -080011363 /* For framebuffer backed by dmabuf, wait for fence */
11364 if (obj->base.dma_buf)
11365 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11366 false, false,
11367 MAX_SCHEDULE_TIMEOUT) < 0);
11368
Chris Wilson60426392015-10-10 10:44:32 +010011369 intel_do_mmio_flip(mmio_flip);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011370 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011371}
11372
11373static int intel_queue_mmio_flip(struct drm_device *dev,
11374 struct drm_crtc *crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011375 struct drm_i915_gem_object *obj)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011376{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011377 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011378
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011379 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11380 if (mmio_flip == NULL)
11381 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011382
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011383 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011384 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011385 mmio_flip->crtc = to_intel_crtc(crtc);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011386 mmio_flip->rotation = crtc->primary->state->rotation;
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011387
11388 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11389 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011390
Sourab Gupta84c33a62014-06-02 16:47:17 +053011391 return 0;
11392}
11393
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011394static int intel_default_queue_flip(struct drm_device *dev,
11395 struct drm_crtc *crtc,
11396 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011397 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011398 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011399 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011400{
11401 return -ENODEV;
11402}
11403
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011404static bool __intel_pageflip_stall_check(struct drm_device *dev,
11405 struct drm_crtc *crtc)
11406{
11407 struct drm_i915_private *dev_priv = dev->dev_private;
11408 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11409 struct intel_unpin_work *work = intel_crtc->unpin_work;
11410 u32 addr;
11411
11412 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11413 return true;
11414
Chris Wilson908565c2015-08-12 13:08:22 +010011415 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11416 return false;
11417
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011418 if (!work->enable_stall_check)
11419 return false;
11420
11421 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011422 if (work->flip_queued_req &&
11423 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011424 return false;
11425
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011426 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011427 }
11428
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011429 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011430 return false;
11431
11432 /* Potential stall - if we see that the flip has happened,
11433 * assume a missed interrupt. */
11434 if (INTEL_INFO(dev)->gen >= 4)
11435 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11436 else
11437 addr = I915_READ(DSPADDR(intel_crtc->plane));
11438
11439 /* There is a potential issue here with a false positive after a flip
11440 * to the same address. We could address this by checking for a
11441 * non-incrementing frame counter.
11442 */
11443 return addr == work->gtt_offset;
11444}
11445
11446void intel_check_page_flip(struct drm_device *dev, int pipe)
11447{
11448 struct drm_i915_private *dev_priv = dev->dev_private;
11449 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11450 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011451 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011452
Dave Gordon6c51d462015-03-06 15:34:26 +000011453 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011454
11455 if (crtc == NULL)
11456 return;
11457
Daniel Vetterf3260382014-09-15 14:55:23 +020011458 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011459 work = intel_crtc->unpin_work;
11460 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011461 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011462 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011463 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011464 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011465 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011466 if (work != NULL &&
11467 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11468 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011469 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011470}
11471
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011472static int intel_crtc_page_flip(struct drm_crtc *crtc,
11473 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011474 struct drm_pending_vblank_event *event,
11475 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011476{
11477 struct drm_device *dev = crtc->dev;
11478 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011479 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011480 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011481 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011482 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011483 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011484 struct intel_unpin_work *work;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011485 struct intel_engine_cs *engine;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011486 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011487 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011488 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011489
Matt Roper2ff8fde2014-07-08 07:50:07 -070011490 /*
11491 * drm_mode_page_flip_ioctl() should already catch this, but double
11492 * check to be safe. In the future we may enable pageflipping from
11493 * a disabled primary plane.
11494 */
11495 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11496 return -EBUSY;
11497
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011498 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011499 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011500 return -EINVAL;
11501
11502 /*
11503 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11504 * Note that pitch changes could also affect these register.
11505 */
11506 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011507 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11508 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011509 return -EINVAL;
11510
Chris Wilsonf900db42014-02-20 09:26:13 +000011511 if (i915_terminally_wedged(&dev_priv->gpu_error))
11512 goto out_hang;
11513
Daniel Vetterb14c5672013-09-19 12:18:32 +020011514 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011515 if (work == NULL)
11516 return -ENOMEM;
11517
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011518 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011519 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011520 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011521 INIT_WORK(&work->work, intel_unpin_work_fn);
11522
Daniel Vetter87b6b102014-05-15 15:33:46 +020011523 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011524 if (ret)
11525 goto free_work;
11526
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011527 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011528 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011529 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011530 /* Before declaring the flip queue wedged, check if
11531 * the hardware completed the operation behind our backs.
11532 */
11533 if (__intel_pageflip_stall_check(dev, crtc)) {
11534 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11535 page_flip_completed(intel_crtc);
11536 } else {
11537 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011538 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011539
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011540 drm_crtc_vblank_put(crtc);
11541 kfree(work);
11542 return -EBUSY;
11543 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011544 }
11545 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011546 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011547
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011548 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11549 flush_workqueue(dev_priv->wq);
11550
Jesse Barnes75dfca82010-02-10 15:09:44 -080011551 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011552 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011553 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011554
Matt Roperf4510a22014-04-01 15:22:40 -070011555 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011556 update_state_fb(crtc->primary);
Paulo Zanonie8216e52016-01-19 11:35:56 -020011557 intel_fbc_pre_update(intel_crtc);
Matt Roper1ed1f962015-01-30 16:22:36 -080011558
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011559 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011560
Chris Wilson89ed88b2015-02-16 14:31:49 +000011561 ret = i915_mutex_lock_interruptible(dev);
11562 if (ret)
11563 goto cleanup;
11564
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011565 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011566 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011567
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011568 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030011569 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011570
Wayne Boyer666a4532015-12-09 12:29:35 -080011571 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011572 engine = &dev_priv->engine[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011573 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011574 /* vlv: DISPLAY_FLIP fails to change tiling */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011575 engine = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011576 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011577 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011578 } else if (INTEL_INFO(dev)->gen >= 7) {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011579 engine = i915_gem_request_get_engine(obj->last_write_req);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011580 if (engine == NULL || engine->id != RCS)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011581 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011582 } else {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011583 engine = &dev_priv->engine[RCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011584 }
11585
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011586 mmio_flip = use_mmio_flip(engine, obj);
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011587
11588 /* When using CS flips, we want to emit semaphores between rings.
11589 * However, when using mmio flips we will create a task to do the
11590 * synchronisation, so all we want here is to pin the framebuffer
11591 * into the display plane and skip any waits.
11592 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011593 if (!mmio_flip) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011594 ret = i915_gem_object_sync(obj, engine, &request);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011595 if (ret)
11596 goto cleanup_pending;
11597 }
11598
Ville Syrjälä3465c582016-02-15 22:54:43 +020011599 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011600 if (ret)
11601 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011602
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011603 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11604 obj, 0);
11605 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011606
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011607 if (mmio_flip) {
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011608 ret = intel_queue_mmio_flip(dev, crtc, obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011609 if (ret)
11610 goto cleanup_unpin;
11611
John Harrisonf06cc1b2014-11-24 18:49:37 +000011612 i915_gem_request_assign(&work->flip_queued_req,
11613 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011614 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011615 if (!request) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011616 request = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +000011617 if (IS_ERR(request)) {
11618 ret = PTR_ERR(request);
John Harrison6258fbe2015-05-29 17:43:48 +010011619 goto cleanup_unpin;
Dave Gordon26827082016-01-19 19:02:53 +000011620 }
John Harrison6258fbe2015-05-29 17:43:48 +010011621 }
11622
11623 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011624 page_flip_flags);
11625 if (ret)
11626 goto cleanup_unpin;
11627
John Harrison6258fbe2015-05-29 17:43:48 +010011628 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011629 }
11630
John Harrison91af1272015-06-18 13:14:56 +010011631 if (request)
John Harrison75289872015-05-29 17:43:49 +010011632 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011633
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011634 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011635 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011636
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011637 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011638 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011639 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011640
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011641 intel_frontbuffer_flip_prepare(dev,
11642 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011643
Jesse Barnese5510fa2010-07-01 16:48:37 -070011644 trace_i915_flip_request(intel_crtc->plane, obj);
11645
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011646 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011647
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011648cleanup_unpin:
Ville Syrjälä3465c582016-02-15 22:54:43 +020011649 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011650cleanup_pending:
Dave Gordon0aa498d2016-01-28 10:48:09 +000011651 if (!IS_ERR_OR_NULL(request))
John Harrison91af1272015-06-18 13:14:56 +010011652 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011653 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011654 mutex_unlock(&dev->struct_mutex);
11655cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011656 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011657 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011658
Chris Wilson89ed88b2015-02-16 14:31:49 +000011659 drm_gem_object_unreference_unlocked(&obj->base);
11660 drm_framebuffer_unreference(work->old_fb);
11661
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011662 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011663 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011664 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011665
Daniel Vetter87b6b102014-05-15 15:33:46 +020011666 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011667free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011668 kfree(work);
11669
Chris Wilsonf900db42014-02-20 09:26:13 +000011670 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011671 struct drm_atomic_state *state;
11672 struct drm_plane_state *plane_state;
11673
Chris Wilsonf900db42014-02-20 09:26:13 +000011674out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011675 state = drm_atomic_state_alloc(dev);
11676 if (!state)
11677 return -ENOMEM;
11678 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11679
11680retry:
11681 plane_state = drm_atomic_get_plane_state(state, primary);
11682 ret = PTR_ERR_OR_ZERO(plane_state);
11683 if (!ret) {
11684 drm_atomic_set_fb_for_plane(plane_state, fb);
11685
11686 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11687 if (!ret)
11688 ret = drm_atomic_commit(state);
11689 }
11690
11691 if (ret == -EDEADLK) {
11692 drm_modeset_backoff(state->acquire_ctx);
11693 drm_atomic_state_clear(state);
11694 goto retry;
11695 }
11696
11697 if (ret)
11698 drm_atomic_state_free(state);
11699
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011700 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011701 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011702 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011703 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011704 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011705 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011706 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011707}
11708
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011709
11710/**
11711 * intel_wm_need_update - Check whether watermarks need updating
11712 * @plane: drm plane
11713 * @state: new plane state
11714 *
11715 * Check current plane state versus the new one to determine whether
11716 * watermarks need to be recalculated.
11717 *
11718 * Returns true or false.
11719 */
11720static bool intel_wm_need_update(struct drm_plane *plane,
11721 struct drm_plane_state *state)
11722{
Matt Roperd21fbe82015-09-24 15:53:12 -070011723 struct intel_plane_state *new = to_intel_plane_state(state);
11724 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11725
11726 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011727 if (new->visible != cur->visible)
11728 return true;
11729
11730 if (!cur->base.fb || !new->base.fb)
11731 return false;
11732
11733 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11734 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011735 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11736 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11737 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11738 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011739 return true;
11740
11741 return false;
11742}
11743
Matt Roperd21fbe82015-09-24 15:53:12 -070011744static bool needs_scaling(struct intel_plane_state *state)
11745{
11746 int src_w = drm_rect_width(&state->src) >> 16;
11747 int src_h = drm_rect_height(&state->src) >> 16;
11748 int dst_w = drm_rect_width(&state->dst);
11749 int dst_h = drm_rect_height(&state->dst);
11750
11751 return (src_w != dst_w || src_h != dst_h);
11752}
11753
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011754int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11755 struct drm_plane_state *plane_state)
11756{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011757 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011758 struct drm_crtc *crtc = crtc_state->crtc;
11759 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11760 struct drm_plane *plane = plane_state->plane;
11761 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011762 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011763 struct intel_plane_state *old_plane_state =
11764 to_intel_plane_state(plane->state);
11765 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011766 bool mode_changed = needs_modeset(crtc_state);
11767 bool was_crtc_enabled = crtc->state->active;
11768 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011769 bool turn_off, turn_on, visible, was_visible;
11770 struct drm_framebuffer *fb = plane_state->fb;
11771
11772 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11773 plane->type != DRM_PLANE_TYPE_CURSOR) {
11774 ret = skl_update_scaler_plane(
11775 to_intel_crtc_state(crtc_state),
11776 to_intel_plane_state(plane_state));
11777 if (ret)
11778 return ret;
11779 }
11780
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011781 was_visible = old_plane_state->visible;
11782 visible = to_intel_plane_state(plane_state)->visible;
11783
11784 if (!was_crtc_enabled && WARN_ON(was_visible))
11785 was_visible = false;
11786
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011787 /*
11788 * Visibility is calculated as if the crtc was on, but
11789 * after scaler setup everything depends on it being off
11790 * when the crtc isn't active.
11791 */
11792 if (!is_crtc_enabled)
11793 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011794
11795 if (!was_visible && !visible)
11796 return 0;
11797
Maarten Lankhorste8861672016-02-24 11:24:26 +010011798 if (fb != old_plane_state->base.fb)
11799 pipe_config->fb_changed = true;
11800
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011801 turn_off = was_visible && (!visible || mode_changed);
11802 turn_on = visible && (!was_visible || mode_changed);
11803
11804 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11805 plane->base.id, fb ? fb->base.id : -1);
11806
11807 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11808 plane->base.id, was_visible, visible,
11809 turn_off, turn_on, mode_changed);
11810
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011811 if (turn_on) {
11812 pipe_config->update_wm_pre = true;
11813
11814 /* must disable cxsr around plane enable/disable */
11815 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11816 pipe_config->disable_cxsr = true;
11817 } else if (turn_off) {
11818 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011819
Ville Syrjälä852eb002015-06-24 22:00:07 +030011820 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011821 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011822 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011823 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011824 /* FIXME bollocks */
11825 pipe_config->update_wm_pre = true;
11826 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011827 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011828
Matt Ropered4a6a72016-02-23 17:20:13 -080011829 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011830 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11831 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011832 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11833
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011834 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011835 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011836
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011837 /*
11838 * WaCxSRDisabledForSpriteScaling:ivb
11839 *
11840 * cstate->update_wm was already set above, so this flag will
11841 * take effect when we commit and program watermarks.
11842 */
11843 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11844 needs_scaling(to_intel_plane_state(plane_state)) &&
11845 !needs_scaling(old_plane_state))
11846 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011847
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011848 return 0;
11849}
11850
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011851static bool encoders_cloneable(const struct intel_encoder *a,
11852 const struct intel_encoder *b)
11853{
11854 /* masks could be asymmetric, so check both ways */
11855 return a == b || (a->cloneable & (1 << b->type) &&
11856 b->cloneable & (1 << a->type));
11857}
11858
11859static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11860 struct intel_crtc *crtc,
11861 struct intel_encoder *encoder)
11862{
11863 struct intel_encoder *source_encoder;
11864 struct drm_connector *connector;
11865 struct drm_connector_state *connector_state;
11866 int i;
11867
11868 for_each_connector_in_state(state, connector, connector_state, i) {
11869 if (connector_state->crtc != &crtc->base)
11870 continue;
11871
11872 source_encoder =
11873 to_intel_encoder(connector_state->best_encoder);
11874 if (!encoders_cloneable(encoder, source_encoder))
11875 return false;
11876 }
11877
11878 return true;
11879}
11880
11881static bool check_encoder_cloning(struct drm_atomic_state *state,
11882 struct intel_crtc *crtc)
11883{
11884 struct intel_encoder *encoder;
11885 struct drm_connector *connector;
11886 struct drm_connector_state *connector_state;
11887 int i;
11888
11889 for_each_connector_in_state(state, connector, connector_state, i) {
11890 if (connector_state->crtc != &crtc->base)
11891 continue;
11892
11893 encoder = to_intel_encoder(connector_state->best_encoder);
11894 if (!check_single_encoder_cloning(state, crtc, encoder))
11895 return false;
11896 }
11897
11898 return true;
11899}
11900
11901static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11902 struct drm_crtc_state *crtc_state)
11903{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011904 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011905 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011906 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011907 struct intel_crtc_state *pipe_config =
11908 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011909 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011910 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011911 bool mode_changed = needs_modeset(crtc_state);
11912
11913 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11914 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11915 return -EINVAL;
11916 }
11917
Ville Syrjälä852eb002015-06-24 22:00:07 +030011918 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011919 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011920
Maarten Lankhorstad421372015-06-15 12:33:42 +020011921 if (mode_changed && crtc_state->enable &&
11922 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011923 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020011924 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11925 pipe_config);
11926 if (ret)
11927 return ret;
11928 }
11929
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000011930 if (crtc_state->color_mgmt_changed) {
11931 ret = intel_color_check(crtc, crtc_state);
11932 if (ret)
11933 return ret;
11934 }
11935
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011936 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011937 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010011938 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080011939 if (ret) {
11940 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070011941 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080011942 }
11943 }
11944
11945 if (dev_priv->display.compute_intermediate_wm &&
11946 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11947 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11948 return 0;
11949
11950 /*
11951 * Calculate 'intermediate' watermarks that satisfy both the
11952 * old state and the new state. We can program these
11953 * immediately.
11954 */
11955 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11956 intel_crtc,
11957 pipe_config);
11958 if (ret) {
11959 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
11960 return ret;
11961 }
Matt Roper86c8bbb2015-09-24 15:53:16 -070011962 }
11963
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011964 if (INTEL_INFO(dev)->gen >= 9) {
11965 if (mode_changed)
11966 ret = skl_update_scaler_crtc(pipe_config);
11967
11968 if (!ret)
11969 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11970 pipe_config);
11971 }
11972
11973 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011974}
11975
Jani Nikula65b38e02015-04-13 11:26:56 +030011976static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011977 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Matt Roperea2c67b2014-12-23 10:41:52 -080011978 .atomic_begin = intel_begin_crtc_commit,
11979 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011980 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011981};
11982
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011983static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11984{
11985 struct intel_connector *connector;
11986
11987 for_each_intel_connector(dev, connector) {
11988 if (connector->base.encoder) {
11989 connector->base.state->best_encoder =
11990 connector->base.encoder;
11991 connector->base.state->crtc =
11992 connector->base.encoder->crtc;
11993 } else {
11994 connector->base.state->best_encoder = NULL;
11995 connector->base.state->crtc = NULL;
11996 }
11997 }
11998}
11999
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012000static void
Robin Schroereba905b2014-05-18 02:24:50 +020012001connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012002 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012003{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012004 int bpp = pipe_config->pipe_bpp;
12005
12006 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12007 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012008 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012009
12010 /* Don't use an invalid EDID bpc value */
12011 if (connector->base.display_info.bpc &&
12012 connector->base.display_info.bpc * 3 < bpp) {
12013 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12014 bpp, connector->base.display_info.bpc*3);
12015 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12016 }
12017
Jani Nikula013dd9e2016-01-13 16:35:20 +020012018 /* Clamp bpp to default limit on screens without EDID 1.4 */
12019 if (connector->base.display_info.bpc == 0) {
12020 int type = connector->base.connector_type;
12021 int clamp_bpp = 24;
12022
12023 /* Fall back to 18 bpp when DP sink capability is unknown. */
12024 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12025 type == DRM_MODE_CONNECTOR_eDP)
12026 clamp_bpp = 18;
12027
12028 if (bpp > clamp_bpp) {
12029 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12030 bpp, clamp_bpp);
12031 pipe_config->pipe_bpp = clamp_bpp;
12032 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012033 }
12034}
12035
12036static int
12037compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012038 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012039{
12040 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012041 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012042 struct drm_connector *connector;
12043 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012044 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012045
Wayne Boyer666a4532015-12-09 12:29:35 -080012046 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012047 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012048 else if (INTEL_INFO(dev)->gen >= 5)
12049 bpp = 12*3;
12050 else
12051 bpp = 8*3;
12052
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012053
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012054 pipe_config->pipe_bpp = bpp;
12055
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012056 state = pipe_config->base.state;
12057
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012058 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012059 for_each_connector_in_state(state, connector, connector_state, i) {
12060 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012061 continue;
12062
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012063 connected_sink_compute_bpp(to_intel_connector(connector),
12064 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012065 }
12066
12067 return bpp;
12068}
12069
Daniel Vetter644db712013-09-19 14:53:58 +020012070static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12071{
12072 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12073 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012074 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012075 mode->crtc_hdisplay, mode->crtc_hsync_start,
12076 mode->crtc_hsync_end, mode->crtc_htotal,
12077 mode->crtc_vdisplay, mode->crtc_vsync_start,
12078 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12079}
12080
Daniel Vetterc0b03412013-05-28 12:05:54 +020012081static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012082 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012083 const char *context)
12084{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012085 struct drm_device *dev = crtc->base.dev;
12086 struct drm_plane *plane;
12087 struct intel_plane *intel_plane;
12088 struct intel_plane_state *state;
12089 struct drm_framebuffer *fb;
12090
12091 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12092 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012093
Jani Nikulada205632016-03-15 21:51:10 +020012094 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012095 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12096 pipe_config->pipe_bpp, pipe_config->dither);
12097 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12098 pipe_config->has_pch_encoder,
12099 pipe_config->fdi_lanes,
12100 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12101 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12102 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012103 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 +030012104 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012105 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012106 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12107 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12108 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012109
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012110 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 -070012111 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012112 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012113 pipe_config->dp_m2_n2.gmch_m,
12114 pipe_config->dp_m2_n2.gmch_n,
12115 pipe_config->dp_m2_n2.link_m,
12116 pipe_config->dp_m2_n2.link_n,
12117 pipe_config->dp_m2_n2.tu);
12118
Daniel Vetter55072d12014-11-20 16:10:28 +010012119 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12120 pipe_config->has_audio,
12121 pipe_config->has_infoframe);
12122
Daniel Vetterc0b03412013-05-28 12:05:54 +020012123 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012124 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012125 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012126 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12127 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012128 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012129 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12130 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012131 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12132 crtc->num_scalers,
12133 pipe_config->scaler_state.scaler_users,
12134 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012135 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12136 pipe_config->gmch_pfit.control,
12137 pipe_config->gmch_pfit.pgm_ratios,
12138 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012139 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012140 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012141 pipe_config->pch_pfit.size,
12142 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012143 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012144 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012145
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012146 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012147 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012148 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012149 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012150 pipe_config->ddi_pll_sel,
12151 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012152 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012153 pipe_config->dpll_hw_state.pll0,
12154 pipe_config->dpll_hw_state.pll1,
12155 pipe_config->dpll_hw_state.pll2,
12156 pipe_config->dpll_hw_state.pll3,
12157 pipe_config->dpll_hw_state.pll6,
12158 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012159 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012160 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012161 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012162 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012163 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12164 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12165 pipe_config->ddi_pll_sel,
12166 pipe_config->dpll_hw_state.ctrl1,
12167 pipe_config->dpll_hw_state.cfgcr1,
12168 pipe_config->dpll_hw_state.cfgcr2);
12169 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012170 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 +010012171 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012172 pipe_config->dpll_hw_state.wrpll,
12173 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012174 } else {
12175 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12176 "fp0: 0x%x, fp1: 0x%x\n",
12177 pipe_config->dpll_hw_state.dpll,
12178 pipe_config->dpll_hw_state.dpll_md,
12179 pipe_config->dpll_hw_state.fp0,
12180 pipe_config->dpll_hw_state.fp1);
12181 }
12182
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012183 DRM_DEBUG_KMS("planes on this crtc\n");
12184 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12185 intel_plane = to_intel_plane(plane);
12186 if (intel_plane->pipe != crtc->pipe)
12187 continue;
12188
12189 state = to_intel_plane_state(plane->state);
12190 fb = state->base.fb;
12191 if (!fb) {
12192 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12193 "disabled, scaler_id = %d\n",
12194 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12195 plane->base.id, intel_plane->pipe,
12196 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12197 drm_plane_index(plane), state->scaler_id);
12198 continue;
12199 }
12200
12201 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12202 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12203 plane->base.id, intel_plane->pipe,
12204 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12205 drm_plane_index(plane));
12206 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12207 fb->base.id, fb->width, fb->height, fb->pixel_format);
12208 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12209 state->scaler_id,
12210 state->src.x1 >> 16, state->src.y1 >> 16,
12211 drm_rect_width(&state->src) >> 16,
12212 drm_rect_height(&state->src) >> 16,
12213 state->dst.x1, state->dst.y1,
12214 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12215 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012216}
12217
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012218static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012219{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012220 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012221 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012222 unsigned int used_ports = 0;
12223
12224 /*
12225 * Walk the connector list instead of the encoder
12226 * list to detect the problem on ddi platforms
12227 * where there's just one encoder per digital port.
12228 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012229 drm_for_each_connector(connector, dev) {
12230 struct drm_connector_state *connector_state;
12231 struct intel_encoder *encoder;
12232
12233 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12234 if (!connector_state)
12235 connector_state = connector->state;
12236
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012237 if (!connector_state->best_encoder)
12238 continue;
12239
12240 encoder = to_intel_encoder(connector_state->best_encoder);
12241
12242 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012243
12244 switch (encoder->type) {
12245 unsigned int port_mask;
12246 case INTEL_OUTPUT_UNKNOWN:
12247 if (WARN_ON(!HAS_DDI(dev)))
12248 break;
12249 case INTEL_OUTPUT_DISPLAYPORT:
12250 case INTEL_OUTPUT_HDMI:
12251 case INTEL_OUTPUT_EDP:
12252 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12253
12254 /* the same port mustn't appear more than once */
12255 if (used_ports & port_mask)
12256 return false;
12257
12258 used_ports |= port_mask;
12259 default:
12260 break;
12261 }
12262 }
12263
12264 return true;
12265}
12266
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012267static void
12268clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12269{
12270 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012271 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012272 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012273 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012274 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012275 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012276
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012277 /* FIXME: before the switch to atomic started, a new pipe_config was
12278 * kzalloc'd. Code that depends on any field being zero should be
12279 * fixed, so that the crtc_state can be safely duplicated. For now,
12280 * only fields that are know to not cause problems are preserved. */
12281
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012282 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012283 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012284 shared_dpll = crtc_state->shared_dpll;
12285 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012286 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012287 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012288
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012289 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012290
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012291 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012292 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012293 crtc_state->shared_dpll = shared_dpll;
12294 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012295 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012296 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012297}
12298
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012299static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012300intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012301 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012302{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012303 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012304 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012305 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012306 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012307 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012308 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012309 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012310
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012311 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012312
Daniel Vettere143a212013-07-04 12:01:15 +020012313 pipe_config->cpu_transcoder =
12314 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012315
Imre Deak2960bc92013-07-30 13:36:32 +030012316 /*
12317 * Sanitize sync polarity flags based on requested ones. If neither
12318 * positive or negative polarity is requested, treat this as meaning
12319 * negative polarity.
12320 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012321 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012322 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012323 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012324
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012325 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012326 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012327 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012328
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012329 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12330 pipe_config);
12331 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012332 goto fail;
12333
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012334 /*
12335 * Determine the real pipe dimensions. Note that stereo modes can
12336 * increase the actual pipe size due to the frame doubling and
12337 * insertion of additional space for blanks between the frame. This
12338 * is stored in the crtc timings. We use the requested mode to do this
12339 * computation to clearly distinguish it from the adjusted mode, which
12340 * can be changed by the connectors in the below retry loop.
12341 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012342 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012343 &pipe_config->pipe_src_w,
12344 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012345
Daniel Vettere29c22c2013-02-21 00:00:16 +010012346encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012347 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012348 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012349 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012350
Daniel Vetter135c81b2013-07-21 21:37:09 +020012351 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012352 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12353 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012354
Daniel Vetter7758a112012-07-08 19:40:39 +020012355 /* Pass our mode to the connectors and the CRTC to give them a chance to
12356 * adjust it according to limitations or connector properties, and also
12357 * a chance to reject the mode entirely.
12358 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012359 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012360 if (connector_state->crtc != crtc)
12361 continue;
12362
12363 encoder = to_intel_encoder(connector_state->best_encoder);
12364
Daniel Vetterefea6e82013-07-21 21:36:59 +020012365 if (!(encoder->compute_config(encoder, pipe_config))) {
12366 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012367 goto fail;
12368 }
12369 }
12370
Daniel Vetterff9a6752013-06-01 17:16:21 +020012371 /* Set default port clock if not overwritten by the encoder. Needs to be
12372 * done afterwards in case the encoder adjusts the mode. */
12373 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012374 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012375 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012376
Daniel Vettera43f6e02013-06-07 23:10:32 +020012377 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012378 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012379 DRM_DEBUG_KMS("CRTC fixup failed\n");
12380 goto fail;
12381 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012382
12383 if (ret == RETRY) {
12384 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12385 ret = -EINVAL;
12386 goto fail;
12387 }
12388
12389 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12390 retry = false;
12391 goto encoder_retry;
12392 }
12393
Daniel Vettere8fa4272015-08-12 11:43:34 +020012394 /* Dithering seems to not pass-through bits correctly when it should, so
12395 * only enable it on 6bpc panels. */
12396 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012397 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012398 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012399
Daniel Vetter7758a112012-07-08 19:40:39 +020012400fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012401 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012402}
12403
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012404static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012405intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012406{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012407 struct drm_crtc *crtc;
12408 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012409 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012410
Ville Syrjälä76688512014-01-10 11:28:06 +020012411 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012412 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012413 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012414
12415 /* Update hwmode for vblank functions */
12416 if (crtc->state->active)
12417 crtc->hwmode = crtc->state->adjusted_mode;
12418 else
12419 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012420
12421 /*
12422 * Update legacy state to satisfy fbc code. This can
12423 * be removed when fbc uses the atomic state.
12424 */
12425 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12426 struct drm_plane_state *plane_state = crtc->primary->state;
12427
12428 crtc->primary->fb = plane_state->fb;
12429 crtc->x = plane_state->src_x >> 16;
12430 crtc->y = plane_state->src_y >> 16;
12431 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012432 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012433}
12434
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012435static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012436{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012437 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012438
12439 if (clock1 == clock2)
12440 return true;
12441
12442 if (!clock1 || !clock2)
12443 return false;
12444
12445 diff = abs(clock1 - clock2);
12446
12447 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12448 return true;
12449
12450 return false;
12451}
12452
Daniel Vetter25c5b262012-07-08 22:08:04 +020012453#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12454 list_for_each_entry((intel_crtc), \
12455 &(dev)->mode_config.crtc_list, \
12456 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012457 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012458
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012459static bool
12460intel_compare_m_n(unsigned int m, unsigned int n,
12461 unsigned int m2, unsigned int n2,
12462 bool exact)
12463{
12464 if (m == m2 && n == n2)
12465 return true;
12466
12467 if (exact || !m || !n || !m2 || !n2)
12468 return false;
12469
12470 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12471
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012472 if (n > n2) {
12473 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012474 m2 <<= 1;
12475 n2 <<= 1;
12476 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012477 } else if (n < n2) {
12478 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012479 m <<= 1;
12480 n <<= 1;
12481 }
12482 }
12483
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012484 if (n != n2)
12485 return false;
12486
12487 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012488}
12489
12490static bool
12491intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12492 struct intel_link_m_n *m2_n2,
12493 bool adjust)
12494{
12495 if (m_n->tu == m2_n2->tu &&
12496 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12497 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12498 intel_compare_m_n(m_n->link_m, m_n->link_n,
12499 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12500 if (adjust)
12501 *m2_n2 = *m_n;
12502
12503 return true;
12504 }
12505
12506 return false;
12507}
12508
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012509static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012510intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012511 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012512 struct intel_crtc_state *pipe_config,
12513 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012514{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012515 bool ret = true;
12516
12517#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12518 do { \
12519 if (!adjust) \
12520 DRM_ERROR(fmt, ##__VA_ARGS__); \
12521 else \
12522 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12523 } while (0)
12524
Daniel Vetter66e985c2013-06-05 13:34:20 +020012525#define PIPE_CONF_CHECK_X(name) \
12526 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012527 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012528 "(expected 0x%08x, found 0x%08x)\n", \
12529 current_config->name, \
12530 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012531 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012532 }
12533
Daniel Vetter08a24032013-04-19 11:25:34 +020012534#define PIPE_CONF_CHECK_I(name) \
12535 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012536 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012537 "(expected %i, found %i)\n", \
12538 current_config->name, \
12539 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012540 ret = false; \
12541 }
12542
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012543#define PIPE_CONF_CHECK_P(name) \
12544 if (current_config->name != pipe_config->name) { \
12545 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12546 "(expected %p, found %p)\n", \
12547 current_config->name, \
12548 pipe_config->name); \
12549 ret = false; \
12550 }
12551
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012552#define PIPE_CONF_CHECK_M_N(name) \
12553 if (!intel_compare_link_m_n(&current_config->name, \
12554 &pipe_config->name,\
12555 adjust)) { \
12556 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12557 "(expected tu %i gmch %i/%i link %i/%i, " \
12558 "found tu %i, gmch %i/%i link %i/%i)\n", \
12559 current_config->name.tu, \
12560 current_config->name.gmch_m, \
12561 current_config->name.gmch_n, \
12562 current_config->name.link_m, \
12563 current_config->name.link_n, \
12564 pipe_config->name.tu, \
12565 pipe_config->name.gmch_m, \
12566 pipe_config->name.gmch_n, \
12567 pipe_config->name.link_m, \
12568 pipe_config->name.link_n); \
12569 ret = false; \
12570 }
12571
Daniel Vetter55c561a2016-03-30 11:34:36 +020012572/* This is required for BDW+ where there is only one set of registers for
12573 * switching between high and low RR.
12574 * This macro can be used whenever a comparison has to be made between one
12575 * hw state and multiple sw state variables.
12576 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012577#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12578 if (!intel_compare_link_m_n(&current_config->name, \
12579 &pipe_config->name, adjust) && \
12580 !intel_compare_link_m_n(&current_config->alt_name, \
12581 &pipe_config->name, adjust)) { \
12582 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12583 "(expected tu %i gmch %i/%i link %i/%i, " \
12584 "or tu %i gmch %i/%i link %i/%i, " \
12585 "found tu %i, gmch %i/%i link %i/%i)\n", \
12586 current_config->name.tu, \
12587 current_config->name.gmch_m, \
12588 current_config->name.gmch_n, \
12589 current_config->name.link_m, \
12590 current_config->name.link_n, \
12591 current_config->alt_name.tu, \
12592 current_config->alt_name.gmch_m, \
12593 current_config->alt_name.gmch_n, \
12594 current_config->alt_name.link_m, \
12595 current_config->alt_name.link_n, \
12596 pipe_config->name.tu, \
12597 pipe_config->name.gmch_m, \
12598 pipe_config->name.gmch_n, \
12599 pipe_config->name.link_m, \
12600 pipe_config->name.link_n); \
12601 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012602 }
12603
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012604#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12605 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012606 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012607 "(expected %i, found %i)\n", \
12608 current_config->name & (mask), \
12609 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012610 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012611 }
12612
Ville Syrjälä5e550652013-09-06 23:29:07 +030012613#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12614 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012615 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012616 "(expected %i, found %i)\n", \
12617 current_config->name, \
12618 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012619 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012620 }
12621
Daniel Vetterbb760062013-06-06 14:55:52 +020012622#define PIPE_CONF_QUIRK(quirk) \
12623 ((current_config->quirks | pipe_config->quirks) & (quirk))
12624
Daniel Vettereccb1402013-05-22 00:50:22 +020012625 PIPE_CONF_CHECK_I(cpu_transcoder);
12626
Daniel Vetter08a24032013-04-19 11:25:34 +020012627 PIPE_CONF_CHECK_I(has_pch_encoder);
12628 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012629 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012630
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012631 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012632 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012633
12634 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012635 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012636
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012637 if (current_config->has_drrs)
12638 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12639 } else
12640 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012641
Jani Nikulaa65347b2015-11-27 12:21:46 +020012642 PIPE_CONF_CHECK_I(has_dsi_encoder);
12643
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012644 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12645 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12646 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12647 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12648 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12649 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012650
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012651 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12652 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12653 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12654 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12655 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12656 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012657
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012658 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020012659 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012660 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012661 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012662 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012663 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012664
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012665 PIPE_CONF_CHECK_I(has_audio);
12666
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012667 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012668 DRM_MODE_FLAG_INTERLACE);
12669
Daniel Vetterbb760062013-06-06 14:55:52 +020012670 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012671 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012672 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012673 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012674 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012675 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012676 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012677 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012678 DRM_MODE_FLAG_NVSYNC);
12679 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012680
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012681 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012682 /* pfit ratios are autocomputed by the hw on gen4+ */
12683 if (INTEL_INFO(dev)->gen < 4)
12684 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012685 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012686
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012687 if (!adjust) {
12688 PIPE_CONF_CHECK_I(pipe_src_w);
12689 PIPE_CONF_CHECK_I(pipe_src_h);
12690
12691 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12692 if (current_config->pch_pfit.enabled) {
12693 PIPE_CONF_CHECK_X(pch_pfit.pos);
12694 PIPE_CONF_CHECK_X(pch_pfit.size);
12695 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012696
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012697 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12698 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012699
Jesse Barnese59150d2014-01-07 13:30:45 -080012700 /* BDW+ don't expose a synchronous way to read the state */
12701 if (IS_HASWELL(dev))
12702 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012703
Ville Syrjälä282740f2013-09-04 18:30:03 +030012704 PIPE_CONF_CHECK_I(double_wide);
12705
Daniel Vetter26804af2014-06-25 22:01:55 +030012706 PIPE_CONF_CHECK_X(ddi_pll_sel);
12707
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012708 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012709 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012710 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012711 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12712 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012713 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012714 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012715 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12716 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12717 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012718
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012719 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12720 PIPE_CONF_CHECK_I(pipe_bpp);
12721
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012722 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012723 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012724
Daniel Vetter66e985c2013-06-05 13:34:20 +020012725#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012726#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012727#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012728#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012729#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012730#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012731#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012732
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012733 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012734}
12735
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012736static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12737 const struct intel_crtc_state *pipe_config)
12738{
12739 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012740 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012741 &pipe_config->fdi_m_n);
12742 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12743
12744 /*
12745 * FDI already provided one idea for the dotclock.
12746 * Yell if the encoder disagrees.
12747 */
12748 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12749 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12750 fdi_dotclock, dotclock);
12751 }
12752}
12753
Damien Lespiau08db6652014-11-04 17:06:52 +000012754static void check_wm_state(struct drm_device *dev)
12755{
12756 struct drm_i915_private *dev_priv = dev->dev_private;
12757 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12758 struct intel_crtc *intel_crtc;
12759 int plane;
12760
12761 if (INTEL_INFO(dev)->gen < 9)
12762 return;
12763
12764 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12765 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12766
12767 for_each_intel_crtc(dev, intel_crtc) {
12768 struct skl_ddb_entry *hw_entry, *sw_entry;
12769 const enum pipe pipe = intel_crtc->pipe;
12770
12771 if (!intel_crtc->active)
12772 continue;
12773
12774 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012775 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012776 hw_entry = &hw_ddb.plane[pipe][plane];
12777 sw_entry = &sw_ddb->plane[pipe][plane];
12778
12779 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12780 continue;
12781
12782 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12783 "(expected (%u,%u), found (%u,%u))\n",
12784 pipe_name(pipe), plane + 1,
12785 sw_entry->start, sw_entry->end,
12786 hw_entry->start, hw_entry->end);
12787 }
12788
12789 /* cursor */
Matt Roper4969d332015-09-24 15:53:10 -070012790 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12791 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
Damien Lespiau08db6652014-11-04 17:06:52 +000012792
12793 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12794 continue;
12795
12796 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12797 "(expected (%u,%u), found (%u,%u))\n",
12798 pipe_name(pipe),
12799 sw_entry->start, sw_entry->end,
12800 hw_entry->start, hw_entry->end);
12801 }
12802}
12803
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012804static void
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012805check_connector_state(struct drm_device *dev,
12806 struct drm_atomic_state *old_state)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012807{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012808 struct drm_connector_state *old_conn_state;
12809 struct drm_connector *connector;
12810 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012811
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012812 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12813 struct drm_encoder *encoder = connector->encoder;
12814 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012815
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012816 /* This also checks the encoder/connector hw state with the
12817 * ->get_hw_state callbacks. */
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012818 intel_connector_check_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012819
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012820 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012821 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012822 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012823}
12824
12825static void
12826check_encoder_state(struct drm_device *dev)
12827{
12828 struct intel_encoder *encoder;
12829 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012830
Damien Lespiaub2784e12014-08-05 11:29:37 +010012831 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012832 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012833 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012834
12835 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12836 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012837 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012838
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012839 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012840 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012841 continue;
12842 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012843
12844 I915_STATE_WARN(connector->base.state->crtc !=
12845 encoder->base.crtc,
12846 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012847 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012848
Rob Clarke2c719b2014-12-15 13:56:32 -050012849 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012850 "encoder's enabled state mismatch "
12851 "(expected %i, found %i)\n",
12852 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012853
12854 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012855 bool active;
12856
12857 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012858 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012859 "encoder detached but still enabled on pipe %c.\n",
12860 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012861 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012862 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012863}
12864
12865static void
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012866check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012867{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012868 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012869 struct intel_encoder *encoder;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012870 struct drm_crtc_state *old_crtc_state;
12871 struct drm_crtc *crtc;
12872 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012873
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012874 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12875 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12876 struct intel_crtc_state *pipe_config, *sw_config;
Maarten Lankhorst7b89b8d2015-08-05 12:37:03 +020012877 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012878
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012879 if (!needs_modeset(crtc->state) &&
12880 !to_intel_crtc_state(crtc->state)->update_pipe)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012881 continue;
12882
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012883 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12884 pipe_config = to_intel_crtc_state(old_crtc_state);
12885 memset(pipe_config, 0, sizeof(*pipe_config));
12886 pipe_config->base.crtc = crtc;
12887 pipe_config->base.state = old_state;
12888
12889 DRM_DEBUG_KMS("[CRTC:%d]\n",
12890 crtc->base.id);
12891
12892 active = dev_priv->display.get_pipe_config(intel_crtc,
12893 pipe_config);
12894
12895 /* hw state is inconsistent with the pipe quirk */
12896 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12897 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12898 active = crtc->state->active;
12899
12900 I915_STATE_WARN(crtc->state->active != active,
12901 "crtc active state doesn't match with hw state "
12902 "(expected %i, found %i)\n", crtc->state->active, active);
12903
12904 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
12905 "transitional active state does not match atomic hw state "
12906 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12907
12908 for_each_encoder_on_crtc(dev, crtc, encoder) {
12909 enum pipe pipe;
12910
12911 active = encoder->get_hw_state(encoder, &pipe);
12912 I915_STATE_WARN(active != crtc->state->active,
12913 "[ENCODER:%i] active %i with crtc active %i\n",
12914 encoder->base.base.id, active, crtc->state->active);
12915
12916 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12917 "Encoder connected to wrong pipe %c\n",
12918 pipe_name(pipe));
12919
12920 if (active)
12921 encoder->get_config(encoder, pipe_config);
12922 }
12923
12924 if (!crtc->state->active)
12925 continue;
12926
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012927 intel_pipe_config_sanity_check(dev_priv, pipe_config);
12928
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012929 sw_config = to_intel_crtc_state(crtc->state);
12930 if (!intel_pipe_config_compare(dev, sw_config,
12931 pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012932 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012933 intel_dump_pipe_config(intel_crtc, pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012934 "[hw state]");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012935 intel_dump_pipe_config(intel_crtc, sw_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012936 "[sw state]");
12937 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012938 }
12939}
12940
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012941static void
12942check_shared_dpll_state(struct drm_device *dev)
12943{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012944 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012945 struct intel_crtc *crtc;
12946 struct intel_dpll_hw_state dpll_hw_state;
12947 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012948
12949 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012950 struct intel_shared_dpll *pll =
12951 intel_get_shared_dpll_by_id(dev_priv, i);
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012952 unsigned enabled_crtcs = 0, active_crtcs = 0;
Daniel Vetter53589012013-06-05 13:34:16 +020012953 bool active;
12954
12955 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12956
12957 DRM_DEBUG_KMS("%s\n", pll->name);
12958
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020012959 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020012960
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012961 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
12962 "more active pll users than references: %x vs %x\n",
12963 pll->active_mask, pll->config.crtc_mask);
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020012964
12965 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012966 I915_STATE_WARN(!pll->on && pll->active_mask,
12967 "pll in active use but not on in sw tracking\n");
12968 I915_STATE_WARN(pll->on && !pll->active_mask,
12969 "pll is on but not used by any active crtc\n");
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020012970 I915_STATE_WARN(pll->on != active,
12971 "pll on state mismatch (expected %i, found %i)\n",
12972 pll->on, active);
12973 }
Daniel Vetter53589012013-06-05 13:34:16 +020012974
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012975 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012976 if (crtc->base.state->enable && crtc->config->shared_dpll == pll)
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012977 enabled_crtcs |= 1 << drm_crtc_index(&crtc->base);
12978 if (crtc->base.state->active && crtc->config->shared_dpll == pll)
12979 active_crtcs |= 1 << drm_crtc_index(&crtc->base);
Daniel Vetter53589012013-06-05 13:34:16 +020012980 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012981
12982 I915_STATE_WARN(pll->active_mask != active_crtcs,
12983 "pll active crtcs mismatch (expected %x, found %x)\n",
12984 pll->active_mask, active_crtcs);
12985 I915_STATE_WARN(pll->config.crtc_mask != enabled_crtcs,
12986 "pll enabled crtcs mismatch (expected %x, found %x)\n",
12987 pll->config.crtc_mask, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012988
Rob Clarke2c719b2014-12-15 13:56:32 -050012989 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012990 sizeof(dpll_hw_state)),
12991 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012992 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012993}
12994
Maarten Lankhorstee165b12015-08-05 12:37:00 +020012995static void
12996intel_modeset_check_state(struct drm_device *dev,
12997 struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012998{
Damien Lespiau08db6652014-11-04 17:06:52 +000012999 check_wm_state(dev);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013000 check_connector_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013001 check_encoder_state(dev);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013002 check_crtc_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013003 check_shared_dpll_state(dev);
13004}
13005
Ville Syrjälä80715b22014-05-15 20:23:23 +030013006static void update_scanline_offset(struct intel_crtc *crtc)
13007{
13008 struct drm_device *dev = crtc->base.dev;
13009
13010 /*
13011 * The scanline counter increments at the leading edge of hsync.
13012 *
13013 * On most platforms it starts counting from vtotal-1 on the
13014 * first active line. That means the scanline counter value is
13015 * always one less than what we would expect. Ie. just after
13016 * start of vblank, which also occurs at start of hsync (on the
13017 * last active line), the scanline counter will read vblank_start-1.
13018 *
13019 * On gen2 the scanline counter starts counting from 1 instead
13020 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13021 * to keep the value positive), instead of adding one.
13022 *
13023 * On HSW+ the behaviour of the scanline counter depends on the output
13024 * type. For DP ports it behaves like most other platforms, but on HDMI
13025 * there's an extra 1 line difference. So we need to add two instead of
13026 * one to the value.
13027 */
13028 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013029 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013030 int vtotal;
13031
Ville Syrjälä124abe02015-09-08 13:40:45 +030013032 vtotal = adjusted_mode->crtc_vtotal;
13033 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013034 vtotal /= 2;
13035
13036 crtc->scanline_offset = vtotal - 1;
13037 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013038 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013039 crtc->scanline_offset = 2;
13040 } else
13041 crtc->scanline_offset = 1;
13042}
13043
Maarten Lankhorstad421372015-06-15 12:33:42 +020013044static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013045{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013046 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013047 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013048 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013049 struct drm_crtc *crtc;
13050 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013051 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013052
13053 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013054 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013055
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013056 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013057 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013058 struct intel_shared_dpll *old_dpll =
13059 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013060
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013061 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013062 continue;
13063
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013064 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013065
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013066 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013067 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013068
Maarten Lankhorstad421372015-06-15 12:33:42 +020013069 if (!shared_dpll)
13070 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13071
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013072 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013073 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013074}
13075
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013076/*
13077 * This implements the workaround described in the "notes" section of the mode
13078 * set sequence documentation. When going from no pipes or single pipe to
13079 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13080 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13081 */
13082static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13083{
13084 struct drm_crtc_state *crtc_state;
13085 struct intel_crtc *intel_crtc;
13086 struct drm_crtc *crtc;
13087 struct intel_crtc_state *first_crtc_state = NULL;
13088 struct intel_crtc_state *other_crtc_state = NULL;
13089 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13090 int i;
13091
13092 /* look at all crtc's that are going to be enabled in during modeset */
13093 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13094 intel_crtc = to_intel_crtc(crtc);
13095
13096 if (!crtc_state->active || !needs_modeset(crtc_state))
13097 continue;
13098
13099 if (first_crtc_state) {
13100 other_crtc_state = to_intel_crtc_state(crtc_state);
13101 break;
13102 } else {
13103 first_crtc_state = to_intel_crtc_state(crtc_state);
13104 first_pipe = intel_crtc->pipe;
13105 }
13106 }
13107
13108 /* No workaround needed? */
13109 if (!first_crtc_state)
13110 return 0;
13111
13112 /* w/a possibly needed, check how many crtc's are already enabled. */
13113 for_each_intel_crtc(state->dev, intel_crtc) {
13114 struct intel_crtc_state *pipe_config;
13115
13116 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13117 if (IS_ERR(pipe_config))
13118 return PTR_ERR(pipe_config);
13119
13120 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13121
13122 if (!pipe_config->base.active ||
13123 needs_modeset(&pipe_config->base))
13124 continue;
13125
13126 /* 2 or more enabled crtcs means no need for w/a */
13127 if (enabled_pipe != INVALID_PIPE)
13128 return 0;
13129
13130 enabled_pipe = intel_crtc->pipe;
13131 }
13132
13133 if (enabled_pipe != INVALID_PIPE)
13134 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13135 else if (other_crtc_state)
13136 other_crtc_state->hsw_workaround_pipe = first_pipe;
13137
13138 return 0;
13139}
13140
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013141static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13142{
13143 struct drm_crtc *crtc;
13144 struct drm_crtc_state *crtc_state;
13145 int ret = 0;
13146
13147 /* add all active pipes to the state */
13148 for_each_crtc(state->dev, crtc) {
13149 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13150 if (IS_ERR(crtc_state))
13151 return PTR_ERR(crtc_state);
13152
13153 if (!crtc_state->active || needs_modeset(crtc_state))
13154 continue;
13155
13156 crtc_state->mode_changed = true;
13157
13158 ret = drm_atomic_add_affected_connectors(state, crtc);
13159 if (ret)
13160 break;
13161
13162 ret = drm_atomic_add_affected_planes(state, crtc);
13163 if (ret)
13164 break;
13165 }
13166
13167 return ret;
13168}
13169
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013170static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013171{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013172 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13173 struct drm_i915_private *dev_priv = state->dev->dev_private;
13174 struct drm_crtc *crtc;
13175 struct drm_crtc_state *crtc_state;
13176 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013177
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013178 if (!check_digital_port_conflicts(state)) {
13179 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13180 return -EINVAL;
13181 }
13182
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013183 intel_state->modeset = true;
13184 intel_state->active_crtcs = dev_priv->active_crtcs;
13185
13186 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13187 if (crtc_state->active)
13188 intel_state->active_crtcs |= 1 << i;
13189 else
13190 intel_state->active_crtcs &= ~(1 << i);
13191 }
13192
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013193 /*
13194 * See if the config requires any additional preparation, e.g.
13195 * to adjust global state with pipes off. We need to do this
13196 * here so we can get the modeset_pipe updated config for the new
13197 * mode set on this crtc. For other crtcs we need to use the
13198 * adjusted_mode bits in the crtc directly.
13199 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013200 if (dev_priv->display.modeset_calc_cdclk) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013201 ret = dev_priv->display.modeset_calc_cdclk(state);
13202
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013203 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013204 ret = intel_modeset_all_pipes(state);
13205
13206 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013207 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013208
13209 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13210 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013211 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013212 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013213
Maarten Lankhorstad421372015-06-15 12:33:42 +020013214 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013215
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013216 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013217 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013218
Maarten Lankhorstad421372015-06-15 12:33:42 +020013219 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013220}
13221
Matt Roperaa363132015-09-24 15:53:18 -070013222/*
13223 * Handle calculation of various watermark data at the end of the atomic check
13224 * phase. The code here should be run after the per-crtc and per-plane 'check'
13225 * handlers to ensure that all derived state has been updated.
13226 */
13227static void calc_watermark_data(struct drm_atomic_state *state)
13228{
13229 struct drm_device *dev = state->dev;
13230 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13231 struct drm_crtc *crtc;
13232 struct drm_crtc_state *cstate;
13233 struct drm_plane *plane;
13234 struct drm_plane_state *pstate;
13235
13236 /*
13237 * Calculate watermark configuration details now that derived
13238 * plane/crtc state is all properly updated.
13239 */
13240 drm_for_each_crtc(crtc, dev) {
13241 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13242 crtc->state;
13243
13244 if (cstate->active)
13245 intel_state->wm_config.num_pipes_active++;
13246 }
13247 drm_for_each_legacy_plane(plane, dev) {
13248 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13249 plane->state;
13250
13251 if (!to_intel_plane_state(pstate)->visible)
13252 continue;
13253
13254 intel_state->wm_config.sprites_enabled = true;
13255 if (pstate->crtc_w != pstate->src_w >> 16 ||
13256 pstate->crtc_h != pstate->src_h >> 16)
13257 intel_state->wm_config.sprites_scaled = true;
13258 }
13259}
13260
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013261/**
13262 * intel_atomic_check - validate state object
13263 * @dev: drm device
13264 * @state: state to validate
13265 */
13266static int intel_atomic_check(struct drm_device *dev,
13267 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013268{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013269 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013270 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013271 struct drm_crtc *crtc;
13272 struct drm_crtc_state *crtc_state;
13273 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013274 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013275
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013276 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013277 if (ret)
13278 return ret;
13279
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013280 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013281 struct intel_crtc_state *pipe_config =
13282 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013283
13284 /* Catch I915_MODE_FLAG_INHERITED */
13285 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13286 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013287
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013288 if (!crtc_state->enable) {
13289 if (needs_modeset(crtc_state))
13290 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013291 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013292 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013293
Daniel Vetter26495482015-07-15 14:15:52 +020013294 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013295 continue;
13296
Daniel Vetter26495482015-07-15 14:15:52 +020013297 /* FIXME: For only active_changed we shouldn't need to do any
13298 * state recomputation at all. */
13299
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013300 ret = drm_atomic_add_affected_connectors(state, crtc);
13301 if (ret)
13302 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013303
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013304 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013305 if (ret)
13306 return ret;
13307
Jani Nikula73831232015-11-19 10:26:30 +020013308 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013309 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013310 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013311 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013312 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013313 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013314 }
13315
13316 if (needs_modeset(crtc_state)) {
13317 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013318
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013319 ret = drm_atomic_add_affected_planes(state, crtc);
13320 if (ret)
13321 return ret;
13322 }
13323
Daniel Vetter26495482015-07-15 14:15:52 +020013324 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13325 needs_modeset(crtc_state) ?
13326 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013327 }
13328
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013329 if (any_ms) {
13330 ret = intel_modeset_checks(state);
13331
13332 if (ret)
13333 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013334 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013335 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013336
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013337 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013338 if (ret)
13339 return ret;
13340
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013341 intel_fbc_choose_crtc(dev_priv, state);
Matt Roperaa363132015-09-24 15:53:18 -070013342 calc_watermark_data(state);
13343
13344 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013345}
13346
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013347static int intel_atomic_prepare_commit(struct drm_device *dev,
13348 struct drm_atomic_state *state,
13349 bool async)
13350{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013351 struct drm_i915_private *dev_priv = dev->dev_private;
13352 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013353 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013354 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013355 struct drm_crtc *crtc;
13356 int i, ret;
13357
13358 if (async) {
13359 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13360 return -EINVAL;
13361 }
13362
13363 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13364 ret = intel_crtc_wait_for_pending_flips(crtc);
13365 if (ret)
13366 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013367
13368 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13369 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013370 }
13371
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013372 ret = mutex_lock_interruptible(&dev->struct_mutex);
13373 if (ret)
13374 return ret;
13375
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013376 ret = drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013377 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13378 u32 reset_counter;
13379
13380 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13381 mutex_unlock(&dev->struct_mutex);
13382
13383 for_each_plane_in_state(state, plane, plane_state, i) {
13384 struct intel_plane_state *intel_plane_state =
13385 to_intel_plane_state(plane_state);
13386
13387 if (!intel_plane_state->wait_req)
13388 continue;
13389
13390 ret = __i915_wait_request(intel_plane_state->wait_req,
13391 reset_counter, true,
13392 NULL, NULL);
13393
13394 /* Swallow -EIO errors to allow updates during hw lockup. */
13395 if (ret == -EIO)
13396 ret = 0;
13397
13398 if (ret)
13399 break;
13400 }
13401
13402 if (!ret)
13403 return 0;
13404
13405 mutex_lock(&dev->struct_mutex);
13406 drm_atomic_helper_cleanup_planes(dev, state);
13407 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013408
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013409 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013410 return ret;
13411}
13412
Maarten Lankhorste8861672016-02-24 11:24:26 +010013413static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13414 struct drm_i915_private *dev_priv,
13415 unsigned crtc_mask)
13416{
13417 unsigned last_vblank_count[I915_MAX_PIPES];
13418 enum pipe pipe;
13419 int ret;
13420
13421 if (!crtc_mask)
13422 return;
13423
13424 for_each_pipe(dev_priv, pipe) {
13425 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13426
13427 if (!((1 << pipe) & crtc_mask))
13428 continue;
13429
13430 ret = drm_crtc_vblank_get(crtc);
13431 if (WARN_ON(ret != 0)) {
13432 crtc_mask &= ~(1 << pipe);
13433 continue;
13434 }
13435
13436 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13437 }
13438
13439 for_each_pipe(dev_priv, pipe) {
13440 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13441 long lret;
13442
13443 if (!((1 << pipe) & crtc_mask))
13444 continue;
13445
13446 lret = wait_event_timeout(dev->vblank[pipe].queue,
13447 last_vblank_count[pipe] !=
13448 drm_crtc_vblank_count(crtc),
13449 msecs_to_jiffies(50));
13450
13451 WARN_ON(!lret);
13452
13453 drm_crtc_vblank_put(crtc);
13454 }
13455}
13456
13457static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13458{
13459 /* fb updated, need to unpin old fb */
13460 if (crtc_state->fb_changed)
13461 return true;
13462
13463 /* wm changes, need vblank before final wm's */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013464 if (crtc_state->update_wm_post)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013465 return true;
13466
13467 /*
13468 * cxsr is re-enabled after vblank.
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013469 * This is already handled by crtc_state->update_wm_post,
Maarten Lankhorste8861672016-02-24 11:24:26 +010013470 * but added for clarity.
13471 */
13472 if (crtc_state->disable_cxsr)
13473 return true;
13474
13475 return false;
13476}
13477
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013478/**
13479 * intel_atomic_commit - commit validated state object
13480 * @dev: DRM device
13481 * @state: the top-level driver state object
13482 * @async: asynchronous commit
13483 *
13484 * This function commits a top-level state object that has been validated
13485 * with drm_atomic_helper_check().
13486 *
13487 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13488 * we can only handle plane-related operations and do not yet support
13489 * asynchronous commit.
13490 *
13491 * RETURNS
13492 * Zero for success or -errno.
13493 */
13494static int intel_atomic_commit(struct drm_device *dev,
13495 struct drm_atomic_state *state,
13496 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013497{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013498 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013499 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013500 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013501 struct drm_crtc *crtc;
Matt Ropered4a6a72016-02-23 17:20:13 -080013502 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013503 int ret = 0, i;
13504 bool hw_check = intel_state->modeset;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013505 unsigned long put_domains[I915_MAX_PIPES] = {};
Maarten Lankhorste8861672016-02-24 11:24:26 +010013506 unsigned crtc_vblank_mask = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013507
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013508 ret = intel_atomic_prepare_commit(dev, state, async);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013509 if (ret) {
13510 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013511 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013512 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013513
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013514 drm_atomic_helper_swap_state(dev, state);
Maarten Lankhorsta1475e72016-03-14 09:27:53 +010013515 dev_priv->wm.config = intel_state->wm_config;
13516 intel_shared_dpll_commit(state);
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013517
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013518 if (intel_state->modeset) {
13519 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13520 sizeof(intel_state->min_pixclk));
13521 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013522 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013523
13524 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013525 }
13526
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013527 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013528 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13529
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013530 if (needs_modeset(crtc->state) ||
13531 to_intel_crtc_state(crtc->state)->update_pipe) {
13532 hw_check = true;
13533
13534 put_domains[to_intel_crtc(crtc)->pipe] =
13535 modeset_get_crtc_power_domains(crtc,
13536 to_intel_crtc_state(crtc->state));
13537 }
13538
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013539 if (!needs_modeset(crtc->state))
13540 continue;
13541
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013542 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013543
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013544 if (old_crtc_state->active) {
13545 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013546 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013547 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013548 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013549 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013550
13551 /*
13552 * Underruns don't always raise
13553 * interrupts, so check manually.
13554 */
13555 intel_check_cpu_fifo_underruns(dev_priv);
13556 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013557
13558 if (!crtc->state->active)
13559 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013560 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013561 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013562
Daniel Vetterea9d7582012-07-10 10:42:52 +020013563 /* Only after disabling all output pipelines that will be changed can we
13564 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013565 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013566
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013567 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013568 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013569
13570 if (dev_priv->display.modeset_commit_cdclk &&
13571 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13572 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013573 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013574
Daniel Vettera6778b32012-07-02 09:56:42 +020013575 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013576 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013577 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13578 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorste8861672016-02-24 11:24:26 +010013579 struct intel_crtc_state *pipe_config =
13580 to_intel_crtc_state(crtc->state);
13581 bool update_pipe = !modeset && pipe_config->update_pipe;
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013582
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013583 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013584 update_scanline_offset(to_intel_crtc(crtc));
13585 dev_priv->display.crtc_enable(crtc);
13586 }
13587
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013588 if (!modeset &&
13589 crtc->state->active &&
13590 crtc->state->color_mgmt_changed) {
13591 /*
13592 * Only update color management when not doing
13593 * a modeset as this will be done by
13594 * crtc_enable already.
13595 */
13596 intel_color_set_csc(crtc);
13597 intel_color_load_luts(crtc);
13598 }
13599
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013600 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013601 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013602
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010013603 if (crtc->state->active &&
13604 drm_atomic_get_existing_plane_state(state, crtc->primary))
Paulo Zanoni49227c42016-01-19 11:35:52 -020013605 intel_fbc_enable(intel_crtc);
13606
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013607 if (crtc->state->active &&
13608 (crtc->state->planes_changed || update_pipe))
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013609 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013610
Maarten Lankhorste8861672016-02-24 11:24:26 +010013611 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13612 crtc_vblank_mask |= 1 << i;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013613 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013614
Daniel Vettera6778b32012-07-02 09:56:42 +020013615 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013616
Maarten Lankhorste8861672016-02-24 11:24:26 +010013617 if (!state->legacy_cursor_update)
13618 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013619
Matt Ropered4a6a72016-02-23 17:20:13 -080013620 /*
13621 * Now that the vblank has passed, we can go ahead and program the
13622 * optimal watermarks on platforms that need two-step watermark
13623 * programming.
13624 *
13625 * TODO: Move this (and other cleanup) to an async worker eventually.
13626 */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013627 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Matt Ropered4a6a72016-02-23 17:20:13 -080013628 intel_cstate = to_intel_crtc_state(crtc->state);
13629
13630 if (dev_priv->display.optimize_watermarks)
13631 dev_priv->display.optimize_watermarks(intel_cstate);
13632 }
13633
Matt Roper177246a2016-03-04 15:59:39 -080013634 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13635 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13636
13637 if (put_domains[i])
13638 modeset_put_power_domains(dev_priv, put_domains[i]);
13639 }
13640
13641 if (intel_state->modeset)
13642 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13643
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013644 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013645 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013646 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013647
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013648 if (hw_check)
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013649 intel_modeset_check_state(dev, state);
13650
13651 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013652
Mika Kuoppala75714942015-12-16 09:26:48 +020013653 /* As one of the primary mmio accessors, KMS has a high likelihood
13654 * of triggering bugs in unclaimed access. After we finish
13655 * modesetting, see if an error has been flagged, and if so
13656 * enable debugging for the next modeset - and hope we catch
13657 * the culprit.
13658 *
13659 * XXX note that we assume display power is on at this point.
13660 * This might hold true now but we need to add pm helper to check
13661 * unclaimed only when the hardware is on, as atomic commits
13662 * can happen also when the device is completely off.
13663 */
13664 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13665
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013666 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013667}
13668
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013669void intel_crtc_restore_mode(struct drm_crtc *crtc)
13670{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013671 struct drm_device *dev = crtc->dev;
13672 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013673 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013674 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013675
13676 state = drm_atomic_state_alloc(dev);
13677 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013678 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013679 crtc->base.id);
13680 return;
13681 }
13682
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013683 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013684
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013685retry:
13686 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13687 ret = PTR_ERR_OR_ZERO(crtc_state);
13688 if (!ret) {
13689 if (!crtc_state->active)
13690 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013691
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013692 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013693 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013694 }
13695
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013696 if (ret == -EDEADLK) {
13697 drm_atomic_state_clear(state);
13698 drm_modeset_backoff(state->acquire_ctx);
13699 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013700 }
13701
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013702 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013703out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013704 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013705}
13706
Daniel Vetter25c5b262012-07-08 22:08:04 +020013707#undef for_each_intel_crtc_masked
13708
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013709static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013710 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013711 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013712 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013713 .destroy = intel_crtc_destroy,
13714 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013715 .atomic_duplicate_state = intel_crtc_duplicate_state,
13716 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013717};
13718
Matt Roper6beb8c232014-12-01 15:40:14 -080013719/**
13720 * intel_prepare_plane_fb - Prepare fb for usage on plane
13721 * @plane: drm plane to prepare for
13722 * @fb: framebuffer to prepare for presentation
13723 *
13724 * Prepares a framebuffer for usage on a display plane. Generally this
13725 * involves pinning the underlying object and updating the frontbuffer tracking
13726 * bits. Some older platforms need special physical address handling for
13727 * cursor planes.
13728 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013729 * Must be called with struct_mutex held.
13730 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013731 * Returns 0 on success, negative error code on failure.
13732 */
13733int
13734intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013735 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013736{
13737 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013738 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013739 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013740 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013741 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013742 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013743
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013744 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013745 return 0;
13746
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013747 if (old_obj) {
13748 struct drm_crtc_state *crtc_state =
13749 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13750
13751 /* Big Hammer, we also need to ensure that any pending
13752 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13753 * current scanout is retired before unpinning the old
13754 * framebuffer. Note that we rely on userspace rendering
13755 * into the buffer attached to the pipe they are waiting
13756 * on. If not, userspace generates a GPU hang with IPEHR
13757 * point to the MI_WAIT_FOR_EVENT.
13758 *
13759 * This should only fail upon a hung GPU, in which case we
13760 * can safely continue.
13761 */
13762 if (needs_modeset(crtc_state))
13763 ret = i915_gem_object_wait_rendering(old_obj, true);
13764
13765 /* Swallow -EIO errors to allow updates during hw lockup. */
13766 if (ret && ret != -EIO)
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013767 return ret;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013768 }
13769
Alex Goins3c28ff22015-11-25 18:43:39 -080013770 /* For framebuffer backed by dmabuf, wait for fence */
13771 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013772 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080013773
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013774 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13775 false, true,
13776 MAX_SCHEDULE_TIMEOUT);
13777 if (lret == -ERESTARTSYS)
13778 return lret;
13779
13780 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080013781 }
13782
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013783 if (!obj) {
13784 ret = 0;
13785 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013786 INTEL_INFO(dev)->cursor_needs_physical) {
13787 int align = IS_I830(dev) ? 16 * 1024 : 256;
13788 ret = i915_gem_object_attach_phys(obj, align);
13789 if (ret)
13790 DRM_DEBUG_KMS("failed to attach phys object\n");
13791 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020013792 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080013793 }
13794
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013795 if (ret == 0) {
13796 if (obj) {
13797 struct intel_plane_state *plane_state =
13798 to_intel_plane_state(new_state);
13799
13800 i915_gem_request_assign(&plane_state->wait_req,
13801 obj->last_write_req);
13802 }
13803
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013804 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013805 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013806
Matt Roper6beb8c232014-12-01 15:40:14 -080013807 return ret;
13808}
13809
Matt Roper38f3ce32014-12-02 07:45:25 -080013810/**
13811 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13812 * @plane: drm plane to clean up for
13813 * @fb: old framebuffer that was on plane
13814 *
13815 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013816 *
13817 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013818 */
13819void
13820intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013821 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013822{
13823 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013824 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013825 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013826 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13827 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013828
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013829 old_intel_state = to_intel_plane_state(old_state);
13830
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013831 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013832 return;
13833
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013834 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13835 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020013836 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013837
13838 /* prepare_fb aborted? */
13839 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13840 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13841 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013842
13843 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070013844}
13845
Chandra Konduru6156a452015-04-27 13:48:39 -070013846int
13847skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13848{
13849 int max_scale;
13850 struct drm_device *dev;
13851 struct drm_i915_private *dev_priv;
13852 int crtc_clock, cdclk;
13853
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013854 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013855 return DRM_PLANE_HELPER_NO_SCALING;
13856
13857 dev = intel_crtc->base.dev;
13858 dev_priv = dev->dev_private;
13859 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013860 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013861
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010013862 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013863 return DRM_PLANE_HELPER_NO_SCALING;
13864
13865 /*
13866 * skl max scale is lower of:
13867 * close to 3 but not 3, -1 is for that purpose
13868 * or
13869 * cdclk/crtc_clock
13870 */
13871 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13872
13873 return max_scale;
13874}
13875
Matt Roper465c1202014-05-29 08:06:54 -070013876static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013877intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013878 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013879 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013880{
Matt Roper2b875c22014-12-01 15:40:13 -080013881 struct drm_crtc *crtc = state->base.crtc;
13882 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013883 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013884 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13885 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013886
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013887 if (INTEL_INFO(plane->dev)->gen >= 9) {
13888 /* use scaler when colorkey is not required */
13889 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13890 min_scale = 1;
13891 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13892 }
Sonika Jindald8106362015-04-10 14:37:28 +053013893 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013894 }
Sonika Jindald8106362015-04-10 14:37:28 +053013895
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013896 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13897 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013898 min_scale, max_scale,
13899 can_position, true,
13900 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013901}
13902
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013903static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13904 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013905{
13906 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080013907 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013908 struct intel_crtc_state *old_intel_state =
13909 to_intel_crtc_state(old_crtc_state);
13910 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013911
Matt Roperc34c9ee2014-12-23 10:41:50 -080013912 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020013913 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013914
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013915 if (modeset)
13916 return;
13917
13918 if (to_intel_crtc_state(crtc->state)->update_pipe)
13919 intel_update_pipe_config(intel_crtc, old_intel_state);
13920 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020013921 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013922}
13923
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013924static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13925 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013926{
Matt Roper32b7eee2014-12-24 07:59:06 -080013927 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013928
Maarten Lankhorst62852622015-09-23 16:29:38 +020013929 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013930}
13931
Matt Ropercf4c7c12014-12-04 10:27:42 -080013932/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013933 * intel_plane_destroy - destroy a plane
13934 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013935 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013936 * Common destruction function for all types of planes (primary, cursor,
13937 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013938 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013939void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013940{
13941 struct intel_plane *intel_plane = to_intel_plane(plane);
13942 drm_plane_cleanup(plane);
13943 kfree(intel_plane);
13944}
13945
Matt Roper65a3fea2015-01-21 16:35:42 -080013946const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013947 .update_plane = drm_atomic_helper_update_plane,
13948 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013949 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013950 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013951 .atomic_get_property = intel_plane_atomic_get_property,
13952 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013953 .atomic_duplicate_state = intel_plane_duplicate_state,
13954 .atomic_destroy_state = intel_plane_destroy_state,
13955
Matt Roper465c1202014-05-29 08:06:54 -070013956};
13957
13958static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13959 int pipe)
13960{
13961 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013962 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013963 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020013964 unsigned int num_formats;
Matt Roper465c1202014-05-29 08:06:54 -070013965
13966 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13967 if (primary == NULL)
13968 return NULL;
13969
Matt Roper8e7d6882015-01-21 16:35:41 -080013970 state = intel_create_plane_state(&primary->base);
13971 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013972 kfree(primary);
13973 return NULL;
13974 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013975 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013976
Matt Roper465c1202014-05-29 08:06:54 -070013977 primary->can_scale = false;
13978 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013979 if (INTEL_INFO(dev)->gen >= 9) {
13980 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013981 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013982 }
Matt Roper465c1202014-05-29 08:06:54 -070013983 primary->pipe = pipe;
13984 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013985 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013986 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013987 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13988 primary->plane = !pipe;
13989
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013990 if (INTEL_INFO(dev)->gen >= 9) {
13991 intel_primary_formats = skl_primary_formats;
13992 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013993
13994 primary->update_plane = skylake_update_primary_plane;
13995 primary->disable_plane = skylake_disable_primary_plane;
13996 } else if (HAS_PCH_SPLIT(dev)) {
13997 intel_primary_formats = i965_primary_formats;
13998 num_formats = ARRAY_SIZE(i965_primary_formats);
13999
14000 primary->update_plane = ironlake_update_primary_plane;
14001 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014002 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014003 intel_primary_formats = i965_primary_formats;
14004 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014005
14006 primary->update_plane = i9xx_update_primary_plane;
14007 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014008 } else {
14009 intel_primary_formats = i8xx_primary_formats;
14010 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014011
14012 primary->update_plane = i9xx_update_primary_plane;
14013 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014014 }
14015
14016 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014017 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070014018 intel_primary_formats, num_formats,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020014019 DRM_PLANE_TYPE_PRIMARY, NULL);
Sonika Jindal48404c12014-08-22 14:06:04 +053014020
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014021 if (INTEL_INFO(dev)->gen >= 4)
14022 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014023
Matt Roperea2c67b2014-12-23 10:41:52 -080014024 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14025
Matt Roper465c1202014-05-29 08:06:54 -070014026 return &primary->base;
14027}
14028
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014029void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14030{
14031 if (!dev->mode_config.rotation_property) {
14032 unsigned long flags = BIT(DRM_ROTATE_0) |
14033 BIT(DRM_ROTATE_180);
14034
14035 if (INTEL_INFO(dev)->gen >= 9)
14036 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14037
14038 dev->mode_config.rotation_property =
14039 drm_mode_create_rotation_property(dev, flags);
14040 }
14041 if (dev->mode_config.rotation_property)
14042 drm_object_attach_property(&plane->base.base,
14043 dev->mode_config.rotation_property,
14044 plane->base.state->rotation);
14045}
14046
Matt Roper3d7d6512014-06-10 08:28:13 -070014047static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014048intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014049 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014050 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014051{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014052 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014053 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014054 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014055 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014056 unsigned stride;
14057 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014058
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014059 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14060 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014061 DRM_PLANE_HELPER_NO_SCALING,
14062 DRM_PLANE_HELPER_NO_SCALING,
14063 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014064 if (ret)
14065 return ret;
14066
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014067 /* if we want to turn off the cursor ignore width and height */
14068 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014069 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014070
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014071 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014072 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014073 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14074 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014075 return -EINVAL;
14076 }
14077
Matt Roperea2c67b2014-12-23 10:41:52 -080014078 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14079 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014080 DRM_DEBUG_KMS("buffer is too small\n");
14081 return -ENOMEM;
14082 }
14083
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014084 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014085 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014086 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014087 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014088
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014089 /*
14090 * There's something wrong with the cursor on CHV pipe C.
14091 * If it straddles the left edge of the screen then
14092 * moving it away from the edge or disabling it often
14093 * results in a pipe underrun, and often that can lead to
14094 * dead pipe (constant underrun reported, and it scans
14095 * out just a solid color). To recover from that, the
14096 * display power well must be turned off and on again.
14097 * Refuse the put the cursor into that compromised position.
14098 */
14099 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14100 state->visible && state->base.crtc_x < 0) {
14101 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14102 return -EINVAL;
14103 }
14104
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014105 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014106}
14107
Matt Roperf4a2cf22014-12-01 15:40:12 -080014108static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014109intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014110 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014111{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014112 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14113
14114 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014115 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014116}
14117
14118static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014119intel_update_cursor_plane(struct drm_plane *plane,
14120 const struct intel_crtc_state *crtc_state,
14121 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014122{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014123 struct drm_crtc *crtc = crtc_state->base.crtc;
14124 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014125 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014126 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014127 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014128
Matt Roperf4a2cf22014-12-01 15:40:12 -080014129 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014130 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014131 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014132 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014133 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014134 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014135
Gustavo Padovana912f122014-12-01 15:40:10 -080014136 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014137 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014138}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014139
Matt Roper3d7d6512014-06-10 08:28:13 -070014140static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14141 int pipe)
14142{
14143 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014144 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014145
14146 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14147 if (cursor == NULL)
14148 return NULL;
14149
Matt Roper8e7d6882015-01-21 16:35:41 -080014150 state = intel_create_plane_state(&cursor->base);
14151 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014152 kfree(cursor);
14153 return NULL;
14154 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014155 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014156
Matt Roper3d7d6512014-06-10 08:28:13 -070014157 cursor->can_scale = false;
14158 cursor->max_downscale = 1;
14159 cursor->pipe = pipe;
14160 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014161 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014162 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014163 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014164 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014165
14166 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014167 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014168 intel_cursor_formats,
14169 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjäläb0b3b792015-12-09 16:19:55 +020014170 DRM_PLANE_TYPE_CURSOR, NULL);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014171
14172 if (INTEL_INFO(dev)->gen >= 4) {
14173 if (!dev->mode_config.rotation_property)
14174 dev->mode_config.rotation_property =
14175 drm_mode_create_rotation_property(dev,
14176 BIT(DRM_ROTATE_0) |
14177 BIT(DRM_ROTATE_180));
14178 if (dev->mode_config.rotation_property)
14179 drm_object_attach_property(&cursor->base.base,
14180 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014181 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014182 }
14183
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014184 if (INTEL_INFO(dev)->gen >=9)
14185 state->scaler_id = -1;
14186
Matt Roperea2c67b2014-12-23 10:41:52 -080014187 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14188
Matt Roper3d7d6512014-06-10 08:28:13 -070014189 return &cursor->base;
14190}
14191
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014192static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14193 struct intel_crtc_state *crtc_state)
14194{
14195 int i;
14196 struct intel_scaler *intel_scaler;
14197 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14198
14199 for (i = 0; i < intel_crtc->num_scalers; i++) {
14200 intel_scaler = &scaler_state->scalers[i];
14201 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014202 intel_scaler->mode = PS_SCALER_MODE_DYN;
14203 }
14204
14205 scaler_state->scaler_id = -1;
14206}
14207
Hannes Ederb358d0a2008-12-18 21:18:47 +010014208static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014209{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014210 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014211 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014212 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014213 struct drm_plane *primary = NULL;
14214 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014215 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014216
Daniel Vetter955382f2013-09-19 14:05:45 +020014217 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014218 if (intel_crtc == NULL)
14219 return;
14220
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014221 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14222 if (!crtc_state)
14223 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014224 intel_crtc->config = crtc_state;
14225 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014226 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014227
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014228 /* initialize shared scalers */
14229 if (INTEL_INFO(dev)->gen >= 9) {
14230 if (pipe == PIPE_C)
14231 intel_crtc->num_scalers = 1;
14232 else
14233 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14234
14235 skl_init_scalers(dev, intel_crtc, crtc_state);
14236 }
14237
Matt Roper465c1202014-05-29 08:06:54 -070014238 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014239 if (!primary)
14240 goto fail;
14241
14242 cursor = intel_cursor_plane_create(dev, pipe);
14243 if (!cursor)
14244 goto fail;
14245
Matt Roper465c1202014-05-29 08:06:54 -070014246 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020014247 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070014248 if (ret)
14249 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014250
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014251 /*
14252 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014253 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014254 */
Jesse Barnes80824002009-09-10 15:28:06 -070014255 intel_crtc->pipe = pipe;
14256 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014257 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014258 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014259 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014260 }
14261
Chris Wilson4b0e3332014-05-30 16:35:26 +030014262 intel_crtc->cursor_base = ~0;
14263 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014264 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014265
Ville Syrjälä852eb002015-06-24 22:00:07 +030014266 intel_crtc->wm.cxsr_allowed = true;
14267
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014268 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14269 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14270 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14271 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14272
Jesse Barnes79e53942008-11-07 14:24:08 -080014273 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014274
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014275 intel_color_init(&intel_crtc->base);
14276
Daniel Vetter87b6b102014-05-15 15:33:46 +020014277 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014278 return;
14279
14280fail:
14281 if (primary)
14282 drm_plane_cleanup(primary);
14283 if (cursor)
14284 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014285 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014286 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014287}
14288
Jesse Barnes752aa882013-10-31 18:55:49 +020014289enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14290{
14291 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014292 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014293
Rob Clark51fd3712013-11-19 12:10:12 -050014294 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014295
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014296 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014297 return INVALID_PIPE;
14298
14299 return to_intel_crtc(encoder->crtc)->pipe;
14300}
14301
Carl Worth08d7b3d2009-04-29 14:43:54 -070014302int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014303 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014304{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014305 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014306 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014307 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014308
Rob Clark7707e652014-07-17 23:30:04 -040014309 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014310
Rob Clark7707e652014-07-17 23:30:04 -040014311 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014312 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014313 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014314 }
14315
Rob Clark7707e652014-07-17 23:30:04 -040014316 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014317 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014318
Daniel Vetterc05422d2009-08-11 16:05:30 +020014319 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014320}
14321
Daniel Vetter66a92782012-07-12 20:08:18 +020014322static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014323{
Daniel Vetter66a92782012-07-12 20:08:18 +020014324 struct drm_device *dev = encoder->base.dev;
14325 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014326 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014327 int entry = 0;
14328
Damien Lespiaub2784e12014-08-05 11:29:37 +010014329 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014330 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014331 index_mask |= (1 << entry);
14332
Jesse Barnes79e53942008-11-07 14:24:08 -080014333 entry++;
14334 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014335
Jesse Barnes79e53942008-11-07 14:24:08 -080014336 return index_mask;
14337}
14338
Chris Wilson4d302442010-12-14 19:21:29 +000014339static bool has_edp_a(struct drm_device *dev)
14340{
14341 struct drm_i915_private *dev_priv = dev->dev_private;
14342
14343 if (!IS_MOBILE(dev))
14344 return false;
14345
14346 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14347 return false;
14348
Damien Lespiaue3589902014-02-07 19:12:50 +000014349 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014350 return false;
14351
14352 return true;
14353}
14354
Jesse Barnes84b4e042014-06-25 08:24:29 -070014355static bool intel_crt_present(struct drm_device *dev)
14356{
14357 struct drm_i915_private *dev_priv = dev->dev_private;
14358
Damien Lespiau884497e2013-12-03 13:56:23 +000014359 if (INTEL_INFO(dev)->gen >= 9)
14360 return false;
14361
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014362 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014363 return false;
14364
14365 if (IS_CHERRYVIEW(dev))
14366 return false;
14367
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014368 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14369 return false;
14370
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014371 /* DDI E can't be used if DDI A requires 4 lanes */
14372 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14373 return false;
14374
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014375 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014376 return false;
14377
14378 return true;
14379}
14380
Jesse Barnes79e53942008-11-07 14:24:08 -080014381static void intel_setup_outputs(struct drm_device *dev)
14382{
Eric Anholt725e30a2009-01-22 13:01:02 -080014383 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014384 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014385 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014386
Daniel Vetterc9093352013-06-06 22:22:47 +020014387 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014388
Jesse Barnes84b4e042014-06-25 08:24:29 -070014389 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014390 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014391
Vandana Kannanc776eb22014-08-19 12:05:01 +053014392 if (IS_BROXTON(dev)) {
14393 /*
14394 * FIXME: Broxton doesn't support port detection via the
14395 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14396 * detect the ports.
14397 */
14398 intel_ddi_init(dev, PORT_A);
14399 intel_ddi_init(dev, PORT_B);
14400 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014401
14402 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014403 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014404 int found;
14405
Jesse Barnesde31fac2015-03-06 15:53:32 -080014406 /*
14407 * Haswell uses DDI functions to detect digital outputs.
14408 * On SKL pre-D0 the strap isn't connected, so we assume
14409 * it's there.
14410 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014411 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014412 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014413 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014414 intel_ddi_init(dev, PORT_A);
14415
14416 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14417 * register */
14418 found = I915_READ(SFUSE_STRAP);
14419
14420 if (found & SFUSE_STRAP_DDIB_DETECTED)
14421 intel_ddi_init(dev, PORT_B);
14422 if (found & SFUSE_STRAP_DDIC_DETECTED)
14423 intel_ddi_init(dev, PORT_C);
14424 if (found & SFUSE_STRAP_DDID_DETECTED)
14425 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014426 /*
14427 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14428 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014429 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014430 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14431 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14432 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14433 intel_ddi_init(dev, PORT_E);
14434
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014435 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014436 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014437 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014438
14439 if (has_edp_a(dev))
14440 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014441
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014442 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014443 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014444 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014445 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014446 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014447 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014448 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014449 }
14450
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014451 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014452 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014453
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014454 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014455 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014456
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014457 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014458 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014459
Daniel Vetter270b3042012-10-27 15:52:05 +020014460 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014461 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014462 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014463 /*
14464 * The DP_DETECTED bit is the latched state of the DDC
14465 * SDA pin at boot. However since eDP doesn't require DDC
14466 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14467 * eDP ports may have been muxed to an alternate function.
14468 * Thus we can't rely on the DP_DETECTED bit alone to detect
14469 * eDP ports. Consult the VBT as well as DP_DETECTED to
14470 * detect eDP ports.
14471 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014472 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014473 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014474 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14475 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014476 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014477 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014478
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014479 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014480 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014481 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14482 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014483 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014484 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014485
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014486 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014487 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014488 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14489 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14490 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14491 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014492 }
14493
Jani Nikula3cfca972013-08-27 15:12:26 +030014494 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014495 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014496 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014497
Paulo Zanonie2debe92013-02-18 19:00:27 -030014498 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014499 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014500 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014501 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014502 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014503 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014504 }
Ma Ling27185ae2009-08-24 13:50:23 +080014505
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014506 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014507 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014508 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014509
14510 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014511
Paulo Zanonie2debe92013-02-18 19:00:27 -030014512 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014513 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014514 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014515 }
Ma Ling27185ae2009-08-24 13:50:23 +080014516
Paulo Zanonie2debe92013-02-18 19:00:27 -030014517 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014518
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014519 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014520 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014521 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014522 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014523 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014524 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014525 }
Ma Ling27185ae2009-08-24 13:50:23 +080014526
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014527 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014528 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014529 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014530 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014531 intel_dvo_init(dev);
14532
Zhenyu Wang103a1962009-11-27 11:44:36 +080014533 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014534 intel_tv_init(dev);
14535
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014536 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014537
Damien Lespiaub2784e12014-08-05 11:29:37 +010014538 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014539 encoder->base.possible_crtcs = encoder->crtc_mask;
14540 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014541 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014542 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014543
Paulo Zanonidde86e22012-12-01 12:04:25 -020014544 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014545
14546 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014547}
14548
14549static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14550{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014551 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014552 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014553
Daniel Vetteref2d6332014-02-10 18:00:38 +010014554 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014555 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014556 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014557 drm_gem_object_unreference(&intel_fb->obj->base);
14558 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014559 kfree(intel_fb);
14560}
14561
14562static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014563 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014564 unsigned int *handle)
14565{
14566 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014567 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014568
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014569 if (obj->userptr.mm) {
14570 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14571 return -EINVAL;
14572 }
14573
Chris Wilson05394f32010-11-08 19:18:58 +000014574 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014575}
14576
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014577static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14578 struct drm_file *file,
14579 unsigned flags, unsigned color,
14580 struct drm_clip_rect *clips,
14581 unsigned num_clips)
14582{
14583 struct drm_device *dev = fb->dev;
14584 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14585 struct drm_i915_gem_object *obj = intel_fb->obj;
14586
14587 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014588 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014589 mutex_unlock(&dev->struct_mutex);
14590
14591 return 0;
14592}
14593
Jesse Barnes79e53942008-11-07 14:24:08 -080014594static const struct drm_framebuffer_funcs intel_fb_funcs = {
14595 .destroy = intel_user_framebuffer_destroy,
14596 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014597 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014598};
14599
Damien Lespiaub3218032015-02-27 11:15:18 +000014600static
14601u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14602 uint32_t pixel_format)
14603{
14604 u32 gen = INTEL_INFO(dev)->gen;
14605
14606 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014607 int cpp = drm_format_plane_cpp(pixel_format, 0);
14608
Damien Lespiaub3218032015-02-27 11:15:18 +000014609 /* "The stride in bytes must not exceed the of the size of 8K
14610 * pixels and 32K bytes."
14611 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014612 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014613 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014614 return 32*1024;
14615 } else if (gen >= 4) {
14616 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14617 return 16*1024;
14618 else
14619 return 32*1024;
14620 } else if (gen >= 3) {
14621 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14622 return 8*1024;
14623 else
14624 return 16*1024;
14625 } else {
14626 /* XXX DSPC is limited to 4k tiled */
14627 return 8*1024;
14628 }
14629}
14630
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014631static int intel_framebuffer_init(struct drm_device *dev,
14632 struct intel_framebuffer *intel_fb,
14633 struct drm_mode_fb_cmd2 *mode_cmd,
14634 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014635{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014636 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014637 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014638 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014639 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014640
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014641 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14642
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014643 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14644 /* Enforce that fb modifier and tiling mode match, but only for
14645 * X-tiled. This is needed for FBC. */
14646 if (!!(obj->tiling_mode == I915_TILING_X) !=
14647 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14648 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14649 return -EINVAL;
14650 }
14651 } else {
14652 if (obj->tiling_mode == I915_TILING_X)
14653 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14654 else if (obj->tiling_mode == I915_TILING_Y) {
14655 DRM_DEBUG("No Y tiling for legacy addfb\n");
14656 return -EINVAL;
14657 }
14658 }
14659
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014660 /* Passed in modifier sanity checking. */
14661 switch (mode_cmd->modifier[0]) {
14662 case I915_FORMAT_MOD_Y_TILED:
14663 case I915_FORMAT_MOD_Yf_TILED:
14664 if (INTEL_INFO(dev)->gen < 9) {
14665 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14666 mode_cmd->modifier[0]);
14667 return -EINVAL;
14668 }
14669 case DRM_FORMAT_MOD_NONE:
14670 case I915_FORMAT_MOD_X_TILED:
14671 break;
14672 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014673 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14674 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014675 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014676 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014677
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014678 stride_alignment = intel_fb_stride_alignment(dev_priv,
14679 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014680 mode_cmd->pixel_format);
14681 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14682 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14683 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014684 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014685 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014686
Damien Lespiaub3218032015-02-27 11:15:18 +000014687 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14688 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014689 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014690 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14691 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014692 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014693 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014694 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014695 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014696
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014697 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014698 mode_cmd->pitches[0] != obj->stride) {
14699 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14700 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014701 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014702 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014703
Ville Syrjälä57779d02012-10-31 17:50:14 +020014704 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014705 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014706 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014707 case DRM_FORMAT_RGB565:
14708 case DRM_FORMAT_XRGB8888:
14709 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014710 break;
14711 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014712 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014713 DRM_DEBUG("unsupported pixel format: %s\n",
14714 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014715 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014716 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014717 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014718 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014719 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14720 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014721 DRM_DEBUG("unsupported pixel format: %s\n",
14722 drm_get_format_name(mode_cmd->pixel_format));
14723 return -EINVAL;
14724 }
14725 break;
14726 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014727 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014728 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014729 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014730 DRM_DEBUG("unsupported pixel format: %s\n",
14731 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014732 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014733 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014734 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014735 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014736 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014737 DRM_DEBUG("unsupported pixel format: %s\n",
14738 drm_get_format_name(mode_cmd->pixel_format));
14739 return -EINVAL;
14740 }
14741 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014742 case DRM_FORMAT_YUYV:
14743 case DRM_FORMAT_UYVY:
14744 case DRM_FORMAT_YVYU:
14745 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014746 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014747 DRM_DEBUG("unsupported pixel format: %s\n",
14748 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014749 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014750 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014751 break;
14752 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014753 DRM_DEBUG("unsupported pixel format: %s\n",
14754 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014755 return -EINVAL;
14756 }
14757
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014758 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14759 if (mode_cmd->offsets[0] != 0)
14760 return -EINVAL;
14761
Damien Lespiauec2c9812015-01-20 12:51:45 +000014762 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014763 mode_cmd->pixel_format,
14764 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014765 /* FIXME drm helper for size checks (especially planar formats)? */
14766 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14767 return -EINVAL;
14768
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014769 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14770 intel_fb->obj = obj;
14771
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014772 intel_fill_fb_info(dev_priv, &intel_fb->base);
14773
Jesse Barnes79e53942008-11-07 14:24:08 -080014774 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14775 if (ret) {
14776 DRM_ERROR("framebuffer init failed %d\n", ret);
14777 return ret;
14778 }
14779
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020014780 intel_fb->obj->framebuffer_references++;
14781
Jesse Barnes79e53942008-11-07 14:24:08 -080014782 return 0;
14783}
14784
Jesse Barnes79e53942008-11-07 14:24:08 -080014785static struct drm_framebuffer *
14786intel_user_framebuffer_create(struct drm_device *dev,
14787 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020014788 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014789{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014790 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014791 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014792 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014793
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014794 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014795 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014796 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014797 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014798
Daniel Vetter92907cb2015-11-23 09:04:05 +010014799 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014800 if (IS_ERR(fb))
14801 drm_gem_object_unreference_unlocked(&obj->base);
14802
14803 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014804}
14805
Daniel Vetter06957262015-08-10 13:34:08 +020014806#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014807static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014808{
14809}
14810#endif
14811
Jesse Barnes79e53942008-11-07 14:24:08 -080014812static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014813 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014814 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014815 .atomic_check = intel_atomic_check,
14816 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014817 .atomic_state_alloc = intel_atomic_state_alloc,
14818 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014819};
14820
Imre Deak88212942016-03-16 13:38:53 +020014821/**
14822 * intel_init_display_hooks - initialize the display modesetting hooks
14823 * @dev_priv: device private
14824 */
14825void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014826{
Imre Deak88212942016-03-16 13:38:53 +020014827 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014828 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014829 dev_priv->display.get_initial_plane_config =
14830 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014831 dev_priv->display.crtc_compute_clock =
14832 haswell_crtc_compute_clock;
14833 dev_priv->display.crtc_enable = haswell_crtc_enable;
14834 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014835 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014836 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014837 dev_priv->display.get_initial_plane_config =
14838 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014839 dev_priv->display.crtc_compute_clock =
14840 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014841 dev_priv->display.crtc_enable = haswell_crtc_enable;
14842 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014843 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014844 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014845 dev_priv->display.get_initial_plane_config =
14846 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014847 dev_priv->display.crtc_compute_clock =
14848 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014849 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14850 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014851 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014852 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014853 dev_priv->display.get_initial_plane_config =
14854 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014855 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14856 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14857 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14858 } else if (IS_VALLEYVIEW(dev_priv)) {
14859 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14860 dev_priv->display.get_initial_plane_config =
14861 i9xx_get_initial_plane_config;
14862 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014863 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14864 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020014865 } else if (IS_G4X(dev_priv)) {
14866 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14867 dev_priv->display.get_initial_plane_config =
14868 i9xx_get_initial_plane_config;
14869 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14870 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14871 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020014872 } else if (IS_PINEVIEW(dev_priv)) {
14873 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14874 dev_priv->display.get_initial_plane_config =
14875 i9xx_get_initial_plane_config;
14876 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14877 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14878 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014879 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014880 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014881 dev_priv->display.get_initial_plane_config =
14882 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014883 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014884 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14885 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014886 } else {
14887 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14888 dev_priv->display.get_initial_plane_config =
14889 i9xx_get_initial_plane_config;
14890 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14891 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14892 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014893 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014894
Jesse Barnese70236a2009-09-21 10:42:27 -070014895 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020014896 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014897 dev_priv->display.get_display_clock_speed =
14898 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014899 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014900 dev_priv->display.get_display_clock_speed =
14901 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014902 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014903 dev_priv->display.get_display_clock_speed =
14904 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014905 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014906 dev_priv->display.get_display_clock_speed =
14907 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014908 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014909 dev_priv->display.get_display_clock_speed =
14910 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014911 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014912 dev_priv->display.get_display_clock_speed =
14913 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014914 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14915 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014916 dev_priv->display.get_display_clock_speed =
14917 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014918 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014919 dev_priv->display.get_display_clock_speed =
14920 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014921 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014922 dev_priv->display.get_display_clock_speed =
14923 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014924 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014925 dev_priv->display.get_display_clock_speed =
14926 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014927 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014928 dev_priv->display.get_display_clock_speed =
14929 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014930 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014931 dev_priv->display.get_display_clock_speed =
14932 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014933 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014934 dev_priv->display.get_display_clock_speed =
14935 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014936 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014937 dev_priv->display.get_display_clock_speed =
14938 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014939 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014940 dev_priv->display.get_display_clock_speed =
14941 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014942 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014943 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014944 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014945 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020014946 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014947 dev_priv->display.get_display_clock_speed =
14948 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014949 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014950
Imre Deak88212942016-03-16 13:38:53 +020014951 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014952 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014953 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014954 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014955 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014956 /* FIXME: detect B0+ stepping and use auto training */
14957 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014958 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014959 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014960 if (IS_BROADWELL(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014961 dev_priv->display.modeset_commit_cdclk =
14962 broadwell_modeset_commit_cdclk;
14963 dev_priv->display.modeset_calc_cdclk =
14964 broadwell_modeset_calc_cdclk;
14965 }
Imre Deak88212942016-03-16 13:38:53 +020014966 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014967 dev_priv->display.modeset_commit_cdclk =
14968 valleyview_modeset_commit_cdclk;
14969 dev_priv->display.modeset_calc_cdclk =
14970 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020014971 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014972 dev_priv->display.modeset_commit_cdclk =
14973 broxton_modeset_commit_cdclk;
14974 dev_priv->display.modeset_calc_cdclk =
14975 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014976 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014977
Imre Deak88212942016-03-16 13:38:53 +020014978 switch (INTEL_INFO(dev_priv)->gen) {
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014979 case 2:
14980 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14981 break;
14982
14983 case 3:
14984 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14985 break;
14986
14987 case 4:
14988 case 5:
14989 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14990 break;
14991
14992 case 6:
14993 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14994 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014995 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014996 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014997 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14998 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014999 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000015000 /* Drop through - unsupported since execlist only. */
15001 default:
15002 /* Default just returns -ENODEV to indicate unsupported */
15003 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015004 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015005}
15006
Jesse Barnesb690e962010-07-19 13:53:12 -070015007/*
15008 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15009 * resume, or other times. This quirk makes sure that's the case for
15010 * affected systems.
15011 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015012static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015013{
15014 struct drm_i915_private *dev_priv = dev->dev_private;
15015
15016 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015017 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015018}
15019
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015020static void quirk_pipeb_force(struct drm_device *dev)
15021{
15022 struct drm_i915_private *dev_priv = dev->dev_private;
15023
15024 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15025 DRM_INFO("applying pipe b force quirk\n");
15026}
15027
Keith Packard435793d2011-07-12 14:56:22 -070015028/*
15029 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15030 */
15031static void quirk_ssc_force_disable(struct drm_device *dev)
15032{
15033 struct drm_i915_private *dev_priv = dev->dev_private;
15034 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015035 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015036}
15037
Carsten Emde4dca20e2012-03-15 15:56:26 +010015038/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015039 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15040 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015041 */
15042static void quirk_invert_brightness(struct drm_device *dev)
15043{
15044 struct drm_i915_private *dev_priv = dev->dev_private;
15045 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015046 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015047}
15048
Scot Doyle9c72cc62014-07-03 23:27:50 +000015049/* Some VBT's incorrectly indicate no backlight is present */
15050static void quirk_backlight_present(struct drm_device *dev)
15051{
15052 struct drm_i915_private *dev_priv = dev->dev_private;
15053 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15054 DRM_INFO("applying backlight present quirk\n");
15055}
15056
Jesse Barnesb690e962010-07-19 13:53:12 -070015057struct intel_quirk {
15058 int device;
15059 int subsystem_vendor;
15060 int subsystem_device;
15061 void (*hook)(struct drm_device *dev);
15062};
15063
Egbert Eich5f85f172012-10-14 15:46:38 +020015064/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15065struct intel_dmi_quirk {
15066 void (*hook)(struct drm_device *dev);
15067 const struct dmi_system_id (*dmi_id_list)[];
15068};
15069
15070static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15071{
15072 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15073 return 1;
15074}
15075
15076static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15077 {
15078 .dmi_id_list = &(const struct dmi_system_id[]) {
15079 {
15080 .callback = intel_dmi_reverse_brightness,
15081 .ident = "NCR Corporation",
15082 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15083 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15084 },
15085 },
15086 { } /* terminating entry */
15087 },
15088 .hook = quirk_invert_brightness,
15089 },
15090};
15091
Ben Widawskyc43b5632012-04-16 14:07:40 -070015092static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015093 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15094 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15095
Jesse Barnesb690e962010-07-19 13:53:12 -070015096 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15097 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15098
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015099 /* 830 needs to leave pipe A & dpll A up */
15100 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15101
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015102 /* 830 needs to leave pipe B & dpll B up */
15103 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15104
Keith Packard435793d2011-07-12 14:56:22 -070015105 /* Lenovo U160 cannot use SSC on LVDS */
15106 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015107
15108 /* Sony Vaio Y cannot use SSC on LVDS */
15109 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015110
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015111 /* Acer Aspire 5734Z must invert backlight brightness */
15112 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15113
15114 /* Acer/eMachines G725 */
15115 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15116
15117 /* Acer/eMachines e725 */
15118 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15119
15120 /* Acer/Packard Bell NCL20 */
15121 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15122
15123 /* Acer Aspire 4736Z */
15124 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015125
15126 /* Acer Aspire 5336 */
15127 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015128
15129 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15130 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015131
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015132 /* Acer C720 Chromebook (Core i3 4005U) */
15133 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15134
jens steinb2a96012014-10-28 20:25:53 +010015135 /* Apple Macbook 2,1 (Core 2 T7400) */
15136 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15137
Jani Nikula1b9448b2015-11-05 11:49:59 +020015138 /* Apple Macbook 4,1 */
15139 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15140
Scot Doyled4967d82014-07-03 23:27:52 +000015141 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15142 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015143
15144 /* HP Chromebook 14 (Celeron 2955U) */
15145 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015146
15147 /* Dell Chromebook 11 */
15148 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015149
15150 /* Dell Chromebook 11 (2015 version) */
15151 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015152};
15153
15154static void intel_init_quirks(struct drm_device *dev)
15155{
15156 struct pci_dev *d = dev->pdev;
15157 int i;
15158
15159 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15160 struct intel_quirk *q = &intel_quirks[i];
15161
15162 if (d->device == q->device &&
15163 (d->subsystem_vendor == q->subsystem_vendor ||
15164 q->subsystem_vendor == PCI_ANY_ID) &&
15165 (d->subsystem_device == q->subsystem_device ||
15166 q->subsystem_device == PCI_ANY_ID))
15167 q->hook(dev);
15168 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015169 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15170 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15171 intel_dmi_quirks[i].hook(dev);
15172 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015173}
15174
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015175/* Disable the VGA plane that we never use */
15176static void i915_disable_vga(struct drm_device *dev)
15177{
15178 struct drm_i915_private *dev_priv = dev->dev_private;
15179 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015180 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015181
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015182 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015183 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015184 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015185 sr1 = inb(VGA_SR_DATA);
15186 outb(sr1 | 1<<5, VGA_SR_DATA);
15187 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15188 udelay(300);
15189
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015190 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015191 POSTING_READ(vga_reg);
15192}
15193
Daniel Vetterf8175862012-04-10 15:50:11 +020015194void intel_modeset_init_hw(struct drm_device *dev)
15195{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015196 struct drm_i915_private *dev_priv = dev->dev_private;
15197
Ville Syrjäläb6283052015-06-03 15:45:07 +030015198 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015199
15200 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15201
Daniel Vetterf8175862012-04-10 15:50:11 +020015202 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015203 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015204}
15205
Matt Roperd93c0372015-12-03 11:37:41 -080015206/*
15207 * Calculate what we think the watermarks should be for the state we've read
15208 * out of the hardware and then immediately program those watermarks so that
15209 * we ensure the hardware settings match our internal state.
15210 *
15211 * We can calculate what we think WM's should be by creating a duplicate of the
15212 * current state (which was constructed during hardware readout) and running it
15213 * through the atomic check code to calculate new watermark values in the
15214 * state object.
15215 */
15216static void sanitize_watermarks(struct drm_device *dev)
15217{
15218 struct drm_i915_private *dev_priv = to_i915(dev);
15219 struct drm_atomic_state *state;
15220 struct drm_crtc *crtc;
15221 struct drm_crtc_state *cstate;
15222 struct drm_modeset_acquire_ctx ctx;
15223 int ret;
15224 int i;
15225
15226 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015227 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015228 return;
15229
15230 /*
15231 * We need to hold connection_mutex before calling duplicate_state so
15232 * that the connector loop is protected.
15233 */
15234 drm_modeset_acquire_init(&ctx, 0);
15235retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015236 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015237 if (ret == -EDEADLK) {
15238 drm_modeset_backoff(&ctx);
15239 goto retry;
15240 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015241 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015242 }
15243
15244 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15245 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015246 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015247
Matt Ropered4a6a72016-02-23 17:20:13 -080015248 /*
15249 * Hardware readout is the only time we don't want to calculate
15250 * intermediate watermarks (since we don't trust the current
15251 * watermarks).
15252 */
15253 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15254
Matt Roperd93c0372015-12-03 11:37:41 -080015255 ret = intel_atomic_check(dev, state);
15256 if (ret) {
15257 /*
15258 * If we fail here, it means that the hardware appears to be
15259 * programmed in a way that shouldn't be possible, given our
15260 * understanding of watermark requirements. This might mean a
15261 * mistake in the hardware readout code or a mistake in the
15262 * watermark calculations for a given platform. Raise a WARN
15263 * so that this is noticeable.
15264 *
15265 * If this actually happens, we'll have to just leave the
15266 * BIOS-programmed watermarks untouched and hope for the best.
15267 */
15268 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015269 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015270 }
15271
15272 /* Write calculated watermark values back */
15273 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15274 for_each_crtc_in_state(state, crtc, cstate, i) {
15275 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15276
Matt Ropered4a6a72016-02-23 17:20:13 -080015277 cs->wm.need_postvbl_update = true;
15278 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015279 }
15280
15281 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015282fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015283 drm_modeset_drop_locks(&ctx);
15284 drm_modeset_acquire_fini(&ctx);
15285}
15286
Jesse Barnes79e53942008-11-07 14:24:08 -080015287void intel_modeset_init(struct drm_device *dev)
15288{
Jesse Barnes652c3932009-08-17 13:31:43 -070015289 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015290 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015291 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015292 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015293
15294 drm_mode_config_init(dev);
15295
15296 dev->mode_config.min_width = 0;
15297 dev->mode_config.min_height = 0;
15298
Dave Airlie019d96c2011-09-29 16:20:42 +010015299 dev->mode_config.preferred_depth = 24;
15300 dev->mode_config.prefer_shadow = 1;
15301
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015302 dev->mode_config.allow_fb_modifiers = true;
15303
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015304 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015305
Jesse Barnesb690e962010-07-19 13:53:12 -070015306 intel_init_quirks(dev);
15307
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015308 intel_init_pm(dev);
15309
Ben Widawskye3c74752013-04-05 13:12:39 -070015310 if (INTEL_INFO(dev)->num_pipes == 0)
15311 return;
15312
Lukas Wunner69f92f62015-07-15 13:57:35 +020015313 /*
15314 * There may be no VBT; and if the BIOS enabled SSC we can
15315 * just keep using it to avoid unnecessary flicker. Whereas if the
15316 * BIOS isn't using it, don't assume it will work even if the VBT
15317 * indicates as much.
15318 */
15319 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15320 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15321 DREF_SSC1_ENABLE);
15322
15323 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15324 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15325 bios_lvds_use_ssc ? "en" : "dis",
15326 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15327 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15328 }
15329 }
15330
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015331 if (IS_GEN2(dev)) {
15332 dev->mode_config.max_width = 2048;
15333 dev->mode_config.max_height = 2048;
15334 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015335 dev->mode_config.max_width = 4096;
15336 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015337 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015338 dev->mode_config.max_width = 8192;
15339 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015340 }
Damien Lespiau068be562014-03-28 14:17:49 +000015341
Ville Syrjälädc41c152014-08-13 11:57:05 +030015342 if (IS_845G(dev) || IS_I865G(dev)) {
15343 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15344 dev->mode_config.cursor_height = 1023;
15345 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015346 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15347 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15348 } else {
15349 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15350 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15351 }
15352
Joonas Lahtinen62106b42016-03-18 10:42:57 +020015353 dev->mode_config.fb_base = dev_priv->ggtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015354
Zhao Yakui28c97732009-10-09 11:39:41 +080015355 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015356 INTEL_INFO(dev)->num_pipes,
15357 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015358
Damien Lespiau055e3932014-08-18 13:49:10 +010015359 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015360 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015361 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015362 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015363 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015364 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015365 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015366 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015367 }
15368
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015369 intel_update_czclk(dev_priv);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +020015370 intel_update_rawclk(dev_priv);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015371 intel_update_cdclk(dev);
15372
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015373 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015374
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015375 /* Just disable it once at startup */
15376 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015377 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015378
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015379 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015380 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015381 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015382
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015383 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015384 struct intel_initial_plane_config plane_config = {};
15385
Jesse Barnes46f297f2014-03-07 08:57:48 -080015386 if (!crtc->active)
15387 continue;
15388
Jesse Barnes46f297f2014-03-07 08:57:48 -080015389 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015390 * Note that reserving the BIOS fb up front prevents us
15391 * from stuffing other stolen allocations like the ring
15392 * on top. This prevents some ugliness at boot time, and
15393 * can even allow for smooth boot transitions if the BIOS
15394 * fb is large enough for the active pipe configuration.
15395 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015396 dev_priv->display.get_initial_plane_config(crtc,
15397 &plane_config);
15398
15399 /*
15400 * If the fb is shared between multiple heads, we'll
15401 * just get the first one.
15402 */
15403 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015404 }
Matt Roperd93c0372015-12-03 11:37:41 -080015405
15406 /*
15407 * Make sure hardware watermarks really match the state we read out.
15408 * Note that we need to do this after reconstructing the BIOS fb's
15409 * since the watermark calculation done here will use pstate->fb.
15410 */
15411 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015412}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015413
Daniel Vetter7fad7982012-07-04 17:51:47 +020015414static void intel_enable_pipe_a(struct drm_device *dev)
15415{
15416 struct intel_connector *connector;
15417 struct drm_connector *crt = NULL;
15418 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015419 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015420
15421 /* We can't just switch on the pipe A, we need to set things up with a
15422 * proper mode and output configuration. As a gross hack, enable pipe A
15423 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015424 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015425 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15426 crt = &connector->base;
15427 break;
15428 }
15429 }
15430
15431 if (!crt)
15432 return;
15433
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015434 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015435 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015436}
15437
Daniel Vetterfa555832012-10-10 23:14:00 +020015438static bool
15439intel_check_plane_mapping(struct intel_crtc *crtc)
15440{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015441 struct drm_device *dev = crtc->base.dev;
15442 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015443 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015444
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015445 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015446 return true;
15447
Ville Syrjälä649636e2015-09-22 19:50:01 +030015448 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015449
15450 if ((val & DISPLAY_PLANE_ENABLE) &&
15451 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15452 return false;
15453
15454 return true;
15455}
15456
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015457static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15458{
15459 struct drm_device *dev = crtc->base.dev;
15460 struct intel_encoder *encoder;
15461
15462 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15463 return true;
15464
15465 return false;
15466}
15467
Ville Syrjälädd756192016-02-17 21:28:45 +020015468static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15469{
15470 struct drm_device *dev = encoder->base.dev;
15471 struct intel_connector *connector;
15472
15473 for_each_connector_on_encoder(dev, &encoder->base, connector)
15474 return true;
15475
15476 return false;
15477}
15478
Daniel Vetter24929352012-07-02 20:28:59 +020015479static void intel_sanitize_crtc(struct intel_crtc *crtc)
15480{
15481 struct drm_device *dev = crtc->base.dev;
15482 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015483 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015484
Daniel Vetter24929352012-07-02 20:28:59 +020015485 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015486 if (!transcoder_is_dsi(cpu_transcoder)) {
15487 i915_reg_t reg = PIPECONF(cpu_transcoder);
15488
15489 I915_WRITE(reg,
15490 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15491 }
Daniel Vetter24929352012-07-02 20:28:59 +020015492
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015493 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015494 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015495 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015496 struct intel_plane *plane;
15497
Daniel Vetter96256042015-02-13 21:03:42 +010015498 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015499
15500 /* Disable everything but the primary plane */
15501 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15502 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15503 continue;
15504
15505 plane->disable_plane(&plane->base, &crtc->base);
15506 }
Daniel Vetter96256042015-02-13 21:03:42 +010015507 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015508
Daniel Vetter24929352012-07-02 20:28:59 +020015509 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015510 * disable the crtc (and hence change the state) if it is wrong. Note
15511 * that gen4+ has a fixed plane -> pipe mapping. */
15512 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015513 bool plane;
15514
Daniel Vetter24929352012-07-02 20:28:59 +020015515 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15516 crtc->base.base.id);
15517
15518 /* Pipe has the wrong plane attached and the plane is active.
15519 * Temporarily change the plane mapping and disable everything
15520 * ... */
15521 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015522 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015523 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015524 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015525 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015526 }
Daniel Vetter24929352012-07-02 20:28:59 +020015527
Daniel Vetter7fad7982012-07-04 17:51:47 +020015528 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15529 crtc->pipe == PIPE_A && !crtc->active) {
15530 /* BIOS forgot to enable pipe A, this mostly happens after
15531 * resume. Force-enable the pipe to fix this, the update_dpms
15532 * call below we restore the pipe to the right state, but leave
15533 * the required bits on. */
15534 intel_enable_pipe_a(dev);
15535 }
15536
Daniel Vetter24929352012-07-02 20:28:59 +020015537 /* Adjust the state of the output pipe according to whether we
15538 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015539 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015540 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015541
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015542 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015543 /*
15544 * We start out with underrun reporting disabled to avoid races.
15545 * For correct bookkeeping mark this on active crtcs.
15546 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015547 * Also on gmch platforms we dont have any hardware bits to
15548 * disable the underrun reporting. Which means we need to start
15549 * out with underrun reporting disabled also on inactive pipes,
15550 * since otherwise we'll complain about the garbage we read when
15551 * e.g. coming up after runtime pm.
15552 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015553 * No protection against concurrent access is required - at
15554 * worst a fifo underrun happens which also sets this to false.
15555 */
15556 crtc->cpu_fifo_underrun_disabled = true;
15557 crtc->pch_fifo_underrun_disabled = true;
15558 }
Daniel Vetter24929352012-07-02 20:28:59 +020015559}
15560
15561static void intel_sanitize_encoder(struct intel_encoder *encoder)
15562{
15563 struct intel_connector *connector;
15564 struct drm_device *dev = encoder->base.dev;
15565
15566 /* We need to check both for a crtc link (meaning that the
15567 * encoder is active and trying to read from a pipe) and the
15568 * pipe itself being active. */
15569 bool has_active_crtc = encoder->base.crtc &&
15570 to_intel_crtc(encoder->base.crtc)->active;
15571
Ville Syrjälädd756192016-02-17 21:28:45 +020015572 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015573 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15574 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015575 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015576
15577 /* Connector is active, but has no active pipe. This is
15578 * fallout from our resume register restoring. Disable
15579 * the encoder manually again. */
15580 if (encoder->base.crtc) {
15581 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15582 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015583 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015584 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015585 if (encoder->post_disable)
15586 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015587 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015588 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015589
15590 /* Inconsistent output/port/pipe state happens presumably due to
15591 * a bug in one of the get_hw_state functions. Or someplace else
15592 * in our code, like the register restore mess on resume. Clamp
15593 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015594 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015595 if (connector->encoder != encoder)
15596 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015597 connector->base.dpms = DRM_MODE_DPMS_OFF;
15598 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015599 }
15600 }
15601 /* Enabled encoders without active connectors will be fixed in
15602 * the crtc fixup. */
15603}
15604
Imre Deak04098752014-02-18 00:02:16 +020015605void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015606{
15607 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015608 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015609
Imre Deak04098752014-02-18 00:02:16 +020015610 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15611 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15612 i915_disable_vga(dev);
15613 }
15614}
15615
15616void i915_redisable_vga(struct drm_device *dev)
15617{
15618 struct drm_i915_private *dev_priv = dev->dev_private;
15619
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015620 /* This function can be called both from intel_modeset_setup_hw_state or
15621 * at a very early point in our resume sequence, where the power well
15622 * structures are not yet restored. Since this function is at a very
15623 * paranoid "someone might have enabled VGA while we were not looking"
15624 * level, just check if the power well is enabled instead of trying to
15625 * follow the "don't touch the power well if we don't need it" policy
15626 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015627 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015628 return;
15629
Imre Deak04098752014-02-18 00:02:16 +020015630 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015631
15632 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015633}
15634
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015635static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015636{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015637 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015638
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015639 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015640}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015641
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015642/* FIXME read out full plane state for all planes */
15643static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015644{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015645 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015646 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015647 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015648
Matt Roper19b8d382015-09-24 15:53:17 -070015649 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015650 primary_get_hw_state(to_intel_plane(primary));
15651
15652 if (plane_state->visible)
15653 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015654}
15655
Daniel Vetter30e984d2013-06-05 13:34:17 +020015656static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015657{
15658 struct drm_i915_private *dev_priv = dev->dev_private;
15659 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015660 struct intel_crtc *crtc;
15661 struct intel_encoder *encoder;
15662 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015663 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015664
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015665 dev_priv->active_crtcs = 0;
15666
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015667 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015668 struct intel_crtc_state *crtc_state = crtc->config;
15669 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015670
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015671 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15672 memset(crtc_state, 0, sizeof(*crtc_state));
15673 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015674
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015675 crtc_state->base.active = crtc_state->base.enable =
15676 dev_priv->display.get_pipe_config(crtc, crtc_state);
15677
15678 crtc->base.enabled = crtc_state->base.enable;
15679 crtc->active = crtc_state->base.active;
15680
15681 if (crtc_state->base.active) {
15682 dev_priv->active_crtcs |= 1 << crtc->pipe;
15683
15684 if (IS_BROADWELL(dev_priv)) {
15685 pixclk = ilk_pipe_pixel_rate(crtc_state);
15686
15687 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15688 if (crtc_state->ips_enabled)
15689 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15690 } else if (IS_VALLEYVIEW(dev_priv) ||
15691 IS_CHERRYVIEW(dev_priv) ||
15692 IS_BROXTON(dev_priv))
15693 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15694 else
15695 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15696 }
15697
15698 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015699
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015700 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015701
15702 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15703 crtc->base.base.id,
15704 crtc->active ? "enabled" : "disabled");
15705 }
15706
Daniel Vetter53589012013-06-05 13:34:16 +020015707 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15708 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15709
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015710 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15711 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015712 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015713 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015714 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015715 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015716 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015717 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015718
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015719 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015720 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015721 }
15722
Damien Lespiaub2784e12014-08-05 11:29:37 +010015723 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015724 pipe = 0;
15725
15726 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015727 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15728 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015729 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015730 } else {
15731 encoder->base.crtc = NULL;
15732 }
15733
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015734 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015735 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015736 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015737 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015738 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015739 }
15740
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015741 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015742 if (connector->get_hw_state(connector)) {
15743 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015744
15745 encoder = connector->encoder;
15746 connector->base.encoder = &encoder->base;
15747
15748 if (encoder->base.crtc &&
15749 encoder->base.crtc->state->active) {
15750 /*
15751 * This has to be done during hardware readout
15752 * because anything calling .crtc_disable may
15753 * rely on the connector_mask being accurate.
15754 */
15755 encoder->base.crtc->state->connector_mask |=
15756 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015757 encoder->base.crtc->state->encoder_mask |=
15758 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015759 }
15760
Daniel Vetter24929352012-07-02 20:28:59 +020015761 } else {
15762 connector->base.dpms = DRM_MODE_DPMS_OFF;
15763 connector->base.encoder = NULL;
15764 }
15765 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15766 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015767 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015768 connector->base.encoder ? "enabled" : "disabled");
15769 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015770
15771 for_each_intel_crtc(dev, crtc) {
15772 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15773
15774 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15775 if (crtc->base.state->active) {
15776 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15777 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15778 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15779
15780 /*
15781 * The initial mode needs to be set in order to keep
15782 * the atomic core happy. It wants a valid mode if the
15783 * crtc's enabled, so we do the above call.
15784 *
15785 * At this point some state updated by the connectors
15786 * in their ->detect() callback has not run yet, so
15787 * no recalculation can be done yet.
15788 *
15789 * Even if we could do a recalculation and modeset
15790 * right now it would cause a double modeset if
15791 * fbdev or userspace chooses a different initial mode.
15792 *
15793 * If that happens, someone indicated they wanted a
15794 * mode change, which means it's safe to do a full
15795 * recalculation.
15796 */
15797 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015798
15799 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15800 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015801 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015802
15803 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015804 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015805}
15806
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015807/* Scan out the current hw modeset state,
15808 * and sanitizes it to the current state
15809 */
15810static void
15811intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015812{
15813 struct drm_i915_private *dev_priv = dev->dev_private;
15814 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015815 struct intel_crtc *crtc;
15816 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015817 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015818
15819 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015820
15821 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015822 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015823 intel_sanitize_encoder(encoder);
15824 }
15825
Damien Lespiau055e3932014-08-18 13:49:10 +010015826 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015827 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15828 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015829 intel_dump_pipe_config(crtc, crtc->config,
15830 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015831 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015832
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015833 intel_modeset_update_connector_atomic_state(dev);
15834
Daniel Vetter35c95372013-07-17 06:55:04 +020015835 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15836 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15837
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015838 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015839 continue;
15840
15841 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15842
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015843 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015844 pll->on = false;
15845 }
15846
Wayne Boyer666a4532015-12-09 12:29:35 -080015847 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015848 vlv_wm_get_hw_state(dev);
15849 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015850 skl_wm_get_hw_state(dev);
15851 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015852 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015853
15854 for_each_intel_crtc(dev, crtc) {
15855 unsigned long put_domains;
15856
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015857 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015858 if (WARN_ON(put_domains))
15859 modeset_put_power_domains(dev_priv, put_domains);
15860 }
15861 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015862
15863 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015864}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015865
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015866void intel_display_resume(struct drm_device *dev)
15867{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015868 struct drm_i915_private *dev_priv = to_i915(dev);
15869 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15870 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015871 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015872 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020015873
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015874 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015875
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015876 /*
15877 * This is a cludge because with real atomic modeset mode_config.mutex
15878 * won't be taken. Unfortunately some probed state like
15879 * audio_codec_enable is still protected by mode_config.mutex, so lock
15880 * it here for now.
15881 */
15882 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015883 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015884
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015885retry:
15886 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015887
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015888 if (ret == 0 && !setup) {
15889 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015890
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015891 intel_modeset_setup_hw_state(dev);
15892 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015893 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015894
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015895 if (ret == 0 && state) {
15896 struct drm_crtc_state *crtc_state;
15897 struct drm_crtc *crtc;
15898 int i;
15899
15900 state->acquire_ctx = &ctx;
15901
15902 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15903 /*
15904 * Force recalculation even if we restore
15905 * current state. With fast modeset this may not result
15906 * in a modeset when the state is compatible.
15907 */
15908 crtc_state->mode_changed = true;
15909 }
15910
15911 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015912 }
15913
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015914 if (ret == -EDEADLK) {
15915 drm_modeset_backoff(&ctx);
15916 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015917 }
15918
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015919 drm_modeset_drop_locks(&ctx);
15920 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015921 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015922
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015923 if (ret) {
15924 DRM_ERROR("Restoring old state failed with %i\n", ret);
15925 drm_atomic_state_free(state);
15926 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015927}
15928
15929void intel_modeset_gem_init(struct drm_device *dev)
15930{
Jesse Barnes484b41d2014-03-07 08:57:55 -080015931 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015932 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015933 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015934
Imre Deakae484342014-03-31 15:10:44 +030015935 intel_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +030015936
Chris Wilson1833b132012-05-09 11:56:28 +010015937 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015938
15939 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015940
15941 /*
15942 * Make sure any fbs we allocated at startup are properly
15943 * pinned & fenced. When we do the allocation it's too early
15944 * for this.
15945 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015946 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015947 obj = intel_fb_obj(c->primary->fb);
15948 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015949 continue;
15950
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015951 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020015952 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
15953 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015954 mutex_unlock(&dev->struct_mutex);
15955 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015956 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15957 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015958 drm_framebuffer_unreference(c->primary->fb);
15959 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015960 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015961 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015962 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015963 }
15964 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015965
15966 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015967}
15968
Imre Deak4932e2c2014-02-11 17:12:48 +020015969void intel_connector_unregister(struct intel_connector *intel_connector)
15970{
15971 struct drm_connector *connector = &intel_connector->base;
15972
15973 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015974 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015975}
15976
Jesse Barnes79e53942008-11-07 14:24:08 -080015977void intel_modeset_cleanup(struct drm_device *dev)
15978{
Jesse Barnes652c3932009-08-17 13:31:43 -070015979 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020015980 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015981
Imre Deak2eb52522014-11-19 15:30:05 +020015982 intel_disable_gt_powersave(dev);
15983
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015984 intel_backlight_unregister(dev);
15985
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015986 /*
15987 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015988 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015989 * experience fancy races otherwise.
15990 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015991 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015992
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015993 /*
15994 * Due to the hpd irq storm handling the hotplug work can re-arm the
15995 * poll handlers. Hence disable polling after hpd handling is shut down.
15996 */
Keith Packardf87ea762010-10-03 19:36:26 -070015997 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015998
Jesse Barnes723bfd72010-10-07 16:01:13 -070015999 intel_unregister_dsm_handler();
16000
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016001 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016002
Chris Wilson1630fe72011-07-08 12:22:42 +010016003 /* flush any delayed tasks or pending work */
16004 flush_scheduled_work();
16005
Jani Nikuladb31af1d2013-11-08 16:48:53 +020016006 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020016007 for_each_intel_connector(dev, connector)
16008 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030016009
Jesse Barnes79e53942008-11-07 14:24:08 -080016010 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016011
16012 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030016013
Imre Deakae484342014-03-31 15:10:44 +030016014 intel_cleanup_gt_powersave(dev);
Daniel Vetterf5949142016-01-13 11:55:28 +010016015
16016 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016017}
16018
Dave Airlie28d52042009-09-21 14:33:58 +100016019/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080016020 * Return which encoder is currently attached for connector.
16021 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010016022struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080016023{
Chris Wilsondf0e9242010-09-09 16:20:55 +010016024 return &intel_attached_encoder(connector)->base;
16025}
Jesse Barnes79e53942008-11-07 14:24:08 -080016026
Chris Wilsondf0e9242010-09-09 16:20:55 +010016027void intel_connector_attach_encoder(struct intel_connector *connector,
16028 struct intel_encoder *encoder)
16029{
16030 connector->encoder = encoder;
16031 drm_mode_connector_attach_encoder(&connector->base,
16032 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016033}
Dave Airlie28d52042009-09-21 14:33:58 +100016034
16035/*
16036 * set vga decode state - true == enable VGA decode
16037 */
16038int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16039{
16040 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016041 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016042 u16 gmch_ctrl;
16043
Chris Wilson75fa0412014-02-07 18:37:02 -020016044 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16045 DRM_ERROR("failed to read control word\n");
16046 return -EIO;
16047 }
16048
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016049 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16050 return 0;
16051
Dave Airlie28d52042009-09-21 14:33:58 +100016052 if (state)
16053 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16054 else
16055 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016056
16057 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16058 DRM_ERROR("failed to write control word\n");
16059 return -EIO;
16060 }
16061
Dave Airlie28d52042009-09-21 14:33:58 +100016062 return 0;
16063}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016064
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016065struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016066
16067 u32 power_well_driver;
16068
Chris Wilson63b66e52013-08-08 15:12:06 +020016069 int num_transcoders;
16070
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016071 struct intel_cursor_error_state {
16072 u32 control;
16073 u32 position;
16074 u32 base;
16075 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016076 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016077
16078 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016079 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016080 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030016081 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016082 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016083
16084 struct intel_plane_error_state {
16085 u32 control;
16086 u32 stride;
16087 u32 size;
16088 u32 pos;
16089 u32 addr;
16090 u32 surface;
16091 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016092 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016093
16094 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016095 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016096 enum transcoder cpu_transcoder;
16097
16098 u32 conf;
16099
16100 u32 htotal;
16101 u32 hblank;
16102 u32 hsync;
16103 u32 vtotal;
16104 u32 vblank;
16105 u32 vsync;
16106 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016107};
16108
16109struct intel_display_error_state *
16110intel_display_capture_error_state(struct drm_device *dev)
16111{
Jani Nikulafbee40d2014-03-31 14:27:18 +030016112 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016113 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016114 int transcoders[] = {
16115 TRANSCODER_A,
16116 TRANSCODER_B,
16117 TRANSCODER_C,
16118 TRANSCODER_EDP,
16119 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016120 int i;
16121
Chris Wilson63b66e52013-08-08 15:12:06 +020016122 if (INTEL_INFO(dev)->num_pipes == 0)
16123 return NULL;
16124
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016125 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016126 if (error == NULL)
16127 return NULL;
16128
Imre Deak190be112013-11-25 17:15:31 +020016129 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016130 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16131
Damien Lespiau055e3932014-08-18 13:49:10 +010016132 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016133 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016134 __intel_display_power_is_enabled(dev_priv,
16135 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016136 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016137 continue;
16138
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016139 error->cursor[i].control = I915_READ(CURCNTR(i));
16140 error->cursor[i].position = I915_READ(CURPOS(i));
16141 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016142
16143 error->plane[i].control = I915_READ(DSPCNTR(i));
16144 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016145 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016146 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016147 error->plane[i].pos = I915_READ(DSPPOS(i));
16148 }
Paulo Zanonica291362013-03-06 20:03:14 -030016149 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16150 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016151 if (INTEL_INFO(dev)->gen >= 4) {
16152 error->plane[i].surface = I915_READ(DSPSURF(i));
16153 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16154 }
16155
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016156 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030016157
Sonika Jindal3abfce72014-07-21 15:23:43 +053016158 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030016159 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016160 }
16161
Jani Nikula4d1de972016-03-18 17:05:42 +020016162 /* Note: this does not include DSI transcoders. */
Chris Wilson63b66e52013-08-08 15:12:06 +020016163 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16164 if (HAS_DDI(dev_priv->dev))
16165 error->num_transcoders++; /* Account for eDP. */
16166
16167 for (i = 0; i < error->num_transcoders; i++) {
16168 enum transcoder cpu_transcoder = transcoders[i];
16169
Imre Deakddf9c532013-11-27 22:02:02 +020016170 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016171 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016172 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016173 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016174 continue;
16175
Chris Wilson63b66e52013-08-08 15:12:06 +020016176 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16177
16178 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16179 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16180 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16181 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16182 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16183 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16184 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016185 }
16186
16187 return error;
16188}
16189
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016190#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16191
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016192void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016193intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016194 struct drm_device *dev,
16195 struct intel_display_error_state *error)
16196{
Damien Lespiau055e3932014-08-18 13:49:10 +010016197 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016198 int i;
16199
Chris Wilson63b66e52013-08-08 15:12:06 +020016200 if (!error)
16201 return;
16202
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016203 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016204 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016205 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016206 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016207 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016208 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016209 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016210 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016211 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016212 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016213
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016214 err_printf(m, "Plane [%d]:\n", i);
16215 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16216 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016217 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016218 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16219 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016220 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016221 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016222 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016223 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016224 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16225 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016226 }
16227
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016228 err_printf(m, "Cursor [%d]:\n", i);
16229 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16230 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16231 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016232 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016233
16234 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016235 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016236 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016237 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016238 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016239 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16240 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16241 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16242 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16243 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16244 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16245 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16246 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016247}