blob: af74cdba708175b547ebc3def88801dbf20eda19 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Imre Deakdb18b6a2016-03-24 12:41:40 +020039#include "intel_dsi.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070040#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080041#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080042#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010043#include <drm/drm_dp_helper.h>
44#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070045#include <drm/drm_plane_helper.h>
46#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080047#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080048#include <linux/reservation.h>
49#include <linux/dma-buf.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080050
Matt Roper465c1202014-05-29 08:06:54 -070051/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010052static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010053 DRM_FORMAT_C8,
54 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070055 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010056 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070057};
58
59/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010060static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010061 DRM_FORMAT_C8,
62 DRM_FORMAT_RGB565,
63 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070064 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010065 DRM_FORMAT_XRGB2101010,
66 DRM_FORMAT_XBGR2101010,
67};
68
69static const uint32_t skl_primary_formats[] = {
70 DRM_FORMAT_C8,
71 DRM_FORMAT_RGB565,
72 DRM_FORMAT_XRGB8888,
73 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010074 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070075 DRM_FORMAT_ABGR8888,
76 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070077 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053078 DRM_FORMAT_YUYV,
79 DRM_FORMAT_YVYU,
80 DRM_FORMAT_UYVY,
81 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070082};
83
Matt Roper3d7d6512014-06-10 08:28:13 -070084/* Cursor formats */
85static const uint32_t intel_cursor_formats[] = {
86 DRM_FORMAT_ARGB8888,
87};
88
Jesse Barnesf1f644d2013-06-27 00:39:25 +030089static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020090 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030091static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020092 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030093
Jesse Barneseb1bfe82014-02-12 12:26:25 -080094static int intel_framebuffer_init(struct drm_device *dev,
95 struct intel_framebuffer *ifb,
96 struct drm_mode_fb_cmd2 *mode_cmd,
97 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020098static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
99static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +0200100static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200101static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700102 struct intel_link_m_n *m_n,
103 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200104static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200105static void haswell_set_pipeconf(struct drm_crtc *crtc);
Jani Nikula391bf042016-03-18 17:05:40 +0200106static void haswell_set_pipemisc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200107static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200108 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200109static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200110 const struct intel_crtc_state *pipe_config);
Maarten Lankhorst613d2b22015-07-21 13:28:58 +0200111static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
112static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700113static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
114 struct intel_crtc_state *crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200115static void skylake_pfit_enable(struct intel_crtc *crtc);
116static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
117static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200118static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200119static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100120
Jesse Barnes79e53942008-11-07 14:24:08 -0800121typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400122 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800123} intel_range_t;
124
125typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400126 int dot_limit;
127 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800128} intel_p2_t;
129
Ma Lingd4906092009-03-18 20:13:27 +0800130typedef struct intel_limit intel_limit_t;
131struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400132 intel_range_t dot, vco, n, m, m1, m2, p, p1;
133 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800134};
Jesse Barnes79e53942008-11-07 14:24:08 -0800135
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300136/* returns HPLL frequency in kHz */
137static int valleyview_get_vco(struct drm_i915_private *dev_priv)
138{
139 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
140
141 /* Obtain SKU information */
142 mutex_lock(&dev_priv->sb_lock);
143 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
144 CCK_FUSE_HPLL_FREQ_MASK;
145 mutex_unlock(&dev_priv->sb_lock);
146
147 return vco_freq[hpll_freq] * 1000;
148}
149
150static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
151 const char *name, u32 reg)
152{
153 u32 val;
154 int divider;
155
156 if (dev_priv->hpll_freq == 0)
157 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
158
159 mutex_lock(&dev_priv->sb_lock);
160 val = vlv_cck_read(dev_priv, reg);
161 mutex_unlock(&dev_priv->sb_lock);
162
163 divider = val & CCK_FREQUENCY_VALUES;
164
165 WARN((val & CCK_FREQUENCY_STATUS) !=
166 (divider << CCK_FREQUENCY_STATUS_SHIFT),
167 "%s change in progress\n", name);
168
169 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
170}
171
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200172static int
173intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200174{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200175 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200176}
177
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200178static int
179intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300180{
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200181 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
182 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200183}
184
185static int
186intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
187{
Jani Nikula79e50a42015-08-26 10:58:20 +0300188 uint32_t clkcfg;
189
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200190 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300191 clkcfg = I915_READ(CLKCFG);
192 switch (clkcfg & CLKCFG_FSB_MASK) {
193 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200194 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300195 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200196 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300197 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200198 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300199 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200200 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300201 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200202 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300203 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200204 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300205 /* these two are just a guess; one of them might be right */
206 case CLKCFG_FSB_1600:
207 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200208 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300209 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200210 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300211 }
212}
213
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200214static void intel_update_rawclk(struct drm_i915_private *dev_priv)
215{
216 if (HAS_PCH_SPLIT(dev_priv))
217 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
218 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
219 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
220 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
221 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
222 else
223 return; /* no rawclk on other platforms, or no need to know it */
224
225 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
226}
227
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300228static void intel_update_czclk(struct drm_i915_private *dev_priv)
229{
Wayne Boyer666a4532015-12-09 12:29:35 -0800230 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300231 return;
232
233 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
234 CCK_CZ_CLOCK_CONTROL);
235
236 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
237}
238
Chris Wilson021357a2010-09-07 20:54:59 +0100239static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200240intel_fdi_link_freq(struct drm_i915_private *dev_priv,
241 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100242{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200243 if (HAS_DDI(dev_priv))
244 return pipe_config->port_clock; /* SPLL */
245 else if (IS_GEN5(dev_priv))
246 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200247 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200248 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100249}
250
Daniel Vetter5d536e22013-07-06 12:52:06 +0200251static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400252 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200253 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200254 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400255 .m = { .min = 96, .max = 140 },
256 .m1 = { .min = 18, .max = 26 },
257 .m2 = { .min = 6, .max = 16 },
258 .p = { .min = 4, .max = 128 },
259 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700260 .p2 = { .dot_limit = 165000,
261 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700262};
263
Daniel Vetter5d536e22013-07-06 12:52:06 +0200264static const intel_limit_t intel_limits_i8xx_dvo = {
265 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200266 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200267 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200268 .m = { .min = 96, .max = 140 },
269 .m1 = { .min = 18, .max = 26 },
270 .m2 = { .min = 6, .max = 16 },
271 .p = { .min = 4, .max = 128 },
272 .p1 = { .min = 2, .max = 33 },
273 .p2 = { .dot_limit = 165000,
274 .p2_slow = 4, .p2_fast = 4 },
275};
276
Keith Packarde4b36692009-06-05 19:22:17 -0700277static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400278 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200279 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200280 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400281 .m = { .min = 96, .max = 140 },
282 .m1 = { .min = 18, .max = 26 },
283 .m2 = { .min = 6, .max = 16 },
284 .p = { .min = 4, .max = 128 },
285 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700286 .p2 = { .dot_limit = 165000,
287 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700288};
Eric Anholt273e27c2011-03-30 13:01:10 -0700289
Keith Packarde4b36692009-06-05 19:22:17 -0700290static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400291 .dot = { .min = 20000, .max = 400000 },
292 .vco = { .min = 1400000, .max = 2800000 },
293 .n = { .min = 1, .max = 6 },
294 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100295 .m1 = { .min = 8, .max = 18 },
296 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400297 .p = { .min = 5, .max = 80 },
298 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700299 .p2 = { .dot_limit = 200000,
300 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700301};
302
303static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400304 .dot = { .min = 20000, .max = 400000 },
305 .vco = { .min = 1400000, .max = 2800000 },
306 .n = { .min = 1, .max = 6 },
307 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100308 .m1 = { .min = 8, .max = 18 },
309 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400310 .p = { .min = 7, .max = 98 },
311 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700312 .p2 = { .dot_limit = 112000,
313 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700314};
315
Eric Anholt273e27c2011-03-30 13:01:10 -0700316
Keith Packarde4b36692009-06-05 19:22:17 -0700317static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700318 .dot = { .min = 25000, .max = 270000 },
319 .vco = { .min = 1750000, .max = 3500000},
320 .n = { .min = 1, .max = 4 },
321 .m = { .min = 104, .max = 138 },
322 .m1 = { .min = 17, .max = 23 },
323 .m2 = { .min = 5, .max = 11 },
324 .p = { .min = 10, .max = 30 },
325 .p1 = { .min = 1, .max = 3},
326 .p2 = { .dot_limit = 270000,
327 .p2_slow = 10,
328 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800329 },
Keith Packarde4b36692009-06-05 19:22:17 -0700330};
331
332static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700333 .dot = { .min = 22000, .max = 400000 },
334 .vco = { .min = 1750000, .max = 3500000},
335 .n = { .min = 1, .max = 4 },
336 .m = { .min = 104, .max = 138 },
337 .m1 = { .min = 16, .max = 23 },
338 .m2 = { .min = 5, .max = 11 },
339 .p = { .min = 5, .max = 80 },
340 .p1 = { .min = 1, .max = 8},
341 .p2 = { .dot_limit = 165000,
342 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700343};
344
345static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700346 .dot = { .min = 20000, .max = 115000 },
347 .vco = { .min = 1750000, .max = 3500000 },
348 .n = { .min = 1, .max = 3 },
349 .m = { .min = 104, .max = 138 },
350 .m1 = { .min = 17, .max = 23 },
351 .m2 = { .min = 5, .max = 11 },
352 .p = { .min = 28, .max = 112 },
353 .p1 = { .min = 2, .max = 8 },
354 .p2 = { .dot_limit = 0,
355 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800356 },
Keith Packarde4b36692009-06-05 19:22:17 -0700357};
358
359static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700360 .dot = { .min = 80000, .max = 224000 },
361 .vco = { .min = 1750000, .max = 3500000 },
362 .n = { .min = 1, .max = 3 },
363 .m = { .min = 104, .max = 138 },
364 .m1 = { .min = 17, .max = 23 },
365 .m2 = { .min = 5, .max = 11 },
366 .p = { .min = 14, .max = 42 },
367 .p1 = { .min = 2, .max = 6 },
368 .p2 = { .dot_limit = 0,
369 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800370 },
Keith Packarde4b36692009-06-05 19:22:17 -0700371};
372
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500373static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400374 .dot = { .min = 20000, .max = 400000},
375 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700376 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400377 .n = { .min = 3, .max = 6 },
378 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700379 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400380 .m1 = { .min = 0, .max = 0 },
381 .m2 = { .min = 0, .max = 254 },
382 .p = { .min = 5, .max = 80 },
383 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700384 .p2 = { .dot_limit = 200000,
385 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700386};
387
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500388static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400389 .dot = { .min = 20000, .max = 400000 },
390 .vco = { .min = 1700000, .max = 3500000 },
391 .n = { .min = 3, .max = 6 },
392 .m = { .min = 2, .max = 256 },
393 .m1 = { .min = 0, .max = 0 },
394 .m2 = { .min = 0, .max = 254 },
395 .p = { .min = 7, .max = 112 },
396 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700397 .p2 = { .dot_limit = 112000,
398 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700399};
400
Eric Anholt273e27c2011-03-30 13:01:10 -0700401/* Ironlake / Sandybridge
402 *
403 * We calculate clock using (register_value + 2) for N/M1/M2, so here
404 * the range value for them is (actual_value - 2).
405 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800406static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700407 .dot = { .min = 25000, .max = 350000 },
408 .vco = { .min = 1760000, .max = 3510000 },
409 .n = { .min = 1, .max = 5 },
410 .m = { .min = 79, .max = 127 },
411 .m1 = { .min = 12, .max = 22 },
412 .m2 = { .min = 5, .max = 9 },
413 .p = { .min = 5, .max = 80 },
414 .p1 = { .min = 1, .max = 8 },
415 .p2 = { .dot_limit = 225000,
416 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700417};
418
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800419static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700420 .dot = { .min = 25000, .max = 350000 },
421 .vco = { .min = 1760000, .max = 3510000 },
422 .n = { .min = 1, .max = 3 },
423 .m = { .min = 79, .max = 118 },
424 .m1 = { .min = 12, .max = 22 },
425 .m2 = { .min = 5, .max = 9 },
426 .p = { .min = 28, .max = 112 },
427 .p1 = { .min = 2, .max = 8 },
428 .p2 = { .dot_limit = 225000,
429 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800430};
431
432static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700433 .dot = { .min = 25000, .max = 350000 },
434 .vco = { .min = 1760000, .max = 3510000 },
435 .n = { .min = 1, .max = 3 },
436 .m = { .min = 79, .max = 127 },
437 .m1 = { .min = 12, .max = 22 },
438 .m2 = { .min = 5, .max = 9 },
439 .p = { .min = 14, .max = 56 },
440 .p1 = { .min = 2, .max = 8 },
441 .p2 = { .dot_limit = 225000,
442 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800443};
444
Eric Anholt273e27c2011-03-30 13:01:10 -0700445/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800446static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700447 .dot = { .min = 25000, .max = 350000 },
448 .vco = { .min = 1760000, .max = 3510000 },
449 .n = { .min = 1, .max = 2 },
450 .m = { .min = 79, .max = 126 },
451 .m1 = { .min = 12, .max = 22 },
452 .m2 = { .min = 5, .max = 9 },
453 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400454 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700455 .p2 = { .dot_limit = 225000,
456 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800457};
458
459static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700460 .dot = { .min = 25000, .max = 350000 },
461 .vco = { .min = 1760000, .max = 3510000 },
462 .n = { .min = 1, .max = 3 },
463 .m = { .min = 79, .max = 126 },
464 .m1 = { .min = 12, .max = 22 },
465 .m2 = { .min = 5, .max = 9 },
466 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400467 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700468 .p2 = { .dot_limit = 225000,
469 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800470};
471
Ville Syrjälädc730512013-09-24 21:26:30 +0300472static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300473 /*
474 * These are the data rate limits (measured in fast clocks)
475 * since those are the strictest limits we have. The fast
476 * clock and actual rate limits are more relaxed, so checking
477 * them would make no difference.
478 */
479 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200480 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700481 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700482 .m1 = { .min = 2, .max = 3 },
483 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300484 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300485 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700486};
487
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300488static const intel_limit_t intel_limits_chv = {
489 /*
490 * These are the data rate limits (measured in fast clocks)
491 * since those are the strictest limits we have. The fast
492 * clock and actual rate limits are more relaxed, so checking
493 * them would make no difference.
494 */
495 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200496 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300497 .n = { .min = 1, .max = 1 },
498 .m1 = { .min = 2, .max = 2 },
499 .m2 = { .min = 24 << 22, .max = 175 << 22 },
500 .p1 = { .min = 2, .max = 4 },
501 .p2 = { .p2_slow = 1, .p2_fast = 14 },
502};
503
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200504static const intel_limit_t intel_limits_bxt = {
505 /* FIXME: find real dot limits */
506 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530507 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200508 .n = { .min = 1, .max = 1 },
509 .m1 = { .min = 2, .max = 2 },
510 /* FIXME: find real m2 limits */
511 .m2 = { .min = 2 << 22, .max = 255 << 22 },
512 .p1 = { .min = 2, .max = 4 },
513 .p2 = { .p2_slow = 1, .p2_fast = 20 },
514};
515
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200516static bool
517needs_modeset(struct drm_crtc_state *state)
518{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200519 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200520}
521
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300522/**
523 * Returns whether any output on the specified pipe is of the specified type
524 */
Damien Lespiau40935612014-10-29 11:16:59 +0000525bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300526{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300527 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300528 struct intel_encoder *encoder;
529
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300530 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300531 if (encoder->type == type)
532 return true;
533
534 return false;
535}
536
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200537/**
538 * Returns whether any output on the specified pipe will have the specified
539 * type after a staged modeset is complete, i.e., the same as
540 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
541 * encoder->crtc.
542 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200543static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
544 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200545{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200546 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300547 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200548 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200549 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200550 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200551
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300552 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200553 if (connector_state->crtc != crtc_state->base.crtc)
554 continue;
555
556 num_connectors++;
557
558 encoder = to_intel_encoder(connector_state->best_encoder);
559 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200560 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200561 }
562
563 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200564
565 return false;
566}
567
Imre Deakdccbea32015-06-22 23:35:51 +0300568/*
569 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
570 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
571 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
572 * The helpers' return value is the rate of the clock that is fed to the
573 * display engine's pipe which can be the above fast dot clock rate or a
574 * divided-down version of it.
575 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500576/* m1 is reserved as 0 in Pineview, n is a ring counter */
Imre Deakdccbea32015-06-22 23:35:51 +0300577static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800578{
Shaohua Li21778322009-02-23 15:19:16 +0800579 clock->m = clock->m2 + 2;
580 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200581 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300582 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300583 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
584 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300585
586 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800587}
588
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200589static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
590{
591 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
592}
593
Imre Deakdccbea32015-06-22 23:35:51 +0300594static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800595{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200596 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800597 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200598 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300599 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300600 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
601 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300602
603 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800604}
605
Imre Deakdccbea32015-06-22 23:35:51 +0300606static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300607{
608 clock->m = clock->m1 * clock->m2;
609 clock->p = clock->p1 * clock->p2;
610 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300611 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300612 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
613 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300614
615 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300616}
617
Imre Deakdccbea32015-06-22 23:35:51 +0300618int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300619{
620 clock->m = clock->m1 * clock->m2;
621 clock->p = clock->p1 * clock->p2;
622 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300623 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300624 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
625 clock->n << 22);
626 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300627
628 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300629}
630
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800631#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800632/**
633 * Returns whether the given set of divisors are valid for a given refclk with
634 * the given connectors.
635 */
636
Chris Wilson1b894b52010-12-14 20:04:54 +0000637static bool intel_PLL_is_valid(struct drm_device *dev,
638 const intel_limit_t *limit,
639 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800640{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300641 if (clock->n < limit->n.min || limit->n.max < clock->n)
642 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800643 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400644 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800645 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400646 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800647 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400648 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300649
Wayne Boyer666a4532015-12-09 12:29:35 -0800650 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
651 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300652 if (clock->m1 <= clock->m2)
653 INTELPllInvalid("m1 <= m2\n");
654
Wayne Boyer666a4532015-12-09 12:29:35 -0800655 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300656 if (clock->p < limit->p.min || limit->p.max < clock->p)
657 INTELPllInvalid("p out of range\n");
658 if (clock->m < limit->m.min || limit->m.max < clock->m)
659 INTELPllInvalid("m out of range\n");
660 }
661
Jesse Barnes79e53942008-11-07 14:24:08 -0800662 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400663 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800664 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
665 * connector, etc., rather than just a single range.
666 */
667 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400668 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800669
670 return true;
671}
672
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300673static int
674i9xx_select_p2_div(const intel_limit_t *limit,
675 const struct intel_crtc_state *crtc_state,
676 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800677{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300678 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800679
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200680 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800681 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100682 * For LVDS just rely on its current settings for dual-channel.
683 * We haven't figured out how to reliably set up different
684 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800685 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100686 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300687 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800688 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300689 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800690 } else {
691 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300692 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800693 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300694 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800695 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300696}
697
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200698/*
699 * Returns a set of divisors for the desired target clock with the given
700 * refclk, or FALSE. The returned values represent the clock equation:
701 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
702 *
703 * Target and reference clocks are specified in kHz.
704 *
705 * If match_clock is provided, then best_clock P divider must match the P
706 * divider from @match_clock used for LVDS downclocking.
707 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300708static bool
709i9xx_find_best_dpll(const intel_limit_t *limit,
710 struct intel_crtc_state *crtc_state,
711 int target, int refclk, intel_clock_t *match_clock,
712 intel_clock_t *best_clock)
713{
714 struct drm_device *dev = crtc_state->base.crtc->dev;
715 intel_clock_t clock;
716 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800717
Akshay Joshi0206e352011-08-16 15:34:10 -0400718 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800719
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300720 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
721
Zhao Yakui42158662009-11-20 11:24:18 +0800722 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
723 clock.m1++) {
724 for (clock.m2 = limit->m2.min;
725 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200726 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800727 break;
728 for (clock.n = limit->n.min;
729 clock.n <= limit->n.max; clock.n++) {
730 for (clock.p1 = limit->p1.min;
731 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800732 int this_err;
733
Imre Deakdccbea32015-06-22 23:35:51 +0300734 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000735 if (!intel_PLL_is_valid(dev, limit,
736 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800737 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800738 if (match_clock &&
739 clock.p != match_clock->p)
740 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800741
742 this_err = abs(clock.dot - target);
743 if (this_err < err) {
744 *best_clock = clock;
745 err = this_err;
746 }
747 }
748 }
749 }
750 }
751
752 return (err != target);
753}
754
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200755/*
756 * Returns a set of divisors for the desired target clock with the given
757 * refclk, or FALSE. The returned values represent the clock equation:
758 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
759 *
760 * Target and reference clocks are specified in kHz.
761 *
762 * If match_clock is provided, then best_clock P divider must match the P
763 * divider from @match_clock used for LVDS downclocking.
764 */
Ma Lingd4906092009-03-18 20:13:27 +0800765static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200766pnv_find_best_dpll(const intel_limit_t *limit,
767 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200768 int target, int refclk, intel_clock_t *match_clock,
769 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200770{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300771 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200772 intel_clock_t clock;
773 int err = target;
774
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200775 memset(best_clock, 0, sizeof(*best_clock));
776
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300777 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
778
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200779 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
780 clock.m1++) {
781 for (clock.m2 = limit->m2.min;
782 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200783 for (clock.n = limit->n.min;
784 clock.n <= limit->n.max; clock.n++) {
785 for (clock.p1 = limit->p1.min;
786 clock.p1 <= limit->p1.max; clock.p1++) {
787 int this_err;
788
Imre Deakdccbea32015-06-22 23:35:51 +0300789 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800790 if (!intel_PLL_is_valid(dev, limit,
791 &clock))
792 continue;
793 if (match_clock &&
794 clock.p != match_clock->p)
795 continue;
796
797 this_err = abs(clock.dot - target);
798 if (this_err < err) {
799 *best_clock = clock;
800 err = this_err;
801 }
802 }
803 }
804 }
805 }
806
807 return (err != target);
808}
809
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200810/*
811 * Returns a set of divisors for the desired target clock with the given
812 * refclk, or FALSE. The returned values represent the clock equation:
813 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200814 *
815 * Target and reference clocks are specified in kHz.
816 *
817 * If match_clock is provided, then best_clock P divider must match the P
818 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200819 */
Ma Lingd4906092009-03-18 20:13:27 +0800820static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200821g4x_find_best_dpll(const intel_limit_t *limit,
822 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200823 int target, int refclk, intel_clock_t *match_clock,
824 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800825{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300826 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800827 intel_clock_t clock;
828 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300829 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400830 /* approximately equals target * 0.00585 */
831 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800832
833 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300834
835 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
836
Ma Lingd4906092009-03-18 20:13:27 +0800837 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200838 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800839 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200840 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800841 for (clock.m1 = limit->m1.max;
842 clock.m1 >= limit->m1.min; clock.m1--) {
843 for (clock.m2 = limit->m2.max;
844 clock.m2 >= limit->m2.min; clock.m2--) {
845 for (clock.p1 = limit->p1.max;
846 clock.p1 >= limit->p1.min; clock.p1--) {
847 int this_err;
848
Imre Deakdccbea32015-06-22 23:35:51 +0300849 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000850 if (!intel_PLL_is_valid(dev, limit,
851 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800852 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000853
854 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800855 if (this_err < err_most) {
856 *best_clock = clock;
857 err_most = this_err;
858 max_n = clock.n;
859 found = true;
860 }
861 }
862 }
863 }
864 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800865 return found;
866}
Ma Lingd4906092009-03-18 20:13:27 +0800867
Imre Deakd5dd62b2015-03-17 11:40:03 +0200868/*
869 * Check if the calculated PLL configuration is more optimal compared to the
870 * best configuration and error found so far. Return the calculated error.
871 */
872static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
873 const intel_clock_t *calculated_clock,
874 const intel_clock_t *best_clock,
875 unsigned int best_error_ppm,
876 unsigned int *error_ppm)
877{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200878 /*
879 * For CHV ignore the error and consider only the P value.
880 * Prefer a bigger P value based on HW requirements.
881 */
882 if (IS_CHERRYVIEW(dev)) {
883 *error_ppm = 0;
884
885 return calculated_clock->p > best_clock->p;
886 }
887
Imre Deak24be4e42015-03-17 11:40:04 +0200888 if (WARN_ON_ONCE(!target_freq))
889 return false;
890
Imre Deakd5dd62b2015-03-17 11:40:03 +0200891 *error_ppm = div_u64(1000000ULL *
892 abs(target_freq - calculated_clock->dot),
893 target_freq);
894 /*
895 * Prefer a better P value over a better (smaller) error if the error
896 * is small. Ensure this preference for future configurations too by
897 * setting the error to 0.
898 */
899 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
900 *error_ppm = 0;
901
902 return true;
903 }
904
905 return *error_ppm + 10 < best_error_ppm;
906}
907
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200908/*
909 * Returns a set of divisors for the desired target clock with the given
910 * refclk, or FALSE. The returned values represent the clock equation:
911 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
912 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800913static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200914vlv_find_best_dpll(const intel_limit_t *limit,
915 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200916 int target, int refclk, intel_clock_t *match_clock,
917 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700918{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200919 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300920 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300921 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300922 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300923 /* min update 19.2 MHz */
924 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300925 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700926
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300927 target *= 5; /* fast clock */
928
929 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700930
931 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300932 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300933 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300934 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300935 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300936 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700937 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300938 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200939 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300940
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300941 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
942 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300943
Imre Deakdccbea32015-06-22 23:35:51 +0300944 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300945
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300946 if (!intel_PLL_is_valid(dev, limit,
947 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300948 continue;
949
Imre Deakd5dd62b2015-03-17 11:40:03 +0200950 if (!vlv_PLL_is_optimal(dev, target,
951 &clock,
952 best_clock,
953 bestppm, &ppm))
954 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300955
Imre Deakd5dd62b2015-03-17 11:40:03 +0200956 *best_clock = clock;
957 bestppm = ppm;
958 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700959 }
960 }
961 }
962 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700963
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300964 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700965}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700966
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200967/*
968 * Returns a set of divisors for the desired target clock with the given
969 * refclk, or FALSE. The returned values represent the clock equation:
970 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
971 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300972static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200973chv_find_best_dpll(const intel_limit_t *limit,
974 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300975 int target, int refclk, intel_clock_t *match_clock,
976 intel_clock_t *best_clock)
977{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200978 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300979 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200980 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300981 intel_clock_t clock;
982 uint64_t m2;
983 int found = false;
984
985 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200986 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300987
988 /*
989 * Based on hardware doc, the n always set to 1, and m1 always
990 * set to 2. If requires to support 200Mhz refclk, we need to
991 * revisit this because n may not 1 anymore.
992 */
993 clock.n = 1, clock.m1 = 2;
994 target *= 5; /* fast clock */
995
996 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
997 for (clock.p2 = limit->p2.p2_fast;
998 clock.p2 >= limit->p2.p2_slow;
999 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001000 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001001
1002 clock.p = clock.p1 * clock.p2;
1003
1004 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1005 clock.n) << 22, refclk * clock.m1);
1006
1007 if (m2 > INT_MAX/clock.m1)
1008 continue;
1009
1010 clock.m2 = m2;
1011
Imre Deakdccbea32015-06-22 23:35:51 +03001012 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001013
1014 if (!intel_PLL_is_valid(dev, limit, &clock))
1015 continue;
1016
Imre Deak9ca3ba02015-03-17 11:40:05 +02001017 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1018 best_error_ppm, &error_ppm))
1019 continue;
1020
1021 *best_clock = clock;
1022 best_error_ppm = error_ppm;
1023 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001024 }
1025 }
1026
1027 return found;
1028}
1029
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001030bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1031 intel_clock_t *best_clock)
1032{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001033 int refclk = 100000;
1034 const intel_limit_t *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001035
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001036 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001037 target_clock, refclk, NULL, best_clock);
1038}
1039
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001040bool intel_crtc_active(struct drm_crtc *crtc)
1041{
1042 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1043
1044 /* Be paranoid as we can arrive here with only partial
1045 * state retrieved from the hardware during setup.
1046 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001047 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001048 * as Haswell has gained clock readout/fastboot support.
1049 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001050 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001051 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001052 *
1053 * FIXME: The intel_crtc->active here should be switched to
1054 * crtc->state->active once we have proper CRTC states wired up
1055 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001056 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001057 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001058 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001059}
1060
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001061enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1062 enum pipe pipe)
1063{
1064 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1065 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1066
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001067 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001068}
1069
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001070static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1071{
1072 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001073 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001074 u32 line1, line2;
1075 u32 line_mask;
1076
1077 if (IS_GEN2(dev))
1078 line_mask = DSL_LINEMASK_GEN2;
1079 else
1080 line_mask = DSL_LINEMASK_GEN3;
1081
1082 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001083 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001084 line2 = I915_READ(reg) & line_mask;
1085
1086 return line1 == line2;
1087}
1088
Keith Packardab7ad7f2010-10-03 00:33:06 -07001089/*
1090 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001091 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001092 *
1093 * After disabling a pipe, we can't wait for vblank in the usual way,
1094 * spinning on the vblank interrupt status bit, since we won't actually
1095 * see an interrupt when the pipe is disabled.
1096 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001097 * On Gen4 and above:
1098 * wait for the pipe register state bit to turn off
1099 *
1100 * Otherwise:
1101 * wait for the display line value to settle (it usually
1102 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001103 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001104 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001105static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001106{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001107 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001108 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001109 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001110 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001111
Keith Packardab7ad7f2010-10-03 00:33:06 -07001112 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001113 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001114
Keith Packardab7ad7f2010-10-03 00:33:06 -07001115 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001116 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1117 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001118 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001119 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001120 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001121 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001122 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001123 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001124}
1125
Jesse Barnesb24e7172011-01-04 15:09:30 -08001126/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001127void assert_pll(struct drm_i915_private *dev_priv,
1128 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001129{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001130 u32 val;
1131 bool cur_state;
1132
Ville Syrjälä649636e2015-09-22 19:50:01 +03001133 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001134 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001135 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001136 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001137 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001138}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001139
Jani Nikula23538ef2013-08-27 15:12:22 +03001140/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001141void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001142{
1143 u32 val;
1144 bool cur_state;
1145
Ville Syrjäläa5805162015-05-26 20:42:30 +03001146 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001147 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001148 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001149
1150 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001151 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001152 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001153 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001154}
Jani Nikula23538ef2013-08-27 15:12:22 +03001155
Jesse Barnes040484a2011-01-03 12:14:26 -08001156static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1157 enum pipe pipe, bool state)
1158{
Jesse Barnes040484a2011-01-03 12:14:26 -08001159 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001160 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1161 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001162
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001163 if (HAS_DDI(dev_priv->dev)) {
1164 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001165 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001166 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001167 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001168 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001169 cur_state = !!(val & FDI_TX_ENABLE);
1170 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001171 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001172 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001173 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001174}
1175#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1176#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1177
1178static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1179 enum pipe pipe, bool state)
1180{
Jesse Barnes040484a2011-01-03 12:14:26 -08001181 u32 val;
1182 bool cur_state;
1183
Ville Syrjälä649636e2015-09-22 19:50:01 +03001184 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001185 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001186 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001187 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001188 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001189}
1190#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1191#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1192
1193static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1194 enum pipe pipe)
1195{
Jesse Barnes040484a2011-01-03 12:14:26 -08001196 u32 val;
1197
1198 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001199 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001200 return;
1201
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001202 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001203 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001204 return;
1205
Ville Syrjälä649636e2015-09-22 19:50:01 +03001206 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001207 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
Jesse Barnes040484a2011-01-03 12:14:26 -08001208}
1209
Daniel Vetter55607e82013-06-16 21:42:39 +02001210void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1211 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001212{
Jesse Barnes040484a2011-01-03 12:14:26 -08001213 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001214 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001215
Ville Syrjälä649636e2015-09-22 19:50:01 +03001216 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001217 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001218 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001219 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001220 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001221}
1222
Daniel Vetterb680c372014-09-19 18:27:27 +02001223void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1224 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001225{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001226 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001227 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001228 u32 val;
1229 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001230 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001231
Jani Nikulabedd4db2014-08-22 15:04:13 +03001232 if (WARN_ON(HAS_DDI(dev)))
1233 return;
1234
1235 if (HAS_PCH_SPLIT(dev)) {
1236 u32 port_sel;
1237
Jesse Barnesea0760c2011-01-04 15:09:32 -08001238 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001239 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1240
1241 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1242 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1243 panel_pipe = PIPE_B;
1244 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001245 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001246 /* presumably write lock depends on pipe, not port select */
1247 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1248 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001249 } else {
1250 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001251 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1252 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001253 }
1254
1255 val = I915_READ(pp_reg);
1256 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001257 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001258 locked = false;
1259
Rob Clarke2c719b2014-12-15 13:56:32 -05001260 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001261 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001262 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001263}
1264
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001265static void assert_cursor(struct drm_i915_private *dev_priv,
1266 enum pipe pipe, bool state)
1267{
1268 struct drm_device *dev = dev_priv->dev;
1269 bool cur_state;
1270
Paulo Zanonid9d82082014-02-27 16:30:56 -03001271 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001272 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001273 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001274 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001275
Rob Clarke2c719b2014-12-15 13:56:32 -05001276 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001277 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001278 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001279}
1280#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1281#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1282
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001283void assert_pipe(struct drm_i915_private *dev_priv,
1284 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001285{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001286 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001287 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1288 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001289 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001290
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001291 /* if we need the pipe quirk it must be always on */
1292 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1293 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001294 state = true;
1295
Imre Deak4feed0e2016-02-12 18:55:14 +02001296 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1297 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001298 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001299 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001300
1301 intel_display_power_put(dev_priv, power_domain);
1302 } else {
1303 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001304 }
1305
Rob Clarke2c719b2014-12-15 13:56:32 -05001306 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001307 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001308 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001309}
1310
Chris Wilson931872f2012-01-16 23:01:13 +00001311static void assert_plane(struct drm_i915_private *dev_priv,
1312 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001313{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001314 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001315 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001316
Ville Syrjälä649636e2015-09-22 19:50:01 +03001317 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001318 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001319 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001320 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001321 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001322}
1323
Chris Wilson931872f2012-01-16 23:01:13 +00001324#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1325#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1326
Jesse Barnesb24e7172011-01-04 15:09:30 -08001327static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1328 enum pipe pipe)
1329{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001330 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001331 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001332
Ville Syrjälä653e1022013-06-04 13:49:05 +03001333 /* Primary planes are fixed to pipes on gen4+ */
1334 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001335 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001336 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001337 "plane %c assertion failure, should be disabled but not\n",
1338 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001339 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001340 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001341
Jesse Barnesb24e7172011-01-04 15:09:30 -08001342 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001343 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001344 u32 val = I915_READ(DSPCNTR(i));
1345 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001346 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001347 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001348 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1349 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001350 }
1351}
1352
Jesse Barnes19332d72013-03-28 09:55:38 -07001353static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1354 enum pipe pipe)
1355{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001356 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001357 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001358
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001359 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001360 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001361 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001362 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001363 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1364 sprite, pipe_name(pipe));
1365 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001366 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001367 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001368 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001369 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001370 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001371 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001372 }
1373 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001374 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001375 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001376 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001377 plane_name(pipe), pipe_name(pipe));
1378 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001379 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001380 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001381 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1382 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001383 }
1384}
1385
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001386static void assert_vblank_disabled(struct drm_crtc *crtc)
1387{
Rob Clarke2c719b2014-12-15 13:56:32 -05001388 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001389 drm_crtc_vblank_put(crtc);
1390}
1391
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001392void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1393 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001394{
Jesse Barnes92f25842011-01-04 15:09:34 -08001395 u32 val;
1396 bool enabled;
1397
Ville Syrjälä649636e2015-09-22 19:50:01 +03001398 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001399 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001400 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001401 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1402 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001403}
1404
Keith Packard4e634382011-08-06 10:39:45 -07001405static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1406 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001407{
1408 if ((val & DP_PORT_EN) == 0)
1409 return false;
1410
1411 if (HAS_PCH_CPT(dev_priv->dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001412 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001413 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1414 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001415 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1416 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1417 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001418 } else {
1419 if ((val & DP_PIPE_MASK) != (pipe << 30))
1420 return false;
1421 }
1422 return true;
1423}
1424
Keith Packard1519b992011-08-06 10:35:34 -07001425static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1426 enum pipe pipe, u32 val)
1427{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001428 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001429 return false;
1430
1431 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001432 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001433 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001434 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1435 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1436 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001437 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001438 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001439 return false;
1440 }
1441 return true;
1442}
1443
1444static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1445 enum pipe pipe, u32 val)
1446{
1447 if ((val & LVDS_PORT_EN) == 0)
1448 return false;
1449
1450 if (HAS_PCH_CPT(dev_priv->dev)) {
1451 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1452 return false;
1453 } else {
1454 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1455 return false;
1456 }
1457 return true;
1458}
1459
1460static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1461 enum pipe pipe, u32 val)
1462{
1463 if ((val & ADPA_DAC_ENABLE) == 0)
1464 return false;
1465 if (HAS_PCH_CPT(dev_priv->dev)) {
1466 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1467 return false;
1468 } else {
1469 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1470 return false;
1471 }
1472 return true;
1473}
1474
Jesse Barnes291906f2011-02-02 12:28:03 -08001475static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001476 enum pipe pipe, i915_reg_t reg,
1477 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001478{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001479 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001480 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001481 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001482 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001483
Rob Clarke2c719b2014-12-15 13:56:32 -05001484 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001485 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001486 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001487}
1488
1489static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001490 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001491{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001492 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001493 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001494 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001495 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001496
Rob Clarke2c719b2014-12-15 13:56:32 -05001497 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001498 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001499 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001500}
1501
1502static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1503 enum pipe pipe)
1504{
Jesse Barnes291906f2011-02-02 12:28:03 -08001505 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001506
Keith Packardf0575e92011-07-25 22:12:43 -07001507 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1508 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1509 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001510
Ville Syrjälä649636e2015-09-22 19:50:01 +03001511 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001512 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001513 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001514 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001515
Ville Syrjälä649636e2015-09-22 19:50:01 +03001516 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001517 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001518 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001519 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001520
Paulo Zanonie2debe92013-02-18 19:00:27 -03001521 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1522 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1523 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001524}
1525
Ville Syrjäläd288f652014-10-28 13:20:22 +02001526static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001527 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001528{
Daniel Vetter426115c2013-07-11 22:13:42 +02001529 struct drm_device *dev = crtc->base.dev;
1530 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001531 enum pipe pipe = crtc->pipe;
1532 i915_reg_t reg = DPLL(pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001533 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001534
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001535 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001536
Daniel Vetter87442f72013-06-06 00:52:17 +02001537 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001538 assert_panel_unlocked(dev_priv, 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))
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001545 DRM_ERROR("DPLL %d failed to lock\n", pipe);
Daniel Vetter426115c2013-07-11 22:13:42 +02001546
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001547 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1548 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001549}
1550
Ville Syrjäläd288f652014-10-28 13:20:22 +02001551static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001552 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001553{
1554 struct drm_device *dev = crtc->base.dev;
1555 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001556 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001557 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001558 u32 tmp;
1559
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001560 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001561
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001562 /* PLL is protected by panel, make sure we can write it */
1563 assert_panel_unlocked(dev_priv, pipe);
1564
Ville Syrjäläa5805162015-05-26 20:42:30 +03001565 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001566
1567 /* Enable back the 10bit clock to display controller */
1568 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1569 tmp |= DPIO_DCLKP_EN;
1570 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1571
Ville Syrjälä54433e92015-05-26 20:42:31 +03001572 mutex_unlock(&dev_priv->sb_lock);
1573
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001574 /*
1575 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1576 */
1577 udelay(1);
1578
1579 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001580 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001581
1582 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001583 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001584 DRM_ERROR("PLL %d failed to lock\n", pipe);
1585
Ville Syrjäläc2317752016-03-15 16:39:56 +02001586 if (pipe != PIPE_A) {
1587 /*
1588 * WaPixelRepeatModeFixForC0:chv
1589 *
1590 * DPLLCMD is AWOL. Use chicken bits to propagate
1591 * the value from DPLLBMD to either pipe B or C.
1592 */
1593 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1594 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1595 I915_WRITE(CBR4_VLV, 0);
1596 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1597
1598 /*
1599 * DPLLB VGA mode also seems to cause problems.
1600 * We should always have it disabled.
1601 */
1602 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1603 } else {
1604 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1605 POSTING_READ(DPLL_MD(pipe));
1606 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001607}
1608
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001609static int intel_num_dvo_pipes(struct drm_device *dev)
1610{
1611 struct intel_crtc *crtc;
1612 int count = 0;
1613
1614 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001615 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001616 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001617
1618 return count;
1619}
1620
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001621static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001622{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001623 struct drm_device *dev = crtc->base.dev;
1624 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001625 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001626 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001627
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001628 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001629
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001630 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001631 if (IS_MOBILE(dev) && !IS_I830(dev))
1632 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001633
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001634 /* Enable DVO 2x clock on both PLLs if necessary */
1635 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1636 /*
1637 * It appears to be important that we don't enable this
1638 * for the current pipe before otherwise configuring the
1639 * PLL. No idea how this should be handled if multiple
1640 * DVO outputs are enabled simultaneosly.
1641 */
1642 dpll |= DPLL_DVO_2X_MODE;
1643 I915_WRITE(DPLL(!crtc->pipe),
1644 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1645 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001646
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001647 /*
1648 * Apparently we need to have VGA mode enabled prior to changing
1649 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1650 * dividers, even though the register value does change.
1651 */
1652 I915_WRITE(reg, 0);
1653
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001654 I915_WRITE(reg, dpll);
1655
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001656 /* Wait for the clocks to stabilize. */
1657 POSTING_READ(reg);
1658 udelay(150);
1659
1660 if (INTEL_INFO(dev)->gen >= 4) {
1661 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001662 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001663 } else {
1664 /* The pixel multiplier can only be updated once the
1665 * DPLL is enabled and the clocks are stable.
1666 *
1667 * So write it again.
1668 */
1669 I915_WRITE(reg, dpll);
1670 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001671
1672 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001673 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001674 POSTING_READ(reg);
1675 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001676 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001677 POSTING_READ(reg);
1678 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001679 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001680 POSTING_READ(reg);
1681 udelay(150); /* wait for warmup */
1682}
1683
1684/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001685 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001686 * @dev_priv: i915 private structure
1687 * @pipe: pipe PLL to disable
1688 *
1689 * Disable the PLL for @pipe, making sure the pipe is off first.
1690 *
1691 * Note! This is for pre-ILK only.
1692 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001693static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001694{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001695 struct drm_device *dev = crtc->base.dev;
1696 struct drm_i915_private *dev_priv = dev->dev_private;
1697 enum pipe pipe = crtc->pipe;
1698
1699 /* Disable DVO 2x clock on both PLLs if necessary */
1700 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001701 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001702 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001703 I915_WRITE(DPLL(PIPE_B),
1704 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1705 I915_WRITE(DPLL(PIPE_A),
1706 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1707 }
1708
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001709 /* Don't disable pipe or pipe PLLs if needed */
1710 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1711 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001712 return;
1713
1714 /* Make sure the pipe isn't still relying on us */
1715 assert_pipe_disabled(dev_priv, pipe);
1716
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001717 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001718 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001719}
1720
Jesse Barnesf6071162013-10-01 10:41:38 -07001721static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1722{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001723 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001724
1725 /* Make sure the pipe isn't still relying on us */
1726 assert_pipe_disabled(dev_priv, pipe);
1727
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001728 val = DPLL_INTEGRATED_REF_CLK_VLV |
1729 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1730 if (pipe != PIPE_A)
1731 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1732
Jesse Barnesf6071162013-10-01 10:41:38 -07001733 I915_WRITE(DPLL(pipe), val);
1734 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001735}
1736
1737static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1738{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001739 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001740 u32 val;
1741
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001742 /* Make sure the pipe isn't still relying on us */
1743 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001744
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001745 val = DPLL_SSC_REF_CLK_CHV |
1746 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001747 if (pipe != PIPE_A)
1748 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001749
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001750 I915_WRITE(DPLL(pipe), val);
1751 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001752
Ville Syrjäläa5805162015-05-26 20:42:30 +03001753 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001754
1755 /* Disable 10bit clock to display controller */
1756 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1757 val &= ~DPIO_DCLKP_EN;
1758 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1759
Ville Syrjäläa5805162015-05-26 20:42:30 +03001760 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001761}
1762
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001763void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001764 struct intel_digital_port *dport,
1765 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001766{
1767 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001768 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001769
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001770 switch (dport->port) {
1771 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001772 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001773 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001774 break;
1775 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001776 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001777 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001778 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001779 break;
1780 case PORT_D:
1781 port_mask = DPLL_PORTD_READY_MASK;
1782 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001783 break;
1784 default:
1785 BUG();
1786 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001787
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001788 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1789 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1790 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001791}
1792
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001793static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1794 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001795{
Daniel Vetter23670b322012-11-01 09:15:30 +01001796 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001797 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001798 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001799 i915_reg_t reg;
1800 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001801
Jesse Barnes040484a2011-01-03 12:14:26 -08001802 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001803 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001804
1805 /* FDI must be feeding us bits for PCH ports */
1806 assert_fdi_tx_enabled(dev_priv, pipe);
1807 assert_fdi_rx_enabled(dev_priv, pipe);
1808
Daniel Vetter23670b322012-11-01 09:15:30 +01001809 if (HAS_PCH_CPT(dev)) {
1810 /* Workaround: Set the timing override bit before enabling the
1811 * pch transcoder. */
1812 reg = TRANS_CHICKEN2(pipe);
1813 val = I915_READ(reg);
1814 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1815 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001816 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001817
Daniel Vetterab9412b2013-05-03 11:49:46 +02001818 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001819 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001820 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001821
1822 if (HAS_PCH_IBX(dev_priv->dev)) {
1823 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001824 * Make the BPC in transcoder be consistent with
1825 * that in pipeconf reg. For HDMI we must use 8bpc
1826 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001827 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001828 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001829 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1830 val |= PIPECONF_8BPC;
1831 else
1832 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001833 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001834
1835 val &= ~TRANS_INTERLACE_MASK;
1836 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001837 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001838 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001839 val |= TRANS_LEGACY_INTERLACED_ILK;
1840 else
1841 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001842 else
1843 val |= TRANS_PROGRESSIVE;
1844
Jesse Barnes040484a2011-01-03 12:14:26 -08001845 I915_WRITE(reg, val | TRANS_ENABLE);
1846 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001847 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001848}
1849
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001850static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001851 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001852{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001853 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001854
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001855 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001856 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001857 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001858
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001859 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001860 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001861 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001862 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001863
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001864 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001865 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001866
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001867 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1868 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001869 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001870 else
1871 val |= TRANS_PROGRESSIVE;
1872
Daniel Vetterab9412b2013-05-03 11:49:46 +02001873 I915_WRITE(LPT_TRANSCONF, val);
1874 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001875 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001876}
1877
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001878static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1879 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001880{
Daniel Vetter23670b322012-11-01 09:15:30 +01001881 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001882 i915_reg_t reg;
1883 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001884
1885 /* FDI relies on the transcoder */
1886 assert_fdi_tx_disabled(dev_priv, pipe);
1887 assert_fdi_rx_disabled(dev_priv, pipe);
1888
Jesse Barnes291906f2011-02-02 12:28:03 -08001889 /* Ports must be off as well */
1890 assert_pch_ports_disabled(dev_priv, pipe);
1891
Daniel Vetterab9412b2013-05-03 11:49:46 +02001892 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001893 val = I915_READ(reg);
1894 val &= ~TRANS_ENABLE;
1895 I915_WRITE(reg, val);
1896 /* wait for PCH transcoder off, transcoder state */
1897 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001898 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001899
Ville Syrjäläc4656132015-10-29 21:25:56 +02001900 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001901 /* Workaround: Clear the timing override chicken bit again. */
1902 reg = TRANS_CHICKEN2(pipe);
1903 val = I915_READ(reg);
1904 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1905 I915_WRITE(reg, val);
1906 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001907}
1908
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001909static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001910{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001911 u32 val;
1912
Daniel Vetterab9412b2013-05-03 11:49:46 +02001913 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001914 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001915 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001916 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001917 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001918 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001919
1920 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001921 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001922 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001923 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001924}
1925
1926/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001927 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001928 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001929 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001930 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001931 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001932 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001933static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001934{
Paulo Zanoni03722642014-01-17 13:51:09 -02001935 struct drm_device *dev = crtc->base.dev;
1936 struct drm_i915_private *dev_priv = dev->dev_private;
1937 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001938 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001939 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001940 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001941 u32 val;
1942
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001943 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1944
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001945 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001946 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001947 assert_sprites_disabled(dev_priv, pipe);
1948
Paulo Zanoni681e5812012-12-06 11:12:38 -02001949 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001950 pch_transcoder = TRANSCODER_A;
1951 else
1952 pch_transcoder = pipe;
1953
Jesse Barnesb24e7172011-01-04 15:09:30 -08001954 /*
1955 * A pipe without a PLL won't actually be able to drive bits from
1956 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1957 * need the check.
1958 */
Imre Deak50360402015-01-16 00:55:16 -08001959 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Jani Nikulaa65347b2015-11-27 12:21:46 +02001960 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03001961 assert_dsi_pll_enabled(dev_priv);
1962 else
1963 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001964 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001965 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001966 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001967 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001968 assert_fdi_tx_pll_enabled(dev_priv,
1969 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001970 }
1971 /* FIXME: assert CPU port conditions for SNB+ */
1972 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001973
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001974 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001975 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001976 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001977 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1978 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00001979 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001980 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001981
1982 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001983 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001984
1985 /*
1986 * Until the pipe starts DSL will read as 0, which would cause
1987 * an apparent vblank timestamp jump, which messes up also the
1988 * frame count when it's derived from the timestamps. So let's
1989 * wait for the pipe to start properly before we call
1990 * drm_crtc_vblank_on()
1991 */
1992 if (dev->max_vblank_count == 0 &&
1993 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1994 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001995}
1996
1997/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001998 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001999 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002000 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002001 * Disable the pipe of @crtc, making sure that various hardware
2002 * specific requirements are met, if applicable, e.g. plane
2003 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002004 *
2005 * Will wait until the pipe has shut down before returning.
2006 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002007static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002008{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002009 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002010 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002011 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002012 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002013 u32 val;
2014
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002015 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2016
Jesse Barnesb24e7172011-01-04 15:09:30 -08002017 /*
2018 * Make sure planes won't keep trying to pump pixels to us,
2019 * or we might hang the display.
2020 */
2021 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002022 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002023 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002024
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002025 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002026 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002027 if ((val & PIPECONF_ENABLE) == 0)
2028 return;
2029
Ville Syrjälä67adc642014-08-15 01:21:57 +03002030 /*
2031 * Double wide has implications for planes
2032 * so best keep it disabled when not needed.
2033 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002034 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002035 val &= ~PIPECONF_DOUBLE_WIDE;
2036
2037 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002038 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2039 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002040 val &= ~PIPECONF_ENABLE;
2041
2042 I915_WRITE(reg, val);
2043 if ((val & PIPECONF_ENABLE) == 0)
2044 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002045}
2046
Chris Wilson693db182013-03-05 14:52:39 +00002047static bool need_vtd_wa(struct drm_device *dev)
2048{
2049#ifdef CONFIG_INTEL_IOMMU
2050 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2051 return true;
2052#endif
2053 return false;
2054}
2055
Ville Syrjälä832be822016-01-12 21:08:33 +02002056static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2057{
2058 return IS_GEN2(dev_priv) ? 2048 : 4096;
2059}
2060
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002061static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2062 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002063{
2064 switch (fb_modifier) {
2065 case DRM_FORMAT_MOD_NONE:
2066 return cpp;
2067 case I915_FORMAT_MOD_X_TILED:
2068 if (IS_GEN2(dev_priv))
2069 return 128;
2070 else
2071 return 512;
2072 case I915_FORMAT_MOD_Y_TILED:
2073 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2074 return 128;
2075 else
2076 return 512;
2077 case I915_FORMAT_MOD_Yf_TILED:
2078 switch (cpp) {
2079 case 1:
2080 return 64;
2081 case 2:
2082 case 4:
2083 return 128;
2084 case 8:
2085 case 16:
2086 return 256;
2087 default:
2088 MISSING_CASE(cpp);
2089 return cpp;
2090 }
2091 break;
2092 default:
2093 MISSING_CASE(fb_modifier);
2094 return cpp;
2095 }
2096}
2097
Ville Syrjälä832be822016-01-12 21:08:33 +02002098unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2099 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002100{
Ville Syrjälä832be822016-01-12 21:08:33 +02002101 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2102 return 1;
2103 else
2104 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002105 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002106}
2107
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002108/* Return the tile dimensions in pixel units */
2109static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2110 unsigned int *tile_width,
2111 unsigned int *tile_height,
2112 uint64_t fb_modifier,
2113 unsigned int cpp)
2114{
2115 unsigned int tile_width_bytes =
2116 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2117
2118 *tile_width = tile_width_bytes / cpp;
2119 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2120}
2121
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002122unsigned int
2123intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002124 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002125{
Ville Syrjälä832be822016-01-12 21:08:33 +02002126 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2127 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2128
2129 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002130}
2131
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002132unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2133{
2134 unsigned int size = 0;
2135 int i;
2136
2137 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2138 size += rot_info->plane[i].width * rot_info->plane[i].height;
2139
2140 return size;
2141}
2142
Daniel Vetter75c82a52015-10-14 16:51:04 +02002143static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002144intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2145 const struct drm_framebuffer *fb,
2146 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002147{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002148 if (intel_rotation_90_or_270(rotation)) {
2149 *view = i915_ggtt_view_rotated;
2150 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2151 } else {
2152 *view = i915_ggtt_view_normal;
2153 }
2154}
2155
2156static void
2157intel_fill_fb_info(struct drm_i915_private *dev_priv,
2158 struct drm_framebuffer *fb)
2159{
2160 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002161 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002162
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002163 tile_size = intel_tile_size(dev_priv);
2164
2165 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002166 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2167 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002168
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002169 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2170 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002171
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002172 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002173 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002174 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2175 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002176
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002177 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002178 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2179 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002180 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002181}
2182
Ville Syrjälä603525d2016-01-12 21:08:37 +02002183static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002184{
2185 if (INTEL_INFO(dev_priv)->gen >= 9)
2186 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002187 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002188 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002189 return 128 * 1024;
2190 else if (INTEL_INFO(dev_priv)->gen >= 4)
2191 return 4 * 1024;
2192 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002193 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002194}
2195
Ville Syrjälä603525d2016-01-12 21:08:37 +02002196static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2197 uint64_t fb_modifier)
2198{
2199 switch (fb_modifier) {
2200 case DRM_FORMAT_MOD_NONE:
2201 return intel_linear_alignment(dev_priv);
2202 case I915_FORMAT_MOD_X_TILED:
2203 if (INTEL_INFO(dev_priv)->gen >= 9)
2204 return 256 * 1024;
2205 return 0;
2206 case I915_FORMAT_MOD_Y_TILED:
2207 case I915_FORMAT_MOD_Yf_TILED:
2208 return 1 * 1024 * 1024;
2209 default:
2210 MISSING_CASE(fb_modifier);
2211 return 0;
2212 }
2213}
2214
Chris Wilson127bd2a2010-07-23 23:32:05 +01002215int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002216intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2217 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002218{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002219 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002220 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002221 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002222 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002223 u32 alignment;
2224 int ret;
2225
Matt Roperebcdd392014-07-09 16:22:11 -07002226 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2227
Ville Syrjälä603525d2016-01-12 21:08:37 +02002228 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002229
Ville Syrjälä3465c582016-02-15 22:54:43 +02002230 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002231
Chris Wilson693db182013-03-05 14:52:39 +00002232 /* Note that the w/a also requires 64 PTE of padding following the
2233 * bo. We currently fill all unused PTE with the shadow page and so
2234 * we should always have valid PTE following the scanout preventing
2235 * the VT-d warning.
2236 */
2237 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2238 alignment = 256 * 1024;
2239
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002240 /*
2241 * Global gtt pte registers are special registers which actually forward
2242 * writes to a chunk of system memory. Which means that there is no risk
2243 * that the register values disappear as soon as we call
2244 * intel_runtime_pm_put(), so it is correct to wrap only the
2245 * pin/unpin/fence and not more.
2246 */
2247 intel_runtime_pm_get(dev_priv);
2248
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002249 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2250 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002251 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002252 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002253
2254 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2255 * fence, whereas 965+ only requires a fence if using
2256 * framebuffer compression. For simplicity, we always install
2257 * a fence as the cost is not that onerous.
2258 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002259 if (view.type == I915_GGTT_VIEW_NORMAL) {
2260 ret = i915_gem_object_get_fence(obj);
2261 if (ret == -EDEADLK) {
2262 /*
2263 * -EDEADLK means there are no free fences
2264 * no pending flips.
2265 *
2266 * This is propagated to atomic, but it uses
2267 * -EDEADLK to force a locking recovery, so
2268 * change the returned error to -EBUSY.
2269 */
2270 ret = -EBUSY;
2271 goto err_unpin;
2272 } else if (ret)
2273 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002274
Vivek Kasireddy98072162015-10-29 18:54:38 -07002275 i915_gem_object_pin_fence(obj);
2276 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002277
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002278 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002279 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002280
2281err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002282 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002283err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002284 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002285 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002286}
2287
Ville Syrjälä3465c582016-02-15 22:54:43 +02002288static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002289{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002290 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002291 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002292
Matt Roperebcdd392014-07-09 16:22:11 -07002293 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2294
Ville Syrjälä3465c582016-02-15 22:54:43 +02002295 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002296
Vivek Kasireddy98072162015-10-29 18:54:38 -07002297 if (view.type == I915_GGTT_VIEW_NORMAL)
2298 i915_gem_object_unpin_fence(obj);
2299
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002300 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002301}
2302
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002303/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002304 * Adjust the tile offset by moving the difference into
2305 * the x/y offsets.
2306 *
2307 * Input tile dimensions and pitch must already be
2308 * rotated to match x and y, and in pixel units.
2309 */
2310static u32 intel_adjust_tile_offset(int *x, int *y,
2311 unsigned int tile_width,
2312 unsigned int tile_height,
2313 unsigned int tile_size,
2314 unsigned int pitch_tiles,
2315 u32 old_offset,
2316 u32 new_offset)
2317{
2318 unsigned int tiles;
2319
2320 WARN_ON(old_offset & (tile_size - 1));
2321 WARN_ON(new_offset & (tile_size - 1));
2322 WARN_ON(new_offset > old_offset);
2323
2324 tiles = (old_offset - new_offset) / tile_size;
2325
2326 *y += tiles / pitch_tiles * tile_height;
2327 *x += tiles % pitch_tiles * tile_width;
2328
2329 return new_offset;
2330}
2331
2332/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002333 * Computes the linear offset to the base tile and adjusts
2334 * x, y. bytes per pixel is assumed to be a power-of-two.
2335 *
2336 * In the 90/270 rotated case, x and y are assumed
2337 * to be already rotated to match the rotated GTT view, and
2338 * pitch is the tile_height aligned framebuffer height.
2339 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002340u32 intel_compute_tile_offset(int *x, int *y,
2341 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002342 unsigned int pitch,
2343 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002344{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002345 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2346 uint64_t fb_modifier = fb->modifier[plane];
2347 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002348 u32 offset, offset_aligned, alignment;
2349
2350 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2351 if (alignment)
2352 alignment--;
2353
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002354 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002355 unsigned int tile_size, tile_width, tile_height;
2356 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002357
Ville Syrjäläd8433102016-01-12 21:08:35 +02002358 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002359 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2360 fb_modifier, cpp);
2361
2362 if (intel_rotation_90_or_270(rotation)) {
2363 pitch_tiles = pitch / tile_height;
2364 swap(tile_width, tile_height);
2365 } else {
2366 pitch_tiles = pitch / (tile_width * cpp);
2367 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002368
Ville Syrjäläd8433102016-01-12 21:08:35 +02002369 tile_rows = *y / tile_height;
2370 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002371
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002372 tiles = *x / tile_width;
2373 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002374
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002375 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2376 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002377
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002378 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2379 tile_size, pitch_tiles,
2380 offset, offset_aligned);
2381 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002382 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002383 offset_aligned = offset & ~alignment;
2384
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002385 *y = (offset & alignment) / pitch;
2386 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002387 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002388
2389 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002390}
2391
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002392static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002393{
2394 switch (format) {
2395 case DISPPLANE_8BPP:
2396 return DRM_FORMAT_C8;
2397 case DISPPLANE_BGRX555:
2398 return DRM_FORMAT_XRGB1555;
2399 case DISPPLANE_BGRX565:
2400 return DRM_FORMAT_RGB565;
2401 default:
2402 case DISPPLANE_BGRX888:
2403 return DRM_FORMAT_XRGB8888;
2404 case DISPPLANE_RGBX888:
2405 return DRM_FORMAT_XBGR8888;
2406 case DISPPLANE_BGRX101010:
2407 return DRM_FORMAT_XRGB2101010;
2408 case DISPPLANE_RGBX101010:
2409 return DRM_FORMAT_XBGR2101010;
2410 }
2411}
2412
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002413static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2414{
2415 switch (format) {
2416 case PLANE_CTL_FORMAT_RGB_565:
2417 return DRM_FORMAT_RGB565;
2418 default:
2419 case PLANE_CTL_FORMAT_XRGB_8888:
2420 if (rgb_order) {
2421 if (alpha)
2422 return DRM_FORMAT_ABGR8888;
2423 else
2424 return DRM_FORMAT_XBGR8888;
2425 } else {
2426 if (alpha)
2427 return DRM_FORMAT_ARGB8888;
2428 else
2429 return DRM_FORMAT_XRGB8888;
2430 }
2431 case PLANE_CTL_FORMAT_XRGB_2101010:
2432 if (rgb_order)
2433 return DRM_FORMAT_XBGR2101010;
2434 else
2435 return DRM_FORMAT_XRGB2101010;
2436 }
2437}
2438
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002439static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002440intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2441 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002442{
2443 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002444 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002445 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002446 struct drm_i915_gem_object *obj = NULL;
2447 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002448 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002449 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2450 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2451 PAGE_SIZE);
2452
2453 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002454
Chris Wilsonff2652e2014-03-10 08:07:02 +00002455 if (plane_config->size == 0)
2456 return false;
2457
Paulo Zanoni3badb492015-09-23 12:52:23 -03002458 /* If the FB is too big, just don't use it since fbdev is not very
2459 * important and we should probably use that space with FBC or other
2460 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002461 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002462 return false;
2463
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002464 mutex_lock(&dev->struct_mutex);
2465
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002466 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2467 base_aligned,
2468 base_aligned,
2469 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002470 if (!obj) {
2471 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002472 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002473 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002474
Damien Lespiau49af4492015-01-20 12:51:44 +00002475 obj->tiling_mode = plane_config->tiling;
2476 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002477 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002478
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002479 mode_cmd.pixel_format = fb->pixel_format;
2480 mode_cmd.width = fb->width;
2481 mode_cmd.height = fb->height;
2482 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002483 mode_cmd.modifier[0] = fb->modifier[0];
2484 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002485
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002486 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002487 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002488 DRM_DEBUG_KMS("intel fb init failed\n");
2489 goto out_unref_obj;
2490 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002491
Jesse Barnes46f297f2014-03-07 08:57:48 -08002492 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002493
Daniel Vetterf6936e22015-03-26 12:17:05 +01002494 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002495 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002496
2497out_unref_obj:
2498 drm_gem_object_unreference(&obj->base);
2499 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002500 return false;
2501}
2502
Matt Roperafd65eb2015-02-03 13:10:04 -08002503/* Update plane->state->fb to match plane->fb after driver-internal updates */
2504static void
2505update_state_fb(struct drm_plane *plane)
2506{
2507 if (plane->fb == plane->state->fb)
2508 return;
2509
2510 if (plane->state->fb)
2511 drm_framebuffer_unreference(plane->state->fb);
2512 plane->state->fb = plane->fb;
2513 if (plane->state->fb)
2514 drm_framebuffer_reference(plane->state->fb);
2515}
2516
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002517static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002518intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2519 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002520{
2521 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002522 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002523 struct drm_crtc *c;
2524 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002525 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002526 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002527 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002528 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2529 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002530 struct intel_plane_state *intel_state =
2531 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002532 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002533
Damien Lespiau2d140302015-02-05 17:22:18 +00002534 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002535 return;
2536
Daniel Vetterf6936e22015-03-26 12:17:05 +01002537 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002538 fb = &plane_config->fb->base;
2539 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002540 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002541
Damien Lespiau2d140302015-02-05 17:22:18 +00002542 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002543
2544 /*
2545 * Failed to alloc the obj, check to see if we should share
2546 * an fb with another CRTC instead
2547 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002548 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002549 i = to_intel_crtc(c);
2550
2551 if (c == &intel_crtc->base)
2552 continue;
2553
Matt Roper2ff8fde2014-07-08 07:50:07 -07002554 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002555 continue;
2556
Daniel Vetter88595ac2015-03-26 12:42:24 +01002557 fb = c->primary->fb;
2558 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002559 continue;
2560
Daniel Vetter88595ac2015-03-26 12:42:24 +01002561 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002562 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002563 drm_framebuffer_reference(fb);
2564 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002565 }
2566 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002567
Matt Roper200757f2015-12-03 11:37:36 -08002568 /*
2569 * We've failed to reconstruct the BIOS FB. Current display state
2570 * indicates that the primary plane is visible, but has a NULL FB,
2571 * which will lead to problems later if we don't fix it up. The
2572 * simplest solution is to just disable the primary plane now and
2573 * pretend the BIOS never had it enabled.
2574 */
2575 to_intel_plane_state(plane_state)->visible = false;
2576 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002577 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002578 intel_plane->disable_plane(primary, &intel_crtc->base);
2579
Daniel Vetter88595ac2015-03-26 12:42:24 +01002580 return;
2581
2582valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002583 plane_state->src_x = 0;
2584 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002585 plane_state->src_w = fb->width << 16;
2586 plane_state->src_h = fb->height << 16;
2587
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002588 plane_state->crtc_x = 0;
2589 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002590 plane_state->crtc_w = fb->width;
2591 plane_state->crtc_h = fb->height;
2592
Matt Roper0a8d8a82015-12-03 11:37:38 -08002593 intel_state->src.x1 = plane_state->src_x;
2594 intel_state->src.y1 = plane_state->src_y;
2595 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2596 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2597 intel_state->dst.x1 = plane_state->crtc_x;
2598 intel_state->dst.y1 = plane_state->crtc_y;
2599 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2600 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2601
Daniel Vetter88595ac2015-03-26 12:42:24 +01002602 obj = intel_fb_obj(fb);
2603 if (obj->tiling_mode != I915_TILING_NONE)
2604 dev_priv->preserve_bios_swizzle = true;
2605
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002606 drm_framebuffer_reference(fb);
2607 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002608 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002609 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002610 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002611}
2612
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002613static void i9xx_update_primary_plane(struct drm_plane *primary,
2614 const struct intel_crtc_state *crtc_state,
2615 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002616{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002617 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002618 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002619 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2620 struct drm_framebuffer *fb = plane_state->base.fb;
2621 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002622 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002623 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002624 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002625 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002626 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002627 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002628 int x = plane_state->src.x1 >> 16;
2629 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002630
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002631 dspcntr = DISPPLANE_GAMMA_ENABLE;
2632
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002633 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002634
2635 if (INTEL_INFO(dev)->gen < 4) {
2636 if (intel_crtc->pipe == PIPE_B)
2637 dspcntr |= DISPPLANE_SEL_PIPE_B;
2638
2639 /* pipesrc and dspsize control the size that is scaled from,
2640 * which should always be the user's requested size.
2641 */
2642 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002643 ((crtc_state->pipe_src_h - 1) << 16) |
2644 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002645 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002646 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2647 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002648 ((crtc_state->pipe_src_h - 1) << 16) |
2649 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002650 I915_WRITE(PRIMPOS(plane), 0);
2651 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002652 }
2653
Ville Syrjälä57779d02012-10-31 17:50:14 +02002654 switch (fb->pixel_format) {
2655 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002656 dspcntr |= DISPPLANE_8BPP;
2657 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002658 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002659 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002660 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002661 case DRM_FORMAT_RGB565:
2662 dspcntr |= DISPPLANE_BGRX565;
2663 break;
2664 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002665 dspcntr |= DISPPLANE_BGRX888;
2666 break;
2667 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002668 dspcntr |= DISPPLANE_RGBX888;
2669 break;
2670 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002671 dspcntr |= DISPPLANE_BGRX101010;
2672 break;
2673 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002674 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002675 break;
2676 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002677 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002678 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002679
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002680 if (INTEL_INFO(dev)->gen >= 4 &&
2681 obj->tiling_mode != I915_TILING_NONE)
2682 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002683
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002684 if (IS_G4X(dev))
2685 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2686
Ville Syrjäläac484962016-01-20 21:05:26 +02002687 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002688
Daniel Vetterc2c75132012-07-05 12:17:30 +02002689 if (INTEL_INFO(dev)->gen >= 4) {
2690 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002691 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002692 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002693 linear_offset -= intel_crtc->dspaddr_offset;
2694 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002695 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002696 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002697
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002698 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302699 dspcntr |= DISPPLANE_ROTATE_180;
2700
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002701 x += (crtc_state->pipe_src_w - 1);
2702 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302703
2704 /* Finding the last pixel of the last line of the display
2705 data and adding to linear_offset*/
2706 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002707 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002708 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302709 }
2710
Paulo Zanoni2db33662015-09-14 15:20:03 -03002711 intel_crtc->adjusted_x = x;
2712 intel_crtc->adjusted_y = y;
2713
Sonika Jindal48404c12014-08-22 14:06:04 +05302714 I915_WRITE(reg, dspcntr);
2715
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002716 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002717 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002718 I915_WRITE(DSPSURF(plane),
2719 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002720 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002721 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002722 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002723 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002724 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002725}
2726
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002727static void i9xx_disable_primary_plane(struct drm_plane *primary,
2728 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002729{
2730 struct drm_device *dev = crtc->dev;
2731 struct drm_i915_private *dev_priv = dev->dev_private;
2732 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002733 int plane = intel_crtc->plane;
2734
2735 I915_WRITE(DSPCNTR(plane), 0);
2736 if (INTEL_INFO(dev_priv)->gen >= 4)
2737 I915_WRITE(DSPSURF(plane), 0);
2738 else
2739 I915_WRITE(DSPADDR(plane), 0);
2740 POSTING_READ(DSPCNTR(plane));
2741}
2742
2743static void ironlake_update_primary_plane(struct drm_plane *primary,
2744 const struct intel_crtc_state *crtc_state,
2745 const struct intel_plane_state *plane_state)
2746{
2747 struct drm_device *dev = primary->dev;
2748 struct drm_i915_private *dev_priv = dev->dev_private;
2749 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2750 struct drm_framebuffer *fb = plane_state->base.fb;
2751 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002752 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002753 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002754 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002755 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002756 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002757 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002758 int x = plane_state->src.x1 >> 16;
2759 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002760
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002761 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002762 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002763
2764 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2765 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2766
Ville Syrjälä57779d02012-10-31 17:50:14 +02002767 switch (fb->pixel_format) {
2768 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002769 dspcntr |= DISPPLANE_8BPP;
2770 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002771 case DRM_FORMAT_RGB565:
2772 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002773 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002774 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002775 dspcntr |= DISPPLANE_BGRX888;
2776 break;
2777 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002778 dspcntr |= DISPPLANE_RGBX888;
2779 break;
2780 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002781 dspcntr |= DISPPLANE_BGRX101010;
2782 break;
2783 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002784 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002785 break;
2786 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002787 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002788 }
2789
2790 if (obj->tiling_mode != I915_TILING_NONE)
2791 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002792
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002793 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002794 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002795
Ville Syrjäläac484962016-01-20 21:05:26 +02002796 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002797 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002798 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002799 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002800 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002801 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302802 dspcntr |= DISPPLANE_ROTATE_180;
2803
2804 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002805 x += (crtc_state->pipe_src_w - 1);
2806 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302807
2808 /* Finding the last pixel of the last line of the display
2809 data and adding to linear_offset*/
2810 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002811 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002812 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302813 }
2814 }
2815
Paulo Zanoni2db33662015-09-14 15:20:03 -03002816 intel_crtc->adjusted_x = x;
2817 intel_crtc->adjusted_y = y;
2818
Sonika Jindal48404c12014-08-22 14:06:04 +05302819 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002820
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002821 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002822 I915_WRITE(DSPSURF(plane),
2823 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002824 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002825 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2826 } else {
2827 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2828 I915_WRITE(DSPLINOFF(plane), linear_offset);
2829 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002830 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002831}
2832
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002833u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2834 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002835{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002836 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2837 return 64;
2838 } else {
2839 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002840
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002841 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002842 }
2843}
2844
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002845u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2846 struct drm_i915_gem_object *obj,
2847 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002848{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002849 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002850 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002851 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002852
Ville Syrjäläe7941292016-01-19 18:23:17 +02002853 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002854 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002855
Daniel Vetterce7f1722015-10-14 16:51:06 +02002856 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002857 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002858 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002859 return -1;
2860
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002861 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002862
2863 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002864 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002865 PAGE_SIZE;
2866 }
2867
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002868 WARN_ON(upper_32_bits(offset));
2869
2870 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002871}
2872
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002873static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2874{
2875 struct drm_device *dev = intel_crtc->base.dev;
2876 struct drm_i915_private *dev_priv = dev->dev_private;
2877
2878 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2879 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2880 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002881}
2882
Chandra Kondurua1b22782015-04-07 15:28:45 -07002883/*
2884 * This function detaches (aka. unbinds) unused scalers in hardware
2885 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002886static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002887{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002888 struct intel_crtc_scaler_state *scaler_state;
2889 int i;
2890
Chandra Kondurua1b22782015-04-07 15:28:45 -07002891 scaler_state = &intel_crtc->config->scaler_state;
2892
2893 /* loop through and disable scalers that aren't in use */
2894 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002895 if (!scaler_state->scalers[i].in_use)
2896 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002897 }
2898}
2899
Chandra Konduru6156a452015-04-27 13:48:39 -07002900u32 skl_plane_ctl_format(uint32_t pixel_format)
2901{
Chandra Konduru6156a452015-04-27 13:48:39 -07002902 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002903 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002904 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002905 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002906 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002907 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002908 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002909 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002910 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002911 /*
2912 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2913 * to be already pre-multiplied. We need to add a knob (or a different
2914 * DRM_FORMAT) for user-space to configure that.
2915 */
2916 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002917 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002918 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002919 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002920 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002921 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002922 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002923 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002924 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002925 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002926 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002927 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002928 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002929 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002930 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002931 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002932 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002933 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002934 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002935 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002936 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002937
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002938 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002939}
2940
2941u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2942{
Chandra Konduru6156a452015-04-27 13:48:39 -07002943 switch (fb_modifier) {
2944 case DRM_FORMAT_MOD_NONE:
2945 break;
2946 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002947 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002948 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002949 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002951 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002952 default:
2953 MISSING_CASE(fb_modifier);
2954 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002955
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002956 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002957}
2958
2959u32 skl_plane_ctl_rotation(unsigned int rotation)
2960{
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 switch (rotation) {
2962 case BIT(DRM_ROTATE_0):
2963 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302964 /*
2965 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2966 * while i915 HW rotation is clockwise, thats why this swapping.
2967 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002968 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302969 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002970 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002971 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002972 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302973 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002974 default:
2975 MISSING_CASE(rotation);
2976 }
2977
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002978 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002979}
2980
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002981static void skylake_update_primary_plane(struct drm_plane *plane,
2982 const struct intel_crtc_state *crtc_state,
2983 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01002984{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002985 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002986 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002987 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2988 struct drm_framebuffer *fb = plane_state->base.fb;
2989 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002990 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302991 u32 plane_ctl, stride_div, stride;
2992 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002993 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302994 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002995 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002996 int scaler_id = plane_state->scaler_id;
2997 int src_x = plane_state->src.x1 >> 16;
2998 int src_y = plane_state->src.y1 >> 16;
2999 int src_w = drm_rect_width(&plane_state->src) >> 16;
3000 int src_h = drm_rect_height(&plane_state->src) >> 16;
3001 int dst_x = plane_state->dst.x1;
3002 int dst_y = plane_state->dst.y1;
3003 int dst_w = drm_rect_width(&plane_state->dst);
3004 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003005
3006 plane_ctl = PLANE_CTL_ENABLE |
3007 PLANE_CTL_PIPE_GAMMA_ENABLE |
3008 PLANE_CTL_PIPE_CSC_ENABLE;
3009
Chandra Konduru6156a452015-04-27 13:48:39 -07003010 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3011 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003012 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003013 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003014
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003015 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003016 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003017 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303018
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003019 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003020
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303021 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003022 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3023
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303024 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003025 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303026 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003027 x_offset = stride * tile_height - src_y - src_h;
3028 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003029 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303030 } else {
3031 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003032 x_offset = src_x;
3033 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003034 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303035 }
3036 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003037
Paulo Zanoni2db33662015-09-14 15:20:03 -03003038 intel_crtc->adjusted_x = x_offset;
3039 intel_crtc->adjusted_y = y_offset;
3040
Damien Lespiau70d21f02013-07-03 21:06:04 +01003041 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303042 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3043 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3044 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003045
3046 if (scaler_id >= 0) {
3047 uint32_t ps_ctrl = 0;
3048
3049 WARN_ON(!dst_w || !dst_h);
3050 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3051 crtc_state->scaler_state.scalers[scaler_id].mode;
3052 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3053 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3054 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3055 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3056 I915_WRITE(PLANE_POS(pipe, 0), 0);
3057 } else {
3058 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3059 }
3060
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003061 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003062
3063 POSTING_READ(PLANE_SURF(pipe, 0));
3064}
3065
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003066static void skylake_disable_primary_plane(struct drm_plane *primary,
3067 struct drm_crtc *crtc)
3068{
3069 struct drm_device *dev = crtc->dev;
3070 struct drm_i915_private *dev_priv = dev->dev_private;
3071 int pipe = to_intel_crtc(crtc)->pipe;
3072
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003073 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3074 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3075 POSTING_READ(PLANE_SURF(pipe, 0));
3076}
3077
Jesse Barnes17638cd2011-06-24 12:19:23 -07003078/* Assume fb object is pinned & idle & fenced and just update base pointers */
3079static int
3080intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3081 int x, int y, enum mode_set_atomic state)
3082{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003083 /* Support for kgdboc is disabled, this needs a major rework. */
3084 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003085
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003086 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003087}
3088
Ville Syrjälä75147472014-11-24 18:28:11 +02003089static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003090{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003091 struct drm_crtc *crtc;
3092
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003093 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003094 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3095 enum plane plane = intel_crtc->plane;
3096
3097 intel_prepare_page_flip(dev, plane);
3098 intel_finish_page_flip_plane(dev, plane);
3099 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003100}
3101
3102static void intel_update_primary_planes(struct drm_device *dev)
3103{
Ville Syrjälä75147472014-11-24 18:28:11 +02003104 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003105
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003106 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003107 struct intel_plane *plane = to_intel_plane(crtc->primary);
3108 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003109
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003110 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003111 plane_state = to_intel_plane_state(plane->base.state);
3112
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003113 if (plane_state->visible)
3114 plane->update_plane(&plane->base,
3115 to_intel_crtc_state(crtc->state),
3116 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003117
3118 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003119 }
3120}
3121
Ville Syrjälä75147472014-11-24 18:28:11 +02003122void intel_prepare_reset(struct drm_device *dev)
3123{
3124 /* no reset support for gen2 */
3125 if (IS_GEN2(dev))
3126 return;
3127
3128 /* reset doesn't touch the display */
3129 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3130 return;
3131
3132 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003133 /*
3134 * Disabling the crtcs gracefully seems nicer. Also the
3135 * g33 docs say we should at least disable all the planes.
3136 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003137 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003138}
3139
3140void intel_finish_reset(struct drm_device *dev)
3141{
3142 struct drm_i915_private *dev_priv = to_i915(dev);
3143
3144 /*
3145 * Flips in the rings will be nuked by the reset,
3146 * so complete all pending flips so that user space
3147 * will get its events and not get stuck.
3148 */
3149 intel_complete_page_flips(dev);
3150
3151 /* no reset support for gen2 */
3152 if (IS_GEN2(dev))
3153 return;
3154
3155 /* reset doesn't touch the display */
3156 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3157 /*
3158 * Flips in the rings have been nuked by the reset,
3159 * so update the base address of all primary
3160 * planes to the the last fb to make sure we're
3161 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003162 *
3163 * FIXME: Atomic will make this obsolete since we won't schedule
3164 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003165 */
3166 intel_update_primary_planes(dev);
3167 return;
3168 }
3169
3170 /*
3171 * The display has been reset as well,
3172 * so need a full re-initialization.
3173 */
3174 intel_runtime_pm_disable_interrupts(dev_priv);
3175 intel_runtime_pm_enable_interrupts(dev_priv);
3176
3177 intel_modeset_init_hw(dev);
3178
3179 spin_lock_irq(&dev_priv->irq_lock);
3180 if (dev_priv->display.hpd_irq_setup)
3181 dev_priv->display.hpd_irq_setup(dev);
3182 spin_unlock_irq(&dev_priv->irq_lock);
3183
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003184 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003185
3186 intel_hpd_init(dev_priv);
3187
3188 drm_modeset_unlock_all(dev);
3189}
3190
Chris Wilson7d5e3792014-03-04 13:15:08 +00003191static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3192{
3193 struct drm_device *dev = crtc->dev;
3194 struct drm_i915_private *dev_priv = dev->dev_private;
3195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003196 bool pending;
3197
3198 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3199 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3200 return false;
3201
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003202 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003203 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003204 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003205
3206 return pending;
3207}
3208
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003209static void intel_update_pipe_config(struct intel_crtc *crtc,
3210 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003211{
3212 struct drm_device *dev = crtc->base.dev;
3213 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003214 struct intel_crtc_state *pipe_config =
3215 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003216
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003217 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3218 crtc->base.mode = crtc->base.state->mode;
3219
3220 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3221 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3222 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003223
3224 /*
3225 * Update pipe size and adjust fitter if needed: the reason for this is
3226 * that in compute_mode_changes we check the native mode (not the pfit
3227 * mode) to see if we can flip rather than do a full mode set. In the
3228 * fastboot case, we'll flip, but if we don't update the pipesrc and
3229 * pfit state, we'll end up with a big fb scanned out into the wrong
3230 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003231 */
3232
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003233 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003234 ((pipe_config->pipe_src_w - 1) << 16) |
3235 (pipe_config->pipe_src_h - 1));
3236
3237 /* on skylake this is done by detaching scalers */
3238 if (INTEL_INFO(dev)->gen >= 9) {
3239 skl_detach_scalers(crtc);
3240
3241 if (pipe_config->pch_pfit.enabled)
3242 skylake_pfit_enable(crtc);
3243 } else if (HAS_PCH_SPLIT(dev)) {
3244 if (pipe_config->pch_pfit.enabled)
3245 ironlake_pfit_enable(crtc);
3246 else if (old_crtc_state->pch_pfit.enabled)
3247 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003248 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003249}
3250
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003251static void intel_fdi_normal_train(struct drm_crtc *crtc)
3252{
3253 struct drm_device *dev = crtc->dev;
3254 struct drm_i915_private *dev_priv = dev->dev_private;
3255 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3256 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003257 i915_reg_t reg;
3258 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003259
3260 /* enable normal train */
3261 reg = FDI_TX_CTL(pipe);
3262 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003263 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003264 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3265 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003266 } else {
3267 temp &= ~FDI_LINK_TRAIN_NONE;
3268 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003269 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003270 I915_WRITE(reg, temp);
3271
3272 reg = FDI_RX_CTL(pipe);
3273 temp = I915_READ(reg);
3274 if (HAS_PCH_CPT(dev)) {
3275 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3276 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3277 } else {
3278 temp &= ~FDI_LINK_TRAIN_NONE;
3279 temp |= FDI_LINK_TRAIN_NONE;
3280 }
3281 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3282
3283 /* wait one idle pattern time */
3284 POSTING_READ(reg);
3285 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003286
3287 /* IVB wants error correction enabled */
3288 if (IS_IVYBRIDGE(dev))
3289 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3290 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003291}
3292
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003293/* The FDI link training functions for ILK/Ibexpeak. */
3294static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3295{
3296 struct drm_device *dev = crtc->dev;
3297 struct drm_i915_private *dev_priv = dev->dev_private;
3298 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3299 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003300 i915_reg_t reg;
3301 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003302
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003303 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003304 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003305
Adam Jacksone1a44742010-06-25 15:32:14 -04003306 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3307 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003308 reg = FDI_RX_IMR(pipe);
3309 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003310 temp &= ~FDI_RX_SYMBOL_LOCK;
3311 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003312 I915_WRITE(reg, temp);
3313 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003314 udelay(150);
3315
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003316 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003317 reg = FDI_TX_CTL(pipe);
3318 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003319 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003320 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003321 temp &= ~FDI_LINK_TRAIN_NONE;
3322 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003323 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003324
Chris Wilson5eddb702010-09-11 13:48:45 +01003325 reg = FDI_RX_CTL(pipe);
3326 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003327 temp &= ~FDI_LINK_TRAIN_NONE;
3328 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003329 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3330
3331 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003332 udelay(150);
3333
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003334 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003335 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3336 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3337 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003338
Chris Wilson5eddb702010-09-11 13:48:45 +01003339 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003340 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003341 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003342 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3343
3344 if ((temp & FDI_RX_BIT_LOCK)) {
3345 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003346 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003347 break;
3348 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003349 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003350 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003351 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003352
3353 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003354 reg = FDI_TX_CTL(pipe);
3355 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003356 temp &= ~FDI_LINK_TRAIN_NONE;
3357 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003358 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003359
Chris Wilson5eddb702010-09-11 13:48:45 +01003360 reg = FDI_RX_CTL(pipe);
3361 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003362 temp &= ~FDI_LINK_TRAIN_NONE;
3363 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003364 I915_WRITE(reg, temp);
3365
3366 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003367 udelay(150);
3368
Chris Wilson5eddb702010-09-11 13:48:45 +01003369 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003370 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003371 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003372 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3373
3374 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003375 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003376 DRM_DEBUG_KMS("FDI train 2 done.\n");
3377 break;
3378 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003379 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003380 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003381 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003382
3383 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003384
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003385}
3386
Akshay Joshi0206e352011-08-16 15:34:10 -04003387static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003388 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3389 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3390 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3391 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3392};
3393
3394/* The FDI link training functions for SNB/Cougarpoint. */
3395static void gen6_fdi_link_train(struct drm_crtc *crtc)
3396{
3397 struct drm_device *dev = crtc->dev;
3398 struct drm_i915_private *dev_priv = dev->dev_private;
3399 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3400 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003401 i915_reg_t reg;
3402 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003403
Adam Jacksone1a44742010-06-25 15:32:14 -04003404 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3405 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003406 reg = FDI_RX_IMR(pipe);
3407 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003408 temp &= ~FDI_RX_SYMBOL_LOCK;
3409 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003410 I915_WRITE(reg, temp);
3411
3412 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003413 udelay(150);
3414
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003415 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003416 reg = FDI_TX_CTL(pipe);
3417 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003418 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003419 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003420 temp &= ~FDI_LINK_TRAIN_NONE;
3421 temp |= FDI_LINK_TRAIN_PATTERN_1;
3422 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3423 /* SNB-B */
3424 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003425 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003426
Daniel Vetterd74cf322012-10-26 10:58:13 +02003427 I915_WRITE(FDI_RX_MISC(pipe),
3428 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3429
Chris Wilson5eddb702010-09-11 13:48:45 +01003430 reg = FDI_RX_CTL(pipe);
3431 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003432 if (HAS_PCH_CPT(dev)) {
3433 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3434 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3435 } else {
3436 temp &= ~FDI_LINK_TRAIN_NONE;
3437 temp |= FDI_LINK_TRAIN_PATTERN_1;
3438 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003439 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3440
3441 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003442 udelay(150);
3443
Akshay Joshi0206e352011-08-16 15:34:10 -04003444 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003445 reg = FDI_TX_CTL(pipe);
3446 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003447 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3448 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003449 I915_WRITE(reg, temp);
3450
3451 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003452 udelay(500);
3453
Sean Paulfa37d392012-03-02 12:53:39 -05003454 for (retry = 0; retry < 5; retry++) {
3455 reg = FDI_RX_IIR(pipe);
3456 temp = I915_READ(reg);
3457 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3458 if (temp & FDI_RX_BIT_LOCK) {
3459 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3460 DRM_DEBUG_KMS("FDI train 1 done.\n");
3461 break;
3462 }
3463 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003464 }
Sean Paulfa37d392012-03-02 12:53:39 -05003465 if (retry < 5)
3466 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003467 }
3468 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003469 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003470
3471 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003472 reg = FDI_TX_CTL(pipe);
3473 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003474 temp &= ~FDI_LINK_TRAIN_NONE;
3475 temp |= FDI_LINK_TRAIN_PATTERN_2;
3476 if (IS_GEN6(dev)) {
3477 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3478 /* SNB-B */
3479 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3480 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003481 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003482
Chris Wilson5eddb702010-09-11 13:48:45 +01003483 reg = FDI_RX_CTL(pipe);
3484 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003485 if (HAS_PCH_CPT(dev)) {
3486 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3487 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3488 } else {
3489 temp &= ~FDI_LINK_TRAIN_NONE;
3490 temp |= FDI_LINK_TRAIN_PATTERN_2;
3491 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003492 I915_WRITE(reg, temp);
3493
3494 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003495 udelay(150);
3496
Akshay Joshi0206e352011-08-16 15:34:10 -04003497 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003498 reg = FDI_TX_CTL(pipe);
3499 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003500 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3501 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003502 I915_WRITE(reg, temp);
3503
3504 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003505 udelay(500);
3506
Sean Paulfa37d392012-03-02 12:53:39 -05003507 for (retry = 0; retry < 5; retry++) {
3508 reg = FDI_RX_IIR(pipe);
3509 temp = I915_READ(reg);
3510 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3511 if (temp & FDI_RX_SYMBOL_LOCK) {
3512 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3513 DRM_DEBUG_KMS("FDI train 2 done.\n");
3514 break;
3515 }
3516 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003517 }
Sean Paulfa37d392012-03-02 12:53:39 -05003518 if (retry < 5)
3519 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003520 }
3521 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003522 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003523
3524 DRM_DEBUG_KMS("FDI train done.\n");
3525}
3526
Jesse Barnes357555c2011-04-28 15:09:55 -07003527/* Manual link training for Ivy Bridge A0 parts */
3528static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3529{
3530 struct drm_device *dev = crtc->dev;
3531 struct drm_i915_private *dev_priv = dev->dev_private;
3532 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3533 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003534 i915_reg_t reg;
3535 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003536
3537 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3538 for train result */
3539 reg = FDI_RX_IMR(pipe);
3540 temp = I915_READ(reg);
3541 temp &= ~FDI_RX_SYMBOL_LOCK;
3542 temp &= ~FDI_RX_BIT_LOCK;
3543 I915_WRITE(reg, temp);
3544
3545 POSTING_READ(reg);
3546 udelay(150);
3547
Daniel Vetter01a415f2012-10-27 15:58:40 +02003548 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3549 I915_READ(FDI_RX_IIR(pipe)));
3550
Jesse Barnes139ccd32013-08-19 11:04:55 -07003551 /* Try each vswing and preemphasis setting twice before moving on */
3552 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3553 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003554 reg = FDI_TX_CTL(pipe);
3555 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003556 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3557 temp &= ~FDI_TX_ENABLE;
3558 I915_WRITE(reg, temp);
3559
3560 reg = FDI_RX_CTL(pipe);
3561 temp = I915_READ(reg);
3562 temp &= ~FDI_LINK_TRAIN_AUTO;
3563 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3564 temp &= ~FDI_RX_ENABLE;
3565 I915_WRITE(reg, temp);
3566
3567 /* enable CPU FDI TX and PCH FDI RX */
3568 reg = FDI_TX_CTL(pipe);
3569 temp = I915_READ(reg);
3570 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003571 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003572 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003573 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003574 temp |= snb_b_fdi_train_param[j/2];
3575 temp |= FDI_COMPOSITE_SYNC;
3576 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3577
3578 I915_WRITE(FDI_RX_MISC(pipe),
3579 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3580
3581 reg = FDI_RX_CTL(pipe);
3582 temp = I915_READ(reg);
3583 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3584 temp |= FDI_COMPOSITE_SYNC;
3585 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3586
3587 POSTING_READ(reg);
3588 udelay(1); /* should be 0.5us */
3589
3590 for (i = 0; i < 4; i++) {
3591 reg = FDI_RX_IIR(pipe);
3592 temp = I915_READ(reg);
3593 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3594
3595 if (temp & FDI_RX_BIT_LOCK ||
3596 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3597 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3598 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3599 i);
3600 break;
3601 }
3602 udelay(1); /* should be 0.5us */
3603 }
3604 if (i == 4) {
3605 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3606 continue;
3607 }
3608
3609 /* Train 2 */
3610 reg = FDI_TX_CTL(pipe);
3611 temp = I915_READ(reg);
3612 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3613 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3614 I915_WRITE(reg, temp);
3615
3616 reg = FDI_RX_CTL(pipe);
3617 temp = I915_READ(reg);
3618 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3619 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003620 I915_WRITE(reg, temp);
3621
3622 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003623 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003624
Jesse Barnes139ccd32013-08-19 11:04:55 -07003625 for (i = 0; i < 4; i++) {
3626 reg = FDI_RX_IIR(pipe);
3627 temp = I915_READ(reg);
3628 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003629
Jesse Barnes139ccd32013-08-19 11:04:55 -07003630 if (temp & FDI_RX_SYMBOL_LOCK ||
3631 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3632 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3633 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3634 i);
3635 goto train_done;
3636 }
3637 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003638 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003639 if (i == 4)
3640 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003641 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003642
Jesse Barnes139ccd32013-08-19 11:04:55 -07003643train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003644 DRM_DEBUG_KMS("FDI train done.\n");
3645}
3646
Daniel Vetter88cefb62012-08-12 19:27:14 +02003647static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003648{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003649 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003650 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003651 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003652 i915_reg_t reg;
3653 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003654
Jesse Barnes0e23b992010-09-10 11:10:00 -07003655 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003656 reg = FDI_RX_CTL(pipe);
3657 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003658 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003659 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003660 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003661 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3662
3663 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003664 udelay(200);
3665
3666 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003667 temp = I915_READ(reg);
3668 I915_WRITE(reg, temp | FDI_PCDCLK);
3669
3670 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003671 udelay(200);
3672
Paulo Zanoni20749732012-11-23 15:30:38 -02003673 /* Enable CPU FDI TX PLL, always on for Ironlake */
3674 reg = FDI_TX_CTL(pipe);
3675 temp = I915_READ(reg);
3676 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3677 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003678
Paulo Zanoni20749732012-11-23 15:30:38 -02003679 POSTING_READ(reg);
3680 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003681 }
3682}
3683
Daniel Vetter88cefb62012-08-12 19:27:14 +02003684static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3685{
3686 struct drm_device *dev = intel_crtc->base.dev;
3687 struct drm_i915_private *dev_priv = dev->dev_private;
3688 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003689 i915_reg_t reg;
3690 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003691
3692 /* Switch from PCDclk to Rawclk */
3693 reg = FDI_RX_CTL(pipe);
3694 temp = I915_READ(reg);
3695 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3696
3697 /* Disable CPU FDI TX PLL */
3698 reg = FDI_TX_CTL(pipe);
3699 temp = I915_READ(reg);
3700 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3701
3702 POSTING_READ(reg);
3703 udelay(100);
3704
3705 reg = FDI_RX_CTL(pipe);
3706 temp = I915_READ(reg);
3707 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3708
3709 /* Wait for the clocks to turn off. */
3710 POSTING_READ(reg);
3711 udelay(100);
3712}
3713
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003714static void ironlake_fdi_disable(struct drm_crtc *crtc)
3715{
3716 struct drm_device *dev = crtc->dev;
3717 struct drm_i915_private *dev_priv = dev->dev_private;
3718 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3719 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003720 i915_reg_t reg;
3721 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003722
3723 /* disable CPU FDI tx and PCH FDI rx */
3724 reg = FDI_TX_CTL(pipe);
3725 temp = I915_READ(reg);
3726 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3727 POSTING_READ(reg);
3728
3729 reg = FDI_RX_CTL(pipe);
3730 temp = I915_READ(reg);
3731 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003732 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003733 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3734
3735 POSTING_READ(reg);
3736 udelay(100);
3737
3738 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003739 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003740 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003741
3742 /* still set train pattern 1 */
3743 reg = FDI_TX_CTL(pipe);
3744 temp = I915_READ(reg);
3745 temp &= ~FDI_LINK_TRAIN_NONE;
3746 temp |= FDI_LINK_TRAIN_PATTERN_1;
3747 I915_WRITE(reg, temp);
3748
3749 reg = FDI_RX_CTL(pipe);
3750 temp = I915_READ(reg);
3751 if (HAS_PCH_CPT(dev)) {
3752 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3753 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3754 } else {
3755 temp &= ~FDI_LINK_TRAIN_NONE;
3756 temp |= FDI_LINK_TRAIN_PATTERN_1;
3757 }
3758 /* BPC in FDI rx is consistent with that in PIPECONF */
3759 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003760 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003761 I915_WRITE(reg, temp);
3762
3763 POSTING_READ(reg);
3764 udelay(100);
3765}
3766
Chris Wilson5dce5b932014-01-20 10:17:36 +00003767bool intel_has_pending_fb_unpin(struct drm_device *dev)
3768{
3769 struct intel_crtc *crtc;
3770
3771 /* Note that we don't need to be called with mode_config.lock here
3772 * as our list of CRTC objects is static for the lifetime of the
3773 * device and so cannot disappear as we iterate. Similarly, we can
3774 * happily treat the predicates as racy, atomic checks as userspace
3775 * cannot claim and pin a new fb without at least acquring the
3776 * struct_mutex and so serialising with us.
3777 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003778 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003779 if (atomic_read(&crtc->unpin_work_count) == 0)
3780 continue;
3781
3782 if (crtc->unpin_work)
3783 intel_wait_for_vblank(dev, crtc->pipe);
3784
3785 return true;
3786 }
3787
3788 return false;
3789}
3790
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003791static void page_flip_completed(struct intel_crtc *intel_crtc)
3792{
3793 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3794 struct intel_unpin_work *work = intel_crtc->unpin_work;
3795
3796 /* ensure that the unpin work is consistent wrt ->pending. */
3797 smp_rmb();
3798 intel_crtc->unpin_work = NULL;
3799
3800 if (work->event)
3801 drm_send_vblank_event(intel_crtc->base.dev,
3802 intel_crtc->pipe,
3803 work->event);
3804
3805 drm_crtc_vblank_put(&intel_crtc->base);
3806
3807 wake_up_all(&dev_priv->pending_flip_queue);
3808 queue_work(dev_priv->wq, &work->work);
3809
3810 trace_i915_flip_complete(intel_crtc->plane,
3811 work->pending_flip_obj);
3812}
3813
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003814static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003815{
Chris Wilson0f911282012-04-17 10:05:38 +01003816 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003817 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003818 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003819
Daniel Vetter2c10d572012-12-20 21:24:07 +01003820 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003821
3822 ret = wait_event_interruptible_timeout(
3823 dev_priv->pending_flip_queue,
3824 !intel_crtc_has_pending_flip(crtc),
3825 60*HZ);
3826
3827 if (ret < 0)
3828 return ret;
3829
3830 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01003831 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003832
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003833 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003834 if (intel_crtc->unpin_work) {
3835 WARN_ONCE(1, "Removing stuck page flip\n");
3836 page_flip_completed(intel_crtc);
3837 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003838 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003839 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003840
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003841 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003842}
3843
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003844static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3845{
3846 u32 temp;
3847
3848 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3849
3850 mutex_lock(&dev_priv->sb_lock);
3851
3852 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3853 temp |= SBI_SSCCTL_DISABLE;
3854 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3855
3856 mutex_unlock(&dev_priv->sb_lock);
3857}
3858
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003859/* Program iCLKIP clock to the desired frequency */
3860static void lpt_program_iclkip(struct drm_crtc *crtc)
3861{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003862 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003863 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003864 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3865 u32 temp;
3866
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003867 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003868
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003869 /* The iCLK virtual clock root frequency is in MHz,
3870 * but the adjusted_mode->crtc_clock in in KHz. To get the
3871 * divisors, it is necessary to divide one by another, so we
3872 * convert the virtual clock precision to KHz here for higher
3873 * precision.
3874 */
3875 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003876 u32 iclk_virtual_root_freq = 172800 * 1000;
3877 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003878 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003879
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003880 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3881 clock << auxdiv);
3882 divsel = (desired_divisor / iclk_pi_range) - 2;
3883 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003884
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003885 /*
3886 * Near 20MHz is a corner case which is
3887 * out of range for the 7-bit divisor
3888 */
3889 if (divsel <= 0x7f)
3890 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003891 }
3892
3893 /* This should not happen with any sane values */
3894 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3895 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3896 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3897 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3898
3899 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 +03003900 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003901 auxdiv,
3902 divsel,
3903 phasedir,
3904 phaseinc);
3905
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003906 mutex_lock(&dev_priv->sb_lock);
3907
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003908 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003909 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003910 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3911 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3912 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3913 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3914 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3915 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003916 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003917
3918 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003919 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003920 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3921 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003922 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003923
3924 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003925 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003926 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003927 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003928
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003929 mutex_unlock(&dev_priv->sb_lock);
3930
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003931 /* Wait for initialization time */
3932 udelay(24);
3933
3934 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3935}
3936
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003937int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3938{
3939 u32 divsel, phaseinc, auxdiv;
3940 u32 iclk_virtual_root_freq = 172800 * 1000;
3941 u32 iclk_pi_range = 64;
3942 u32 desired_divisor;
3943 u32 temp;
3944
3945 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3946 return 0;
3947
3948 mutex_lock(&dev_priv->sb_lock);
3949
3950 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3951 if (temp & SBI_SSCCTL_DISABLE) {
3952 mutex_unlock(&dev_priv->sb_lock);
3953 return 0;
3954 }
3955
3956 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3957 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3958 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3959 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3960 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3961
3962 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3963 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3964 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3965
3966 mutex_unlock(&dev_priv->sb_lock);
3967
3968 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3969
3970 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3971 desired_divisor << auxdiv);
3972}
3973
Daniel Vetter275f01b22013-05-03 11:49:47 +02003974static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3975 enum pipe pch_transcoder)
3976{
3977 struct drm_device *dev = crtc->base.dev;
3978 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003979 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003980
3981 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3982 I915_READ(HTOTAL(cpu_transcoder)));
3983 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3984 I915_READ(HBLANK(cpu_transcoder)));
3985 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3986 I915_READ(HSYNC(cpu_transcoder)));
3987
3988 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3989 I915_READ(VTOTAL(cpu_transcoder)));
3990 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3991 I915_READ(VBLANK(cpu_transcoder)));
3992 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3993 I915_READ(VSYNC(cpu_transcoder)));
3994 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3995 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3996}
3997
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003998static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003999{
4000 struct drm_i915_private *dev_priv = dev->dev_private;
4001 uint32_t temp;
4002
4003 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004004 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004005 return;
4006
4007 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4008 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4009
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004010 temp &= ~FDI_BC_BIFURCATION_SELECT;
4011 if (enable)
4012 temp |= FDI_BC_BIFURCATION_SELECT;
4013
4014 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004015 I915_WRITE(SOUTH_CHICKEN1, temp);
4016 POSTING_READ(SOUTH_CHICKEN1);
4017}
4018
4019static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4020{
4021 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004022
4023 switch (intel_crtc->pipe) {
4024 case PIPE_A:
4025 break;
4026 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004027 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004028 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004029 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004030 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004031
4032 break;
4033 case PIPE_C:
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 default:
4038 BUG();
4039 }
4040}
4041
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004042/* Return which DP Port should be selected for Transcoder DP control */
4043static enum port
4044intel_trans_dp_port_sel(struct drm_crtc *crtc)
4045{
4046 struct drm_device *dev = crtc->dev;
4047 struct intel_encoder *encoder;
4048
4049 for_each_encoder_on_crtc(dev, crtc, encoder) {
4050 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4051 encoder->type == INTEL_OUTPUT_EDP)
4052 return enc_to_dig_port(&encoder->base)->port;
4053 }
4054
4055 return -1;
4056}
4057
Jesse Barnesf67a5592011-01-05 10:31:48 -08004058/*
4059 * Enable PCH resources required for PCH ports:
4060 * - PCH PLLs
4061 * - FDI training & RX/TX
4062 * - update transcoder timings
4063 * - DP transcoding bits
4064 * - transcoder
4065 */
4066static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004067{
4068 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004069 struct drm_i915_private *dev_priv = dev->dev_private;
4070 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4071 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004072 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004073
Daniel Vetterab9412b2013-05-03 11:49:46 +02004074 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004075
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004076 if (IS_IVYBRIDGE(dev))
4077 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4078
Daniel Vettercd986ab2012-10-26 10:58:12 +02004079 /* Write the TU size bits before fdi link training, so that error
4080 * detection works. */
4081 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4082 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4083
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004084 /*
4085 * Sometimes spurious CPU pipe underruns happen during FDI
4086 * training, at least with VGA+HDMI cloning. Suppress them.
4087 */
4088 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4089
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004090 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004091 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004092
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004093 /* We need to program the right clock selection before writing the pixel
4094 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004095 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004096 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004097
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004098 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004099 temp |= TRANS_DPLL_ENABLE(pipe);
4100 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004101 if (intel_crtc->config->shared_dpll ==
4102 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004103 temp |= sel;
4104 else
4105 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004106 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004107 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004108
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004109 /* XXX: pch pll's can be enabled any time before we enable the PCH
4110 * transcoder, and we actually should do this to not upset any PCH
4111 * transcoder that already use the clock when we share it.
4112 *
4113 * Note that enable_shared_dpll tries to do the right thing, but
4114 * get_shared_dpll unconditionally resets the pll - we need that to have
4115 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004116 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004117
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004118 /* set transcoder timing, panel must allow it */
4119 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004120 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004121
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004122 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004123
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004124 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4125
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004126 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004127 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004128 const struct drm_display_mode *adjusted_mode =
4129 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004130 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004131 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004132 temp = I915_READ(reg);
4133 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004134 TRANS_DP_SYNC_MASK |
4135 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004136 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004137 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004138
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004139 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004140 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004141 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004142 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004143
4144 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004145 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004146 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004147 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004148 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004149 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004150 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004151 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004152 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004153 break;
4154 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004155 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004156 }
4157
Chris Wilson5eddb702010-09-11 13:48:45 +01004158 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004159 }
4160
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004161 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004162}
4163
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004164static void lpt_pch_enable(struct drm_crtc *crtc)
4165{
4166 struct drm_device *dev = crtc->dev;
4167 struct drm_i915_private *dev_priv = dev->dev_private;
4168 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004169 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004170
Daniel Vetterab9412b2013-05-03 11:49:46 +02004171 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004172
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004173 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004174
Paulo Zanoni0540e482012-10-31 18:12:40 -02004175 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004176 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004177
Paulo Zanoni937bb612012-10-31 18:12:47 -02004178 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004179}
4180
Daniel Vettera1520312013-05-03 11:49:50 +02004181static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004182{
4183 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004184 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004185 u32 temp;
4186
4187 temp = I915_READ(dslreg);
4188 udelay(500);
4189 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004190 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004191 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004192 }
4193}
4194
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004195static int
4196skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4197 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4198 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004199{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004200 struct intel_crtc_scaler_state *scaler_state =
4201 &crtc_state->scaler_state;
4202 struct intel_crtc *intel_crtc =
4203 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004204 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004205
4206 need_scaling = intel_rotation_90_or_270(rotation) ?
4207 (src_h != dst_w || src_w != dst_h):
4208 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004209
4210 /*
4211 * if plane is being disabled or scaler is no more required or force detach
4212 * - free scaler binded to this plane/crtc
4213 * - in order to do this, update crtc->scaler_usage
4214 *
4215 * Here scaler state in crtc_state is set free so that
4216 * scaler can be assigned to other user. Actual register
4217 * update to free the scaler is done in plane/panel-fit programming.
4218 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4219 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004220 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004221 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004222 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004223 scaler_state->scalers[*scaler_id].in_use = 0;
4224
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004225 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4226 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4227 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004228 scaler_state->scaler_users);
4229 *scaler_id = -1;
4230 }
4231 return 0;
4232 }
4233
4234 /* range checks */
4235 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4236 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4237
4238 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4239 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004240 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004241 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004242 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004243 return -EINVAL;
4244 }
4245
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004246 /* mark this plane as a scaler user in crtc_state */
4247 scaler_state->scaler_users |= (1 << scaler_user);
4248 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4249 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4250 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4251 scaler_state->scaler_users);
4252
4253 return 0;
4254}
4255
4256/**
4257 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4258 *
4259 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004260 *
4261 * Return
4262 * 0 - scaler_usage updated successfully
4263 * error - requested scaling cannot be supported or other error condition
4264 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004265int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004266{
4267 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004268 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004269
4270 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4271 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4272
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004273 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004274 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004275 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004276 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004277}
4278
4279/**
4280 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4281 *
4282 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004283 * @plane_state: atomic plane state to update
4284 *
4285 * Return
4286 * 0 - scaler_usage updated successfully
4287 * error - requested scaling cannot be supported or other error condition
4288 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004289static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4290 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004291{
4292
4293 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004294 struct intel_plane *intel_plane =
4295 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004296 struct drm_framebuffer *fb = plane_state->base.fb;
4297 int ret;
4298
4299 bool force_detach = !fb || !plane_state->visible;
4300
4301 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4302 intel_plane->base.base.id, intel_crtc->pipe,
4303 drm_plane_index(&intel_plane->base));
4304
4305 ret = skl_update_scaler(crtc_state, force_detach,
4306 drm_plane_index(&intel_plane->base),
4307 &plane_state->scaler_id,
4308 plane_state->base.rotation,
4309 drm_rect_width(&plane_state->src) >> 16,
4310 drm_rect_height(&plane_state->src) >> 16,
4311 drm_rect_width(&plane_state->dst),
4312 drm_rect_height(&plane_state->dst));
4313
4314 if (ret || plane_state->scaler_id < 0)
4315 return ret;
4316
Chandra Kondurua1b22782015-04-07 15:28:45 -07004317 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004318 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004319 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004320 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004321 return -EINVAL;
4322 }
4323
4324 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004325 switch (fb->pixel_format) {
4326 case DRM_FORMAT_RGB565:
4327 case DRM_FORMAT_XBGR8888:
4328 case DRM_FORMAT_XRGB8888:
4329 case DRM_FORMAT_ABGR8888:
4330 case DRM_FORMAT_ARGB8888:
4331 case DRM_FORMAT_XRGB2101010:
4332 case DRM_FORMAT_XBGR2101010:
4333 case DRM_FORMAT_YUYV:
4334 case DRM_FORMAT_YVYU:
4335 case DRM_FORMAT_UYVY:
4336 case DRM_FORMAT_VYUY:
4337 break;
4338 default:
4339 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4340 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4341 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004342 }
4343
Chandra Kondurua1b22782015-04-07 15:28:45 -07004344 return 0;
4345}
4346
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004347static void skylake_scaler_disable(struct intel_crtc *crtc)
4348{
4349 int i;
4350
4351 for (i = 0; i < crtc->num_scalers; i++)
4352 skl_detach_scaler(crtc, i);
4353}
4354
4355static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004356{
4357 struct drm_device *dev = crtc->base.dev;
4358 struct drm_i915_private *dev_priv = dev->dev_private;
4359 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004360 struct intel_crtc_scaler_state *scaler_state =
4361 &crtc->config->scaler_state;
4362
4363 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4364
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004365 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004366 int id;
4367
4368 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4369 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4370 return;
4371 }
4372
4373 id = scaler_state->scaler_id;
4374 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4375 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4376 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4377 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4378
4379 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004380 }
4381}
4382
Jesse Barnesb074cec2013-04-25 12:55:02 -07004383static void ironlake_pfit_enable(struct intel_crtc *crtc)
4384{
4385 struct drm_device *dev = crtc->base.dev;
4386 struct drm_i915_private *dev_priv = dev->dev_private;
4387 int pipe = crtc->pipe;
4388
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004389 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004390 /* Force use of hard-coded filter coefficients
4391 * as some pre-programmed values are broken,
4392 * e.g. x201.
4393 */
4394 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4395 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4396 PF_PIPE_SEL_IVB(pipe));
4397 else
4398 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004399 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4400 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004401 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004402}
4403
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004404void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004405{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004406 struct drm_device *dev = crtc->base.dev;
4407 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004408
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004409 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004410 return;
4411
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004412 /*
4413 * We can only enable IPS after we enable a plane and wait for a vblank
4414 * This function is called from post_plane_update, which is run after
4415 * a vblank wait.
4416 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004417
Paulo Zanonid77e4532013-09-24 13:52:55 -03004418 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004419 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004420 mutex_lock(&dev_priv->rps.hw_lock);
4421 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4422 mutex_unlock(&dev_priv->rps.hw_lock);
4423 /* Quoting Art Runyan: "its not safe to expect any particular
4424 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004425 * mailbox." Moreover, the mailbox may return a bogus state,
4426 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004427 */
4428 } else {
4429 I915_WRITE(IPS_CTL, IPS_ENABLE);
4430 /* The bit only becomes 1 in the next vblank, so this wait here
4431 * is essentially intel_wait_for_vblank. If we don't have this
4432 * and don't wait for vblanks until the end of crtc_enable, then
4433 * the HW state readout code will complain that the expected
4434 * IPS_CTL value is not the one we read. */
4435 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4436 DRM_ERROR("Timed out waiting for IPS enable\n");
4437 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004438}
4439
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004440void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004441{
4442 struct drm_device *dev = crtc->base.dev;
4443 struct drm_i915_private *dev_priv = dev->dev_private;
4444
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004445 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004446 return;
4447
4448 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004449 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004450 mutex_lock(&dev_priv->rps.hw_lock);
4451 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4452 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004453 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4454 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4455 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004456 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004457 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004458 POSTING_READ(IPS_CTL);
4459 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004460
4461 /* We need to wait for a vblank before we can disable the plane. */
4462 intel_wait_for_vblank(dev, crtc->pipe);
4463}
4464
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004465static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004466{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004467 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004468 struct drm_device *dev = intel_crtc->base.dev;
4469 struct drm_i915_private *dev_priv = dev->dev_private;
4470
4471 mutex_lock(&dev->struct_mutex);
4472 dev_priv->mm.interruptible = false;
4473 (void) intel_overlay_switch_off(intel_crtc->overlay);
4474 dev_priv->mm.interruptible = true;
4475 mutex_unlock(&dev->struct_mutex);
4476 }
4477
4478 /* Let userspace switch the overlay on again. In most cases userspace
4479 * has to recompute where to put it anyway.
4480 */
4481}
4482
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004483/**
4484 * intel_post_enable_primary - Perform operations after enabling primary plane
4485 * @crtc: the CRTC whose primary plane was just enabled
4486 *
4487 * Performs potentially sleeping operations that must be done after the primary
4488 * plane is enabled, such as updating FBC and IPS. Note that this may be
4489 * called due to an explicit primary plane update, or due to an implicit
4490 * re-enable that is caused when a sprite plane is updated to no longer
4491 * completely hide the primary plane.
4492 */
4493static void
4494intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004495{
4496 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004497 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004498 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4499 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004500
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004501 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004502 * FIXME IPS should be fine as long as one plane is
4503 * enabled, but in practice it seems to have problems
4504 * when going from primary only to sprite only and vice
4505 * versa.
4506 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004507 hsw_enable_ips(intel_crtc);
4508
Daniel Vetterf99d7062014-06-19 16:01:59 +02004509 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004510 * Gen2 reports pipe underruns whenever all planes are disabled.
4511 * So don't enable underrun reporting before at least some planes
4512 * are enabled.
4513 * FIXME: Need to fix the logic to work when we turn off all planes
4514 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004515 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004516 if (IS_GEN2(dev))
4517 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4518
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004519 /* Underruns don't always raise interrupts, so check manually. */
4520 intel_check_cpu_fifo_underruns(dev_priv);
4521 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004522}
4523
Ville Syrjälä2622a082016-03-09 19:07:26 +02004524/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004525static void
4526intel_pre_disable_primary(struct drm_crtc *crtc)
4527{
4528 struct drm_device *dev = crtc->dev;
4529 struct drm_i915_private *dev_priv = dev->dev_private;
4530 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4531 int pipe = intel_crtc->pipe;
4532
4533 /*
4534 * Gen2 reports pipe underruns whenever all planes are disabled.
4535 * So diasble underrun reporting before all the planes get disabled.
4536 * FIXME: Need to fix the logic to work when we turn off all planes
4537 * but leave the pipe running.
4538 */
4539 if (IS_GEN2(dev))
4540 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4541
4542 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004543 * FIXME IPS should be fine as long as one plane is
4544 * enabled, but in practice it seems to have problems
4545 * when going from primary only to sprite only and vice
4546 * versa.
4547 */
4548 hsw_disable_ips(intel_crtc);
4549}
4550
4551/* FIXME get rid of this and use pre_plane_update */
4552static void
4553intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4554{
4555 struct drm_device *dev = crtc->dev;
4556 struct drm_i915_private *dev_priv = dev->dev_private;
4557 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4558 int pipe = intel_crtc->pipe;
4559
4560 intel_pre_disable_primary(crtc);
4561
4562 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004563 * Vblank time updates from the shadow to live plane control register
4564 * are blocked if the memory self-refresh mode is active at that
4565 * moment. So to make sure the plane gets truly disabled, disable
4566 * first the self-refresh mode. The self-refresh enable bit in turn
4567 * will be checked/applied by the HW only at the next frame start
4568 * event which is after the vblank start event, so we need to have a
4569 * wait-for-vblank between disabling the plane and the pipe.
4570 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004571 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004572 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004573 dev_priv->wm.vlv.cxsr = false;
4574 intel_wait_for_vblank(dev, pipe);
4575 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004576}
4577
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004578static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004579{
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004580 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4581 struct drm_atomic_state *old_state = old_crtc_state->base.state;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004582 struct intel_crtc_state *pipe_config =
4583 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004584 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004585 struct drm_plane *primary = crtc->base.primary;
4586 struct drm_plane_state *old_pri_state =
4587 drm_atomic_get_existing_plane_state(old_state, primary);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004588
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004589 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004590
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004591 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004592
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004593 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004594 intel_update_watermarks(&crtc->base);
4595
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004596 if (old_pri_state) {
4597 struct intel_plane_state *primary_state =
4598 to_intel_plane_state(primary->state);
4599 struct intel_plane_state *old_primary_state =
4600 to_intel_plane_state(old_pri_state);
4601
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004602 intel_fbc_post_update(crtc);
4603
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004604 if (primary_state->visible &&
4605 (needs_modeset(&pipe_config->base) ||
4606 !old_primary_state->visible))
4607 intel_post_enable_primary(&crtc->base);
4608 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004609}
4610
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004611static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004612{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004613 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004614 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004615 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004616 struct intel_crtc_state *pipe_config =
4617 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004618 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4619 struct drm_plane *primary = crtc->base.primary;
4620 struct drm_plane_state *old_pri_state =
4621 drm_atomic_get_existing_plane_state(old_state, primary);
4622 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004623
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004624 if (old_pri_state) {
4625 struct intel_plane_state *primary_state =
4626 to_intel_plane_state(primary->state);
4627 struct intel_plane_state *old_primary_state =
4628 to_intel_plane_state(old_pri_state);
4629
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004630 intel_fbc_pre_update(crtc);
4631
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004632 if (old_primary_state->visible &&
4633 (modeset || !primary_state->visible))
4634 intel_pre_disable_primary(&crtc->base);
4635 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004636
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004637 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004638 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004639
Ville Syrjälä2622a082016-03-09 19:07:26 +02004640 /*
4641 * Vblank time updates from the shadow to live plane control register
4642 * are blocked if the memory self-refresh mode is active at that
4643 * moment. So to make sure the plane gets truly disabled, disable
4644 * first the self-refresh mode. The self-refresh enable bit in turn
4645 * will be checked/applied by the HW only at the next frame start
4646 * event which is after the vblank start event, so we need to have a
4647 * wait-for-vblank between disabling the plane and the pipe.
4648 */
4649 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004650 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004651 dev_priv->wm.vlv.cxsr = false;
4652 intel_wait_for_vblank(dev, crtc->pipe);
4653 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004654 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004655
Matt Ropered4a6a72016-02-23 17:20:13 -08004656 /*
4657 * IVB workaround: must disable low power watermarks for at least
4658 * one frame before enabling scaling. LP watermarks can be re-enabled
4659 * when scaling is disabled.
4660 *
4661 * WaCxSRDisabledForSpriteScaling:ivb
4662 */
4663 if (pipe_config->disable_lp_wm) {
4664 ilk_disable_lp_wm(dev);
4665 intel_wait_for_vblank(dev, crtc->pipe);
4666 }
4667
4668 /*
4669 * If we're doing a modeset, we're done. No need to do any pre-vblank
4670 * watermark programming here.
4671 */
4672 if (needs_modeset(&pipe_config->base))
4673 return;
4674
4675 /*
4676 * For platforms that support atomic watermarks, program the
4677 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4678 * will be the intermediate values that are safe for both pre- and
4679 * post- vblank; when vblank happens, the 'active' values will be set
4680 * to the final 'target' values and we'll do this again to get the
4681 * optimal watermarks. For gen9+ platforms, the values we program here
4682 * will be the final target values which will get automatically latched
4683 * at vblank time; no further programming will be necessary.
4684 *
4685 * If a platform hasn't been transitioned to atomic watermarks yet,
4686 * we'll continue to update watermarks the old way, if flags tell
4687 * us to.
4688 */
4689 if (dev_priv->display.initial_watermarks != NULL)
4690 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004691 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004692 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004693}
4694
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004695static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004696{
4697 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004698 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004699 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004700 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004701
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004702 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004703
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004704 drm_for_each_plane_mask(p, dev, plane_mask)
4705 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004706
Daniel Vetterf99d7062014-06-19 16:01:59 +02004707 /*
4708 * FIXME: Once we grow proper nuclear flip support out of this we need
4709 * to compute the mask of flip planes precisely. For the time being
4710 * consider this a flip to a NULL plane.
4711 */
4712 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004713}
4714
Jesse Barnesf67a5592011-01-05 10:31:48 -08004715static void ironlake_crtc_enable(struct drm_crtc *crtc)
4716{
4717 struct drm_device *dev = crtc->dev;
4718 struct drm_i915_private *dev_priv = dev->dev_private;
4719 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004720 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004721 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004722 struct intel_crtc_state *pipe_config =
4723 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004724
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004725 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004726 return;
4727
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004728 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004729 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4730
4731 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004732 intel_prepare_shared_dpll(intel_crtc);
4733
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004734 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304735 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004736
4737 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004738 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004739
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004740 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004741 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004742 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004743 }
4744
4745 ironlake_set_pipeconf(crtc);
4746
Jesse Barnesf67a5592011-01-05 10:31:48 -08004747 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004748
Daniel Vettera72e4c92014-09-30 10:56:47 +02004749 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004750
Daniel Vetterf6736a12013-06-05 13:34:30 +02004751 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004752 if (encoder->pre_enable)
4753 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004754
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004755 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004756 /* Note: FDI PLL enabling _must_ be done before we enable the
4757 * cpu pipes, hence this is separate from all the other fdi/pch
4758 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004759 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004760 } else {
4761 assert_fdi_tx_disabled(dev_priv, pipe);
4762 assert_fdi_rx_disabled(dev_priv, pipe);
4763 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004764
Jesse Barnesb074cec2013-04-25 12:55:02 -07004765 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004766
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004767 /*
4768 * On ILK+ LUT must be loaded before the pipe is running but with
4769 * clocks enabled
4770 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004771 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004772
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004773 if (dev_priv->display.initial_watermarks != NULL)
4774 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004775 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004776
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004777 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004778 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004779
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004780 assert_vblank_disabled(crtc);
4781 drm_crtc_vblank_on(crtc);
4782
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004783 for_each_encoder_on_crtc(dev, crtc, encoder)
4784 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004785
4786 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004787 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004788
4789 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4790 if (intel_crtc->config->has_pch_encoder)
4791 intel_wait_for_vblank(dev, pipe);
4792 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004793}
4794
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004795/* IPS only exists on ULT machines and is tied to pipe A. */
4796static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4797{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004798 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004799}
4800
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004801static void haswell_crtc_enable(struct drm_crtc *crtc)
4802{
4803 struct drm_device *dev = crtc->dev;
4804 struct drm_i915_private *dev_priv = dev->dev_private;
4805 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4806 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004807 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004808 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004809 struct intel_crtc_state *pipe_config =
4810 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004811
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004812 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004813 return;
4814
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004815 if (intel_crtc->config->has_pch_encoder)
4816 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4817 false);
4818
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004819 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004820 intel_enable_shared_dpll(intel_crtc);
4821
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004822 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304823 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004824
Jani Nikula4d1de972016-03-18 17:05:42 +02004825 if (!intel_crtc->config->has_dsi_encoder)
4826 intel_set_pipe_timings(intel_crtc);
4827
Jani Nikulabc58be62016-03-18 17:05:39 +02004828 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004829
Jani Nikula4d1de972016-03-18 17:05:42 +02004830 if (cpu_transcoder != TRANSCODER_EDP &&
4831 !transcoder_is_dsi(cpu_transcoder)) {
4832 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004833 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004834 }
4835
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004836 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004837 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004838 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004839 }
4840
Jani Nikula4d1de972016-03-18 17:05:42 +02004841 if (!intel_crtc->config->has_dsi_encoder)
4842 haswell_set_pipeconf(crtc);
4843
Jani Nikula391bf042016-03-18 17:05:40 +02004844 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004845
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004846 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02004847
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004848 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004849
Daniel Vetter6b698512015-11-28 11:05:39 +01004850 if (intel_crtc->config->has_pch_encoder)
4851 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4852 else
4853 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4854
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304855 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004856 if (encoder->pre_enable)
4857 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304858 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004859
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004860 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004861 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004862
Jani Nikulaa65347b2015-11-27 12:21:46 +02004863 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304864 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004865
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004866 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004867 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004868 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004869 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004870
4871 /*
4872 * On ILK+ LUT must be loaded before the pipe is running but with
4873 * clocks enabled
4874 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004875 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004876
Paulo Zanoni1f544382012-10-24 11:32:00 -02004877 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004878 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304879 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004880
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004881 if (dev_priv->display.initial_watermarks != NULL)
4882 dev_priv->display.initial_watermarks(pipe_config);
4883 else
4884 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004885
4886 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4887 if (!intel_crtc->config->has_dsi_encoder)
4888 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004889
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004890 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004891 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004892
Jani Nikulaa65347b2015-11-27 12:21:46 +02004893 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004894 intel_ddi_set_vc_payload_alloc(crtc, true);
4895
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004896 assert_vblank_disabled(crtc);
4897 drm_crtc_vblank_on(crtc);
4898
Jani Nikula8807e552013-08-30 19:40:32 +03004899 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004900 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004901 intel_opregion_notify_encoder(encoder, true);
4902 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004903
Daniel Vetter6b698512015-11-28 11:05:39 +01004904 if (intel_crtc->config->has_pch_encoder) {
4905 intel_wait_for_vblank(dev, pipe);
4906 intel_wait_for_vblank(dev, pipe);
4907 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004908 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4909 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004910 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004911
Paulo Zanonie4916942013-09-20 16:21:19 -03004912 /* If we change the relative order between pipe/planes enabling, we need
4913 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004914 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4915 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4916 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4917 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4918 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004919}
4920
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004921static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004922{
4923 struct drm_device *dev = crtc->base.dev;
4924 struct drm_i915_private *dev_priv = dev->dev_private;
4925 int pipe = crtc->pipe;
4926
4927 /* To avoid upsetting the power well on haswell only disable the pfit if
4928 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004929 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004930 I915_WRITE(PF_CTL(pipe), 0);
4931 I915_WRITE(PF_WIN_POS(pipe), 0);
4932 I915_WRITE(PF_WIN_SZ(pipe), 0);
4933 }
4934}
4935
Jesse Barnes6be4a602010-09-10 10:26:01 -07004936static void ironlake_crtc_disable(struct drm_crtc *crtc)
4937{
4938 struct drm_device *dev = crtc->dev;
4939 struct drm_i915_private *dev_priv = dev->dev_private;
4940 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004941 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004942 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004943
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004944 if (intel_crtc->config->has_pch_encoder)
4945 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4946
Daniel Vetterea9d7582012-07-10 10:42:52 +02004947 for_each_encoder_on_crtc(dev, crtc, encoder)
4948 encoder->disable(encoder);
4949
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004950 drm_crtc_vblank_off(crtc);
4951 assert_vblank_disabled(crtc);
4952
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004953 /*
4954 * Sometimes spurious CPU pipe underruns happen when the
4955 * pipe is already disabled, but FDI RX/TX is still enabled.
4956 * Happens at least with VGA+HDMI cloning. Suppress them.
4957 */
4958 if (intel_crtc->config->has_pch_encoder)
4959 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4960
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004961 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004962
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004963 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004964
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004965 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004966 ironlake_fdi_disable(crtc);
Ville Syrjälä3860b2e2015-11-20 22:09:18 +02004967 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4968 }
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004969
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004970 for_each_encoder_on_crtc(dev, crtc, encoder)
4971 if (encoder->post_disable)
4972 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004973
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004974 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004975 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004976
Daniel Vetterd925c592013-06-05 13:34:04 +02004977 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004978 i915_reg_t reg;
4979 u32 temp;
4980
Daniel Vetterd925c592013-06-05 13:34:04 +02004981 /* disable TRANS_DP_CTL */
4982 reg = TRANS_DP_CTL(pipe);
4983 temp = I915_READ(reg);
4984 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4985 TRANS_DP_PORT_SEL_MASK);
4986 temp |= TRANS_DP_PORT_SEL_NONE;
4987 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004988
Daniel Vetterd925c592013-06-05 13:34:04 +02004989 /* disable DPLL_SEL */
4990 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004991 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004992 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004993 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004994
Daniel Vetterd925c592013-06-05 13:34:04 +02004995 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004996 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004997
4998 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004999}
5000
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005001static void haswell_crtc_disable(struct drm_crtc *crtc)
5002{
5003 struct drm_device *dev = crtc->dev;
5004 struct drm_i915_private *dev_priv = dev->dev_private;
5005 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5006 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005007 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005008
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005009 if (intel_crtc->config->has_pch_encoder)
5010 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5011 false);
5012
Jani Nikula8807e552013-08-30 19:40:32 +03005013 for_each_encoder_on_crtc(dev, crtc, encoder) {
5014 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005015 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005016 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005017
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005018 drm_crtc_vblank_off(crtc);
5019 assert_vblank_disabled(crtc);
5020
Jani Nikula4d1de972016-03-18 17:05:42 +02005021 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5022 if (!intel_crtc->config->has_dsi_encoder)
5023 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005024
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005025 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005026 intel_ddi_set_vc_payload_alloc(crtc, false);
5027
Jani Nikulaa65347b2015-11-27 12:21:46 +02005028 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305029 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005030
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005031 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005032 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005033 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005034 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005035
Jani Nikulaa65347b2015-11-27 12:21:46 +02005036 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305037 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005038
Imre Deak97b040a2014-06-25 22:01:50 +03005039 for_each_encoder_on_crtc(dev, crtc, encoder)
5040 if (encoder->post_disable)
5041 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005042
Ville Syrjälä92966a32015-12-08 16:05:48 +02005043 if (intel_crtc->config->has_pch_encoder) {
5044 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005045 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005046 intel_ddi_fdi_disable(crtc);
5047
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005048 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5049 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005050 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005051}
5052
Jesse Barnes2dd24552013-04-25 12:55:01 -07005053static void i9xx_pfit_enable(struct intel_crtc *crtc)
5054{
5055 struct drm_device *dev = crtc->base.dev;
5056 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005057 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005058
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005059 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005060 return;
5061
Daniel Vetterc0b03412013-05-28 12:05:54 +02005062 /*
5063 * The panel fitter should only be adjusted whilst the pipe is disabled,
5064 * according to register description and PRM.
5065 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005066 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5067 assert_pipe_disabled(dev_priv, crtc->pipe);
5068
Jesse Barnesb074cec2013-04-25 12:55:02 -07005069 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5070 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005071
5072 /* Border color in case we don't scale up to the full screen. Black by
5073 * default, change to something else for debugging. */
5074 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005075}
5076
Dave Airlied05410f2014-06-05 13:22:59 +10005077static enum intel_display_power_domain port_to_power_domain(enum port port)
5078{
5079 switch (port) {
5080 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005081 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005082 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005083 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005084 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005085 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005086 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005087 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005088 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005089 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005090 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005091 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005092 return POWER_DOMAIN_PORT_OTHER;
5093 }
5094}
5095
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005096static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5097{
5098 switch (port) {
5099 case PORT_A:
5100 return POWER_DOMAIN_AUX_A;
5101 case PORT_B:
5102 return POWER_DOMAIN_AUX_B;
5103 case PORT_C:
5104 return POWER_DOMAIN_AUX_C;
5105 case PORT_D:
5106 return POWER_DOMAIN_AUX_D;
5107 case PORT_E:
5108 /* FIXME: Check VBT for actual wiring of PORT E */
5109 return POWER_DOMAIN_AUX_D;
5110 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005111 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005112 return POWER_DOMAIN_AUX_A;
5113 }
5114}
5115
Imre Deak319be8a2014-03-04 19:22:57 +02005116enum intel_display_power_domain
5117intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005118{
Imre Deak319be8a2014-03-04 19:22:57 +02005119 struct drm_device *dev = intel_encoder->base.dev;
5120 struct intel_digital_port *intel_dig_port;
5121
5122 switch (intel_encoder->type) {
5123 case INTEL_OUTPUT_UNKNOWN:
5124 /* Only DDI platforms should ever use this output type */
5125 WARN_ON_ONCE(!HAS_DDI(dev));
5126 case INTEL_OUTPUT_DISPLAYPORT:
5127 case INTEL_OUTPUT_HDMI:
5128 case INTEL_OUTPUT_EDP:
5129 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005130 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005131 case INTEL_OUTPUT_DP_MST:
5132 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5133 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005134 case INTEL_OUTPUT_ANALOG:
5135 return POWER_DOMAIN_PORT_CRT;
5136 case INTEL_OUTPUT_DSI:
5137 return POWER_DOMAIN_PORT_DSI;
5138 default:
5139 return POWER_DOMAIN_PORT_OTHER;
5140 }
5141}
5142
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005143enum intel_display_power_domain
5144intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5145{
5146 struct drm_device *dev = intel_encoder->base.dev;
5147 struct intel_digital_port *intel_dig_port;
5148
5149 switch (intel_encoder->type) {
5150 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005151 case INTEL_OUTPUT_HDMI:
5152 /*
5153 * Only DDI platforms should ever use these output types.
5154 * We can get here after the HDMI detect code has already set
5155 * the type of the shared encoder. Since we can't be sure
5156 * what's the status of the given connectors, play safe and
5157 * run the DP detection too.
5158 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005159 WARN_ON_ONCE(!HAS_DDI(dev));
5160 case INTEL_OUTPUT_DISPLAYPORT:
5161 case INTEL_OUTPUT_EDP:
5162 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5163 return port_to_aux_power_domain(intel_dig_port->port);
5164 case INTEL_OUTPUT_DP_MST:
5165 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5166 return port_to_aux_power_domain(intel_dig_port->port);
5167 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005168 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005169 return POWER_DOMAIN_AUX_A;
5170 }
5171}
5172
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005173static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5174 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005175{
5176 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005177 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5179 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005180 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005181 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005182
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005183 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005184 return 0;
5185
Imre Deak77d22dc2014-03-05 16:20:52 +02005186 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5187 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005188 if (crtc_state->pch_pfit.enabled ||
5189 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005190 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5191
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005192 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5193 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5194
Imre Deak319be8a2014-03-04 19:22:57 +02005195 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005196 }
Imre Deak319be8a2014-03-04 19:22:57 +02005197
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005198 if (crtc_state->shared_dpll)
5199 mask |= BIT(POWER_DOMAIN_PLLS);
5200
Imre Deak77d22dc2014-03-05 16:20:52 +02005201 return mask;
5202}
5203
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005204static unsigned long
5205modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5206 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005207{
5208 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5209 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5210 enum intel_display_power_domain domain;
5211 unsigned long domains, new_domains, old_domains;
5212
5213 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005214 intel_crtc->enabled_power_domains = new_domains =
5215 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005216
5217 domains = new_domains & ~old_domains;
5218
5219 for_each_power_domain(domain, domains)
5220 intel_display_power_get(dev_priv, domain);
5221
5222 return old_domains & ~new_domains;
5223}
5224
5225static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5226 unsigned long domains)
5227{
5228 enum intel_display_power_domain domain;
5229
5230 for_each_power_domain(domain, domains)
5231 intel_display_power_put(dev_priv, domain);
5232}
5233
Mika Kaholaadafdc62015-08-18 14:36:59 +03005234static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5235{
5236 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5237
5238 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5239 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5240 return max_cdclk_freq;
5241 else if (IS_CHERRYVIEW(dev_priv))
5242 return max_cdclk_freq*95/100;
5243 else if (INTEL_INFO(dev_priv)->gen < 4)
5244 return 2*max_cdclk_freq*90/100;
5245 else
5246 return max_cdclk_freq*90/100;
5247}
5248
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005249static void intel_update_max_cdclk(struct drm_device *dev)
5250{
5251 struct drm_i915_private *dev_priv = dev->dev_private;
5252
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005253 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005254 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5255
5256 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5257 dev_priv->max_cdclk_freq = 675000;
5258 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5259 dev_priv->max_cdclk_freq = 540000;
5260 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5261 dev_priv->max_cdclk_freq = 450000;
5262 else
5263 dev_priv->max_cdclk_freq = 337500;
5264 } else if (IS_BROADWELL(dev)) {
5265 /*
5266 * FIXME with extra cooling we can allow
5267 * 540 MHz for ULX and 675 Mhz for ULT.
5268 * How can we know if extra cooling is
5269 * available? PCI ID, VTB, something else?
5270 */
5271 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5272 dev_priv->max_cdclk_freq = 450000;
5273 else if (IS_BDW_ULX(dev))
5274 dev_priv->max_cdclk_freq = 450000;
5275 else if (IS_BDW_ULT(dev))
5276 dev_priv->max_cdclk_freq = 540000;
5277 else
5278 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005279 } else if (IS_CHERRYVIEW(dev)) {
5280 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005281 } else if (IS_VALLEYVIEW(dev)) {
5282 dev_priv->max_cdclk_freq = 400000;
5283 } else {
5284 /* otherwise assume cdclk is fixed */
5285 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5286 }
5287
Mika Kaholaadafdc62015-08-18 14:36:59 +03005288 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5289
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005290 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5291 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005292
5293 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5294 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005295}
5296
5297static void intel_update_cdclk(struct drm_device *dev)
5298{
5299 struct drm_i915_private *dev_priv = dev->dev_private;
5300
5301 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5302 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5303 dev_priv->cdclk_freq);
5304
5305 /*
5306 * Program the gmbus_freq based on the cdclk frequency.
5307 * BSpec erroneously claims we should aim for 4MHz, but
5308 * in fact 1MHz is the correct frequency.
5309 */
Wayne Boyer666a4532015-12-09 12:29:35 -08005310 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005311 /*
5312 * Program the gmbus_freq based on the cdclk frequency.
5313 * BSpec erroneously claims we should aim for 4MHz, but
5314 * in fact 1MHz is the correct frequency.
5315 */
5316 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5317 }
5318
5319 if (dev_priv->max_cdclk_freq == 0)
5320 intel_update_max_cdclk(dev);
5321}
5322
Damien Lespiau70d0c572015-06-04 18:21:29 +01005323static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305324{
5325 struct drm_i915_private *dev_priv = dev->dev_private;
5326 uint32_t divider;
5327 uint32_t ratio;
5328 uint32_t current_freq;
5329 int ret;
5330
5331 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5332 switch (frequency) {
5333 case 144000:
5334 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5335 ratio = BXT_DE_PLL_RATIO(60);
5336 break;
5337 case 288000:
5338 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5339 ratio = BXT_DE_PLL_RATIO(60);
5340 break;
5341 case 384000:
5342 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5343 ratio = BXT_DE_PLL_RATIO(60);
5344 break;
5345 case 576000:
5346 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5347 ratio = BXT_DE_PLL_RATIO(60);
5348 break;
5349 case 624000:
5350 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5351 ratio = BXT_DE_PLL_RATIO(65);
5352 break;
5353 case 19200:
5354 /*
5355 * Bypass frequency with DE PLL disabled. Init ratio, divider
5356 * to suppress GCC warning.
5357 */
5358 ratio = 0;
5359 divider = 0;
5360 break;
5361 default:
5362 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5363
5364 return;
5365 }
5366
5367 mutex_lock(&dev_priv->rps.hw_lock);
5368 /* Inform power controller of upcoming frequency change */
5369 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5370 0x80000000);
5371 mutex_unlock(&dev_priv->rps.hw_lock);
5372
5373 if (ret) {
5374 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5375 ret, frequency);
5376 return;
5377 }
5378
5379 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5380 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5381 current_freq = current_freq * 500 + 1000;
5382
5383 /*
5384 * DE PLL has to be disabled when
5385 * - setting to 19.2MHz (bypass, PLL isn't used)
5386 * - before setting to 624MHz (PLL needs toggling)
5387 * - before setting to any frequency from 624MHz (PLL needs toggling)
5388 */
5389 if (frequency == 19200 || frequency == 624000 ||
5390 current_freq == 624000) {
5391 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5392 /* Timeout 200us */
5393 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5394 1))
5395 DRM_ERROR("timout waiting for DE PLL unlock\n");
5396 }
5397
5398 if (frequency != 19200) {
5399 uint32_t val;
5400
5401 val = I915_READ(BXT_DE_PLL_CTL);
5402 val &= ~BXT_DE_PLL_RATIO_MASK;
5403 val |= ratio;
5404 I915_WRITE(BXT_DE_PLL_CTL, val);
5405
5406 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5407 /* Timeout 200us */
5408 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5409 DRM_ERROR("timeout waiting for DE PLL lock\n");
5410
5411 val = I915_READ(CDCLK_CTL);
5412 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5413 val |= divider;
5414 /*
5415 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5416 * enable otherwise.
5417 */
5418 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5419 if (frequency >= 500000)
5420 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5421
5422 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5423 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5424 val |= (frequency - 1000) / 500;
5425 I915_WRITE(CDCLK_CTL, val);
5426 }
5427
5428 mutex_lock(&dev_priv->rps.hw_lock);
5429 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5430 DIV_ROUND_UP(frequency, 25000));
5431 mutex_unlock(&dev_priv->rps.hw_lock);
5432
5433 if (ret) {
5434 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5435 ret, frequency);
5436 return;
5437 }
5438
Damien Lespiaua47871b2015-06-04 18:21:34 +01005439 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305440}
5441
5442void broxton_init_cdclk(struct drm_device *dev)
5443{
5444 struct drm_i915_private *dev_priv = dev->dev_private;
5445 uint32_t val;
5446
5447 /*
5448 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5449 * or else the reset will hang because there is no PCH to respond.
5450 * Move the handshake programming to initialization sequence.
5451 * Previously was left up to BIOS.
5452 */
5453 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5454 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5455 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5456
5457 /* Enable PG1 for cdclk */
5458 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5459
5460 /* check if cd clock is enabled */
5461 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5462 DRM_DEBUG_KMS("Display already initialized\n");
5463 return;
5464 }
5465
5466 /*
5467 * FIXME:
5468 * - The initial CDCLK needs to be read from VBT.
5469 * Need to make this change after VBT has changes for BXT.
5470 * - check if setting the max (or any) cdclk freq is really necessary
5471 * here, it belongs to modeset time
5472 */
5473 broxton_set_cdclk(dev, 624000);
5474
5475 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005476 POSTING_READ(DBUF_CTL);
5477
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305478 udelay(10);
5479
5480 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5481 DRM_ERROR("DBuf power enable timeout!\n");
5482}
5483
5484void broxton_uninit_cdclk(struct drm_device *dev)
5485{
5486 struct drm_i915_private *dev_priv = dev->dev_private;
5487
5488 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005489 POSTING_READ(DBUF_CTL);
5490
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305491 udelay(10);
5492
5493 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5494 DRM_ERROR("DBuf power disable timeout!\n");
5495
5496 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5497 broxton_set_cdclk(dev, 19200);
5498
5499 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5500}
5501
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005502static const struct skl_cdclk_entry {
5503 unsigned int freq;
5504 unsigned int vco;
5505} skl_cdclk_frequencies[] = {
5506 { .freq = 308570, .vco = 8640 },
5507 { .freq = 337500, .vco = 8100 },
5508 { .freq = 432000, .vco = 8640 },
5509 { .freq = 450000, .vco = 8100 },
5510 { .freq = 540000, .vco = 8100 },
5511 { .freq = 617140, .vco = 8640 },
5512 { .freq = 675000, .vco = 8100 },
5513};
5514
5515static unsigned int skl_cdclk_decimal(unsigned int freq)
5516{
5517 return (freq - 1000) / 500;
5518}
5519
5520static unsigned int skl_cdclk_get_vco(unsigned int freq)
5521{
5522 unsigned int i;
5523
5524 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5525 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5526
5527 if (e->freq == freq)
5528 return e->vco;
5529 }
5530
5531 return 8100;
5532}
5533
5534static void
5535skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5536{
5537 unsigned int min_freq;
5538 u32 val;
5539
5540 /* select the minimum CDCLK before enabling DPLL 0 */
5541 val = I915_READ(CDCLK_CTL);
5542 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5543 val |= CDCLK_FREQ_337_308;
5544
5545 if (required_vco == 8640)
5546 min_freq = 308570;
5547 else
5548 min_freq = 337500;
5549
5550 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5551
5552 I915_WRITE(CDCLK_CTL, val);
5553 POSTING_READ(CDCLK_CTL);
5554
5555 /*
5556 * We always enable DPLL0 with the lowest link rate possible, but still
5557 * taking into account the VCO required to operate the eDP panel at the
5558 * desired frequency. The usual DP link rates operate with a VCO of
5559 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5560 * The modeset code is responsible for the selection of the exact link
5561 * rate later on, with the constraint of choosing a frequency that
5562 * works with required_vco.
5563 */
5564 val = I915_READ(DPLL_CTRL1);
5565
5566 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5567 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5568 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5569 if (required_vco == 8640)
5570 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5571 SKL_DPLL0);
5572 else
5573 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5574 SKL_DPLL0);
5575
5576 I915_WRITE(DPLL_CTRL1, val);
5577 POSTING_READ(DPLL_CTRL1);
5578
5579 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5580
5581 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5582 DRM_ERROR("DPLL0 not locked\n");
5583}
5584
5585static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5586{
5587 int ret;
5588 u32 val;
5589
5590 /* inform PCU we want to change CDCLK */
5591 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5592 mutex_lock(&dev_priv->rps.hw_lock);
5593 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5594 mutex_unlock(&dev_priv->rps.hw_lock);
5595
5596 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5597}
5598
5599static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5600{
5601 unsigned int i;
5602
5603 for (i = 0; i < 15; i++) {
5604 if (skl_cdclk_pcu_ready(dev_priv))
5605 return true;
5606 udelay(10);
5607 }
5608
5609 return false;
5610}
5611
5612static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5613{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005614 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005615 u32 freq_select, pcu_ack;
5616
5617 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5618
5619 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5620 DRM_ERROR("failed to inform PCU about cdclk change\n");
5621 return;
5622 }
5623
5624 /* set CDCLK_CTL */
5625 switch(freq) {
5626 case 450000:
5627 case 432000:
5628 freq_select = CDCLK_FREQ_450_432;
5629 pcu_ack = 1;
5630 break;
5631 case 540000:
5632 freq_select = CDCLK_FREQ_540;
5633 pcu_ack = 2;
5634 break;
5635 case 308570:
5636 case 337500:
5637 default:
5638 freq_select = CDCLK_FREQ_337_308;
5639 pcu_ack = 0;
5640 break;
5641 case 617140:
5642 case 675000:
5643 freq_select = CDCLK_FREQ_675_617;
5644 pcu_ack = 3;
5645 break;
5646 }
5647
5648 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5649 POSTING_READ(CDCLK_CTL);
5650
5651 /* inform PCU of the change */
5652 mutex_lock(&dev_priv->rps.hw_lock);
5653 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5654 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005655
5656 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005657}
5658
5659void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5660{
5661 /* disable DBUF power */
5662 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5663 POSTING_READ(DBUF_CTL);
5664
5665 udelay(10);
5666
5667 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5668 DRM_ERROR("DBuf power disable timeout\n");
5669
Imre Deakab96c1ee2015-11-04 19:24:18 +02005670 /* disable DPLL0 */
5671 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5672 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5673 DRM_ERROR("Couldn't disable DPLL0\n");
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005674}
5675
5676void skl_init_cdclk(struct drm_i915_private *dev_priv)
5677{
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005678 unsigned int required_vco;
5679
Gary Wang39d9b852015-08-28 16:40:34 +08005680 /* DPLL0 not enabled (happens on early BIOS versions) */
5681 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5682 /* enable DPLL0 */
5683 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5684 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005685 }
5686
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005687 /* set CDCLK to the frequency the BIOS chose */
5688 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5689
5690 /* enable DBUF power */
5691 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5692 POSTING_READ(DBUF_CTL);
5693
5694 udelay(10);
5695
5696 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5697 DRM_ERROR("DBuf power enable timeout\n");
5698}
5699
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305700int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5701{
5702 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5703 uint32_t cdctl = I915_READ(CDCLK_CTL);
5704 int freq = dev_priv->skl_boot_cdclk;
5705
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305706 /*
5707 * check if the pre-os intialized the display
5708 * There is SWF18 scratchpad register defined which is set by the
5709 * pre-os which can be used by the OS drivers to check the status
5710 */
5711 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5712 goto sanitize;
5713
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305714 /* Is PLL enabled and locked ? */
5715 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5716 goto sanitize;
5717
5718 /* DPLL okay; verify the cdclock
5719 *
5720 * Noticed in some instances that the freq selection is correct but
5721 * decimal part is programmed wrong from BIOS where pre-os does not
5722 * enable display. Verify the same as well.
5723 */
5724 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5725 /* All well; nothing to sanitize */
5726 return false;
5727sanitize:
5728 /*
5729 * As of now initialize with max cdclk till
5730 * we get dynamic cdclk support
5731 * */
5732 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5733 skl_init_cdclk(dev_priv);
5734
5735 /* we did have to sanitize */
5736 return true;
5737}
5738
Jesse Barnes30a970c2013-11-04 13:48:12 -08005739/* Adjust CDclk dividers to allow high res or save power if possible */
5740static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5741{
5742 struct drm_i915_private *dev_priv = dev->dev_private;
5743 u32 val, cmd;
5744
Vandana Kannan164dfd22014-11-24 13:37:41 +05305745 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5746 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005747
Ville Syrjälädfcab172014-06-13 13:37:47 +03005748 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005749 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005750 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005751 cmd = 1;
5752 else
5753 cmd = 0;
5754
5755 mutex_lock(&dev_priv->rps.hw_lock);
5756 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5757 val &= ~DSPFREQGUAR_MASK;
5758 val |= (cmd << DSPFREQGUAR_SHIFT);
5759 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5760 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5761 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5762 50)) {
5763 DRM_ERROR("timed out waiting for CDclk change\n");
5764 }
5765 mutex_unlock(&dev_priv->rps.hw_lock);
5766
Ville Syrjälä54433e92015-05-26 20:42:31 +03005767 mutex_lock(&dev_priv->sb_lock);
5768
Ville Syrjälädfcab172014-06-13 13:37:47 +03005769 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005770 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005771
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005772 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005773
Jesse Barnes30a970c2013-11-04 13:48:12 -08005774 /* adjust cdclk divider */
5775 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005776 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005777 val |= divider;
5778 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005779
5780 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005781 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005782 50))
5783 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005784 }
5785
Jesse Barnes30a970c2013-11-04 13:48:12 -08005786 /* adjust self-refresh exit latency value */
5787 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5788 val &= ~0x7f;
5789
5790 /*
5791 * For high bandwidth configs, we set a higher latency in the bunit
5792 * so that the core display fetch happens in time to avoid underruns.
5793 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005794 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005795 val |= 4500 / 250; /* 4.5 usec */
5796 else
5797 val |= 3000 / 250; /* 3.0 usec */
5798 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005799
Ville Syrjäläa5805162015-05-26 20:42:30 +03005800 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005801
Ville Syrjäläb6283052015-06-03 15:45:07 +03005802 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005803}
5804
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005805static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5806{
5807 struct drm_i915_private *dev_priv = dev->dev_private;
5808 u32 val, cmd;
5809
Vandana Kannan164dfd22014-11-24 13:37:41 +05305810 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5811 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005812
5813 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005814 case 333333:
5815 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005816 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005817 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005818 break;
5819 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005820 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005821 return;
5822 }
5823
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005824 /*
5825 * Specs are full of misinformation, but testing on actual
5826 * hardware has shown that we just need to write the desired
5827 * CCK divider into the Punit register.
5828 */
5829 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5830
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005831 mutex_lock(&dev_priv->rps.hw_lock);
5832 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5833 val &= ~DSPFREQGUAR_MASK_CHV;
5834 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5835 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5836 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5837 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5838 50)) {
5839 DRM_ERROR("timed out waiting for CDclk change\n");
5840 }
5841 mutex_unlock(&dev_priv->rps.hw_lock);
5842
Ville Syrjäläb6283052015-06-03 15:45:07 +03005843 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005844}
5845
Jesse Barnes30a970c2013-11-04 13:48:12 -08005846static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5847 int max_pixclk)
5848{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005849 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005850 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005851
Jesse Barnes30a970c2013-11-04 13:48:12 -08005852 /*
5853 * Really only a few cases to deal with, as only 4 CDclks are supported:
5854 * 200MHz
5855 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005856 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005857 * 400MHz (VLV only)
5858 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5859 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005860 *
5861 * We seem to get an unstable or solid color picture at 200MHz.
5862 * Not sure what's wrong. For now use 200MHz only when all pipes
5863 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005864 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005865 if (!IS_CHERRYVIEW(dev_priv) &&
5866 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005867 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005868 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005869 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005870 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005871 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005872 else
5873 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005874}
5875
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305876static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5877 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005878{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305879 /*
5880 * FIXME:
5881 * - remove the guardband, it's not needed on BXT
5882 * - set 19.2MHz bypass frequency if there are no active pipes
5883 */
5884 if (max_pixclk > 576000*9/10)
5885 return 624000;
5886 else if (max_pixclk > 384000*9/10)
5887 return 576000;
5888 else if (max_pixclk > 288000*9/10)
5889 return 384000;
5890 else if (max_pixclk > 144000*9/10)
5891 return 288000;
5892 else
5893 return 144000;
5894}
5895
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005896/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005897static int intel_mode_max_pixclk(struct drm_device *dev,
5898 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005899{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005900 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5901 struct drm_i915_private *dev_priv = dev->dev_private;
5902 struct drm_crtc *crtc;
5903 struct drm_crtc_state *crtc_state;
5904 unsigned max_pixclk = 0, i;
5905 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005906
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005907 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5908 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005909
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005910 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5911 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005912
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005913 if (crtc_state->enable)
5914 pixclk = crtc_state->adjusted_mode.crtc_clock;
5915
5916 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005917 }
5918
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005919 for_each_pipe(dev_priv, pipe)
5920 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5921
Jesse Barnes30a970c2013-11-04 13:48:12 -08005922 return max_pixclk;
5923}
5924
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005925static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005926{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005927 struct drm_device *dev = state->dev;
5928 struct drm_i915_private *dev_priv = dev->dev_private;
5929 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005930 struct intel_atomic_state *intel_state =
5931 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005932
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005933 if (max_pixclk < 0)
5934 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005935
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005936 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005937 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305938
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005939 if (!intel_state->active_crtcs)
5940 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5941
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005942 return 0;
5943}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005944
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005945static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5946{
5947 struct drm_device *dev = state->dev;
5948 struct drm_i915_private *dev_priv = dev->dev_private;
5949 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005950 struct intel_atomic_state *intel_state =
5951 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005952
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005953 if (max_pixclk < 0)
5954 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005955
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005956 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005957 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005958
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005959 if (!intel_state->active_crtcs)
5960 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
5961
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005962 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005963}
5964
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005965static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5966{
5967 unsigned int credits, default_credits;
5968
5969 if (IS_CHERRYVIEW(dev_priv))
5970 default_credits = PFI_CREDIT(12);
5971 else
5972 default_credits = PFI_CREDIT(8);
5973
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005974 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005975 /* CHV suggested value is 31 or 63 */
5976 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005977 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005978 else
5979 credits = PFI_CREDIT(15);
5980 } else {
5981 credits = default_credits;
5982 }
5983
5984 /*
5985 * WA - write default credits before re-programming
5986 * FIXME: should we also set the resend bit here?
5987 */
5988 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5989 default_credits);
5990
5991 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5992 credits | PFI_CREDIT_RESEND);
5993
5994 /*
5995 * FIXME is this guaranteed to clear
5996 * immediately or should we poll for it?
5997 */
5998 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5999}
6000
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006001static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006002{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006003 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006004 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006005 struct intel_atomic_state *old_intel_state =
6006 to_intel_atomic_state(old_state);
6007 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006008
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006009 /*
6010 * FIXME: We can end up here with all power domains off, yet
6011 * with a CDCLK frequency other than the minimum. To account
6012 * for this take the PIPE-A power domain, which covers the HW
6013 * blocks needed for the following programming. This can be
6014 * removed once it's guaranteed that we get here either with
6015 * the minimum CDCLK set, or the required power domains
6016 * enabled.
6017 */
6018 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006019
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006020 if (IS_CHERRYVIEW(dev))
6021 cherryview_set_cdclk(dev, req_cdclk);
6022 else
6023 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006024
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006025 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006026
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006027 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006028}
6029
Jesse Barnes89b667f2013-04-18 14:51:36 -07006030static void valleyview_crtc_enable(struct drm_crtc *crtc)
6031{
6032 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006033 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006034 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6035 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006036 struct intel_crtc_state *pipe_config =
6037 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006038 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 Cfe3cd482015-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
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006082 intel_color_load_luts(&pipe_config->base);
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;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006109 struct intel_crtc_state *pipe_config =
6110 to_intel_crtc_state(crtc->state);
Jesse Barnes79e53942008-11-07 14:24:08 -08006111 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006112
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006113 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006114 return;
6115
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006116 i9xx_set_pll_dividers(intel_crtc);
6117
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006118 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306119 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006120
6121 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006122 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006123
Daniel Vetter5b18e572014-04-24 23:55:06 +02006124 i9xx_set_pipeconf(intel_crtc);
6125
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006126 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006127
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006128 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006129 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006130
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006131 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006132 if (encoder->pre_enable)
6133 encoder->pre_enable(encoder);
6134
Daniel Vetterf6736a12013-06-05 13:34:30 +02006135 i9xx_enable_pll(intel_crtc);
6136
Jesse Barnes2dd24552013-04-25 12:55:01 -07006137 i9xx_pfit_enable(intel_crtc);
6138
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006139 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006140
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006141 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006142 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006143
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006144 assert_vblank_disabled(crtc);
6145 drm_crtc_vblank_on(crtc);
6146
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006147 for_each_encoder_on_crtc(dev, crtc, encoder)
6148 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006149}
6150
Daniel Vetter87476d62013-04-11 16:29:06 +02006151static void i9xx_pfit_disable(struct intel_crtc *crtc)
6152{
6153 struct drm_device *dev = crtc->base.dev;
6154 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006155
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006156 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006157 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006158
6159 assert_pipe_disabled(dev_priv, crtc->pipe);
6160
Daniel Vetter328d8e82013-05-08 10:36:31 +02006161 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6162 I915_READ(PFIT_CONTROL));
6163 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006164}
6165
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006166static void i9xx_crtc_disable(struct drm_crtc *crtc)
6167{
6168 struct drm_device *dev = crtc->dev;
6169 struct drm_i915_private *dev_priv = dev->dev_private;
6170 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006171 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006172 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006173
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006174 /*
6175 * On gen2 planes are double buffered but the pipe isn't, so we must
6176 * wait for planes to fully turn off before disabling the pipe.
6177 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006178 if (IS_GEN2(dev))
6179 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006180
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006181 for_each_encoder_on_crtc(dev, crtc, encoder)
6182 encoder->disable(encoder);
6183
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006184 drm_crtc_vblank_off(crtc);
6185 assert_vblank_disabled(crtc);
6186
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006187 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006188
Daniel Vetter87476d62013-04-11 16:29:06 +02006189 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006190
Jesse Barnes89b667f2013-04-18 14:51:36 -07006191 for_each_encoder_on_crtc(dev, crtc, encoder)
6192 if (encoder->post_disable)
6193 encoder->post_disable(encoder);
6194
Jani Nikulaa65347b2015-11-27 12:21:46 +02006195 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006196 if (IS_CHERRYVIEW(dev))
6197 chv_disable_pll(dev_priv, pipe);
6198 else if (IS_VALLEYVIEW(dev))
6199 vlv_disable_pll(dev_priv, pipe);
6200 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006201 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006202 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006203
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006204 for_each_encoder_on_crtc(dev, crtc, encoder)
6205 if (encoder->post_pll_disable)
6206 encoder->post_pll_disable(encoder);
6207
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006208 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006209 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006210}
6211
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006212static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006213{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006214 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006215 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006216 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006217 enum intel_display_power_domain domain;
6218 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006219
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006220 if (!intel_crtc->active)
6221 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006222
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006223 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006224 WARN_ON(intel_crtc->unpin_work);
6225
Ville Syrjälä2622a082016-03-09 19:07:26 +02006226 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006227
6228 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6229 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006230 }
6231
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006232 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006233
6234 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6235 crtc->base.id);
6236
6237 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6238 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006239 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006240 crtc->enabled = false;
6241 crtc->state->connector_mask = 0;
6242 crtc->state->encoder_mask = 0;
6243
6244 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6245 encoder->base.crtc = NULL;
6246
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006247 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006248 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006249 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006250
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006251 domains = intel_crtc->enabled_power_domains;
6252 for_each_power_domain(domain, domains)
6253 intel_display_power_put(dev_priv, domain);
6254 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006255
6256 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6257 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006258}
6259
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006260/*
6261 * turn all crtc's off, but do not adjust state
6262 * This has to be paired with a call to intel_modeset_setup_hw_state.
6263 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006264int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006265{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006266 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006267 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006268 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006269
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006270 state = drm_atomic_helper_suspend(dev);
6271 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006272 if (ret)
6273 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006274 else
6275 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006276 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006277}
6278
Chris Wilsonea5b2132010-08-04 13:50:23 +01006279void intel_encoder_destroy(struct drm_encoder *encoder)
6280{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006281 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006282
Chris Wilsonea5b2132010-08-04 13:50:23 +01006283 drm_encoder_cleanup(encoder);
6284 kfree(intel_encoder);
6285}
6286
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006287/* Cross check the actual hw state with our own modeset state tracking (and it's
6288 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006289static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006290{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006291 struct drm_crtc *crtc = connector->base.state->crtc;
6292
6293 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6294 connector->base.base.id,
6295 connector->base.name);
6296
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006297 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006298 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006299 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006300
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006301 I915_STATE_WARN(!crtc,
6302 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006303
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006304 if (!crtc)
6305 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006306
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006307 I915_STATE_WARN(!crtc->state->active,
6308 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006309
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006310 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006311 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006312
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006313 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006314 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006315
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006316 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006317 "attached encoder crtc differs from connector crtc\n");
6318 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006319 I915_STATE_WARN(crtc && crtc->state->active,
6320 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006321 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6322 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006323 }
6324}
6325
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006326int intel_connector_init(struct intel_connector *connector)
6327{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006328 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006329
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006330 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006331 return -ENOMEM;
6332
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006333 return 0;
6334}
6335
6336struct intel_connector *intel_connector_alloc(void)
6337{
6338 struct intel_connector *connector;
6339
6340 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6341 if (!connector)
6342 return NULL;
6343
6344 if (intel_connector_init(connector) < 0) {
6345 kfree(connector);
6346 return NULL;
6347 }
6348
6349 return connector;
6350}
6351
Daniel Vetterf0947c32012-07-02 13:10:34 +02006352/* Simple connector->get_hw_state implementation for encoders that support only
6353 * one connector and no cloning and hence the encoder state determines the state
6354 * of the connector. */
6355bool intel_connector_get_hw_state(struct intel_connector *connector)
6356{
Daniel Vetter24929352012-07-02 20:28:59 +02006357 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006358 struct intel_encoder *encoder = connector->encoder;
6359
6360 return encoder->get_hw_state(encoder, &pipe);
6361}
6362
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006363static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006364{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006365 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6366 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006367
6368 return 0;
6369}
6370
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006371static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006372 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006373{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006374 struct drm_atomic_state *state = pipe_config->base.state;
6375 struct intel_crtc *other_crtc;
6376 struct intel_crtc_state *other_crtc_state;
6377
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006378 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6379 pipe_name(pipe), pipe_config->fdi_lanes);
6380 if (pipe_config->fdi_lanes > 4) {
6381 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6382 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006383 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006384 }
6385
Paulo Zanonibafb6552013-11-02 21:07:44 -07006386 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006387 if (pipe_config->fdi_lanes > 2) {
6388 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6389 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006390 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006391 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006392 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006393 }
6394 }
6395
6396 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006397 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006398
6399 /* Ivybridge 3 pipe is really complicated */
6400 switch (pipe) {
6401 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006402 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006403 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006404 if (pipe_config->fdi_lanes <= 2)
6405 return 0;
6406
6407 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6408 other_crtc_state =
6409 intel_atomic_get_crtc_state(state, other_crtc);
6410 if (IS_ERR(other_crtc_state))
6411 return PTR_ERR(other_crtc_state);
6412
6413 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006414 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6415 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006416 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006417 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006418 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006419 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006420 if (pipe_config->fdi_lanes > 2) {
6421 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6422 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006423 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006424 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006425
6426 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6427 other_crtc_state =
6428 intel_atomic_get_crtc_state(state, other_crtc);
6429 if (IS_ERR(other_crtc_state))
6430 return PTR_ERR(other_crtc_state);
6431
6432 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006433 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006434 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006435 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006436 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006437 default:
6438 BUG();
6439 }
6440}
6441
Daniel Vettere29c22c2013-02-21 00:00:16 +01006442#define RETRY 1
6443static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006444 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006445{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006446 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006447 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006448 int lane, link_bw, fdi_dotclock, ret;
6449 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006450
Daniel Vettere29c22c2013-02-21 00:00:16 +01006451retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006452 /* FDI is a binary signal running at ~2.7GHz, encoding
6453 * each output octet as 10 bits. The actual frequency
6454 * is stored as a divider into a 100MHz clock, and the
6455 * mode pixel clock is stored in units of 1KHz.
6456 * Hence the bw of each lane in terms of the mode signal
6457 * is:
6458 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006459 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006460
Damien Lespiau241bfc32013-09-25 16:45:37 +01006461 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006462
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006463 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006464 pipe_config->pipe_bpp);
6465
6466 pipe_config->fdi_lanes = lane;
6467
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006468 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006469 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006470
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006471 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006472 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006473 pipe_config->pipe_bpp -= 2*3;
6474 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6475 pipe_config->pipe_bpp);
6476 needs_recompute = true;
6477 pipe_config->bw_constrained = true;
6478
6479 goto retry;
6480 }
6481
6482 if (needs_recompute)
6483 return RETRY;
6484
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006485 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006486}
6487
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006488static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6489 struct intel_crtc_state *pipe_config)
6490{
6491 if (pipe_config->pipe_bpp > 24)
6492 return false;
6493
6494 /* HSW can handle pixel rate up to cdclk? */
6495 if (IS_HASWELL(dev_priv->dev))
6496 return true;
6497
6498 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006499 * We compare against max which means we must take
6500 * the increased cdclk requirement into account when
6501 * calculating the new cdclk.
6502 *
6503 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006504 */
6505 return ilk_pipe_pixel_rate(pipe_config) <=
6506 dev_priv->max_cdclk_freq * 95 / 100;
6507}
6508
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006509static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006510 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006511{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006512 struct drm_device *dev = crtc->base.dev;
6513 struct drm_i915_private *dev_priv = dev->dev_private;
6514
Jani Nikulad330a952014-01-21 11:24:25 +02006515 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006516 hsw_crtc_supports_ips(crtc) &&
6517 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006518}
6519
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006520static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6521{
6522 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6523
6524 /* GDG double wide on either pipe, otherwise pipe A only */
6525 return INTEL_INFO(dev_priv)->gen < 4 &&
6526 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6527}
6528
Daniel Vettera43f6e02013-06-07 23:10:32 +02006529static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006530 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006531{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006532 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006533 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006534 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006535
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006536 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006537 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006538 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006539
6540 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006541 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006542 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006543 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006544 if (intel_crtc_supports_double_wide(crtc) &&
6545 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006546 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006547 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006548 }
6549
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006550 if (adjusted_mode->crtc_clock > clock_limit) {
6551 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6552 adjusted_mode->crtc_clock, clock_limit,
6553 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006554 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006555 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006556 }
Chris Wilson89749352010-09-12 18:25:19 +01006557
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006558 /*
6559 * Pipe horizontal size must be even in:
6560 * - DVO ganged mode
6561 * - LVDS dual channel mode
6562 * - Double wide pipe
6563 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006564 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006565 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6566 pipe_config->pipe_src_w &= ~1;
6567
Damien Lespiau8693a822013-05-03 18:48:11 +01006568 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6569 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006570 */
6571 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006572 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006573 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006574
Damien Lespiauf5adf942013-06-24 18:29:34 +01006575 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006576 hsw_compute_ips_config(crtc, pipe_config);
6577
Daniel Vetter877d48d2013-04-19 11:24:43 +02006578 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006579 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006580
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006581 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006582}
6583
Ville Syrjälä1652d192015-03-31 14:12:01 +03006584static int skylake_get_display_clock_speed(struct drm_device *dev)
6585{
6586 struct drm_i915_private *dev_priv = to_i915(dev);
6587 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6588 uint32_t cdctl = I915_READ(CDCLK_CTL);
6589 uint32_t linkrate;
6590
Damien Lespiau414355a2015-06-04 18:21:31 +01006591 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006592 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006593
6594 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6595 return 540000;
6596
6597 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006598 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006599
Damien Lespiau71cd8422015-04-30 16:39:17 +01006600 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6601 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006602 /* vco 8640 */
6603 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6604 case CDCLK_FREQ_450_432:
6605 return 432000;
6606 case CDCLK_FREQ_337_308:
6607 return 308570;
6608 case CDCLK_FREQ_675_617:
6609 return 617140;
6610 default:
6611 WARN(1, "Unknown cd freq selection\n");
6612 }
6613 } else {
6614 /* vco 8100 */
6615 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6616 case CDCLK_FREQ_450_432:
6617 return 450000;
6618 case CDCLK_FREQ_337_308:
6619 return 337500;
6620 case CDCLK_FREQ_675_617:
6621 return 675000;
6622 default:
6623 WARN(1, "Unknown cd freq selection\n");
6624 }
6625 }
6626
6627 /* error case, do as if DPLL0 isn't enabled */
6628 return 24000;
6629}
6630
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006631static int broxton_get_display_clock_speed(struct drm_device *dev)
6632{
6633 struct drm_i915_private *dev_priv = to_i915(dev);
6634 uint32_t cdctl = I915_READ(CDCLK_CTL);
6635 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6636 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6637 int cdclk;
6638
6639 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6640 return 19200;
6641
6642 cdclk = 19200 * pll_ratio / 2;
6643
6644 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6645 case BXT_CDCLK_CD2X_DIV_SEL_1:
6646 return cdclk; /* 576MHz or 624MHz */
6647 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6648 return cdclk * 2 / 3; /* 384MHz */
6649 case BXT_CDCLK_CD2X_DIV_SEL_2:
6650 return cdclk / 2; /* 288MHz */
6651 case BXT_CDCLK_CD2X_DIV_SEL_4:
6652 return cdclk / 4; /* 144MHz */
6653 }
6654
6655 /* error case, do as if DE PLL isn't enabled */
6656 return 19200;
6657}
6658
Ville Syrjälä1652d192015-03-31 14:12:01 +03006659static int broadwell_get_display_clock_speed(struct drm_device *dev)
6660{
6661 struct drm_i915_private *dev_priv = dev->dev_private;
6662 uint32_t lcpll = I915_READ(LCPLL_CTL);
6663 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6664
6665 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6666 return 800000;
6667 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6668 return 450000;
6669 else if (freq == LCPLL_CLK_FREQ_450)
6670 return 450000;
6671 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6672 return 540000;
6673 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6674 return 337500;
6675 else
6676 return 675000;
6677}
6678
6679static int haswell_get_display_clock_speed(struct drm_device *dev)
6680{
6681 struct drm_i915_private *dev_priv = dev->dev_private;
6682 uint32_t lcpll = I915_READ(LCPLL_CTL);
6683 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6684
6685 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6686 return 800000;
6687 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6688 return 450000;
6689 else if (freq == LCPLL_CLK_FREQ_450)
6690 return 450000;
6691 else if (IS_HSW_ULT(dev))
6692 return 337500;
6693 else
6694 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006695}
6696
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006697static int valleyview_get_display_clock_speed(struct drm_device *dev)
6698{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006699 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6700 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006701}
6702
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006703static int ilk_get_display_clock_speed(struct drm_device *dev)
6704{
6705 return 450000;
6706}
6707
Jesse Barnese70236a2009-09-21 10:42:27 -07006708static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006709{
Jesse Barnese70236a2009-09-21 10:42:27 -07006710 return 400000;
6711}
Jesse Barnes79e53942008-11-07 14:24:08 -08006712
Jesse Barnese70236a2009-09-21 10:42:27 -07006713static int i915_get_display_clock_speed(struct drm_device *dev)
6714{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006715 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006716}
Jesse Barnes79e53942008-11-07 14:24:08 -08006717
Jesse Barnese70236a2009-09-21 10:42:27 -07006718static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6719{
6720 return 200000;
6721}
Jesse Barnes79e53942008-11-07 14:24:08 -08006722
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006723static int pnv_get_display_clock_speed(struct drm_device *dev)
6724{
6725 u16 gcfgc = 0;
6726
6727 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6728
6729 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6730 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006731 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006732 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006733 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006734 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006735 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006736 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6737 return 200000;
6738 default:
6739 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6740 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006741 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006742 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006743 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006744 }
6745}
6746
Jesse Barnese70236a2009-09-21 10:42:27 -07006747static int i915gm_get_display_clock_speed(struct drm_device *dev)
6748{
6749 u16 gcfgc = 0;
6750
6751 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6752
6753 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006754 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006755 else {
6756 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6757 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006758 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006759 default:
6760 case GC_DISPLAY_CLOCK_190_200_MHZ:
6761 return 190000;
6762 }
6763 }
6764}
Jesse Barnes79e53942008-11-07 14:24:08 -08006765
Jesse Barnese70236a2009-09-21 10:42:27 -07006766static int i865_get_display_clock_speed(struct drm_device *dev)
6767{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006768 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006769}
6770
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006771static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006772{
6773 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006774
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006775 /*
6776 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6777 * encoding is different :(
6778 * FIXME is this the right way to detect 852GM/852GMV?
6779 */
6780 if (dev->pdev->revision == 0x1)
6781 return 133333;
6782
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006783 pci_bus_read_config_word(dev->pdev->bus,
6784 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6785
Jesse Barnese70236a2009-09-21 10:42:27 -07006786 /* Assume that the hardware is in the high speed state. This
6787 * should be the default.
6788 */
6789 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6790 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006791 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006792 case GC_CLOCK_100_200:
6793 return 200000;
6794 case GC_CLOCK_166_250:
6795 return 250000;
6796 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006797 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006798 case GC_CLOCK_133_266:
6799 case GC_CLOCK_133_266_2:
6800 case GC_CLOCK_166_266:
6801 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006802 }
6803
6804 /* Shouldn't happen */
6805 return 0;
6806}
6807
6808static int i830_get_display_clock_speed(struct drm_device *dev)
6809{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006810 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006811}
6812
Ville Syrjälä34edce22015-05-22 11:22:33 +03006813static unsigned int intel_hpll_vco(struct drm_device *dev)
6814{
6815 struct drm_i915_private *dev_priv = dev->dev_private;
6816 static const unsigned int blb_vco[8] = {
6817 [0] = 3200000,
6818 [1] = 4000000,
6819 [2] = 5333333,
6820 [3] = 4800000,
6821 [4] = 6400000,
6822 };
6823 static const unsigned int pnv_vco[8] = {
6824 [0] = 3200000,
6825 [1] = 4000000,
6826 [2] = 5333333,
6827 [3] = 4800000,
6828 [4] = 2666667,
6829 };
6830 static const unsigned int cl_vco[8] = {
6831 [0] = 3200000,
6832 [1] = 4000000,
6833 [2] = 5333333,
6834 [3] = 6400000,
6835 [4] = 3333333,
6836 [5] = 3566667,
6837 [6] = 4266667,
6838 };
6839 static const unsigned int elk_vco[8] = {
6840 [0] = 3200000,
6841 [1] = 4000000,
6842 [2] = 5333333,
6843 [3] = 4800000,
6844 };
6845 static const unsigned int ctg_vco[8] = {
6846 [0] = 3200000,
6847 [1] = 4000000,
6848 [2] = 5333333,
6849 [3] = 6400000,
6850 [4] = 2666667,
6851 [5] = 4266667,
6852 };
6853 const unsigned int *vco_table;
6854 unsigned int vco;
6855 uint8_t tmp = 0;
6856
6857 /* FIXME other chipsets? */
6858 if (IS_GM45(dev))
6859 vco_table = ctg_vco;
6860 else if (IS_G4X(dev))
6861 vco_table = elk_vco;
6862 else if (IS_CRESTLINE(dev))
6863 vco_table = cl_vco;
6864 else if (IS_PINEVIEW(dev))
6865 vco_table = pnv_vco;
6866 else if (IS_G33(dev))
6867 vco_table = blb_vco;
6868 else
6869 return 0;
6870
6871 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6872
6873 vco = vco_table[tmp & 0x7];
6874 if (vco == 0)
6875 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6876 else
6877 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6878
6879 return vco;
6880}
6881
6882static int gm45_get_display_clock_speed(struct drm_device *dev)
6883{
6884 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6885 uint16_t tmp = 0;
6886
6887 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6888
6889 cdclk_sel = (tmp >> 12) & 0x1;
6890
6891 switch (vco) {
6892 case 2666667:
6893 case 4000000:
6894 case 5333333:
6895 return cdclk_sel ? 333333 : 222222;
6896 case 3200000:
6897 return cdclk_sel ? 320000 : 228571;
6898 default:
6899 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6900 return 222222;
6901 }
6902}
6903
6904static int i965gm_get_display_clock_speed(struct drm_device *dev)
6905{
6906 static const uint8_t div_3200[] = { 16, 10, 8 };
6907 static const uint8_t div_4000[] = { 20, 12, 10 };
6908 static const uint8_t div_5333[] = { 24, 16, 14 };
6909 const uint8_t *div_table;
6910 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6911 uint16_t tmp = 0;
6912
6913 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6914
6915 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6916
6917 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6918 goto fail;
6919
6920 switch (vco) {
6921 case 3200000:
6922 div_table = div_3200;
6923 break;
6924 case 4000000:
6925 div_table = div_4000;
6926 break;
6927 case 5333333:
6928 div_table = div_5333;
6929 break;
6930 default:
6931 goto fail;
6932 }
6933
6934 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6935
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006936fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006937 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6938 return 200000;
6939}
6940
6941static int g33_get_display_clock_speed(struct drm_device *dev)
6942{
6943 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6944 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6945 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6946 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6947 const uint8_t *div_table;
6948 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6949 uint16_t tmp = 0;
6950
6951 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6952
6953 cdclk_sel = (tmp >> 4) & 0x7;
6954
6955 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6956 goto fail;
6957
6958 switch (vco) {
6959 case 3200000:
6960 div_table = div_3200;
6961 break;
6962 case 4000000:
6963 div_table = div_4000;
6964 break;
6965 case 4800000:
6966 div_table = div_4800;
6967 break;
6968 case 5333333:
6969 div_table = div_5333;
6970 break;
6971 default:
6972 goto fail;
6973 }
6974
6975 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6976
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006977fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006978 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6979 return 190476;
6980}
6981
Zhenyu Wang2c072452009-06-05 15:38:42 +08006982static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006983intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006984{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006985 while (*num > DATA_LINK_M_N_MASK ||
6986 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006987 *num >>= 1;
6988 *den >>= 1;
6989 }
6990}
6991
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006992static void compute_m_n(unsigned int m, unsigned int n,
6993 uint32_t *ret_m, uint32_t *ret_n)
6994{
6995 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6996 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6997 intel_reduce_m_n_ratio(ret_m, ret_n);
6998}
6999
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007000void
7001intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7002 int pixel_clock, int link_clock,
7003 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007004{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007005 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007006
7007 compute_m_n(bits_per_pixel * pixel_clock,
7008 link_clock * nlanes * 8,
7009 &m_n->gmch_m, &m_n->gmch_n);
7010
7011 compute_m_n(pixel_clock, link_clock,
7012 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007013}
7014
Chris Wilsona7615032011-01-12 17:04:08 +00007015static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7016{
Jani Nikulad330a952014-01-21 11:24:25 +02007017 if (i915.panel_use_ssc >= 0)
7018 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007019 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007020 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007021}
7022
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007023static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007024{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007025 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007026}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007027
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007028static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7029{
7030 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007031}
7032
Daniel Vetterf47709a2013-03-28 10:42:02 +01007033static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007034 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007035 intel_clock_t *reduced_clock)
7036{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007037 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007038 u32 fp, fp2 = 0;
7039
7040 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007041 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007042 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007043 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007044 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007045 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007046 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007047 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007048 }
7049
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007050 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007051
Daniel Vetterf47709a2013-03-28 10:42:02 +01007052 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007053 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007054 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007055 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007056 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007057 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007058 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007059 }
7060}
7061
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007062static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7063 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007064{
7065 u32 reg_val;
7066
7067 /*
7068 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7069 * and set it to a reasonable value instead.
7070 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007071 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007072 reg_val &= 0xffffff00;
7073 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007074 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007075
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007076 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007077 reg_val &= 0x8cffffff;
7078 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007079 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007080
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007081 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007082 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007083 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007084
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007085 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007086 reg_val &= 0x00ffffff;
7087 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007088 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007089}
7090
Daniel Vetterb5518422013-05-03 11:49:48 +02007091static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7092 struct intel_link_m_n *m_n)
7093{
7094 struct drm_device *dev = crtc->base.dev;
7095 struct drm_i915_private *dev_priv = dev->dev_private;
7096 int pipe = crtc->pipe;
7097
Daniel Vettere3b95f12013-05-03 11:49:49 +02007098 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7099 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7100 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7101 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007102}
7103
7104static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007105 struct intel_link_m_n *m_n,
7106 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007107{
7108 struct drm_device *dev = crtc->base.dev;
7109 struct drm_i915_private *dev_priv = dev->dev_private;
7110 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007111 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007112
7113 if (INTEL_INFO(dev)->gen >= 5) {
7114 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7115 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7116 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7117 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007118 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7119 * for gen < 8) and if DRRS is supported (to make sure the
7120 * registers are not unnecessarily accessed).
7121 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307122 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007123 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007124 I915_WRITE(PIPE_DATA_M2(transcoder),
7125 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7126 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7127 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7128 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7129 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007130 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007131 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7132 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7133 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7134 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007135 }
7136}
7137
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307138void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007139{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307140 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7141
7142 if (m_n == M1_N1) {
7143 dp_m_n = &crtc->config->dp_m_n;
7144 dp_m2_n2 = &crtc->config->dp_m2_n2;
7145 } else if (m_n == M2_N2) {
7146
7147 /*
7148 * M2_N2 registers are not supported. Hence m2_n2 divider value
7149 * needs to be programmed into M1_N1.
7150 */
7151 dp_m_n = &crtc->config->dp_m2_n2;
7152 } else {
7153 DRM_ERROR("Unsupported divider value\n");
7154 return;
7155 }
7156
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007157 if (crtc->config->has_pch_encoder)
7158 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007159 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307160 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007161}
7162
Daniel Vetter251ac862015-06-18 10:30:24 +02007163static void vlv_compute_dpll(struct intel_crtc *crtc,
7164 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007165{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007166 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
7167 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7168 DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV;
7169 if (crtc->pipe != PIPE_A)
7170 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007171
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007172 pipe_config->dpll_hw_state.dpll_md =
7173 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7174}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007175
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007176static void chv_compute_dpll(struct intel_crtc *crtc,
7177 struct intel_crtc_state *pipe_config)
7178{
7179 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7180 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7181 DPLL_VCO_ENABLE;
7182 if (crtc->pipe != PIPE_A)
7183 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7184
7185 pipe_config->dpll_hw_state.dpll_md =
7186 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007187}
7188
Ville Syrjäläd288f652014-10-28 13:20:22 +02007189static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007190 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007191{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007192 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007193 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007194 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007195 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007196 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007197 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007198
Ville Syrjäläa5805162015-05-26 20:42:30 +03007199 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007200
Ville Syrjäläd288f652014-10-28 13:20:22 +02007201 bestn = pipe_config->dpll.n;
7202 bestm1 = pipe_config->dpll.m1;
7203 bestm2 = pipe_config->dpll.m2;
7204 bestp1 = pipe_config->dpll.p1;
7205 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007206
Jesse Barnes89b667f2013-04-18 14:51:36 -07007207 /* See eDP HDMI DPIO driver vbios notes doc */
7208
7209 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007210 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007211 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007212
7213 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007214 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007215
7216 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007217 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007218 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007219 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007220
7221 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007222 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007223
7224 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007225 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7226 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7227 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007228 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007229
7230 /*
7231 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7232 * but we don't support that).
7233 * Note: don't use the DAC post divider as it seems unstable.
7234 */
7235 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007236 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007237
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007238 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007239 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007240
Jesse Barnes89b667f2013-04-18 14:51:36 -07007241 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007242 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007243 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7244 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007245 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007246 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007247 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007248 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007249 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007250
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007251 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007252 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007253 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007254 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007255 0x0df40000);
7256 else
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 { /* HDMI or VGA */
7260 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007261 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007262 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007263 0x0df70000);
7264 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007265 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007266 0x0df40000);
7267 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007268
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007269 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007270 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007271 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7272 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007273 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007274 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007275
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007276 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007277 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007278}
7279
Ville Syrjäläd288f652014-10-28 13:20:22 +02007280static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007281 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007282{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007283 struct drm_device *dev = crtc->base.dev;
7284 struct drm_i915_private *dev_priv = dev->dev_private;
7285 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02007286 i915_reg_t dpll_reg = DPLL(crtc->pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007287 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307288 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007289 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307290 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307291 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007292
Ville Syrjäläd288f652014-10-28 13:20:22 +02007293 bestn = pipe_config->dpll.n;
7294 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7295 bestm1 = pipe_config->dpll.m1;
7296 bestm2 = pipe_config->dpll.m2 >> 22;
7297 bestp1 = pipe_config->dpll.p1;
7298 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307299 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307300 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307301 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007302
7303 /*
7304 * Enable Refclk and SSC
7305 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007306 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007307 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007308
Ville Syrjäläa5805162015-05-26 20:42:30 +03007309 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007310
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007311 /* p1 and p2 divider */
7312 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7313 5 << DPIO_CHV_S1_DIV_SHIFT |
7314 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7315 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7316 1 << DPIO_CHV_K_DIV_SHIFT);
7317
7318 /* Feedback post-divider - m2 */
7319 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7320
7321 /* Feedback refclk divider - n and m1 */
7322 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7323 DPIO_CHV_M1_DIV_BY_2 |
7324 1 << DPIO_CHV_N_DIV_SHIFT);
7325
7326 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007327 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007328
7329 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307330 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7331 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7332 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7333 if (bestm2_frac)
7334 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7335 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007336
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307337 /* Program digital lock detect threshold */
7338 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7339 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7340 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7341 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7342 if (!bestm2_frac)
7343 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7344 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7345
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007346 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307347 if (vco == 5400000) {
7348 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7349 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7350 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7351 tribuf_calcntr = 0x9;
7352 } else if (vco <= 6200000) {
7353 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7354 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7355 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7356 tribuf_calcntr = 0x9;
7357 } else if (vco <= 6480000) {
7358 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7359 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7360 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7361 tribuf_calcntr = 0x8;
7362 } else {
7363 /* Not supported. Apply the same limits as in the max case */
7364 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7365 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7366 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7367 tribuf_calcntr = 0;
7368 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007369 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7370
Ville Syrjälä968040b2015-03-11 22:52:08 +02007371 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307372 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7373 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7374 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7375
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007376 /* AFC Recal */
7377 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7378 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7379 DPIO_AFC_RECAL);
7380
Ville Syrjäläa5805162015-05-26 20:42:30 +03007381 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007382}
7383
Ville Syrjäläd288f652014-10-28 13:20:22 +02007384/**
7385 * vlv_force_pll_on - forcibly enable just the PLL
7386 * @dev_priv: i915 private structure
7387 * @pipe: pipe PLL to enable
7388 * @dpll: PLL configuration
7389 *
7390 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7391 * in cases where we need the PLL enabled even when @pipe is not going to
7392 * be enabled.
7393 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007394int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7395 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007396{
7397 struct intel_crtc *crtc =
7398 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007399 struct intel_crtc_state *pipe_config;
7400
7401 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7402 if (!pipe_config)
7403 return -ENOMEM;
7404
7405 pipe_config->base.crtc = &crtc->base;
7406 pipe_config->pixel_multiplier = 1;
7407 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007408
7409 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007410 chv_compute_dpll(crtc, pipe_config);
7411 chv_prepare_pll(crtc, pipe_config);
7412 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007413 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007414 vlv_compute_dpll(crtc, pipe_config);
7415 vlv_prepare_pll(crtc, pipe_config);
7416 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007417 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007418
7419 kfree(pipe_config);
7420
7421 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007422}
7423
7424/**
7425 * vlv_force_pll_off - forcibly disable just the PLL
7426 * @dev_priv: i915 private structure
7427 * @pipe: pipe PLL to disable
7428 *
7429 * Disable the PLL for @pipe. To be used in cases where we need
7430 * the PLL enabled even when @pipe is not going to be enabled.
7431 */
7432void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7433{
7434 if (IS_CHERRYVIEW(dev))
7435 chv_disable_pll(to_i915(dev), pipe);
7436 else
7437 vlv_disable_pll(to_i915(dev), pipe);
7438}
7439
Daniel Vetter251ac862015-06-18 10:30:24 +02007440static void i9xx_compute_dpll(struct intel_crtc *crtc,
7441 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007442 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007443{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007444 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007445 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007446 u32 dpll;
7447 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007448 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007449
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007450 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307451
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007452 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7453 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007454
7455 dpll = DPLL_VGA_MODE_DIS;
7456
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007457 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007458 dpll |= DPLLB_MODE_LVDS;
7459 else
7460 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007461
Daniel Vetteref1b4602013-06-01 17:17:04 +02007462 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007463 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007464 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007465 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007466
7467 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007468 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007469
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007470 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007471 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007472
7473 /* compute bitmask from p1 value */
7474 if (IS_PINEVIEW(dev))
7475 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7476 else {
7477 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7478 if (IS_G4X(dev) && reduced_clock)
7479 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7480 }
7481 switch (clock->p2) {
7482 case 5:
7483 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7484 break;
7485 case 7:
7486 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7487 break;
7488 case 10:
7489 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7490 break;
7491 case 14:
7492 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7493 break;
7494 }
7495 if (INTEL_INFO(dev)->gen >= 4)
7496 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7497
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007498 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007499 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007500 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007501 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007502 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7503 else
7504 dpll |= PLL_REF_INPUT_DREFCLK;
7505
7506 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007507 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007508
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007509 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007510 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007511 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007512 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007513 }
7514}
7515
Daniel Vetter251ac862015-06-18 10:30:24 +02007516static void i8xx_compute_dpll(struct intel_crtc *crtc,
7517 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007518 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007519{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007520 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007521 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007522 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007523 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007524
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007525 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307526
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007527 dpll = DPLL_VGA_MODE_DIS;
7528
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007529 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007530 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7531 } else {
7532 if (clock->p1 == 2)
7533 dpll |= PLL_P1_DIVIDE_BY_TWO;
7534 else
7535 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7536 if (clock->p2 == 4)
7537 dpll |= PLL_P2_DIVIDE_BY_4;
7538 }
7539
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007540 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007541 dpll |= DPLL_DVO_2X_MODE;
7542
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007543 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007544 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007545 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7546 else
7547 dpll |= PLL_REF_INPUT_DREFCLK;
7548
7549 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007550 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007551}
7552
Daniel Vetter8a654f32013-06-01 17:16:22 +02007553static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007554{
7555 struct drm_device *dev = intel_crtc->base.dev;
7556 struct drm_i915_private *dev_priv = dev->dev_private;
7557 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007558 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007559 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007560 uint32_t crtc_vtotal, crtc_vblank_end;
7561 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007562
7563 /* We need to be careful not to changed the adjusted mode, for otherwise
7564 * the hw state checker will get angry at the mismatch. */
7565 crtc_vtotal = adjusted_mode->crtc_vtotal;
7566 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007567
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007568 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007569 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007570 crtc_vtotal -= 1;
7571 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007572
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007573 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007574 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7575 else
7576 vsyncshift = adjusted_mode->crtc_hsync_start -
7577 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007578 if (vsyncshift < 0)
7579 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007580 }
7581
7582 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007583 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007584
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007585 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007586 (adjusted_mode->crtc_hdisplay - 1) |
7587 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007588 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007589 (adjusted_mode->crtc_hblank_start - 1) |
7590 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007591 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007592 (adjusted_mode->crtc_hsync_start - 1) |
7593 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7594
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007595 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007596 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007597 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007598 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007599 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007600 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007601 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007602 (adjusted_mode->crtc_vsync_start - 1) |
7603 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7604
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007605 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7606 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7607 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7608 * bits. */
7609 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7610 (pipe == PIPE_B || pipe == PIPE_C))
7611 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7612
Jani Nikulabc58be62016-03-18 17:05:39 +02007613}
7614
7615static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7616{
7617 struct drm_device *dev = intel_crtc->base.dev;
7618 struct drm_i915_private *dev_priv = dev->dev_private;
7619 enum pipe pipe = intel_crtc->pipe;
7620
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007621 /* pipesrc controls the size that is scaled from, which should
7622 * always be the user's requested size.
7623 */
7624 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007625 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7626 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007627}
7628
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007629static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007630 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007631{
7632 struct drm_device *dev = crtc->base.dev;
7633 struct drm_i915_private *dev_priv = dev->dev_private;
7634 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7635 uint32_t tmp;
7636
7637 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007638 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7639 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007640 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007641 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7642 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007643 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007644 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7645 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007646
7647 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007648 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7649 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007650 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007651 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7652 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007653 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007654 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7655 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007656
7657 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007658 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7659 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7660 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007661 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007662}
7663
7664static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7665 struct intel_crtc_state *pipe_config)
7666{
7667 struct drm_device *dev = crtc->base.dev;
7668 struct drm_i915_private *dev_priv = dev->dev_private;
7669 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007670
7671 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007672 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7673 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7674
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007675 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7676 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007677}
7678
Daniel Vetterf6a83282014-02-11 15:28:57 -08007679void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007680 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007681{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007682 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7683 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7684 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7685 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007686
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007687 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7688 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7689 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7690 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007691
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007692 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007693 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007694
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007695 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7696 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007697
7698 mode->hsync = drm_mode_hsync(mode);
7699 mode->vrefresh = drm_mode_vrefresh(mode);
7700 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007701}
7702
Daniel Vetter84b046f2013-02-19 18:48:54 +01007703static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7704{
7705 struct drm_device *dev = intel_crtc->base.dev;
7706 struct drm_i915_private *dev_priv = dev->dev_private;
7707 uint32_t pipeconf;
7708
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007709 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007710
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007711 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7712 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7713 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007714
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007715 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007716 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007717
Daniel Vetterff9ce462013-04-24 14:57:17 +02007718 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007719 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007720 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007721 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007722 pipeconf |= PIPECONF_DITHER_EN |
7723 PIPECONF_DITHER_TYPE_SP;
7724
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007725 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007726 case 18:
7727 pipeconf |= PIPECONF_6BPC;
7728 break;
7729 case 24:
7730 pipeconf |= PIPECONF_8BPC;
7731 break;
7732 case 30:
7733 pipeconf |= PIPECONF_10BPC;
7734 break;
7735 default:
7736 /* Case prevented by intel_choose_pipe_bpp_dither. */
7737 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007738 }
7739 }
7740
7741 if (HAS_PIPE_CXSR(dev)) {
7742 if (intel_crtc->lowfreq_avail) {
7743 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7744 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7745 } else {
7746 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007747 }
7748 }
7749
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007750 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007751 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007752 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007753 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7754 else
7755 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7756 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007757 pipeconf |= PIPECONF_PROGRESSIVE;
7758
Wayne Boyer666a4532015-12-09 12:29:35 -08007759 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7760 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007761 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007762
Daniel Vetter84b046f2013-02-19 18:48:54 +01007763 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7764 POSTING_READ(PIPECONF(intel_crtc->pipe));
7765}
7766
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007767static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7768 struct intel_crtc_state *crtc_state)
7769{
7770 struct drm_device *dev = crtc->base.dev;
7771 struct drm_i915_private *dev_priv = dev->dev_private;
7772 const intel_limit_t *limit;
7773 int refclk = 48000;
7774
7775 memset(&crtc_state->dpll_hw_state, 0,
7776 sizeof(crtc_state->dpll_hw_state));
7777
7778 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7779 if (intel_panel_use_ssc(dev_priv)) {
7780 refclk = dev_priv->vbt.lvds_ssc_freq;
7781 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7782 }
7783
7784 limit = &intel_limits_i8xx_lvds;
7785 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7786 limit = &intel_limits_i8xx_dvo;
7787 } else {
7788 limit = &intel_limits_i8xx_dac;
7789 }
7790
7791 if (!crtc_state->clock_set &&
7792 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7793 refclk, NULL, &crtc_state->dpll)) {
7794 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7795 return -EINVAL;
7796 }
7797
7798 i8xx_compute_dpll(crtc, crtc_state, NULL);
7799
7800 return 0;
7801}
7802
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007803static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7804 struct intel_crtc_state *crtc_state)
7805{
7806 struct drm_device *dev = crtc->base.dev;
7807 struct drm_i915_private *dev_priv = dev->dev_private;
7808 const intel_limit_t *limit;
7809 int refclk = 96000;
7810
7811 memset(&crtc_state->dpll_hw_state, 0,
7812 sizeof(crtc_state->dpll_hw_state));
7813
7814 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7815 if (intel_panel_use_ssc(dev_priv)) {
7816 refclk = dev_priv->vbt.lvds_ssc_freq;
7817 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7818 }
7819
7820 if (intel_is_dual_link_lvds(dev))
7821 limit = &intel_limits_g4x_dual_channel_lvds;
7822 else
7823 limit = &intel_limits_g4x_single_channel_lvds;
7824 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7825 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7826 limit = &intel_limits_g4x_hdmi;
7827 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7828 limit = &intel_limits_g4x_sdvo;
7829 } else {
7830 /* The option is for other outputs */
7831 limit = &intel_limits_i9xx_sdvo;
7832 }
7833
7834 if (!crtc_state->clock_set &&
7835 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7836 refclk, NULL, &crtc_state->dpll)) {
7837 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7838 return -EINVAL;
7839 }
7840
7841 i9xx_compute_dpll(crtc, crtc_state, NULL);
7842
7843 return 0;
7844}
7845
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007846static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7847 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007848{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007849 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007850 struct drm_i915_private *dev_priv = dev->dev_private;
Ma Lingd4906092009-03-18 20:13:27 +08007851 const intel_limit_t *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007852 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007853
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007854 memset(&crtc_state->dpll_hw_state, 0,
7855 sizeof(crtc_state->dpll_hw_state));
7856
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007857 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7858 if (intel_panel_use_ssc(dev_priv)) {
7859 refclk = dev_priv->vbt.lvds_ssc_freq;
7860 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7861 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007862
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007863 limit = &intel_limits_pineview_lvds;
7864 } else {
7865 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007866 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007867
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007868 if (!crtc_state->clock_set &&
7869 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7870 refclk, NULL, &crtc_state->dpll)) {
7871 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7872 return -EINVAL;
7873 }
7874
7875 i9xx_compute_dpll(crtc, crtc_state, NULL);
7876
7877 return 0;
7878}
7879
7880static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7881 struct intel_crtc_state *crtc_state)
7882{
7883 struct drm_device *dev = crtc->base.dev;
7884 struct drm_i915_private *dev_priv = dev->dev_private;
7885 const intel_limit_t *limit;
7886 int refclk = 96000;
7887
7888 memset(&crtc_state->dpll_hw_state, 0,
7889 sizeof(crtc_state->dpll_hw_state));
7890
7891 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7892 if (intel_panel_use_ssc(dev_priv)) {
7893 refclk = dev_priv->vbt.lvds_ssc_freq;
7894 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007895 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007896
7897 limit = &intel_limits_i9xx_lvds;
7898 } else {
7899 limit = &intel_limits_i9xx_sdvo;
7900 }
7901
7902 if (!crtc_state->clock_set &&
7903 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7904 refclk, NULL, &crtc_state->dpll)) {
7905 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7906 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007907 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007908
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007909 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007910
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007911 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007912}
7913
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007914static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7915 struct intel_crtc_state *crtc_state)
7916{
7917 int refclk = 100000;
7918 const intel_limit_t *limit = &intel_limits_chv;
7919
7920 memset(&crtc_state->dpll_hw_state, 0,
7921 sizeof(crtc_state->dpll_hw_state));
7922
7923 if (crtc_state->has_dsi_encoder)
7924 return 0;
7925
7926 if (!crtc_state->clock_set &&
7927 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7928 refclk, NULL, &crtc_state->dpll)) {
7929 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7930 return -EINVAL;
7931 }
7932
7933 chv_compute_dpll(crtc, crtc_state);
7934
7935 return 0;
7936}
7937
7938static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7939 struct intel_crtc_state *crtc_state)
7940{
7941 int refclk = 100000;
7942 const intel_limit_t *limit = &intel_limits_vlv;
7943
7944 memset(&crtc_state->dpll_hw_state, 0,
7945 sizeof(crtc_state->dpll_hw_state));
7946
7947 if (crtc_state->has_dsi_encoder)
7948 return 0;
7949
7950 if (!crtc_state->clock_set &&
7951 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7952 refclk, NULL, &crtc_state->dpll)) {
7953 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7954 return -EINVAL;
7955 }
7956
7957 vlv_compute_dpll(crtc, crtc_state);
7958
7959 return 0;
7960}
7961
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007962static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007963 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007964{
7965 struct drm_device *dev = crtc->base.dev;
7966 struct drm_i915_private *dev_priv = dev->dev_private;
7967 uint32_t tmp;
7968
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007969 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7970 return;
7971
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007972 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007973 if (!(tmp & PFIT_ENABLE))
7974 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007975
Daniel Vetter06922822013-07-11 13:35:40 +02007976 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007977 if (INTEL_INFO(dev)->gen < 4) {
7978 if (crtc->pipe != PIPE_B)
7979 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007980 } else {
7981 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7982 return;
7983 }
7984
Daniel Vetter06922822013-07-11 13:35:40 +02007985 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007986 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7987 if (INTEL_INFO(dev)->gen < 5)
7988 pipe_config->gmch_pfit.lvds_border_bits =
7989 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7990}
7991
Jesse Barnesacbec812013-09-20 11:29:32 -07007992static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007993 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007994{
7995 struct drm_device *dev = crtc->base.dev;
7996 struct drm_i915_private *dev_priv = dev->dev_private;
7997 int pipe = pipe_config->cpu_transcoder;
7998 intel_clock_t clock;
7999 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008000 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008001
Shobhit Kumarf573de52014-07-30 20:32:37 +05308002 /* In case of MIPI DPLL will not even be used */
8003 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8004 return;
8005
Ville Syrjäläa5805162015-05-26 20:42:30 +03008006 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008007 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008008 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008009
8010 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8011 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8012 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8013 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8014 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8015
Imre Deakdccbea32015-06-22 23:35:51 +03008016 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008017}
8018
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008019static void
8020i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8021 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008022{
8023 struct drm_device *dev = crtc->base.dev;
8024 struct drm_i915_private *dev_priv = dev->dev_private;
8025 u32 val, base, offset;
8026 int pipe = crtc->pipe, plane = crtc->plane;
8027 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008028 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008029 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008030 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008031
Damien Lespiau42a7b082015-02-05 19:35:13 +00008032 val = I915_READ(DSPCNTR(plane));
8033 if (!(val & DISPLAY_PLANE_ENABLE))
8034 return;
8035
Damien Lespiaud9806c92015-01-21 14:07:19 +00008036 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008037 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008038 DRM_DEBUG_KMS("failed to alloc fb\n");
8039 return;
8040 }
8041
Damien Lespiau1b842c82015-01-21 13:50:54 +00008042 fb = &intel_fb->base;
8043
Daniel Vetter18c52472015-02-10 17:16:09 +00008044 if (INTEL_INFO(dev)->gen >= 4) {
8045 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008046 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008047 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8048 }
8049 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008050
8051 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008052 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008053 fb->pixel_format = fourcc;
8054 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008055
8056 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008057 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008058 offset = I915_READ(DSPTILEOFF(plane));
8059 else
8060 offset = I915_READ(DSPLINOFF(plane));
8061 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8062 } else {
8063 base = I915_READ(DSPADDR(plane));
8064 }
8065 plane_config->base = base;
8066
8067 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008068 fb->width = ((val >> 16) & 0xfff) + 1;
8069 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008070
8071 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008072 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008073
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008074 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008075 fb->pixel_format,
8076 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008077
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008078 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008079
Damien Lespiau2844a922015-01-20 12:51:48 +00008080 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8081 pipe_name(pipe), plane, fb->width, fb->height,
8082 fb->bits_per_pixel, base, fb->pitches[0],
8083 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008084
Damien Lespiau2d140302015-02-05 17:22:18 +00008085 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008086}
8087
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008088static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008089 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008090{
8091 struct drm_device *dev = crtc->base.dev;
8092 struct drm_i915_private *dev_priv = dev->dev_private;
8093 int pipe = pipe_config->cpu_transcoder;
8094 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8095 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008096 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008097 int refclk = 100000;
8098
Ville Syrjäläa5805162015-05-26 20:42:30 +03008099 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008100 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8101 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8102 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8103 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008104 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008105 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008106
8107 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008108 clock.m2 = (pll_dw0 & 0xff) << 22;
8109 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8110 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008111 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8112 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8113 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8114
Imre Deakdccbea32015-06-22 23:35:51 +03008115 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008116}
8117
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008118static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008119 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008120{
8121 struct drm_device *dev = crtc->base.dev;
8122 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008123 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008124 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008125 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008126
Imre Deak17290502016-02-12 18:55:11 +02008127 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8128 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008129 return false;
8130
Daniel Vettere143a212013-07-04 12:01:15 +02008131 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008132 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008133
Imre Deak17290502016-02-12 18:55:11 +02008134 ret = false;
8135
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008136 tmp = I915_READ(PIPECONF(crtc->pipe));
8137 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008138 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008139
Wayne Boyer666a4532015-12-09 12:29:35 -08008140 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008141 switch (tmp & PIPECONF_BPC_MASK) {
8142 case PIPECONF_6BPC:
8143 pipe_config->pipe_bpp = 18;
8144 break;
8145 case PIPECONF_8BPC:
8146 pipe_config->pipe_bpp = 24;
8147 break;
8148 case PIPECONF_10BPC:
8149 pipe_config->pipe_bpp = 30;
8150 break;
8151 default:
8152 break;
8153 }
8154 }
8155
Wayne Boyer666a4532015-12-09 12:29:35 -08008156 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8157 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008158 pipe_config->limited_color_range = true;
8159
Ville Syrjälä282740f2013-09-04 18:30:03 +03008160 if (INTEL_INFO(dev)->gen < 4)
8161 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8162
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008163 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008164 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008165
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008166 i9xx_get_pfit_config(crtc, pipe_config);
8167
Daniel Vetter6c49f242013-06-06 12:45:25 +02008168 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008169 /* No way to read it out on pipes B and C */
8170 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8171 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8172 else
8173 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008174 pipe_config->pixel_multiplier =
8175 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8176 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008177 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008178 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8179 tmp = I915_READ(DPLL(crtc->pipe));
8180 pipe_config->pixel_multiplier =
8181 ((tmp & SDVO_MULTIPLIER_MASK)
8182 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8183 } else {
8184 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8185 * port and will be fixed up in the encoder->get_config
8186 * function. */
8187 pipe_config->pixel_multiplier = 1;
8188 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008189 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008190 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008191 /*
8192 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8193 * on 830. Filter it out here so that we don't
8194 * report errors due to that.
8195 */
8196 if (IS_I830(dev))
8197 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8198
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008199 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8200 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008201 } else {
8202 /* Mask out read-only status bits. */
8203 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8204 DPLL_PORTC_READY_MASK |
8205 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008206 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008207
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008208 if (IS_CHERRYVIEW(dev))
8209 chv_crtc_clock_get(crtc, pipe_config);
8210 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008211 vlv_crtc_clock_get(crtc, pipe_config);
8212 else
8213 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008214
Ville Syrjälä0f646142015-08-26 19:39:18 +03008215 /*
8216 * Normally the dotclock is filled in by the encoder .get_config()
8217 * but in case the pipe is enabled w/o any ports we need a sane
8218 * default.
8219 */
8220 pipe_config->base.adjusted_mode.crtc_clock =
8221 pipe_config->port_clock / pipe_config->pixel_multiplier;
8222
Imre Deak17290502016-02-12 18:55:11 +02008223 ret = true;
8224
8225out:
8226 intel_display_power_put(dev_priv, power_domain);
8227
8228 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008229}
8230
Paulo Zanonidde86e22012-12-01 12:04:25 -02008231static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008232{
8233 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008234 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008235 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008236 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008237 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008238 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008239 bool has_ck505 = false;
8240 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008241
8242 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008243 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008244 switch (encoder->type) {
8245 case INTEL_OUTPUT_LVDS:
8246 has_panel = true;
8247 has_lvds = true;
8248 break;
8249 case INTEL_OUTPUT_EDP:
8250 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008251 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008252 has_cpu_edp = true;
8253 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008254 default:
8255 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008256 }
8257 }
8258
Keith Packard99eb6a02011-09-26 14:29:12 -07008259 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008260 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008261 can_ssc = has_ck505;
8262 } else {
8263 has_ck505 = false;
8264 can_ssc = true;
8265 }
8266
Imre Deak2de69052013-05-08 13:14:04 +03008267 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8268 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008269
8270 /* Ironlake: try to setup display ref clock before DPLL
8271 * enabling. This is only under driver's control after
8272 * PCH B stepping, previous chipset stepping should be
8273 * ignoring this setting.
8274 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008275 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008276
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008277 /* As we must carefully and slowly disable/enable each source in turn,
8278 * compute the final state we want first and check if we need to
8279 * make any changes at all.
8280 */
8281 final = val;
8282 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008283 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008284 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008285 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008286 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8287
8288 final &= ~DREF_SSC_SOURCE_MASK;
8289 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8290 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008291
Keith Packard199e5d72011-09-22 12:01:57 -07008292 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008293 final |= DREF_SSC_SOURCE_ENABLE;
8294
8295 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8296 final |= DREF_SSC1_ENABLE;
8297
8298 if (has_cpu_edp) {
8299 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8300 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8301 else
8302 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8303 } else
8304 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8305 } else {
8306 final |= DREF_SSC_SOURCE_DISABLE;
8307 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8308 }
8309
8310 if (final == val)
8311 return;
8312
8313 /* Always enable nonspread source */
8314 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8315
8316 if (has_ck505)
8317 val |= DREF_NONSPREAD_CK505_ENABLE;
8318 else
8319 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8320
8321 if (has_panel) {
8322 val &= ~DREF_SSC_SOURCE_MASK;
8323 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008324
Keith Packard199e5d72011-09-22 12:01:57 -07008325 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008326 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008327 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008328 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008329 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008330 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008331
8332 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008333 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008334 POSTING_READ(PCH_DREF_CONTROL);
8335 udelay(200);
8336
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008337 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008338
8339 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008340 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008341 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008342 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008343 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008344 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008345 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008346 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008347 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008348
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008349 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008350 POSTING_READ(PCH_DREF_CONTROL);
8351 udelay(200);
8352 } else {
8353 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8354
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008355 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008356
8357 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008358 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008359
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008360 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008361 POSTING_READ(PCH_DREF_CONTROL);
8362 udelay(200);
8363
8364 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008365 val &= ~DREF_SSC_SOURCE_MASK;
8366 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008367
8368 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008369 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008370
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008371 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008372 POSTING_READ(PCH_DREF_CONTROL);
8373 udelay(200);
8374 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008375
8376 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008377}
8378
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008379static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008380{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008381 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008382
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008383 tmp = I915_READ(SOUTH_CHICKEN2);
8384 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8385 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008386
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008387 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8388 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8389 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008390
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008391 tmp = I915_READ(SOUTH_CHICKEN2);
8392 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8393 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008394
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008395 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8396 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8397 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008398}
8399
8400/* WaMPhyProgramming:hsw */
8401static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8402{
8403 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008404
8405 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8406 tmp &= ~(0xFF << 24);
8407 tmp |= (0x12 << 24);
8408 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8409
Paulo Zanonidde86e22012-12-01 12:04:25 -02008410 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8411 tmp |= (1 << 11);
8412 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8413
8414 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8415 tmp |= (1 << 11);
8416 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8417
Paulo Zanonidde86e22012-12-01 12:04:25 -02008418 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8419 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8420 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8421
8422 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8423 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8424 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8425
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008426 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8427 tmp &= ~(7 << 13);
8428 tmp |= (5 << 13);
8429 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008430
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008431 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8432 tmp &= ~(7 << 13);
8433 tmp |= (5 << 13);
8434 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008435
8436 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8437 tmp &= ~0xFF;
8438 tmp |= 0x1C;
8439 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8440
8441 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8442 tmp &= ~0xFF;
8443 tmp |= 0x1C;
8444 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8445
8446 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8447 tmp &= ~(0xFF << 16);
8448 tmp |= (0x1C << 16);
8449 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8450
8451 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8452 tmp &= ~(0xFF << 16);
8453 tmp |= (0x1C << 16);
8454 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8455
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008456 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8457 tmp |= (1 << 27);
8458 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008459
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008460 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8461 tmp |= (1 << 27);
8462 intel_sbi_write(dev_priv, 0x21C4, 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, 0x20EC, SBI_MPHY);
8465 tmp &= ~(0xF << 28);
8466 tmp |= (4 << 28);
8467 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008468
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008469 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8470 tmp &= ~(0xF << 28);
8471 tmp |= (4 << 28);
8472 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008473}
8474
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008475/* Implements 3 different sequences from BSpec chapter "Display iCLK
8476 * Programming" based on the parameters passed:
8477 * - Sequence to enable CLKOUT_DP
8478 * - Sequence to enable CLKOUT_DP without spread
8479 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8480 */
8481static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8482 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008483{
8484 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008485 uint32_t reg, tmp;
8486
8487 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8488 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008489 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008490 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008491
Ville Syrjäläa5805162015-05-26 20:42:30 +03008492 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008493
8494 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8495 tmp &= ~SBI_SSCCTL_DISABLE;
8496 tmp |= SBI_SSCCTL_PATHALT;
8497 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8498
8499 udelay(24);
8500
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008501 if (with_spread) {
8502 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8503 tmp &= ~SBI_SSCCTL_PATHALT;
8504 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008505
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008506 if (with_fdi) {
8507 lpt_reset_fdi_mphy(dev_priv);
8508 lpt_program_fdi_mphy(dev_priv);
8509 }
8510 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008511
Ville Syrjäläc2699522015-08-27 23:55:59 +03008512 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008513 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8514 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8515 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008516
Ville Syrjäläa5805162015-05-26 20:42:30 +03008517 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008518}
8519
Paulo Zanoni47701c32013-07-23 11:19:25 -03008520/* Sequence to disable CLKOUT_DP */
8521static void lpt_disable_clkout_dp(struct drm_device *dev)
8522{
8523 struct drm_i915_private *dev_priv = dev->dev_private;
8524 uint32_t reg, tmp;
8525
Ville Syrjäläa5805162015-05-26 20:42:30 +03008526 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008527
Ville Syrjäläc2699522015-08-27 23:55:59 +03008528 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008529 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8530 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8531 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8532
8533 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8534 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8535 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8536 tmp |= SBI_SSCCTL_PATHALT;
8537 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8538 udelay(32);
8539 }
8540 tmp |= SBI_SSCCTL_DISABLE;
8541 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8542 }
8543
Ville Syrjäläa5805162015-05-26 20:42:30 +03008544 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008545}
8546
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008547#define BEND_IDX(steps) ((50 + (steps)) / 5)
8548
8549static const uint16_t sscdivintphase[] = {
8550 [BEND_IDX( 50)] = 0x3B23,
8551 [BEND_IDX( 45)] = 0x3B23,
8552 [BEND_IDX( 40)] = 0x3C23,
8553 [BEND_IDX( 35)] = 0x3C23,
8554 [BEND_IDX( 30)] = 0x3D23,
8555 [BEND_IDX( 25)] = 0x3D23,
8556 [BEND_IDX( 20)] = 0x3E23,
8557 [BEND_IDX( 15)] = 0x3E23,
8558 [BEND_IDX( 10)] = 0x3F23,
8559 [BEND_IDX( 5)] = 0x3F23,
8560 [BEND_IDX( 0)] = 0x0025,
8561 [BEND_IDX( -5)] = 0x0025,
8562 [BEND_IDX(-10)] = 0x0125,
8563 [BEND_IDX(-15)] = 0x0125,
8564 [BEND_IDX(-20)] = 0x0225,
8565 [BEND_IDX(-25)] = 0x0225,
8566 [BEND_IDX(-30)] = 0x0325,
8567 [BEND_IDX(-35)] = 0x0325,
8568 [BEND_IDX(-40)] = 0x0425,
8569 [BEND_IDX(-45)] = 0x0425,
8570 [BEND_IDX(-50)] = 0x0525,
8571};
8572
8573/*
8574 * Bend CLKOUT_DP
8575 * steps -50 to 50 inclusive, in steps of 5
8576 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8577 * change in clock period = -(steps / 10) * 5.787 ps
8578 */
8579static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8580{
8581 uint32_t tmp;
8582 int idx = BEND_IDX(steps);
8583
8584 if (WARN_ON(steps % 5 != 0))
8585 return;
8586
8587 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8588 return;
8589
8590 mutex_lock(&dev_priv->sb_lock);
8591
8592 if (steps % 10 != 0)
8593 tmp = 0xAAAAAAAB;
8594 else
8595 tmp = 0x00000000;
8596 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8597
8598 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8599 tmp &= 0xffff0000;
8600 tmp |= sscdivintphase[idx];
8601 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8602
8603 mutex_unlock(&dev_priv->sb_lock);
8604}
8605
8606#undef BEND_IDX
8607
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008608static void lpt_init_pch_refclk(struct drm_device *dev)
8609{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008610 struct intel_encoder *encoder;
8611 bool has_vga = false;
8612
Damien Lespiaub2784e12014-08-05 11:29:37 +01008613 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008614 switch (encoder->type) {
8615 case INTEL_OUTPUT_ANALOG:
8616 has_vga = true;
8617 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008618 default:
8619 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008620 }
8621 }
8622
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008623 if (has_vga) {
8624 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008625 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008626 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008627 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008628 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008629}
8630
Paulo Zanonidde86e22012-12-01 12:04:25 -02008631/*
8632 * Initialize reference clocks when the driver loads
8633 */
8634void intel_init_pch_refclk(struct drm_device *dev)
8635{
8636 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8637 ironlake_init_pch_refclk(dev);
8638 else if (HAS_PCH_LPT(dev))
8639 lpt_init_pch_refclk(dev);
8640}
8641
Daniel Vetter6ff93602013-04-19 11:24:36 +02008642static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008643{
8644 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8645 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8646 int pipe = intel_crtc->pipe;
8647 uint32_t val;
8648
Daniel Vetter78114072013-06-13 00:54:57 +02008649 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008650
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008651 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008652 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008653 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008654 break;
8655 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008656 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008657 break;
8658 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008659 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008660 break;
8661 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008662 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008663 break;
8664 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008665 /* Case prevented by intel_choose_pipe_bpp_dither. */
8666 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008667 }
8668
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008669 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008670 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8671
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008672 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008673 val |= PIPECONF_INTERLACED_ILK;
8674 else
8675 val |= PIPECONF_PROGRESSIVE;
8676
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008677 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008678 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008679
Paulo Zanonic8203562012-09-12 10:06:29 -03008680 I915_WRITE(PIPECONF(pipe), val);
8681 POSTING_READ(PIPECONF(pipe));
8682}
8683
Daniel Vetter6ff93602013-04-19 11:24:36 +02008684static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008685{
Jani Nikula391bf042016-03-18 17:05:40 +02008686 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008687 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008688 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008689 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008690
Jani Nikula391bf042016-03-18 17:05:40 +02008691 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008692 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8693
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008694 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008695 val |= PIPECONF_INTERLACED_ILK;
8696 else
8697 val |= PIPECONF_PROGRESSIVE;
8698
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008699 I915_WRITE(PIPECONF(cpu_transcoder), val);
8700 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008701}
8702
Jani Nikula391bf042016-03-18 17:05:40 +02008703static void haswell_set_pipemisc(struct drm_crtc *crtc)
8704{
8705 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8706 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8707
8708 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8709 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008710
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008711 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008712 case 18:
8713 val |= PIPEMISC_DITHER_6_BPC;
8714 break;
8715 case 24:
8716 val |= PIPEMISC_DITHER_8_BPC;
8717 break;
8718 case 30:
8719 val |= PIPEMISC_DITHER_10_BPC;
8720 break;
8721 case 36:
8722 val |= PIPEMISC_DITHER_12_BPC;
8723 break;
8724 default:
8725 /* Case prevented by pipe_config_set_bpp. */
8726 BUG();
8727 }
8728
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008729 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008730 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8731
Jani Nikula391bf042016-03-18 17:05:40 +02008732 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008733 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008734}
8735
Paulo Zanonid4b19312012-11-29 11:29:32 -02008736int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8737{
8738 /*
8739 * Account for spread spectrum to avoid
8740 * oversubscribing the link. Max center spread
8741 * is 2.5%; use 5% for safety's sake.
8742 */
8743 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008744 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008745}
8746
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008747static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008748{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008749 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008750}
8751
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008752static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8753 struct intel_crtc_state *crtc_state,
8754 intel_clock_t *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008755{
8756 struct drm_crtc *crtc = &intel_crtc->base;
8757 struct drm_device *dev = crtc->dev;
8758 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008759 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008760 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008761 struct drm_connector_state *connector_state;
8762 struct intel_encoder *encoder;
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008763 u32 dpll, fp, fp2;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008764 int factor, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008765 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008766
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008767 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008768 if (connector_state->crtc != crtc_state->base.crtc)
8769 continue;
8770
8771 encoder = to_intel_encoder(connector_state->best_encoder);
8772
8773 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008774 case INTEL_OUTPUT_LVDS:
8775 is_lvds = true;
8776 break;
8777 case INTEL_OUTPUT_SDVO:
8778 case INTEL_OUTPUT_HDMI:
8779 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008780 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008781 default:
8782 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008783 }
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008784 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008785
Chris Wilsonc1858122010-12-03 21:35:48 +00008786 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008787 factor = 21;
8788 if (is_lvds) {
8789 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008790 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008791 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008792 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008793 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008794 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008795
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008796 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008797
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008798 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8799 fp |= FP_CB_TUNE;
8800
8801 if (reduced_clock) {
8802 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8803
8804 if (reduced_clock->m < factor * reduced_clock->n)
8805 fp2 |= FP_CB_TUNE;
8806 } else {
8807 fp2 = fp;
8808 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008809
Chris Wilson5eddb702010-09-11 13:48:45 +01008810 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008811
Eric Anholta07d6782011-03-30 13:01:08 -07008812 if (is_lvds)
8813 dpll |= DPLLB_MODE_LVDS;
8814 else
8815 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008816
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008817 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008818 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008819
8820 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008821 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008822 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008823 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008824
Eric Anholta07d6782011-03-30 13:01:08 -07008825 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008826 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008827 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008828 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008829
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008830 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008831 case 5:
8832 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8833 break;
8834 case 7:
8835 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8836 break;
8837 case 10:
8838 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8839 break;
8840 case 14:
8841 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8842 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008843 }
8844
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008845 if (is_lvds && intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008846 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008847 else
8848 dpll |= PLL_REF_INPUT_DREFCLK;
8849
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008850 dpll |= DPLL_VCO_ENABLE;
8851
8852 crtc_state->dpll_hw_state.dpll = dpll;
8853 crtc_state->dpll_hw_state.fp0 = fp;
8854 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008855}
8856
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008857static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8858 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008859{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008860 struct drm_device *dev = crtc->base.dev;
8861 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008862 intel_clock_t reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008863 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008864 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008865 const intel_limit_t *limit;
8866 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008867
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008868 memset(&crtc_state->dpll_hw_state, 0,
8869 sizeof(crtc_state->dpll_hw_state));
8870
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008871 crtc->lowfreq_avail = false;
8872
8873 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8874 if (!crtc_state->has_pch_encoder)
8875 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008876
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008877 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8878 if (intel_panel_use_ssc(dev_priv)) {
8879 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8880 dev_priv->vbt.lvds_ssc_freq);
8881 refclk = dev_priv->vbt.lvds_ssc_freq;
8882 }
8883
8884 if (intel_is_dual_link_lvds(dev)) {
8885 if (refclk == 100000)
8886 limit = &intel_limits_ironlake_dual_lvds_100m;
8887 else
8888 limit = &intel_limits_ironlake_dual_lvds;
8889 } else {
8890 if (refclk == 100000)
8891 limit = &intel_limits_ironlake_single_lvds_100m;
8892 else
8893 limit = &intel_limits_ironlake_single_lvds;
8894 }
8895 } else {
8896 limit = &intel_limits_ironlake_dac;
8897 }
8898
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008899 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008900 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8901 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008902 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8903 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008904 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008905
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008906 ironlake_compute_dpll(crtc, crtc_state,
8907 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008908
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008909 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8910 if (pll == NULL) {
8911 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8912 pipe_name(crtc->pipe));
8913 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008914 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008915
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008916 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8917 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008918 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02008919
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008920 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008921}
8922
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008923static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8924 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008925{
8926 struct drm_device *dev = crtc->base.dev;
8927 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008928 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008929
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008930 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8931 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8932 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8933 & ~TU_SIZE_MASK;
8934 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8935 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8936 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8937}
8938
8939static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8940 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008941 struct intel_link_m_n *m_n,
8942 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008943{
8944 struct drm_device *dev = crtc->base.dev;
8945 struct drm_i915_private *dev_priv = dev->dev_private;
8946 enum pipe pipe = crtc->pipe;
8947
8948 if (INTEL_INFO(dev)->gen >= 5) {
8949 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8950 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8951 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8952 & ~TU_SIZE_MASK;
8953 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8954 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8955 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008956 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8957 * gen < 8) and if DRRS is supported (to make sure the
8958 * registers are not unnecessarily read).
8959 */
8960 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008961 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008962 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8963 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8964 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8965 & ~TU_SIZE_MASK;
8966 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8967 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8968 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8969 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008970 } else {
8971 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8972 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8973 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8974 & ~TU_SIZE_MASK;
8975 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8976 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8977 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8978 }
8979}
8980
8981void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008982 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008983{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008984 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008985 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8986 else
8987 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008988 &pipe_config->dp_m_n,
8989 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008990}
8991
Daniel Vetter72419202013-04-04 13:28:53 +02008992static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008993 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008994{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008995 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008996 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008997}
8998
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008999static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009000 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009001{
9002 struct drm_device *dev = crtc->base.dev;
9003 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009004 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9005 uint32_t ps_ctrl = 0;
9006 int id = -1;
9007 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009008
Chandra Kondurua1b22782015-04-07 15:28:45 -07009009 /* find scaler attached to this pipe */
9010 for (i = 0; i < crtc->num_scalers; i++) {
9011 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9012 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9013 id = i;
9014 pipe_config->pch_pfit.enabled = true;
9015 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9016 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9017 break;
9018 }
9019 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009020
Chandra Kondurua1b22782015-04-07 15:28:45 -07009021 scaler_state->scaler_id = id;
9022 if (id >= 0) {
9023 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9024 } else {
9025 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009026 }
9027}
9028
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009029static void
9030skylake_get_initial_plane_config(struct intel_crtc *crtc,
9031 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009032{
9033 struct drm_device *dev = crtc->base.dev;
9034 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009035 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009036 int pipe = crtc->pipe;
9037 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009038 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009039 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009040 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009041
Damien Lespiaud9806c92015-01-21 14:07:19 +00009042 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009043 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009044 DRM_DEBUG_KMS("failed to alloc fb\n");
9045 return;
9046 }
9047
Damien Lespiau1b842c82015-01-21 13:50:54 +00009048 fb = &intel_fb->base;
9049
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009050 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009051 if (!(val & PLANE_CTL_ENABLE))
9052 goto error;
9053
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009054 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9055 fourcc = skl_format_to_fourcc(pixel_format,
9056 val & PLANE_CTL_ORDER_RGBX,
9057 val & PLANE_CTL_ALPHA_MASK);
9058 fb->pixel_format = fourcc;
9059 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9060
Damien Lespiau40f46282015-02-27 11:15:21 +00009061 tiling = val & PLANE_CTL_TILED_MASK;
9062 switch (tiling) {
9063 case PLANE_CTL_TILED_LINEAR:
9064 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9065 break;
9066 case PLANE_CTL_TILED_X:
9067 plane_config->tiling = I915_TILING_X;
9068 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9069 break;
9070 case PLANE_CTL_TILED_Y:
9071 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9072 break;
9073 case PLANE_CTL_TILED_YF:
9074 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9075 break;
9076 default:
9077 MISSING_CASE(tiling);
9078 goto error;
9079 }
9080
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009081 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9082 plane_config->base = base;
9083
9084 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9085
9086 val = I915_READ(PLANE_SIZE(pipe, 0));
9087 fb->height = ((val >> 16) & 0xfff) + 1;
9088 fb->width = ((val >> 0) & 0x1fff) + 1;
9089
9090 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009091 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009092 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009093 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9094
9095 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009096 fb->pixel_format,
9097 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009098
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009099 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009100
9101 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9102 pipe_name(pipe), fb->width, fb->height,
9103 fb->bits_per_pixel, base, fb->pitches[0],
9104 plane_config->size);
9105
Damien Lespiau2d140302015-02-05 17:22:18 +00009106 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009107 return;
9108
9109error:
9110 kfree(fb);
9111}
9112
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009113static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009114 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009115{
9116 struct drm_device *dev = crtc->base.dev;
9117 struct drm_i915_private *dev_priv = dev->dev_private;
9118 uint32_t tmp;
9119
9120 tmp = I915_READ(PF_CTL(crtc->pipe));
9121
9122 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009123 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009124 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9125 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009126
9127 /* We currently do not free assignements of panel fitters on
9128 * ivb/hsw (since we don't use the higher upscaling modes which
9129 * differentiates them) so just WARN about this case for now. */
9130 if (IS_GEN7(dev)) {
9131 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9132 PF_PIPE_SEL_IVB(crtc->pipe));
9133 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009134 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009135}
9136
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009137static void
9138ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9139 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009140{
9141 struct drm_device *dev = crtc->base.dev;
9142 struct drm_i915_private *dev_priv = dev->dev_private;
9143 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009144 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009145 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009146 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009147 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009148 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009149
Damien Lespiau42a7b082015-02-05 19:35:13 +00009150 val = I915_READ(DSPCNTR(pipe));
9151 if (!(val & DISPLAY_PLANE_ENABLE))
9152 return;
9153
Damien Lespiaud9806c92015-01-21 14:07:19 +00009154 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009155 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009156 DRM_DEBUG_KMS("failed to alloc fb\n");
9157 return;
9158 }
9159
Damien Lespiau1b842c82015-01-21 13:50:54 +00009160 fb = &intel_fb->base;
9161
Daniel Vetter18c52472015-02-10 17:16:09 +00009162 if (INTEL_INFO(dev)->gen >= 4) {
9163 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009164 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009165 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9166 }
9167 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009168
9169 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009170 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009171 fb->pixel_format = fourcc;
9172 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009173
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009174 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009175 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009176 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009177 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009178 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009179 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009180 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009181 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009182 }
9183 plane_config->base = base;
9184
9185 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009186 fb->width = ((val >> 16) & 0xfff) + 1;
9187 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009188
9189 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009190 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009191
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009192 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009193 fb->pixel_format,
9194 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009195
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009196 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009197
Damien Lespiau2844a922015-01-20 12:51:48 +00009198 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9199 pipe_name(pipe), fb->width, fb->height,
9200 fb->bits_per_pixel, base, fb->pitches[0],
9201 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009202
Damien Lespiau2d140302015-02-05 17:22:18 +00009203 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009204}
9205
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009206static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009207 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009208{
9209 struct drm_device *dev = crtc->base.dev;
9210 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009211 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009212 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009213 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009214
Imre Deak17290502016-02-12 18:55:11 +02009215 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9216 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009217 return false;
9218
Daniel Vettere143a212013-07-04 12:01:15 +02009219 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009220 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009221
Imre Deak17290502016-02-12 18:55:11 +02009222 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009223 tmp = I915_READ(PIPECONF(crtc->pipe));
9224 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009225 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009226
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009227 switch (tmp & PIPECONF_BPC_MASK) {
9228 case PIPECONF_6BPC:
9229 pipe_config->pipe_bpp = 18;
9230 break;
9231 case PIPECONF_8BPC:
9232 pipe_config->pipe_bpp = 24;
9233 break;
9234 case PIPECONF_10BPC:
9235 pipe_config->pipe_bpp = 30;
9236 break;
9237 case PIPECONF_12BPC:
9238 pipe_config->pipe_bpp = 36;
9239 break;
9240 default:
9241 break;
9242 }
9243
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009244 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9245 pipe_config->limited_color_range = true;
9246
Daniel Vetterab9412b2013-05-03 11:49:46 +02009247 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009248 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009249 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009250
Daniel Vetter88adfff2013-03-28 10:42:01 +01009251 pipe_config->has_pch_encoder = true;
9252
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009253 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9254 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9255 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009256
9257 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009258
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009259 if (HAS_PCH_IBX(dev_priv->dev)) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009260 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009261 } else {
9262 tmp = I915_READ(PCH_DPLL_SEL);
9263 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009264 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009265 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009266 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009267 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009268
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009269 pipe_config->shared_dpll =
9270 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9271 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009272
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009273 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9274 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009275
9276 tmp = pipe_config->dpll_hw_state.dpll;
9277 pipe_config->pixel_multiplier =
9278 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9279 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009280
9281 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009282 } else {
9283 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009284 }
9285
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009286 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009287 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009288
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009289 ironlake_get_pfit_config(crtc, pipe_config);
9290
Imre Deak17290502016-02-12 18:55:11 +02009291 ret = true;
9292
9293out:
9294 intel_display_power_put(dev_priv, power_domain);
9295
9296 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009297}
9298
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009299static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9300{
9301 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009302 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009303
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009304 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009305 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009306 pipe_name(crtc->pipe));
9307
Rob Clarke2c719b2014-12-15 13:56:32 -05009308 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9309 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009310 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9311 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009312 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9313 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009314 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009315 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009316 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009317 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009318 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009319 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009320 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009321 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009322 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009323
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009324 /*
9325 * In theory we can still leave IRQs enabled, as long as only the HPD
9326 * interrupts remain enabled. We used to check for that, but since it's
9327 * gen-specific and since we only disable LCPLL after we fully disable
9328 * the interrupts, the check below should be enough.
9329 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009330 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009331}
9332
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009333static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9334{
9335 struct drm_device *dev = dev_priv->dev;
9336
9337 if (IS_HASWELL(dev))
9338 return I915_READ(D_COMP_HSW);
9339 else
9340 return I915_READ(D_COMP_BDW);
9341}
9342
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009343static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9344{
9345 struct drm_device *dev = dev_priv->dev;
9346
9347 if (IS_HASWELL(dev)) {
9348 mutex_lock(&dev_priv->rps.hw_lock);
9349 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9350 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009351 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009352 mutex_unlock(&dev_priv->rps.hw_lock);
9353 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009354 I915_WRITE(D_COMP_BDW, val);
9355 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009356 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009357}
9358
9359/*
9360 * This function implements pieces of two sequences from BSpec:
9361 * - Sequence for display software to disable LCPLL
9362 * - Sequence for display software to allow package C8+
9363 * The steps implemented here are just the steps that actually touch the LCPLL
9364 * register. Callers should take care of disabling all the display engine
9365 * functions, doing the mode unset, fixing interrupts, etc.
9366 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009367static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9368 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009369{
9370 uint32_t val;
9371
9372 assert_can_disable_lcpll(dev_priv);
9373
9374 val = I915_READ(LCPLL_CTL);
9375
9376 if (switch_to_fclk) {
9377 val |= LCPLL_CD_SOURCE_FCLK;
9378 I915_WRITE(LCPLL_CTL, val);
9379
9380 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9381 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9382 DRM_ERROR("Switching to FCLK failed\n");
9383
9384 val = I915_READ(LCPLL_CTL);
9385 }
9386
9387 val |= LCPLL_PLL_DISABLE;
9388 I915_WRITE(LCPLL_CTL, val);
9389 POSTING_READ(LCPLL_CTL);
9390
9391 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9392 DRM_ERROR("LCPLL still locked\n");
9393
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009394 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009395 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009396 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009397 ndelay(100);
9398
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009399 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9400 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009401 DRM_ERROR("D_COMP RCOMP still in progress\n");
9402
9403 if (allow_power_down) {
9404 val = I915_READ(LCPLL_CTL);
9405 val |= LCPLL_POWER_DOWN_ALLOW;
9406 I915_WRITE(LCPLL_CTL, val);
9407 POSTING_READ(LCPLL_CTL);
9408 }
9409}
9410
9411/*
9412 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9413 * source.
9414 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009415static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009416{
9417 uint32_t val;
9418
9419 val = I915_READ(LCPLL_CTL);
9420
9421 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9422 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9423 return;
9424
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009425 /*
9426 * Make sure we're not on PC8 state before disabling PC8, otherwise
9427 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009428 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009429 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009430
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009431 if (val & LCPLL_POWER_DOWN_ALLOW) {
9432 val &= ~LCPLL_POWER_DOWN_ALLOW;
9433 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009434 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009435 }
9436
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009437 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009438 val |= D_COMP_COMP_FORCE;
9439 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009440 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009441
9442 val = I915_READ(LCPLL_CTL);
9443 val &= ~LCPLL_PLL_DISABLE;
9444 I915_WRITE(LCPLL_CTL, val);
9445
9446 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9447 DRM_ERROR("LCPLL not locked yet\n");
9448
9449 if (val & LCPLL_CD_SOURCE_FCLK) {
9450 val = I915_READ(LCPLL_CTL);
9451 val &= ~LCPLL_CD_SOURCE_FCLK;
9452 I915_WRITE(LCPLL_CTL, val);
9453
9454 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9455 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9456 DRM_ERROR("Switching back to LCPLL failed\n");
9457 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009458
Mika Kuoppala59bad942015-01-16 11:34:40 +02009459 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009460 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009461}
9462
Paulo Zanoni765dab672014-03-07 20:08:18 -03009463/*
9464 * Package states C8 and deeper are really deep PC states that can only be
9465 * reached when all the devices on the system allow it, so even if the graphics
9466 * device allows PC8+, it doesn't mean the system will actually get to these
9467 * states. Our driver only allows PC8+ when going into runtime PM.
9468 *
9469 * The requirements for PC8+ are that all the outputs are disabled, the power
9470 * well is disabled and most interrupts are disabled, and these are also
9471 * requirements for runtime PM. When these conditions are met, we manually do
9472 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9473 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9474 * hang the machine.
9475 *
9476 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9477 * the state of some registers, so when we come back from PC8+ we need to
9478 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9479 * need to take care of the registers kept by RC6. Notice that this happens even
9480 * if we don't put the device in PCI D3 state (which is what currently happens
9481 * because of the runtime PM support).
9482 *
9483 * For more, read "Display Sequences for Package C8" on the hardware
9484 * documentation.
9485 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009486void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009487{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009488 struct drm_device *dev = dev_priv->dev;
9489 uint32_t val;
9490
Paulo Zanonic67a4702013-08-19 13:18:09 -03009491 DRM_DEBUG_KMS("Enabling package C8+\n");
9492
Ville Syrjäläc2699522015-08-27 23:55:59 +03009493 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009494 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9495 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9496 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9497 }
9498
9499 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009500 hsw_disable_lcpll(dev_priv, true, true);
9501}
9502
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009503void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009504{
9505 struct drm_device *dev = dev_priv->dev;
9506 uint32_t val;
9507
Paulo Zanonic67a4702013-08-19 13:18:09 -03009508 DRM_DEBUG_KMS("Disabling package C8+\n");
9509
9510 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009511 lpt_init_pch_refclk(dev);
9512
Ville Syrjäläc2699522015-08-27 23:55:59 +03009513 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009514 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9515 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9516 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9517 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009518}
9519
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009520static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309521{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009522 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009523 struct intel_atomic_state *old_intel_state =
9524 to_intel_atomic_state(old_state);
9525 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309526
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009527 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309528}
9529
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009530/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009531static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009532{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009533 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9534 struct drm_i915_private *dev_priv = state->dev->dev_private;
9535 struct drm_crtc *crtc;
9536 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009537 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009538 unsigned max_pixel_rate = 0, i;
9539 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009540
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009541 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9542 sizeof(intel_state->min_pixclk));
9543
9544 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009545 int pixel_rate;
9546
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009547 crtc_state = to_intel_crtc_state(cstate);
9548 if (!crtc_state->base.enable) {
9549 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009550 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009551 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009552
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009553 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009554
9555 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009556 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009557 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9558
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009559 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009560 }
9561
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009562 for_each_pipe(dev_priv, pipe)
9563 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9564
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009565 return max_pixel_rate;
9566}
9567
9568static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9569{
9570 struct drm_i915_private *dev_priv = dev->dev_private;
9571 uint32_t val, data;
9572 int ret;
9573
9574 if (WARN((I915_READ(LCPLL_CTL) &
9575 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9576 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9577 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9578 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9579 "trying to change cdclk frequency with cdclk not enabled\n"))
9580 return;
9581
9582 mutex_lock(&dev_priv->rps.hw_lock);
9583 ret = sandybridge_pcode_write(dev_priv,
9584 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9585 mutex_unlock(&dev_priv->rps.hw_lock);
9586 if (ret) {
9587 DRM_ERROR("failed to inform pcode about cdclk change\n");
9588 return;
9589 }
9590
9591 val = I915_READ(LCPLL_CTL);
9592 val |= LCPLL_CD_SOURCE_FCLK;
9593 I915_WRITE(LCPLL_CTL, val);
9594
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009595 if (wait_for_us(I915_READ(LCPLL_CTL) &
9596 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009597 DRM_ERROR("Switching to FCLK failed\n");
9598
9599 val = I915_READ(LCPLL_CTL);
9600 val &= ~LCPLL_CLK_FREQ_MASK;
9601
9602 switch (cdclk) {
9603 case 450000:
9604 val |= LCPLL_CLK_FREQ_450;
9605 data = 0;
9606 break;
9607 case 540000:
9608 val |= LCPLL_CLK_FREQ_54O_BDW;
9609 data = 1;
9610 break;
9611 case 337500:
9612 val |= LCPLL_CLK_FREQ_337_5_BDW;
9613 data = 2;
9614 break;
9615 case 675000:
9616 val |= LCPLL_CLK_FREQ_675_BDW;
9617 data = 3;
9618 break;
9619 default:
9620 WARN(1, "invalid cdclk frequency\n");
9621 return;
9622 }
9623
9624 I915_WRITE(LCPLL_CTL, val);
9625
9626 val = I915_READ(LCPLL_CTL);
9627 val &= ~LCPLL_CD_SOURCE_FCLK;
9628 I915_WRITE(LCPLL_CTL, val);
9629
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009630 if (wait_for_us((I915_READ(LCPLL_CTL) &
9631 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009632 DRM_ERROR("Switching back to LCPLL failed\n");
9633
9634 mutex_lock(&dev_priv->rps.hw_lock);
9635 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9636 mutex_unlock(&dev_priv->rps.hw_lock);
9637
9638 intel_update_cdclk(dev);
9639
9640 WARN(cdclk != dev_priv->cdclk_freq,
9641 "cdclk requested %d kHz but got %d kHz\n",
9642 cdclk, dev_priv->cdclk_freq);
9643}
9644
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009645static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009646{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009647 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009648 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009649 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009650 int cdclk;
9651
9652 /*
9653 * FIXME should also account for plane ratio
9654 * once 64bpp pixel formats are supported.
9655 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009656 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009657 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009658 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009659 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009660 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009661 cdclk = 450000;
9662 else
9663 cdclk = 337500;
9664
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009665 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009666 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9667 cdclk, dev_priv->max_cdclk_freq);
9668 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009669 }
9670
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009671 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9672 if (!intel_state->active_crtcs)
9673 intel_state->dev_cdclk = 337500;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009674
9675 return 0;
9676}
9677
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009678static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009679{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009680 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009681 struct intel_atomic_state *old_intel_state =
9682 to_intel_atomic_state(old_state);
9683 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009684
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009685 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009686}
9687
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009688static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9689 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009690{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009691 struct intel_encoder *intel_encoder =
9692 intel_ddi_get_crtc_new_encoder(crtc_state);
9693
9694 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9695 if (!intel_ddi_pll_select(crtc, crtc_state))
9696 return -EINVAL;
9697 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009698
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009699 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009700
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009701 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009702}
9703
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309704static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9705 enum port port,
9706 struct intel_crtc_state *pipe_config)
9707{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009708 enum intel_dpll_id id;
9709
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309710 switch (port) {
9711 case PORT_A:
9712 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009713 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309714 break;
9715 case PORT_B:
9716 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009717 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309718 break;
9719 case PORT_C:
9720 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009721 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309722 break;
9723 default:
9724 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009725 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309726 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009727
9728 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309729}
9730
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009731static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9732 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009733 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009734{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009735 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009736 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009737
9738 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9739 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9740
9741 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009742 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009743 id = DPLL_ID_SKL_DPLL0;
9744 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009745 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009746 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009747 break;
9748 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009749 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009750 break;
9751 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009752 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009753 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009754 default:
9755 MISSING_CASE(pipe_config->ddi_pll_sel);
9756 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009757 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009758
9759 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009760}
9761
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009762static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9763 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009764 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009765{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009766 enum intel_dpll_id id;
9767
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009768 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9769
9770 switch (pipe_config->ddi_pll_sel) {
9771 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009772 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009773 break;
9774 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009775 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009776 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009777 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009778 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009779 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009780 case PORT_CLK_SEL_LCPLL_810:
9781 id = DPLL_ID_LCPLL_810;
9782 break;
9783 case PORT_CLK_SEL_LCPLL_1350:
9784 id = DPLL_ID_LCPLL_1350;
9785 break;
9786 case PORT_CLK_SEL_LCPLL_2700:
9787 id = DPLL_ID_LCPLL_2700;
9788 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009789 default:
9790 MISSING_CASE(pipe_config->ddi_pll_sel);
9791 /* fall through */
9792 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009793 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009794 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009795
9796 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009797}
9798
Jani Nikulacf304292016-03-18 17:05:41 +02009799static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9800 struct intel_crtc_state *pipe_config,
9801 unsigned long *power_domain_mask)
9802{
9803 struct drm_device *dev = crtc->base.dev;
9804 struct drm_i915_private *dev_priv = dev->dev_private;
9805 enum intel_display_power_domain power_domain;
9806 u32 tmp;
9807
9808 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9809
9810 /*
9811 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9812 * consistency and less surprising code; it's in always on power).
9813 */
9814 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9815 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9816 enum pipe trans_edp_pipe;
9817 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9818 default:
9819 WARN(1, "unknown pipe linked to edp transcoder\n");
9820 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9821 case TRANS_DDI_EDP_INPUT_A_ON:
9822 trans_edp_pipe = PIPE_A;
9823 break;
9824 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9825 trans_edp_pipe = PIPE_B;
9826 break;
9827 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9828 trans_edp_pipe = PIPE_C;
9829 break;
9830 }
9831
9832 if (trans_edp_pipe == crtc->pipe)
9833 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9834 }
9835
9836 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9837 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9838 return false;
9839 *power_domain_mask |= BIT(power_domain);
9840
9841 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9842
9843 return tmp & PIPECONF_ENABLE;
9844}
9845
Jani Nikula4d1de972016-03-18 17:05:42 +02009846static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9847 struct intel_crtc_state *pipe_config,
9848 unsigned long *power_domain_mask)
9849{
9850 struct drm_device *dev = crtc->base.dev;
9851 struct drm_i915_private *dev_priv = dev->dev_private;
9852 enum intel_display_power_domain power_domain;
9853 enum port port;
9854 enum transcoder cpu_transcoder;
9855 u32 tmp;
9856
9857 pipe_config->has_dsi_encoder = false;
9858
9859 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9860 if (port == PORT_A)
9861 cpu_transcoder = TRANSCODER_DSI_A;
9862 else
9863 cpu_transcoder = TRANSCODER_DSI_C;
9864
9865 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9866 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9867 continue;
9868 *power_domain_mask |= BIT(power_domain);
9869
Imre Deakdb18b6a2016-03-24 12:41:40 +02009870 /*
9871 * The PLL needs to be enabled with a valid divider
9872 * configuration, otherwise accessing DSI registers will hang
9873 * the machine. See BSpec North Display Engine
9874 * registers/MIPI[BXT]. We can break out here early, since we
9875 * need the same DSI PLL to be enabled for both DSI ports.
9876 */
9877 if (!intel_dsi_pll_is_enabled(dev_priv))
9878 break;
9879
Jani Nikula4d1de972016-03-18 17:05:42 +02009880 /* XXX: this works for video mode only */
9881 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9882 if (!(tmp & DPI_ENABLE))
9883 continue;
9884
9885 tmp = I915_READ(MIPI_CTRL(port));
9886 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9887 continue;
9888
9889 pipe_config->cpu_transcoder = cpu_transcoder;
9890 pipe_config->has_dsi_encoder = true;
9891 break;
9892 }
9893
9894 return pipe_config->has_dsi_encoder;
9895}
9896
Daniel Vetter26804af2014-06-25 22:01:55 +03009897static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009898 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009899{
9900 struct drm_device *dev = crtc->base.dev;
9901 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009902 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009903 enum port port;
9904 uint32_t tmp;
9905
9906 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9907
9908 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9909
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07009910 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009911 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309912 else if (IS_BROXTON(dev))
9913 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009914 else
9915 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009916
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009917 pll = pipe_config->shared_dpll;
9918 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009919 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9920 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009921 }
9922
Daniel Vetter26804af2014-06-25 22:01:55 +03009923 /*
9924 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9925 * DDI E. So just check whether this pipe is wired to DDI E and whether
9926 * the PCH transcoder is on.
9927 */
Damien Lespiauca370452013-12-03 13:56:24 +00009928 if (INTEL_INFO(dev)->gen < 9 &&
9929 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009930 pipe_config->has_pch_encoder = true;
9931
9932 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9933 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9934 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9935
9936 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9937 }
9938}
9939
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009940static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009941 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009942{
9943 struct drm_device *dev = crtc->base.dev;
9944 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009945 enum intel_display_power_domain power_domain;
9946 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009947 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009948
Imre Deak17290502016-02-12 18:55:11 +02009949 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9950 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009951 return false;
Imre Deak17290502016-02-12 18:55:11 +02009952 power_domain_mask = BIT(power_domain);
9953
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009954 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009955
Jani Nikulacf304292016-03-18 17:05:41 +02009956 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +02009957
Jani Nikula4d1de972016-03-18 17:05:42 +02009958 if (IS_BROXTON(dev_priv)) {
9959 bxt_get_dsi_transcoder_state(crtc, pipe_config,
9960 &power_domain_mask);
9961 WARN_ON(active && pipe_config->has_dsi_encoder);
9962 if (pipe_config->has_dsi_encoder)
9963 active = true;
9964 }
9965
Jani Nikulacf304292016-03-18 17:05:41 +02009966 if (!active)
Imre Deak17290502016-02-12 18:55:11 +02009967 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009968
Jani Nikula4d1de972016-03-18 17:05:42 +02009969 if (!pipe_config->has_dsi_encoder) {
9970 haswell_get_ddi_port_state(crtc, pipe_config);
9971 intel_get_pipe_timings(crtc, pipe_config);
9972 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009973
Jani Nikulabc58be62016-03-18 17:05:39 +02009974 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009975
Lionel Landwerlin05dc6982016-03-16 10:57:15 +00009976 pipe_config->gamma_mode =
9977 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9978
Chandra Kondurua1b22782015-04-07 15:28:45 -07009979 if (INTEL_INFO(dev)->gen >= 9) {
9980 skl_init_scalers(dev, crtc, pipe_config);
9981 }
9982
Chandra Konduruaf99ceda2015-05-11 14:35:47 -07009983 if (INTEL_INFO(dev)->gen >= 9) {
9984 pipe_config->scaler_state.scaler_id = -1;
9985 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9986 }
9987
Imre Deak17290502016-02-12 18:55:11 +02009988 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9989 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
9990 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009991 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009992 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009993 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009994 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009995 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009996
Jesse Barnese59150d2014-01-07 13:30:45 -08009997 if (IS_HASWELL(dev))
9998 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9999 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010000
Jani Nikula4d1de972016-03-18 17:05:42 +020010001 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10002 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010003 pipe_config->pixel_multiplier =
10004 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10005 } else {
10006 pipe_config->pixel_multiplier = 1;
10007 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010008
Imre Deak17290502016-02-12 18:55:11 +020010009out:
10010 for_each_power_domain(power_domain, power_domain_mask)
10011 intel_display_power_put(dev_priv, power_domain);
10012
Jani Nikulacf304292016-03-18 17:05:41 +020010013 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010014}
10015
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010016static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10017 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010018{
10019 struct drm_device *dev = crtc->dev;
10020 struct drm_i915_private *dev_priv = dev->dev_private;
10021 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010022 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010023
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010024 if (plane_state && plane_state->visible) {
10025 unsigned int width = plane_state->base.crtc_w;
10026 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010027 unsigned int stride = roundup_pow_of_two(width) * 4;
10028
10029 switch (stride) {
10030 default:
10031 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10032 width, stride);
10033 stride = 256;
10034 /* fallthrough */
10035 case 256:
10036 case 512:
10037 case 1024:
10038 case 2048:
10039 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010040 }
10041
Ville Syrjälädc41c152014-08-13 11:57:05 +030010042 cntl |= CURSOR_ENABLE |
10043 CURSOR_GAMMA_ENABLE |
10044 CURSOR_FORMAT_ARGB |
10045 CURSOR_STRIDE(stride);
10046
10047 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010048 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010049
Ville Syrjälädc41c152014-08-13 11:57:05 +030010050 if (intel_crtc->cursor_cntl != 0 &&
10051 (intel_crtc->cursor_base != base ||
10052 intel_crtc->cursor_size != size ||
10053 intel_crtc->cursor_cntl != cntl)) {
10054 /* On these chipsets we can only modify the base/size/stride
10055 * whilst the cursor is disabled.
10056 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010057 I915_WRITE(CURCNTR(PIPE_A), 0);
10058 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010059 intel_crtc->cursor_cntl = 0;
10060 }
10061
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010062 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010063 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010064 intel_crtc->cursor_base = base;
10065 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010066
10067 if (intel_crtc->cursor_size != size) {
10068 I915_WRITE(CURSIZE, size);
10069 intel_crtc->cursor_size = size;
10070 }
10071
Chris Wilson4b0e3332014-05-30 16:35:26 +030010072 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010073 I915_WRITE(CURCNTR(PIPE_A), cntl);
10074 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010075 intel_crtc->cursor_cntl = cntl;
10076 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010077}
10078
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010079static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10080 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010081{
10082 struct drm_device *dev = crtc->dev;
10083 struct drm_i915_private *dev_priv = dev->dev_private;
10084 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10085 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010086 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010087
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010088 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010089 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010090 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010091 case 64:
10092 cntl |= CURSOR_MODE_64_ARGB_AX;
10093 break;
10094 case 128:
10095 cntl |= CURSOR_MODE_128_ARGB_AX;
10096 break;
10097 case 256:
10098 cntl |= CURSOR_MODE_256_ARGB_AX;
10099 break;
10100 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010101 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010102 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010103 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010104 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010105
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010106 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010107 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010108
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010109 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10110 cntl |= CURSOR_ROTATE_180;
10111 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010112
Chris Wilson4b0e3332014-05-30 16:35:26 +030010113 if (intel_crtc->cursor_cntl != cntl) {
10114 I915_WRITE(CURCNTR(pipe), cntl);
10115 POSTING_READ(CURCNTR(pipe));
10116 intel_crtc->cursor_cntl = cntl;
10117 }
10118
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010119 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010120 I915_WRITE(CURBASE(pipe), base);
10121 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010122
10123 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010124}
10125
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010126/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010127static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010128 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010129{
10130 struct drm_device *dev = crtc->dev;
10131 struct drm_i915_private *dev_priv = dev->dev_private;
10132 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10133 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010134 u32 base = intel_crtc->cursor_addr;
10135 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010136
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010137 if (plane_state) {
10138 int x = plane_state->base.crtc_x;
10139 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010140
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010141 if (x < 0) {
10142 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10143 x = -x;
10144 }
10145 pos |= x << CURSOR_X_SHIFT;
10146
10147 if (y < 0) {
10148 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10149 y = -y;
10150 }
10151 pos |= y << CURSOR_Y_SHIFT;
10152
10153 /* ILK+ do this automagically */
10154 if (HAS_GMCH_DISPLAY(dev) &&
10155 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10156 base += (plane_state->base.crtc_h *
10157 plane_state->base.crtc_w - 1) * 4;
10158 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010159 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010160
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010161 I915_WRITE(CURPOS(pipe), pos);
10162
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010163 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010164 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010165 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010166 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010167}
10168
Ville Syrjälädc41c152014-08-13 11:57:05 +030010169static bool cursor_size_ok(struct drm_device *dev,
10170 uint32_t width, uint32_t height)
10171{
10172 if (width == 0 || height == 0)
10173 return false;
10174
10175 /*
10176 * 845g/865g are special in that they are only limited by
10177 * the width of their cursors, the height is arbitrary up to
10178 * the precision of the register. Everything else requires
10179 * square cursors, limited to a few power-of-two sizes.
10180 */
10181 if (IS_845G(dev) || IS_I865G(dev)) {
10182 if ((width & 63) != 0)
10183 return false;
10184
10185 if (width > (IS_845G(dev) ? 64 : 512))
10186 return false;
10187
10188 if (height > 1023)
10189 return false;
10190 } else {
10191 switch (width | height) {
10192 case 256:
10193 case 128:
10194 if (IS_GEN2(dev))
10195 return false;
10196 case 64:
10197 break;
10198 default:
10199 return false;
10200 }
10201 }
10202
10203 return true;
10204}
10205
Jesse Barnes79e53942008-11-07 14:24:08 -080010206/* VESA 640x480x72Hz mode to set on the pipe */
10207static struct drm_display_mode load_detect_mode = {
10208 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10209 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10210};
10211
Daniel Vettera8bb6812014-02-10 18:00:39 +010010212struct drm_framebuffer *
10213__intel_framebuffer_create(struct drm_device *dev,
10214 struct drm_mode_fb_cmd2 *mode_cmd,
10215 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010216{
10217 struct intel_framebuffer *intel_fb;
10218 int ret;
10219
10220 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010221 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010222 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010223
10224 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010225 if (ret)
10226 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010227
10228 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010229
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010230err:
10231 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010232 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010233}
10234
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010235static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010236intel_framebuffer_create(struct drm_device *dev,
10237 struct drm_mode_fb_cmd2 *mode_cmd,
10238 struct drm_i915_gem_object *obj)
10239{
10240 struct drm_framebuffer *fb;
10241 int ret;
10242
10243 ret = i915_mutex_lock_interruptible(dev);
10244 if (ret)
10245 return ERR_PTR(ret);
10246 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10247 mutex_unlock(&dev->struct_mutex);
10248
10249 return fb;
10250}
10251
Chris Wilsond2dff872011-04-19 08:36:26 +010010252static u32
10253intel_framebuffer_pitch_for_width(int width, int bpp)
10254{
10255 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10256 return ALIGN(pitch, 64);
10257}
10258
10259static u32
10260intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10261{
10262 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010263 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010264}
10265
10266static struct drm_framebuffer *
10267intel_framebuffer_create_for_mode(struct drm_device *dev,
10268 struct drm_display_mode *mode,
10269 int depth, int bpp)
10270{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010271 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010272 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010273 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010274
10275 obj = i915_gem_alloc_object(dev,
10276 intel_framebuffer_size_for_mode(mode, bpp));
10277 if (obj == NULL)
10278 return ERR_PTR(-ENOMEM);
10279
10280 mode_cmd.width = mode->hdisplay;
10281 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010282 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10283 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010284 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010285
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010286 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10287 if (IS_ERR(fb))
10288 drm_gem_object_unreference_unlocked(&obj->base);
10289
10290 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010291}
10292
10293static struct drm_framebuffer *
10294mode_fits_in_fbdev(struct drm_device *dev,
10295 struct drm_display_mode *mode)
10296{
Daniel Vetter06957262015-08-10 13:34:08 +020010297#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010298 struct drm_i915_private *dev_priv = dev->dev_private;
10299 struct drm_i915_gem_object *obj;
10300 struct drm_framebuffer *fb;
10301
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010302 if (!dev_priv->fbdev)
10303 return NULL;
10304
10305 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010306 return NULL;
10307
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010308 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010309 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010310
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010311 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010312 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10313 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010314 return NULL;
10315
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010316 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010317 return NULL;
10318
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010319 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010320 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010321#else
10322 return NULL;
10323#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010324}
10325
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010326static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10327 struct drm_crtc *crtc,
10328 struct drm_display_mode *mode,
10329 struct drm_framebuffer *fb,
10330 int x, int y)
10331{
10332 struct drm_plane_state *plane_state;
10333 int hdisplay, vdisplay;
10334 int ret;
10335
10336 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10337 if (IS_ERR(plane_state))
10338 return PTR_ERR(plane_state);
10339
10340 if (mode)
10341 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10342 else
10343 hdisplay = vdisplay = 0;
10344
10345 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10346 if (ret)
10347 return ret;
10348 drm_atomic_set_fb_for_plane(plane_state, fb);
10349 plane_state->crtc_x = 0;
10350 plane_state->crtc_y = 0;
10351 plane_state->crtc_w = hdisplay;
10352 plane_state->crtc_h = vdisplay;
10353 plane_state->src_x = x << 16;
10354 plane_state->src_y = y << 16;
10355 plane_state->src_w = hdisplay << 16;
10356 plane_state->src_h = vdisplay << 16;
10357
10358 return 0;
10359}
10360
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010361bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010362 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010363 struct intel_load_detect_pipe *old,
10364 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010365{
10366 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010367 struct intel_encoder *intel_encoder =
10368 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010369 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010370 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010371 struct drm_crtc *crtc = NULL;
10372 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010373 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010374 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010375 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010376 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010377 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010378 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010379
Chris Wilsond2dff872011-04-19 08:36:26 +010010380 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010381 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010382 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010383
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010384 old->restore_state = NULL;
10385
Rob Clark51fd3712013-11-19 12:10:12 -050010386retry:
10387 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10388 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010389 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010390
Jesse Barnes79e53942008-11-07 14:24:08 -080010391 /*
10392 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010393 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010394 * - if the connector already has an assigned crtc, use it (but make
10395 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010396 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010397 * - try to find the first unused crtc that can drive this connector,
10398 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010399 */
10400
10401 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010402 if (connector->state->crtc) {
10403 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010404
Rob Clark51fd3712013-11-19 12:10:12 -050010405 ret = drm_modeset_lock(&crtc->mutex, ctx);
10406 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010407 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010408
10409 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010410 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010411 }
10412
10413 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010414 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010415 i++;
10416 if (!(encoder->possible_crtcs & (1 << i)))
10417 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010418
10419 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10420 if (ret)
10421 goto fail;
10422
10423 if (possible_crtc->state->enable) {
10424 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010425 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010426 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010427
10428 crtc = possible_crtc;
10429 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010430 }
10431
10432 /*
10433 * If we didn't find an unused CRTC, don't use any.
10434 */
10435 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010436 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010437 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010438 }
10439
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010440found:
10441 intel_crtc = to_intel_crtc(crtc);
10442
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010443 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10444 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010445 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010446
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010447 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010448 restore_state = drm_atomic_state_alloc(dev);
10449 if (!state || !restore_state) {
10450 ret = -ENOMEM;
10451 goto fail;
10452 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010453
10454 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010455 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010456
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010457 connector_state = drm_atomic_get_connector_state(state, connector);
10458 if (IS_ERR(connector_state)) {
10459 ret = PTR_ERR(connector_state);
10460 goto fail;
10461 }
10462
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010463 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10464 if (ret)
10465 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010466
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010467 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10468 if (IS_ERR(crtc_state)) {
10469 ret = PTR_ERR(crtc_state);
10470 goto fail;
10471 }
10472
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010473 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010474
Chris Wilson64927112011-04-20 07:25:26 +010010475 if (!mode)
10476 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010477
Chris Wilsond2dff872011-04-19 08:36:26 +010010478 /* We need a framebuffer large enough to accommodate all accesses
10479 * that the plane may generate whilst we perform load detection.
10480 * We can not rely on the fbcon either being present (we get called
10481 * during its initialisation to detect all boot displays, or it may
10482 * not even exist) or that it is large enough to satisfy the
10483 * requested mode.
10484 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010485 fb = mode_fits_in_fbdev(dev, mode);
10486 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010487 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010488 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010489 } else
10490 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010491 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010492 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010493 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010494 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010495
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010496 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10497 if (ret)
10498 goto fail;
10499
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010500 drm_framebuffer_unreference(fb);
10501
10502 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10503 if (ret)
10504 goto fail;
10505
10506 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10507 if (!ret)
10508 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10509 if (!ret)
10510 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10511 if (ret) {
10512 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10513 goto fail;
10514 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010515
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010516 ret = drm_atomic_commit(state);
10517 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010518 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010519 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010520 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010521
10522 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010523
Jesse Barnes79e53942008-11-07 14:24:08 -080010524 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010525 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010526 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010527
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010528fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010529 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010530 drm_atomic_state_free(restore_state);
10531 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010532
Rob Clark51fd3712013-11-19 12:10:12 -050010533 if (ret == -EDEADLK) {
10534 drm_modeset_backoff(ctx);
10535 goto retry;
10536 }
10537
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010538 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010539}
10540
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010541void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010542 struct intel_load_detect_pipe *old,
10543 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010544{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010545 struct intel_encoder *intel_encoder =
10546 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010547 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010548 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010549 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010550
Chris Wilsond2dff872011-04-19 08:36:26 +010010551 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010552 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010553 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010554
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010555 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010556 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010557
10558 ret = drm_atomic_commit(state);
10559 if (ret) {
10560 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10561 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010562 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010563}
10564
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010565static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010566 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010567{
10568 struct drm_i915_private *dev_priv = dev->dev_private;
10569 u32 dpll = pipe_config->dpll_hw_state.dpll;
10570
10571 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010572 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010573 else if (HAS_PCH_SPLIT(dev))
10574 return 120000;
10575 else if (!IS_GEN2(dev))
10576 return 96000;
10577 else
10578 return 48000;
10579}
10580
Jesse Barnes79e53942008-11-07 14:24:08 -080010581/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010582static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010583 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010584{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010585 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010586 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010587 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010588 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010589 u32 fp;
10590 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010591 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010592 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010593
10594 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010595 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010596 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010597 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010598
10599 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010600 if (IS_PINEVIEW(dev)) {
10601 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10602 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010603 } else {
10604 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10605 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10606 }
10607
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010608 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010609 if (IS_PINEVIEW(dev))
10610 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10611 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010612 else
10613 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010614 DPLL_FPA01_P1_POST_DIV_SHIFT);
10615
10616 switch (dpll & DPLL_MODE_MASK) {
10617 case DPLLB_MODE_DAC_SERIAL:
10618 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10619 5 : 10;
10620 break;
10621 case DPLLB_MODE_LVDS:
10622 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10623 7 : 14;
10624 break;
10625 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010626 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010627 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010628 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010629 }
10630
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010631 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010632 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010633 else
Imre Deakdccbea32015-06-22 23:35:51 +030010634 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010635 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010636 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010637 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010638
10639 if (is_lvds) {
10640 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10641 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010642
10643 if (lvds & LVDS_CLKB_POWER_UP)
10644 clock.p2 = 7;
10645 else
10646 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010647 } else {
10648 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10649 clock.p1 = 2;
10650 else {
10651 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10652 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10653 }
10654 if (dpll & PLL_P2_DIVIDE_BY_4)
10655 clock.p2 = 4;
10656 else
10657 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010658 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010659
Imre Deakdccbea32015-06-22 23:35:51 +030010660 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010661 }
10662
Ville Syrjälä18442d02013-09-13 16:00:08 +030010663 /*
10664 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010665 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010666 * encoder's get_config() function.
10667 */
Imre Deakdccbea32015-06-22 23:35:51 +030010668 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010669}
10670
Ville Syrjälä6878da02013-09-13 15:59:11 +030010671int intel_dotclock_calculate(int link_freq,
10672 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010673{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010674 /*
10675 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010676 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010677 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010678 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010679 *
10680 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010681 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010682 */
10683
Ville Syrjälä6878da02013-09-13 15:59:11 +030010684 if (!m_n->link_n)
10685 return 0;
10686
10687 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10688}
10689
Ville Syrjälä18442d02013-09-13 16:00:08 +030010690static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010691 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010692{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010693 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010694
10695 /* read out port_clock from the DPLL */
10696 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010697
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010698 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010699 * In case there is an active pipe without active ports,
10700 * we may need some idea for the dotclock anyway.
10701 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010702 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010703 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010704 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010705 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010706}
10707
10708/** Returns the currently programmed mode of the given pipe. */
10709struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10710 struct drm_crtc *crtc)
10711{
Jesse Barnes548f2452011-02-17 10:40:53 -080010712 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010713 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010714 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010715 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010716 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010717 int htot = I915_READ(HTOTAL(cpu_transcoder));
10718 int hsync = I915_READ(HSYNC(cpu_transcoder));
10719 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10720 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010721 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010722
10723 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10724 if (!mode)
10725 return NULL;
10726
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010727 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10728 if (!pipe_config) {
10729 kfree(mode);
10730 return NULL;
10731 }
10732
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010733 /*
10734 * Construct a pipe_config sufficient for getting the clock info
10735 * back out of crtc_clock_get.
10736 *
10737 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10738 * to use a real value here instead.
10739 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010740 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10741 pipe_config->pixel_multiplier = 1;
10742 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10743 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10744 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10745 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010746
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010747 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010748 mode->hdisplay = (htot & 0xffff) + 1;
10749 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10750 mode->hsync_start = (hsync & 0xffff) + 1;
10751 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10752 mode->vdisplay = (vtot & 0xffff) + 1;
10753 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10754 mode->vsync_start = (vsync & 0xffff) + 1;
10755 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10756
10757 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010758
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010759 kfree(pipe_config);
10760
Jesse Barnes79e53942008-11-07 14:24:08 -080010761 return mode;
10762}
10763
Chris Wilsonf047e392012-07-21 12:31:41 +010010764void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010765{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010766 struct drm_i915_private *dev_priv = dev->dev_private;
10767
Chris Wilsonf62a0072014-02-21 17:55:39 +000010768 if (dev_priv->mm.busy)
10769 return;
10770
Paulo Zanoni43694d62014-03-07 20:08:08 -030010771 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010772 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010773 if (INTEL_INFO(dev)->gen >= 6)
10774 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010775 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010776}
10777
10778void intel_mark_idle(struct drm_device *dev)
10779{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010780 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010781
Chris Wilsonf62a0072014-02-21 17:55:39 +000010782 if (!dev_priv->mm.busy)
10783 return;
10784
10785 dev_priv->mm.busy = false;
10786
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010787 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010788 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010789
Paulo Zanoni43694d62014-03-07 20:08:08 -030010790 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010791}
10792
Jesse Barnes79e53942008-11-07 14:24:08 -080010793static void intel_crtc_destroy(struct drm_crtc *crtc)
10794{
10795 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010796 struct drm_device *dev = crtc->dev;
10797 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010798
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010799 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010800 work = intel_crtc->unpin_work;
10801 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010802 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010803
10804 if (work) {
10805 cancel_work_sync(&work->work);
10806 kfree(work);
10807 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010808
10809 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010810
Jesse Barnes79e53942008-11-07 14:24:08 -080010811 kfree(intel_crtc);
10812}
10813
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010814static void intel_unpin_work_fn(struct work_struct *__work)
10815{
10816 struct intel_unpin_work *work =
10817 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010818 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10819 struct drm_device *dev = crtc->base.dev;
10820 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010821
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010822 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020010823 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilson05394f32010-11-08 19:18:58 +000010824 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010825
John Harrisonf06cc1b2014-11-24 18:49:37 +000010826 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010827 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010828 mutex_unlock(&dev->struct_mutex);
10829
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010830 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanoni1eb52232016-01-19 11:35:44 -020010831 intel_fbc_post_update(crtc);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010832 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010833
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010834 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10835 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010836
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010837 kfree(work);
10838}
10839
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010840static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010841 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010842{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010843 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10844 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010845 unsigned long flags;
10846
10847 /* Ignore early vblank irqs */
10848 if (intel_crtc == NULL)
10849 return;
10850
Daniel Vetterf3260382014-09-15 14:55:23 +020010851 /*
10852 * This is called both by irq handlers and the reset code (to complete
10853 * lost pageflips) so needs the full irqsave spinlocks.
10854 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010855 spin_lock_irqsave(&dev->event_lock, flags);
10856 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010857
10858 /* Ensure we don't miss a work->pending update ... */
10859 smp_rmb();
10860
10861 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010862 spin_unlock_irqrestore(&dev->event_lock, flags);
10863 return;
10864 }
10865
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010866 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010867
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010868 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010869}
10870
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010871void intel_finish_page_flip(struct drm_device *dev, int pipe)
10872{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010873 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010874 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10875
Mario Kleiner49b14a52010-12-09 07:00:07 +010010876 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010877}
10878
10879void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10880{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010881 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010882 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10883
Mario Kleiner49b14a52010-12-09 07:00:07 +010010884 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010885}
10886
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010887/* Is 'a' after or equal to 'b'? */
10888static bool g4x_flip_count_after_eq(u32 a, u32 b)
10889{
10890 return !((a - b) & 0x80000000);
10891}
10892
10893static bool page_flip_finished(struct intel_crtc *crtc)
10894{
10895 struct drm_device *dev = crtc->base.dev;
10896 struct drm_i915_private *dev_priv = dev->dev_private;
10897
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010898 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10899 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10900 return true;
10901
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010902 /*
10903 * The relevant registers doen't exist on pre-ctg.
10904 * As the flip done interrupt doesn't trigger for mmio
10905 * flips on gmch platforms, a flip count check isn't
10906 * really needed there. But since ctg has the registers,
10907 * include it in the check anyway.
10908 */
10909 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10910 return true;
10911
10912 /*
Maarten Lankhorste8861672016-02-24 11:24:26 +010010913 * BDW signals flip done immediately if the plane
10914 * is disabled, even if the plane enable is already
10915 * armed to occur at the next vblank :(
10916 */
10917
10918 /*
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010919 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10920 * used the same base address. In that case the mmio flip might
10921 * have completed, but the CS hasn't even executed the flip yet.
10922 *
10923 * A flip count check isn't enough as the CS might have updated
10924 * the base address just after start of vblank, but before we
10925 * managed to process the interrupt. This means we'd complete the
10926 * CS flip too soon.
10927 *
10928 * Combining both checks should get us a good enough result. It may
10929 * still happen that the CS flip has been executed, but has not
10930 * yet actually completed. But in case the base address is the same
10931 * anyway, we don't really care.
10932 */
10933 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10934 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030010935 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010936 crtc->unpin_work->flip_count);
10937}
10938
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010939void intel_prepare_page_flip(struct drm_device *dev, int plane)
10940{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010941 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010942 struct intel_crtc *intel_crtc =
10943 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10944 unsigned long flags;
10945
Daniel Vetterf3260382014-09-15 14:55:23 +020010946
10947 /*
10948 * This is called both by irq handlers and the reset code (to complete
10949 * lost pageflips) so needs the full irqsave spinlocks.
10950 *
10951 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010952 * generate a page-flip completion irq, i.e. every modeset
10953 * is also accompanied by a spurious intel_prepare_page_flip().
10954 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010955 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010956 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010957 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010958 spin_unlock_irqrestore(&dev->event_lock, flags);
10959}
10960
Chris Wilson60426392015-10-10 10:44:32 +010010961static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010962{
10963 /* Ensure that the work item is consistent when activating it ... */
10964 smp_wmb();
Chris Wilson60426392015-10-10 10:44:32 +010010965 atomic_set(&work->pending, INTEL_FLIP_PENDING);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010966 /* and that it is marked active as soon as the irq could fire. */
10967 smp_wmb();
10968}
10969
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010970static int intel_gen2_queue_flip(struct drm_device *dev,
10971 struct drm_crtc *crtc,
10972 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010973 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010974 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010975 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010976{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010977 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010978 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010979 u32 flip_mask;
10980 int ret;
10981
John Harrison5fb9de12015-05-29 17:44:07 +010010982 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010983 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010984 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010985
10986 /* Can't queue multiple flips, so wait for the previous
10987 * one to finish before executing the next.
10988 */
10989 if (intel_crtc->plane)
10990 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10991 else
10992 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010993 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
10994 intel_ring_emit(engine, MI_NOOP);
10995 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020010996 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000010997 intel_ring_emit(engine, fb->pitches[0]);
10998 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
10999 intel_ring_emit(engine, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000011000
Chris Wilson60426392015-10-10 10:44:32 +010011001 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011002 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011003}
11004
11005static int intel_gen3_queue_flip(struct drm_device *dev,
11006 struct drm_crtc *crtc,
11007 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011008 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011009 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011010 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011011{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011012 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011013 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011014 u32 flip_mask;
11015 int ret;
11016
John Harrison5fb9de12015-05-29 17:44:07 +010011017 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011018 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011019 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011020
11021 if (intel_crtc->plane)
11022 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11023 else
11024 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011025 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11026 intel_ring_emit(engine, MI_NOOP);
11027 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011028 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011029 intel_ring_emit(engine, fb->pitches[0]);
11030 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11031 intel_ring_emit(engine, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011032
Chris Wilson60426392015-10-10 10:44:32 +010011033 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011034 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011035}
11036
11037static int intel_gen4_queue_flip(struct drm_device *dev,
11038 struct drm_crtc *crtc,
11039 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011040 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011041 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011042 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011043{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011044 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011045 struct drm_i915_private *dev_priv = dev->dev_private;
11046 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11047 uint32_t pf, pipesrc;
11048 int ret;
11049
John Harrison5fb9de12015-05-29 17:44:07 +010011050 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011051 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011052 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011053
11054 /* i965+ uses the linear or tiled offsets from the
11055 * Display Registers (which do not change across a page-flip)
11056 * so we need only reprogram the base address.
11057 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011058 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011059 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011060 intel_ring_emit(engine, fb->pitches[0]);
11061 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011062 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011063
11064 /* XXX Enabling the panel-fitter across page-flip is so far
11065 * untested on non-native modes, so ignore it for now.
11066 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11067 */
11068 pf = 0;
11069 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011070 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011071
Chris Wilson60426392015-10-10 10:44:32 +010011072 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011073 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011074}
11075
11076static int intel_gen6_queue_flip(struct drm_device *dev,
11077 struct drm_crtc *crtc,
11078 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011079 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011080 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011081 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011082{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011083 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011084 struct drm_i915_private *dev_priv = dev->dev_private;
11085 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11086 uint32_t pf, pipesrc;
11087 int ret;
11088
John Harrison5fb9de12015-05-29 17:44:07 +010011089 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011090 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011091 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011092
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011093 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011094 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011095 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11096 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011097
Chris Wilson99d9acd2012-04-17 20:37:00 +010011098 /* Contrary to the suggestions in the documentation,
11099 * "Enable Panel Fitter" does not seem to be required when page
11100 * flipping with a non-native mode, and worse causes a normal
11101 * modeset to fail.
11102 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11103 */
11104 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011105 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011106 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011107
Chris Wilson60426392015-10-10 10:44:32 +010011108 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011109 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011110}
11111
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011112static int intel_gen7_queue_flip(struct drm_device *dev,
11113 struct drm_crtc *crtc,
11114 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011115 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011116 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011117 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011118{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011119 struct intel_engine_cs *engine = req->engine;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011120 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011121 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011122 int len, ret;
11123
Robin Schroereba905b2014-05-18 02:24:50 +020011124 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011125 case PLANE_A:
11126 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11127 break;
11128 case PLANE_B:
11129 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11130 break;
11131 case PLANE_C:
11132 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11133 break;
11134 default:
11135 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011136 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011137 }
11138
Chris Wilsonffe74d72013-08-26 20:58:12 +010011139 len = 4;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011140 if (engine->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011141 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011142 /*
11143 * On Gen 8, SRM is now taking an extra dword to accommodate
11144 * 48bits addresses, and we need a NOOP for the batch size to
11145 * stay even.
11146 */
11147 if (IS_GEN8(dev))
11148 len += 2;
11149 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011150
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011151 /*
11152 * BSpec MI_DISPLAY_FLIP for IVB:
11153 * "The full packet must be contained within the same cache line."
11154 *
11155 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11156 * cacheline, if we ever start emitting more commands before
11157 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11158 * then do the cacheline alignment, and finally emit the
11159 * MI_DISPLAY_FLIP.
11160 */
John Harrisonbba09b12015-05-29 17:44:06 +010011161 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011162 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011163 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011164
John Harrison5fb9de12015-05-29 17:44:07 +010011165 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011166 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011167 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011168
Chris Wilsonffe74d72013-08-26 20:58:12 +010011169 /* Unmask the flip-done completion message. Note that the bspec says that
11170 * we should do this for both the BCS and RCS, and that we must not unmask
11171 * more than one flip event at any time (or ensure that one flip message
11172 * can be sent by waiting for flip-done prior to queueing new flips).
11173 * Experimentation says that BCS works despite DERRMR masking all
11174 * flip-done completion events and that unmasking all planes at once
11175 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11176 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11177 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011178 if (engine->id == RCS) {
11179 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11180 intel_ring_emit_reg(engine, DERRMR);
11181 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11182 DERRMR_PIPEB_PRI_FLIP_DONE |
11183 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011184 if (IS_GEN8(dev))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011185 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011186 MI_SRM_LRM_GLOBAL_GTT);
11187 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011188 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011189 MI_SRM_LRM_GLOBAL_GTT);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011190 intel_ring_emit_reg(engine, DERRMR);
11191 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011192 if (IS_GEN8(dev)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011193 intel_ring_emit(engine, 0);
11194 intel_ring_emit(engine, MI_NOOP);
Damien Lespiauf4768282014-04-07 20:24:34 +010011195 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011196 }
11197
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011198 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11199 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11200 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11201 intel_ring_emit(engine, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011202
Chris Wilson60426392015-10-10 10:44:32 +010011203 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011204 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011205}
11206
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011207static bool use_mmio_flip(struct intel_engine_cs *engine,
Sourab Gupta84c33a62014-06-02 16:47:17 +053011208 struct drm_i915_gem_object *obj)
11209{
11210 /*
11211 * This is not being used for older platforms, because
11212 * non-availability of flip done interrupt forces us to use
11213 * CS flips. Older platforms derive flip done using some clever
11214 * tricks involving the flip_pending status bits and vblank irqs.
11215 * So using MMIO flips there would disrupt this mechanism.
11216 */
11217
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011218 if (engine == NULL)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011219 return true;
11220
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011221 if (INTEL_INFO(engine->dev)->gen < 5)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011222 return false;
11223
11224 if (i915.use_mmio_flip < 0)
11225 return false;
11226 else if (i915.use_mmio_flip > 0)
11227 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011228 else if (i915.enable_execlists)
11229 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011230 else if (obj->base.dma_buf &&
11231 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11232 false))
11233 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011234 else
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011235 return engine != i915_gem_request_get_engine(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011236}
11237
Chris Wilson60426392015-10-10 10:44:32 +010011238static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011239 unsigned int rotation,
Chris Wilson60426392015-10-10 10:44:32 +010011240 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011241{
11242 struct drm_device *dev = intel_crtc->base.dev;
11243 struct drm_i915_private *dev_priv = dev->dev_private;
11244 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011245 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011246 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011247
11248 ctl = I915_READ(PLANE_CTL(pipe, 0));
11249 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011250 switch (fb->modifier[0]) {
11251 case DRM_FORMAT_MOD_NONE:
11252 break;
11253 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011254 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011255 break;
11256 case I915_FORMAT_MOD_Y_TILED:
11257 ctl |= PLANE_CTL_TILED_Y;
11258 break;
11259 case I915_FORMAT_MOD_Yf_TILED:
11260 ctl |= PLANE_CTL_TILED_YF;
11261 break;
11262 default:
11263 MISSING_CASE(fb->modifier[0]);
11264 }
Damien Lespiauff944562014-11-20 14:58:16 +000011265
11266 /*
11267 * The stride is either expressed as a multiple of 64 bytes chunks for
11268 * linear buffers or in number of tiles for tiled buffers.
11269 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011270 if (intel_rotation_90_or_270(rotation)) {
11271 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +020011272 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011273 stride = DIV_ROUND_UP(fb->height, tile_height);
11274 } else {
11275 stride = fb->pitches[0] /
Ville Syrjälä7b49f942016-01-12 21:08:32 +020011276 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11277 fb->pixel_format);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011278 }
Damien Lespiauff944562014-11-20 14:58:16 +000011279
11280 /*
11281 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11282 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11283 */
11284 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11285 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11286
Chris Wilson60426392015-10-10 10:44:32 +010011287 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011288 POSTING_READ(PLANE_SURF(pipe, 0));
11289}
11290
Chris Wilson60426392015-10-10 10:44:32 +010011291static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11292 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011293{
11294 struct drm_device *dev = intel_crtc->base.dev;
11295 struct drm_i915_private *dev_priv = dev->dev_private;
11296 struct intel_framebuffer *intel_fb =
11297 to_intel_framebuffer(intel_crtc->base.primary->fb);
11298 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011299 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011300 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011301
Sourab Gupta84c33a62014-06-02 16:47:17 +053011302 dspcntr = I915_READ(reg);
11303
Damien Lespiauc5d97472014-10-25 00:11:11 +010011304 if (obj->tiling_mode != I915_TILING_NONE)
11305 dspcntr |= DISPPLANE_TILED;
11306 else
11307 dspcntr &= ~DISPPLANE_TILED;
11308
Sourab Gupta84c33a62014-06-02 16:47:17 +053011309 I915_WRITE(reg, dspcntr);
11310
Chris Wilson60426392015-10-10 10:44:32 +010011311 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011312 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011313}
11314
11315/*
11316 * XXX: This is the temporary way to update the plane registers until we get
11317 * around to using the usual plane update functions for MMIO flips
11318 */
Chris Wilson60426392015-10-10 10:44:32 +010011319static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011320{
Chris Wilson60426392015-10-10 10:44:32 +010011321 struct intel_crtc *crtc = mmio_flip->crtc;
11322 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011323
Chris Wilson60426392015-10-10 10:44:32 +010011324 spin_lock_irq(&crtc->base.dev->event_lock);
11325 work = crtc->unpin_work;
11326 spin_unlock_irq(&crtc->base.dev->event_lock);
11327 if (work == NULL)
11328 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011329
Chris Wilson60426392015-10-10 10:44:32 +010011330 intel_mark_page_flip_active(work);
Damien Lespiauff944562014-11-20 14:58:16 +000011331
Chris Wilson60426392015-10-10 10:44:32 +010011332 intel_pipe_update_start(crtc);
11333
11334 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011335 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011336 else
11337 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011338 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011339
Chris Wilson60426392015-10-10 10:44:32 +010011340 intel_pipe_update_end(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011341}
11342
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011343static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011344{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011345 struct intel_mmio_flip *mmio_flip =
11346 container_of(work, struct intel_mmio_flip, work);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011347 struct intel_framebuffer *intel_fb =
11348 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11349 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011350
Chris Wilson60426392015-10-10 10:44:32 +010011351 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011352 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011353 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011354 false, NULL,
11355 &mmio_flip->i915->rps.mmioflips));
Chris Wilson60426392015-10-10 10:44:32 +010011356 i915_gem_request_unreference__unlocked(mmio_flip->req);
11357 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011358
Alex Goinsfd8e0582015-11-25 18:43:38 -080011359 /* For framebuffer backed by dmabuf, wait for fence */
11360 if (obj->base.dma_buf)
11361 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11362 false, false,
11363 MAX_SCHEDULE_TIMEOUT) < 0);
11364
Chris Wilson60426392015-10-10 10:44:32 +010011365 intel_do_mmio_flip(mmio_flip);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011366 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011367}
11368
11369static int intel_queue_mmio_flip(struct drm_device *dev,
11370 struct drm_crtc *crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011371 struct drm_i915_gem_object *obj)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011372{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011373 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011374
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011375 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11376 if (mmio_flip == NULL)
11377 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011378
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011379 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011380 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011381 mmio_flip->crtc = to_intel_crtc(crtc);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011382 mmio_flip->rotation = crtc->primary->state->rotation;
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011383
11384 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11385 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011386
Sourab Gupta84c33a62014-06-02 16:47:17 +053011387 return 0;
11388}
11389
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011390static int intel_default_queue_flip(struct drm_device *dev,
11391 struct drm_crtc *crtc,
11392 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011393 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011394 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011395 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011396{
11397 return -ENODEV;
11398}
11399
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011400static bool __intel_pageflip_stall_check(struct drm_device *dev,
11401 struct drm_crtc *crtc)
11402{
11403 struct drm_i915_private *dev_priv = dev->dev_private;
11404 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11405 struct intel_unpin_work *work = intel_crtc->unpin_work;
11406 u32 addr;
11407
11408 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11409 return true;
11410
Chris Wilson908565c2015-08-12 13:08:22 +010011411 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11412 return false;
11413
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011414 if (!work->enable_stall_check)
11415 return false;
11416
11417 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011418 if (work->flip_queued_req &&
11419 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011420 return false;
11421
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011422 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011423 }
11424
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011425 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011426 return false;
11427
11428 /* Potential stall - if we see that the flip has happened,
11429 * assume a missed interrupt. */
11430 if (INTEL_INFO(dev)->gen >= 4)
11431 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11432 else
11433 addr = I915_READ(DSPADDR(intel_crtc->plane));
11434
11435 /* There is a potential issue here with a false positive after a flip
11436 * to the same address. We could address this by checking for a
11437 * non-incrementing frame counter.
11438 */
11439 return addr == work->gtt_offset;
11440}
11441
11442void intel_check_page_flip(struct drm_device *dev, int pipe)
11443{
11444 struct drm_i915_private *dev_priv = dev->dev_private;
11445 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11446 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011447 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011448
Dave Gordon6c51d462015-03-06 15:34:26 +000011449 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011450
11451 if (crtc == NULL)
11452 return;
11453
Daniel Vetterf3260382014-09-15 14:55:23 +020011454 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011455 work = intel_crtc->unpin_work;
11456 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011457 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011458 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011459 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011460 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011461 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011462 if (work != NULL &&
11463 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11464 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011465 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011466}
11467
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011468static int intel_crtc_page_flip(struct drm_crtc *crtc,
11469 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011470 struct drm_pending_vblank_event *event,
11471 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011472{
11473 struct drm_device *dev = crtc->dev;
11474 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011475 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011476 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011477 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011478 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011479 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011480 struct intel_unpin_work *work;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011481 struct intel_engine_cs *engine;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011482 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011483 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011484 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011485
Matt Roper2ff8fde2014-07-08 07:50:07 -070011486 /*
11487 * drm_mode_page_flip_ioctl() should already catch this, but double
11488 * check to be safe. In the future we may enable pageflipping from
11489 * a disabled primary plane.
11490 */
11491 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11492 return -EBUSY;
11493
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011494 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011495 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011496 return -EINVAL;
11497
11498 /*
11499 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11500 * Note that pitch changes could also affect these register.
11501 */
11502 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011503 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11504 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011505 return -EINVAL;
11506
Chris Wilsonf900db42014-02-20 09:26:13 +000011507 if (i915_terminally_wedged(&dev_priv->gpu_error))
11508 goto out_hang;
11509
Daniel Vetterb14c5672013-09-19 12:18:32 +020011510 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011511 if (work == NULL)
11512 return -ENOMEM;
11513
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011514 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011515 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011516 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011517 INIT_WORK(&work->work, intel_unpin_work_fn);
11518
Daniel Vetter87b6b102014-05-15 15:33:46 +020011519 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011520 if (ret)
11521 goto free_work;
11522
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011523 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011524 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011525 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011526 /* Before declaring the flip queue wedged, check if
11527 * the hardware completed the operation behind our backs.
11528 */
11529 if (__intel_pageflip_stall_check(dev, crtc)) {
11530 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11531 page_flip_completed(intel_crtc);
11532 } else {
11533 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011534 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011535
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011536 drm_crtc_vblank_put(crtc);
11537 kfree(work);
11538 return -EBUSY;
11539 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011540 }
11541 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011542 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011543
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011544 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11545 flush_workqueue(dev_priv->wq);
11546
Jesse Barnes75dfca82010-02-10 15:09:44 -080011547 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011548 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011549 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011550
Matt Roperf4510a22014-04-01 15:22:40 -070011551 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011552 update_state_fb(crtc->primary);
Paulo Zanonie8216e52016-01-19 11:35:56 -020011553 intel_fbc_pre_update(intel_crtc);
Matt Roper1ed1f962015-01-30 16:22:36 -080011554
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011555 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011556
Chris Wilson89ed88b2015-02-16 14:31:49 +000011557 ret = i915_mutex_lock_interruptible(dev);
11558 if (ret)
11559 goto cleanup;
11560
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011561 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011562 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011563
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011564 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f5072015-09-18 20:03:42 +030011565 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011566
Wayne Boyer666a4532015-12-09 12:29:35 -080011567 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011568 engine = &dev_priv->engine[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011569 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011570 /* vlv: DISPLAY_FLIP fails to change tiling */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011571 engine = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011572 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011573 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011574 } else if (INTEL_INFO(dev)->gen >= 7) {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011575 engine = i915_gem_request_get_engine(obj->last_write_req);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011576 if (engine == NULL || engine->id != RCS)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011577 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011578 } else {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011579 engine = &dev_priv->engine[RCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011580 }
11581
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011582 mmio_flip = use_mmio_flip(engine, obj);
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011583
11584 /* When using CS flips, we want to emit semaphores between rings.
11585 * However, when using mmio flips we will create a task to do the
11586 * synchronisation, so all we want here is to pin the framebuffer
11587 * into the display plane and skip any waits.
11588 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011589 if (!mmio_flip) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011590 ret = i915_gem_object_sync(obj, engine, &request);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011591 if (ret)
11592 goto cleanup_pending;
11593 }
11594
Ville Syrjälä3465c582016-02-15 22:54:43 +020011595 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011596 if (ret)
11597 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011598
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011599 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11600 obj, 0);
11601 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011602
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011603 if (mmio_flip) {
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011604 ret = intel_queue_mmio_flip(dev, crtc, obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011605 if (ret)
11606 goto cleanup_unpin;
11607
John Harrisonf06cc1b2014-11-24 18:49:37 +000011608 i915_gem_request_assign(&work->flip_queued_req,
11609 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011610 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011611 if (!request) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011612 request = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +000011613 if (IS_ERR(request)) {
11614 ret = PTR_ERR(request);
John Harrison6258fbe2015-05-29 17:43:48 +010011615 goto cleanup_unpin;
Dave Gordon26827082016-01-19 19:02:53 +000011616 }
John Harrison6258fbe2015-05-29 17:43:48 +010011617 }
11618
11619 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011620 page_flip_flags);
11621 if (ret)
11622 goto cleanup_unpin;
11623
John Harrison6258fbe2015-05-29 17:43:48 +010011624 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011625 }
11626
John Harrison91af1272015-06-18 13:14:56 +010011627 if (request)
John Harrison75289872015-05-29 17:43:49 +010011628 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011629
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011630 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011631 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011632
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011633 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011634 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011635 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011636
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011637 intel_frontbuffer_flip_prepare(dev,
11638 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011639
Jesse Barnese5510fa2010-07-01 16:48:37 -070011640 trace_i915_flip_request(intel_crtc->plane, obj);
11641
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011642 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011643
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011644cleanup_unpin:
Ville Syrjälä3465c582016-02-15 22:54:43 +020011645 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011646cleanup_pending:
Dave Gordon0aa498d2016-01-28 10:48:09 +000011647 if (!IS_ERR_OR_NULL(request))
John Harrison91af1272015-06-18 13:14:56 +010011648 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011649 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011650 mutex_unlock(&dev->struct_mutex);
11651cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011652 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011653 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011654
Chris Wilson89ed88b2015-02-16 14:31:49 +000011655 drm_gem_object_unreference_unlocked(&obj->base);
11656 drm_framebuffer_unreference(work->old_fb);
11657
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011658 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011659 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011660 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011661
Daniel Vetter87b6b102014-05-15 15:33:46 +020011662 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011663free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011664 kfree(work);
11665
Chris Wilsonf900db42014-02-20 09:26:13 +000011666 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011667 struct drm_atomic_state *state;
11668 struct drm_plane_state *plane_state;
11669
Chris Wilsonf900db42014-02-20 09:26:13 +000011670out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011671 state = drm_atomic_state_alloc(dev);
11672 if (!state)
11673 return -ENOMEM;
11674 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11675
11676retry:
11677 plane_state = drm_atomic_get_plane_state(state, primary);
11678 ret = PTR_ERR_OR_ZERO(plane_state);
11679 if (!ret) {
11680 drm_atomic_set_fb_for_plane(plane_state, fb);
11681
11682 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11683 if (!ret)
11684 ret = drm_atomic_commit(state);
11685 }
11686
11687 if (ret == -EDEADLK) {
11688 drm_modeset_backoff(state->acquire_ctx);
11689 drm_atomic_state_clear(state);
11690 goto retry;
11691 }
11692
11693 if (ret)
11694 drm_atomic_state_free(state);
11695
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011696 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011697 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011698 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011699 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011700 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011701 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011702 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011703}
11704
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011705
11706/**
11707 * intel_wm_need_update - Check whether watermarks need updating
11708 * @plane: drm plane
11709 * @state: new plane state
11710 *
11711 * Check current plane state versus the new one to determine whether
11712 * watermarks need to be recalculated.
11713 *
11714 * Returns true or false.
11715 */
11716static bool intel_wm_need_update(struct drm_plane *plane,
11717 struct drm_plane_state *state)
11718{
Matt Roperd21fbe82015-09-24 15:53:12 -070011719 struct intel_plane_state *new = to_intel_plane_state(state);
11720 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11721
11722 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011723 if (new->visible != cur->visible)
11724 return true;
11725
11726 if (!cur->base.fb || !new->base.fb)
11727 return false;
11728
11729 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11730 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011731 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11732 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11733 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11734 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011735 return true;
11736
11737 return false;
11738}
11739
Matt Roperd21fbe82015-09-24 15:53:12 -070011740static bool needs_scaling(struct intel_plane_state *state)
11741{
11742 int src_w = drm_rect_width(&state->src) >> 16;
11743 int src_h = drm_rect_height(&state->src) >> 16;
11744 int dst_w = drm_rect_width(&state->dst);
11745 int dst_h = drm_rect_height(&state->dst);
11746
11747 return (src_w != dst_w || src_h != dst_h);
11748}
11749
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011750int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11751 struct drm_plane_state *plane_state)
11752{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011753 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011754 struct drm_crtc *crtc = crtc_state->crtc;
11755 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11756 struct drm_plane *plane = plane_state->plane;
11757 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011758 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011759 struct intel_plane_state *old_plane_state =
11760 to_intel_plane_state(plane->state);
11761 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011762 bool mode_changed = needs_modeset(crtc_state);
11763 bool was_crtc_enabled = crtc->state->active;
11764 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011765 bool turn_off, turn_on, visible, was_visible;
11766 struct drm_framebuffer *fb = plane_state->fb;
11767
11768 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11769 plane->type != DRM_PLANE_TYPE_CURSOR) {
11770 ret = skl_update_scaler_plane(
11771 to_intel_crtc_state(crtc_state),
11772 to_intel_plane_state(plane_state));
11773 if (ret)
11774 return ret;
11775 }
11776
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011777 was_visible = old_plane_state->visible;
11778 visible = to_intel_plane_state(plane_state)->visible;
11779
11780 if (!was_crtc_enabled && WARN_ON(was_visible))
11781 was_visible = false;
11782
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011783 /*
11784 * Visibility is calculated as if the crtc was on, but
11785 * after scaler setup everything depends on it being off
11786 * when the crtc isn't active.
11787 */
11788 if (!is_crtc_enabled)
11789 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011790
11791 if (!was_visible && !visible)
11792 return 0;
11793
Maarten Lankhorste8861672016-02-24 11:24:26 +010011794 if (fb != old_plane_state->base.fb)
11795 pipe_config->fb_changed = true;
11796
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011797 turn_off = was_visible && (!visible || mode_changed);
11798 turn_on = visible && (!was_visible || mode_changed);
11799
11800 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11801 plane->base.id, fb ? fb->base.id : -1);
11802
11803 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11804 plane->base.id, was_visible, visible,
11805 turn_off, turn_on, mode_changed);
11806
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011807 if (turn_on) {
11808 pipe_config->update_wm_pre = true;
11809
11810 /* must disable cxsr around plane enable/disable */
11811 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11812 pipe_config->disable_cxsr = true;
11813 } else if (turn_off) {
11814 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011815
Ville Syrjälä852eb002015-06-24 22:00:07 +030011816 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011817 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011818 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011819 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011820 /* FIXME bollocks */
11821 pipe_config->update_wm_pre = true;
11822 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011823 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011824
Matt Ropered4a6a72016-02-23 17:20:13 -080011825 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011826 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11827 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011828 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11829
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011830 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011831 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011832
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011833 /*
11834 * WaCxSRDisabledForSpriteScaling:ivb
11835 *
11836 * cstate->update_wm was already set above, so this flag will
11837 * take effect when we commit and program watermarks.
11838 */
11839 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11840 needs_scaling(to_intel_plane_state(plane_state)) &&
11841 !needs_scaling(old_plane_state))
11842 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011843
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011844 return 0;
11845}
11846
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011847static bool encoders_cloneable(const struct intel_encoder *a,
11848 const struct intel_encoder *b)
11849{
11850 /* masks could be asymmetric, so check both ways */
11851 return a == b || (a->cloneable & (1 << b->type) &&
11852 b->cloneable & (1 << a->type));
11853}
11854
11855static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11856 struct intel_crtc *crtc,
11857 struct intel_encoder *encoder)
11858{
11859 struct intel_encoder *source_encoder;
11860 struct drm_connector *connector;
11861 struct drm_connector_state *connector_state;
11862 int i;
11863
11864 for_each_connector_in_state(state, connector, connector_state, i) {
11865 if (connector_state->crtc != &crtc->base)
11866 continue;
11867
11868 source_encoder =
11869 to_intel_encoder(connector_state->best_encoder);
11870 if (!encoders_cloneable(encoder, source_encoder))
11871 return false;
11872 }
11873
11874 return true;
11875}
11876
11877static bool check_encoder_cloning(struct drm_atomic_state *state,
11878 struct intel_crtc *crtc)
11879{
11880 struct intel_encoder *encoder;
11881 struct drm_connector *connector;
11882 struct drm_connector_state *connector_state;
11883 int i;
11884
11885 for_each_connector_in_state(state, connector, connector_state, i) {
11886 if (connector_state->crtc != &crtc->base)
11887 continue;
11888
11889 encoder = to_intel_encoder(connector_state->best_encoder);
11890 if (!check_single_encoder_cloning(state, crtc, encoder))
11891 return false;
11892 }
11893
11894 return true;
11895}
11896
11897static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11898 struct drm_crtc_state *crtc_state)
11899{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011900 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011901 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011902 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011903 struct intel_crtc_state *pipe_config =
11904 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011905 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011906 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011907 bool mode_changed = needs_modeset(crtc_state);
11908
11909 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11910 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11911 return -EINVAL;
11912 }
11913
Ville Syrjälä852eb002015-06-24 22:00:07 +030011914 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011915 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011916
Maarten Lankhorstad421372015-06-15 12:33:42 +020011917 if (mode_changed && crtc_state->enable &&
11918 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011919 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020011920 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11921 pipe_config);
11922 if (ret)
11923 return ret;
11924 }
11925
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000011926 if (crtc_state->color_mgmt_changed) {
11927 ret = intel_color_check(crtc, crtc_state);
11928 if (ret)
11929 return ret;
11930 }
11931
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011932 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011933 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010011934 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080011935 if (ret) {
11936 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070011937 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080011938 }
11939 }
11940
11941 if (dev_priv->display.compute_intermediate_wm &&
11942 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11943 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11944 return 0;
11945
11946 /*
11947 * Calculate 'intermediate' watermarks that satisfy both the
11948 * old state and the new state. We can program these
11949 * immediately.
11950 */
11951 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11952 intel_crtc,
11953 pipe_config);
11954 if (ret) {
11955 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
11956 return ret;
11957 }
Matt Roper86c8bbb2015-09-24 15:53:16 -070011958 }
11959
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011960 if (INTEL_INFO(dev)->gen >= 9) {
11961 if (mode_changed)
11962 ret = skl_update_scaler_crtc(pipe_config);
11963
11964 if (!ret)
11965 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11966 pipe_config);
11967 }
11968
11969 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011970}
11971
Jani Nikula65b38e02015-04-13 11:26:56 +030011972static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011973 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Matt Roperea2c67b2014-12-23 10:41:52 -080011974 .atomic_begin = intel_begin_crtc_commit,
11975 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011976 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011977};
11978
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011979static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11980{
11981 struct intel_connector *connector;
11982
11983 for_each_intel_connector(dev, connector) {
11984 if (connector->base.encoder) {
11985 connector->base.state->best_encoder =
11986 connector->base.encoder;
11987 connector->base.state->crtc =
11988 connector->base.encoder->crtc;
11989 } else {
11990 connector->base.state->best_encoder = NULL;
11991 connector->base.state->crtc = NULL;
11992 }
11993 }
11994}
11995
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011996static void
Robin Schroereba905b2014-05-18 02:24:50 +020011997connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011998 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011999{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012000 int bpp = pipe_config->pipe_bpp;
12001
12002 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12003 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012004 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012005
12006 /* Don't use an invalid EDID bpc value */
12007 if (connector->base.display_info.bpc &&
12008 connector->base.display_info.bpc * 3 < bpp) {
12009 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12010 bpp, connector->base.display_info.bpc*3);
12011 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12012 }
12013
Jani Nikula013dd9e2016-01-13 16:35:20 +020012014 /* Clamp bpp to default limit on screens without EDID 1.4 */
12015 if (connector->base.display_info.bpc == 0) {
12016 int type = connector->base.connector_type;
12017 int clamp_bpp = 24;
12018
12019 /* Fall back to 18 bpp when DP sink capability is unknown. */
12020 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12021 type == DRM_MODE_CONNECTOR_eDP)
12022 clamp_bpp = 18;
12023
12024 if (bpp > clamp_bpp) {
12025 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12026 bpp, clamp_bpp);
12027 pipe_config->pipe_bpp = clamp_bpp;
12028 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012029 }
12030}
12031
12032static int
12033compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012034 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012035{
12036 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012037 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012038 struct drm_connector *connector;
12039 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012040 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012041
Wayne Boyer666a4532015-12-09 12:29:35 -080012042 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012043 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012044 else if (INTEL_INFO(dev)->gen >= 5)
12045 bpp = 12*3;
12046 else
12047 bpp = 8*3;
12048
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012049
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012050 pipe_config->pipe_bpp = bpp;
12051
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012052 state = pipe_config->base.state;
12053
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012054 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012055 for_each_connector_in_state(state, connector, connector_state, i) {
12056 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012057 continue;
12058
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012059 connected_sink_compute_bpp(to_intel_connector(connector),
12060 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012061 }
12062
12063 return bpp;
12064}
12065
Daniel Vetter644db712013-09-19 14:53:58 +020012066static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12067{
12068 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12069 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012070 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012071 mode->crtc_hdisplay, mode->crtc_hsync_start,
12072 mode->crtc_hsync_end, mode->crtc_htotal,
12073 mode->crtc_vdisplay, mode->crtc_vsync_start,
12074 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12075}
12076
Daniel Vetterc0b03412013-05-28 12:05:54 +020012077static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012078 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012079 const char *context)
12080{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012081 struct drm_device *dev = crtc->base.dev;
12082 struct drm_plane *plane;
12083 struct intel_plane *intel_plane;
12084 struct intel_plane_state *state;
12085 struct drm_framebuffer *fb;
12086
12087 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12088 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012089
Jani Nikulada205632016-03-15 21:51:10 +020012090 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012091 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12092 pipe_config->pipe_bpp, pipe_config->dither);
12093 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12094 pipe_config->has_pch_encoder,
12095 pipe_config->fdi_lanes,
12096 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12097 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12098 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012099 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 +030012100 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012101 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012102 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12103 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12104 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012105
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012106 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 -070012107 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012108 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012109 pipe_config->dp_m2_n2.gmch_m,
12110 pipe_config->dp_m2_n2.gmch_n,
12111 pipe_config->dp_m2_n2.link_m,
12112 pipe_config->dp_m2_n2.link_n,
12113 pipe_config->dp_m2_n2.tu);
12114
Daniel Vetter55072d12014-11-20 16:10:28 +010012115 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12116 pipe_config->has_audio,
12117 pipe_config->has_infoframe);
12118
Daniel Vetterc0b03412013-05-28 12:05:54 +020012119 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012120 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012121 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012122 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12123 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012124 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012125 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12126 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012127 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12128 crtc->num_scalers,
12129 pipe_config->scaler_state.scaler_users,
12130 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012131 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12132 pipe_config->gmch_pfit.control,
12133 pipe_config->gmch_pfit.pgm_ratios,
12134 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012135 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012136 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012137 pipe_config->pch_pfit.size,
12138 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012139 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012140 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012141
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012142 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012143 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012144 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012145 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012146 pipe_config->ddi_pll_sel,
12147 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012148 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012149 pipe_config->dpll_hw_state.pll0,
12150 pipe_config->dpll_hw_state.pll1,
12151 pipe_config->dpll_hw_state.pll2,
12152 pipe_config->dpll_hw_state.pll3,
12153 pipe_config->dpll_hw_state.pll6,
12154 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012155 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012156 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012157 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012158 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012159 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12160 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12161 pipe_config->ddi_pll_sel,
12162 pipe_config->dpll_hw_state.ctrl1,
12163 pipe_config->dpll_hw_state.cfgcr1,
12164 pipe_config->dpll_hw_state.cfgcr2);
12165 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012166 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 +010012167 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012168 pipe_config->dpll_hw_state.wrpll,
12169 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012170 } else {
12171 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12172 "fp0: 0x%x, fp1: 0x%x\n",
12173 pipe_config->dpll_hw_state.dpll,
12174 pipe_config->dpll_hw_state.dpll_md,
12175 pipe_config->dpll_hw_state.fp0,
12176 pipe_config->dpll_hw_state.fp1);
12177 }
12178
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012179 DRM_DEBUG_KMS("planes on this crtc\n");
12180 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12181 intel_plane = to_intel_plane(plane);
12182 if (intel_plane->pipe != crtc->pipe)
12183 continue;
12184
12185 state = to_intel_plane_state(plane->state);
12186 fb = state->base.fb;
12187 if (!fb) {
12188 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12189 "disabled, scaler_id = %d\n",
12190 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12191 plane->base.id, intel_plane->pipe,
12192 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12193 drm_plane_index(plane), state->scaler_id);
12194 continue;
12195 }
12196
12197 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12198 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12199 plane->base.id, intel_plane->pipe,
12200 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12201 drm_plane_index(plane));
12202 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12203 fb->base.id, fb->width, fb->height, fb->pixel_format);
12204 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12205 state->scaler_id,
12206 state->src.x1 >> 16, state->src.y1 >> 16,
12207 drm_rect_width(&state->src) >> 16,
12208 drm_rect_height(&state->src) >> 16,
12209 state->dst.x1, state->dst.y1,
12210 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12211 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012212}
12213
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012214static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012215{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012216 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012217 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012218 unsigned int used_ports = 0;
12219
12220 /*
12221 * Walk the connector list instead of the encoder
12222 * list to detect the problem on ddi platforms
12223 * where there's just one encoder per digital port.
12224 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012225 drm_for_each_connector(connector, dev) {
12226 struct drm_connector_state *connector_state;
12227 struct intel_encoder *encoder;
12228
12229 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12230 if (!connector_state)
12231 connector_state = connector->state;
12232
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012233 if (!connector_state->best_encoder)
12234 continue;
12235
12236 encoder = to_intel_encoder(connector_state->best_encoder);
12237
12238 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012239
12240 switch (encoder->type) {
12241 unsigned int port_mask;
12242 case INTEL_OUTPUT_UNKNOWN:
12243 if (WARN_ON(!HAS_DDI(dev)))
12244 break;
12245 case INTEL_OUTPUT_DISPLAYPORT:
12246 case INTEL_OUTPUT_HDMI:
12247 case INTEL_OUTPUT_EDP:
12248 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12249
12250 /* the same port mustn't appear more than once */
12251 if (used_ports & port_mask)
12252 return false;
12253
12254 used_ports |= port_mask;
12255 default:
12256 break;
12257 }
12258 }
12259
12260 return true;
12261}
12262
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012263static void
12264clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12265{
12266 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012267 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012268 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012269 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012270 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012271 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012272
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012273 /* FIXME: before the switch to atomic started, a new pipe_config was
12274 * kzalloc'd. Code that depends on any field being zero should be
12275 * fixed, so that the crtc_state can be safely duplicated. For now,
12276 * only fields that are know to not cause problems are preserved. */
12277
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012278 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012279 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012280 shared_dpll = crtc_state->shared_dpll;
12281 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012282 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012283 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012284
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012285 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012286
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012287 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012288 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012289 crtc_state->shared_dpll = shared_dpll;
12290 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012291 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012292 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012293}
12294
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012295static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012296intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012297 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012298{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012299 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012300 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012301 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012302 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012303 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012304 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012305 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012306
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012307 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012308
Daniel Vettere143a212013-07-04 12:01:15 +020012309 pipe_config->cpu_transcoder =
12310 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012311
Imre Deak2960bc92013-07-30 13:36:32 +030012312 /*
12313 * Sanitize sync polarity flags based on requested ones. If neither
12314 * positive or negative polarity is requested, treat this as meaning
12315 * negative polarity.
12316 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012317 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012318 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012319 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012320
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_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012323 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012324
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012325 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12326 pipe_config);
12327 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012328 goto fail;
12329
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012330 /*
12331 * Determine the real pipe dimensions. Note that stereo modes can
12332 * increase the actual pipe size due to the frame doubling and
12333 * insertion of additional space for blanks between the frame. This
12334 * is stored in the crtc timings. We use the requested mode to do this
12335 * computation to clearly distinguish it from the adjusted mode, which
12336 * can be changed by the connectors in the below retry loop.
12337 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012338 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012339 &pipe_config->pipe_src_w,
12340 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012341
Daniel Vettere29c22c2013-02-21 00:00:16 +010012342encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012343 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012344 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012345 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012346
Daniel Vetter135c81b2013-07-21 21:37:09 +020012347 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012348 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12349 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012350
Daniel Vetter7758a112012-07-08 19:40:39 +020012351 /* Pass our mode to the connectors and the CRTC to give them a chance to
12352 * adjust it according to limitations or connector properties, and also
12353 * a chance to reject the mode entirely.
12354 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012355 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012356 if (connector_state->crtc != crtc)
12357 continue;
12358
12359 encoder = to_intel_encoder(connector_state->best_encoder);
12360
Daniel Vetterefea6e82013-07-21 21:36:59 +020012361 if (!(encoder->compute_config(encoder, pipe_config))) {
12362 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012363 goto fail;
12364 }
12365 }
12366
Daniel Vetterff9a6752013-06-01 17:16:21 +020012367 /* Set default port clock if not overwritten by the encoder. Needs to be
12368 * done afterwards in case the encoder adjusts the mode. */
12369 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012370 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012371 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012372
Daniel Vettera43f6e02013-06-07 23:10:32 +020012373 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012374 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012375 DRM_DEBUG_KMS("CRTC fixup failed\n");
12376 goto fail;
12377 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012378
12379 if (ret == RETRY) {
12380 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12381 ret = -EINVAL;
12382 goto fail;
12383 }
12384
12385 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12386 retry = false;
12387 goto encoder_retry;
12388 }
12389
Daniel Vettere8fa4272015-08-12 11:43:34 +020012390 /* Dithering seems to not pass-through bits correctly when it should, so
12391 * only enable it on 6bpc panels. */
12392 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012393 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012394 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012395
Daniel Vetter7758a112012-07-08 19:40:39 +020012396fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012397 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012398}
12399
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012400static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012401intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012402{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012403 struct drm_crtc *crtc;
12404 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012405 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012406
Ville Syrjälä76688512014-01-10 11:28:06 +020012407 /* Double check state. */
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012408 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012409 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012410
12411 /* Update hwmode for vblank functions */
12412 if (crtc->state->active)
12413 crtc->hwmode = crtc->state->adjusted_mode;
12414 else
12415 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012416
12417 /*
12418 * Update legacy state to satisfy fbc code. This can
12419 * be removed when fbc uses the atomic state.
12420 */
12421 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12422 struct drm_plane_state *plane_state = crtc->primary->state;
12423
12424 crtc->primary->fb = plane_state->fb;
12425 crtc->x = plane_state->src_x >> 16;
12426 crtc->y = plane_state->src_y >> 16;
12427 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012428 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012429}
12430
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012431static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012432{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012433 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012434
12435 if (clock1 == clock2)
12436 return true;
12437
12438 if (!clock1 || !clock2)
12439 return false;
12440
12441 diff = abs(clock1 - clock2);
12442
12443 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12444 return true;
12445
12446 return false;
12447}
12448
Daniel Vetter25c5b262012-07-08 22:08:04 +020012449#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12450 list_for_each_entry((intel_crtc), \
12451 &(dev)->mode_config.crtc_list, \
12452 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012453 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012454
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012455static bool
12456intel_compare_m_n(unsigned int m, unsigned int n,
12457 unsigned int m2, unsigned int n2,
12458 bool exact)
12459{
12460 if (m == m2 && n == n2)
12461 return true;
12462
12463 if (exact || !m || !n || !m2 || !n2)
12464 return false;
12465
12466 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12467
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012468 if (n > n2) {
12469 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012470 m2 <<= 1;
12471 n2 <<= 1;
12472 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012473 } else if (n < n2) {
12474 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012475 m <<= 1;
12476 n <<= 1;
12477 }
12478 }
12479
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012480 if (n != n2)
12481 return false;
12482
12483 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012484}
12485
12486static bool
12487intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12488 struct intel_link_m_n *m2_n2,
12489 bool adjust)
12490{
12491 if (m_n->tu == m2_n2->tu &&
12492 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12493 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12494 intel_compare_m_n(m_n->link_m, m_n->link_n,
12495 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12496 if (adjust)
12497 *m2_n2 = *m_n;
12498
12499 return true;
12500 }
12501
12502 return false;
12503}
12504
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012505static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012506intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012507 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012508 struct intel_crtc_state *pipe_config,
12509 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012510{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012511 bool ret = true;
12512
12513#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12514 do { \
12515 if (!adjust) \
12516 DRM_ERROR(fmt, ##__VA_ARGS__); \
12517 else \
12518 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12519 } while (0)
12520
Daniel Vetter66e985c2013-06-05 13:34:20 +020012521#define PIPE_CONF_CHECK_X(name) \
12522 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012523 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012524 "(expected 0x%08x, found 0x%08x)\n", \
12525 current_config->name, \
12526 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012527 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012528 }
12529
Daniel Vetter08a24032013-04-19 11:25:34 +020012530#define PIPE_CONF_CHECK_I(name) \
12531 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012532 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012533 "(expected %i, found %i)\n", \
12534 current_config->name, \
12535 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012536 ret = false; \
12537 }
12538
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012539#define PIPE_CONF_CHECK_P(name) \
12540 if (current_config->name != pipe_config->name) { \
12541 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12542 "(expected %p, found %p)\n", \
12543 current_config->name, \
12544 pipe_config->name); \
12545 ret = false; \
12546 }
12547
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012548#define PIPE_CONF_CHECK_M_N(name) \
12549 if (!intel_compare_link_m_n(&current_config->name, \
12550 &pipe_config->name,\
12551 adjust)) { \
12552 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12553 "(expected tu %i gmch %i/%i link %i/%i, " \
12554 "found tu %i, gmch %i/%i link %i/%i)\n", \
12555 current_config->name.tu, \
12556 current_config->name.gmch_m, \
12557 current_config->name.gmch_n, \
12558 current_config->name.link_m, \
12559 current_config->name.link_n, \
12560 pipe_config->name.tu, \
12561 pipe_config->name.gmch_m, \
12562 pipe_config->name.gmch_n, \
12563 pipe_config->name.link_m, \
12564 pipe_config->name.link_n); \
12565 ret = false; \
12566 }
12567
Daniel Vetter55c561a2016-03-30 11:34:36 +020012568/* This is required for BDW+ where there is only one set of registers for
12569 * switching between high and low RR.
12570 * This macro can be used whenever a comparison has to be made between one
12571 * hw state and multiple sw state variables.
12572 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012573#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12574 if (!intel_compare_link_m_n(&current_config->name, \
12575 &pipe_config->name, adjust) && \
12576 !intel_compare_link_m_n(&current_config->alt_name, \
12577 &pipe_config->name, adjust)) { \
12578 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12579 "(expected tu %i gmch %i/%i link %i/%i, " \
12580 "or tu %i gmch %i/%i link %i/%i, " \
12581 "found tu %i, gmch %i/%i link %i/%i)\n", \
12582 current_config->name.tu, \
12583 current_config->name.gmch_m, \
12584 current_config->name.gmch_n, \
12585 current_config->name.link_m, \
12586 current_config->name.link_n, \
12587 current_config->alt_name.tu, \
12588 current_config->alt_name.gmch_m, \
12589 current_config->alt_name.gmch_n, \
12590 current_config->alt_name.link_m, \
12591 current_config->alt_name.link_n, \
12592 pipe_config->name.tu, \
12593 pipe_config->name.gmch_m, \
12594 pipe_config->name.gmch_n, \
12595 pipe_config->name.link_m, \
12596 pipe_config->name.link_n); \
12597 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012598 }
12599
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012600#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12601 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012602 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012603 "(expected %i, found %i)\n", \
12604 current_config->name & (mask), \
12605 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012606 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012607 }
12608
Ville Syrjälä5e550652013-09-06 23:29:07 +030012609#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12610 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012611 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012612 "(expected %i, found %i)\n", \
12613 current_config->name, \
12614 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012615 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012616 }
12617
Daniel Vetterbb760062013-06-06 14:55:52 +020012618#define PIPE_CONF_QUIRK(quirk) \
12619 ((current_config->quirks | pipe_config->quirks) & (quirk))
12620
Daniel Vettereccb1402013-05-22 00:50:22 +020012621 PIPE_CONF_CHECK_I(cpu_transcoder);
12622
Daniel Vetter08a24032013-04-19 11:25:34 +020012623 PIPE_CONF_CHECK_I(has_pch_encoder);
12624 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012625 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012626
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012627 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012628 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012629
12630 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012631 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012632
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012633 if (current_config->has_drrs)
12634 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12635 } else
12636 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012637
Jani Nikulaa65347b2015-11-27 12:21:46 +020012638 PIPE_CONF_CHECK_I(has_dsi_encoder);
12639
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012640 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12641 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12642 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12643 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12644 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12645 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012646
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012647 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12648 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12649 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12650 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12651 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12652 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012653
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012654 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012655 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012656 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012657 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012658 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012659 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012660
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012661 PIPE_CONF_CHECK_I(has_audio);
12662
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012663 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012664 DRM_MODE_FLAG_INTERLACE);
12665
Daniel Vetterbb760062013-06-06 14:55:52 +020012666 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012667 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012668 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012669 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012670 DRM_MODE_FLAG_NHSYNC);
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_PVSYNC);
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_NVSYNC);
12675 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012676
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012677 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012678 /* pfit ratios are autocomputed by the hw on gen4+ */
12679 if (INTEL_INFO(dev)->gen < 4)
12680 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012681 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012682
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012683 if (!adjust) {
12684 PIPE_CONF_CHECK_I(pipe_src_w);
12685 PIPE_CONF_CHECK_I(pipe_src_h);
12686
12687 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12688 if (current_config->pch_pfit.enabled) {
12689 PIPE_CONF_CHECK_X(pch_pfit.pos);
12690 PIPE_CONF_CHECK_X(pch_pfit.size);
12691 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012692
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012693 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12694 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012695
Jesse Barnese59150d2014-01-07 13:30:45 -080012696 /* BDW+ don't expose a synchronous way to read the state */
12697 if (IS_HASWELL(dev))
12698 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012699
Ville Syrjälä282740f2013-09-04 18:30:03 +030012700 PIPE_CONF_CHECK_I(double_wide);
12701
Daniel Vetter26804af2014-06-25 22:01:55 +030012702 PIPE_CONF_CHECK_X(ddi_pll_sel);
12703
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012704 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012705 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012706 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012707 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12708 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012709 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012710 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012711 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12712 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12713 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012714
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012715 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12716 PIPE_CONF_CHECK_I(pipe_bpp);
12717
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012718 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012719 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012720
Daniel Vetter66e985c2013-06-05 13:34:20 +020012721#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012722#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012723#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012724#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012725#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012726#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012727#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012728
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012729 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012730}
12731
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012732static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12733 const struct intel_crtc_state *pipe_config)
12734{
12735 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012736 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012737 &pipe_config->fdi_m_n);
12738 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12739
12740 /*
12741 * FDI already provided one idea for the dotclock.
12742 * Yell if the encoder disagrees.
12743 */
12744 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12745 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12746 fdi_dotclock, dotclock);
12747 }
12748}
12749
Damien Lespiau08db6652014-11-04 17:06:52 +000012750static void check_wm_state(struct drm_device *dev)
12751{
12752 struct drm_i915_private *dev_priv = dev->dev_private;
12753 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12754 struct intel_crtc *intel_crtc;
12755 int plane;
12756
12757 if (INTEL_INFO(dev)->gen < 9)
12758 return;
12759
12760 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12761 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12762
12763 for_each_intel_crtc(dev, intel_crtc) {
12764 struct skl_ddb_entry *hw_entry, *sw_entry;
12765 const enum pipe pipe = intel_crtc->pipe;
12766
12767 if (!intel_crtc->active)
12768 continue;
12769
12770 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012771 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012772 hw_entry = &hw_ddb.plane[pipe][plane];
12773 sw_entry = &sw_ddb->plane[pipe][plane];
12774
12775 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12776 continue;
12777
12778 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12779 "(expected (%u,%u), found (%u,%u))\n",
12780 pipe_name(pipe), plane + 1,
12781 sw_entry->start, sw_entry->end,
12782 hw_entry->start, hw_entry->end);
12783 }
12784
12785 /* cursor */
Matt Roper4969d332015-09-24 15:53:10 -070012786 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12787 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
Damien Lespiau08db6652014-11-04 17:06:52 +000012788
12789 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12790 continue;
12791
12792 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12793 "(expected (%u,%u), found (%u,%u))\n",
12794 pipe_name(pipe),
12795 sw_entry->start, sw_entry->end,
12796 hw_entry->start, hw_entry->end);
12797 }
12798}
12799
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012800static void
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012801check_connector_state(struct drm_device *dev,
12802 struct drm_atomic_state *old_state)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012803{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012804 struct drm_connector_state *old_conn_state;
12805 struct drm_connector *connector;
12806 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012807
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012808 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12809 struct drm_encoder *encoder = connector->encoder;
12810 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012811
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012812 /* This also checks the encoder/connector hw state with the
12813 * ->get_hw_state callbacks. */
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012814 intel_connector_check_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012815
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012816 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012817 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012818 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012819}
12820
12821static void
12822check_encoder_state(struct drm_device *dev)
12823{
12824 struct intel_encoder *encoder;
12825 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012826
Damien Lespiaub2784e12014-08-05 11:29:37 +010012827 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012828 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012829 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012830
12831 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12832 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012833 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012834
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012835 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012836 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012837 continue;
12838 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012839
12840 I915_STATE_WARN(connector->base.state->crtc !=
12841 encoder->base.crtc,
12842 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012843 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012844
Rob Clarke2c719b2014-12-15 13:56:32 -050012845 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012846 "encoder's enabled state mismatch "
12847 "(expected %i, found %i)\n",
12848 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012849
12850 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012851 bool active;
12852
12853 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012854 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012855 "encoder detached but still enabled on pipe %c.\n",
12856 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012857 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012858 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012859}
12860
12861static void
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012862check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012863{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012864 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012865 struct intel_encoder *encoder;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012866 struct drm_crtc_state *old_crtc_state;
12867 struct drm_crtc *crtc;
12868 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012869
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012870 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12871 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12872 struct intel_crtc_state *pipe_config, *sw_config;
Maarten Lankhorst7b89b8d2015-08-05 12:37:03 +020012873 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012874
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012875 if (!needs_modeset(crtc->state) &&
12876 !to_intel_crtc_state(crtc->state)->update_pipe)
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012877 continue;
12878
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012879 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12880 pipe_config = to_intel_crtc_state(old_crtc_state);
12881 memset(pipe_config, 0, sizeof(*pipe_config));
12882 pipe_config->base.crtc = crtc;
12883 pipe_config->base.state = old_state;
12884
12885 DRM_DEBUG_KMS("[CRTC:%d]\n",
12886 crtc->base.id);
12887
12888 active = dev_priv->display.get_pipe_config(intel_crtc,
12889 pipe_config);
12890
12891 /* hw state is inconsistent with the pipe quirk */
12892 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12893 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12894 active = crtc->state->active;
12895
12896 I915_STATE_WARN(crtc->state->active != active,
12897 "crtc active state doesn't match with hw state "
12898 "(expected %i, found %i)\n", crtc->state->active, active);
12899
12900 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
12901 "transitional active state does not match atomic hw state "
12902 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12903
12904 for_each_encoder_on_crtc(dev, crtc, encoder) {
12905 enum pipe pipe;
12906
12907 active = encoder->get_hw_state(encoder, &pipe);
12908 I915_STATE_WARN(active != crtc->state->active,
12909 "[ENCODER:%i] active %i with crtc active %i\n",
12910 encoder->base.base.id, active, crtc->state->active);
12911
12912 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12913 "Encoder connected to wrong pipe %c\n",
12914 pipe_name(pipe));
12915
12916 if (active)
12917 encoder->get_config(encoder, pipe_config);
12918 }
12919
12920 if (!crtc->state->active)
12921 continue;
12922
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012923 intel_pipe_config_sanity_check(dev_priv, pipe_config);
12924
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012925 sw_config = to_intel_crtc_state(crtc->state);
12926 if (!intel_pipe_config_compare(dev, sw_config,
12927 pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012928 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012929 intel_dump_pipe_config(intel_crtc, pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012930 "[hw state]");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012931 intel_dump_pipe_config(intel_crtc, sw_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012932 "[sw state]");
12933 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012934 }
12935}
12936
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012937static void
12938check_shared_dpll_state(struct drm_device *dev)
12939{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012940 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012941 struct intel_crtc *crtc;
12942 struct intel_dpll_hw_state dpll_hw_state;
12943 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012944
12945 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012946 struct intel_shared_dpll *pll =
12947 intel_get_shared_dpll_by_id(dev_priv, i);
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012948 unsigned enabled_crtcs = 0, active_crtcs = 0;
Daniel Vetter53589012013-06-05 13:34:16 +020012949 bool active;
12950
12951 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12952
12953 DRM_DEBUG_KMS("%s\n", pll->name);
12954
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020012955 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020012956
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012957 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
12958 "more active pll users than references: %x vs %x\n",
12959 pll->active_mask, pll->config.crtc_mask);
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020012960
12961 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012962 I915_STATE_WARN(!pll->on && pll->active_mask,
12963 "pll in active use but not on in sw tracking\n");
12964 I915_STATE_WARN(pll->on && !pll->active_mask,
12965 "pll is on but not used by any active crtc\n");
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020012966 I915_STATE_WARN(pll->on != active,
12967 "pll on state mismatch (expected %i, found %i)\n",
12968 pll->on, active);
12969 }
Daniel Vetter53589012013-06-05 13:34:16 +020012970
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012971 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012972 if (crtc->base.state->enable && crtc->config->shared_dpll == pll)
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012973 enabled_crtcs |= 1 << drm_crtc_index(&crtc->base);
12974 if (crtc->base.state->active && crtc->config->shared_dpll == pll)
12975 active_crtcs |= 1 << drm_crtc_index(&crtc->base);
Daniel Vetter53589012013-06-05 13:34:16 +020012976 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010012977
12978 I915_STATE_WARN(pll->active_mask != active_crtcs,
12979 "pll active crtcs mismatch (expected %x, found %x)\n",
12980 pll->active_mask, active_crtcs);
12981 I915_STATE_WARN(pll->config.crtc_mask != enabled_crtcs,
12982 "pll enabled crtcs mismatch (expected %x, found %x)\n",
12983 pll->config.crtc_mask, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012984
Rob Clarke2c719b2014-12-15 13:56:32 -050012985 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012986 sizeof(dpll_hw_state)),
12987 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012988 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012989}
12990
Maarten Lankhorstee165b12015-08-05 12:37:00 +020012991static void
12992intel_modeset_check_state(struct drm_device *dev,
12993 struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012994{
Damien Lespiau08db6652014-11-04 17:06:52 +000012995 check_wm_state(dev);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012996 check_connector_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012997 check_encoder_state(dev);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012998 check_crtc_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012999 check_shared_dpll_state(dev);
13000}
13001
Ville Syrjälä80715b22014-05-15 20:23:23 +030013002static void update_scanline_offset(struct intel_crtc *crtc)
13003{
13004 struct drm_device *dev = crtc->base.dev;
13005
13006 /*
13007 * The scanline counter increments at the leading edge of hsync.
13008 *
13009 * On most platforms it starts counting from vtotal-1 on the
13010 * first active line. That means the scanline counter value is
13011 * always one less than what we would expect. Ie. just after
13012 * start of vblank, which also occurs at start of hsync (on the
13013 * last active line), the scanline counter will read vblank_start-1.
13014 *
13015 * On gen2 the scanline counter starts counting from 1 instead
13016 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13017 * to keep the value positive), instead of adding one.
13018 *
13019 * On HSW+ the behaviour of the scanline counter depends on the output
13020 * type. For DP ports it behaves like most other platforms, but on HDMI
13021 * there's an extra 1 line difference. So we need to add two instead of
13022 * one to the value.
13023 */
13024 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013025 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013026 int vtotal;
13027
Ville Syrjälä124abe02015-09-08 13:40:45 +030013028 vtotal = adjusted_mode->crtc_vtotal;
13029 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013030 vtotal /= 2;
13031
13032 crtc->scanline_offset = vtotal - 1;
13033 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013034 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013035 crtc->scanline_offset = 2;
13036 } else
13037 crtc->scanline_offset = 1;
13038}
13039
Maarten Lankhorstad421372015-06-15 12:33:42 +020013040static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013041{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013042 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013043 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013044 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013045 struct drm_crtc *crtc;
13046 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013047 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013048
13049 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013050 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013051
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013052 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013054 struct intel_shared_dpll *old_dpll =
13055 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013056
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013057 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013058 continue;
13059
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013060 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013061
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013062 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013063 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013064
Maarten Lankhorstad421372015-06-15 12:33:42 +020013065 if (!shared_dpll)
13066 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13067
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013068 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013069 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013070}
13071
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013072/*
13073 * This implements the workaround described in the "notes" section of the mode
13074 * set sequence documentation. When going from no pipes or single pipe to
13075 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13076 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13077 */
13078static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13079{
13080 struct drm_crtc_state *crtc_state;
13081 struct intel_crtc *intel_crtc;
13082 struct drm_crtc *crtc;
13083 struct intel_crtc_state *first_crtc_state = NULL;
13084 struct intel_crtc_state *other_crtc_state = NULL;
13085 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13086 int i;
13087
13088 /* look at all crtc's that are going to be enabled in during modeset */
13089 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13090 intel_crtc = to_intel_crtc(crtc);
13091
13092 if (!crtc_state->active || !needs_modeset(crtc_state))
13093 continue;
13094
13095 if (first_crtc_state) {
13096 other_crtc_state = to_intel_crtc_state(crtc_state);
13097 break;
13098 } else {
13099 first_crtc_state = to_intel_crtc_state(crtc_state);
13100 first_pipe = intel_crtc->pipe;
13101 }
13102 }
13103
13104 /* No workaround needed? */
13105 if (!first_crtc_state)
13106 return 0;
13107
13108 /* w/a possibly needed, check how many crtc's are already enabled. */
13109 for_each_intel_crtc(state->dev, intel_crtc) {
13110 struct intel_crtc_state *pipe_config;
13111
13112 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13113 if (IS_ERR(pipe_config))
13114 return PTR_ERR(pipe_config);
13115
13116 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13117
13118 if (!pipe_config->base.active ||
13119 needs_modeset(&pipe_config->base))
13120 continue;
13121
13122 /* 2 or more enabled crtcs means no need for w/a */
13123 if (enabled_pipe != INVALID_PIPE)
13124 return 0;
13125
13126 enabled_pipe = intel_crtc->pipe;
13127 }
13128
13129 if (enabled_pipe != INVALID_PIPE)
13130 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13131 else if (other_crtc_state)
13132 other_crtc_state->hsw_workaround_pipe = first_pipe;
13133
13134 return 0;
13135}
13136
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013137static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13138{
13139 struct drm_crtc *crtc;
13140 struct drm_crtc_state *crtc_state;
13141 int ret = 0;
13142
13143 /* add all active pipes to the state */
13144 for_each_crtc(state->dev, crtc) {
13145 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13146 if (IS_ERR(crtc_state))
13147 return PTR_ERR(crtc_state);
13148
13149 if (!crtc_state->active || needs_modeset(crtc_state))
13150 continue;
13151
13152 crtc_state->mode_changed = true;
13153
13154 ret = drm_atomic_add_affected_connectors(state, crtc);
13155 if (ret)
13156 break;
13157
13158 ret = drm_atomic_add_affected_planes(state, crtc);
13159 if (ret)
13160 break;
13161 }
13162
13163 return ret;
13164}
13165
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013166static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013167{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013168 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13169 struct drm_i915_private *dev_priv = state->dev->dev_private;
13170 struct drm_crtc *crtc;
13171 struct drm_crtc_state *crtc_state;
13172 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013173
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013174 if (!check_digital_port_conflicts(state)) {
13175 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13176 return -EINVAL;
13177 }
13178
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013179 intel_state->modeset = true;
13180 intel_state->active_crtcs = dev_priv->active_crtcs;
13181
13182 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13183 if (crtc_state->active)
13184 intel_state->active_crtcs |= 1 << i;
13185 else
13186 intel_state->active_crtcs &= ~(1 << i);
13187 }
13188
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013189 /*
13190 * See if the config requires any additional preparation, e.g.
13191 * to adjust global state with pipes off. We need to do this
13192 * here so we can get the modeset_pipe updated config for the new
13193 * mode set on this crtc. For other crtcs we need to use the
13194 * adjusted_mode bits in the crtc directly.
13195 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013196 if (dev_priv->display.modeset_calc_cdclk) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013197 ret = dev_priv->display.modeset_calc_cdclk(state);
13198
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013199 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013200 ret = intel_modeset_all_pipes(state);
13201
13202 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013203 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013204
13205 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13206 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013207 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013208 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013209
Maarten Lankhorstad421372015-06-15 12:33:42 +020013210 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013211
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013212 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013213 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013214
Maarten Lankhorstad421372015-06-15 12:33:42 +020013215 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013216}
13217
Matt Roperaa363132015-09-24 15:53:18 -070013218/*
13219 * Handle calculation of various watermark data at the end of the atomic check
13220 * phase. The code here should be run after the per-crtc and per-plane 'check'
13221 * handlers to ensure that all derived state has been updated.
13222 */
13223static void calc_watermark_data(struct drm_atomic_state *state)
13224{
13225 struct drm_device *dev = state->dev;
13226 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13227 struct drm_crtc *crtc;
13228 struct drm_crtc_state *cstate;
13229 struct drm_plane *plane;
13230 struct drm_plane_state *pstate;
13231
13232 /*
13233 * Calculate watermark configuration details now that derived
13234 * plane/crtc state is all properly updated.
13235 */
13236 drm_for_each_crtc(crtc, dev) {
13237 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13238 crtc->state;
13239
13240 if (cstate->active)
13241 intel_state->wm_config.num_pipes_active++;
13242 }
13243 drm_for_each_legacy_plane(plane, dev) {
13244 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13245 plane->state;
13246
13247 if (!to_intel_plane_state(pstate)->visible)
13248 continue;
13249
13250 intel_state->wm_config.sprites_enabled = true;
13251 if (pstate->crtc_w != pstate->src_w >> 16 ||
13252 pstate->crtc_h != pstate->src_h >> 16)
13253 intel_state->wm_config.sprites_scaled = true;
13254 }
13255}
13256
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013257/**
13258 * intel_atomic_check - validate state object
13259 * @dev: drm device
13260 * @state: state to validate
13261 */
13262static int intel_atomic_check(struct drm_device *dev,
13263 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013264{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013265 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013266 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013267 struct drm_crtc *crtc;
13268 struct drm_crtc_state *crtc_state;
13269 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013270 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013271
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013272 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013273 if (ret)
13274 return ret;
13275
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013276 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013277 struct intel_crtc_state *pipe_config =
13278 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013279
13280 /* Catch I915_MODE_FLAG_INHERITED */
13281 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13282 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013283
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013284 if (!crtc_state->enable) {
13285 if (needs_modeset(crtc_state))
13286 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013287 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013288 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013289
Daniel Vetter26495482015-07-15 14:15:52 +020013290 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013291 continue;
13292
Daniel Vetter26495482015-07-15 14:15:52 +020013293 /* FIXME: For only active_changed we shouldn't need to do any
13294 * state recomputation at all. */
13295
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013296 ret = drm_atomic_add_affected_connectors(state, crtc);
13297 if (ret)
13298 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013299
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013300 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013301 if (ret)
13302 return ret;
13303
Jani Nikula73831232015-11-19 10:26:30 +020013304 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013305 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013306 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013307 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013308 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013309 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013310 }
13311
13312 if (needs_modeset(crtc_state)) {
13313 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013314
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013315 ret = drm_atomic_add_affected_planes(state, crtc);
13316 if (ret)
13317 return ret;
13318 }
13319
Daniel Vetter26495482015-07-15 14:15:52 +020013320 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13321 needs_modeset(crtc_state) ?
13322 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013323 }
13324
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013325 if (any_ms) {
13326 ret = intel_modeset_checks(state);
13327
13328 if (ret)
13329 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013330 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013331 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013332
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013333 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013334 if (ret)
13335 return ret;
13336
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013337 intel_fbc_choose_crtc(dev_priv, state);
Matt Roperaa363132015-09-24 15:53:18 -070013338 calc_watermark_data(state);
13339
13340 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013341}
13342
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013343static int intel_atomic_prepare_commit(struct drm_device *dev,
13344 struct drm_atomic_state *state,
13345 bool async)
13346{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013347 struct drm_i915_private *dev_priv = dev->dev_private;
13348 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013349 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013350 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013351 struct drm_crtc *crtc;
13352 int i, ret;
13353
13354 if (async) {
13355 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13356 return -EINVAL;
13357 }
13358
13359 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13360 ret = intel_crtc_wait_for_pending_flips(crtc);
13361 if (ret)
13362 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013363
13364 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13365 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013366 }
13367
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013368 ret = mutex_lock_interruptible(&dev->struct_mutex);
13369 if (ret)
13370 return ret;
13371
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013372 ret = drm_atomic_helper_prepare_planes(dev, state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013373 if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
13374 u32 reset_counter;
13375
13376 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
13377 mutex_unlock(&dev->struct_mutex);
13378
13379 for_each_plane_in_state(state, plane, plane_state, i) {
13380 struct intel_plane_state *intel_plane_state =
13381 to_intel_plane_state(plane_state);
13382
13383 if (!intel_plane_state->wait_req)
13384 continue;
13385
13386 ret = __i915_wait_request(intel_plane_state->wait_req,
13387 reset_counter, true,
13388 NULL, NULL);
13389
13390 /* Swallow -EIO errors to allow updates during hw lockup. */
13391 if (ret == -EIO)
13392 ret = 0;
13393
13394 if (ret)
13395 break;
13396 }
13397
13398 if (!ret)
13399 return 0;
13400
13401 mutex_lock(&dev->struct_mutex);
13402 drm_atomic_helper_cleanup_planes(dev, state);
13403 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013404
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013405 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013406 return ret;
13407}
13408
Maarten Lankhorste8861672016-02-24 11:24:26 +010013409static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13410 struct drm_i915_private *dev_priv,
13411 unsigned crtc_mask)
13412{
13413 unsigned last_vblank_count[I915_MAX_PIPES];
13414 enum pipe pipe;
13415 int ret;
13416
13417 if (!crtc_mask)
13418 return;
13419
13420 for_each_pipe(dev_priv, pipe) {
13421 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13422
13423 if (!((1 << pipe) & crtc_mask))
13424 continue;
13425
13426 ret = drm_crtc_vblank_get(crtc);
13427 if (WARN_ON(ret != 0)) {
13428 crtc_mask &= ~(1 << pipe);
13429 continue;
13430 }
13431
13432 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13433 }
13434
13435 for_each_pipe(dev_priv, pipe) {
13436 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13437 long lret;
13438
13439 if (!((1 << pipe) & crtc_mask))
13440 continue;
13441
13442 lret = wait_event_timeout(dev->vblank[pipe].queue,
13443 last_vblank_count[pipe] !=
13444 drm_crtc_vblank_count(crtc),
13445 msecs_to_jiffies(50));
13446
13447 WARN_ON(!lret);
13448
13449 drm_crtc_vblank_put(crtc);
13450 }
13451}
13452
13453static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13454{
13455 /* fb updated, need to unpin old fb */
13456 if (crtc_state->fb_changed)
13457 return true;
13458
13459 /* wm changes, need vblank before final wm's */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013460 if (crtc_state->update_wm_post)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013461 return true;
13462
13463 /*
13464 * cxsr is re-enabled after vblank.
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013465 * This is already handled by crtc_state->update_wm_post,
Maarten Lankhorste8861672016-02-24 11:24:26 +010013466 * but added for clarity.
13467 */
13468 if (crtc_state->disable_cxsr)
13469 return true;
13470
13471 return false;
13472}
13473
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013474/**
13475 * intel_atomic_commit - commit validated state object
13476 * @dev: DRM device
13477 * @state: the top-level driver state object
13478 * @async: asynchronous commit
13479 *
13480 * This function commits a top-level state object that has been validated
13481 * with drm_atomic_helper_check().
13482 *
13483 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13484 * we can only handle plane-related operations and do not yet support
13485 * asynchronous commit.
13486 *
13487 * RETURNS
13488 * Zero for success or -errno.
13489 */
13490static int intel_atomic_commit(struct drm_device *dev,
13491 struct drm_atomic_state *state,
13492 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013493{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013494 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013495 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013496 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013497 struct drm_crtc *crtc;
Matt Ropered4a6a72016-02-23 17:20:13 -080013498 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013499 int ret = 0, i;
13500 bool hw_check = intel_state->modeset;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013501 unsigned long put_domains[I915_MAX_PIPES] = {};
Maarten Lankhorste8861672016-02-24 11:24:26 +010013502 unsigned crtc_vblank_mask = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013503
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013504 ret = intel_atomic_prepare_commit(dev, state, async);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013505 if (ret) {
13506 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013507 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013508 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013509
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013510 drm_atomic_helper_swap_state(dev, state);
Maarten Lankhorsta1475e72016-03-14 09:27:53 +010013511 dev_priv->wm.config = intel_state->wm_config;
13512 intel_shared_dpll_commit(state);
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013513
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013514 if (intel_state->modeset) {
13515 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13516 sizeof(intel_state->min_pixclk));
13517 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013518 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013519
13520 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013521 }
13522
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013523 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013524 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13525
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013526 if (needs_modeset(crtc->state) ||
13527 to_intel_crtc_state(crtc->state)->update_pipe) {
13528 hw_check = true;
13529
13530 put_domains[to_intel_crtc(crtc)->pipe] =
13531 modeset_get_crtc_power_domains(crtc,
13532 to_intel_crtc_state(crtc->state));
13533 }
13534
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013535 if (!needs_modeset(crtc->state))
13536 continue;
13537
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013538 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013539
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013540 if (old_crtc_state->active) {
13541 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013542 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013543 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013544 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013545 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013546
13547 /*
13548 * Underruns don't always raise
13549 * interrupts, so check manually.
13550 */
13551 intel_check_cpu_fifo_underruns(dev_priv);
13552 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013553
13554 if (!crtc->state->active)
13555 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013556 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013557 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013558
Daniel Vetterea9d7582012-07-10 10:42:52 +020013559 /* Only after disabling all output pipelines that will be changed can we
13560 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013561 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013562
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013563 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013564 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013565
13566 if (dev_priv->display.modeset_commit_cdclk &&
13567 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13568 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013569 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013570
Daniel Vettera6778b32012-07-02 09:56:42 +020013571 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013572 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013573 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13574 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorste8861672016-02-24 11:24:26 +010013575 struct intel_crtc_state *pipe_config =
13576 to_intel_crtc_state(crtc->state);
13577 bool update_pipe = !modeset && pipe_config->update_pipe;
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013578
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013579 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013580 update_scanline_offset(to_intel_crtc(crtc));
13581 dev_priv->display.crtc_enable(crtc);
13582 }
13583
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013584 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013585 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013586
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010013587 if (crtc->state->active &&
13588 drm_atomic_get_existing_plane_state(state, crtc->primary))
Paulo Zanoni49227c42016-01-19 11:35:52 -020013589 intel_fbc_enable(intel_crtc);
13590
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013591 if (crtc->state->active &&
13592 (crtc->state->planes_changed || update_pipe))
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013593 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013594
Maarten Lankhorste8861672016-02-24 11:24:26 +010013595 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13596 crtc_vblank_mask |= 1 << i;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013597 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013598
Daniel Vettera6778b32012-07-02 09:56:42 +020013599 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013600
Maarten Lankhorste8861672016-02-24 11:24:26 +010013601 if (!state->legacy_cursor_update)
13602 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013603
Matt Ropered4a6a72016-02-23 17:20:13 -080013604 /*
13605 * Now that the vblank has passed, we can go ahead and program the
13606 * optimal watermarks on platforms that need two-step watermark
13607 * programming.
13608 *
13609 * TODO: Move this (and other cleanup) to an async worker eventually.
13610 */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013611 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Matt Ropered4a6a72016-02-23 17:20:13 -080013612 intel_cstate = to_intel_crtc_state(crtc->state);
13613
13614 if (dev_priv->display.optimize_watermarks)
13615 dev_priv->display.optimize_watermarks(intel_cstate);
13616 }
13617
Matt Roper177246a2016-03-04 15:59:39 -080013618 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13619 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13620
13621 if (put_domains[i])
13622 modeset_put_power_domains(dev_priv, put_domains[i]);
13623 }
13624
13625 if (intel_state->modeset)
13626 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13627
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013628 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013629 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013630 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013631
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013632 if (hw_check)
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013633 intel_modeset_check_state(dev, state);
13634
13635 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013636
Mika Kuoppala75714942015-12-16 09:26:48 +020013637 /* As one of the primary mmio accessors, KMS has a high likelihood
13638 * of triggering bugs in unclaimed access. After we finish
13639 * modesetting, see if an error has been flagged, and if so
13640 * enable debugging for the next modeset - and hope we catch
13641 * the culprit.
13642 *
13643 * XXX note that we assume display power is on at this point.
13644 * This might hold true now but we need to add pm helper to check
13645 * unclaimed only when the hardware is on, as atomic commits
13646 * can happen also when the device is completely off.
13647 */
13648 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13649
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013650 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013651}
13652
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013653void intel_crtc_restore_mode(struct drm_crtc *crtc)
13654{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013655 struct drm_device *dev = crtc->dev;
13656 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013657 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013658 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013659
13660 state = drm_atomic_state_alloc(dev);
13661 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013662 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013663 crtc->base.id);
13664 return;
13665 }
13666
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013667 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013668
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013669retry:
13670 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13671 ret = PTR_ERR_OR_ZERO(crtc_state);
13672 if (!ret) {
13673 if (!crtc_state->active)
13674 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013675
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013676 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013677 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013678 }
13679
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013680 if (ret == -EDEADLK) {
13681 drm_atomic_state_clear(state);
13682 drm_modeset_backoff(state->acquire_ctx);
13683 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013684 }
13685
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013686 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013687out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013688 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013689}
13690
Daniel Vetter25c5b262012-07-08 22:08:04 +020013691#undef for_each_intel_crtc_masked
13692
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013693static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013694 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013695 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013696 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013697 .destroy = intel_crtc_destroy,
13698 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013699 .atomic_duplicate_state = intel_crtc_duplicate_state,
13700 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013701};
13702
Matt Roper6beb8c232014-12-01 15:40:14 -080013703/**
13704 * intel_prepare_plane_fb - Prepare fb for usage on plane
13705 * @plane: drm plane to prepare for
13706 * @fb: framebuffer to prepare for presentation
13707 *
13708 * Prepares a framebuffer for usage on a display plane. Generally this
13709 * involves pinning the underlying object and updating the frontbuffer tracking
13710 * bits. Some older platforms need special physical address handling for
13711 * cursor planes.
13712 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013713 * Must be called with struct_mutex held.
13714 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013715 * Returns 0 on success, negative error code on failure.
13716 */
13717int
13718intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013719 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013720{
13721 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013722 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013723 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013724 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013725 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013726 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013727
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013728 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013729 return 0;
13730
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013731 if (old_obj) {
13732 struct drm_crtc_state *crtc_state =
13733 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13734
13735 /* Big Hammer, we also need to ensure that any pending
13736 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13737 * current scanout is retired before unpinning the old
13738 * framebuffer. Note that we rely on userspace rendering
13739 * into the buffer attached to the pipe they are waiting
13740 * on. If not, userspace generates a GPU hang with IPEHR
13741 * point to the MI_WAIT_FOR_EVENT.
13742 *
13743 * This should only fail upon a hung GPU, in which case we
13744 * can safely continue.
13745 */
13746 if (needs_modeset(crtc_state))
13747 ret = i915_gem_object_wait_rendering(old_obj, true);
13748
13749 /* Swallow -EIO errors to allow updates during hw lockup. */
13750 if (ret && ret != -EIO)
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013751 return ret;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013752 }
13753
Alex Goins3c28ff22015-11-25 18:43:39 -080013754 /* For framebuffer backed by dmabuf, wait for fence */
13755 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013756 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080013757
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013758 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13759 false, true,
13760 MAX_SCHEDULE_TIMEOUT);
13761 if (lret == -ERESTARTSYS)
13762 return lret;
13763
13764 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080013765 }
13766
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013767 if (!obj) {
13768 ret = 0;
13769 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013770 INTEL_INFO(dev)->cursor_needs_physical) {
13771 int align = IS_I830(dev) ? 16 * 1024 : 256;
13772 ret = i915_gem_object_attach_phys(obj, align);
13773 if (ret)
13774 DRM_DEBUG_KMS("failed to attach phys object\n");
13775 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020013776 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080013777 }
13778
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013779 if (ret == 0) {
13780 if (obj) {
13781 struct intel_plane_state *plane_state =
13782 to_intel_plane_state(new_state);
13783
13784 i915_gem_request_assign(&plane_state->wait_req,
13785 obj->last_write_req);
13786 }
13787
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013788 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013789 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013790
Matt Roper6beb8c232014-12-01 15:40:14 -080013791 return ret;
13792}
13793
Matt Roper38f3ce32014-12-02 07:45:25 -080013794/**
13795 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13796 * @plane: drm plane to clean up for
13797 * @fb: old framebuffer that was on plane
13798 *
13799 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013800 *
13801 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013802 */
13803void
13804intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013805 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013806{
13807 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013808 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013809 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013810 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13811 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013812
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013813 old_intel_state = to_intel_plane_state(old_state);
13814
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013815 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013816 return;
13817
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013818 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13819 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020013820 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013821
13822 /* prepare_fb aborted? */
13823 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13824 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13825 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013826
13827 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070013828}
13829
Chandra Konduru6156a452015-04-27 13:48:39 -070013830int
13831skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13832{
13833 int max_scale;
13834 struct drm_device *dev;
13835 struct drm_i915_private *dev_priv;
13836 int crtc_clock, cdclk;
13837
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013838 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013839 return DRM_PLANE_HELPER_NO_SCALING;
13840
13841 dev = intel_crtc->base.dev;
13842 dev_priv = dev->dev_private;
13843 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013844 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013845
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010013846 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013847 return DRM_PLANE_HELPER_NO_SCALING;
13848
13849 /*
13850 * skl max scale is lower of:
13851 * close to 3 but not 3, -1 is for that purpose
13852 * or
13853 * cdclk/crtc_clock
13854 */
13855 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13856
13857 return max_scale;
13858}
13859
Matt Roper465c1202014-05-29 08:06:54 -070013860static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013861intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013862 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013863 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013864{
Matt Roper2b875c22014-12-01 15:40:13 -080013865 struct drm_crtc *crtc = state->base.crtc;
13866 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013867 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013868 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13869 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013870
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013871 if (INTEL_INFO(plane->dev)->gen >= 9) {
13872 /* use scaler when colorkey is not required */
13873 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13874 min_scale = 1;
13875 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13876 }
Sonika Jindald8106362015-04-10 14:37:28 +053013877 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013878 }
Sonika Jindald8106362015-04-10 14:37:28 +053013879
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013880 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13881 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013882 min_scale, max_scale,
13883 can_position, true,
13884 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013885}
13886
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013887static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13888 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013889{
13890 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080013891 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013892 struct intel_crtc_state *old_intel_state =
13893 to_intel_crtc_state(old_crtc_state);
13894 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013895
Matt Roperc34c9ee2014-12-23 10:41:50 -080013896 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020013897 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013898
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013899 if (modeset)
13900 return;
13901
Maarten Lankhorst20a34e72016-03-30 17:16:36 +020013902 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
13903 intel_color_set_csc(crtc->state);
13904 intel_color_load_luts(crtc->state);
13905 }
13906
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013907 if (to_intel_crtc_state(crtc->state)->update_pipe)
13908 intel_update_pipe_config(intel_crtc, old_intel_state);
13909 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020013910 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013911}
13912
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013913static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13914 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013915{
Matt Roper32b7eee2014-12-24 07:59:06 -080013916 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013917
Maarten Lankhorst62852622015-09-23 16:29:38 +020013918 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013919}
13920
Matt Ropercf4c7c12014-12-04 10:27:42 -080013921/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013922 * intel_plane_destroy - destroy a plane
13923 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013924 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013925 * Common destruction function for all types of planes (primary, cursor,
13926 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013927 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013928void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013929{
13930 struct intel_plane *intel_plane = to_intel_plane(plane);
13931 drm_plane_cleanup(plane);
13932 kfree(intel_plane);
13933}
13934
Matt Roper65a3fea2015-01-21 16:35:42 -080013935const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013936 .update_plane = drm_atomic_helper_update_plane,
13937 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013938 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013939 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013940 .atomic_get_property = intel_plane_atomic_get_property,
13941 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013942 .atomic_duplicate_state = intel_plane_duplicate_state,
13943 .atomic_destroy_state = intel_plane_destroy_state,
13944
Matt Roper465c1202014-05-29 08:06:54 -070013945};
13946
13947static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13948 int pipe)
13949{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013950 struct intel_plane *primary = NULL;
13951 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013952 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020013953 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013954 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070013955
13956 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013957 if (!primary)
13958 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070013959
Matt Roper8e7d6882015-01-21 16:35:41 -080013960 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013961 if (!state)
13962 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080013963 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013964
Matt Roper465c1202014-05-29 08:06:54 -070013965 primary->can_scale = false;
13966 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013967 if (INTEL_INFO(dev)->gen >= 9) {
13968 primary->can_scale = true;
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070013969 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013970 }
Matt Roper465c1202014-05-29 08:06:54 -070013971 primary->pipe = pipe;
13972 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013973 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013974 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013975 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13976 primary->plane = !pipe;
13977
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013978 if (INTEL_INFO(dev)->gen >= 9) {
13979 intel_primary_formats = skl_primary_formats;
13980 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013981
13982 primary->update_plane = skylake_update_primary_plane;
13983 primary->disable_plane = skylake_disable_primary_plane;
13984 } else if (HAS_PCH_SPLIT(dev)) {
13985 intel_primary_formats = i965_primary_formats;
13986 num_formats = ARRAY_SIZE(i965_primary_formats);
13987
13988 primary->update_plane = ironlake_update_primary_plane;
13989 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013990 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013991 intel_primary_formats = i965_primary_formats;
13992 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013993
13994 primary->update_plane = i9xx_update_primary_plane;
13995 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013996 } else {
13997 intel_primary_formats = i8xx_primary_formats;
13998 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010013999
14000 primary->update_plane = i9xx_update_primary_plane;
14001 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014002 }
14003
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014004 ret = drm_universal_plane_init(dev, &primary->base, 0,
14005 &intel_plane_funcs,
14006 intel_primary_formats, num_formats,
14007 DRM_PLANE_TYPE_PRIMARY, NULL);
14008 if (ret)
14009 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053014010
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014011 if (INTEL_INFO(dev)->gen >= 4)
14012 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014013
Matt Roperea2c67b2014-12-23 10:41:52 -080014014 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14015
Matt Roper465c1202014-05-29 08:06:54 -070014016 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014017
14018fail:
14019 kfree(state);
14020 kfree(primary);
14021
14022 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014023}
14024
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014025void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14026{
14027 if (!dev->mode_config.rotation_property) {
14028 unsigned long flags = BIT(DRM_ROTATE_0) |
14029 BIT(DRM_ROTATE_180);
14030
14031 if (INTEL_INFO(dev)->gen >= 9)
14032 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14033
14034 dev->mode_config.rotation_property =
14035 drm_mode_create_rotation_property(dev, flags);
14036 }
14037 if (dev->mode_config.rotation_property)
14038 drm_object_attach_property(&plane->base.base,
14039 dev->mode_config.rotation_property,
14040 plane->base.state->rotation);
14041}
14042
Matt Roper3d7d6512014-06-10 08:28:13 -070014043static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014044intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014045 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014046 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014047{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014048 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014049 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014050 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014051 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014052 unsigned stride;
14053 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014054
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014055 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14056 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014057 DRM_PLANE_HELPER_NO_SCALING,
14058 DRM_PLANE_HELPER_NO_SCALING,
14059 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014060 if (ret)
14061 return ret;
14062
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014063 /* if we want to turn off the cursor ignore width and height */
14064 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014065 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014066
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014067 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014068 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014069 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14070 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014071 return -EINVAL;
14072 }
14073
Matt Roperea2c67b2014-12-23 10:41:52 -080014074 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14075 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014076 DRM_DEBUG_KMS("buffer is too small\n");
14077 return -ENOMEM;
14078 }
14079
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014080 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014081 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014082 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014083 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014084
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014085 /*
14086 * There's something wrong with the cursor on CHV pipe C.
14087 * If it straddles the left edge of the screen then
14088 * moving it away from the edge or disabling it often
14089 * results in a pipe underrun, and often that can lead to
14090 * dead pipe (constant underrun reported, and it scans
14091 * out just a solid color). To recover from that, the
14092 * display power well must be turned off and on again.
14093 * Refuse the put the cursor into that compromised position.
14094 */
14095 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14096 state->visible && state->base.crtc_x < 0) {
14097 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14098 return -EINVAL;
14099 }
14100
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014101 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014102}
14103
Matt Roperf4a2cf22014-12-01 15:40:12 -080014104static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014105intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014106 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014107{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14109
14110 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014111 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014112}
14113
14114static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014115intel_update_cursor_plane(struct drm_plane *plane,
14116 const struct intel_crtc_state *crtc_state,
14117 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014118{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014119 struct drm_crtc *crtc = crtc_state->base.crtc;
14120 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014121 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014122 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014123 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014124
Matt Roperf4a2cf22014-12-01 15:40:12 -080014125 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014126 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014127 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014128 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014129 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014130 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014131
Gustavo Padovana912f122014-12-01 15:40:10 -080014132 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014133 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014134}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014135
Matt Roper3d7d6512014-06-10 08:28:13 -070014136static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14137 int pipe)
14138{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014139 struct intel_plane *cursor = NULL;
14140 struct intel_plane_state *state = NULL;
14141 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070014142
14143 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014144 if (!cursor)
14145 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070014146
Matt Roper8e7d6882015-01-21 16:35:41 -080014147 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014148 if (!state)
14149 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014150 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014151
Matt Roper3d7d6512014-06-10 08:28:13 -070014152 cursor->can_scale = false;
14153 cursor->max_downscale = 1;
14154 cursor->pipe = pipe;
14155 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014156 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014157 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014158 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014159 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014160
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014161 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14162 &intel_plane_funcs,
14163 intel_cursor_formats,
14164 ARRAY_SIZE(intel_cursor_formats),
14165 DRM_PLANE_TYPE_CURSOR, NULL);
14166 if (ret)
14167 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014168
14169 if (INTEL_INFO(dev)->gen >= 4) {
14170 if (!dev->mode_config.rotation_property)
14171 dev->mode_config.rotation_property =
14172 drm_mode_create_rotation_property(dev,
14173 BIT(DRM_ROTATE_0) |
14174 BIT(DRM_ROTATE_180));
14175 if (dev->mode_config.rotation_property)
14176 drm_object_attach_property(&cursor->base.base,
14177 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014178 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014179 }
14180
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070014181 if (INTEL_INFO(dev)->gen >=9)
14182 state->scaler_id = -1;
14183
Matt Roperea2c67b2014-12-23 10:41:52 -080014184 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14185
Matt Roper3d7d6512014-06-10 08:28:13 -070014186 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014187
14188fail:
14189 kfree(state);
14190 kfree(cursor);
14191
14192 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014193}
14194
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014195static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14196 struct intel_crtc_state *crtc_state)
14197{
14198 int i;
14199 struct intel_scaler *intel_scaler;
14200 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14201
14202 for (i = 0; i < intel_crtc->num_scalers; i++) {
14203 intel_scaler = &scaler_state->scalers[i];
14204 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014205 intel_scaler->mode = PS_SCALER_MODE_DYN;
14206 }
14207
14208 scaler_state->scaler_id = -1;
14209}
14210
Hannes Ederb358d0a2008-12-18 21:18:47 +010014211static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014212{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014213 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014214 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014215 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014216 struct drm_plane *primary = NULL;
14217 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014218 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014219
Daniel Vetter955382f2013-09-19 14:05:45 +020014220 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014221 if (intel_crtc == NULL)
14222 return;
14223
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014224 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14225 if (!crtc_state)
14226 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014227 intel_crtc->config = crtc_state;
14228 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014229 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014230
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014231 /* initialize shared scalers */
14232 if (INTEL_INFO(dev)->gen >= 9) {
14233 if (pipe == PIPE_C)
14234 intel_crtc->num_scalers = 1;
14235 else
14236 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14237
14238 skl_init_scalers(dev, intel_crtc, crtc_state);
14239 }
14240
Matt Roper465c1202014-05-29 08:06:54 -070014241 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014242 if (!primary)
14243 goto fail;
14244
14245 cursor = intel_cursor_plane_create(dev, pipe);
14246 if (!cursor)
14247 goto fail;
14248
Matt Roper465c1202014-05-29 08:06:54 -070014249 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020014250 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070014251 if (ret)
14252 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014253
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014254 /*
14255 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014256 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014257 */
Jesse Barnes80824002009-09-10 15:28:06 -070014258 intel_crtc->pipe = pipe;
14259 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014260 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014261 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014262 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014263 }
14264
Chris Wilson4b0e3332014-05-30 16:35:26 +030014265 intel_crtc->cursor_base = ~0;
14266 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014267 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014268
Ville Syrjälä852eb002015-06-24 22:00:07 +030014269 intel_crtc->wm.cxsr_allowed = true;
14270
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014271 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14272 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14273 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14274 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14275
Jesse Barnes79e53942008-11-07 14:24:08 -080014276 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014277
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014278 intel_color_init(&intel_crtc->base);
14279
Daniel Vetter87b6b102014-05-15 15:33:46 +020014280 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014281 return;
14282
14283fail:
14284 if (primary)
14285 drm_plane_cleanup(primary);
14286 if (cursor)
14287 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014288 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014289 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014290}
14291
Jesse Barnes752aa882013-10-31 18:55:49 +020014292enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14293{
14294 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014295 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014296
Rob Clark51fd3712013-11-19 12:10:12 -050014297 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014298
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014299 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014300 return INVALID_PIPE;
14301
14302 return to_intel_crtc(encoder->crtc)->pipe;
14303}
14304
Carl Worth08d7b3d2009-04-29 14:43:54 -070014305int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014306 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014307{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014308 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014309 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014310 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014311
Rob Clark7707e652014-07-17 23:30:04 -040014312 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014313
Rob Clark7707e652014-07-17 23:30:04 -040014314 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014315 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014316 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014317 }
14318
Rob Clark7707e652014-07-17 23:30:04 -040014319 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014320 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014321
Daniel Vetterc05422d2009-08-11 16:05:30 +020014322 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014323}
14324
Daniel Vetter66a92782012-07-12 20:08:18 +020014325static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014326{
Daniel Vetter66a92782012-07-12 20:08:18 +020014327 struct drm_device *dev = encoder->base.dev;
14328 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014329 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014330 int entry = 0;
14331
Damien Lespiaub2784e12014-08-05 11:29:37 +010014332 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014333 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014334 index_mask |= (1 << entry);
14335
Jesse Barnes79e53942008-11-07 14:24:08 -080014336 entry++;
14337 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014338
Jesse Barnes79e53942008-11-07 14:24:08 -080014339 return index_mask;
14340}
14341
Chris Wilson4d302442010-12-14 19:21:29 +000014342static bool has_edp_a(struct drm_device *dev)
14343{
14344 struct drm_i915_private *dev_priv = dev->dev_private;
14345
14346 if (!IS_MOBILE(dev))
14347 return false;
14348
14349 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14350 return false;
14351
Damien Lespiaue3589902014-02-07 19:12:50 +000014352 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014353 return false;
14354
14355 return true;
14356}
14357
Jesse Barnes84b4e042014-06-25 08:24:29 -070014358static bool intel_crt_present(struct drm_device *dev)
14359{
14360 struct drm_i915_private *dev_priv = dev->dev_private;
14361
Damien Lespiau884497e2013-12-03 13:56:23 +000014362 if (INTEL_INFO(dev)->gen >= 9)
14363 return false;
14364
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014365 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014366 return false;
14367
14368 if (IS_CHERRYVIEW(dev))
14369 return false;
14370
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014371 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14372 return false;
14373
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014374 /* DDI E can't be used if DDI A requires 4 lanes */
14375 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14376 return false;
14377
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014378 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014379 return false;
14380
14381 return true;
14382}
14383
Jesse Barnes79e53942008-11-07 14:24:08 -080014384static void intel_setup_outputs(struct drm_device *dev)
14385{
Eric Anholt725e30a2009-01-22 13:01:02 -080014386 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014387 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014388 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014389
Daniel Vetterc9093352013-06-06 22:22:47 +020014390 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014391
Jesse Barnes84b4e042014-06-25 08:24:29 -070014392 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014393 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014394
Vandana Kannanc776eb22014-08-19 12:05:01 +053014395 if (IS_BROXTON(dev)) {
14396 /*
14397 * FIXME: Broxton doesn't support port detection via the
14398 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14399 * detect the ports.
14400 */
14401 intel_ddi_init(dev, PORT_A);
14402 intel_ddi_init(dev, PORT_B);
14403 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014404
14405 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014406 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014407 int found;
14408
Jesse Barnesde31fac2015-03-06 15:53:32 -080014409 /*
14410 * Haswell uses DDI functions to detect digital outputs.
14411 * On SKL pre-D0 the strap isn't connected, so we assume
14412 * it's there.
14413 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014414 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014415 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014416 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014417 intel_ddi_init(dev, PORT_A);
14418
14419 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14420 * register */
14421 found = I915_READ(SFUSE_STRAP);
14422
14423 if (found & SFUSE_STRAP_DDIB_DETECTED)
14424 intel_ddi_init(dev, PORT_B);
14425 if (found & SFUSE_STRAP_DDIC_DETECTED)
14426 intel_ddi_init(dev, PORT_C);
14427 if (found & SFUSE_STRAP_DDID_DETECTED)
14428 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014429 /*
14430 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14431 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014432 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014433 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14434 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14435 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14436 intel_ddi_init(dev, PORT_E);
14437
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014438 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014439 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014440 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014441
14442 if (has_edp_a(dev))
14443 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014444
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014445 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014446 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014447 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014448 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014449 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014450 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014451 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014452 }
14453
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014454 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014455 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014456
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014457 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014458 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014459
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014460 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014461 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014462
Daniel Vetter270b3042012-10-27 15:52:05 +020014463 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014464 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014465 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014466 /*
14467 * The DP_DETECTED bit is the latched state of the DDC
14468 * SDA pin at boot. However since eDP doesn't require DDC
14469 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14470 * eDP ports may have been muxed to an alternate function.
14471 * Thus we can't rely on the DP_DETECTED bit alone to detect
14472 * eDP ports. Consult the VBT as well as DP_DETECTED to
14473 * detect eDP ports.
14474 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014475 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014476 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014477 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14478 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014479 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014480 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014481
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014482 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014483 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014484 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14485 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014486 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014487 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014488
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014489 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014490 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014491 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14492 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14493 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14494 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014495 }
14496
Jani Nikula3cfca972013-08-27 15:12:26 +030014497 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014498 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014499 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014500
Paulo Zanonie2debe92013-02-18 19:00:27 -030014501 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014502 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014503 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014504 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014505 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014506 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014507 }
Ma Ling27185ae2009-08-24 13:50:23 +080014508
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014509 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014510 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014511 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014512
14513 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014514
Paulo Zanonie2debe92013-02-18 19:00:27 -030014515 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014516 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014517 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014518 }
Ma Ling27185ae2009-08-24 13:50:23 +080014519
Paulo Zanonie2debe92013-02-18 19:00:27 -030014520 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014521
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014522 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014523 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014524 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014525 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014526 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014527 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014528 }
Ma Ling27185ae2009-08-24 13:50:23 +080014529
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014530 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014531 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014532 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014533 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014534 intel_dvo_init(dev);
14535
Zhenyu Wang103a1962009-11-27 11:44:36 +080014536 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014537 intel_tv_init(dev);
14538
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014539 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014540
Damien Lespiaub2784e12014-08-05 11:29:37 +010014541 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014542 encoder->base.possible_crtcs = encoder->crtc_mask;
14543 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014544 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014545 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014546
Paulo Zanonidde86e22012-12-01 12:04:25 -020014547 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014548
14549 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014550}
14551
14552static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14553{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014554 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014555 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014556
Daniel Vetteref2d6332014-02-10 18:00:38 +010014557 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014558 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014559 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014560 drm_gem_object_unreference(&intel_fb->obj->base);
14561 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014562 kfree(intel_fb);
14563}
14564
14565static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014566 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014567 unsigned int *handle)
14568{
14569 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014570 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014571
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014572 if (obj->userptr.mm) {
14573 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14574 return -EINVAL;
14575 }
14576
Chris Wilson05394f32010-11-08 19:18:58 +000014577 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014578}
14579
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014580static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14581 struct drm_file *file,
14582 unsigned flags, unsigned color,
14583 struct drm_clip_rect *clips,
14584 unsigned num_clips)
14585{
14586 struct drm_device *dev = fb->dev;
14587 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14588 struct drm_i915_gem_object *obj = intel_fb->obj;
14589
14590 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014591 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014592 mutex_unlock(&dev->struct_mutex);
14593
14594 return 0;
14595}
14596
Jesse Barnes79e53942008-11-07 14:24:08 -080014597static const struct drm_framebuffer_funcs intel_fb_funcs = {
14598 .destroy = intel_user_framebuffer_destroy,
14599 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014600 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014601};
14602
Damien Lespiaub3218032015-02-27 11:15:18 +000014603static
14604u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14605 uint32_t pixel_format)
14606{
14607 u32 gen = INTEL_INFO(dev)->gen;
14608
14609 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014610 int cpp = drm_format_plane_cpp(pixel_format, 0);
14611
Damien Lespiaub3218032015-02-27 11:15:18 +000014612 /* "The stride in bytes must not exceed the of the size of 8K
14613 * pixels and 32K bytes."
14614 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014615 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014616 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014617 return 32*1024;
14618 } else if (gen >= 4) {
14619 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14620 return 16*1024;
14621 else
14622 return 32*1024;
14623 } else if (gen >= 3) {
14624 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14625 return 8*1024;
14626 else
14627 return 16*1024;
14628 } else {
14629 /* XXX DSPC is limited to 4k tiled */
14630 return 8*1024;
14631 }
14632}
14633
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014634static int intel_framebuffer_init(struct drm_device *dev,
14635 struct intel_framebuffer *intel_fb,
14636 struct drm_mode_fb_cmd2 *mode_cmd,
14637 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014638{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014639 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014640 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014641 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014642 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014643
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014644 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14645
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014646 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14647 /* Enforce that fb modifier and tiling mode match, but only for
14648 * X-tiled. This is needed for FBC. */
14649 if (!!(obj->tiling_mode == I915_TILING_X) !=
14650 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14651 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14652 return -EINVAL;
14653 }
14654 } else {
14655 if (obj->tiling_mode == I915_TILING_X)
14656 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14657 else if (obj->tiling_mode == I915_TILING_Y) {
14658 DRM_DEBUG("No Y tiling for legacy addfb\n");
14659 return -EINVAL;
14660 }
14661 }
14662
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014663 /* Passed in modifier sanity checking. */
14664 switch (mode_cmd->modifier[0]) {
14665 case I915_FORMAT_MOD_Y_TILED:
14666 case I915_FORMAT_MOD_Yf_TILED:
14667 if (INTEL_INFO(dev)->gen < 9) {
14668 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14669 mode_cmd->modifier[0]);
14670 return -EINVAL;
14671 }
14672 case DRM_FORMAT_MOD_NONE:
14673 case I915_FORMAT_MOD_X_TILED:
14674 break;
14675 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014676 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14677 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014678 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014679 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014680
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014681 stride_alignment = intel_fb_stride_alignment(dev_priv,
14682 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014683 mode_cmd->pixel_format);
14684 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14685 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14686 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014687 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014688 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014689
Damien Lespiaub3218032015-02-27 11:15:18 +000014690 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14691 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014692 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014693 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14694 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014695 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014696 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014697 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014698 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014699
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014700 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014701 mode_cmd->pitches[0] != obj->stride) {
14702 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14703 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014704 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014705 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014706
Ville Syrjälä57779d02012-10-31 17:50:14 +020014707 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014708 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014709 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014710 case DRM_FORMAT_RGB565:
14711 case DRM_FORMAT_XRGB8888:
14712 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014713 break;
14714 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014715 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014716 DRM_DEBUG("unsupported pixel format: %s\n",
14717 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014718 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014719 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014720 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014721 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014722 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14723 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014724 DRM_DEBUG("unsupported pixel format: %s\n",
14725 drm_get_format_name(mode_cmd->pixel_format));
14726 return -EINVAL;
14727 }
14728 break;
14729 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014730 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014731 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014732 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014733 DRM_DEBUG("unsupported pixel format: %s\n",
14734 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014735 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014736 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014737 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014738 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014739 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014740 DRM_DEBUG("unsupported pixel format: %s\n",
14741 drm_get_format_name(mode_cmd->pixel_format));
14742 return -EINVAL;
14743 }
14744 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014745 case DRM_FORMAT_YUYV:
14746 case DRM_FORMAT_UYVY:
14747 case DRM_FORMAT_YVYU:
14748 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014749 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014750 DRM_DEBUG("unsupported pixel format: %s\n",
14751 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014752 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014753 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014754 break;
14755 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014756 DRM_DEBUG("unsupported pixel format: %s\n",
14757 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014758 return -EINVAL;
14759 }
14760
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014761 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14762 if (mode_cmd->offsets[0] != 0)
14763 return -EINVAL;
14764
Damien Lespiauec2c9812015-01-20 12:51:45 +000014765 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014766 mode_cmd->pixel_format,
14767 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014768 /* FIXME drm helper for size checks (especially planar formats)? */
14769 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14770 return -EINVAL;
14771
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014772 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14773 intel_fb->obj = obj;
14774
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014775 intel_fill_fb_info(dev_priv, &intel_fb->base);
14776
Jesse Barnes79e53942008-11-07 14:24:08 -080014777 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14778 if (ret) {
14779 DRM_ERROR("framebuffer init failed %d\n", ret);
14780 return ret;
14781 }
14782
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020014783 intel_fb->obj->framebuffer_references++;
14784
Jesse Barnes79e53942008-11-07 14:24:08 -080014785 return 0;
14786}
14787
Jesse Barnes79e53942008-11-07 14:24:08 -080014788static struct drm_framebuffer *
14789intel_user_framebuffer_create(struct drm_device *dev,
14790 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020014791 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014792{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014793 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014794 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014795 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014796
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014797 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014798 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014799 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014800 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014801
Daniel Vetter92907cb2015-11-23 09:04:05 +010014802 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014803 if (IS_ERR(fb))
14804 drm_gem_object_unreference_unlocked(&obj->base);
14805
14806 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014807}
14808
Daniel Vetter06957262015-08-10 13:34:08 +020014809#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014810static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014811{
14812}
14813#endif
14814
Jesse Barnes79e53942008-11-07 14:24:08 -080014815static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014816 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014817 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014818 .atomic_check = intel_atomic_check,
14819 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014820 .atomic_state_alloc = intel_atomic_state_alloc,
14821 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014822};
14823
Imre Deak88212942016-03-16 13:38:53 +020014824/**
14825 * intel_init_display_hooks - initialize the display modesetting hooks
14826 * @dev_priv: device private
14827 */
14828void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014829{
Imre Deak88212942016-03-16 13:38:53 +020014830 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014831 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014832 dev_priv->display.get_initial_plane_config =
14833 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014834 dev_priv->display.crtc_compute_clock =
14835 haswell_crtc_compute_clock;
14836 dev_priv->display.crtc_enable = haswell_crtc_enable;
14837 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014838 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014839 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014840 dev_priv->display.get_initial_plane_config =
14841 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014842 dev_priv->display.crtc_compute_clock =
14843 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014844 dev_priv->display.crtc_enable = haswell_crtc_enable;
14845 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014846 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014847 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014848 dev_priv->display.get_initial_plane_config =
14849 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014850 dev_priv->display.crtc_compute_clock =
14851 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014852 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14853 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014854 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014855 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014856 dev_priv->display.get_initial_plane_config =
14857 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014858 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14859 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14860 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14861 } else if (IS_VALLEYVIEW(dev_priv)) {
14862 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14863 dev_priv->display.get_initial_plane_config =
14864 i9xx_get_initial_plane_config;
14865 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014866 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14867 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020014868 } else if (IS_G4X(dev_priv)) {
14869 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14870 dev_priv->display.get_initial_plane_config =
14871 i9xx_get_initial_plane_config;
14872 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14873 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14874 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020014875 } else if (IS_PINEVIEW(dev_priv)) {
14876 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14877 dev_priv->display.get_initial_plane_config =
14878 i9xx_get_initial_plane_config;
14879 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14880 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14881 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014882 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014883 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014884 dev_priv->display.get_initial_plane_config =
14885 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014886 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014887 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14888 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014889 } else {
14890 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14891 dev_priv->display.get_initial_plane_config =
14892 i9xx_get_initial_plane_config;
14893 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14894 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14895 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014896 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014897
Jesse Barnese70236a2009-09-21 10:42:27 -070014898 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020014899 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014900 dev_priv->display.get_display_clock_speed =
14901 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014902 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014903 dev_priv->display.get_display_clock_speed =
14904 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014905 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014906 dev_priv->display.get_display_clock_speed =
14907 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014908 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014909 dev_priv->display.get_display_clock_speed =
14910 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014911 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014912 dev_priv->display.get_display_clock_speed =
14913 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014914 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014915 dev_priv->display.get_display_clock_speed =
14916 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014917 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14918 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014919 dev_priv->display.get_display_clock_speed =
14920 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014921 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014922 dev_priv->display.get_display_clock_speed =
14923 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014924 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014925 dev_priv->display.get_display_clock_speed =
14926 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014927 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014928 dev_priv->display.get_display_clock_speed =
14929 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014930 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014931 dev_priv->display.get_display_clock_speed =
14932 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014933 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014934 dev_priv->display.get_display_clock_speed =
14935 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014936 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014937 dev_priv->display.get_display_clock_speed =
14938 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014939 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014940 dev_priv->display.get_display_clock_speed =
14941 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014942 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014943 dev_priv->display.get_display_clock_speed =
14944 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014945 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014946 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014947 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014948 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020014949 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014950 dev_priv->display.get_display_clock_speed =
14951 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014952 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014953
Imre Deak88212942016-03-16 13:38:53 +020014954 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014955 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014956 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014957 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014958 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014959 /* FIXME: detect B0+ stepping and use auto training */
14960 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014961 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014962 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014963 if (IS_BROADWELL(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014964 dev_priv->display.modeset_commit_cdclk =
14965 broadwell_modeset_commit_cdclk;
14966 dev_priv->display.modeset_calc_cdclk =
14967 broadwell_modeset_calc_cdclk;
14968 }
Imre Deak88212942016-03-16 13:38:53 +020014969 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014970 dev_priv->display.modeset_commit_cdclk =
14971 valleyview_modeset_commit_cdclk;
14972 dev_priv->display.modeset_calc_cdclk =
14973 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020014974 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014975 dev_priv->display.modeset_commit_cdclk =
14976 broxton_modeset_commit_cdclk;
14977 dev_priv->display.modeset_calc_cdclk =
14978 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014979 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014980
Imre Deak88212942016-03-16 13:38:53 +020014981 switch (INTEL_INFO(dev_priv)->gen) {
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014982 case 2:
14983 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14984 break;
14985
14986 case 3:
14987 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14988 break;
14989
14990 case 4:
14991 case 5:
14992 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14993 break;
14994
14995 case 6:
14996 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14997 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014998 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014999 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015000 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15001 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000015002 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000015003 /* Drop through - unsupported since execlist only. */
15004 default:
15005 /* Default just returns -ENODEV to indicate unsupported */
15006 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015007 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015008}
15009
Jesse Barnesb690e962010-07-19 13:53:12 -070015010/*
15011 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15012 * resume, or other times. This quirk makes sure that's the case for
15013 * affected systems.
15014 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015015static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015016{
15017 struct drm_i915_private *dev_priv = dev->dev_private;
15018
15019 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015020 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015021}
15022
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015023static void quirk_pipeb_force(struct drm_device *dev)
15024{
15025 struct drm_i915_private *dev_priv = dev->dev_private;
15026
15027 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15028 DRM_INFO("applying pipe b force quirk\n");
15029}
15030
Keith Packard435793d2011-07-12 14:56:22 -070015031/*
15032 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15033 */
15034static void quirk_ssc_force_disable(struct drm_device *dev)
15035{
15036 struct drm_i915_private *dev_priv = dev->dev_private;
15037 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015038 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015039}
15040
Carsten Emde4dca20e2012-03-15 15:56:26 +010015041/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015042 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15043 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015044 */
15045static void quirk_invert_brightness(struct drm_device *dev)
15046{
15047 struct drm_i915_private *dev_priv = dev->dev_private;
15048 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015049 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015050}
15051
Scot Doyle9c72cc62014-07-03 23:27:50 +000015052/* Some VBT's incorrectly indicate no backlight is present */
15053static void quirk_backlight_present(struct drm_device *dev)
15054{
15055 struct drm_i915_private *dev_priv = dev->dev_private;
15056 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15057 DRM_INFO("applying backlight present quirk\n");
15058}
15059
Jesse Barnesb690e962010-07-19 13:53:12 -070015060struct intel_quirk {
15061 int device;
15062 int subsystem_vendor;
15063 int subsystem_device;
15064 void (*hook)(struct drm_device *dev);
15065};
15066
Egbert Eich5f85f172012-10-14 15:46:38 +020015067/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15068struct intel_dmi_quirk {
15069 void (*hook)(struct drm_device *dev);
15070 const struct dmi_system_id (*dmi_id_list)[];
15071};
15072
15073static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15074{
15075 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15076 return 1;
15077}
15078
15079static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15080 {
15081 .dmi_id_list = &(const struct dmi_system_id[]) {
15082 {
15083 .callback = intel_dmi_reverse_brightness,
15084 .ident = "NCR Corporation",
15085 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15086 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15087 },
15088 },
15089 { } /* terminating entry */
15090 },
15091 .hook = quirk_invert_brightness,
15092 },
15093};
15094
Ben Widawskyc43b5632012-04-16 14:07:40 -070015095static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015096 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15097 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15098
Jesse Barnesb690e962010-07-19 13:53:12 -070015099 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15100 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15101
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015102 /* 830 needs to leave pipe A & dpll A up */
15103 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15104
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015105 /* 830 needs to leave pipe B & dpll B up */
15106 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15107
Keith Packard435793d2011-07-12 14:56:22 -070015108 /* Lenovo U160 cannot use SSC on LVDS */
15109 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015110
15111 /* Sony Vaio Y cannot use SSC on LVDS */
15112 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015113
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015114 /* Acer Aspire 5734Z must invert backlight brightness */
15115 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15116
15117 /* Acer/eMachines G725 */
15118 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15119
15120 /* Acer/eMachines e725 */
15121 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15122
15123 /* Acer/Packard Bell NCL20 */
15124 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15125
15126 /* Acer Aspire 4736Z */
15127 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015128
15129 /* Acer Aspire 5336 */
15130 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015131
15132 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15133 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015134
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015135 /* Acer C720 Chromebook (Core i3 4005U) */
15136 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15137
jens steinb2a96012014-10-28 20:25:53 +010015138 /* Apple Macbook 2,1 (Core 2 T7400) */
15139 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15140
Jani Nikula1b9448b2015-11-05 11:49:59 +020015141 /* Apple Macbook 4,1 */
15142 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15143
Scot Doyled4967d82014-07-03 23:27:52 +000015144 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15145 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015146
15147 /* HP Chromebook 14 (Celeron 2955U) */
15148 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015149
15150 /* Dell Chromebook 11 */
15151 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015152
15153 /* Dell Chromebook 11 (2015 version) */
15154 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015155};
15156
15157static void intel_init_quirks(struct drm_device *dev)
15158{
15159 struct pci_dev *d = dev->pdev;
15160 int i;
15161
15162 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15163 struct intel_quirk *q = &intel_quirks[i];
15164
15165 if (d->device == q->device &&
15166 (d->subsystem_vendor == q->subsystem_vendor ||
15167 q->subsystem_vendor == PCI_ANY_ID) &&
15168 (d->subsystem_device == q->subsystem_device ||
15169 q->subsystem_device == PCI_ANY_ID))
15170 q->hook(dev);
15171 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015172 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15173 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15174 intel_dmi_quirks[i].hook(dev);
15175 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015176}
15177
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015178/* Disable the VGA plane that we never use */
15179static void i915_disable_vga(struct drm_device *dev)
15180{
15181 struct drm_i915_private *dev_priv = dev->dev_private;
15182 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015183 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015184
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015185 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015186 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015187 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015188 sr1 = inb(VGA_SR_DATA);
15189 outb(sr1 | 1<<5, VGA_SR_DATA);
15190 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15191 udelay(300);
15192
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015193 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015194 POSTING_READ(vga_reg);
15195}
15196
Daniel Vetterf8175862012-04-10 15:50:11 +020015197void intel_modeset_init_hw(struct drm_device *dev)
15198{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015199 struct drm_i915_private *dev_priv = dev->dev_private;
15200
Ville Syrjäläb6283052015-06-03 15:45:07 +030015201 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015202
15203 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15204
Daniel Vetterf8175862012-04-10 15:50:11 +020015205 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015206 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015207}
15208
Matt Roperd93c0372015-12-03 11:37:41 -080015209/*
15210 * Calculate what we think the watermarks should be for the state we've read
15211 * out of the hardware and then immediately program those watermarks so that
15212 * we ensure the hardware settings match our internal state.
15213 *
15214 * We can calculate what we think WM's should be by creating a duplicate of the
15215 * current state (which was constructed during hardware readout) and running it
15216 * through the atomic check code to calculate new watermark values in the
15217 * state object.
15218 */
15219static void sanitize_watermarks(struct drm_device *dev)
15220{
15221 struct drm_i915_private *dev_priv = to_i915(dev);
15222 struct drm_atomic_state *state;
15223 struct drm_crtc *crtc;
15224 struct drm_crtc_state *cstate;
15225 struct drm_modeset_acquire_ctx ctx;
15226 int ret;
15227 int i;
15228
15229 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015230 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015231 return;
15232
15233 /*
15234 * We need to hold connection_mutex before calling duplicate_state so
15235 * that the connector loop is protected.
15236 */
15237 drm_modeset_acquire_init(&ctx, 0);
15238retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015239 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015240 if (ret == -EDEADLK) {
15241 drm_modeset_backoff(&ctx);
15242 goto retry;
15243 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015244 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015245 }
15246
15247 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15248 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015249 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015250
Matt Ropered4a6a72016-02-23 17:20:13 -080015251 /*
15252 * Hardware readout is the only time we don't want to calculate
15253 * intermediate watermarks (since we don't trust the current
15254 * watermarks).
15255 */
15256 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15257
Matt Roperd93c0372015-12-03 11:37:41 -080015258 ret = intel_atomic_check(dev, state);
15259 if (ret) {
15260 /*
15261 * If we fail here, it means that the hardware appears to be
15262 * programmed in a way that shouldn't be possible, given our
15263 * understanding of watermark requirements. This might mean a
15264 * mistake in the hardware readout code or a mistake in the
15265 * watermark calculations for a given platform. Raise a WARN
15266 * so that this is noticeable.
15267 *
15268 * If this actually happens, we'll have to just leave the
15269 * BIOS-programmed watermarks untouched and hope for the best.
15270 */
15271 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015272 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015273 }
15274
15275 /* Write calculated watermark values back */
15276 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15277 for_each_crtc_in_state(state, crtc, cstate, i) {
15278 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15279
Matt Ropered4a6a72016-02-23 17:20:13 -080015280 cs->wm.need_postvbl_update = true;
15281 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015282 }
15283
15284 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015285fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015286 drm_modeset_drop_locks(&ctx);
15287 drm_modeset_acquire_fini(&ctx);
15288}
15289
Jesse Barnes79e53942008-11-07 14:24:08 -080015290void intel_modeset_init(struct drm_device *dev)
15291{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015292 struct drm_i915_private *dev_priv = to_i915(dev);
15293 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015294 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015295 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015296 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015297
15298 drm_mode_config_init(dev);
15299
15300 dev->mode_config.min_width = 0;
15301 dev->mode_config.min_height = 0;
15302
Dave Airlie019d96c2011-09-29 16:20:42 +010015303 dev->mode_config.preferred_depth = 24;
15304 dev->mode_config.prefer_shadow = 1;
15305
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015306 dev->mode_config.allow_fb_modifiers = true;
15307
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015308 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015309
Jesse Barnesb690e962010-07-19 13:53:12 -070015310 intel_init_quirks(dev);
15311
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015312 intel_init_pm(dev);
15313
Ben Widawskye3c74752013-04-05 13:12:39 -070015314 if (INTEL_INFO(dev)->num_pipes == 0)
15315 return;
15316
Lukas Wunner69f92f62015-07-15 13:57:35 +020015317 /*
15318 * There may be no VBT; and if the BIOS enabled SSC we can
15319 * just keep using it to avoid unnecessary flicker. Whereas if the
15320 * BIOS isn't using it, don't assume it will work even if the VBT
15321 * indicates as much.
15322 */
15323 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15324 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15325 DREF_SSC1_ENABLE);
15326
15327 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15328 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15329 bios_lvds_use_ssc ? "en" : "dis",
15330 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15331 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15332 }
15333 }
15334
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015335 if (IS_GEN2(dev)) {
15336 dev->mode_config.max_width = 2048;
15337 dev->mode_config.max_height = 2048;
15338 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015339 dev->mode_config.max_width = 4096;
15340 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015341 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015342 dev->mode_config.max_width = 8192;
15343 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015344 }
Damien Lespiau068be562014-03-28 14:17:49 +000015345
Ville Syrjälädc41c152014-08-13 11:57:05 +030015346 if (IS_845G(dev) || IS_I865G(dev)) {
15347 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15348 dev->mode_config.cursor_height = 1023;
15349 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015350 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15351 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15352 } else {
15353 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15354 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15355 }
15356
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015357 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015358
Zhao Yakui28c97732009-10-09 11:39:41 +080015359 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015360 INTEL_INFO(dev)->num_pipes,
15361 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015362
Damien Lespiau055e3932014-08-18 13:49:10 +010015363 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015364 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015365 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015366 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015367 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015368 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015369 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015370 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015371 }
15372
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015373 intel_update_czclk(dev_priv);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +020015374 intel_update_rawclk(dev_priv);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015375 intel_update_cdclk(dev);
15376
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015377 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015378
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015379 /* Just disable it once at startup */
15380 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015381 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015382
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015383 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015384 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015385 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015386
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015387 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015388 struct intel_initial_plane_config plane_config = {};
15389
Jesse Barnes46f297f2014-03-07 08:57:48 -080015390 if (!crtc->active)
15391 continue;
15392
Jesse Barnes46f297f2014-03-07 08:57:48 -080015393 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015394 * Note that reserving the BIOS fb up front prevents us
15395 * from stuffing other stolen allocations like the ring
15396 * on top. This prevents some ugliness at boot time, and
15397 * can even allow for smooth boot transitions if the BIOS
15398 * fb is large enough for the active pipe configuration.
15399 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015400 dev_priv->display.get_initial_plane_config(crtc,
15401 &plane_config);
15402
15403 /*
15404 * If the fb is shared between multiple heads, we'll
15405 * just get the first one.
15406 */
15407 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015408 }
Matt Roperd93c0372015-12-03 11:37:41 -080015409
15410 /*
15411 * Make sure hardware watermarks really match the state we read out.
15412 * Note that we need to do this after reconstructing the BIOS fb's
15413 * since the watermark calculation done here will use pstate->fb.
15414 */
15415 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015416}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015417
Daniel Vetter7fad7982012-07-04 17:51:47 +020015418static void intel_enable_pipe_a(struct drm_device *dev)
15419{
15420 struct intel_connector *connector;
15421 struct drm_connector *crt = NULL;
15422 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015423 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015424
15425 /* We can't just switch on the pipe A, we need to set things up with a
15426 * proper mode and output configuration. As a gross hack, enable pipe A
15427 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015428 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015429 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15430 crt = &connector->base;
15431 break;
15432 }
15433 }
15434
15435 if (!crt)
15436 return;
15437
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015438 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015439 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015440}
15441
Daniel Vetterfa555832012-10-10 23:14:00 +020015442static bool
15443intel_check_plane_mapping(struct intel_crtc *crtc)
15444{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015445 struct drm_device *dev = crtc->base.dev;
15446 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015447 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015448
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015449 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015450 return true;
15451
Ville Syrjälä649636e2015-09-22 19:50:01 +030015452 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015453
15454 if ((val & DISPLAY_PLANE_ENABLE) &&
15455 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15456 return false;
15457
15458 return true;
15459}
15460
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015461static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15462{
15463 struct drm_device *dev = crtc->base.dev;
15464 struct intel_encoder *encoder;
15465
15466 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15467 return true;
15468
15469 return false;
15470}
15471
Ville Syrjälädd756192016-02-17 21:28:45 +020015472static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15473{
15474 struct drm_device *dev = encoder->base.dev;
15475 struct intel_connector *connector;
15476
15477 for_each_connector_on_encoder(dev, &encoder->base, connector)
15478 return true;
15479
15480 return false;
15481}
15482
Daniel Vetter24929352012-07-02 20:28:59 +020015483static void intel_sanitize_crtc(struct intel_crtc *crtc)
15484{
15485 struct drm_device *dev = crtc->base.dev;
15486 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015487 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015488
Daniel Vetter24929352012-07-02 20:28:59 +020015489 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015490 if (!transcoder_is_dsi(cpu_transcoder)) {
15491 i915_reg_t reg = PIPECONF(cpu_transcoder);
15492
15493 I915_WRITE(reg,
15494 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15495 }
Daniel Vetter24929352012-07-02 20:28:59 +020015496
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015497 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015498 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015499 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015500 struct intel_plane *plane;
15501
Daniel Vetter96256042015-02-13 21:03:42 +010015502 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015503
15504 /* Disable everything but the primary plane */
15505 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15506 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15507 continue;
15508
15509 plane->disable_plane(&plane->base, &crtc->base);
15510 }
Daniel Vetter96256042015-02-13 21:03:42 +010015511 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015512
Daniel Vetter24929352012-07-02 20:28:59 +020015513 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015514 * disable the crtc (and hence change the state) if it is wrong. Note
15515 * that gen4+ has a fixed plane -> pipe mapping. */
15516 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015517 bool plane;
15518
Daniel Vetter24929352012-07-02 20:28:59 +020015519 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15520 crtc->base.base.id);
15521
15522 /* Pipe has the wrong plane attached and the plane is active.
15523 * Temporarily change the plane mapping and disable everything
15524 * ... */
15525 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015526 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015527 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015528 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015529 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015530 }
Daniel Vetter24929352012-07-02 20:28:59 +020015531
Daniel Vetter7fad7982012-07-04 17:51:47 +020015532 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15533 crtc->pipe == PIPE_A && !crtc->active) {
15534 /* BIOS forgot to enable pipe A, this mostly happens after
15535 * resume. Force-enable the pipe to fix this, the update_dpms
15536 * call below we restore the pipe to the right state, but leave
15537 * the required bits on. */
15538 intel_enable_pipe_a(dev);
15539 }
15540
Daniel Vetter24929352012-07-02 20:28:59 +020015541 /* Adjust the state of the output pipe according to whether we
15542 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015543 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015544 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015545
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015546 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015547 /*
15548 * We start out with underrun reporting disabled to avoid races.
15549 * For correct bookkeeping mark this on active crtcs.
15550 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015551 * Also on gmch platforms we dont have any hardware bits to
15552 * disable the underrun reporting. Which means we need to start
15553 * out with underrun reporting disabled also on inactive pipes,
15554 * since otherwise we'll complain about the garbage we read when
15555 * e.g. coming up after runtime pm.
15556 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015557 * No protection against concurrent access is required - at
15558 * worst a fifo underrun happens which also sets this to false.
15559 */
15560 crtc->cpu_fifo_underrun_disabled = true;
15561 crtc->pch_fifo_underrun_disabled = true;
15562 }
Daniel Vetter24929352012-07-02 20:28:59 +020015563}
15564
15565static void intel_sanitize_encoder(struct intel_encoder *encoder)
15566{
15567 struct intel_connector *connector;
15568 struct drm_device *dev = encoder->base.dev;
15569
15570 /* We need to check both for a crtc link (meaning that the
15571 * encoder is active and trying to read from a pipe) and the
15572 * pipe itself being active. */
15573 bool has_active_crtc = encoder->base.crtc &&
15574 to_intel_crtc(encoder->base.crtc)->active;
15575
Ville Syrjälädd756192016-02-17 21:28:45 +020015576 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015577 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15578 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015579 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015580
15581 /* Connector is active, but has no active pipe. This is
15582 * fallout from our resume register restoring. Disable
15583 * the encoder manually again. */
15584 if (encoder->base.crtc) {
15585 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15586 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015587 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015588 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015589 if (encoder->post_disable)
15590 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015591 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015592 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015593
15594 /* Inconsistent output/port/pipe state happens presumably due to
15595 * a bug in one of the get_hw_state functions. Or someplace else
15596 * in our code, like the register restore mess on resume. Clamp
15597 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015598 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015599 if (connector->encoder != encoder)
15600 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015601 connector->base.dpms = DRM_MODE_DPMS_OFF;
15602 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015603 }
15604 }
15605 /* Enabled encoders without active connectors will be fixed in
15606 * the crtc fixup. */
15607}
15608
Imre Deak04098752014-02-18 00:02:16 +020015609void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015610{
15611 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015612 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015613
Imre Deak04098752014-02-18 00:02:16 +020015614 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15615 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15616 i915_disable_vga(dev);
15617 }
15618}
15619
15620void i915_redisable_vga(struct drm_device *dev)
15621{
15622 struct drm_i915_private *dev_priv = dev->dev_private;
15623
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015624 /* This function can be called both from intel_modeset_setup_hw_state or
15625 * at a very early point in our resume sequence, where the power well
15626 * structures are not yet restored. Since this function is at a very
15627 * paranoid "someone might have enabled VGA while we were not looking"
15628 * level, just check if the power well is enabled instead of trying to
15629 * follow the "don't touch the power well if we don't need it" policy
15630 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015631 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015632 return;
15633
Imre Deak04098752014-02-18 00:02:16 +020015634 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015635
15636 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015637}
15638
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015639static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015640{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015641 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015642
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015643 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015644}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015645
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015646/* FIXME read out full plane state for all planes */
15647static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015648{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015649 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015650 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015651 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015652
Matt Roper19b8d382015-09-24 15:53:17 -070015653 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015654 primary_get_hw_state(to_intel_plane(primary));
15655
15656 if (plane_state->visible)
15657 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015658}
15659
Daniel Vetter30e984d2013-06-05 13:34:17 +020015660static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015661{
15662 struct drm_i915_private *dev_priv = dev->dev_private;
15663 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015664 struct intel_crtc *crtc;
15665 struct intel_encoder *encoder;
15666 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015667 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015668
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015669 dev_priv->active_crtcs = 0;
15670
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015671 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015672 struct intel_crtc_state *crtc_state = crtc->config;
15673 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015674
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015675 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15676 memset(crtc_state, 0, sizeof(*crtc_state));
15677 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015678
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015679 crtc_state->base.active = crtc_state->base.enable =
15680 dev_priv->display.get_pipe_config(crtc, crtc_state);
15681
15682 crtc->base.enabled = crtc_state->base.enable;
15683 crtc->active = crtc_state->base.active;
15684
15685 if (crtc_state->base.active) {
15686 dev_priv->active_crtcs |= 1 << crtc->pipe;
15687
15688 if (IS_BROADWELL(dev_priv)) {
15689 pixclk = ilk_pipe_pixel_rate(crtc_state);
15690
15691 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15692 if (crtc_state->ips_enabled)
15693 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15694 } else if (IS_VALLEYVIEW(dev_priv) ||
15695 IS_CHERRYVIEW(dev_priv) ||
15696 IS_BROXTON(dev_priv))
15697 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15698 else
15699 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15700 }
15701
15702 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015703
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015704 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015705
15706 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15707 crtc->base.base.id,
15708 crtc->active ? "enabled" : "disabled");
15709 }
15710
Daniel Vetter53589012013-06-05 13:34:16 +020015711 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15712 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15713
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015714 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15715 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015716 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015717 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015718 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015719 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015720 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015721 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015722
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015723 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015724 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015725 }
15726
Damien Lespiaub2784e12014-08-05 11:29:37 +010015727 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015728 pipe = 0;
15729
15730 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015731 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15732 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015733 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015734 } else {
15735 encoder->base.crtc = NULL;
15736 }
15737
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015738 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015739 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015740 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015741 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015742 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015743 }
15744
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015745 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015746 if (connector->get_hw_state(connector)) {
15747 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015748
15749 encoder = connector->encoder;
15750 connector->base.encoder = &encoder->base;
15751
15752 if (encoder->base.crtc &&
15753 encoder->base.crtc->state->active) {
15754 /*
15755 * This has to be done during hardware readout
15756 * because anything calling .crtc_disable may
15757 * rely on the connector_mask being accurate.
15758 */
15759 encoder->base.crtc->state->connector_mask |=
15760 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015761 encoder->base.crtc->state->encoder_mask |=
15762 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015763 }
15764
Daniel Vetter24929352012-07-02 20:28:59 +020015765 } else {
15766 connector->base.dpms = DRM_MODE_DPMS_OFF;
15767 connector->base.encoder = NULL;
15768 }
15769 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15770 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015771 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015772 connector->base.encoder ? "enabled" : "disabled");
15773 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015774
15775 for_each_intel_crtc(dev, crtc) {
15776 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15777
15778 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15779 if (crtc->base.state->active) {
15780 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15781 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15782 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15783
15784 /*
15785 * The initial mode needs to be set in order to keep
15786 * the atomic core happy. It wants a valid mode if the
15787 * crtc's enabled, so we do the above call.
15788 *
15789 * At this point some state updated by the connectors
15790 * in their ->detect() callback has not run yet, so
15791 * no recalculation can be done yet.
15792 *
15793 * Even if we could do a recalculation and modeset
15794 * right now it would cause a double modeset if
15795 * fbdev or userspace chooses a different initial mode.
15796 *
15797 * If that happens, someone indicated they wanted a
15798 * mode change, which means it's safe to do a full
15799 * recalculation.
15800 */
15801 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015802
15803 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15804 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015805 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015806
15807 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015808 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015809}
15810
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015811/* Scan out the current hw modeset state,
15812 * and sanitizes it to the current state
15813 */
15814static void
15815intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015816{
15817 struct drm_i915_private *dev_priv = dev->dev_private;
15818 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015819 struct intel_crtc *crtc;
15820 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015821 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015822
15823 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015824
15825 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015826 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015827 intel_sanitize_encoder(encoder);
15828 }
15829
Damien Lespiau055e3932014-08-18 13:49:10 +010015830 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015831 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15832 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015833 intel_dump_pipe_config(crtc, crtc->config,
15834 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015835 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015836
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015837 intel_modeset_update_connector_atomic_state(dev);
15838
Daniel Vetter35c95372013-07-17 06:55:04 +020015839 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15840 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15841
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015842 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015843 continue;
15844
15845 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15846
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015847 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015848 pll->on = false;
15849 }
15850
Wayne Boyer666a4532015-12-09 12:29:35 -080015851 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015852 vlv_wm_get_hw_state(dev);
15853 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015854 skl_wm_get_hw_state(dev);
15855 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015856 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015857
15858 for_each_intel_crtc(dev, crtc) {
15859 unsigned long put_domains;
15860
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015861 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015862 if (WARN_ON(put_domains))
15863 modeset_put_power_domains(dev_priv, put_domains);
15864 }
15865 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015866
15867 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015868}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015869
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015870void intel_display_resume(struct drm_device *dev)
15871{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015872 struct drm_i915_private *dev_priv = to_i915(dev);
15873 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15874 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015875 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015876 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020015877
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015878 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015879
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015880 /*
15881 * This is a cludge because with real atomic modeset mode_config.mutex
15882 * won't be taken. Unfortunately some probed state like
15883 * audio_codec_enable is still protected by mode_config.mutex, so lock
15884 * it here for now.
15885 */
15886 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015887 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015888
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015889retry:
15890 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015891
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015892 if (ret == 0 && !setup) {
15893 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015894
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015895 intel_modeset_setup_hw_state(dev);
15896 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015897 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015898
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015899 if (ret == 0 && state) {
15900 struct drm_crtc_state *crtc_state;
15901 struct drm_crtc *crtc;
15902 int i;
15903
15904 state->acquire_ctx = &ctx;
15905
15906 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15907 /*
15908 * Force recalculation even if we restore
15909 * current state. With fast modeset this may not result
15910 * in a modeset when the state is compatible.
15911 */
15912 crtc_state->mode_changed = true;
15913 }
15914
15915 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015916 }
15917
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015918 if (ret == -EDEADLK) {
15919 drm_modeset_backoff(&ctx);
15920 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015921 }
15922
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015923 drm_modeset_drop_locks(&ctx);
15924 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015925 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015926
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015927 if (ret) {
15928 DRM_ERROR("Restoring old state failed with %i\n", ret);
15929 drm_atomic_state_free(state);
15930 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015931}
15932
15933void intel_modeset_gem_init(struct drm_device *dev)
15934{
Jesse Barnes484b41d2014-03-07 08:57:55 -080015935 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015936 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015937 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015938
Imre Deakae484342014-03-31 15:10:44 +030015939 intel_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +030015940
Chris Wilson1833b132012-05-09 11:56:28 +010015941 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015942
15943 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015944
15945 /*
15946 * Make sure any fbs we allocated at startup are properly
15947 * pinned & fenced. When we do the allocation it's too early
15948 * for this.
15949 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015950 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015951 obj = intel_fb_obj(c->primary->fb);
15952 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015953 continue;
15954
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015955 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020015956 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
15957 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015958 mutex_unlock(&dev->struct_mutex);
15959 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015960 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15961 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015962 drm_framebuffer_unreference(c->primary->fb);
15963 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015964 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015965 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015966 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015967 }
15968 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015969
15970 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015971}
15972
Imre Deak4932e2c2014-02-11 17:12:48 +020015973void intel_connector_unregister(struct intel_connector *intel_connector)
15974{
15975 struct drm_connector *connector = &intel_connector->base;
15976
15977 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015978 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015979}
15980
Jesse Barnes79e53942008-11-07 14:24:08 -080015981void intel_modeset_cleanup(struct drm_device *dev)
15982{
Jesse Barnes652c3932009-08-17 13:31:43 -070015983 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020015984 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015985
Imre Deak2eb52522014-11-19 15:30:05 +020015986 intel_disable_gt_powersave(dev);
15987
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015988 intel_backlight_unregister(dev);
15989
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015990 /*
15991 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015992 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015993 * experience fancy races otherwise.
15994 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015995 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015996
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015997 /*
15998 * Due to the hpd irq storm handling the hotplug work can re-arm the
15999 * poll handlers. Hence disable polling after hpd handling is shut down.
16000 */
Keith Packardf87ea762010-10-03 19:36:26 -070016001 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016002
Jesse Barnes723bfd72010-10-07 16:01:13 -070016003 intel_unregister_dsm_handler();
16004
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016005 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016006
Chris Wilson1630fe72011-07-08 12:22:42 +010016007 /* flush any delayed tasks or pending work */
16008 flush_scheduled_work();
16009
Jani Nikuladb31af1d2013-11-08 16:48:53 +020016010 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020016011 for_each_intel_connector(dev, connector)
16012 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030016013
Jesse Barnes79e53942008-11-07 14:24:08 -080016014 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016015
16016 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030016017
Imre Deakae484342014-03-31 15:10:44 +030016018 intel_cleanup_gt_powersave(dev);
Daniel Vetterf5949142016-01-13 11:55:28 +010016019
16020 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016021}
16022
Dave Airlie28d52042009-09-21 14:33:58 +100016023/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080016024 * Return which encoder is currently attached for connector.
16025 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010016026struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080016027{
Chris Wilsondf0e9242010-09-09 16:20:55 +010016028 return &intel_attached_encoder(connector)->base;
16029}
Jesse Barnes79e53942008-11-07 14:24:08 -080016030
Chris Wilsondf0e9242010-09-09 16:20:55 +010016031void intel_connector_attach_encoder(struct intel_connector *connector,
16032 struct intel_encoder *encoder)
16033{
16034 connector->encoder = encoder;
16035 drm_mode_connector_attach_encoder(&connector->base,
16036 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016037}
Dave Airlie28d52042009-09-21 14:33:58 +100016038
16039/*
16040 * set vga decode state - true == enable VGA decode
16041 */
16042int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16043{
16044 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016045 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016046 u16 gmch_ctrl;
16047
Chris Wilson75fa0412014-02-07 18:37:02 -020016048 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16049 DRM_ERROR("failed to read control word\n");
16050 return -EIO;
16051 }
16052
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016053 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16054 return 0;
16055
Dave Airlie28d52042009-09-21 14:33:58 +100016056 if (state)
16057 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16058 else
16059 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016060
16061 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16062 DRM_ERROR("failed to write control word\n");
16063 return -EIO;
16064 }
16065
Dave Airlie28d52042009-09-21 14:33:58 +100016066 return 0;
16067}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016068
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016069struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016070
16071 u32 power_well_driver;
16072
Chris Wilson63b66e52013-08-08 15:12:06 +020016073 int num_transcoders;
16074
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016075 struct intel_cursor_error_state {
16076 u32 control;
16077 u32 position;
16078 u32 base;
16079 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016080 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016081
16082 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016083 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016084 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030016085 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016086 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016087
16088 struct intel_plane_error_state {
16089 u32 control;
16090 u32 stride;
16091 u32 size;
16092 u32 pos;
16093 u32 addr;
16094 u32 surface;
16095 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016096 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016097
16098 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016099 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016100 enum transcoder cpu_transcoder;
16101
16102 u32 conf;
16103
16104 u32 htotal;
16105 u32 hblank;
16106 u32 hsync;
16107 u32 vtotal;
16108 u32 vblank;
16109 u32 vsync;
16110 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016111};
16112
16113struct intel_display_error_state *
16114intel_display_capture_error_state(struct drm_device *dev)
16115{
Jani Nikulafbee40d2014-03-31 14:27:18 +030016116 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016117 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016118 int transcoders[] = {
16119 TRANSCODER_A,
16120 TRANSCODER_B,
16121 TRANSCODER_C,
16122 TRANSCODER_EDP,
16123 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016124 int i;
16125
Chris Wilson63b66e52013-08-08 15:12:06 +020016126 if (INTEL_INFO(dev)->num_pipes == 0)
16127 return NULL;
16128
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016129 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016130 if (error == NULL)
16131 return NULL;
16132
Imre Deak190be112013-11-25 17:15:31 +020016133 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016134 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16135
Damien Lespiau055e3932014-08-18 13:49:10 +010016136 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016137 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016138 __intel_display_power_is_enabled(dev_priv,
16139 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016140 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016141 continue;
16142
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016143 error->cursor[i].control = I915_READ(CURCNTR(i));
16144 error->cursor[i].position = I915_READ(CURPOS(i));
16145 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016146
16147 error->plane[i].control = I915_READ(DSPCNTR(i));
16148 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016149 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016150 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016151 error->plane[i].pos = I915_READ(DSPPOS(i));
16152 }
Paulo Zanonica291362013-03-06 20:03:14 -030016153 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16154 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016155 if (INTEL_INFO(dev)->gen >= 4) {
16156 error->plane[i].surface = I915_READ(DSPSURF(i));
16157 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16158 }
16159
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016160 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030016161
Sonika Jindal3abfce72014-07-21 15:23:43 +053016162 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030016163 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016164 }
16165
Jani Nikula4d1de972016-03-18 17:05:42 +020016166 /* Note: this does not include DSI transcoders. */
Chris Wilson63b66e52013-08-08 15:12:06 +020016167 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
16168 if (HAS_DDI(dev_priv->dev))
16169 error->num_transcoders++; /* Account for eDP. */
16170
16171 for (i = 0; i < error->num_transcoders; i++) {
16172 enum transcoder cpu_transcoder = transcoders[i];
16173
Imre Deakddf9c532013-11-27 22:02:02 +020016174 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016175 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016176 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016177 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016178 continue;
16179
Chris Wilson63b66e52013-08-08 15:12:06 +020016180 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16181
16182 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16183 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16184 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16185 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16186 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16187 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16188 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016189 }
16190
16191 return error;
16192}
16193
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016194#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16195
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016196void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016197intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016198 struct drm_device *dev,
16199 struct intel_display_error_state *error)
16200{
Damien Lespiau055e3932014-08-18 13:49:10 +010016201 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016202 int i;
16203
Chris Wilson63b66e52013-08-08 15:12:06 +020016204 if (!error)
16205 return;
16206
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016207 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016208 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016209 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016210 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016211 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016212 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016213 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016214 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016215 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016216 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016217
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016218 err_printf(m, "Plane [%d]:\n", i);
16219 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16220 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016221 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016222 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16223 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016224 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016225 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016226 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016227 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016228 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16229 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016230 }
16231
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016232 err_printf(m, "Cursor [%d]:\n", i);
16233 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16234 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16235 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016236 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016237
16238 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016239 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016240 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016241 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016242 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016243 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16244 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16245 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16246 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16247 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16248 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16249 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16250 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016251}