blob: def25ea8d5b367aa80c5278f66a3806d7ecda1e1 [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
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200150int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
151 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300152{
153 u32 val;
154 int divider;
155
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300156 mutex_lock(&dev_priv->sb_lock);
157 val = vlv_cck_read(dev_priv, reg);
158 mutex_unlock(&dev_priv->sb_lock);
159
160 divider = val & CCK_FREQUENCY_VALUES;
161
162 WARN((val & CCK_FREQUENCY_STATUS) !=
163 (divider << CCK_FREQUENCY_STATUS_SHIFT),
164 "%s change in progress\n", name);
165
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200166 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
167}
168
169static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
170 const char *name, u32 reg)
171{
172 if (dev_priv->hpll_freq == 0)
173 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
174
175 return vlv_get_cck_clock(dev_priv, name, reg,
176 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300177}
178
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200179static int
180intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200181{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200182 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200183}
184
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200185static int
186intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300187{
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200188 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
189 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200190}
191
192static int
193intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
194{
Jani Nikula79e50a42015-08-26 10:58:20 +0300195 uint32_t clkcfg;
196
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200197 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300198 clkcfg = I915_READ(CLKCFG);
199 switch (clkcfg & CLKCFG_FSB_MASK) {
200 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200201 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300202 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200203 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300204 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200205 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300206 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200207 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300208 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200209 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300210 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200211 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300212 /* these two are just a guess; one of them might be right */
213 case CLKCFG_FSB_1600:
214 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200215 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300216 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200217 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300218 }
219}
220
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200221static void intel_update_rawclk(struct drm_i915_private *dev_priv)
222{
223 if (HAS_PCH_SPLIT(dev_priv))
224 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
225 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
226 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
227 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
228 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
229 else
230 return; /* no rawclk on other platforms, or no need to know it */
231
232 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
233}
234
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300235static void intel_update_czclk(struct drm_i915_private *dev_priv)
236{
Wayne Boyer666a4532015-12-09 12:29:35 -0800237 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300238 return;
239
240 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
241 CCK_CZ_CLOCK_CONTROL);
242
243 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
244}
245
Chris Wilson021357a2010-09-07 20:54:59 +0100246static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200247intel_fdi_link_freq(struct drm_i915_private *dev_priv,
248 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100249{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200250 if (HAS_DDI(dev_priv))
251 return pipe_config->port_clock; /* SPLL */
252 else if (IS_GEN5(dev_priv))
253 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200254 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200255 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100256}
257
Daniel Vetter5d536e22013-07-06 12:52:06 +0200258static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400259 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200260 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200261 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400262 .m = { .min = 96, .max = 140 },
263 .m1 = { .min = 18, .max = 26 },
264 .m2 = { .min = 6, .max = 16 },
265 .p = { .min = 4, .max = 128 },
266 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700267 .p2 = { .dot_limit = 165000,
268 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700269};
270
Daniel Vetter5d536e22013-07-06 12:52:06 +0200271static const intel_limit_t intel_limits_i8xx_dvo = {
272 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200273 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200274 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200275 .m = { .min = 96, .max = 140 },
276 .m1 = { .min = 18, .max = 26 },
277 .m2 = { .min = 6, .max = 16 },
278 .p = { .min = 4, .max = 128 },
279 .p1 = { .min = 2, .max = 33 },
280 .p2 = { .dot_limit = 165000,
281 .p2_slow = 4, .p2_fast = 4 },
282};
283
Keith Packarde4b36692009-06-05 19:22:17 -0700284static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400285 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200286 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200287 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400288 .m = { .min = 96, .max = 140 },
289 .m1 = { .min = 18, .max = 26 },
290 .m2 = { .min = 6, .max = 16 },
291 .p = { .min = 4, .max = 128 },
292 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700293 .p2 = { .dot_limit = 165000,
294 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700295};
Eric Anholt273e27c2011-03-30 13:01:10 -0700296
Keith Packarde4b36692009-06-05 19:22:17 -0700297static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400298 .dot = { .min = 20000, .max = 400000 },
299 .vco = { .min = 1400000, .max = 2800000 },
300 .n = { .min = 1, .max = 6 },
301 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100302 .m1 = { .min = 8, .max = 18 },
303 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400304 .p = { .min = 5, .max = 80 },
305 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700306 .p2 = { .dot_limit = 200000,
307 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700308};
309
310static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400311 .dot = { .min = 20000, .max = 400000 },
312 .vco = { .min = 1400000, .max = 2800000 },
313 .n = { .min = 1, .max = 6 },
314 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100315 .m1 = { .min = 8, .max = 18 },
316 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400317 .p = { .min = 7, .max = 98 },
318 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700319 .p2 = { .dot_limit = 112000,
320 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700321};
322
Eric Anholt273e27c2011-03-30 13:01:10 -0700323
Keith Packarde4b36692009-06-05 19:22:17 -0700324static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700325 .dot = { .min = 25000, .max = 270000 },
326 .vco = { .min = 1750000, .max = 3500000},
327 .n = { .min = 1, .max = 4 },
328 .m = { .min = 104, .max = 138 },
329 .m1 = { .min = 17, .max = 23 },
330 .m2 = { .min = 5, .max = 11 },
331 .p = { .min = 10, .max = 30 },
332 .p1 = { .min = 1, .max = 3},
333 .p2 = { .dot_limit = 270000,
334 .p2_slow = 10,
335 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800336 },
Keith Packarde4b36692009-06-05 19:22:17 -0700337};
338
339static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700340 .dot = { .min = 22000, .max = 400000 },
341 .vco = { .min = 1750000, .max = 3500000},
342 .n = { .min = 1, .max = 4 },
343 .m = { .min = 104, .max = 138 },
344 .m1 = { .min = 16, .max = 23 },
345 .m2 = { .min = 5, .max = 11 },
346 .p = { .min = 5, .max = 80 },
347 .p1 = { .min = 1, .max = 8},
348 .p2 = { .dot_limit = 165000,
349 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700350};
351
352static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700353 .dot = { .min = 20000, .max = 115000 },
354 .vco = { .min = 1750000, .max = 3500000 },
355 .n = { .min = 1, .max = 3 },
356 .m = { .min = 104, .max = 138 },
357 .m1 = { .min = 17, .max = 23 },
358 .m2 = { .min = 5, .max = 11 },
359 .p = { .min = 28, .max = 112 },
360 .p1 = { .min = 2, .max = 8 },
361 .p2 = { .dot_limit = 0,
362 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800363 },
Keith Packarde4b36692009-06-05 19:22:17 -0700364};
365
366static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700367 .dot = { .min = 80000, .max = 224000 },
368 .vco = { .min = 1750000, .max = 3500000 },
369 .n = { .min = 1, .max = 3 },
370 .m = { .min = 104, .max = 138 },
371 .m1 = { .min = 17, .max = 23 },
372 .m2 = { .min = 5, .max = 11 },
373 .p = { .min = 14, .max = 42 },
374 .p1 = { .min = 2, .max = 6 },
375 .p2 = { .dot_limit = 0,
376 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800377 },
Keith Packarde4b36692009-06-05 19:22:17 -0700378};
379
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500380static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400381 .dot = { .min = 20000, .max = 400000},
382 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700383 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400384 .n = { .min = 3, .max = 6 },
385 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700386 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400387 .m1 = { .min = 0, .max = 0 },
388 .m2 = { .min = 0, .max = 254 },
389 .p = { .min = 5, .max = 80 },
390 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700391 .p2 = { .dot_limit = 200000,
392 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700393};
394
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500395static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400396 .dot = { .min = 20000, .max = 400000 },
397 .vco = { .min = 1700000, .max = 3500000 },
398 .n = { .min = 3, .max = 6 },
399 .m = { .min = 2, .max = 256 },
400 .m1 = { .min = 0, .max = 0 },
401 .m2 = { .min = 0, .max = 254 },
402 .p = { .min = 7, .max = 112 },
403 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700404 .p2 = { .dot_limit = 112000,
405 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700406};
407
Eric Anholt273e27c2011-03-30 13:01:10 -0700408/* Ironlake / Sandybridge
409 *
410 * We calculate clock using (register_value + 2) for N/M1/M2, so here
411 * the range value for them is (actual_value - 2).
412 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800413static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700414 .dot = { .min = 25000, .max = 350000 },
415 .vco = { .min = 1760000, .max = 3510000 },
416 .n = { .min = 1, .max = 5 },
417 .m = { .min = 79, .max = 127 },
418 .m1 = { .min = 12, .max = 22 },
419 .m2 = { .min = 5, .max = 9 },
420 .p = { .min = 5, .max = 80 },
421 .p1 = { .min = 1, .max = 8 },
422 .p2 = { .dot_limit = 225000,
423 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700424};
425
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800426static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700427 .dot = { .min = 25000, .max = 350000 },
428 .vco = { .min = 1760000, .max = 3510000 },
429 .n = { .min = 1, .max = 3 },
430 .m = { .min = 79, .max = 118 },
431 .m1 = { .min = 12, .max = 22 },
432 .m2 = { .min = 5, .max = 9 },
433 .p = { .min = 28, .max = 112 },
434 .p1 = { .min = 2, .max = 8 },
435 .p2 = { .dot_limit = 225000,
436 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800437};
438
439static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700440 .dot = { .min = 25000, .max = 350000 },
441 .vco = { .min = 1760000, .max = 3510000 },
442 .n = { .min = 1, .max = 3 },
443 .m = { .min = 79, .max = 127 },
444 .m1 = { .min = 12, .max = 22 },
445 .m2 = { .min = 5, .max = 9 },
446 .p = { .min = 14, .max = 56 },
447 .p1 = { .min = 2, .max = 8 },
448 .p2 = { .dot_limit = 225000,
449 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800450};
451
Eric Anholt273e27c2011-03-30 13:01:10 -0700452/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800453static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700454 .dot = { .min = 25000, .max = 350000 },
455 .vco = { .min = 1760000, .max = 3510000 },
456 .n = { .min = 1, .max = 2 },
457 .m = { .min = 79, .max = 126 },
458 .m1 = { .min = 12, .max = 22 },
459 .m2 = { .min = 5, .max = 9 },
460 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400461 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700462 .p2 = { .dot_limit = 225000,
463 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800464};
465
466static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700467 .dot = { .min = 25000, .max = 350000 },
468 .vco = { .min = 1760000, .max = 3510000 },
469 .n = { .min = 1, .max = 3 },
470 .m = { .min = 79, .max = 126 },
471 .m1 = { .min = 12, .max = 22 },
472 .m2 = { .min = 5, .max = 9 },
473 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400474 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700475 .p2 = { .dot_limit = 225000,
476 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800477};
478
Ville Syrjälädc730512013-09-24 21:26:30 +0300479static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300480 /*
481 * These are the data rate limits (measured in fast clocks)
482 * since those are the strictest limits we have. The fast
483 * clock and actual rate limits are more relaxed, so checking
484 * them would make no difference.
485 */
486 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200487 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700488 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700489 .m1 = { .min = 2, .max = 3 },
490 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300491 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300492 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700493};
494
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300495static const intel_limit_t intel_limits_chv = {
496 /*
497 * These are the data rate limits (measured in fast clocks)
498 * since those are the strictest limits we have. The fast
499 * clock and actual rate limits are more relaxed, so checking
500 * them would make no difference.
501 */
502 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200503 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300504 .n = { .min = 1, .max = 1 },
505 .m1 = { .min = 2, .max = 2 },
506 .m2 = { .min = 24 << 22, .max = 175 << 22 },
507 .p1 = { .min = 2, .max = 4 },
508 .p2 = { .p2_slow = 1, .p2_fast = 14 },
509};
510
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200511static const intel_limit_t intel_limits_bxt = {
512 /* FIXME: find real dot limits */
513 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530514 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200515 .n = { .min = 1, .max = 1 },
516 .m1 = { .min = 2, .max = 2 },
517 /* FIXME: find real m2 limits */
518 .m2 = { .min = 2 << 22, .max = 255 << 22 },
519 .p1 = { .min = 2, .max = 4 },
520 .p2 = { .p2_slow = 1, .p2_fast = 20 },
521};
522
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200523static bool
524needs_modeset(struct drm_crtc_state *state)
525{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200526 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200527}
528
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300529/**
530 * Returns whether any output on the specified pipe is of the specified type
531 */
Damien Lespiau40935612014-10-29 11:16:59 +0000532bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300533{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300534 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300535 struct intel_encoder *encoder;
536
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300537 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300538 if (encoder->type == type)
539 return true;
540
541 return false;
542}
543
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200544/**
545 * Returns whether any output on the specified pipe will have the specified
546 * type after a staged modeset is complete, i.e., the same as
547 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
548 * encoder->crtc.
549 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200550static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
551 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200552{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200553 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300554 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200555 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200556 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200557 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200558
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300559 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200560 if (connector_state->crtc != crtc_state->base.crtc)
561 continue;
562
563 num_connectors++;
564
565 encoder = to_intel_encoder(connector_state->best_encoder);
566 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200567 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200568 }
569
570 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200571
572 return false;
573}
574
Imre Deakdccbea32015-06-22 23:35:51 +0300575/*
576 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
577 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
578 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
579 * The helpers' return value is the rate of the clock that is fed to the
580 * display engine's pipe which can be the above fast dot clock rate or a
581 * divided-down version of it.
582 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500583/* m1 is reserved as 0 in Pineview, n is a ring counter */
Imre Deakdccbea32015-06-22 23:35:51 +0300584static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800585{
Shaohua Li21778322009-02-23 15:19:16 +0800586 clock->m = clock->m2 + 2;
587 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200588 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300589 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300590 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
591 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300592
593 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800594}
595
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200596static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
597{
598 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
599}
600
Imre Deakdccbea32015-06-22 23:35:51 +0300601static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800602{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200603 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800604 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200605 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300606 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300607 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
608 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300609
610 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800611}
612
Imre Deakdccbea32015-06-22 23:35:51 +0300613static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300614{
615 clock->m = clock->m1 * clock->m2;
616 clock->p = clock->p1 * clock->p2;
617 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300618 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300619 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
620 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300621
622 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300623}
624
Imre Deakdccbea32015-06-22 23:35:51 +0300625int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300626{
627 clock->m = clock->m1 * clock->m2;
628 clock->p = clock->p1 * clock->p2;
629 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300630 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300631 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
632 clock->n << 22);
633 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300634
635 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300636}
637
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800638#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800639/**
640 * Returns whether the given set of divisors are valid for a given refclk with
641 * the given connectors.
642 */
643
Chris Wilson1b894b52010-12-14 20:04:54 +0000644static bool intel_PLL_is_valid(struct drm_device *dev,
645 const intel_limit_t *limit,
646 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800647{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300648 if (clock->n < limit->n.min || limit->n.max < clock->n)
649 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800650 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400651 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800652 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400653 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800654 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400655 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300656
Wayne Boyer666a4532015-12-09 12:29:35 -0800657 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
658 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300659 if (clock->m1 <= clock->m2)
660 INTELPllInvalid("m1 <= m2\n");
661
Wayne Boyer666a4532015-12-09 12:29:35 -0800662 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300663 if (clock->p < limit->p.min || limit->p.max < clock->p)
664 INTELPllInvalid("p out of range\n");
665 if (clock->m < limit->m.min || limit->m.max < clock->m)
666 INTELPllInvalid("m out of range\n");
667 }
668
Jesse Barnes79e53942008-11-07 14:24:08 -0800669 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400670 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800671 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
672 * connector, etc., rather than just a single range.
673 */
674 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400675 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800676
677 return true;
678}
679
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300680static int
681i9xx_select_p2_div(const intel_limit_t *limit,
682 const struct intel_crtc_state *crtc_state,
683 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800684{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300685 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800686
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200687 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800688 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100689 * For LVDS just rely on its current settings for dual-channel.
690 * We haven't figured out how to reliably set up different
691 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800692 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100693 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300694 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800695 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300696 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800697 } else {
698 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300699 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800700 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300701 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800702 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300703}
704
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200705/*
706 * Returns a set of divisors for the desired target clock with the given
707 * refclk, or FALSE. The returned values represent the clock equation:
708 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
709 *
710 * Target and reference clocks are specified in kHz.
711 *
712 * If match_clock is provided, then best_clock P divider must match the P
713 * divider from @match_clock used for LVDS downclocking.
714 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300715static bool
716i9xx_find_best_dpll(const intel_limit_t *limit,
717 struct intel_crtc_state *crtc_state,
718 int target, int refclk, intel_clock_t *match_clock,
719 intel_clock_t *best_clock)
720{
721 struct drm_device *dev = crtc_state->base.crtc->dev;
722 intel_clock_t clock;
723 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800724
Akshay Joshi0206e352011-08-16 15:34:10 -0400725 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800726
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300727 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
728
Zhao Yakui42158662009-11-20 11:24:18 +0800729 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
730 clock.m1++) {
731 for (clock.m2 = limit->m2.min;
732 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200733 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800734 break;
735 for (clock.n = limit->n.min;
736 clock.n <= limit->n.max; clock.n++) {
737 for (clock.p1 = limit->p1.min;
738 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800739 int this_err;
740
Imre Deakdccbea32015-06-22 23:35:51 +0300741 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000742 if (!intel_PLL_is_valid(dev, limit,
743 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800744 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800745 if (match_clock &&
746 clock.p != match_clock->p)
747 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800748
749 this_err = abs(clock.dot - target);
750 if (this_err < err) {
751 *best_clock = clock;
752 err = this_err;
753 }
754 }
755 }
756 }
757 }
758
759 return (err != target);
760}
761
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200762/*
763 * Returns a set of divisors for the desired target clock with the given
764 * refclk, or FALSE. The returned values represent the clock equation:
765 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
766 *
767 * Target and reference clocks are specified in kHz.
768 *
769 * If match_clock is provided, then best_clock P divider must match the P
770 * divider from @match_clock used for LVDS downclocking.
771 */
Ma Lingd4906092009-03-18 20:13:27 +0800772static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200773pnv_find_best_dpll(const intel_limit_t *limit,
774 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200775 int target, int refclk, intel_clock_t *match_clock,
776 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200777{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300778 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200779 intel_clock_t clock;
780 int err = target;
781
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200782 memset(best_clock, 0, sizeof(*best_clock));
783
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300784 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
785
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200786 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
787 clock.m1++) {
788 for (clock.m2 = limit->m2.min;
789 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200790 for (clock.n = limit->n.min;
791 clock.n <= limit->n.max; clock.n++) {
792 for (clock.p1 = limit->p1.min;
793 clock.p1 <= limit->p1.max; clock.p1++) {
794 int this_err;
795
Imre Deakdccbea32015-06-22 23:35:51 +0300796 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800797 if (!intel_PLL_is_valid(dev, limit,
798 &clock))
799 continue;
800 if (match_clock &&
801 clock.p != match_clock->p)
802 continue;
803
804 this_err = abs(clock.dot - target);
805 if (this_err < err) {
806 *best_clock = clock;
807 err = this_err;
808 }
809 }
810 }
811 }
812 }
813
814 return (err != target);
815}
816
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200817/*
818 * Returns a set of divisors for the desired target clock with the given
819 * refclk, or FALSE. The returned values represent the clock equation:
820 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200821 *
822 * Target and reference clocks are specified in kHz.
823 *
824 * If match_clock is provided, then best_clock P divider must match the P
825 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200826 */
Ma Lingd4906092009-03-18 20:13:27 +0800827static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200828g4x_find_best_dpll(const intel_limit_t *limit,
829 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200830 int target, int refclk, intel_clock_t *match_clock,
831 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800832{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300833 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800834 intel_clock_t clock;
835 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300836 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400837 /* approximately equals target * 0.00585 */
838 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800839
840 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300841
842 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
843
Ma Lingd4906092009-03-18 20:13:27 +0800844 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200845 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800846 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200847 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800848 for (clock.m1 = limit->m1.max;
849 clock.m1 >= limit->m1.min; clock.m1--) {
850 for (clock.m2 = limit->m2.max;
851 clock.m2 >= limit->m2.min; clock.m2--) {
852 for (clock.p1 = limit->p1.max;
853 clock.p1 >= limit->p1.min; clock.p1--) {
854 int this_err;
855
Imre Deakdccbea32015-06-22 23:35:51 +0300856 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000857 if (!intel_PLL_is_valid(dev, limit,
858 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800859 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000860
861 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800862 if (this_err < err_most) {
863 *best_clock = clock;
864 err_most = this_err;
865 max_n = clock.n;
866 found = true;
867 }
868 }
869 }
870 }
871 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800872 return found;
873}
Ma Lingd4906092009-03-18 20:13:27 +0800874
Imre Deakd5dd62b2015-03-17 11:40:03 +0200875/*
876 * Check if the calculated PLL configuration is more optimal compared to the
877 * best configuration and error found so far. Return the calculated error.
878 */
879static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
880 const intel_clock_t *calculated_clock,
881 const intel_clock_t *best_clock,
882 unsigned int best_error_ppm,
883 unsigned int *error_ppm)
884{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200885 /*
886 * For CHV ignore the error and consider only the P value.
887 * Prefer a bigger P value based on HW requirements.
888 */
889 if (IS_CHERRYVIEW(dev)) {
890 *error_ppm = 0;
891
892 return calculated_clock->p > best_clock->p;
893 }
894
Imre Deak24be4e42015-03-17 11:40:04 +0200895 if (WARN_ON_ONCE(!target_freq))
896 return false;
897
Imre Deakd5dd62b2015-03-17 11:40:03 +0200898 *error_ppm = div_u64(1000000ULL *
899 abs(target_freq - calculated_clock->dot),
900 target_freq);
901 /*
902 * Prefer a better P value over a better (smaller) error if the error
903 * is small. Ensure this preference for future configurations too by
904 * setting the error to 0.
905 */
906 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
907 *error_ppm = 0;
908
909 return true;
910 }
911
912 return *error_ppm + 10 < best_error_ppm;
913}
914
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200915/*
916 * Returns a set of divisors for the desired target clock with the given
917 * refclk, or FALSE. The returned values represent the clock equation:
918 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
919 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800920static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200921vlv_find_best_dpll(const intel_limit_t *limit,
922 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200923 int target, int refclk, intel_clock_t *match_clock,
924 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700925{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200926 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300927 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300928 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300929 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300930 /* min update 19.2 MHz */
931 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300932 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700933
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300934 target *= 5; /* fast clock */
935
936 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700937
938 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300939 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300940 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300941 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300942 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300943 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700944 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300945 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200946 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300947
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300948 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
949 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300950
Imre Deakdccbea32015-06-22 23:35:51 +0300951 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300952
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300953 if (!intel_PLL_is_valid(dev, limit,
954 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300955 continue;
956
Imre Deakd5dd62b2015-03-17 11:40:03 +0200957 if (!vlv_PLL_is_optimal(dev, target,
958 &clock,
959 best_clock,
960 bestppm, &ppm))
961 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300962
Imre Deakd5dd62b2015-03-17 11:40:03 +0200963 *best_clock = clock;
964 bestppm = ppm;
965 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700966 }
967 }
968 }
969 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700970
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300971 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700972}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700973
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200974/*
975 * Returns a set of divisors for the desired target clock with the given
976 * refclk, or FALSE. The returned values represent the clock equation:
977 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
978 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300979static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200980chv_find_best_dpll(const intel_limit_t *limit,
981 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300982 int target, int refclk, intel_clock_t *match_clock,
983 intel_clock_t *best_clock)
984{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200985 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300986 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200987 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300988 intel_clock_t clock;
989 uint64_t m2;
990 int found = false;
991
992 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200993 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300994
995 /*
996 * Based on hardware doc, the n always set to 1, and m1 always
997 * set to 2. If requires to support 200Mhz refclk, we need to
998 * revisit this because n may not 1 anymore.
999 */
1000 clock.n = 1, clock.m1 = 2;
1001 target *= 5; /* fast clock */
1002
1003 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1004 for (clock.p2 = limit->p2.p2_fast;
1005 clock.p2 >= limit->p2.p2_slow;
1006 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001007 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001008
1009 clock.p = clock.p1 * clock.p2;
1010
1011 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1012 clock.n) << 22, refclk * clock.m1);
1013
1014 if (m2 > INT_MAX/clock.m1)
1015 continue;
1016
1017 clock.m2 = m2;
1018
Imre Deakdccbea32015-06-22 23:35:51 +03001019 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001020
1021 if (!intel_PLL_is_valid(dev, limit, &clock))
1022 continue;
1023
Imre Deak9ca3ba02015-03-17 11:40:05 +02001024 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1025 best_error_ppm, &error_ppm))
1026 continue;
1027
1028 *best_clock = clock;
1029 best_error_ppm = error_ppm;
1030 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001031 }
1032 }
1033
1034 return found;
1035}
1036
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001037bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1038 intel_clock_t *best_clock)
1039{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001040 int refclk = 100000;
1041 const intel_limit_t *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001042
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001043 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001044 target_clock, refclk, NULL, best_clock);
1045}
1046
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001047bool intel_crtc_active(struct drm_crtc *crtc)
1048{
1049 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1050
1051 /* Be paranoid as we can arrive here with only partial
1052 * state retrieved from the hardware during setup.
1053 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001054 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001055 * as Haswell has gained clock readout/fastboot support.
1056 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001057 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001058 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001059 *
1060 * FIXME: The intel_crtc->active here should be switched to
1061 * crtc->state->active once we have proper CRTC states wired up
1062 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001063 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001064 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001065 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001066}
1067
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001068enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1069 enum pipe pipe)
1070{
1071 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1072 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1073
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001074 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001075}
1076
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001077static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1078{
1079 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001080 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001081 u32 line1, line2;
1082 u32 line_mask;
1083
1084 if (IS_GEN2(dev))
1085 line_mask = DSL_LINEMASK_GEN2;
1086 else
1087 line_mask = DSL_LINEMASK_GEN3;
1088
1089 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001090 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001091 line2 = I915_READ(reg) & line_mask;
1092
1093 return line1 == line2;
1094}
1095
Keith Packardab7ad7f2010-10-03 00:33:06 -07001096/*
1097 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001098 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001099 *
1100 * After disabling a pipe, we can't wait for vblank in the usual way,
1101 * spinning on the vblank interrupt status bit, since we won't actually
1102 * see an interrupt when the pipe is disabled.
1103 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001104 * On Gen4 and above:
1105 * wait for the pipe register state bit to turn off
1106 *
1107 * Otherwise:
1108 * wait for the display line value to settle (it usually
1109 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001110 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001111 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001112static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001113{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001114 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001115 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001116 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001117 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001118
Keith Packardab7ad7f2010-10-03 00:33:06 -07001119 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001120 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001121
Keith Packardab7ad7f2010-10-03 00:33:06 -07001122 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001123 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1124 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001125 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001126 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001127 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001128 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001129 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001130 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001131}
1132
Jesse Barnesb24e7172011-01-04 15:09:30 -08001133/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001134void assert_pll(struct drm_i915_private *dev_priv,
1135 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001136{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001137 u32 val;
1138 bool cur_state;
1139
Ville Syrjälä649636e2015-09-22 19:50:01 +03001140 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001141 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001142 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001143 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001144 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001145}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001146
Jani Nikula23538ef2013-08-27 15:12:22 +03001147/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001148void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001149{
1150 u32 val;
1151 bool cur_state;
1152
Ville Syrjäläa5805162015-05-26 20:42:30 +03001153 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001154 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001155 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001156
1157 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001158 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001159 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001160 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001161}
Jani Nikula23538ef2013-08-27 15:12:22 +03001162
Jesse Barnes040484a2011-01-03 12:14:26 -08001163static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1164 enum pipe pipe, bool state)
1165{
Jesse Barnes040484a2011-01-03 12:14:26 -08001166 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001167 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1168 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001169
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001170 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001171 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001172 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001173 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001174 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001175 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001176 cur_state = !!(val & FDI_TX_ENABLE);
1177 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001178 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001179 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001180 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001181}
1182#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1183#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1184
1185static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1186 enum pipe pipe, bool state)
1187{
Jesse Barnes040484a2011-01-03 12:14:26 -08001188 u32 val;
1189 bool cur_state;
1190
Ville Syrjälä649636e2015-09-22 19:50:01 +03001191 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001192 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001193 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001194 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001195 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001196}
1197#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1198#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1199
1200static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1201 enum pipe pipe)
1202{
Jesse Barnes040484a2011-01-03 12:14:26 -08001203 u32 val;
1204
1205 /* ILK FDI PLL is always enabled */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001206 if (INTEL_INFO(dev_priv)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001207 return;
1208
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001209 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001210 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001211 return;
1212
Ville Syrjälä649636e2015-09-22 19:50:01 +03001213 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001214 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 -08001215}
1216
Daniel Vetter55607e82013-06-16 21:42:39 +02001217void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1218 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001219{
Jesse Barnes040484a2011-01-03 12:14:26 -08001220 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001221 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001222
Ville Syrjälä649636e2015-09-22 19:50:01 +03001223 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001224 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001225 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001226 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001227 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001228}
1229
Daniel Vetterb680c372014-09-19 18:27:27 +02001230void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1231 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001232{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001233 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001234 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001235 u32 val;
1236 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001237 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001238
Jani Nikulabedd4db2014-08-22 15:04:13 +03001239 if (WARN_ON(HAS_DDI(dev)))
1240 return;
1241
1242 if (HAS_PCH_SPLIT(dev)) {
1243 u32 port_sel;
1244
Jesse Barnesea0760c2011-01-04 15:09:32 -08001245 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001246 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1247
1248 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1249 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1250 panel_pipe = PIPE_B;
1251 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001252 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001253 /* presumably write lock depends on pipe, not port select */
1254 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1255 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001256 } else {
1257 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001258 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1259 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001260 }
1261
1262 val = I915_READ(pp_reg);
1263 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001264 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001265 locked = false;
1266
Rob Clarke2c719b2014-12-15 13:56:32 -05001267 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001268 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001269 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001270}
1271
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001272static void assert_cursor(struct drm_i915_private *dev_priv,
1273 enum pipe pipe, bool state)
1274{
1275 struct drm_device *dev = dev_priv->dev;
1276 bool cur_state;
1277
Paulo Zanonid9d82082014-02-27 16:30:56 -03001278 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001279 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001280 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001281 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001282
Rob Clarke2c719b2014-12-15 13:56:32 -05001283 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001284 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001285 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001286}
1287#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1288#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1289
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001290void assert_pipe(struct drm_i915_private *dev_priv,
1291 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001292{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001293 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001294 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1295 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001296 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001297
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001298 /* if we need the pipe quirk it must be always on */
1299 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1300 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001301 state = true;
1302
Imre Deak4feed0e2016-02-12 18:55:14 +02001303 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1304 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001305 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001306 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001307
1308 intel_display_power_put(dev_priv, power_domain);
1309 } else {
1310 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001311 }
1312
Rob Clarke2c719b2014-12-15 13:56:32 -05001313 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001314 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001315 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001316}
1317
Chris Wilson931872f2012-01-16 23:01:13 +00001318static void assert_plane(struct drm_i915_private *dev_priv,
1319 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001320{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001321 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001322 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001323
Ville Syrjälä649636e2015-09-22 19:50:01 +03001324 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001325 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001326 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001327 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001328 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001329}
1330
Chris Wilson931872f2012-01-16 23:01:13 +00001331#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1332#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1333
Jesse Barnesb24e7172011-01-04 15:09:30 -08001334static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1335 enum pipe pipe)
1336{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001337 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001338 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001339
Ville Syrjälä653e1022013-06-04 13:49:05 +03001340 /* Primary planes are fixed to pipes on gen4+ */
1341 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001342 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001343 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001344 "plane %c assertion failure, should be disabled but not\n",
1345 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001346 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001347 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001348
Jesse Barnesb24e7172011-01-04 15:09:30 -08001349 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001350 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001351 u32 val = I915_READ(DSPCNTR(i));
1352 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001353 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001354 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001355 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1356 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001357 }
1358}
1359
Jesse Barnes19332d72013-03-28 09:55:38 -07001360static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1361 enum pipe pipe)
1362{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001363 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001364 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001365
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001366 if (INTEL_INFO(dev)->gen >= 9) {
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(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001369 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001370 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1371 sprite, pipe_name(pipe));
1372 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001373 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001374 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001375 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001376 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001377 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001378 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001379 }
1380 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001381 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001382 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001383 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001384 plane_name(pipe), pipe_name(pipe));
1385 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001386 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001387 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001388 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1389 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001390 }
1391}
1392
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001393static void assert_vblank_disabled(struct drm_crtc *crtc)
1394{
Rob Clarke2c719b2014-12-15 13:56:32 -05001395 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001396 drm_crtc_vblank_put(crtc);
1397}
1398
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001399void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1400 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001401{
Jesse Barnes92f25842011-01-04 15:09:34 -08001402 u32 val;
1403 bool enabled;
1404
Ville Syrjälä649636e2015-09-22 19:50:01 +03001405 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001406 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001407 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001408 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1409 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001410}
1411
Keith Packard4e634382011-08-06 10:39:45 -07001412static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1413 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001414{
1415 if ((val & DP_PORT_EN) == 0)
1416 return false;
1417
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001418 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001419 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001420 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1421 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001422 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001423 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1424 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001425 } else {
1426 if ((val & DP_PIPE_MASK) != (pipe << 30))
1427 return false;
1428 }
1429 return true;
1430}
1431
Keith Packard1519b992011-08-06 10:35:34 -07001432static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1433 enum pipe pipe, u32 val)
1434{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001435 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001436 return false;
1437
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001438 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001439 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001440 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001441 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001442 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1443 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001444 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001445 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001446 return false;
1447 }
1448 return true;
1449}
1450
1451static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1452 enum pipe pipe, u32 val)
1453{
1454 if ((val & LVDS_PORT_EN) == 0)
1455 return false;
1456
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001457 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001458 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1459 return false;
1460 } else {
1461 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1462 return false;
1463 }
1464 return true;
1465}
1466
1467static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1468 enum pipe pipe, u32 val)
1469{
1470 if ((val & ADPA_DAC_ENABLE) == 0)
1471 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001472 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001473 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1474 return false;
1475 } else {
1476 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1477 return false;
1478 }
1479 return true;
1480}
1481
Jesse Barnes291906f2011-02-02 12:28:03 -08001482static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001483 enum pipe pipe, i915_reg_t reg,
1484 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001485{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001486 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001487 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001488 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001489 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001490
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001491 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001492 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001493 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001494}
1495
1496static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001497 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001498{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001499 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001500 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001501 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001502 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001503
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001504 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001505 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001506 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001507}
1508
1509static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1510 enum pipe pipe)
1511{
Jesse Barnes291906f2011-02-02 12:28:03 -08001512 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001513
Keith Packardf0575e92011-07-25 22:12:43 -07001514 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1515 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1516 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001517
Ville Syrjälä649636e2015-09-22 19:50:01 +03001518 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001519 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001520 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001521 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001522
Ville Syrjälä649636e2015-09-22 19:50:01 +03001523 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001524 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001525 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001526 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001527
Paulo Zanonie2debe92013-02-18 19:00:27 -03001528 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1529 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1530 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001531}
1532
Ville Syrjäläd288f652014-10-28 13:20:22 +02001533static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001534 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001535{
Daniel Vetter426115c2013-07-11 22:13:42 +02001536 struct drm_device *dev = crtc->base.dev;
1537 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001538 enum pipe pipe = crtc->pipe;
1539 i915_reg_t reg = DPLL(pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001540 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001541
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001542 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001543
Daniel Vetter87442f72013-06-06 00:52:17 +02001544 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001545 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001546
Daniel Vetter426115c2013-07-11 22:13:42 +02001547 I915_WRITE(reg, dpll);
1548 POSTING_READ(reg);
1549 udelay(150);
1550
1551 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001552 DRM_ERROR("DPLL %d failed to lock\n", pipe);
Daniel Vetter426115c2013-07-11 22:13:42 +02001553
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001554 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1555 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001556}
1557
Ville Syrjäläd288f652014-10-28 13:20:22 +02001558static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001559 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001560{
1561 struct drm_device *dev = crtc->base.dev;
1562 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001563 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001564 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001565 u32 tmp;
1566
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001567 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001568
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001569 /* PLL is protected by panel, make sure we can write it */
1570 assert_panel_unlocked(dev_priv, pipe);
1571
Ville Syrjäläa5805162015-05-26 20:42:30 +03001572 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001573
1574 /* Enable back the 10bit clock to display controller */
1575 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1576 tmp |= DPIO_DCLKP_EN;
1577 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1578
Ville Syrjälä54433e92015-05-26 20:42:31 +03001579 mutex_unlock(&dev_priv->sb_lock);
1580
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001581 /*
1582 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1583 */
1584 udelay(1);
1585
1586 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001587 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001588
1589 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001590 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001591 DRM_ERROR("PLL %d failed to lock\n", pipe);
1592
Ville Syrjäläc2317752016-03-15 16:39:56 +02001593 if (pipe != PIPE_A) {
1594 /*
1595 * WaPixelRepeatModeFixForC0:chv
1596 *
1597 * DPLLCMD is AWOL. Use chicken bits to propagate
1598 * the value from DPLLBMD to either pipe B or C.
1599 */
1600 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1601 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1602 I915_WRITE(CBR4_VLV, 0);
1603 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1604
1605 /*
1606 * DPLLB VGA mode also seems to cause problems.
1607 * We should always have it disabled.
1608 */
1609 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1610 } else {
1611 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1612 POSTING_READ(DPLL_MD(pipe));
1613 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001614}
1615
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001616static int intel_num_dvo_pipes(struct drm_device *dev)
1617{
1618 struct intel_crtc *crtc;
1619 int count = 0;
1620
1621 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001622 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001623 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001624
1625 return count;
1626}
1627
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001628static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001629{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001630 struct drm_device *dev = crtc->base.dev;
1631 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001632 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001633 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001634
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001635 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001636
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001637 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001638 if (IS_MOBILE(dev) && !IS_I830(dev))
1639 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001640
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001641 /* Enable DVO 2x clock on both PLLs if necessary */
1642 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1643 /*
1644 * It appears to be important that we don't enable this
1645 * for the current pipe before otherwise configuring the
1646 * PLL. No idea how this should be handled if multiple
1647 * DVO outputs are enabled simultaneosly.
1648 */
1649 dpll |= DPLL_DVO_2X_MODE;
1650 I915_WRITE(DPLL(!crtc->pipe),
1651 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1652 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001653
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001654 /*
1655 * Apparently we need to have VGA mode enabled prior to changing
1656 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1657 * dividers, even though the register value does change.
1658 */
1659 I915_WRITE(reg, 0);
1660
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001661 I915_WRITE(reg, dpll);
1662
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001663 /* Wait for the clocks to stabilize. */
1664 POSTING_READ(reg);
1665 udelay(150);
1666
1667 if (INTEL_INFO(dev)->gen >= 4) {
1668 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001669 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001670 } else {
1671 /* The pixel multiplier can only be updated once the
1672 * DPLL is enabled and the clocks are stable.
1673 *
1674 * So write it again.
1675 */
1676 I915_WRITE(reg, dpll);
1677 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001678
1679 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001680 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001681 POSTING_READ(reg);
1682 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001683 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001684 POSTING_READ(reg);
1685 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001686 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001687 POSTING_READ(reg);
1688 udelay(150); /* wait for warmup */
1689}
1690
1691/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001692 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001693 * @dev_priv: i915 private structure
1694 * @pipe: pipe PLL to disable
1695 *
1696 * Disable the PLL for @pipe, making sure the pipe is off first.
1697 *
1698 * Note! This is for pre-ILK only.
1699 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001700static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001701{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001702 struct drm_device *dev = crtc->base.dev;
1703 struct drm_i915_private *dev_priv = dev->dev_private;
1704 enum pipe pipe = crtc->pipe;
1705
1706 /* Disable DVO 2x clock on both PLLs if necessary */
1707 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001708 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001709 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001710 I915_WRITE(DPLL(PIPE_B),
1711 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1712 I915_WRITE(DPLL(PIPE_A),
1713 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1714 }
1715
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001716 /* Don't disable pipe or pipe PLLs if needed */
1717 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1718 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001719 return;
1720
1721 /* Make sure the pipe isn't still relying on us */
1722 assert_pipe_disabled(dev_priv, pipe);
1723
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001724 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001725 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001726}
1727
Jesse Barnesf6071162013-10-01 10:41:38 -07001728static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1729{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001730 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001731
1732 /* Make sure the pipe isn't still relying on us */
1733 assert_pipe_disabled(dev_priv, pipe);
1734
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001735 val = DPLL_INTEGRATED_REF_CLK_VLV |
1736 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1737 if (pipe != PIPE_A)
1738 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1739
Jesse Barnesf6071162013-10-01 10:41:38 -07001740 I915_WRITE(DPLL(pipe), val);
1741 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001742}
1743
1744static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1745{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001746 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001747 u32 val;
1748
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001749 /* Make sure the pipe isn't still relying on us */
1750 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001751
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001752 val = DPLL_SSC_REF_CLK_CHV |
1753 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001754 if (pipe != PIPE_A)
1755 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001756
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001757 I915_WRITE(DPLL(pipe), val);
1758 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001759
Ville Syrjäläa5805162015-05-26 20:42:30 +03001760 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001761
1762 /* Disable 10bit clock to display controller */
1763 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1764 val &= ~DPIO_DCLKP_EN;
1765 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1766
Ville Syrjäläa5805162015-05-26 20:42:30 +03001767 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001768}
1769
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001770void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001771 struct intel_digital_port *dport,
1772 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001773{
1774 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001775 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001776
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001777 switch (dport->port) {
1778 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001779 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001780 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001781 break;
1782 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001783 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001784 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001785 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001786 break;
1787 case PORT_D:
1788 port_mask = DPLL_PORTD_READY_MASK;
1789 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001790 break;
1791 default:
1792 BUG();
1793 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001794
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001795 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1796 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1797 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001798}
1799
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001800static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1801 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001802{
Daniel Vetter23670b322012-11-01 09:15:30 +01001803 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001804 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001805 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001806 i915_reg_t reg;
1807 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001808
Jesse Barnes040484a2011-01-03 12:14:26 -08001809 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001810 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001811
1812 /* FDI must be feeding us bits for PCH ports */
1813 assert_fdi_tx_enabled(dev_priv, pipe);
1814 assert_fdi_rx_enabled(dev_priv, pipe);
1815
Daniel Vetter23670b322012-11-01 09:15:30 +01001816 if (HAS_PCH_CPT(dev)) {
1817 /* Workaround: Set the timing override bit before enabling the
1818 * pch transcoder. */
1819 reg = TRANS_CHICKEN2(pipe);
1820 val = I915_READ(reg);
1821 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1822 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001823 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001824
Daniel Vetterab9412b2013-05-03 11:49:46 +02001825 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001826 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001827 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001828
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001829 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001830 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001831 * Make the BPC in transcoder be consistent with
1832 * that in pipeconf reg. For HDMI we must use 8bpc
1833 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001834 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001835 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001836 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1837 val |= PIPECONF_8BPC;
1838 else
1839 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001840 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001841
1842 val &= ~TRANS_INTERLACE_MASK;
1843 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001844 if (HAS_PCH_IBX(dev_priv) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001845 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001846 val |= TRANS_LEGACY_INTERLACED_ILK;
1847 else
1848 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001849 else
1850 val |= TRANS_PROGRESSIVE;
1851
Jesse Barnes040484a2011-01-03 12:14:26 -08001852 I915_WRITE(reg, val | TRANS_ENABLE);
1853 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001854 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001855}
1856
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001857static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001858 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001859{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001860 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001861
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001862 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001863 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001864 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001865
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001866 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001867 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001868 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001869 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001870
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001871 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001872 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001873
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001874 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1875 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001876 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001877 else
1878 val |= TRANS_PROGRESSIVE;
1879
Daniel Vetterab9412b2013-05-03 11:49:46 +02001880 I915_WRITE(LPT_TRANSCONF, val);
1881 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001882 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001883}
1884
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001885static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1886 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001887{
Daniel Vetter23670b322012-11-01 09:15:30 +01001888 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001889 i915_reg_t reg;
1890 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001891
1892 /* FDI relies on the transcoder */
1893 assert_fdi_tx_disabled(dev_priv, pipe);
1894 assert_fdi_rx_disabled(dev_priv, pipe);
1895
Jesse Barnes291906f2011-02-02 12:28:03 -08001896 /* Ports must be off as well */
1897 assert_pch_ports_disabled(dev_priv, pipe);
1898
Daniel Vetterab9412b2013-05-03 11:49:46 +02001899 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001900 val = I915_READ(reg);
1901 val &= ~TRANS_ENABLE;
1902 I915_WRITE(reg, val);
1903 /* wait for PCH transcoder off, transcoder state */
1904 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001905 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001906
Ville Syrjäläc4656132015-10-29 21:25:56 +02001907 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001908 /* Workaround: Clear the timing override chicken bit again. */
1909 reg = TRANS_CHICKEN2(pipe);
1910 val = I915_READ(reg);
1911 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1912 I915_WRITE(reg, val);
1913 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001914}
1915
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001916static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001917{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001918 u32 val;
1919
Daniel Vetterab9412b2013-05-03 11:49:46 +02001920 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001921 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001922 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001923 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001924 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001925 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001926
1927 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001928 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001929 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001930 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001931}
1932
1933/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001934 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001935 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001936 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001937 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001938 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001939 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001940static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001941{
Paulo Zanoni03722642014-01-17 13:51:09 -02001942 struct drm_device *dev = crtc->base.dev;
1943 struct drm_i915_private *dev_priv = dev->dev_private;
1944 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001945 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001946 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001947 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001948 u32 val;
1949
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001950 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1951
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001952 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001953 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001954 assert_sprites_disabled(dev_priv, pipe);
1955
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001956 if (HAS_PCH_LPT(dev_priv))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001957 pch_transcoder = TRANSCODER_A;
1958 else
1959 pch_transcoder = pipe;
1960
Jesse Barnesb24e7172011-01-04 15:09:30 -08001961 /*
1962 * A pipe without a PLL won't actually be able to drive bits from
1963 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1964 * need the check.
1965 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001966 if (HAS_GMCH_DISPLAY(dev_priv))
Jani Nikulaa65347b2015-11-27 12:21:46 +02001967 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03001968 assert_dsi_pll_enabled(dev_priv);
1969 else
1970 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001971 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001972 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001973 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001974 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001975 assert_fdi_tx_pll_enabled(dev_priv,
1976 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001977 }
1978 /* FIXME: assert CPU port conditions for SNB+ */
1979 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001980
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001981 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001982 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001983 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001984 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1985 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00001986 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001987 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001988
1989 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001990 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001991
1992 /*
1993 * Until the pipe starts DSL will read as 0, which would cause
1994 * an apparent vblank timestamp jump, which messes up also the
1995 * frame count when it's derived from the timestamps. So let's
1996 * wait for the pipe to start properly before we call
1997 * drm_crtc_vblank_on()
1998 */
1999 if (dev->max_vblank_count == 0 &&
2000 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2001 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002002}
2003
2004/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002005 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002006 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002007 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002008 * Disable the pipe of @crtc, making sure that various hardware
2009 * specific requirements are met, if applicable, e.g. plane
2010 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002011 *
2012 * Will wait until the pipe has shut down before returning.
2013 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002014static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002015{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002016 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002017 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002018 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002019 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002020 u32 val;
2021
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002022 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2023
Jesse Barnesb24e7172011-01-04 15:09:30 -08002024 /*
2025 * Make sure planes won't keep trying to pump pixels to us,
2026 * or we might hang the display.
2027 */
2028 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002029 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002030 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002031
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002032 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002033 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002034 if ((val & PIPECONF_ENABLE) == 0)
2035 return;
2036
Ville Syrjälä67adc642014-08-15 01:21:57 +03002037 /*
2038 * Double wide has implications for planes
2039 * so best keep it disabled when not needed.
2040 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002041 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002042 val &= ~PIPECONF_DOUBLE_WIDE;
2043
2044 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002045 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2046 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002047 val &= ~PIPECONF_ENABLE;
2048
2049 I915_WRITE(reg, val);
2050 if ((val & PIPECONF_ENABLE) == 0)
2051 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002052}
2053
Chris Wilson693db182013-03-05 14:52:39 +00002054static bool need_vtd_wa(struct drm_device *dev)
2055{
2056#ifdef CONFIG_INTEL_IOMMU
2057 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2058 return true;
2059#endif
2060 return false;
2061}
2062
Ville Syrjälä832be822016-01-12 21:08:33 +02002063static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2064{
2065 return IS_GEN2(dev_priv) ? 2048 : 4096;
2066}
2067
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002068static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2069 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002070{
2071 switch (fb_modifier) {
2072 case DRM_FORMAT_MOD_NONE:
2073 return cpp;
2074 case I915_FORMAT_MOD_X_TILED:
2075 if (IS_GEN2(dev_priv))
2076 return 128;
2077 else
2078 return 512;
2079 case I915_FORMAT_MOD_Y_TILED:
2080 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2081 return 128;
2082 else
2083 return 512;
2084 case I915_FORMAT_MOD_Yf_TILED:
2085 switch (cpp) {
2086 case 1:
2087 return 64;
2088 case 2:
2089 case 4:
2090 return 128;
2091 case 8:
2092 case 16:
2093 return 256;
2094 default:
2095 MISSING_CASE(cpp);
2096 return cpp;
2097 }
2098 break;
2099 default:
2100 MISSING_CASE(fb_modifier);
2101 return cpp;
2102 }
2103}
2104
Ville Syrjälä832be822016-01-12 21:08:33 +02002105unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2106 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002107{
Ville Syrjälä832be822016-01-12 21:08:33 +02002108 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2109 return 1;
2110 else
2111 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002112 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002113}
2114
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002115/* Return the tile dimensions in pixel units */
2116static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2117 unsigned int *tile_width,
2118 unsigned int *tile_height,
2119 uint64_t fb_modifier,
2120 unsigned int cpp)
2121{
2122 unsigned int tile_width_bytes =
2123 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2124
2125 *tile_width = tile_width_bytes / cpp;
2126 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2127}
2128
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002129unsigned int
2130intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002131 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002132{
Ville Syrjälä832be822016-01-12 21:08:33 +02002133 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2134 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2135
2136 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002137}
2138
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002139unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2140{
2141 unsigned int size = 0;
2142 int i;
2143
2144 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2145 size += rot_info->plane[i].width * rot_info->plane[i].height;
2146
2147 return size;
2148}
2149
Daniel Vetter75c82a52015-10-14 16:51:04 +02002150static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002151intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2152 const struct drm_framebuffer *fb,
2153 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002154{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002155 if (intel_rotation_90_or_270(rotation)) {
2156 *view = i915_ggtt_view_rotated;
2157 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2158 } else {
2159 *view = i915_ggtt_view_normal;
2160 }
2161}
2162
2163static void
2164intel_fill_fb_info(struct drm_i915_private *dev_priv,
2165 struct drm_framebuffer *fb)
2166{
2167 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002168 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002169
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002170 tile_size = intel_tile_size(dev_priv);
2171
2172 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002173 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2174 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002175
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002176 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2177 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002178
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002179 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002180 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002181 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2182 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002183
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002184 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002185 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2186 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002187 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002188}
2189
Ville Syrjälä603525d2016-01-12 21:08:37 +02002190static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002191{
2192 if (INTEL_INFO(dev_priv)->gen >= 9)
2193 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002194 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002195 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002196 return 128 * 1024;
2197 else if (INTEL_INFO(dev_priv)->gen >= 4)
2198 return 4 * 1024;
2199 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002200 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002201}
2202
Ville Syrjälä603525d2016-01-12 21:08:37 +02002203static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2204 uint64_t fb_modifier)
2205{
2206 switch (fb_modifier) {
2207 case DRM_FORMAT_MOD_NONE:
2208 return intel_linear_alignment(dev_priv);
2209 case I915_FORMAT_MOD_X_TILED:
2210 if (INTEL_INFO(dev_priv)->gen >= 9)
2211 return 256 * 1024;
2212 return 0;
2213 case I915_FORMAT_MOD_Y_TILED:
2214 case I915_FORMAT_MOD_Yf_TILED:
2215 return 1 * 1024 * 1024;
2216 default:
2217 MISSING_CASE(fb_modifier);
2218 return 0;
2219 }
2220}
2221
Chris Wilson127bd2a2010-07-23 23:32:05 +01002222int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002223intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2224 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002225{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002226 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002227 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002228 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002229 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002230 u32 alignment;
2231 int ret;
2232
Matt Roperebcdd392014-07-09 16:22:11 -07002233 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2234
Ville Syrjälä603525d2016-01-12 21:08:37 +02002235 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002236
Ville Syrjälä3465c582016-02-15 22:54:43 +02002237 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002238
Chris Wilson693db182013-03-05 14:52:39 +00002239 /* Note that the w/a also requires 64 PTE of padding following the
2240 * bo. We currently fill all unused PTE with the shadow page and so
2241 * we should always have valid PTE following the scanout preventing
2242 * the VT-d warning.
2243 */
2244 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2245 alignment = 256 * 1024;
2246
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002247 /*
2248 * Global gtt pte registers are special registers which actually forward
2249 * writes to a chunk of system memory. Which means that there is no risk
2250 * that the register values disappear as soon as we call
2251 * intel_runtime_pm_put(), so it is correct to wrap only the
2252 * pin/unpin/fence and not more.
2253 */
2254 intel_runtime_pm_get(dev_priv);
2255
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002256 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2257 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002258 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002259 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002260
2261 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2262 * fence, whereas 965+ only requires a fence if using
2263 * framebuffer compression. For simplicity, we always install
2264 * a fence as the cost is not that onerous.
2265 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002266 if (view.type == I915_GGTT_VIEW_NORMAL) {
2267 ret = i915_gem_object_get_fence(obj);
2268 if (ret == -EDEADLK) {
2269 /*
2270 * -EDEADLK means there are no free fences
2271 * no pending flips.
2272 *
2273 * This is propagated to atomic, but it uses
2274 * -EDEADLK to force a locking recovery, so
2275 * change the returned error to -EBUSY.
2276 */
2277 ret = -EBUSY;
2278 goto err_unpin;
2279 } else if (ret)
2280 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002281
Vivek Kasireddy98072162015-10-29 18:54:38 -07002282 i915_gem_object_pin_fence(obj);
2283 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002284
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002285 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002286 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002287
2288err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002289 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002290err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002291 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002292 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002293}
2294
Ville Syrjälä3465c582016-02-15 22:54:43 +02002295static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002296{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002297 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002298 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002299
Matt Roperebcdd392014-07-09 16:22:11 -07002300 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2301
Ville Syrjälä3465c582016-02-15 22:54:43 +02002302 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002303
Vivek Kasireddy98072162015-10-29 18:54:38 -07002304 if (view.type == I915_GGTT_VIEW_NORMAL)
2305 i915_gem_object_unpin_fence(obj);
2306
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002307 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002308}
2309
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002310/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002311 * Adjust the tile offset by moving the difference into
2312 * the x/y offsets.
2313 *
2314 * Input tile dimensions and pitch must already be
2315 * rotated to match x and y, and in pixel units.
2316 */
2317static u32 intel_adjust_tile_offset(int *x, int *y,
2318 unsigned int tile_width,
2319 unsigned int tile_height,
2320 unsigned int tile_size,
2321 unsigned int pitch_tiles,
2322 u32 old_offset,
2323 u32 new_offset)
2324{
2325 unsigned int tiles;
2326
2327 WARN_ON(old_offset & (tile_size - 1));
2328 WARN_ON(new_offset & (tile_size - 1));
2329 WARN_ON(new_offset > old_offset);
2330
2331 tiles = (old_offset - new_offset) / tile_size;
2332
2333 *y += tiles / pitch_tiles * tile_height;
2334 *x += tiles % pitch_tiles * tile_width;
2335
2336 return new_offset;
2337}
2338
2339/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002340 * Computes the linear offset to the base tile and adjusts
2341 * x, y. bytes per pixel is assumed to be a power-of-two.
2342 *
2343 * In the 90/270 rotated case, x and y are assumed
2344 * to be already rotated to match the rotated GTT view, and
2345 * pitch is the tile_height aligned framebuffer height.
2346 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002347u32 intel_compute_tile_offset(int *x, int *y,
2348 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002349 unsigned int pitch,
2350 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002351{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002352 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2353 uint64_t fb_modifier = fb->modifier[plane];
2354 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002355 u32 offset, offset_aligned, alignment;
2356
2357 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2358 if (alignment)
2359 alignment--;
2360
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002361 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002362 unsigned int tile_size, tile_width, tile_height;
2363 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002364
Ville Syrjäläd8433102016-01-12 21:08:35 +02002365 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002366 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2367 fb_modifier, cpp);
2368
2369 if (intel_rotation_90_or_270(rotation)) {
2370 pitch_tiles = pitch / tile_height;
2371 swap(tile_width, tile_height);
2372 } else {
2373 pitch_tiles = pitch / (tile_width * cpp);
2374 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002375
Ville Syrjäläd8433102016-01-12 21:08:35 +02002376 tile_rows = *y / tile_height;
2377 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002378
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002379 tiles = *x / tile_width;
2380 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002381
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002382 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2383 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002384
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002385 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2386 tile_size, pitch_tiles,
2387 offset, offset_aligned);
2388 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002389 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002390 offset_aligned = offset & ~alignment;
2391
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002392 *y = (offset & alignment) / pitch;
2393 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002394 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002395
2396 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002397}
2398
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002399static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002400{
2401 switch (format) {
2402 case DISPPLANE_8BPP:
2403 return DRM_FORMAT_C8;
2404 case DISPPLANE_BGRX555:
2405 return DRM_FORMAT_XRGB1555;
2406 case DISPPLANE_BGRX565:
2407 return DRM_FORMAT_RGB565;
2408 default:
2409 case DISPPLANE_BGRX888:
2410 return DRM_FORMAT_XRGB8888;
2411 case DISPPLANE_RGBX888:
2412 return DRM_FORMAT_XBGR8888;
2413 case DISPPLANE_BGRX101010:
2414 return DRM_FORMAT_XRGB2101010;
2415 case DISPPLANE_RGBX101010:
2416 return DRM_FORMAT_XBGR2101010;
2417 }
2418}
2419
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002420static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2421{
2422 switch (format) {
2423 case PLANE_CTL_FORMAT_RGB_565:
2424 return DRM_FORMAT_RGB565;
2425 default:
2426 case PLANE_CTL_FORMAT_XRGB_8888:
2427 if (rgb_order) {
2428 if (alpha)
2429 return DRM_FORMAT_ABGR8888;
2430 else
2431 return DRM_FORMAT_XBGR8888;
2432 } else {
2433 if (alpha)
2434 return DRM_FORMAT_ARGB8888;
2435 else
2436 return DRM_FORMAT_XRGB8888;
2437 }
2438 case PLANE_CTL_FORMAT_XRGB_2101010:
2439 if (rgb_order)
2440 return DRM_FORMAT_XBGR2101010;
2441 else
2442 return DRM_FORMAT_XRGB2101010;
2443 }
2444}
2445
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002446static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002447intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2448 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002449{
2450 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002451 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002452 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002453 struct drm_i915_gem_object *obj = NULL;
2454 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002455 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002456 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2457 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2458 PAGE_SIZE);
2459
2460 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002461
Chris Wilsonff2652e2014-03-10 08:07:02 +00002462 if (plane_config->size == 0)
2463 return false;
2464
Paulo Zanoni3badb492015-09-23 12:52:23 -03002465 /* If the FB is too big, just don't use it since fbdev is not very
2466 * important and we should probably use that space with FBC or other
2467 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002468 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002469 return false;
2470
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002471 mutex_lock(&dev->struct_mutex);
2472
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002473 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2474 base_aligned,
2475 base_aligned,
2476 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002477 if (!obj) {
2478 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002479 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002480 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002481
Damien Lespiau49af4492015-01-20 12:51:44 +00002482 obj->tiling_mode = plane_config->tiling;
2483 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002484 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002485
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002486 mode_cmd.pixel_format = fb->pixel_format;
2487 mode_cmd.width = fb->width;
2488 mode_cmd.height = fb->height;
2489 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002490 mode_cmd.modifier[0] = fb->modifier[0];
2491 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002492
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002493 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002494 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002495 DRM_DEBUG_KMS("intel fb init failed\n");
2496 goto out_unref_obj;
2497 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002498
Jesse Barnes46f297f2014-03-07 08:57:48 -08002499 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002500
Daniel Vetterf6936e22015-03-26 12:17:05 +01002501 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002502 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002503
2504out_unref_obj:
2505 drm_gem_object_unreference(&obj->base);
2506 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002507 return false;
2508}
2509
Matt Roperafd65eb2015-02-03 13:10:04 -08002510/* Update plane->state->fb to match plane->fb after driver-internal updates */
2511static void
2512update_state_fb(struct drm_plane *plane)
2513{
2514 if (plane->fb == plane->state->fb)
2515 return;
2516
2517 if (plane->state->fb)
2518 drm_framebuffer_unreference(plane->state->fb);
2519 plane->state->fb = plane->fb;
2520 if (plane->state->fb)
2521 drm_framebuffer_reference(plane->state->fb);
2522}
2523
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002524static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002525intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2526 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002527{
2528 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002529 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002530 struct drm_crtc *c;
2531 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002532 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002533 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002534 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002535 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2536 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002537 struct intel_plane_state *intel_state =
2538 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002539 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002540
Damien Lespiau2d140302015-02-05 17:22:18 +00002541 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002542 return;
2543
Daniel Vetterf6936e22015-03-26 12:17:05 +01002544 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002545 fb = &plane_config->fb->base;
2546 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002547 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002548
Damien Lespiau2d140302015-02-05 17:22:18 +00002549 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002550
2551 /*
2552 * Failed to alloc the obj, check to see if we should share
2553 * an fb with another CRTC instead
2554 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002555 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002556 i = to_intel_crtc(c);
2557
2558 if (c == &intel_crtc->base)
2559 continue;
2560
Matt Roper2ff8fde2014-07-08 07:50:07 -07002561 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002562 continue;
2563
Daniel Vetter88595ac2015-03-26 12:42:24 +01002564 fb = c->primary->fb;
2565 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002566 continue;
2567
Daniel Vetter88595ac2015-03-26 12:42:24 +01002568 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002569 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002570 drm_framebuffer_reference(fb);
2571 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002572 }
2573 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002574
Matt Roper200757f2015-12-03 11:37:36 -08002575 /*
2576 * We've failed to reconstruct the BIOS FB. Current display state
2577 * indicates that the primary plane is visible, but has a NULL FB,
2578 * which will lead to problems later if we don't fix it up. The
2579 * simplest solution is to just disable the primary plane now and
2580 * pretend the BIOS never had it enabled.
2581 */
2582 to_intel_plane_state(plane_state)->visible = false;
2583 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002584 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002585 intel_plane->disable_plane(primary, &intel_crtc->base);
2586
Daniel Vetter88595ac2015-03-26 12:42:24 +01002587 return;
2588
2589valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002590 plane_state->src_x = 0;
2591 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002592 plane_state->src_w = fb->width << 16;
2593 plane_state->src_h = fb->height << 16;
2594
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002595 plane_state->crtc_x = 0;
2596 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002597 plane_state->crtc_w = fb->width;
2598 plane_state->crtc_h = fb->height;
2599
Matt Roper0a8d8a82015-12-03 11:37:38 -08002600 intel_state->src.x1 = plane_state->src_x;
2601 intel_state->src.y1 = plane_state->src_y;
2602 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2603 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2604 intel_state->dst.x1 = plane_state->crtc_x;
2605 intel_state->dst.y1 = plane_state->crtc_y;
2606 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2607 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2608
Daniel Vetter88595ac2015-03-26 12:42:24 +01002609 obj = intel_fb_obj(fb);
2610 if (obj->tiling_mode != I915_TILING_NONE)
2611 dev_priv->preserve_bios_swizzle = true;
2612
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002613 drm_framebuffer_reference(fb);
2614 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002615 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002616 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002617 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002618}
2619
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002620static void i9xx_update_primary_plane(struct drm_plane *primary,
2621 const struct intel_crtc_state *crtc_state,
2622 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002623{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002624 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002625 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002626 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2627 struct drm_framebuffer *fb = plane_state->base.fb;
2628 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002629 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002630 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002631 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002632 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002633 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002634 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002635 int x = plane_state->src.x1 >> 16;
2636 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002637
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002638 dspcntr = DISPPLANE_GAMMA_ENABLE;
2639
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002640 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002641
2642 if (INTEL_INFO(dev)->gen < 4) {
2643 if (intel_crtc->pipe == PIPE_B)
2644 dspcntr |= DISPPLANE_SEL_PIPE_B;
2645
2646 /* pipesrc and dspsize control the size that is scaled from,
2647 * which should always be the user's requested size.
2648 */
2649 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002650 ((crtc_state->pipe_src_h - 1) << 16) |
2651 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002652 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002653 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2654 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002655 ((crtc_state->pipe_src_h - 1) << 16) |
2656 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002657 I915_WRITE(PRIMPOS(plane), 0);
2658 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002659 }
2660
Ville Syrjälä57779d02012-10-31 17:50:14 +02002661 switch (fb->pixel_format) {
2662 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002663 dspcntr |= DISPPLANE_8BPP;
2664 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002665 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002666 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002667 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002668 case DRM_FORMAT_RGB565:
2669 dspcntr |= DISPPLANE_BGRX565;
2670 break;
2671 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002672 dspcntr |= DISPPLANE_BGRX888;
2673 break;
2674 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002675 dspcntr |= DISPPLANE_RGBX888;
2676 break;
2677 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002678 dspcntr |= DISPPLANE_BGRX101010;
2679 break;
2680 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002681 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002682 break;
2683 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002684 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002685 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002686
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002687 if (INTEL_INFO(dev)->gen >= 4 &&
2688 obj->tiling_mode != I915_TILING_NONE)
2689 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002690
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002691 if (IS_G4X(dev))
2692 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2693
Ville Syrjäläac484962016-01-20 21:05:26 +02002694 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002695
Daniel Vetterc2c75132012-07-05 12:17:30 +02002696 if (INTEL_INFO(dev)->gen >= 4) {
2697 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002698 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002699 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002700 linear_offset -= intel_crtc->dspaddr_offset;
2701 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002702 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002703 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002704
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002705 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302706 dspcntr |= DISPPLANE_ROTATE_180;
2707
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002708 x += (crtc_state->pipe_src_w - 1);
2709 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302710
2711 /* Finding the last pixel of the last line of the display
2712 data and adding to linear_offset*/
2713 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002714 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002715 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302716 }
2717
Paulo Zanoni2db33662015-09-14 15:20:03 -03002718 intel_crtc->adjusted_x = x;
2719 intel_crtc->adjusted_y = y;
2720
Sonika Jindal48404c12014-08-22 14:06:04 +05302721 I915_WRITE(reg, dspcntr);
2722
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002723 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002724 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002725 I915_WRITE(DSPSURF(plane),
2726 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002727 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002728 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002729 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002730 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002731 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002732}
2733
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002734static void i9xx_disable_primary_plane(struct drm_plane *primary,
2735 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002736{
2737 struct drm_device *dev = crtc->dev;
2738 struct drm_i915_private *dev_priv = dev->dev_private;
2739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002740 int plane = intel_crtc->plane;
2741
2742 I915_WRITE(DSPCNTR(plane), 0);
2743 if (INTEL_INFO(dev_priv)->gen >= 4)
2744 I915_WRITE(DSPSURF(plane), 0);
2745 else
2746 I915_WRITE(DSPADDR(plane), 0);
2747 POSTING_READ(DSPCNTR(plane));
2748}
2749
2750static void ironlake_update_primary_plane(struct drm_plane *primary,
2751 const struct intel_crtc_state *crtc_state,
2752 const struct intel_plane_state *plane_state)
2753{
2754 struct drm_device *dev = primary->dev;
2755 struct drm_i915_private *dev_priv = dev->dev_private;
2756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2757 struct drm_framebuffer *fb = plane_state->base.fb;
2758 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002759 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002760 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002761 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002762 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002763 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002764 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002765 int x = plane_state->src.x1 >> 16;
2766 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002767
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002768 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002769 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002770
2771 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2772 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2773
Ville Syrjälä57779d02012-10-31 17:50:14 +02002774 switch (fb->pixel_format) {
2775 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002776 dspcntr |= DISPPLANE_8BPP;
2777 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002778 case DRM_FORMAT_RGB565:
2779 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002780 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002781 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002782 dspcntr |= DISPPLANE_BGRX888;
2783 break;
2784 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002785 dspcntr |= DISPPLANE_RGBX888;
2786 break;
2787 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002788 dspcntr |= DISPPLANE_BGRX101010;
2789 break;
2790 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002791 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002792 break;
2793 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002794 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002795 }
2796
2797 if (obj->tiling_mode != I915_TILING_NONE)
2798 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002799
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002800 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002801 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002802
Ville Syrjäläac484962016-01-20 21:05:26 +02002803 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002804 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002805 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002806 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002807 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002808 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302809 dspcntr |= DISPPLANE_ROTATE_180;
2810
2811 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002812 x += (crtc_state->pipe_src_w - 1);
2813 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302814
2815 /* Finding the last pixel of the last line of the display
2816 data and adding to linear_offset*/
2817 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002818 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002819 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302820 }
2821 }
2822
Paulo Zanoni2db33662015-09-14 15:20:03 -03002823 intel_crtc->adjusted_x = x;
2824 intel_crtc->adjusted_y = y;
2825
Sonika Jindal48404c12014-08-22 14:06:04 +05302826 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002827
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002828 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002829 I915_WRITE(DSPSURF(plane),
2830 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002831 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002832 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2833 } else {
2834 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2835 I915_WRITE(DSPLINOFF(plane), linear_offset);
2836 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002837 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002838}
2839
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002840u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2841 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002842{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002843 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2844 return 64;
2845 } else {
2846 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002847
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002848 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002849 }
2850}
2851
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002852u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2853 struct drm_i915_gem_object *obj,
2854 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002855{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002856 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002857 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002858 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002859
Ville Syrjäläe7941292016-01-19 18:23:17 +02002860 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002861 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002862
Daniel Vetterce7f1722015-10-14 16:51:06 +02002863 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002864 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002865 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002866 return -1;
2867
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002868 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002869
2870 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002871 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002872 PAGE_SIZE;
2873 }
2874
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002875 WARN_ON(upper_32_bits(offset));
2876
2877 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002878}
2879
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002880static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2881{
2882 struct drm_device *dev = intel_crtc->base.dev;
2883 struct drm_i915_private *dev_priv = dev->dev_private;
2884
2885 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2886 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2887 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002888}
2889
Chandra Kondurua1b22782015-04-07 15:28:45 -07002890/*
2891 * This function detaches (aka. unbinds) unused scalers in hardware
2892 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002893static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002894{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002895 struct intel_crtc_scaler_state *scaler_state;
2896 int i;
2897
Chandra Kondurua1b22782015-04-07 15:28:45 -07002898 scaler_state = &intel_crtc->config->scaler_state;
2899
2900 /* loop through and disable scalers that aren't in use */
2901 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002902 if (!scaler_state->scalers[i].in_use)
2903 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002904 }
2905}
2906
Chandra Konduru6156a452015-04-27 13:48:39 -07002907u32 skl_plane_ctl_format(uint32_t pixel_format)
2908{
Chandra Konduru6156a452015-04-27 13:48:39 -07002909 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002910 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002911 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002912 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002913 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002914 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002915 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002916 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002917 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002918 /*
2919 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2920 * to be already pre-multiplied. We need to add a knob (or a different
2921 * DRM_FORMAT) for user-space to configure that.
2922 */
2923 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002924 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002925 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002926 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002927 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002928 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002929 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002930 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002931 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002932 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002933 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002934 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002935 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002936 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002937 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002938 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002939 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002940 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002941 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002942 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002943 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002944
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002945 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002946}
2947
2948u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2949{
Chandra Konduru6156a452015-04-27 13:48:39 -07002950 switch (fb_modifier) {
2951 case DRM_FORMAT_MOD_NONE:
2952 break;
2953 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002954 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002955 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002956 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002957 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002958 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002959 default:
2960 MISSING_CASE(fb_modifier);
2961 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002962
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002963 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002964}
2965
2966u32 skl_plane_ctl_rotation(unsigned int rotation)
2967{
Chandra Konduru6156a452015-04-27 13:48:39 -07002968 switch (rotation) {
2969 case BIT(DRM_ROTATE_0):
2970 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302971 /*
2972 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2973 * while i915 HW rotation is clockwise, thats why this swapping.
2974 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002975 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302976 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002977 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002978 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002979 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302980 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002981 default:
2982 MISSING_CASE(rotation);
2983 }
2984
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002985 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002986}
2987
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002988static void skylake_update_primary_plane(struct drm_plane *plane,
2989 const struct intel_crtc_state *crtc_state,
2990 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01002991{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002992 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002993 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002994 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2995 struct drm_framebuffer *fb = plane_state->base.fb;
2996 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002997 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302998 u32 plane_ctl, stride_div, stride;
2999 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003000 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303001 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003002 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003003 int scaler_id = plane_state->scaler_id;
3004 int src_x = plane_state->src.x1 >> 16;
3005 int src_y = plane_state->src.y1 >> 16;
3006 int src_w = drm_rect_width(&plane_state->src) >> 16;
3007 int src_h = drm_rect_height(&plane_state->src) >> 16;
3008 int dst_x = plane_state->dst.x1;
3009 int dst_y = plane_state->dst.y1;
3010 int dst_w = drm_rect_width(&plane_state->dst);
3011 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003012
3013 plane_ctl = PLANE_CTL_ENABLE |
3014 PLANE_CTL_PIPE_GAMMA_ENABLE |
3015 PLANE_CTL_PIPE_CSC_ENABLE;
3016
Chandra Konduru6156a452015-04-27 13:48:39 -07003017 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3018 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003019 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003020 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003021
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003022 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003023 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003024 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303025
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003026 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003027
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303028 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003029 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3030
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303031 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003032 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303033 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003034 x_offset = stride * tile_height - src_y - src_h;
3035 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003036 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303037 } else {
3038 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003039 x_offset = src_x;
3040 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003041 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303042 }
3043 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003044
Paulo Zanoni2db33662015-09-14 15:20:03 -03003045 intel_crtc->adjusted_x = x_offset;
3046 intel_crtc->adjusted_y = y_offset;
3047
Damien Lespiau70d21f02013-07-03 21:06:04 +01003048 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303049 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3050 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3051 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003052
3053 if (scaler_id >= 0) {
3054 uint32_t ps_ctrl = 0;
3055
3056 WARN_ON(!dst_w || !dst_h);
3057 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3058 crtc_state->scaler_state.scalers[scaler_id].mode;
3059 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3060 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3061 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3062 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3063 I915_WRITE(PLANE_POS(pipe, 0), 0);
3064 } else {
3065 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3066 }
3067
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003068 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003069
3070 POSTING_READ(PLANE_SURF(pipe, 0));
3071}
3072
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003073static void skylake_disable_primary_plane(struct drm_plane *primary,
3074 struct drm_crtc *crtc)
3075{
3076 struct drm_device *dev = crtc->dev;
3077 struct drm_i915_private *dev_priv = dev->dev_private;
3078 int pipe = to_intel_crtc(crtc)->pipe;
3079
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003080 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3081 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3082 POSTING_READ(PLANE_SURF(pipe, 0));
3083}
3084
Jesse Barnes17638cd2011-06-24 12:19:23 -07003085/* Assume fb object is pinned & idle & fenced and just update base pointers */
3086static int
3087intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3088 int x, int y, enum mode_set_atomic state)
3089{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003090 /* Support for kgdboc is disabled, this needs a major rework. */
3091 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003092
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003093 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003094}
3095
Ville Syrjälä75147472014-11-24 18:28:11 +02003096static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003097{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003098 struct drm_crtc *crtc;
3099
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003100 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003101 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3102 enum plane plane = intel_crtc->plane;
3103
3104 intel_prepare_page_flip(dev, plane);
3105 intel_finish_page_flip_plane(dev, plane);
3106 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003107}
3108
3109static void intel_update_primary_planes(struct drm_device *dev)
3110{
Ville Syrjälä75147472014-11-24 18:28:11 +02003111 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003112
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003113 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003114 struct intel_plane *plane = to_intel_plane(crtc->primary);
3115 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003116
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003117 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003118 plane_state = to_intel_plane_state(plane->base.state);
3119
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003120 if (plane_state->visible)
3121 plane->update_plane(&plane->base,
3122 to_intel_crtc_state(crtc->state),
3123 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003124
3125 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003126 }
3127}
3128
Ville Syrjälä75147472014-11-24 18:28:11 +02003129void intel_prepare_reset(struct drm_device *dev)
3130{
3131 /* no reset support for gen2 */
3132 if (IS_GEN2(dev))
3133 return;
3134
3135 /* reset doesn't touch the display */
3136 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3137 return;
3138
3139 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003140 /*
3141 * Disabling the crtcs gracefully seems nicer. Also the
3142 * g33 docs say we should at least disable all the planes.
3143 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003144 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003145}
3146
3147void intel_finish_reset(struct drm_device *dev)
3148{
3149 struct drm_i915_private *dev_priv = to_i915(dev);
3150
3151 /*
3152 * Flips in the rings will be nuked by the reset,
3153 * so complete all pending flips so that user space
3154 * will get its events and not get stuck.
3155 */
3156 intel_complete_page_flips(dev);
3157
3158 /* no reset support for gen2 */
3159 if (IS_GEN2(dev))
3160 return;
3161
3162 /* reset doesn't touch the display */
3163 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3164 /*
3165 * Flips in the rings have been nuked by the reset,
3166 * so update the base address of all primary
3167 * planes to the the last fb to make sure we're
3168 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003169 *
3170 * FIXME: Atomic will make this obsolete since we won't schedule
3171 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003172 */
3173 intel_update_primary_planes(dev);
3174 return;
3175 }
3176
3177 /*
3178 * The display has been reset as well,
3179 * so need a full re-initialization.
3180 */
3181 intel_runtime_pm_disable_interrupts(dev_priv);
3182 intel_runtime_pm_enable_interrupts(dev_priv);
3183
3184 intel_modeset_init_hw(dev);
3185
3186 spin_lock_irq(&dev_priv->irq_lock);
3187 if (dev_priv->display.hpd_irq_setup)
3188 dev_priv->display.hpd_irq_setup(dev);
3189 spin_unlock_irq(&dev_priv->irq_lock);
3190
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003191 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003192
3193 intel_hpd_init(dev_priv);
3194
3195 drm_modeset_unlock_all(dev);
3196}
3197
Chris Wilson7d5e3792014-03-04 13:15:08 +00003198static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3199{
3200 struct drm_device *dev = crtc->dev;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003201 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonc19ae982016-04-13 17:35:03 +01003202 unsigned reset_counter;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003203 bool pending;
3204
Chris Wilson7f1847e2016-04-13 17:35:04 +01003205 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3206 if (intel_crtc->reset_counter != reset_counter)
Chris Wilson7d5e3792014-03-04 13:15:08 +00003207 return false;
3208
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003209 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003210 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003211 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003212
3213 return pending;
3214}
3215
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003216static void intel_update_pipe_config(struct intel_crtc *crtc,
3217 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003218{
3219 struct drm_device *dev = crtc->base.dev;
3220 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003221 struct intel_crtc_state *pipe_config =
3222 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003223
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003224 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3225 crtc->base.mode = crtc->base.state->mode;
3226
3227 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3228 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3229 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003230
3231 /*
3232 * Update pipe size and adjust fitter if needed: the reason for this is
3233 * that in compute_mode_changes we check the native mode (not the pfit
3234 * mode) to see if we can flip rather than do a full mode set. In the
3235 * fastboot case, we'll flip, but if we don't update the pipesrc and
3236 * pfit state, we'll end up with a big fb scanned out into the wrong
3237 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003238 */
3239
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003240 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003241 ((pipe_config->pipe_src_w - 1) << 16) |
3242 (pipe_config->pipe_src_h - 1));
3243
3244 /* on skylake this is done by detaching scalers */
3245 if (INTEL_INFO(dev)->gen >= 9) {
3246 skl_detach_scalers(crtc);
3247
3248 if (pipe_config->pch_pfit.enabled)
3249 skylake_pfit_enable(crtc);
3250 } else if (HAS_PCH_SPLIT(dev)) {
3251 if (pipe_config->pch_pfit.enabled)
3252 ironlake_pfit_enable(crtc);
3253 else if (old_crtc_state->pch_pfit.enabled)
3254 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003255 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003256}
3257
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003258static void intel_fdi_normal_train(struct drm_crtc *crtc)
3259{
3260 struct drm_device *dev = crtc->dev;
3261 struct drm_i915_private *dev_priv = dev->dev_private;
3262 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3263 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003264 i915_reg_t reg;
3265 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003266
3267 /* enable normal train */
3268 reg = FDI_TX_CTL(pipe);
3269 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003270 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003271 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3272 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003273 } else {
3274 temp &= ~FDI_LINK_TRAIN_NONE;
3275 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003276 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003277 I915_WRITE(reg, temp);
3278
3279 reg = FDI_RX_CTL(pipe);
3280 temp = I915_READ(reg);
3281 if (HAS_PCH_CPT(dev)) {
3282 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3283 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3284 } else {
3285 temp &= ~FDI_LINK_TRAIN_NONE;
3286 temp |= FDI_LINK_TRAIN_NONE;
3287 }
3288 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3289
3290 /* wait one idle pattern time */
3291 POSTING_READ(reg);
3292 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003293
3294 /* IVB wants error correction enabled */
3295 if (IS_IVYBRIDGE(dev))
3296 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3297 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003298}
3299
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003300/* The FDI link training functions for ILK/Ibexpeak. */
3301static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3302{
3303 struct drm_device *dev = crtc->dev;
3304 struct drm_i915_private *dev_priv = dev->dev_private;
3305 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3306 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003307 i915_reg_t reg;
3308 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003309
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003310 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003311 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003312
Adam Jacksone1a44742010-06-25 15:32:14 -04003313 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3314 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003315 reg = FDI_RX_IMR(pipe);
3316 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003317 temp &= ~FDI_RX_SYMBOL_LOCK;
3318 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003319 I915_WRITE(reg, temp);
3320 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003321 udelay(150);
3322
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003323 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003324 reg = FDI_TX_CTL(pipe);
3325 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003326 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003327 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003328 temp &= ~FDI_LINK_TRAIN_NONE;
3329 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003330 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003331
Chris Wilson5eddb702010-09-11 13:48:45 +01003332 reg = FDI_RX_CTL(pipe);
3333 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003334 temp &= ~FDI_LINK_TRAIN_NONE;
3335 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003336 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3337
3338 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003339 udelay(150);
3340
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003341 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003342 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3343 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3344 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003345
Chris Wilson5eddb702010-09-11 13:48:45 +01003346 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003347 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003348 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003349 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3350
3351 if ((temp & FDI_RX_BIT_LOCK)) {
3352 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003353 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003354 break;
3355 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003356 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003357 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003358 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003359
3360 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003361 reg = FDI_TX_CTL(pipe);
3362 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003363 temp &= ~FDI_LINK_TRAIN_NONE;
3364 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003365 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003366
Chris Wilson5eddb702010-09-11 13:48:45 +01003367 reg = FDI_RX_CTL(pipe);
3368 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003369 temp &= ~FDI_LINK_TRAIN_NONE;
3370 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003371 I915_WRITE(reg, temp);
3372
3373 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003374 udelay(150);
3375
Chris Wilson5eddb702010-09-11 13:48:45 +01003376 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003377 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003378 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003379 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3380
3381 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003382 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003383 DRM_DEBUG_KMS("FDI train 2 done.\n");
3384 break;
3385 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003386 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003387 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003388 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003389
3390 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003391
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003392}
3393
Akshay Joshi0206e352011-08-16 15:34:10 -04003394static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003395 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3396 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3397 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3398 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3399};
3400
3401/* The FDI link training functions for SNB/Cougarpoint. */
3402static void gen6_fdi_link_train(struct drm_crtc *crtc)
3403{
3404 struct drm_device *dev = crtc->dev;
3405 struct drm_i915_private *dev_priv = dev->dev_private;
3406 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3407 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003408 i915_reg_t reg;
3409 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003410
Adam Jacksone1a44742010-06-25 15:32:14 -04003411 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3412 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003413 reg = FDI_RX_IMR(pipe);
3414 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003415 temp &= ~FDI_RX_SYMBOL_LOCK;
3416 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003417 I915_WRITE(reg, temp);
3418
3419 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003420 udelay(150);
3421
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003422 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003423 reg = FDI_TX_CTL(pipe);
3424 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003425 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003426 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003427 temp &= ~FDI_LINK_TRAIN_NONE;
3428 temp |= FDI_LINK_TRAIN_PATTERN_1;
3429 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3430 /* SNB-B */
3431 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003432 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003433
Daniel Vetterd74cf322012-10-26 10:58:13 +02003434 I915_WRITE(FDI_RX_MISC(pipe),
3435 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3436
Chris Wilson5eddb702010-09-11 13:48:45 +01003437 reg = FDI_RX_CTL(pipe);
3438 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003439 if (HAS_PCH_CPT(dev)) {
3440 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3441 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3442 } else {
3443 temp &= ~FDI_LINK_TRAIN_NONE;
3444 temp |= FDI_LINK_TRAIN_PATTERN_1;
3445 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003446 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3447
3448 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003449 udelay(150);
3450
Akshay Joshi0206e352011-08-16 15:34:10 -04003451 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003452 reg = FDI_TX_CTL(pipe);
3453 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003454 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3455 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003456 I915_WRITE(reg, temp);
3457
3458 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003459 udelay(500);
3460
Sean Paulfa37d392012-03-02 12:53:39 -05003461 for (retry = 0; retry < 5; retry++) {
3462 reg = FDI_RX_IIR(pipe);
3463 temp = I915_READ(reg);
3464 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3465 if (temp & FDI_RX_BIT_LOCK) {
3466 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3467 DRM_DEBUG_KMS("FDI train 1 done.\n");
3468 break;
3469 }
3470 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003471 }
Sean Paulfa37d392012-03-02 12:53:39 -05003472 if (retry < 5)
3473 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003474 }
3475 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003476 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003477
3478 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003479 reg = FDI_TX_CTL(pipe);
3480 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003481 temp &= ~FDI_LINK_TRAIN_NONE;
3482 temp |= FDI_LINK_TRAIN_PATTERN_2;
3483 if (IS_GEN6(dev)) {
3484 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3485 /* SNB-B */
3486 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3487 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003488 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003489
Chris Wilson5eddb702010-09-11 13:48:45 +01003490 reg = FDI_RX_CTL(pipe);
3491 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003492 if (HAS_PCH_CPT(dev)) {
3493 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3494 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3495 } else {
3496 temp &= ~FDI_LINK_TRAIN_NONE;
3497 temp |= FDI_LINK_TRAIN_PATTERN_2;
3498 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003499 I915_WRITE(reg, temp);
3500
3501 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003502 udelay(150);
3503
Akshay Joshi0206e352011-08-16 15:34:10 -04003504 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003505 reg = FDI_TX_CTL(pipe);
3506 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003507 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3508 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003509 I915_WRITE(reg, temp);
3510
3511 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003512 udelay(500);
3513
Sean Paulfa37d392012-03-02 12:53:39 -05003514 for (retry = 0; retry < 5; retry++) {
3515 reg = FDI_RX_IIR(pipe);
3516 temp = I915_READ(reg);
3517 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3518 if (temp & FDI_RX_SYMBOL_LOCK) {
3519 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3520 DRM_DEBUG_KMS("FDI train 2 done.\n");
3521 break;
3522 }
3523 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003524 }
Sean Paulfa37d392012-03-02 12:53:39 -05003525 if (retry < 5)
3526 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003527 }
3528 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003529 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003530
3531 DRM_DEBUG_KMS("FDI train done.\n");
3532}
3533
Jesse Barnes357555c2011-04-28 15:09:55 -07003534/* Manual link training for Ivy Bridge A0 parts */
3535static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3536{
3537 struct drm_device *dev = crtc->dev;
3538 struct drm_i915_private *dev_priv = dev->dev_private;
3539 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3540 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003541 i915_reg_t reg;
3542 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003543
3544 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3545 for train result */
3546 reg = FDI_RX_IMR(pipe);
3547 temp = I915_READ(reg);
3548 temp &= ~FDI_RX_SYMBOL_LOCK;
3549 temp &= ~FDI_RX_BIT_LOCK;
3550 I915_WRITE(reg, temp);
3551
3552 POSTING_READ(reg);
3553 udelay(150);
3554
Daniel Vetter01a415f2012-10-27 15:58:40 +02003555 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3556 I915_READ(FDI_RX_IIR(pipe)));
3557
Jesse Barnes139ccd32013-08-19 11:04:55 -07003558 /* Try each vswing and preemphasis setting twice before moving on */
3559 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3560 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003561 reg = FDI_TX_CTL(pipe);
3562 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003563 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3564 temp &= ~FDI_TX_ENABLE;
3565 I915_WRITE(reg, temp);
3566
3567 reg = FDI_RX_CTL(pipe);
3568 temp = I915_READ(reg);
3569 temp &= ~FDI_LINK_TRAIN_AUTO;
3570 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3571 temp &= ~FDI_RX_ENABLE;
3572 I915_WRITE(reg, temp);
3573
3574 /* enable CPU FDI TX and PCH FDI RX */
3575 reg = FDI_TX_CTL(pipe);
3576 temp = I915_READ(reg);
3577 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003578 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003579 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003580 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003581 temp |= snb_b_fdi_train_param[j/2];
3582 temp |= FDI_COMPOSITE_SYNC;
3583 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3584
3585 I915_WRITE(FDI_RX_MISC(pipe),
3586 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3587
3588 reg = FDI_RX_CTL(pipe);
3589 temp = I915_READ(reg);
3590 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3591 temp |= FDI_COMPOSITE_SYNC;
3592 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3593
3594 POSTING_READ(reg);
3595 udelay(1); /* should be 0.5us */
3596
3597 for (i = 0; i < 4; i++) {
3598 reg = FDI_RX_IIR(pipe);
3599 temp = I915_READ(reg);
3600 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3601
3602 if (temp & FDI_RX_BIT_LOCK ||
3603 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3604 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3605 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3606 i);
3607 break;
3608 }
3609 udelay(1); /* should be 0.5us */
3610 }
3611 if (i == 4) {
3612 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3613 continue;
3614 }
3615
3616 /* Train 2 */
3617 reg = FDI_TX_CTL(pipe);
3618 temp = I915_READ(reg);
3619 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3620 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3621 I915_WRITE(reg, temp);
3622
3623 reg = FDI_RX_CTL(pipe);
3624 temp = I915_READ(reg);
3625 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3626 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003627 I915_WRITE(reg, temp);
3628
3629 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003630 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003631
Jesse Barnes139ccd32013-08-19 11:04:55 -07003632 for (i = 0; i < 4; i++) {
3633 reg = FDI_RX_IIR(pipe);
3634 temp = I915_READ(reg);
3635 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003636
Jesse Barnes139ccd32013-08-19 11:04:55 -07003637 if (temp & FDI_RX_SYMBOL_LOCK ||
3638 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3639 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3640 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3641 i);
3642 goto train_done;
3643 }
3644 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003645 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003646 if (i == 4)
3647 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003648 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003649
Jesse Barnes139ccd32013-08-19 11:04:55 -07003650train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003651 DRM_DEBUG_KMS("FDI train done.\n");
3652}
3653
Daniel Vetter88cefb62012-08-12 19:27:14 +02003654static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003655{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003656 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003657 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003658 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003659 i915_reg_t reg;
3660 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003661
Jesse Barnes0e23b992010-09-10 11:10:00 -07003662 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003663 reg = FDI_RX_CTL(pipe);
3664 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003665 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003666 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003667 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003668 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3669
3670 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003671 udelay(200);
3672
3673 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003674 temp = I915_READ(reg);
3675 I915_WRITE(reg, temp | FDI_PCDCLK);
3676
3677 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003678 udelay(200);
3679
Paulo Zanoni20749732012-11-23 15:30:38 -02003680 /* Enable CPU FDI TX PLL, always on for Ironlake */
3681 reg = FDI_TX_CTL(pipe);
3682 temp = I915_READ(reg);
3683 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3684 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003685
Paulo Zanoni20749732012-11-23 15:30:38 -02003686 POSTING_READ(reg);
3687 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003688 }
3689}
3690
Daniel Vetter88cefb62012-08-12 19:27:14 +02003691static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3692{
3693 struct drm_device *dev = intel_crtc->base.dev;
3694 struct drm_i915_private *dev_priv = dev->dev_private;
3695 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003696 i915_reg_t reg;
3697 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003698
3699 /* Switch from PCDclk to Rawclk */
3700 reg = FDI_RX_CTL(pipe);
3701 temp = I915_READ(reg);
3702 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3703
3704 /* Disable CPU FDI TX PLL */
3705 reg = FDI_TX_CTL(pipe);
3706 temp = I915_READ(reg);
3707 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3708
3709 POSTING_READ(reg);
3710 udelay(100);
3711
3712 reg = FDI_RX_CTL(pipe);
3713 temp = I915_READ(reg);
3714 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3715
3716 /* Wait for the clocks to turn off. */
3717 POSTING_READ(reg);
3718 udelay(100);
3719}
3720
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003721static void ironlake_fdi_disable(struct drm_crtc *crtc)
3722{
3723 struct drm_device *dev = crtc->dev;
3724 struct drm_i915_private *dev_priv = dev->dev_private;
3725 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3726 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003727 i915_reg_t reg;
3728 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003729
3730 /* disable CPU FDI tx and PCH FDI rx */
3731 reg = FDI_TX_CTL(pipe);
3732 temp = I915_READ(reg);
3733 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3734 POSTING_READ(reg);
3735
3736 reg = FDI_RX_CTL(pipe);
3737 temp = I915_READ(reg);
3738 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003739 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003740 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3741
3742 POSTING_READ(reg);
3743 udelay(100);
3744
3745 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003746 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003747 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003748
3749 /* still set train pattern 1 */
3750 reg = FDI_TX_CTL(pipe);
3751 temp = I915_READ(reg);
3752 temp &= ~FDI_LINK_TRAIN_NONE;
3753 temp |= FDI_LINK_TRAIN_PATTERN_1;
3754 I915_WRITE(reg, temp);
3755
3756 reg = FDI_RX_CTL(pipe);
3757 temp = I915_READ(reg);
3758 if (HAS_PCH_CPT(dev)) {
3759 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3760 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3761 } else {
3762 temp &= ~FDI_LINK_TRAIN_NONE;
3763 temp |= FDI_LINK_TRAIN_PATTERN_1;
3764 }
3765 /* BPC in FDI rx is consistent with that in PIPECONF */
3766 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003767 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003768 I915_WRITE(reg, temp);
3769
3770 POSTING_READ(reg);
3771 udelay(100);
3772}
3773
Chris Wilson5dce5b932014-01-20 10:17:36 +00003774bool intel_has_pending_fb_unpin(struct drm_device *dev)
3775{
3776 struct intel_crtc *crtc;
3777
3778 /* Note that we don't need to be called with mode_config.lock here
3779 * as our list of CRTC objects is static for the lifetime of the
3780 * device and so cannot disappear as we iterate. Similarly, we can
3781 * happily treat the predicates as racy, atomic checks as userspace
3782 * cannot claim and pin a new fb without at least acquring the
3783 * struct_mutex and so serialising with us.
3784 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003785 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003786 if (atomic_read(&crtc->unpin_work_count) == 0)
3787 continue;
3788
3789 if (crtc->unpin_work)
3790 intel_wait_for_vblank(dev, crtc->pipe);
3791
3792 return true;
3793 }
3794
3795 return false;
3796}
3797
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003798static void page_flip_completed(struct intel_crtc *intel_crtc)
3799{
3800 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3801 struct intel_unpin_work *work = intel_crtc->unpin_work;
3802
3803 /* ensure that the unpin work is consistent wrt ->pending. */
3804 smp_rmb();
3805 intel_crtc->unpin_work = NULL;
3806
3807 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07003808 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003809
3810 drm_crtc_vblank_put(&intel_crtc->base);
3811
3812 wake_up_all(&dev_priv->pending_flip_queue);
3813 queue_work(dev_priv->wq, &work->work);
3814
3815 trace_i915_flip_complete(intel_crtc->plane,
3816 work->pending_flip_obj);
3817}
3818
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003819static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003820{
Chris Wilson0f911282012-04-17 10:05:38 +01003821 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003822 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003823 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003824
Daniel Vetter2c10d572012-12-20 21:24:07 +01003825 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003826
3827 ret = wait_event_interruptible_timeout(
3828 dev_priv->pending_flip_queue,
3829 !intel_crtc_has_pending_flip(crtc),
3830 60*HZ);
3831
3832 if (ret < 0)
3833 return ret;
3834
3835 if (ret == 0) {
Chris Wilson9c787942014-09-05 07:13:25 +01003836 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003837
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003838 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003839 if (intel_crtc->unpin_work) {
3840 WARN_ONCE(1, "Removing stuck page flip\n");
3841 page_flip_completed(intel_crtc);
3842 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003843 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003844 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003845
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003846 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003847}
3848
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003849static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3850{
3851 u32 temp;
3852
3853 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3854
3855 mutex_lock(&dev_priv->sb_lock);
3856
3857 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3858 temp |= SBI_SSCCTL_DISABLE;
3859 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3860
3861 mutex_unlock(&dev_priv->sb_lock);
3862}
3863
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003864/* Program iCLKIP clock to the desired frequency */
3865static void lpt_program_iclkip(struct drm_crtc *crtc)
3866{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003867 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003868 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003869 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3870 u32 temp;
3871
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003872 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003873
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003874 /* The iCLK virtual clock root frequency is in MHz,
3875 * but the adjusted_mode->crtc_clock in in KHz. To get the
3876 * divisors, it is necessary to divide one by another, so we
3877 * convert the virtual clock precision to KHz here for higher
3878 * precision.
3879 */
3880 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003881 u32 iclk_virtual_root_freq = 172800 * 1000;
3882 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003883 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003884
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003885 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3886 clock << auxdiv);
3887 divsel = (desired_divisor / iclk_pi_range) - 2;
3888 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003889
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003890 /*
3891 * Near 20MHz is a corner case which is
3892 * out of range for the 7-bit divisor
3893 */
3894 if (divsel <= 0x7f)
3895 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003896 }
3897
3898 /* This should not happen with any sane values */
3899 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3900 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3901 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3902 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3903
3904 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 +03003905 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003906 auxdiv,
3907 divsel,
3908 phasedir,
3909 phaseinc);
3910
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003911 mutex_lock(&dev_priv->sb_lock);
3912
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003913 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003914 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003915 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3916 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3917 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3918 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3919 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3920 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003921 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003922
3923 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003924 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003925 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3926 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003927 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003928
3929 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003930 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003931 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003932 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003933
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003934 mutex_unlock(&dev_priv->sb_lock);
3935
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003936 /* Wait for initialization time */
3937 udelay(24);
3938
3939 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3940}
3941
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003942int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3943{
3944 u32 divsel, phaseinc, auxdiv;
3945 u32 iclk_virtual_root_freq = 172800 * 1000;
3946 u32 iclk_pi_range = 64;
3947 u32 desired_divisor;
3948 u32 temp;
3949
3950 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3951 return 0;
3952
3953 mutex_lock(&dev_priv->sb_lock);
3954
3955 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3956 if (temp & SBI_SSCCTL_DISABLE) {
3957 mutex_unlock(&dev_priv->sb_lock);
3958 return 0;
3959 }
3960
3961 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3962 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3963 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3964 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3965 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3966
3967 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3968 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3969 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3970
3971 mutex_unlock(&dev_priv->sb_lock);
3972
3973 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3974
3975 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3976 desired_divisor << auxdiv);
3977}
3978
Daniel Vetter275f01b22013-05-03 11:49:47 +02003979static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3980 enum pipe pch_transcoder)
3981{
3982 struct drm_device *dev = crtc->base.dev;
3983 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003984 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003985
3986 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3987 I915_READ(HTOTAL(cpu_transcoder)));
3988 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3989 I915_READ(HBLANK(cpu_transcoder)));
3990 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3991 I915_READ(HSYNC(cpu_transcoder)));
3992
3993 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3994 I915_READ(VTOTAL(cpu_transcoder)));
3995 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3996 I915_READ(VBLANK(cpu_transcoder)));
3997 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3998 I915_READ(VSYNC(cpu_transcoder)));
3999 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4000 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4001}
4002
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004003static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004004{
4005 struct drm_i915_private *dev_priv = dev->dev_private;
4006 uint32_t temp;
4007
4008 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004009 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004010 return;
4011
4012 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4013 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4014
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004015 temp &= ~FDI_BC_BIFURCATION_SELECT;
4016 if (enable)
4017 temp |= FDI_BC_BIFURCATION_SELECT;
4018
4019 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004020 I915_WRITE(SOUTH_CHICKEN1, temp);
4021 POSTING_READ(SOUTH_CHICKEN1);
4022}
4023
4024static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4025{
4026 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004027
4028 switch (intel_crtc->pipe) {
4029 case PIPE_A:
4030 break;
4031 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004032 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004033 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004034 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004035 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004036
4037 break;
4038 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004039 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004040
4041 break;
4042 default:
4043 BUG();
4044 }
4045}
4046
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004047/* Return which DP Port should be selected for Transcoder DP control */
4048static enum port
4049intel_trans_dp_port_sel(struct drm_crtc *crtc)
4050{
4051 struct drm_device *dev = crtc->dev;
4052 struct intel_encoder *encoder;
4053
4054 for_each_encoder_on_crtc(dev, crtc, encoder) {
4055 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4056 encoder->type == INTEL_OUTPUT_EDP)
4057 return enc_to_dig_port(&encoder->base)->port;
4058 }
4059
4060 return -1;
4061}
4062
Jesse Barnesf67a5592011-01-05 10:31:48 -08004063/*
4064 * Enable PCH resources required for PCH ports:
4065 * - PCH PLLs
4066 * - FDI training & RX/TX
4067 * - update transcoder timings
4068 * - DP transcoding bits
4069 * - transcoder
4070 */
4071static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004072{
4073 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004074 struct drm_i915_private *dev_priv = dev->dev_private;
4075 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4076 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004077 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004078
Daniel Vetterab9412b2013-05-03 11:49:46 +02004079 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004080
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004081 if (IS_IVYBRIDGE(dev))
4082 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4083
Daniel Vettercd986ab2012-10-26 10:58:12 +02004084 /* Write the TU size bits before fdi link training, so that error
4085 * detection works. */
4086 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4087 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4088
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004089 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004090 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004091
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004092 /* We need to program the right clock selection before writing the pixel
4093 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004094 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004095 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004096
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004097 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004098 temp |= TRANS_DPLL_ENABLE(pipe);
4099 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004100 if (intel_crtc->config->shared_dpll ==
4101 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004102 temp |= sel;
4103 else
4104 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004105 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004106 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004107
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004108 /* XXX: pch pll's can be enabled any time before we enable the PCH
4109 * transcoder, and we actually should do this to not upset any PCH
4110 * transcoder that already use the clock when we share it.
4111 *
4112 * Note that enable_shared_dpll tries to do the right thing, but
4113 * get_shared_dpll unconditionally resets the pll - we need that to have
4114 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004115 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004116
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004117 /* set transcoder timing, panel must allow it */
4118 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004119 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004120
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004121 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004122
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004123 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004124 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004125 const struct drm_display_mode *adjusted_mode =
4126 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004127 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004128 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004129 temp = I915_READ(reg);
4130 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004131 TRANS_DP_SYNC_MASK |
4132 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004133 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004134 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004135
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004136 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004137 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004138 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004139 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004140
4141 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004142 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004143 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004144 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004145 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004146 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004147 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004148 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004149 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004150 break;
4151 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004152 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004153 }
4154
Chris Wilson5eddb702010-09-11 13:48:45 +01004155 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004156 }
4157
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004158 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004159}
4160
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004161static void lpt_pch_enable(struct drm_crtc *crtc)
4162{
4163 struct drm_device *dev = crtc->dev;
4164 struct drm_i915_private *dev_priv = dev->dev_private;
4165 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004166 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004167
Daniel Vetterab9412b2013-05-03 11:49:46 +02004168 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004169
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004170 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004171
Paulo Zanoni0540e482012-10-31 18:12:40 -02004172 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004173 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004174
Paulo Zanoni937bb612012-10-31 18:12:47 -02004175 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004176}
4177
Daniel Vettera1520312013-05-03 11:49:50 +02004178static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004179{
4180 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004181 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004182 u32 temp;
4183
4184 temp = I915_READ(dslreg);
4185 udelay(500);
4186 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004187 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004188 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004189 }
4190}
4191
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004192static int
4193skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4194 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4195 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004196{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004197 struct intel_crtc_scaler_state *scaler_state =
4198 &crtc_state->scaler_state;
4199 struct intel_crtc *intel_crtc =
4200 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004201 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004202
4203 need_scaling = intel_rotation_90_or_270(rotation) ?
4204 (src_h != dst_w || src_w != dst_h):
4205 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004206
4207 /*
4208 * if plane is being disabled or scaler is no more required or force detach
4209 * - free scaler binded to this plane/crtc
4210 * - in order to do this, update crtc->scaler_usage
4211 *
4212 * Here scaler state in crtc_state is set free so that
4213 * scaler can be assigned to other user. Actual register
4214 * update to free the scaler is done in plane/panel-fit programming.
4215 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4216 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004217 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004218 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004219 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004220 scaler_state->scalers[*scaler_id].in_use = 0;
4221
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004222 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4223 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4224 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004225 scaler_state->scaler_users);
4226 *scaler_id = -1;
4227 }
4228 return 0;
4229 }
4230
4231 /* range checks */
4232 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4233 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4234
4235 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4236 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004237 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004238 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004239 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004240 return -EINVAL;
4241 }
4242
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004243 /* mark this plane as a scaler user in crtc_state */
4244 scaler_state->scaler_users |= (1 << scaler_user);
4245 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4246 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4247 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4248 scaler_state->scaler_users);
4249
4250 return 0;
4251}
4252
4253/**
4254 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4255 *
4256 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004257 *
4258 * Return
4259 * 0 - scaler_usage updated successfully
4260 * error - requested scaling cannot be supported or other error condition
4261 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004262int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004263{
4264 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004265 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004266
4267 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4268 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4269
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004270 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004271 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004272 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004273 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004274}
4275
4276/**
4277 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4278 *
4279 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004280 * @plane_state: atomic plane state to update
4281 *
4282 * Return
4283 * 0 - scaler_usage updated successfully
4284 * error - requested scaling cannot be supported or other error condition
4285 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004286static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4287 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004288{
4289
4290 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004291 struct intel_plane *intel_plane =
4292 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004293 struct drm_framebuffer *fb = plane_state->base.fb;
4294 int ret;
4295
4296 bool force_detach = !fb || !plane_state->visible;
4297
4298 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4299 intel_plane->base.base.id, intel_crtc->pipe,
4300 drm_plane_index(&intel_plane->base));
4301
4302 ret = skl_update_scaler(crtc_state, force_detach,
4303 drm_plane_index(&intel_plane->base),
4304 &plane_state->scaler_id,
4305 plane_state->base.rotation,
4306 drm_rect_width(&plane_state->src) >> 16,
4307 drm_rect_height(&plane_state->src) >> 16,
4308 drm_rect_width(&plane_state->dst),
4309 drm_rect_height(&plane_state->dst));
4310
4311 if (ret || plane_state->scaler_id < 0)
4312 return ret;
4313
Chandra Kondurua1b22782015-04-07 15:28:45 -07004314 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004315 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004316 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004317 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004318 return -EINVAL;
4319 }
4320
4321 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004322 switch (fb->pixel_format) {
4323 case DRM_FORMAT_RGB565:
4324 case DRM_FORMAT_XBGR8888:
4325 case DRM_FORMAT_XRGB8888:
4326 case DRM_FORMAT_ABGR8888:
4327 case DRM_FORMAT_ARGB8888:
4328 case DRM_FORMAT_XRGB2101010:
4329 case DRM_FORMAT_XBGR2101010:
4330 case DRM_FORMAT_YUYV:
4331 case DRM_FORMAT_YVYU:
4332 case DRM_FORMAT_UYVY:
4333 case DRM_FORMAT_VYUY:
4334 break;
4335 default:
4336 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4337 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4338 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004339 }
4340
Chandra Kondurua1b22782015-04-07 15:28:45 -07004341 return 0;
4342}
4343
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004344static void skylake_scaler_disable(struct intel_crtc *crtc)
4345{
4346 int i;
4347
4348 for (i = 0; i < crtc->num_scalers; i++)
4349 skl_detach_scaler(crtc, i);
4350}
4351
4352static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004353{
4354 struct drm_device *dev = crtc->base.dev;
4355 struct drm_i915_private *dev_priv = dev->dev_private;
4356 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004357 struct intel_crtc_scaler_state *scaler_state =
4358 &crtc->config->scaler_state;
4359
4360 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4361
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004362 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004363 int id;
4364
4365 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4366 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4367 return;
4368 }
4369
4370 id = scaler_state->scaler_id;
4371 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4372 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4373 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4374 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4375
4376 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004377 }
4378}
4379
Jesse Barnesb074cec2013-04-25 12:55:02 -07004380static void ironlake_pfit_enable(struct intel_crtc *crtc)
4381{
4382 struct drm_device *dev = crtc->base.dev;
4383 struct drm_i915_private *dev_priv = dev->dev_private;
4384 int pipe = crtc->pipe;
4385
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004386 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004387 /* Force use of hard-coded filter coefficients
4388 * as some pre-programmed values are broken,
4389 * e.g. x201.
4390 */
4391 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4392 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4393 PF_PIPE_SEL_IVB(pipe));
4394 else
4395 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004396 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4397 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004398 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004399}
4400
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004401void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004402{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004403 struct drm_device *dev = crtc->base.dev;
4404 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004405
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004406 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004407 return;
4408
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004409 /*
4410 * We can only enable IPS after we enable a plane and wait for a vblank
4411 * This function is called from post_plane_update, which is run after
4412 * a vblank wait.
4413 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004414
Paulo Zanonid77e4532013-09-24 13:52:55 -03004415 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004416 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004417 mutex_lock(&dev_priv->rps.hw_lock);
4418 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4419 mutex_unlock(&dev_priv->rps.hw_lock);
4420 /* Quoting Art Runyan: "its not safe to expect any particular
4421 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004422 * mailbox." Moreover, the mailbox may return a bogus state,
4423 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004424 */
4425 } else {
4426 I915_WRITE(IPS_CTL, IPS_ENABLE);
4427 /* The bit only becomes 1 in the next vblank, so this wait here
4428 * is essentially intel_wait_for_vblank. If we don't have this
4429 * and don't wait for vblanks until the end of crtc_enable, then
4430 * the HW state readout code will complain that the expected
4431 * IPS_CTL value is not the one we read. */
4432 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4433 DRM_ERROR("Timed out waiting for IPS enable\n");
4434 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004435}
4436
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004437void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004438{
4439 struct drm_device *dev = crtc->base.dev;
4440 struct drm_i915_private *dev_priv = dev->dev_private;
4441
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004442 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004443 return;
4444
4445 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004446 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004447 mutex_lock(&dev_priv->rps.hw_lock);
4448 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4449 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004450 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4451 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4452 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004453 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004454 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004455 POSTING_READ(IPS_CTL);
4456 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004457
4458 /* We need to wait for a vblank before we can disable the plane. */
4459 intel_wait_for_vblank(dev, crtc->pipe);
4460}
4461
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004462static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004463{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004464 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004465 struct drm_device *dev = intel_crtc->base.dev;
4466 struct drm_i915_private *dev_priv = dev->dev_private;
4467
4468 mutex_lock(&dev->struct_mutex);
4469 dev_priv->mm.interruptible = false;
4470 (void) intel_overlay_switch_off(intel_crtc->overlay);
4471 dev_priv->mm.interruptible = true;
4472 mutex_unlock(&dev->struct_mutex);
4473 }
4474
4475 /* Let userspace switch the overlay on again. In most cases userspace
4476 * has to recompute where to put it anyway.
4477 */
4478}
4479
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004480/**
4481 * intel_post_enable_primary - Perform operations after enabling primary plane
4482 * @crtc: the CRTC whose primary plane was just enabled
4483 *
4484 * Performs potentially sleeping operations that must be done after the primary
4485 * plane is enabled, such as updating FBC and IPS. Note that this may be
4486 * called due to an explicit primary plane update, or due to an implicit
4487 * re-enable that is caused when a sprite plane is updated to no longer
4488 * completely hide the primary plane.
4489 */
4490static void
4491intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004492{
4493 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004494 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004495 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4496 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004497
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004498 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004499 * FIXME IPS should be fine as long as one plane is
4500 * enabled, but in practice it seems to have problems
4501 * when going from primary only to sprite only and vice
4502 * versa.
4503 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004504 hsw_enable_ips(intel_crtc);
4505
Daniel Vetterf99d7062014-06-19 16:01:59 +02004506 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004507 * Gen2 reports pipe underruns whenever all planes are disabled.
4508 * So don't enable underrun reporting before at least some planes
4509 * are enabled.
4510 * FIXME: Need to fix the logic to work when we turn off all planes
4511 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004512 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004513 if (IS_GEN2(dev))
4514 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4515
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004516 /* Underruns don't always raise interrupts, so check manually. */
4517 intel_check_cpu_fifo_underruns(dev_priv);
4518 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004519}
4520
Ville Syrjälä2622a082016-03-09 19:07:26 +02004521/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004522static void
4523intel_pre_disable_primary(struct drm_crtc *crtc)
4524{
4525 struct drm_device *dev = crtc->dev;
4526 struct drm_i915_private *dev_priv = dev->dev_private;
4527 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4528 int pipe = intel_crtc->pipe;
4529
4530 /*
4531 * Gen2 reports pipe underruns whenever all planes are disabled.
4532 * So diasble underrun reporting before all the planes get disabled.
4533 * FIXME: Need to fix the logic to work when we turn off all planes
4534 * but leave the pipe running.
4535 */
4536 if (IS_GEN2(dev))
4537 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4538
4539 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004540 * FIXME IPS should be fine as long as one plane is
4541 * enabled, but in practice it seems to have problems
4542 * when going from primary only to sprite only and vice
4543 * versa.
4544 */
4545 hsw_disable_ips(intel_crtc);
4546}
4547
4548/* FIXME get rid of this and use pre_plane_update */
4549static void
4550intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4551{
4552 struct drm_device *dev = crtc->dev;
4553 struct drm_i915_private *dev_priv = dev->dev_private;
4554 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4555 int pipe = intel_crtc->pipe;
4556
4557 intel_pre_disable_primary(crtc);
4558
4559 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004560 * Vblank time updates from the shadow to live plane control register
4561 * are blocked if the memory self-refresh mode is active at that
4562 * moment. So to make sure the plane gets truly disabled, disable
4563 * first the self-refresh mode. The self-refresh enable bit in turn
4564 * will be checked/applied by the HW only at the next frame start
4565 * event which is after the vblank start event, so we need to have a
4566 * wait-for-vblank between disabling the plane and the pipe.
4567 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004568 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004569 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004570 dev_priv->wm.vlv.cxsr = false;
4571 intel_wait_for_vblank(dev, pipe);
4572 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004573}
4574
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004575static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004576{
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004577 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4578 struct drm_atomic_state *old_state = old_crtc_state->base.state;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004579 struct intel_crtc_state *pipe_config =
4580 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004581 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004582 struct drm_plane *primary = crtc->base.primary;
4583 struct drm_plane_state *old_pri_state =
4584 drm_atomic_get_existing_plane_state(old_state, primary);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004585
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004586 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004587
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004588 crtc->wm.cxsr_allowed = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +03004589
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004590 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjäläf015c552015-06-24 22:00:02 +03004591 intel_update_watermarks(&crtc->base);
4592
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004593 if (old_pri_state) {
4594 struct intel_plane_state *primary_state =
4595 to_intel_plane_state(primary->state);
4596 struct intel_plane_state *old_primary_state =
4597 to_intel_plane_state(old_pri_state);
4598
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004599 intel_fbc_post_update(crtc);
4600
Maarten Lankhorstcd202f62016-03-09 10:35:44 +01004601 if (primary_state->visible &&
4602 (needs_modeset(&pipe_config->base) ||
4603 !old_primary_state->visible))
4604 intel_post_enable_primary(&crtc->base);
4605 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004606}
4607
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004608static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004609{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004610 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004611 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004612 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004613 struct intel_crtc_state *pipe_config =
4614 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004615 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4616 struct drm_plane *primary = crtc->base.primary;
4617 struct drm_plane_state *old_pri_state =
4618 drm_atomic_get_existing_plane_state(old_state, primary);
4619 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004620
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004621 if (old_pri_state) {
4622 struct intel_plane_state *primary_state =
4623 to_intel_plane_state(primary->state);
4624 struct intel_plane_state *old_primary_state =
4625 to_intel_plane_state(old_pri_state);
4626
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004627 intel_fbc_pre_update(crtc);
4628
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004629 if (old_primary_state->visible &&
4630 (modeset || !primary_state->visible))
4631 intel_pre_disable_primary(&crtc->base);
4632 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004633
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004634 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004635 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004636
Ville Syrjälä2622a082016-03-09 19:07:26 +02004637 /*
4638 * Vblank time updates from the shadow to live plane control register
4639 * are blocked if the memory self-refresh mode is active at that
4640 * moment. So to make sure the plane gets truly disabled, disable
4641 * first the self-refresh mode. The self-refresh enable bit in turn
4642 * will be checked/applied by the HW only at the next frame start
4643 * event which is after the vblank start event, so we need to have a
4644 * wait-for-vblank between disabling the plane and the pipe.
4645 */
4646 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004647 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004648 dev_priv->wm.vlv.cxsr = false;
4649 intel_wait_for_vblank(dev, crtc->pipe);
4650 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004651 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004652
Matt Ropered4a6a72016-02-23 17:20:13 -08004653 /*
4654 * IVB workaround: must disable low power watermarks for at least
4655 * one frame before enabling scaling. LP watermarks can be re-enabled
4656 * when scaling is disabled.
4657 *
4658 * WaCxSRDisabledForSpriteScaling:ivb
4659 */
4660 if (pipe_config->disable_lp_wm) {
4661 ilk_disable_lp_wm(dev);
4662 intel_wait_for_vblank(dev, crtc->pipe);
4663 }
4664
4665 /*
4666 * If we're doing a modeset, we're done. No need to do any pre-vblank
4667 * watermark programming here.
4668 */
4669 if (needs_modeset(&pipe_config->base))
4670 return;
4671
4672 /*
4673 * For platforms that support atomic watermarks, program the
4674 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4675 * will be the intermediate values that are safe for both pre- and
4676 * post- vblank; when vblank happens, the 'active' values will be set
4677 * to the final 'target' values and we'll do this again to get the
4678 * optimal watermarks. For gen9+ platforms, the values we program here
4679 * will be the final target values which will get automatically latched
4680 * at vblank time; no further programming will be necessary.
4681 *
4682 * If a platform hasn't been transitioned to atomic watermarks yet,
4683 * we'll continue to update watermarks the old way, if flags tell
4684 * us to.
4685 */
4686 if (dev_priv->display.initial_watermarks != NULL)
4687 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004688 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004689 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004690}
4691
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004692static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004693{
4694 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004695 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004696 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004697 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004698
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004699 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004700
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004701 drm_for_each_plane_mask(p, dev, plane_mask)
4702 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004703
Daniel Vetterf99d7062014-06-19 16:01:59 +02004704 /*
4705 * FIXME: Once we grow proper nuclear flip support out of this we need
4706 * to compute the mask of flip planes precisely. For the time being
4707 * consider this a flip to a NULL plane.
4708 */
4709 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004710}
4711
Jesse Barnesf67a5592011-01-05 10:31:48 -08004712static void ironlake_crtc_enable(struct drm_crtc *crtc)
4713{
4714 struct drm_device *dev = crtc->dev;
4715 struct drm_i915_private *dev_priv = dev->dev_private;
4716 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004717 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004718 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004719 struct intel_crtc_state *pipe_config =
4720 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004721
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004722 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004723 return;
4724
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004725 /*
4726 * Sometimes spurious CPU pipe underruns happen during FDI
4727 * training, at least with VGA+HDMI cloning. Suppress them.
4728 *
4729 * On ILK we get an occasional spurious CPU pipe underruns
4730 * between eDP port A enable and vdd enable. Also PCH port
4731 * enable seems to result in the occasional CPU pipe underrun.
4732 *
4733 * Spurious PCH underruns also occur during PCH enabling.
4734 */
4735 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4736 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004737 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004738 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4739
4740 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004741 intel_prepare_shared_dpll(intel_crtc);
4742
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004743 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304744 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004745
4746 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004747 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004748
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004749 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004750 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004751 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004752 }
4753
4754 ironlake_set_pipeconf(crtc);
4755
Jesse Barnesf67a5592011-01-05 10:31:48 -08004756 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004757
Daniel Vetterf6736a12013-06-05 13:34:30 +02004758 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004759 if (encoder->pre_enable)
4760 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004761
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004762 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004763 /* Note: FDI PLL enabling _must_ be done before we enable the
4764 * cpu pipes, hence this is separate from all the other fdi/pch
4765 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004766 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004767 } else {
4768 assert_fdi_tx_disabled(dev_priv, pipe);
4769 assert_fdi_rx_disabled(dev_priv, pipe);
4770 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004771
Jesse Barnesb074cec2013-04-25 12:55:02 -07004772 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004773
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004774 /*
4775 * On ILK+ LUT must be loaded before the pipe is running but with
4776 * clocks enabled
4777 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004778 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004779
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004780 if (dev_priv->display.initial_watermarks != NULL)
4781 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004782 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004783
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004784 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004785 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004786
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004787 assert_vblank_disabled(crtc);
4788 drm_crtc_vblank_on(crtc);
4789
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004790 for_each_encoder_on_crtc(dev, crtc, encoder)
4791 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004792
4793 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004794 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004795
4796 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4797 if (intel_crtc->config->has_pch_encoder)
4798 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004799 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004800 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004801}
4802
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004803/* IPS only exists on ULT machines and is tied to pipe A. */
4804static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4805{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004806 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004807}
4808
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004809static void haswell_crtc_enable(struct drm_crtc *crtc)
4810{
4811 struct drm_device *dev = crtc->dev;
4812 struct drm_i915_private *dev_priv = dev->dev_private;
4813 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4814 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004815 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004816 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004817 struct intel_crtc_state *pipe_config =
4818 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004819
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004820 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004821 return;
4822
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004823 if (intel_crtc->config->has_pch_encoder)
4824 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4825 false);
4826
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004827 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004828 intel_enable_shared_dpll(intel_crtc);
4829
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004830 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304831 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004832
Jani Nikula4d1de972016-03-18 17:05:42 +02004833 if (!intel_crtc->config->has_dsi_encoder)
4834 intel_set_pipe_timings(intel_crtc);
4835
Jani Nikulabc58be62016-03-18 17:05:39 +02004836 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004837
Jani Nikula4d1de972016-03-18 17:05:42 +02004838 if (cpu_transcoder != TRANSCODER_EDP &&
4839 !transcoder_is_dsi(cpu_transcoder)) {
4840 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004841 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004842 }
4843
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004844 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004845 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004846 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004847 }
4848
Jani Nikula4d1de972016-03-18 17:05:42 +02004849 if (!intel_crtc->config->has_dsi_encoder)
4850 haswell_set_pipeconf(crtc);
4851
Jani Nikula391bf042016-03-18 17:05:40 +02004852 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004853
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004854 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02004855
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004856 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004857
Daniel Vetter6b698512015-11-28 11:05:39 +01004858 if (intel_crtc->config->has_pch_encoder)
4859 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4860 else
4861 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4862
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304863 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004864 if (encoder->pre_enable)
4865 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304866 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004867
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004868 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004869 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004870
Jani Nikulaa65347b2015-11-27 12:21:46 +02004871 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304872 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004873
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004874 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004875 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004876 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004877 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004878
4879 /*
4880 * On ILK+ LUT must be loaded before the pipe is running but with
4881 * clocks enabled
4882 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004883 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004884
Paulo Zanoni1f544382012-10-24 11:32:00 -02004885 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004886 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304887 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004888
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004889 if (dev_priv->display.initial_watermarks != NULL)
4890 dev_priv->display.initial_watermarks(pipe_config);
4891 else
4892 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004893
4894 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4895 if (!intel_crtc->config->has_dsi_encoder)
4896 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004897
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004898 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004899 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004900
Jani Nikulaa65347b2015-11-27 12:21:46 +02004901 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004902 intel_ddi_set_vc_payload_alloc(crtc, true);
4903
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004904 assert_vblank_disabled(crtc);
4905 drm_crtc_vblank_on(crtc);
4906
Jani Nikula8807e552013-08-30 19:40:32 +03004907 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004908 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004909 intel_opregion_notify_encoder(encoder, true);
4910 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004911
Daniel Vetter6b698512015-11-28 11:05:39 +01004912 if (intel_crtc->config->has_pch_encoder) {
4913 intel_wait_for_vblank(dev, pipe);
4914 intel_wait_for_vblank(dev, pipe);
4915 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004916 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4917 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004918 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004919
Paulo Zanonie4916942013-09-20 16:21:19 -03004920 /* If we change the relative order between pipe/planes enabling, we need
4921 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004922 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4923 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4924 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4925 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4926 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004927}
4928
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004929static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004930{
4931 struct drm_device *dev = crtc->base.dev;
4932 struct drm_i915_private *dev_priv = dev->dev_private;
4933 int pipe = crtc->pipe;
4934
4935 /* To avoid upsetting the power well on haswell only disable the pfit if
4936 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004937 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004938 I915_WRITE(PF_CTL(pipe), 0);
4939 I915_WRITE(PF_WIN_POS(pipe), 0);
4940 I915_WRITE(PF_WIN_SZ(pipe), 0);
4941 }
4942}
4943
Jesse Barnes6be4a602010-09-10 10:26:01 -07004944static void ironlake_crtc_disable(struct drm_crtc *crtc)
4945{
4946 struct drm_device *dev = crtc->dev;
4947 struct drm_i915_private *dev_priv = dev->dev_private;
4948 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004949 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004950 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004951
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004952 /*
4953 * Sometimes spurious CPU pipe underruns happen when the
4954 * pipe is already disabled, but FDI RX/TX is still enabled.
4955 * Happens at least with VGA+HDMI cloning. Suppress them.
4956 */
4957 if (intel_crtc->config->has_pch_encoder) {
4958 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004959 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004960 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004961
Daniel Vetterea9d7582012-07-10 10:42:52 +02004962 for_each_encoder_on_crtc(dev, crtc, encoder)
4963 encoder->disable(encoder);
4964
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004965 drm_crtc_vblank_off(crtc);
4966 assert_vblank_disabled(crtc);
4967
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004968 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004969
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004970 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004971
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004972 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004973 ironlake_fdi_disable(crtc);
4974
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004975 for_each_encoder_on_crtc(dev, crtc, encoder)
4976 if (encoder->post_disable)
4977 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004978
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004979 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004980 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004981
Daniel Vetterd925c592013-06-05 13:34:04 +02004982 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004983 i915_reg_t reg;
4984 u32 temp;
4985
Daniel Vetterd925c592013-06-05 13:34:04 +02004986 /* disable TRANS_DP_CTL */
4987 reg = TRANS_DP_CTL(pipe);
4988 temp = I915_READ(reg);
4989 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4990 TRANS_DP_PORT_SEL_MASK);
4991 temp |= TRANS_DP_PORT_SEL_NONE;
4992 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004993
Daniel Vetterd925c592013-06-05 13:34:04 +02004994 /* disable DPLL_SEL */
4995 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004996 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004997 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004998 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004999
Daniel Vetterd925c592013-06-05 13:34:04 +02005000 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005001 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005002
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005003 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005004 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005005}
5006
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005007static void haswell_crtc_disable(struct drm_crtc *crtc)
5008{
5009 struct drm_device *dev = crtc->dev;
5010 struct drm_i915_private *dev_priv = dev->dev_private;
5011 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5012 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005013 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005014
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005015 if (intel_crtc->config->has_pch_encoder)
5016 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5017 false);
5018
Jani Nikula8807e552013-08-30 19:40:32 +03005019 for_each_encoder_on_crtc(dev, crtc, encoder) {
5020 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005021 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005022 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005023
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005024 drm_crtc_vblank_off(crtc);
5025 assert_vblank_disabled(crtc);
5026
Jani Nikula4d1de972016-03-18 17:05:42 +02005027 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5028 if (!intel_crtc->config->has_dsi_encoder)
5029 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005030
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005031 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005032 intel_ddi_set_vc_payload_alloc(crtc, false);
5033
Jani Nikulaa65347b2015-11-27 12:21:46 +02005034 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305035 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005036
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005037 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005038 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005039 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005040 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005041
Jani Nikulaa65347b2015-11-27 12:21:46 +02005042 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305043 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005044
Imre Deak97b040a2014-06-25 22:01:50 +03005045 for_each_encoder_on_crtc(dev, crtc, encoder)
5046 if (encoder->post_disable)
5047 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005048
Ville Syrjälä92966a32015-12-08 16:05:48 +02005049 if (intel_crtc->config->has_pch_encoder) {
5050 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005051 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005052 intel_ddi_fdi_disable(crtc);
5053
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005054 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5055 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005056 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005057}
5058
Jesse Barnes2dd24552013-04-25 12:55:01 -07005059static void i9xx_pfit_enable(struct intel_crtc *crtc)
5060{
5061 struct drm_device *dev = crtc->base.dev;
5062 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005063 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005064
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005065 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005066 return;
5067
Daniel Vetterc0b03412013-05-28 12:05:54 +02005068 /*
5069 * The panel fitter should only be adjusted whilst the pipe is disabled,
5070 * according to register description and PRM.
5071 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005072 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5073 assert_pipe_disabled(dev_priv, crtc->pipe);
5074
Jesse Barnesb074cec2013-04-25 12:55:02 -07005075 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5076 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005077
5078 /* Border color in case we don't scale up to the full screen. Black by
5079 * default, change to something else for debugging. */
5080 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005081}
5082
Dave Airlied05410f2014-06-05 13:22:59 +10005083static enum intel_display_power_domain port_to_power_domain(enum port port)
5084{
5085 switch (port) {
5086 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005087 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005088 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005089 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005090 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005091 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005092 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005093 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005094 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005095 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005096 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005097 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005098 return POWER_DOMAIN_PORT_OTHER;
5099 }
5100}
5101
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005102static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5103{
5104 switch (port) {
5105 case PORT_A:
5106 return POWER_DOMAIN_AUX_A;
5107 case PORT_B:
5108 return POWER_DOMAIN_AUX_B;
5109 case PORT_C:
5110 return POWER_DOMAIN_AUX_C;
5111 case PORT_D:
5112 return POWER_DOMAIN_AUX_D;
5113 case PORT_E:
5114 /* FIXME: Check VBT for actual wiring of PORT E */
5115 return POWER_DOMAIN_AUX_D;
5116 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005117 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005118 return POWER_DOMAIN_AUX_A;
5119 }
5120}
5121
Imre Deak319be8a2014-03-04 19:22:57 +02005122enum intel_display_power_domain
5123intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005124{
Imre Deak319be8a2014-03-04 19:22:57 +02005125 struct drm_device *dev = intel_encoder->base.dev;
5126 struct intel_digital_port *intel_dig_port;
5127
5128 switch (intel_encoder->type) {
5129 case INTEL_OUTPUT_UNKNOWN:
5130 /* Only DDI platforms should ever use this output type */
5131 WARN_ON_ONCE(!HAS_DDI(dev));
5132 case INTEL_OUTPUT_DISPLAYPORT:
5133 case INTEL_OUTPUT_HDMI:
5134 case INTEL_OUTPUT_EDP:
5135 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005136 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005137 case INTEL_OUTPUT_DP_MST:
5138 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5139 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005140 case INTEL_OUTPUT_ANALOG:
5141 return POWER_DOMAIN_PORT_CRT;
5142 case INTEL_OUTPUT_DSI:
5143 return POWER_DOMAIN_PORT_DSI;
5144 default:
5145 return POWER_DOMAIN_PORT_OTHER;
5146 }
5147}
5148
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005149enum intel_display_power_domain
5150intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5151{
5152 struct drm_device *dev = intel_encoder->base.dev;
5153 struct intel_digital_port *intel_dig_port;
5154
5155 switch (intel_encoder->type) {
5156 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005157 case INTEL_OUTPUT_HDMI:
5158 /*
5159 * Only DDI platforms should ever use these output types.
5160 * We can get here after the HDMI detect code has already set
5161 * the type of the shared encoder. Since we can't be sure
5162 * what's the status of the given connectors, play safe and
5163 * run the DP detection too.
5164 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005165 WARN_ON_ONCE(!HAS_DDI(dev));
5166 case INTEL_OUTPUT_DISPLAYPORT:
5167 case INTEL_OUTPUT_EDP:
5168 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5169 return port_to_aux_power_domain(intel_dig_port->port);
5170 case INTEL_OUTPUT_DP_MST:
5171 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5172 return port_to_aux_power_domain(intel_dig_port->port);
5173 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005174 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005175 return POWER_DOMAIN_AUX_A;
5176 }
5177}
5178
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005179static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5180 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005181{
5182 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005183 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005184 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5185 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005186 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005187 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005188
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005189 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005190 return 0;
5191
Imre Deak77d22dc2014-03-05 16:20:52 +02005192 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5193 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005194 if (crtc_state->pch_pfit.enabled ||
5195 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005196 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5197
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005198 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5199 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5200
Imre Deak319be8a2014-03-04 19:22:57 +02005201 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005202 }
Imre Deak319be8a2014-03-04 19:22:57 +02005203
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005204 if (crtc_state->shared_dpll)
5205 mask |= BIT(POWER_DOMAIN_PLLS);
5206
Imre Deak77d22dc2014-03-05 16:20:52 +02005207 return mask;
5208}
5209
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005210static unsigned long
5211modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5212 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005213{
5214 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5215 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5216 enum intel_display_power_domain domain;
5217 unsigned long domains, new_domains, old_domains;
5218
5219 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005220 intel_crtc->enabled_power_domains = new_domains =
5221 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005222
5223 domains = new_domains & ~old_domains;
5224
5225 for_each_power_domain(domain, domains)
5226 intel_display_power_get(dev_priv, domain);
5227
5228 return old_domains & ~new_domains;
5229}
5230
5231static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5232 unsigned long domains)
5233{
5234 enum intel_display_power_domain domain;
5235
5236 for_each_power_domain(domain, domains)
5237 intel_display_power_put(dev_priv, domain);
5238}
5239
Mika Kaholaadafdc62015-08-18 14:36:59 +03005240static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5241{
5242 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5243
5244 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5245 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5246 return max_cdclk_freq;
5247 else if (IS_CHERRYVIEW(dev_priv))
5248 return max_cdclk_freq*95/100;
5249 else if (INTEL_INFO(dev_priv)->gen < 4)
5250 return 2*max_cdclk_freq*90/100;
5251 else
5252 return max_cdclk_freq*90/100;
5253}
5254
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005255static void intel_update_max_cdclk(struct drm_device *dev)
5256{
5257 struct drm_i915_private *dev_priv = dev->dev_private;
5258
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005259 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005260 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5261
5262 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5263 dev_priv->max_cdclk_freq = 675000;
5264 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5265 dev_priv->max_cdclk_freq = 540000;
5266 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5267 dev_priv->max_cdclk_freq = 450000;
5268 else
5269 dev_priv->max_cdclk_freq = 337500;
Matt Roper281c1142016-04-05 14:37:19 -07005270 } else if (IS_BROXTON(dev)) {
5271 dev_priv->max_cdclk_freq = 624000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005272 } else if (IS_BROADWELL(dev)) {
5273 /*
5274 * FIXME with extra cooling we can allow
5275 * 540 MHz for ULX and 675 Mhz for ULT.
5276 * How can we know if extra cooling is
5277 * available? PCI ID, VTB, something else?
5278 */
5279 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5280 dev_priv->max_cdclk_freq = 450000;
5281 else if (IS_BDW_ULX(dev))
5282 dev_priv->max_cdclk_freq = 450000;
5283 else if (IS_BDW_ULT(dev))
5284 dev_priv->max_cdclk_freq = 540000;
5285 else
5286 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005287 } else if (IS_CHERRYVIEW(dev)) {
5288 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005289 } else if (IS_VALLEYVIEW(dev)) {
5290 dev_priv->max_cdclk_freq = 400000;
5291 } else {
5292 /* otherwise assume cdclk is fixed */
5293 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5294 }
5295
Mika Kaholaadafdc62015-08-18 14:36:59 +03005296 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5297
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005298 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5299 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005300
5301 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5302 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005303}
5304
5305static void intel_update_cdclk(struct drm_device *dev)
5306{
5307 struct drm_i915_private *dev_priv = dev->dev_private;
5308
5309 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5310 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5311 dev_priv->cdclk_freq);
5312
5313 /*
5314 * Program the gmbus_freq based on the cdclk frequency.
5315 * BSpec erroneously claims we should aim for 4MHz, but
5316 * in fact 1MHz is the correct frequency.
5317 */
Wayne Boyer666a4532015-12-09 12:29:35 -08005318 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005319 /*
5320 * Program the gmbus_freq based on the cdclk frequency.
5321 * BSpec erroneously claims we should aim for 4MHz, but
5322 * in fact 1MHz is the correct frequency.
5323 */
5324 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5325 }
5326
5327 if (dev_priv->max_cdclk_freq == 0)
5328 intel_update_max_cdclk(dev);
5329}
5330
Imre Deakc6c46962016-04-01 16:02:40 +03005331static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305332{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305333 uint32_t divider;
5334 uint32_t ratio;
5335 uint32_t current_freq;
5336 int ret;
5337
5338 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5339 switch (frequency) {
5340 case 144000:
5341 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5342 ratio = BXT_DE_PLL_RATIO(60);
5343 break;
5344 case 288000:
5345 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5346 ratio = BXT_DE_PLL_RATIO(60);
5347 break;
5348 case 384000:
5349 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5350 ratio = BXT_DE_PLL_RATIO(60);
5351 break;
5352 case 576000:
5353 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5354 ratio = BXT_DE_PLL_RATIO(60);
5355 break;
5356 case 624000:
5357 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5358 ratio = BXT_DE_PLL_RATIO(65);
5359 break;
5360 case 19200:
5361 /*
5362 * Bypass frequency with DE PLL disabled. Init ratio, divider
5363 * to suppress GCC warning.
5364 */
5365 ratio = 0;
5366 divider = 0;
5367 break;
5368 default:
5369 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5370
5371 return;
5372 }
5373
5374 mutex_lock(&dev_priv->rps.hw_lock);
5375 /* Inform power controller of upcoming frequency change */
5376 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5377 0x80000000);
5378 mutex_unlock(&dev_priv->rps.hw_lock);
5379
5380 if (ret) {
5381 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5382 ret, frequency);
5383 return;
5384 }
5385
5386 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5387 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5388 current_freq = current_freq * 500 + 1000;
5389
5390 /*
5391 * DE PLL has to be disabled when
5392 * - setting to 19.2MHz (bypass, PLL isn't used)
5393 * - before setting to 624MHz (PLL needs toggling)
5394 * - before setting to any frequency from 624MHz (PLL needs toggling)
5395 */
5396 if (frequency == 19200 || frequency == 624000 ||
5397 current_freq == 624000) {
5398 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5399 /* Timeout 200us */
5400 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5401 1))
5402 DRM_ERROR("timout waiting for DE PLL unlock\n");
5403 }
5404
5405 if (frequency != 19200) {
5406 uint32_t val;
5407
5408 val = I915_READ(BXT_DE_PLL_CTL);
5409 val &= ~BXT_DE_PLL_RATIO_MASK;
5410 val |= ratio;
5411 I915_WRITE(BXT_DE_PLL_CTL, val);
5412
5413 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5414 /* Timeout 200us */
5415 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5416 DRM_ERROR("timeout waiting for DE PLL lock\n");
5417
5418 val = I915_READ(CDCLK_CTL);
5419 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5420 val |= divider;
5421 /*
5422 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5423 * enable otherwise.
5424 */
5425 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5426 if (frequency >= 500000)
5427 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5428
5429 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5430 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5431 val |= (frequency - 1000) / 500;
5432 I915_WRITE(CDCLK_CTL, val);
5433 }
5434
5435 mutex_lock(&dev_priv->rps.hw_lock);
5436 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5437 DIV_ROUND_UP(frequency, 25000));
5438 mutex_unlock(&dev_priv->rps.hw_lock);
5439
5440 if (ret) {
5441 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5442 ret, frequency);
5443 return;
5444 }
5445
Imre Deakc6c46962016-04-01 16:02:40 +03005446 intel_update_cdclk(dev_priv->dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305447}
5448
Imre Deakc6c46962016-04-01 16:02:40 +03005449void broxton_init_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305450{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305451 uint32_t val;
5452
5453 /*
5454 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5455 * or else the reset will hang because there is no PCH to respond.
5456 * Move the handshake programming to initialization sequence.
5457 * Previously was left up to BIOS.
5458 */
5459 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5460 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5461 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5462
5463 /* Enable PG1 for cdclk */
5464 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5465
5466 /* check if cd clock is enabled */
5467 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5468 DRM_DEBUG_KMS("Display already initialized\n");
5469 return;
5470 }
5471
5472 /*
5473 * FIXME:
5474 * - The initial CDCLK needs to be read from VBT.
5475 * Need to make this change after VBT has changes for BXT.
5476 * - check if setting the max (or any) cdclk freq is really necessary
5477 * here, it belongs to modeset time
5478 */
Imre Deakc6c46962016-04-01 16:02:40 +03005479 broxton_set_cdclk(dev_priv, 624000);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305480
5481 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005482 POSTING_READ(DBUF_CTL);
5483
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305484 udelay(10);
5485
5486 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5487 DRM_ERROR("DBuf power enable timeout!\n");
5488}
5489
Imre Deakc6c46962016-04-01 16:02:40 +03005490void broxton_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305491{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305492 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005493 POSTING_READ(DBUF_CTL);
5494
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305495 udelay(10);
5496
5497 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5498 DRM_ERROR("DBuf power disable timeout!\n");
5499
5500 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
Imre Deakc6c46962016-04-01 16:02:40 +03005501 broxton_set_cdclk(dev_priv, 19200);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305502
5503 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5504}
5505
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005506static const struct skl_cdclk_entry {
5507 unsigned int freq;
5508 unsigned int vco;
5509} skl_cdclk_frequencies[] = {
5510 { .freq = 308570, .vco = 8640 },
5511 { .freq = 337500, .vco = 8100 },
5512 { .freq = 432000, .vco = 8640 },
5513 { .freq = 450000, .vco = 8100 },
5514 { .freq = 540000, .vco = 8100 },
5515 { .freq = 617140, .vco = 8640 },
5516 { .freq = 675000, .vco = 8100 },
5517};
5518
5519static unsigned int skl_cdclk_decimal(unsigned int freq)
5520{
5521 return (freq - 1000) / 500;
5522}
5523
5524static unsigned int skl_cdclk_get_vco(unsigned int freq)
5525{
5526 unsigned int i;
5527
5528 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5529 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5530
5531 if (e->freq == freq)
5532 return e->vco;
5533 }
5534
5535 return 8100;
5536}
5537
5538static void
5539skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5540{
5541 unsigned int min_freq;
5542 u32 val;
5543
5544 /* select the minimum CDCLK before enabling DPLL 0 */
5545 val = I915_READ(CDCLK_CTL);
5546 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5547 val |= CDCLK_FREQ_337_308;
5548
5549 if (required_vco == 8640)
5550 min_freq = 308570;
5551 else
5552 min_freq = 337500;
5553
5554 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5555
5556 I915_WRITE(CDCLK_CTL, val);
5557 POSTING_READ(CDCLK_CTL);
5558
5559 /*
5560 * We always enable DPLL0 with the lowest link rate possible, but still
5561 * taking into account the VCO required to operate the eDP panel at the
5562 * desired frequency. The usual DP link rates operate with a VCO of
5563 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5564 * The modeset code is responsible for the selection of the exact link
5565 * rate later on, with the constraint of choosing a frequency that
5566 * works with required_vco.
5567 */
5568 val = I915_READ(DPLL_CTRL1);
5569
5570 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5571 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5572 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5573 if (required_vco == 8640)
5574 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5575 SKL_DPLL0);
5576 else
5577 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5578 SKL_DPLL0);
5579
5580 I915_WRITE(DPLL_CTRL1, val);
5581 POSTING_READ(DPLL_CTRL1);
5582
5583 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5584
5585 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5586 DRM_ERROR("DPLL0 not locked\n");
5587}
5588
5589static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5590{
5591 int ret;
5592 u32 val;
5593
5594 /* inform PCU we want to change CDCLK */
5595 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5596 mutex_lock(&dev_priv->rps.hw_lock);
5597 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5598 mutex_unlock(&dev_priv->rps.hw_lock);
5599
5600 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5601}
5602
5603static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5604{
5605 unsigned int i;
5606
5607 for (i = 0; i < 15; i++) {
5608 if (skl_cdclk_pcu_ready(dev_priv))
5609 return true;
5610 udelay(10);
5611 }
5612
5613 return false;
5614}
5615
5616static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5617{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005618 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005619 u32 freq_select, pcu_ack;
5620
5621 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5622
5623 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5624 DRM_ERROR("failed to inform PCU about cdclk change\n");
5625 return;
5626 }
5627
5628 /* set CDCLK_CTL */
5629 switch(freq) {
5630 case 450000:
5631 case 432000:
5632 freq_select = CDCLK_FREQ_450_432;
5633 pcu_ack = 1;
5634 break;
5635 case 540000:
5636 freq_select = CDCLK_FREQ_540;
5637 pcu_ack = 2;
5638 break;
5639 case 308570:
5640 case 337500:
5641 default:
5642 freq_select = CDCLK_FREQ_337_308;
5643 pcu_ack = 0;
5644 break;
5645 case 617140:
5646 case 675000:
5647 freq_select = CDCLK_FREQ_675_617;
5648 pcu_ack = 3;
5649 break;
5650 }
5651
5652 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5653 POSTING_READ(CDCLK_CTL);
5654
5655 /* inform PCU of the change */
5656 mutex_lock(&dev_priv->rps.hw_lock);
5657 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5658 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005659
5660 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005661}
5662
5663void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5664{
5665 /* disable DBUF power */
5666 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5667 POSTING_READ(DBUF_CTL);
5668
5669 udelay(10);
5670
5671 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5672 DRM_ERROR("DBuf power disable timeout\n");
5673
Imre Deakab96c1ee2015-11-04 19:24:18 +02005674 /* disable DPLL0 */
5675 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5676 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5677 DRM_ERROR("Couldn't disable DPLL0\n");
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005678}
5679
5680void skl_init_cdclk(struct drm_i915_private *dev_priv)
5681{
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005682 unsigned int required_vco;
5683
Gary Wang39d9b852015-08-28 16:40:34 +08005684 /* DPLL0 not enabled (happens on early BIOS versions) */
5685 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5686 /* enable DPLL0 */
5687 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5688 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005689 }
5690
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005691 /* set CDCLK to the frequency the BIOS chose */
5692 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5693
5694 /* enable DBUF power */
5695 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5696 POSTING_READ(DBUF_CTL);
5697
5698 udelay(10);
5699
5700 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5701 DRM_ERROR("DBuf power enable timeout\n");
5702}
5703
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305704int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
5705{
5706 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5707 uint32_t cdctl = I915_READ(CDCLK_CTL);
5708 int freq = dev_priv->skl_boot_cdclk;
5709
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305710 /*
5711 * check if the pre-os intialized the display
5712 * There is SWF18 scratchpad register defined which is set by the
5713 * pre-os which can be used by the OS drivers to check the status
5714 */
5715 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5716 goto sanitize;
5717
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305718 /* Is PLL enabled and locked ? */
5719 if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
5720 goto sanitize;
5721
5722 /* DPLL okay; verify the cdclock
5723 *
5724 * Noticed in some instances that the freq selection is correct but
5725 * decimal part is programmed wrong from BIOS where pre-os does not
5726 * enable display. Verify the same as well.
5727 */
5728 if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
5729 /* All well; nothing to sanitize */
5730 return false;
5731sanitize:
5732 /*
5733 * As of now initialize with max cdclk till
5734 * we get dynamic cdclk support
5735 * */
5736 dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
5737 skl_init_cdclk(dev_priv);
5738
5739 /* we did have to sanitize */
5740 return true;
5741}
5742
Jesse Barnes30a970c2013-11-04 13:48:12 -08005743/* Adjust CDclk dividers to allow high res or save power if possible */
5744static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5745{
5746 struct drm_i915_private *dev_priv = dev->dev_private;
5747 u32 val, cmd;
5748
Vandana Kannan164dfd22014-11-24 13:37:41 +05305749 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5750 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005751
Ville Syrjälädfcab172014-06-13 13:37:47 +03005752 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005753 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005754 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005755 cmd = 1;
5756 else
5757 cmd = 0;
5758
5759 mutex_lock(&dev_priv->rps.hw_lock);
5760 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5761 val &= ~DSPFREQGUAR_MASK;
5762 val |= (cmd << DSPFREQGUAR_SHIFT);
5763 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5764 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5765 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5766 50)) {
5767 DRM_ERROR("timed out waiting for CDclk change\n");
5768 }
5769 mutex_unlock(&dev_priv->rps.hw_lock);
5770
Ville Syrjälä54433e92015-05-26 20:42:31 +03005771 mutex_lock(&dev_priv->sb_lock);
5772
Ville Syrjälädfcab172014-06-13 13:37:47 +03005773 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005774 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005775
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005776 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005777
Jesse Barnes30a970c2013-11-04 13:48:12 -08005778 /* adjust cdclk divider */
5779 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005780 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005781 val |= divider;
5782 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005783
5784 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005785 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005786 50))
5787 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005788 }
5789
Jesse Barnes30a970c2013-11-04 13:48:12 -08005790 /* adjust self-refresh exit latency value */
5791 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5792 val &= ~0x7f;
5793
5794 /*
5795 * For high bandwidth configs, we set a higher latency in the bunit
5796 * so that the core display fetch happens in time to avoid underruns.
5797 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005798 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005799 val |= 4500 / 250; /* 4.5 usec */
5800 else
5801 val |= 3000 / 250; /* 3.0 usec */
5802 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005803
Ville Syrjäläa5805162015-05-26 20:42:30 +03005804 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005805
Ville Syrjäläb6283052015-06-03 15:45:07 +03005806 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005807}
5808
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005809static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5810{
5811 struct drm_i915_private *dev_priv = dev->dev_private;
5812 u32 val, cmd;
5813
Vandana Kannan164dfd22014-11-24 13:37:41 +05305814 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5815 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005816
5817 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005818 case 333333:
5819 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005820 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005821 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005822 break;
5823 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005824 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005825 return;
5826 }
5827
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005828 /*
5829 * Specs are full of misinformation, but testing on actual
5830 * hardware has shown that we just need to write the desired
5831 * CCK divider into the Punit register.
5832 */
5833 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5834
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005835 mutex_lock(&dev_priv->rps.hw_lock);
5836 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5837 val &= ~DSPFREQGUAR_MASK_CHV;
5838 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5839 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5840 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5841 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5842 50)) {
5843 DRM_ERROR("timed out waiting for CDclk change\n");
5844 }
5845 mutex_unlock(&dev_priv->rps.hw_lock);
5846
Ville Syrjäläb6283052015-06-03 15:45:07 +03005847 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005848}
5849
Jesse Barnes30a970c2013-11-04 13:48:12 -08005850static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5851 int max_pixclk)
5852{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005853 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005854 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005855
Jesse Barnes30a970c2013-11-04 13:48:12 -08005856 /*
5857 * Really only a few cases to deal with, as only 4 CDclks are supported:
5858 * 200MHz
5859 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005860 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005861 * 400MHz (VLV only)
5862 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5863 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005864 *
5865 * We seem to get an unstable or solid color picture at 200MHz.
5866 * Not sure what's wrong. For now use 200MHz only when all pipes
5867 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005868 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005869 if (!IS_CHERRYVIEW(dev_priv) &&
5870 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005871 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005872 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005873 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005874 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005875 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005876 else
5877 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005878}
5879
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305880static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5881 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005882{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305883 /*
5884 * FIXME:
5885 * - remove the guardband, it's not needed on BXT
5886 * - set 19.2MHz bypass frequency if there are no active pipes
5887 */
5888 if (max_pixclk > 576000*9/10)
5889 return 624000;
5890 else if (max_pixclk > 384000*9/10)
5891 return 576000;
5892 else if (max_pixclk > 288000*9/10)
5893 return 384000;
5894 else if (max_pixclk > 144000*9/10)
5895 return 288000;
5896 else
5897 return 144000;
5898}
5899
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005900/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005901static int intel_mode_max_pixclk(struct drm_device *dev,
5902 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005903{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005904 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5905 struct drm_i915_private *dev_priv = dev->dev_private;
5906 struct drm_crtc *crtc;
5907 struct drm_crtc_state *crtc_state;
5908 unsigned max_pixclk = 0, i;
5909 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005910
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005911 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5912 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005913
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005914 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5915 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005916
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005917 if (crtc_state->enable)
5918 pixclk = crtc_state->adjusted_mode.crtc_clock;
5919
5920 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005921 }
5922
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005923 for_each_pipe(dev_priv, pipe)
5924 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5925
Jesse Barnes30a970c2013-11-04 13:48:12 -08005926 return max_pixclk;
5927}
5928
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005929static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005930{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005931 struct drm_device *dev = state->dev;
5932 struct drm_i915_private *dev_priv = dev->dev_private;
5933 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005934 struct intel_atomic_state *intel_state =
5935 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005936
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005937 if (max_pixclk < 0)
5938 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005939
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005940 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005941 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305942
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005943 if (!intel_state->active_crtcs)
5944 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5945
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005946 return 0;
5947}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005948
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005949static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5950{
5951 struct drm_device *dev = state->dev;
5952 struct drm_i915_private *dev_priv = dev->dev_private;
5953 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005954 struct intel_atomic_state *intel_state =
5955 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005956
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005957 if (max_pixclk < 0)
5958 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005959
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005960 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005961 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005962
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005963 if (!intel_state->active_crtcs)
5964 intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
5965
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005966 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005967}
5968
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005969static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5970{
5971 unsigned int credits, default_credits;
5972
5973 if (IS_CHERRYVIEW(dev_priv))
5974 default_credits = PFI_CREDIT(12);
5975 else
5976 default_credits = PFI_CREDIT(8);
5977
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005978 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005979 /* CHV suggested value is 31 or 63 */
5980 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005981 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005982 else
5983 credits = PFI_CREDIT(15);
5984 } else {
5985 credits = default_credits;
5986 }
5987
5988 /*
5989 * WA - write default credits before re-programming
5990 * FIXME: should we also set the resend bit here?
5991 */
5992 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5993 default_credits);
5994
5995 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5996 credits | PFI_CREDIT_RESEND);
5997
5998 /*
5999 * FIXME is this guaranteed to clear
6000 * immediately or should we poll for it?
6001 */
6002 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6003}
6004
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006005static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006006{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006007 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006008 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006009 struct intel_atomic_state *old_intel_state =
6010 to_intel_atomic_state(old_state);
6011 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006012
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006013 /*
6014 * FIXME: We can end up here with all power domains off, yet
6015 * with a CDCLK frequency other than the minimum. To account
6016 * for this take the PIPE-A power domain, which covers the HW
6017 * blocks needed for the following programming. This can be
6018 * removed once it's guaranteed that we get here either with
6019 * the minimum CDCLK set, or the required power domains
6020 * enabled.
6021 */
6022 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006023
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006024 if (IS_CHERRYVIEW(dev))
6025 cherryview_set_cdclk(dev, req_cdclk);
6026 else
6027 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006028
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006029 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006030
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006031 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006032}
6033
Jesse Barnes89b667f2013-04-18 14:51:36 -07006034static void valleyview_crtc_enable(struct drm_crtc *crtc)
6035{
6036 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006037 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006038 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6039 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006040 struct intel_crtc_state *pipe_config =
6041 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006042 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006043
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006044 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006045 return;
6046
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006047 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306048 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006049
6050 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006051 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006052
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006053 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6054 struct drm_i915_private *dev_priv = dev->dev_private;
6055
6056 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6057 I915_WRITE(CHV_CANVAS(pipe), 0);
6058 }
6059
Daniel Vetter5b18e572014-04-24 23:55:06 +02006060 i9xx_set_pipeconf(intel_crtc);
6061
Jesse Barnes89b667f2013-04-18 14:51:36 -07006062 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006063
Daniel Vettera72e4c92014-09-30 10:56:47 +02006064 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006065
Jesse Barnes89b667f2013-04-18 14:51:36 -07006066 for_each_encoder_on_crtc(dev, crtc, encoder)
6067 if (encoder->pre_pll_enable)
6068 encoder->pre_pll_enable(encoder);
6069
Jani Nikulaa65347b2015-11-27 12:21:46 +02006070 if (!intel_crtc->config->has_dsi_encoder) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006071 if (IS_CHERRYVIEW(dev)) {
6072 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006073 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006074 } else {
6075 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006076 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006077 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006078 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006079
6080 for_each_encoder_on_crtc(dev, crtc, encoder)
6081 if (encoder->pre_enable)
6082 encoder->pre_enable(encoder);
6083
Jesse Barnes2dd24552013-04-25 12:55:01 -07006084 i9xx_pfit_enable(intel_crtc);
6085
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006086 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006087
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006088 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006089 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006090
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006091 assert_vblank_disabled(crtc);
6092 drm_crtc_vblank_on(crtc);
6093
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006094 for_each_encoder_on_crtc(dev, crtc, encoder)
6095 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006096}
6097
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006098static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6099{
6100 struct drm_device *dev = crtc->base.dev;
6101 struct drm_i915_private *dev_priv = dev->dev_private;
6102
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006103 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6104 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006105}
6106
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006107static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006108{
6109 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006110 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006111 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006112 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006113 struct intel_crtc_state *pipe_config =
6114 to_intel_crtc_state(crtc->state);
Jesse Barnes79e53942008-11-07 14:24:08 -08006115 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006116
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006117 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006118 return;
6119
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006120 i9xx_set_pll_dividers(intel_crtc);
6121
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006122 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306123 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006124
6125 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006126 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006127
Daniel Vetter5b18e572014-04-24 23:55:06 +02006128 i9xx_set_pipeconf(intel_crtc);
6129
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006130 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006131
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006132 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006133 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006134
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006135 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006136 if (encoder->pre_enable)
6137 encoder->pre_enable(encoder);
6138
Daniel Vetterf6736a12013-06-05 13:34:30 +02006139 i9xx_enable_pll(intel_crtc);
6140
Jesse Barnes2dd24552013-04-25 12:55:01 -07006141 i9xx_pfit_enable(intel_crtc);
6142
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006143 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006144
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006145 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006146 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006147
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006148 assert_vblank_disabled(crtc);
6149 drm_crtc_vblank_on(crtc);
6150
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006151 for_each_encoder_on_crtc(dev, crtc, encoder)
6152 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006153}
6154
Daniel Vetter87476d62013-04-11 16:29:06 +02006155static void i9xx_pfit_disable(struct intel_crtc *crtc)
6156{
6157 struct drm_device *dev = crtc->base.dev;
6158 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006159
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006160 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006161 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006162
6163 assert_pipe_disabled(dev_priv, crtc->pipe);
6164
Daniel Vetter328d8e82013-05-08 10:36:31 +02006165 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6166 I915_READ(PFIT_CONTROL));
6167 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006168}
6169
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006170static void i9xx_crtc_disable(struct drm_crtc *crtc)
6171{
6172 struct drm_device *dev = crtc->dev;
6173 struct drm_i915_private *dev_priv = dev->dev_private;
6174 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006175 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006176 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006177
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006178 /*
6179 * On gen2 planes are double buffered but the pipe isn't, so we must
6180 * wait for planes to fully turn off before disabling the pipe.
6181 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006182 if (IS_GEN2(dev))
6183 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006184
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006185 for_each_encoder_on_crtc(dev, crtc, encoder)
6186 encoder->disable(encoder);
6187
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006188 drm_crtc_vblank_off(crtc);
6189 assert_vblank_disabled(crtc);
6190
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006191 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006192
Daniel Vetter87476d62013-04-11 16:29:06 +02006193 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006194
Jesse Barnes89b667f2013-04-18 14:51:36 -07006195 for_each_encoder_on_crtc(dev, crtc, encoder)
6196 if (encoder->post_disable)
6197 encoder->post_disable(encoder);
6198
Jani Nikulaa65347b2015-11-27 12:21:46 +02006199 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006200 if (IS_CHERRYVIEW(dev))
6201 chv_disable_pll(dev_priv, pipe);
6202 else if (IS_VALLEYVIEW(dev))
6203 vlv_disable_pll(dev_priv, pipe);
6204 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006205 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006206 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006207
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006208 for_each_encoder_on_crtc(dev, crtc, encoder)
6209 if (encoder->post_pll_disable)
6210 encoder->post_pll_disable(encoder);
6211
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006212 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006213 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006214}
6215
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006216static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006217{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006218 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006219 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006220 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006221 enum intel_display_power_domain domain;
6222 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006223
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006224 if (!intel_crtc->active)
6225 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006226
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006227 if (to_intel_plane_state(crtc->primary->state)->visible) {
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006228 WARN_ON(intel_crtc->unpin_work);
6229
Ville Syrjälä2622a082016-03-09 19:07:26 +02006230 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006231
6232 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6233 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006234 }
6235
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006236 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006237
6238 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6239 crtc->base.id);
6240
6241 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6242 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006243 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006244 crtc->enabled = false;
6245 crtc->state->connector_mask = 0;
6246 crtc->state->encoder_mask = 0;
6247
6248 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6249 encoder->base.crtc = NULL;
6250
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006251 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006252 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006253 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006254
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006255 domains = intel_crtc->enabled_power_domains;
6256 for_each_power_domain(domain, domains)
6257 intel_display_power_put(dev_priv, domain);
6258 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006259
6260 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6261 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006262}
6263
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006264/*
6265 * turn all crtc's off, but do not adjust state
6266 * This has to be paired with a call to intel_modeset_setup_hw_state.
6267 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006268int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006269{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006270 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006271 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006272 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006273
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006274 state = drm_atomic_helper_suspend(dev);
6275 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006276 if (ret)
6277 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006278 else
6279 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006280 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006281}
6282
Chris Wilsonea5b2132010-08-04 13:50:23 +01006283void intel_encoder_destroy(struct drm_encoder *encoder)
6284{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006285 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006286
Chris Wilsonea5b2132010-08-04 13:50:23 +01006287 drm_encoder_cleanup(encoder);
6288 kfree(intel_encoder);
6289}
6290
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006291/* Cross check the actual hw state with our own modeset state tracking (and it's
6292 * internal consistency). */
Maarten Lankhorstc0ead702016-03-30 10:00:05 +02006293static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006294{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006295 struct drm_crtc *crtc = connector->base.state->crtc;
6296
6297 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6298 connector->base.base.id,
6299 connector->base.name);
6300
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006301 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006302 struct intel_encoder *encoder = connector->encoder;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006303 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006304
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006305 I915_STATE_WARN(!crtc,
6306 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006307
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006308 if (!crtc)
6309 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006310
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006311 I915_STATE_WARN(!crtc->state->active,
6312 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006313
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006314 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006315 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006316
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006317 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006318 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006319
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006320 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006321 "attached encoder crtc differs from connector crtc\n");
6322 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006323 I915_STATE_WARN(crtc && crtc->state->active,
6324 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006325 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6326 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006327 }
6328}
6329
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006330int intel_connector_init(struct intel_connector *connector)
6331{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006332 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006333
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006334 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006335 return -ENOMEM;
6336
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006337 return 0;
6338}
6339
6340struct intel_connector *intel_connector_alloc(void)
6341{
6342 struct intel_connector *connector;
6343
6344 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6345 if (!connector)
6346 return NULL;
6347
6348 if (intel_connector_init(connector) < 0) {
6349 kfree(connector);
6350 return NULL;
6351 }
6352
6353 return connector;
6354}
6355
Daniel Vetterf0947c32012-07-02 13:10:34 +02006356/* Simple connector->get_hw_state implementation for encoders that support only
6357 * one connector and no cloning and hence the encoder state determines the state
6358 * of the connector. */
6359bool intel_connector_get_hw_state(struct intel_connector *connector)
6360{
Daniel Vetter24929352012-07-02 20:28:59 +02006361 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006362 struct intel_encoder *encoder = connector->encoder;
6363
6364 return encoder->get_hw_state(encoder, &pipe);
6365}
6366
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006367static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006368{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006369 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6370 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006371
6372 return 0;
6373}
6374
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006375static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006376 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006377{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006378 struct drm_atomic_state *state = pipe_config->base.state;
6379 struct intel_crtc *other_crtc;
6380 struct intel_crtc_state *other_crtc_state;
6381
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006382 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6383 pipe_name(pipe), pipe_config->fdi_lanes);
6384 if (pipe_config->fdi_lanes > 4) {
6385 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6386 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006387 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006388 }
6389
Paulo Zanonibafb6552013-11-02 21:07:44 -07006390 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006391 if (pipe_config->fdi_lanes > 2) {
6392 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6393 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006394 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006395 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006396 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006397 }
6398 }
6399
6400 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006401 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006402
6403 /* Ivybridge 3 pipe is really complicated */
6404 switch (pipe) {
6405 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006406 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006407 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006408 if (pipe_config->fdi_lanes <= 2)
6409 return 0;
6410
6411 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6412 other_crtc_state =
6413 intel_atomic_get_crtc_state(state, other_crtc);
6414 if (IS_ERR(other_crtc_state))
6415 return PTR_ERR(other_crtc_state);
6416
6417 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006418 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6419 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006420 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006421 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006422 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006423 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006424 if (pipe_config->fdi_lanes > 2) {
6425 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6426 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006427 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006428 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006429
6430 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6431 other_crtc_state =
6432 intel_atomic_get_crtc_state(state, other_crtc);
6433 if (IS_ERR(other_crtc_state))
6434 return PTR_ERR(other_crtc_state);
6435
6436 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006437 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006438 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006439 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006440 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006441 default:
6442 BUG();
6443 }
6444}
6445
Daniel Vettere29c22c2013-02-21 00:00:16 +01006446#define RETRY 1
6447static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006448 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006449{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006450 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006451 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006452 int lane, link_bw, fdi_dotclock, ret;
6453 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006454
Daniel Vettere29c22c2013-02-21 00:00:16 +01006455retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006456 /* FDI is a binary signal running at ~2.7GHz, encoding
6457 * each output octet as 10 bits. The actual frequency
6458 * is stored as a divider into a 100MHz clock, and the
6459 * mode pixel clock is stored in units of 1KHz.
6460 * Hence the bw of each lane in terms of the mode signal
6461 * is:
6462 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006463 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006464
Damien Lespiau241bfc32013-09-25 16:45:37 +01006465 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006466
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006467 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006468 pipe_config->pipe_bpp);
6469
6470 pipe_config->fdi_lanes = lane;
6471
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006472 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006473 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006474
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006475 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006476 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006477 pipe_config->pipe_bpp -= 2*3;
6478 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6479 pipe_config->pipe_bpp);
6480 needs_recompute = true;
6481 pipe_config->bw_constrained = true;
6482
6483 goto retry;
6484 }
6485
6486 if (needs_recompute)
6487 return RETRY;
6488
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006489 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006490}
6491
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006492static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6493 struct intel_crtc_state *pipe_config)
6494{
6495 if (pipe_config->pipe_bpp > 24)
6496 return false;
6497
6498 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006499 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006500 return true;
6501
6502 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006503 * We compare against max which means we must take
6504 * the increased cdclk requirement into account when
6505 * calculating the new cdclk.
6506 *
6507 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006508 */
6509 return ilk_pipe_pixel_rate(pipe_config) <=
6510 dev_priv->max_cdclk_freq * 95 / 100;
6511}
6512
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006513static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006514 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006515{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006516 struct drm_device *dev = crtc->base.dev;
6517 struct drm_i915_private *dev_priv = dev->dev_private;
6518
Jani Nikulad330a952014-01-21 11:24:25 +02006519 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006520 hsw_crtc_supports_ips(crtc) &&
6521 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006522}
6523
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006524static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6525{
6526 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6527
6528 /* GDG double wide on either pipe, otherwise pipe A only */
6529 return INTEL_INFO(dev_priv)->gen < 4 &&
6530 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6531}
6532
Daniel Vettera43f6e02013-06-07 23:10:32 +02006533static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006534 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006535{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006536 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006537 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006538 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006539
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006540 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006541 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006542 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006543
6544 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006545 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006546 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006547 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006548 if (intel_crtc_supports_double_wide(crtc) &&
6549 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006550 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006551 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006552 }
6553
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006554 if (adjusted_mode->crtc_clock > clock_limit) {
6555 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6556 adjusted_mode->crtc_clock, clock_limit,
6557 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006558 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006559 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006560 }
Chris Wilson89749352010-09-12 18:25:19 +01006561
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006562 /*
6563 * Pipe horizontal size must be even in:
6564 * - DVO ganged mode
6565 * - LVDS dual channel mode
6566 * - Double wide pipe
6567 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006568 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006569 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6570 pipe_config->pipe_src_w &= ~1;
6571
Damien Lespiau8693a822013-05-03 18:48:11 +01006572 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6573 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006574 */
6575 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006576 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006577 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006578
Damien Lespiauf5adf942013-06-24 18:29:34 +01006579 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006580 hsw_compute_ips_config(crtc, pipe_config);
6581
Daniel Vetter877d48d2013-04-19 11:24:43 +02006582 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006583 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006584
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006585 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006586}
6587
Ville Syrjälä1652d192015-03-31 14:12:01 +03006588static int skylake_get_display_clock_speed(struct drm_device *dev)
6589{
6590 struct drm_i915_private *dev_priv = to_i915(dev);
6591 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6592 uint32_t cdctl = I915_READ(CDCLK_CTL);
6593 uint32_t linkrate;
6594
Damien Lespiau414355a2015-06-04 18:21:31 +01006595 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006596 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006597
6598 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6599 return 540000;
6600
6601 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006602 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006603
Damien Lespiau71cd8422015-04-30 16:39:17 +01006604 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6605 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006606 /* vco 8640 */
6607 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6608 case CDCLK_FREQ_450_432:
6609 return 432000;
6610 case CDCLK_FREQ_337_308:
6611 return 308570;
6612 case CDCLK_FREQ_675_617:
6613 return 617140;
6614 default:
6615 WARN(1, "Unknown cd freq selection\n");
6616 }
6617 } else {
6618 /* vco 8100 */
6619 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6620 case CDCLK_FREQ_450_432:
6621 return 450000;
6622 case CDCLK_FREQ_337_308:
6623 return 337500;
6624 case CDCLK_FREQ_675_617:
6625 return 675000;
6626 default:
6627 WARN(1, "Unknown cd freq selection\n");
6628 }
6629 }
6630
6631 /* error case, do as if DPLL0 isn't enabled */
6632 return 24000;
6633}
6634
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006635static int broxton_get_display_clock_speed(struct drm_device *dev)
6636{
6637 struct drm_i915_private *dev_priv = to_i915(dev);
6638 uint32_t cdctl = I915_READ(CDCLK_CTL);
6639 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6640 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6641 int cdclk;
6642
6643 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6644 return 19200;
6645
6646 cdclk = 19200 * pll_ratio / 2;
6647
6648 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6649 case BXT_CDCLK_CD2X_DIV_SEL_1:
6650 return cdclk; /* 576MHz or 624MHz */
6651 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6652 return cdclk * 2 / 3; /* 384MHz */
6653 case BXT_CDCLK_CD2X_DIV_SEL_2:
6654 return cdclk / 2; /* 288MHz */
6655 case BXT_CDCLK_CD2X_DIV_SEL_4:
6656 return cdclk / 4; /* 144MHz */
6657 }
6658
6659 /* error case, do as if DE PLL isn't enabled */
6660 return 19200;
6661}
6662
Ville Syrjälä1652d192015-03-31 14:12:01 +03006663static int broadwell_get_display_clock_speed(struct drm_device *dev)
6664{
6665 struct drm_i915_private *dev_priv = dev->dev_private;
6666 uint32_t lcpll = I915_READ(LCPLL_CTL);
6667 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6668
6669 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6670 return 800000;
6671 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6672 return 450000;
6673 else if (freq == LCPLL_CLK_FREQ_450)
6674 return 450000;
6675 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6676 return 540000;
6677 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6678 return 337500;
6679 else
6680 return 675000;
6681}
6682
6683static int haswell_get_display_clock_speed(struct drm_device *dev)
6684{
6685 struct drm_i915_private *dev_priv = dev->dev_private;
6686 uint32_t lcpll = I915_READ(LCPLL_CTL);
6687 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6688
6689 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6690 return 800000;
6691 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6692 return 450000;
6693 else if (freq == LCPLL_CLK_FREQ_450)
6694 return 450000;
6695 else if (IS_HSW_ULT(dev))
6696 return 337500;
6697 else
6698 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006699}
6700
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006701static int valleyview_get_display_clock_speed(struct drm_device *dev)
6702{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006703 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6704 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006705}
6706
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006707static int ilk_get_display_clock_speed(struct drm_device *dev)
6708{
6709 return 450000;
6710}
6711
Jesse Barnese70236a2009-09-21 10:42:27 -07006712static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006713{
Jesse Barnese70236a2009-09-21 10:42:27 -07006714 return 400000;
6715}
Jesse Barnes79e53942008-11-07 14:24:08 -08006716
Jesse Barnese70236a2009-09-21 10:42:27 -07006717static int i915_get_display_clock_speed(struct drm_device *dev)
6718{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006719 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006720}
Jesse Barnes79e53942008-11-07 14:24:08 -08006721
Jesse Barnese70236a2009-09-21 10:42:27 -07006722static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6723{
6724 return 200000;
6725}
Jesse Barnes79e53942008-11-07 14:24:08 -08006726
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006727static int pnv_get_display_clock_speed(struct drm_device *dev)
6728{
6729 u16 gcfgc = 0;
6730
6731 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6732
6733 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6734 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006735 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006736 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006737 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006738 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006739 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006740 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6741 return 200000;
6742 default:
6743 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6744 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006745 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006746 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006747 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006748 }
6749}
6750
Jesse Barnese70236a2009-09-21 10:42:27 -07006751static int i915gm_get_display_clock_speed(struct drm_device *dev)
6752{
6753 u16 gcfgc = 0;
6754
6755 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6756
6757 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006758 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006759 else {
6760 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6761 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006762 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006763 default:
6764 case GC_DISPLAY_CLOCK_190_200_MHZ:
6765 return 190000;
6766 }
6767 }
6768}
Jesse Barnes79e53942008-11-07 14:24:08 -08006769
Jesse Barnese70236a2009-09-21 10:42:27 -07006770static int i865_get_display_clock_speed(struct drm_device *dev)
6771{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006772 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006773}
6774
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006775static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006776{
6777 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006778
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006779 /*
6780 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6781 * encoding is different :(
6782 * FIXME is this the right way to detect 852GM/852GMV?
6783 */
6784 if (dev->pdev->revision == 0x1)
6785 return 133333;
6786
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006787 pci_bus_read_config_word(dev->pdev->bus,
6788 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6789
Jesse Barnese70236a2009-09-21 10:42:27 -07006790 /* Assume that the hardware is in the high speed state. This
6791 * should be the default.
6792 */
6793 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6794 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006795 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006796 case GC_CLOCK_100_200:
6797 return 200000;
6798 case GC_CLOCK_166_250:
6799 return 250000;
6800 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006801 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006802 case GC_CLOCK_133_266:
6803 case GC_CLOCK_133_266_2:
6804 case GC_CLOCK_166_266:
6805 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006806 }
6807
6808 /* Shouldn't happen */
6809 return 0;
6810}
6811
6812static int i830_get_display_clock_speed(struct drm_device *dev)
6813{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006814 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006815}
6816
Ville Syrjälä34edce22015-05-22 11:22:33 +03006817static unsigned int intel_hpll_vco(struct drm_device *dev)
6818{
6819 struct drm_i915_private *dev_priv = dev->dev_private;
6820 static const unsigned int blb_vco[8] = {
6821 [0] = 3200000,
6822 [1] = 4000000,
6823 [2] = 5333333,
6824 [3] = 4800000,
6825 [4] = 6400000,
6826 };
6827 static const unsigned int pnv_vco[8] = {
6828 [0] = 3200000,
6829 [1] = 4000000,
6830 [2] = 5333333,
6831 [3] = 4800000,
6832 [4] = 2666667,
6833 };
6834 static const unsigned int cl_vco[8] = {
6835 [0] = 3200000,
6836 [1] = 4000000,
6837 [2] = 5333333,
6838 [3] = 6400000,
6839 [4] = 3333333,
6840 [5] = 3566667,
6841 [6] = 4266667,
6842 };
6843 static const unsigned int elk_vco[8] = {
6844 [0] = 3200000,
6845 [1] = 4000000,
6846 [2] = 5333333,
6847 [3] = 4800000,
6848 };
6849 static const unsigned int ctg_vco[8] = {
6850 [0] = 3200000,
6851 [1] = 4000000,
6852 [2] = 5333333,
6853 [3] = 6400000,
6854 [4] = 2666667,
6855 [5] = 4266667,
6856 };
6857 const unsigned int *vco_table;
6858 unsigned int vco;
6859 uint8_t tmp = 0;
6860
6861 /* FIXME other chipsets? */
6862 if (IS_GM45(dev))
6863 vco_table = ctg_vco;
6864 else if (IS_G4X(dev))
6865 vco_table = elk_vco;
6866 else if (IS_CRESTLINE(dev))
6867 vco_table = cl_vco;
6868 else if (IS_PINEVIEW(dev))
6869 vco_table = pnv_vco;
6870 else if (IS_G33(dev))
6871 vco_table = blb_vco;
6872 else
6873 return 0;
6874
6875 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6876
6877 vco = vco_table[tmp & 0x7];
6878 if (vco == 0)
6879 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6880 else
6881 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6882
6883 return vco;
6884}
6885
6886static int gm45_get_display_clock_speed(struct drm_device *dev)
6887{
6888 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6889 uint16_t tmp = 0;
6890
6891 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6892
6893 cdclk_sel = (tmp >> 12) & 0x1;
6894
6895 switch (vco) {
6896 case 2666667:
6897 case 4000000:
6898 case 5333333:
6899 return cdclk_sel ? 333333 : 222222;
6900 case 3200000:
6901 return cdclk_sel ? 320000 : 228571;
6902 default:
6903 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6904 return 222222;
6905 }
6906}
6907
6908static int i965gm_get_display_clock_speed(struct drm_device *dev)
6909{
6910 static const uint8_t div_3200[] = { 16, 10, 8 };
6911 static const uint8_t div_4000[] = { 20, 12, 10 };
6912 static const uint8_t div_5333[] = { 24, 16, 14 };
6913 const uint8_t *div_table;
6914 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6915 uint16_t tmp = 0;
6916
6917 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6918
6919 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6920
6921 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6922 goto fail;
6923
6924 switch (vco) {
6925 case 3200000:
6926 div_table = div_3200;
6927 break;
6928 case 4000000:
6929 div_table = div_4000;
6930 break;
6931 case 5333333:
6932 div_table = div_5333;
6933 break;
6934 default:
6935 goto fail;
6936 }
6937
6938 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6939
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006940fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006941 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6942 return 200000;
6943}
6944
6945static int g33_get_display_clock_speed(struct drm_device *dev)
6946{
6947 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6948 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6949 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6950 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6951 const uint8_t *div_table;
6952 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6953 uint16_t tmp = 0;
6954
6955 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6956
6957 cdclk_sel = (tmp >> 4) & 0x7;
6958
6959 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6960 goto fail;
6961
6962 switch (vco) {
6963 case 3200000:
6964 div_table = div_3200;
6965 break;
6966 case 4000000:
6967 div_table = div_4000;
6968 break;
6969 case 4800000:
6970 div_table = div_4800;
6971 break;
6972 case 5333333:
6973 div_table = div_5333;
6974 break;
6975 default:
6976 goto fail;
6977 }
6978
6979 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6980
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006981fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006982 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6983 return 190476;
6984}
6985
Zhenyu Wang2c072452009-06-05 15:38:42 +08006986static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006987intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006988{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006989 while (*num > DATA_LINK_M_N_MASK ||
6990 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006991 *num >>= 1;
6992 *den >>= 1;
6993 }
6994}
6995
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006996static void compute_m_n(unsigned int m, unsigned int n,
6997 uint32_t *ret_m, uint32_t *ret_n)
6998{
6999 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7000 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7001 intel_reduce_m_n_ratio(ret_m, ret_n);
7002}
7003
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007004void
7005intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7006 int pixel_clock, int link_clock,
7007 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007008{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007009 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007010
7011 compute_m_n(bits_per_pixel * pixel_clock,
7012 link_clock * nlanes * 8,
7013 &m_n->gmch_m, &m_n->gmch_n);
7014
7015 compute_m_n(pixel_clock, link_clock,
7016 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007017}
7018
Chris Wilsona7615032011-01-12 17:04:08 +00007019static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7020{
Jani Nikulad330a952014-01-21 11:24:25 +02007021 if (i915.panel_use_ssc >= 0)
7022 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007023 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007024 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007025}
7026
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007027static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007028{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007029 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007030}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007031
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007032static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7033{
7034 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007035}
7036
Daniel Vetterf47709a2013-03-28 10:42:02 +01007037static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007038 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007039 intel_clock_t *reduced_clock)
7040{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007041 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007042 u32 fp, fp2 = 0;
7043
7044 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007045 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007046 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007047 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007048 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007049 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007050 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007051 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007052 }
7053
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007054 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007055
Daniel Vetterf47709a2013-03-28 10:42:02 +01007056 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007057 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007058 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007059 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007060 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007061 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007062 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007063 }
7064}
7065
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007066static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7067 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007068{
7069 u32 reg_val;
7070
7071 /*
7072 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7073 * and set it to a reasonable value instead.
7074 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007075 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007076 reg_val &= 0xffffff00;
7077 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007078 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007079
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007080 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007081 reg_val &= 0x8cffffff;
7082 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007083 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007084
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007085 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007086 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007087 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007088
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007089 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007090 reg_val &= 0x00ffffff;
7091 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007092 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007093}
7094
Daniel Vetterb5518422013-05-03 11:49:48 +02007095static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7096 struct intel_link_m_n *m_n)
7097{
7098 struct drm_device *dev = crtc->base.dev;
7099 struct drm_i915_private *dev_priv = dev->dev_private;
7100 int pipe = crtc->pipe;
7101
Daniel Vettere3b95f12013-05-03 11:49:49 +02007102 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7103 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7104 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7105 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007106}
7107
7108static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007109 struct intel_link_m_n *m_n,
7110 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007111{
7112 struct drm_device *dev = crtc->base.dev;
7113 struct drm_i915_private *dev_priv = dev->dev_private;
7114 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007115 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007116
7117 if (INTEL_INFO(dev)->gen >= 5) {
7118 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7119 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7120 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7121 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007122 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7123 * for gen < 8) and if DRRS is supported (to make sure the
7124 * registers are not unnecessarily accessed).
7125 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307126 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007127 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007128 I915_WRITE(PIPE_DATA_M2(transcoder),
7129 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7130 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7131 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7132 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7133 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007134 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007135 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7136 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7137 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7138 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007139 }
7140}
7141
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307142void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007143{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307144 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7145
7146 if (m_n == M1_N1) {
7147 dp_m_n = &crtc->config->dp_m_n;
7148 dp_m2_n2 = &crtc->config->dp_m2_n2;
7149 } else if (m_n == M2_N2) {
7150
7151 /*
7152 * M2_N2 registers are not supported. Hence m2_n2 divider value
7153 * needs to be programmed into M1_N1.
7154 */
7155 dp_m_n = &crtc->config->dp_m2_n2;
7156 } else {
7157 DRM_ERROR("Unsupported divider value\n");
7158 return;
7159 }
7160
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007161 if (crtc->config->has_pch_encoder)
7162 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007163 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307164 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007165}
7166
Daniel Vetter251ac862015-06-18 10:30:24 +02007167static void vlv_compute_dpll(struct intel_crtc *crtc,
7168 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007169{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007170 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
7171 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7172 DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV;
7173 if (crtc->pipe != PIPE_A)
7174 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007175
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007176 pipe_config->dpll_hw_state.dpll_md =
7177 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7178}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007179
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007180static void chv_compute_dpll(struct intel_crtc *crtc,
7181 struct intel_crtc_state *pipe_config)
7182{
7183 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7184 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7185 DPLL_VCO_ENABLE;
7186 if (crtc->pipe != PIPE_A)
7187 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7188
7189 pipe_config->dpll_hw_state.dpll_md =
7190 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007191}
7192
Ville Syrjäläd288f652014-10-28 13:20:22 +02007193static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007194 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007195{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007196 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007197 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007198 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007199 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007200 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007201 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007202
Ville Syrjäläa5805162015-05-26 20:42:30 +03007203 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007204
Ville Syrjäläd288f652014-10-28 13:20:22 +02007205 bestn = pipe_config->dpll.n;
7206 bestm1 = pipe_config->dpll.m1;
7207 bestm2 = pipe_config->dpll.m2;
7208 bestp1 = pipe_config->dpll.p1;
7209 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007210
Jesse Barnes89b667f2013-04-18 14:51:36 -07007211 /* See eDP HDMI DPIO driver vbios notes doc */
7212
7213 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007214 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007215 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007216
7217 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007218 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007219
7220 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007221 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007222 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007223 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007224
7225 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007226 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007227
7228 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007229 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7230 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7231 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007232 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007233
7234 /*
7235 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7236 * but we don't support that).
7237 * Note: don't use the DAC post divider as it seems unstable.
7238 */
7239 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007240 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007241
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007242 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007243 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007244
Jesse Barnes89b667f2013-04-18 14:51:36 -07007245 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007246 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007247 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7248 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007249 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007250 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007251 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007252 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007253 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007254
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007255 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007256 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007257 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007258 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007259 0x0df40000);
7260 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007261 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007262 0x0df70000);
7263 } else { /* HDMI or VGA */
7264 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007265 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007266 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007267 0x0df70000);
7268 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007269 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007270 0x0df40000);
7271 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007272
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007273 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007274 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007275 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7276 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007277 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007278 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007279
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007280 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007281 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007282}
7283
Ville Syrjäläd288f652014-10-28 13:20:22 +02007284static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007285 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007286{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007287 struct drm_device *dev = crtc->base.dev;
7288 struct drm_i915_private *dev_priv = dev->dev_private;
7289 int pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02007290 i915_reg_t dpll_reg = DPLL(crtc->pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007291 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307292 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007293 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307294 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307295 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007296
Ville Syrjäläd288f652014-10-28 13:20:22 +02007297 bestn = pipe_config->dpll.n;
7298 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7299 bestm1 = pipe_config->dpll.m1;
7300 bestm2 = pipe_config->dpll.m2 >> 22;
7301 bestp1 = pipe_config->dpll.p1;
7302 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307303 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307304 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307305 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007306
7307 /*
7308 * Enable Refclk and SSC
7309 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007310 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007311 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007312
Ville Syrjäläa5805162015-05-26 20:42:30 +03007313 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007314
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007315 /* p1 and p2 divider */
7316 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7317 5 << DPIO_CHV_S1_DIV_SHIFT |
7318 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7319 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7320 1 << DPIO_CHV_K_DIV_SHIFT);
7321
7322 /* Feedback post-divider - m2 */
7323 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7324
7325 /* Feedback refclk divider - n and m1 */
7326 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7327 DPIO_CHV_M1_DIV_BY_2 |
7328 1 << DPIO_CHV_N_DIV_SHIFT);
7329
7330 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007331 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007332
7333 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307334 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7335 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7336 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7337 if (bestm2_frac)
7338 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7339 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007340
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307341 /* Program digital lock detect threshold */
7342 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7343 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7344 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7345 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7346 if (!bestm2_frac)
7347 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7348 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7349
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007350 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307351 if (vco == 5400000) {
7352 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7353 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7354 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7355 tribuf_calcntr = 0x9;
7356 } else if (vco <= 6200000) {
7357 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7358 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7359 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7360 tribuf_calcntr = 0x9;
7361 } else if (vco <= 6480000) {
7362 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7363 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7364 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7365 tribuf_calcntr = 0x8;
7366 } else {
7367 /* Not supported. Apply the same limits as in the max case */
7368 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7369 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7370 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7371 tribuf_calcntr = 0;
7372 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007373 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7374
Ville Syrjälä968040b2015-03-11 22:52:08 +02007375 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307376 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7377 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7378 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7379
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007380 /* AFC Recal */
7381 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7382 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7383 DPIO_AFC_RECAL);
7384
Ville Syrjäläa5805162015-05-26 20:42:30 +03007385 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007386}
7387
Ville Syrjäläd288f652014-10-28 13:20:22 +02007388/**
7389 * vlv_force_pll_on - forcibly enable just the PLL
7390 * @dev_priv: i915 private structure
7391 * @pipe: pipe PLL to enable
7392 * @dpll: PLL configuration
7393 *
7394 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7395 * in cases where we need the PLL enabled even when @pipe is not going to
7396 * be enabled.
7397 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007398int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7399 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007400{
7401 struct intel_crtc *crtc =
7402 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007403 struct intel_crtc_state *pipe_config;
7404
7405 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7406 if (!pipe_config)
7407 return -ENOMEM;
7408
7409 pipe_config->base.crtc = &crtc->base;
7410 pipe_config->pixel_multiplier = 1;
7411 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007412
7413 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007414 chv_compute_dpll(crtc, pipe_config);
7415 chv_prepare_pll(crtc, pipe_config);
7416 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007417 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007418 vlv_compute_dpll(crtc, pipe_config);
7419 vlv_prepare_pll(crtc, pipe_config);
7420 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007421 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007422
7423 kfree(pipe_config);
7424
7425 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007426}
7427
7428/**
7429 * vlv_force_pll_off - forcibly disable just the PLL
7430 * @dev_priv: i915 private structure
7431 * @pipe: pipe PLL to disable
7432 *
7433 * Disable the PLL for @pipe. To be used in cases where we need
7434 * the PLL enabled even when @pipe is not going to be enabled.
7435 */
7436void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7437{
7438 if (IS_CHERRYVIEW(dev))
7439 chv_disable_pll(to_i915(dev), pipe);
7440 else
7441 vlv_disable_pll(to_i915(dev), pipe);
7442}
7443
Daniel Vetter251ac862015-06-18 10:30:24 +02007444static void i9xx_compute_dpll(struct intel_crtc *crtc,
7445 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007446 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007447{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007448 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007449 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007450 u32 dpll;
7451 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007452 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007453
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007454 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307455
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007456 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7457 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007458
7459 dpll = DPLL_VGA_MODE_DIS;
7460
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007461 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007462 dpll |= DPLLB_MODE_LVDS;
7463 else
7464 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007465
Daniel Vetteref1b4602013-06-01 17:17:04 +02007466 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007467 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007468 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007469 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007470
7471 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007472 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007473
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007474 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007475 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007476
7477 /* compute bitmask from p1 value */
7478 if (IS_PINEVIEW(dev))
7479 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7480 else {
7481 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7482 if (IS_G4X(dev) && reduced_clock)
7483 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7484 }
7485 switch (clock->p2) {
7486 case 5:
7487 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7488 break;
7489 case 7:
7490 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7491 break;
7492 case 10:
7493 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7494 break;
7495 case 14:
7496 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7497 break;
7498 }
7499 if (INTEL_INFO(dev)->gen >= 4)
7500 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7501
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007502 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007503 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007504 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007505 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007506 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7507 else
7508 dpll |= PLL_REF_INPUT_DREFCLK;
7509
7510 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007511 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007512
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007513 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007514 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007515 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007516 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007517 }
7518}
7519
Daniel Vetter251ac862015-06-18 10:30:24 +02007520static void i8xx_compute_dpll(struct intel_crtc *crtc,
7521 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007522 intel_clock_t *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007523{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007524 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007525 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007526 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007527 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007528
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007529 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307530
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007531 dpll = DPLL_VGA_MODE_DIS;
7532
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007533 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007534 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7535 } else {
7536 if (clock->p1 == 2)
7537 dpll |= PLL_P1_DIVIDE_BY_TWO;
7538 else
7539 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7540 if (clock->p2 == 4)
7541 dpll |= PLL_P2_DIVIDE_BY_4;
7542 }
7543
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007544 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007545 dpll |= DPLL_DVO_2X_MODE;
7546
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007547 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007548 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007549 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7550 else
7551 dpll |= PLL_REF_INPUT_DREFCLK;
7552
7553 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007554 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007555}
7556
Daniel Vetter8a654f32013-06-01 17:16:22 +02007557static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007558{
7559 struct drm_device *dev = intel_crtc->base.dev;
7560 struct drm_i915_private *dev_priv = dev->dev_private;
7561 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007562 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007563 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007564 uint32_t crtc_vtotal, crtc_vblank_end;
7565 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007566
7567 /* We need to be careful not to changed the adjusted mode, for otherwise
7568 * the hw state checker will get angry at the mismatch. */
7569 crtc_vtotal = adjusted_mode->crtc_vtotal;
7570 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007571
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007572 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007573 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007574 crtc_vtotal -= 1;
7575 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007576
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007577 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007578 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7579 else
7580 vsyncshift = adjusted_mode->crtc_hsync_start -
7581 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007582 if (vsyncshift < 0)
7583 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007584 }
7585
7586 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007587 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007588
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007589 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007590 (adjusted_mode->crtc_hdisplay - 1) |
7591 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007592 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007593 (adjusted_mode->crtc_hblank_start - 1) |
7594 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007595 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007596 (adjusted_mode->crtc_hsync_start - 1) |
7597 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7598
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007599 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007600 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007601 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007602 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007603 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007604 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007605 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007606 (adjusted_mode->crtc_vsync_start - 1) |
7607 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7608
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007609 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7610 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7611 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7612 * bits. */
7613 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7614 (pipe == PIPE_B || pipe == PIPE_C))
7615 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7616
Jani Nikulabc58be62016-03-18 17:05:39 +02007617}
7618
7619static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7620{
7621 struct drm_device *dev = intel_crtc->base.dev;
7622 struct drm_i915_private *dev_priv = dev->dev_private;
7623 enum pipe pipe = intel_crtc->pipe;
7624
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007625 /* pipesrc controls the size that is scaled from, which should
7626 * always be the user's requested size.
7627 */
7628 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007629 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7630 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007631}
7632
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007633static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007634 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007635{
7636 struct drm_device *dev = crtc->base.dev;
7637 struct drm_i915_private *dev_priv = dev->dev_private;
7638 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7639 uint32_t tmp;
7640
7641 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007642 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7643 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007644 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007645 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7646 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007647 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007648 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7649 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007650
7651 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007652 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7653 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007654 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007655 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7656 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007657 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007658 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7659 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007660
7661 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007662 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7663 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7664 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007665 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007666}
7667
7668static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7669 struct intel_crtc_state *pipe_config)
7670{
7671 struct drm_device *dev = crtc->base.dev;
7672 struct drm_i915_private *dev_priv = dev->dev_private;
7673 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007674
7675 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007676 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7677 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7678
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007679 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7680 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007681}
7682
Daniel Vetterf6a83282014-02-11 15:28:57 -08007683void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007684 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007685{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007686 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7687 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7688 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7689 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007690
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007691 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7692 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7693 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7694 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007695
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007696 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007697 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007698
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007699 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7700 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007701
7702 mode->hsync = drm_mode_hsync(mode);
7703 mode->vrefresh = drm_mode_vrefresh(mode);
7704 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007705}
7706
Daniel Vetter84b046f2013-02-19 18:48:54 +01007707static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7708{
7709 struct drm_device *dev = intel_crtc->base.dev;
7710 struct drm_i915_private *dev_priv = dev->dev_private;
7711 uint32_t pipeconf;
7712
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007713 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007714
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007715 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7716 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7717 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007718
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007719 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007720 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007721
Daniel Vetterff9ce462013-04-24 14:57:17 +02007722 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007723 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007724 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007725 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007726 pipeconf |= PIPECONF_DITHER_EN |
7727 PIPECONF_DITHER_TYPE_SP;
7728
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007729 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007730 case 18:
7731 pipeconf |= PIPECONF_6BPC;
7732 break;
7733 case 24:
7734 pipeconf |= PIPECONF_8BPC;
7735 break;
7736 case 30:
7737 pipeconf |= PIPECONF_10BPC;
7738 break;
7739 default:
7740 /* Case prevented by intel_choose_pipe_bpp_dither. */
7741 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007742 }
7743 }
7744
7745 if (HAS_PIPE_CXSR(dev)) {
7746 if (intel_crtc->lowfreq_avail) {
7747 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7748 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7749 } else {
7750 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007751 }
7752 }
7753
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007754 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007755 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007756 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007757 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7758 else
7759 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7760 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007761 pipeconf |= PIPECONF_PROGRESSIVE;
7762
Wayne Boyer666a4532015-12-09 12:29:35 -08007763 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7764 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007765 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007766
Daniel Vetter84b046f2013-02-19 18:48:54 +01007767 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7768 POSTING_READ(PIPECONF(intel_crtc->pipe));
7769}
7770
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007771static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7772 struct intel_crtc_state *crtc_state)
7773{
7774 struct drm_device *dev = crtc->base.dev;
7775 struct drm_i915_private *dev_priv = dev->dev_private;
7776 const intel_limit_t *limit;
7777 int refclk = 48000;
7778
7779 memset(&crtc_state->dpll_hw_state, 0,
7780 sizeof(crtc_state->dpll_hw_state));
7781
7782 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7783 if (intel_panel_use_ssc(dev_priv)) {
7784 refclk = dev_priv->vbt.lvds_ssc_freq;
7785 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7786 }
7787
7788 limit = &intel_limits_i8xx_lvds;
7789 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7790 limit = &intel_limits_i8xx_dvo;
7791 } else {
7792 limit = &intel_limits_i8xx_dac;
7793 }
7794
7795 if (!crtc_state->clock_set &&
7796 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7797 refclk, NULL, &crtc_state->dpll)) {
7798 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7799 return -EINVAL;
7800 }
7801
7802 i8xx_compute_dpll(crtc, crtc_state, NULL);
7803
7804 return 0;
7805}
7806
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007807static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7808 struct intel_crtc_state *crtc_state)
7809{
7810 struct drm_device *dev = crtc->base.dev;
7811 struct drm_i915_private *dev_priv = dev->dev_private;
7812 const intel_limit_t *limit;
7813 int refclk = 96000;
7814
7815 memset(&crtc_state->dpll_hw_state, 0,
7816 sizeof(crtc_state->dpll_hw_state));
7817
7818 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7819 if (intel_panel_use_ssc(dev_priv)) {
7820 refclk = dev_priv->vbt.lvds_ssc_freq;
7821 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7822 }
7823
7824 if (intel_is_dual_link_lvds(dev))
7825 limit = &intel_limits_g4x_dual_channel_lvds;
7826 else
7827 limit = &intel_limits_g4x_single_channel_lvds;
7828 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7829 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7830 limit = &intel_limits_g4x_hdmi;
7831 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7832 limit = &intel_limits_g4x_sdvo;
7833 } else {
7834 /* The option is for other outputs */
7835 limit = &intel_limits_i9xx_sdvo;
7836 }
7837
7838 if (!crtc_state->clock_set &&
7839 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7840 refclk, NULL, &crtc_state->dpll)) {
7841 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7842 return -EINVAL;
7843 }
7844
7845 i9xx_compute_dpll(crtc, crtc_state, NULL);
7846
7847 return 0;
7848}
7849
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007850static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7851 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007852{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007853 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007854 struct drm_i915_private *dev_priv = dev->dev_private;
Ma Lingd4906092009-03-18 20:13:27 +08007855 const intel_limit_t *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007856 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007857
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007858 memset(&crtc_state->dpll_hw_state, 0,
7859 sizeof(crtc_state->dpll_hw_state));
7860
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007861 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7862 if (intel_panel_use_ssc(dev_priv)) {
7863 refclk = dev_priv->vbt.lvds_ssc_freq;
7864 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7865 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007866
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007867 limit = &intel_limits_pineview_lvds;
7868 } else {
7869 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007870 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007871
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007872 if (!crtc_state->clock_set &&
7873 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7874 refclk, NULL, &crtc_state->dpll)) {
7875 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7876 return -EINVAL;
7877 }
7878
7879 i9xx_compute_dpll(crtc, crtc_state, NULL);
7880
7881 return 0;
7882}
7883
7884static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7885 struct intel_crtc_state *crtc_state)
7886{
7887 struct drm_device *dev = crtc->base.dev;
7888 struct drm_i915_private *dev_priv = dev->dev_private;
7889 const intel_limit_t *limit;
7890 int refclk = 96000;
7891
7892 memset(&crtc_state->dpll_hw_state, 0,
7893 sizeof(crtc_state->dpll_hw_state));
7894
7895 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7896 if (intel_panel_use_ssc(dev_priv)) {
7897 refclk = dev_priv->vbt.lvds_ssc_freq;
7898 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007899 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007900
7901 limit = &intel_limits_i9xx_lvds;
7902 } else {
7903 limit = &intel_limits_i9xx_sdvo;
7904 }
7905
7906 if (!crtc_state->clock_set &&
7907 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7908 refclk, NULL, &crtc_state->dpll)) {
7909 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7910 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007911 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007912
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007913 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007914
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007915 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007916}
7917
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007918static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7919 struct intel_crtc_state *crtc_state)
7920{
7921 int refclk = 100000;
7922 const intel_limit_t *limit = &intel_limits_chv;
7923
7924 memset(&crtc_state->dpll_hw_state, 0,
7925 sizeof(crtc_state->dpll_hw_state));
7926
7927 if (crtc_state->has_dsi_encoder)
7928 return 0;
7929
7930 if (!crtc_state->clock_set &&
7931 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7932 refclk, NULL, &crtc_state->dpll)) {
7933 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7934 return -EINVAL;
7935 }
7936
7937 chv_compute_dpll(crtc, crtc_state);
7938
7939 return 0;
7940}
7941
7942static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
7943 struct intel_crtc_state *crtc_state)
7944{
7945 int refclk = 100000;
7946 const intel_limit_t *limit = &intel_limits_vlv;
7947
7948 memset(&crtc_state->dpll_hw_state, 0,
7949 sizeof(crtc_state->dpll_hw_state));
7950
7951 if (crtc_state->has_dsi_encoder)
7952 return 0;
7953
7954 if (!crtc_state->clock_set &&
7955 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7956 refclk, NULL, &crtc_state->dpll)) {
7957 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7958 return -EINVAL;
7959 }
7960
7961 vlv_compute_dpll(crtc, crtc_state);
7962
7963 return 0;
7964}
7965
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007966static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007967 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007968{
7969 struct drm_device *dev = crtc->base.dev;
7970 struct drm_i915_private *dev_priv = dev->dev_private;
7971 uint32_t tmp;
7972
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007973 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7974 return;
7975
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007976 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007977 if (!(tmp & PFIT_ENABLE))
7978 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007979
Daniel Vetter06922822013-07-11 13:35:40 +02007980 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007981 if (INTEL_INFO(dev)->gen < 4) {
7982 if (crtc->pipe != PIPE_B)
7983 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007984 } else {
7985 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7986 return;
7987 }
7988
Daniel Vetter06922822013-07-11 13:35:40 +02007989 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007990 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7991 if (INTEL_INFO(dev)->gen < 5)
7992 pipe_config->gmch_pfit.lvds_border_bits =
7993 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7994}
7995
Jesse Barnesacbec812013-09-20 11:29:32 -07007996static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007997 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007998{
7999 struct drm_device *dev = crtc->base.dev;
8000 struct drm_i915_private *dev_priv = dev->dev_private;
8001 int pipe = pipe_config->cpu_transcoder;
8002 intel_clock_t clock;
8003 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008004 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008005
Ville Syrjäläb5219732016-03-15 16:40:01 +02008006 /* In case of DSI, DPLL will not be used */
8007 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308008 return;
8009
Ville Syrjäläa5805162015-05-26 20:42:30 +03008010 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008011 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008012 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008013
8014 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8015 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8016 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8017 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8018 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8019
Imre Deakdccbea32015-06-22 23:35:51 +03008020 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008021}
8022
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008023static void
8024i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8025 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008026{
8027 struct drm_device *dev = crtc->base.dev;
8028 struct drm_i915_private *dev_priv = dev->dev_private;
8029 u32 val, base, offset;
8030 int pipe = crtc->pipe, plane = crtc->plane;
8031 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008032 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008033 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008034 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008035
Damien Lespiau42a7b082015-02-05 19:35:13 +00008036 val = I915_READ(DSPCNTR(plane));
8037 if (!(val & DISPLAY_PLANE_ENABLE))
8038 return;
8039
Damien Lespiaud9806c92015-01-21 14:07:19 +00008040 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008041 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008042 DRM_DEBUG_KMS("failed to alloc fb\n");
8043 return;
8044 }
8045
Damien Lespiau1b842c82015-01-21 13:50:54 +00008046 fb = &intel_fb->base;
8047
Daniel Vetter18c52472015-02-10 17:16:09 +00008048 if (INTEL_INFO(dev)->gen >= 4) {
8049 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008050 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008051 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8052 }
8053 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008054
8055 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008056 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008057 fb->pixel_format = fourcc;
8058 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008059
8060 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008061 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008062 offset = I915_READ(DSPTILEOFF(plane));
8063 else
8064 offset = I915_READ(DSPLINOFF(plane));
8065 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8066 } else {
8067 base = I915_READ(DSPADDR(plane));
8068 }
8069 plane_config->base = base;
8070
8071 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008072 fb->width = ((val >> 16) & 0xfff) + 1;
8073 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008074
8075 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008076 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008077
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008078 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008079 fb->pixel_format,
8080 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008081
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008082 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008083
Damien Lespiau2844a922015-01-20 12:51:48 +00008084 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8085 pipe_name(pipe), plane, fb->width, fb->height,
8086 fb->bits_per_pixel, base, fb->pitches[0],
8087 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008088
Damien Lespiau2d140302015-02-05 17:22:18 +00008089 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008090}
8091
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008092static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008093 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008094{
8095 struct drm_device *dev = crtc->base.dev;
8096 struct drm_i915_private *dev_priv = dev->dev_private;
8097 int pipe = pipe_config->cpu_transcoder;
8098 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8099 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008100 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008101 int refclk = 100000;
8102
Ville Syrjäläb5219732016-03-15 16:40:01 +02008103 /* In case of DSI, DPLL will not be used */
8104 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8105 return;
8106
Ville Syrjäläa5805162015-05-26 20:42:30 +03008107 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008108 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8109 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8110 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8111 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008112 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008113 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008114
8115 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008116 clock.m2 = (pll_dw0 & 0xff) << 22;
8117 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8118 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008119 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8120 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8121 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8122
Imre Deakdccbea32015-06-22 23:35:51 +03008123 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008124}
8125
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008126static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008127 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008128{
8129 struct drm_device *dev = crtc->base.dev;
8130 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008131 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008132 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008133 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008134
Imre Deak17290502016-02-12 18:55:11 +02008135 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8136 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008137 return false;
8138
Daniel Vettere143a212013-07-04 12:01:15 +02008139 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008140 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008141
Imre Deak17290502016-02-12 18:55:11 +02008142 ret = false;
8143
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008144 tmp = I915_READ(PIPECONF(crtc->pipe));
8145 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008146 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008147
Wayne Boyer666a4532015-12-09 12:29:35 -08008148 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008149 switch (tmp & PIPECONF_BPC_MASK) {
8150 case PIPECONF_6BPC:
8151 pipe_config->pipe_bpp = 18;
8152 break;
8153 case PIPECONF_8BPC:
8154 pipe_config->pipe_bpp = 24;
8155 break;
8156 case PIPECONF_10BPC:
8157 pipe_config->pipe_bpp = 30;
8158 break;
8159 default:
8160 break;
8161 }
8162 }
8163
Wayne Boyer666a4532015-12-09 12:29:35 -08008164 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8165 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008166 pipe_config->limited_color_range = true;
8167
Ville Syrjälä282740f2013-09-04 18:30:03 +03008168 if (INTEL_INFO(dev)->gen < 4)
8169 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8170
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008171 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008172 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008173
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008174 i9xx_get_pfit_config(crtc, pipe_config);
8175
Daniel Vetter6c49f242013-06-06 12:45:25 +02008176 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008177 /* No way to read it out on pipes B and C */
8178 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8179 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8180 else
8181 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008182 pipe_config->pixel_multiplier =
8183 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8184 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008185 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008186 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8187 tmp = I915_READ(DPLL(crtc->pipe));
8188 pipe_config->pixel_multiplier =
8189 ((tmp & SDVO_MULTIPLIER_MASK)
8190 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8191 } else {
8192 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8193 * port and will be fixed up in the encoder->get_config
8194 * function. */
8195 pipe_config->pixel_multiplier = 1;
8196 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008197 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008198 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008199 /*
8200 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8201 * on 830. Filter it out here so that we don't
8202 * report errors due to that.
8203 */
8204 if (IS_I830(dev))
8205 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8206
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008207 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8208 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008209 } else {
8210 /* Mask out read-only status bits. */
8211 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8212 DPLL_PORTC_READY_MASK |
8213 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008214 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008215
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008216 if (IS_CHERRYVIEW(dev))
8217 chv_crtc_clock_get(crtc, pipe_config);
8218 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008219 vlv_crtc_clock_get(crtc, pipe_config);
8220 else
8221 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008222
Ville Syrjälä0f646142015-08-26 19:39:18 +03008223 /*
8224 * Normally the dotclock is filled in by the encoder .get_config()
8225 * but in case the pipe is enabled w/o any ports we need a sane
8226 * default.
8227 */
8228 pipe_config->base.adjusted_mode.crtc_clock =
8229 pipe_config->port_clock / pipe_config->pixel_multiplier;
8230
Imre Deak17290502016-02-12 18:55:11 +02008231 ret = true;
8232
8233out:
8234 intel_display_power_put(dev_priv, power_domain);
8235
8236 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008237}
8238
Paulo Zanonidde86e22012-12-01 12:04:25 -02008239static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008240{
8241 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008242 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008243 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008244 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008245 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008246 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008247 bool has_ck505 = false;
8248 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008249
8250 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008251 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008252 switch (encoder->type) {
8253 case INTEL_OUTPUT_LVDS:
8254 has_panel = true;
8255 has_lvds = true;
8256 break;
8257 case INTEL_OUTPUT_EDP:
8258 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008259 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008260 has_cpu_edp = true;
8261 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008262 default:
8263 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008264 }
8265 }
8266
Keith Packard99eb6a02011-09-26 14:29:12 -07008267 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008268 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008269 can_ssc = has_ck505;
8270 } else {
8271 has_ck505 = false;
8272 can_ssc = true;
8273 }
8274
Imre Deak2de69052013-05-08 13:14:04 +03008275 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8276 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008277
8278 /* Ironlake: try to setup display ref clock before DPLL
8279 * enabling. This is only under driver's control after
8280 * PCH B stepping, previous chipset stepping should be
8281 * ignoring this setting.
8282 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008283 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008284
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008285 /* As we must carefully and slowly disable/enable each source in turn,
8286 * compute the final state we want first and check if we need to
8287 * make any changes at all.
8288 */
8289 final = val;
8290 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008291 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008292 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008293 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008294 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8295
8296 final &= ~DREF_SSC_SOURCE_MASK;
8297 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8298 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008299
Keith Packard199e5d72011-09-22 12:01:57 -07008300 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008301 final |= DREF_SSC_SOURCE_ENABLE;
8302
8303 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8304 final |= DREF_SSC1_ENABLE;
8305
8306 if (has_cpu_edp) {
8307 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8308 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8309 else
8310 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8311 } else
8312 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8313 } else {
8314 final |= DREF_SSC_SOURCE_DISABLE;
8315 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8316 }
8317
8318 if (final == val)
8319 return;
8320
8321 /* Always enable nonspread source */
8322 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8323
8324 if (has_ck505)
8325 val |= DREF_NONSPREAD_CK505_ENABLE;
8326 else
8327 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8328
8329 if (has_panel) {
8330 val &= ~DREF_SSC_SOURCE_MASK;
8331 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008332
Keith Packard199e5d72011-09-22 12:01:57 -07008333 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008334 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008335 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008336 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008337 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008338 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008339
8340 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008341 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008342 POSTING_READ(PCH_DREF_CONTROL);
8343 udelay(200);
8344
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008345 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008346
8347 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008348 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008349 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008350 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008351 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008352 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008353 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008354 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008355 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008356
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008357 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008358 POSTING_READ(PCH_DREF_CONTROL);
8359 udelay(200);
8360 } else {
8361 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8362
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008363 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008364
8365 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008366 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008367
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008368 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008369 POSTING_READ(PCH_DREF_CONTROL);
8370 udelay(200);
8371
8372 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008373 val &= ~DREF_SSC_SOURCE_MASK;
8374 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008375
8376 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008377 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008378
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008379 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008380 POSTING_READ(PCH_DREF_CONTROL);
8381 udelay(200);
8382 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008383
8384 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008385}
8386
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008387static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008388{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008389 uint32_t tmp;
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, 100))
8397 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008398
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008399 tmp = I915_READ(SOUTH_CHICKEN2);
8400 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8401 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008402
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008403 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8404 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8405 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008406}
8407
8408/* WaMPhyProgramming:hsw */
8409static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8410{
8411 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008412
8413 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8414 tmp &= ~(0xFF << 24);
8415 tmp |= (0x12 << 24);
8416 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8417
Paulo Zanonidde86e22012-12-01 12:04:25 -02008418 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8419 tmp |= (1 << 11);
8420 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8421
8422 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8423 tmp |= (1 << 11);
8424 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8425
Paulo Zanonidde86e22012-12-01 12:04:25 -02008426 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8427 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8428 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8429
8430 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8431 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8432 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8433
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008434 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8435 tmp &= ~(7 << 13);
8436 tmp |= (5 << 13);
8437 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008438
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008439 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8440 tmp &= ~(7 << 13);
8441 tmp |= (5 << 13);
8442 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008443
8444 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8445 tmp &= ~0xFF;
8446 tmp |= 0x1C;
8447 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8448
8449 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8450 tmp &= ~0xFF;
8451 tmp |= 0x1C;
8452 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8453
8454 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8455 tmp &= ~(0xFF << 16);
8456 tmp |= (0x1C << 16);
8457 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8458
8459 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8460 tmp &= ~(0xFF << 16);
8461 tmp |= (0x1C << 16);
8462 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8463
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008464 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8465 tmp |= (1 << 27);
8466 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008467
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008468 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8469 tmp |= (1 << 27);
8470 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008471
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008472 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8473 tmp &= ~(0xF << 28);
8474 tmp |= (4 << 28);
8475 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008476
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008477 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8478 tmp &= ~(0xF << 28);
8479 tmp |= (4 << 28);
8480 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008481}
8482
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008483/* Implements 3 different sequences from BSpec chapter "Display iCLK
8484 * Programming" based on the parameters passed:
8485 * - Sequence to enable CLKOUT_DP
8486 * - Sequence to enable CLKOUT_DP without spread
8487 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8488 */
8489static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8490 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008491{
8492 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008493 uint32_t reg, tmp;
8494
8495 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8496 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008497 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008498 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008499
Ville Syrjäläa5805162015-05-26 20:42:30 +03008500 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008501
8502 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8503 tmp &= ~SBI_SSCCTL_DISABLE;
8504 tmp |= SBI_SSCCTL_PATHALT;
8505 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8506
8507 udelay(24);
8508
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008509 if (with_spread) {
8510 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8511 tmp &= ~SBI_SSCCTL_PATHALT;
8512 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008513
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008514 if (with_fdi) {
8515 lpt_reset_fdi_mphy(dev_priv);
8516 lpt_program_fdi_mphy(dev_priv);
8517 }
8518 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008519
Ville Syrjäläc2699522015-08-27 23:55:59 +03008520 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008521 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8522 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8523 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008524
Ville Syrjäläa5805162015-05-26 20:42:30 +03008525 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008526}
8527
Paulo Zanoni47701c32013-07-23 11:19:25 -03008528/* Sequence to disable CLKOUT_DP */
8529static void lpt_disable_clkout_dp(struct drm_device *dev)
8530{
8531 struct drm_i915_private *dev_priv = dev->dev_private;
8532 uint32_t reg, tmp;
8533
Ville Syrjäläa5805162015-05-26 20:42:30 +03008534 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008535
Ville Syrjäläc2699522015-08-27 23:55:59 +03008536 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008537 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8538 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8539 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8540
8541 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8542 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8543 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8544 tmp |= SBI_SSCCTL_PATHALT;
8545 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8546 udelay(32);
8547 }
8548 tmp |= SBI_SSCCTL_DISABLE;
8549 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8550 }
8551
Ville Syrjäläa5805162015-05-26 20:42:30 +03008552 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008553}
8554
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008555#define BEND_IDX(steps) ((50 + (steps)) / 5)
8556
8557static const uint16_t sscdivintphase[] = {
8558 [BEND_IDX( 50)] = 0x3B23,
8559 [BEND_IDX( 45)] = 0x3B23,
8560 [BEND_IDX( 40)] = 0x3C23,
8561 [BEND_IDX( 35)] = 0x3C23,
8562 [BEND_IDX( 30)] = 0x3D23,
8563 [BEND_IDX( 25)] = 0x3D23,
8564 [BEND_IDX( 20)] = 0x3E23,
8565 [BEND_IDX( 15)] = 0x3E23,
8566 [BEND_IDX( 10)] = 0x3F23,
8567 [BEND_IDX( 5)] = 0x3F23,
8568 [BEND_IDX( 0)] = 0x0025,
8569 [BEND_IDX( -5)] = 0x0025,
8570 [BEND_IDX(-10)] = 0x0125,
8571 [BEND_IDX(-15)] = 0x0125,
8572 [BEND_IDX(-20)] = 0x0225,
8573 [BEND_IDX(-25)] = 0x0225,
8574 [BEND_IDX(-30)] = 0x0325,
8575 [BEND_IDX(-35)] = 0x0325,
8576 [BEND_IDX(-40)] = 0x0425,
8577 [BEND_IDX(-45)] = 0x0425,
8578 [BEND_IDX(-50)] = 0x0525,
8579};
8580
8581/*
8582 * Bend CLKOUT_DP
8583 * steps -50 to 50 inclusive, in steps of 5
8584 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8585 * change in clock period = -(steps / 10) * 5.787 ps
8586 */
8587static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8588{
8589 uint32_t tmp;
8590 int idx = BEND_IDX(steps);
8591
8592 if (WARN_ON(steps % 5 != 0))
8593 return;
8594
8595 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8596 return;
8597
8598 mutex_lock(&dev_priv->sb_lock);
8599
8600 if (steps % 10 != 0)
8601 tmp = 0xAAAAAAAB;
8602 else
8603 tmp = 0x00000000;
8604 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8605
8606 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8607 tmp &= 0xffff0000;
8608 tmp |= sscdivintphase[idx];
8609 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8610
8611 mutex_unlock(&dev_priv->sb_lock);
8612}
8613
8614#undef BEND_IDX
8615
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008616static void lpt_init_pch_refclk(struct drm_device *dev)
8617{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008618 struct intel_encoder *encoder;
8619 bool has_vga = false;
8620
Damien Lespiaub2784e12014-08-05 11:29:37 +01008621 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008622 switch (encoder->type) {
8623 case INTEL_OUTPUT_ANALOG:
8624 has_vga = true;
8625 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008626 default:
8627 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008628 }
8629 }
8630
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008631 if (has_vga) {
8632 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008633 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008634 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008635 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008636 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008637}
8638
Paulo Zanonidde86e22012-12-01 12:04:25 -02008639/*
8640 * Initialize reference clocks when the driver loads
8641 */
8642void intel_init_pch_refclk(struct drm_device *dev)
8643{
8644 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8645 ironlake_init_pch_refclk(dev);
8646 else if (HAS_PCH_LPT(dev))
8647 lpt_init_pch_refclk(dev);
8648}
8649
Daniel Vetter6ff93602013-04-19 11:24:36 +02008650static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008651{
8652 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8653 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8654 int pipe = intel_crtc->pipe;
8655 uint32_t val;
8656
Daniel Vetter78114072013-06-13 00:54:57 +02008657 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008658
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008659 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008660 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008661 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008662 break;
8663 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008664 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008665 break;
8666 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008667 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008668 break;
8669 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008670 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008671 break;
8672 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008673 /* Case prevented by intel_choose_pipe_bpp_dither. */
8674 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008675 }
8676
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008677 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008678 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8679
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008680 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008681 val |= PIPECONF_INTERLACED_ILK;
8682 else
8683 val |= PIPECONF_PROGRESSIVE;
8684
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008685 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008686 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008687
Paulo Zanonic8203562012-09-12 10:06:29 -03008688 I915_WRITE(PIPECONF(pipe), val);
8689 POSTING_READ(PIPECONF(pipe));
8690}
8691
Daniel Vetter6ff93602013-04-19 11:24:36 +02008692static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008693{
Jani Nikula391bf042016-03-18 17:05:40 +02008694 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008695 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008696 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008697 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008698
Jani Nikula391bf042016-03-18 17:05:40 +02008699 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008700 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8701
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008702 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008703 val |= PIPECONF_INTERLACED_ILK;
8704 else
8705 val |= PIPECONF_PROGRESSIVE;
8706
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008707 I915_WRITE(PIPECONF(cpu_transcoder), val);
8708 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008709}
8710
Jani Nikula391bf042016-03-18 17:05:40 +02008711static void haswell_set_pipemisc(struct drm_crtc *crtc)
8712{
8713 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8714 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8715
8716 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8717 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008718
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008719 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008720 case 18:
8721 val |= PIPEMISC_DITHER_6_BPC;
8722 break;
8723 case 24:
8724 val |= PIPEMISC_DITHER_8_BPC;
8725 break;
8726 case 30:
8727 val |= PIPEMISC_DITHER_10_BPC;
8728 break;
8729 case 36:
8730 val |= PIPEMISC_DITHER_12_BPC;
8731 break;
8732 default:
8733 /* Case prevented by pipe_config_set_bpp. */
8734 BUG();
8735 }
8736
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008737 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008738 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8739
Jani Nikula391bf042016-03-18 17:05:40 +02008740 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008741 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008742}
8743
Paulo Zanonid4b19312012-11-29 11:29:32 -02008744int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8745{
8746 /*
8747 * Account for spread spectrum to avoid
8748 * oversubscribing the link. Max center spread
8749 * is 2.5%; use 5% for safety's sake.
8750 */
8751 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008752 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008753}
8754
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008755static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008756{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008757 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008758}
8759
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008760static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8761 struct intel_crtc_state *crtc_state,
8762 intel_clock_t *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008763{
8764 struct drm_crtc *crtc = &intel_crtc->base;
8765 struct drm_device *dev = crtc->dev;
8766 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008767 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008768 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008769 struct drm_connector_state *connector_state;
8770 struct intel_encoder *encoder;
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008771 u32 dpll, fp, fp2;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008772 int factor, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008773 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008774
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008775 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008776 if (connector_state->crtc != crtc_state->base.crtc)
8777 continue;
8778
8779 encoder = to_intel_encoder(connector_state->best_encoder);
8780
8781 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008782 case INTEL_OUTPUT_LVDS:
8783 is_lvds = true;
8784 break;
8785 case INTEL_OUTPUT_SDVO:
8786 case INTEL_OUTPUT_HDMI:
8787 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008788 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008789 default:
8790 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008791 }
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008792 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008793
Chris Wilsonc1858122010-12-03 21:35:48 +00008794 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008795 factor = 21;
8796 if (is_lvds) {
8797 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008798 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008799 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008800 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008801 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008802 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008803
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008804 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008805
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008806 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8807 fp |= FP_CB_TUNE;
8808
8809 if (reduced_clock) {
8810 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8811
8812 if (reduced_clock->m < factor * reduced_clock->n)
8813 fp2 |= FP_CB_TUNE;
8814 } else {
8815 fp2 = fp;
8816 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008817
Chris Wilson5eddb702010-09-11 13:48:45 +01008818 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008819
Eric Anholta07d6782011-03-30 13:01:08 -07008820 if (is_lvds)
8821 dpll |= DPLLB_MODE_LVDS;
8822 else
8823 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008824
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008825 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008826 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008827
8828 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008829 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008830 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008831 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008832
Eric Anholta07d6782011-03-30 13:01:08 -07008833 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008834 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008835 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008836 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008837
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008838 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008839 case 5:
8840 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8841 break;
8842 case 7:
8843 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8844 break;
8845 case 10:
8846 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8847 break;
8848 case 14:
8849 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8850 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008851 }
8852
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008853 if (is_lvds && intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008854 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008855 else
8856 dpll |= PLL_REF_INPUT_DREFCLK;
8857
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008858 dpll |= DPLL_VCO_ENABLE;
8859
8860 crtc_state->dpll_hw_state.dpll = dpll;
8861 crtc_state->dpll_hw_state.fp0 = fp;
8862 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008863}
8864
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008865static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8866 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008867{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008868 struct drm_device *dev = crtc->base.dev;
8869 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008870 intel_clock_t reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008871 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008872 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008873 const intel_limit_t *limit;
8874 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008875
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008876 memset(&crtc_state->dpll_hw_state, 0,
8877 sizeof(crtc_state->dpll_hw_state));
8878
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008879 crtc->lowfreq_avail = false;
8880
8881 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8882 if (!crtc_state->has_pch_encoder)
8883 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008884
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008885 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8886 if (intel_panel_use_ssc(dev_priv)) {
8887 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8888 dev_priv->vbt.lvds_ssc_freq);
8889 refclk = dev_priv->vbt.lvds_ssc_freq;
8890 }
8891
8892 if (intel_is_dual_link_lvds(dev)) {
8893 if (refclk == 100000)
8894 limit = &intel_limits_ironlake_dual_lvds_100m;
8895 else
8896 limit = &intel_limits_ironlake_dual_lvds;
8897 } else {
8898 if (refclk == 100000)
8899 limit = &intel_limits_ironlake_single_lvds_100m;
8900 else
8901 limit = &intel_limits_ironlake_single_lvds;
8902 }
8903 } else {
8904 limit = &intel_limits_ironlake_dac;
8905 }
8906
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008907 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008908 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8909 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008910 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8911 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008912 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008913
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008914 ironlake_compute_dpll(crtc, crtc_state,
8915 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008916
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008917 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8918 if (pll == NULL) {
8919 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8920 pipe_name(crtc->pipe));
8921 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008922 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008923
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008924 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8925 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008926 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02008927
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008928 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008929}
8930
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008931static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8932 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008933{
8934 struct drm_device *dev = crtc->base.dev;
8935 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008936 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008937
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008938 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8939 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8940 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8941 & ~TU_SIZE_MASK;
8942 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8943 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8944 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8945}
8946
8947static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8948 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008949 struct intel_link_m_n *m_n,
8950 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008951{
8952 struct drm_device *dev = crtc->base.dev;
8953 struct drm_i915_private *dev_priv = dev->dev_private;
8954 enum pipe pipe = crtc->pipe;
8955
8956 if (INTEL_INFO(dev)->gen >= 5) {
8957 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8958 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8959 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8960 & ~TU_SIZE_MASK;
8961 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8962 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8963 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008964 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8965 * gen < 8) and if DRRS is supported (to make sure the
8966 * registers are not unnecessarily read).
8967 */
8968 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008969 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008970 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8971 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8972 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8973 & ~TU_SIZE_MASK;
8974 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8975 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8976 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8977 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008978 } else {
8979 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8980 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8981 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8982 & ~TU_SIZE_MASK;
8983 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8984 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8985 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8986 }
8987}
8988
8989void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008990 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008991{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008992 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008993 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8994 else
8995 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008996 &pipe_config->dp_m_n,
8997 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008998}
8999
Daniel Vetter72419202013-04-04 13:28:53 +02009000static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009001 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009002{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009003 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009004 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009005}
9006
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009007static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009008 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009009{
9010 struct drm_device *dev = crtc->base.dev;
9011 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009012 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9013 uint32_t ps_ctrl = 0;
9014 int id = -1;
9015 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009016
Chandra Kondurua1b22782015-04-07 15:28:45 -07009017 /* find scaler attached to this pipe */
9018 for (i = 0; i < crtc->num_scalers; i++) {
9019 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9020 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9021 id = i;
9022 pipe_config->pch_pfit.enabled = true;
9023 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9024 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9025 break;
9026 }
9027 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009028
Chandra Kondurua1b22782015-04-07 15:28:45 -07009029 scaler_state->scaler_id = id;
9030 if (id >= 0) {
9031 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9032 } else {
9033 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009034 }
9035}
9036
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009037static void
9038skylake_get_initial_plane_config(struct intel_crtc *crtc,
9039 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009040{
9041 struct drm_device *dev = crtc->base.dev;
9042 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009043 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009044 int pipe = crtc->pipe;
9045 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009046 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009047 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009048 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009049
Damien Lespiaud9806c92015-01-21 14:07:19 +00009050 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009051 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009052 DRM_DEBUG_KMS("failed to alloc fb\n");
9053 return;
9054 }
9055
Damien Lespiau1b842c82015-01-21 13:50:54 +00009056 fb = &intel_fb->base;
9057
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009058 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009059 if (!(val & PLANE_CTL_ENABLE))
9060 goto error;
9061
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009062 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9063 fourcc = skl_format_to_fourcc(pixel_format,
9064 val & PLANE_CTL_ORDER_RGBX,
9065 val & PLANE_CTL_ALPHA_MASK);
9066 fb->pixel_format = fourcc;
9067 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9068
Damien Lespiau40f46282015-02-27 11:15:21 +00009069 tiling = val & PLANE_CTL_TILED_MASK;
9070 switch (tiling) {
9071 case PLANE_CTL_TILED_LINEAR:
9072 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9073 break;
9074 case PLANE_CTL_TILED_X:
9075 plane_config->tiling = I915_TILING_X;
9076 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9077 break;
9078 case PLANE_CTL_TILED_Y:
9079 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9080 break;
9081 case PLANE_CTL_TILED_YF:
9082 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9083 break;
9084 default:
9085 MISSING_CASE(tiling);
9086 goto error;
9087 }
9088
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009089 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9090 plane_config->base = base;
9091
9092 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9093
9094 val = I915_READ(PLANE_SIZE(pipe, 0));
9095 fb->height = ((val >> 16) & 0xfff) + 1;
9096 fb->width = ((val >> 0) & 0x1fff) + 1;
9097
9098 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009099 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009100 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009101 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9102
9103 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009104 fb->pixel_format,
9105 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009106
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009107 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009108
9109 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9110 pipe_name(pipe), fb->width, fb->height,
9111 fb->bits_per_pixel, base, fb->pitches[0],
9112 plane_config->size);
9113
Damien Lespiau2d140302015-02-05 17:22:18 +00009114 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009115 return;
9116
9117error:
9118 kfree(fb);
9119}
9120
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009121static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009122 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009123{
9124 struct drm_device *dev = crtc->base.dev;
9125 struct drm_i915_private *dev_priv = dev->dev_private;
9126 uint32_t tmp;
9127
9128 tmp = I915_READ(PF_CTL(crtc->pipe));
9129
9130 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009131 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009132 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9133 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009134
9135 /* We currently do not free assignements of panel fitters on
9136 * ivb/hsw (since we don't use the higher upscaling modes which
9137 * differentiates them) so just WARN about this case for now. */
9138 if (IS_GEN7(dev)) {
9139 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9140 PF_PIPE_SEL_IVB(crtc->pipe));
9141 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009142 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009143}
9144
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009145static void
9146ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9147 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009148{
9149 struct drm_device *dev = crtc->base.dev;
9150 struct drm_i915_private *dev_priv = dev->dev_private;
9151 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009152 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009153 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009154 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009155 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009156 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009157
Damien Lespiau42a7b082015-02-05 19:35:13 +00009158 val = I915_READ(DSPCNTR(pipe));
9159 if (!(val & DISPLAY_PLANE_ENABLE))
9160 return;
9161
Damien Lespiaud9806c92015-01-21 14:07:19 +00009162 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009163 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009164 DRM_DEBUG_KMS("failed to alloc fb\n");
9165 return;
9166 }
9167
Damien Lespiau1b842c82015-01-21 13:50:54 +00009168 fb = &intel_fb->base;
9169
Daniel Vetter18c52472015-02-10 17:16:09 +00009170 if (INTEL_INFO(dev)->gen >= 4) {
9171 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009172 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009173 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9174 }
9175 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009176
9177 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009178 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009179 fb->pixel_format = fourcc;
9180 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009181
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009182 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009183 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009184 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009185 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009186 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009187 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009188 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009189 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009190 }
9191 plane_config->base = base;
9192
9193 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009194 fb->width = ((val >> 16) & 0xfff) + 1;
9195 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009196
9197 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009198 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009199
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009200 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009201 fb->pixel_format,
9202 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009203
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009204 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009205
Damien Lespiau2844a922015-01-20 12:51:48 +00009206 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9207 pipe_name(pipe), fb->width, fb->height,
9208 fb->bits_per_pixel, base, fb->pitches[0],
9209 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009210
Damien Lespiau2d140302015-02-05 17:22:18 +00009211 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009212}
9213
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009214static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009215 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009216{
9217 struct drm_device *dev = crtc->base.dev;
9218 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009219 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009220 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009221 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009222
Imre Deak17290502016-02-12 18:55:11 +02009223 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9224 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009225 return false;
9226
Daniel Vettere143a212013-07-04 12:01:15 +02009227 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009228 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009229
Imre Deak17290502016-02-12 18:55:11 +02009230 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009231 tmp = I915_READ(PIPECONF(crtc->pipe));
9232 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009233 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009234
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009235 switch (tmp & PIPECONF_BPC_MASK) {
9236 case PIPECONF_6BPC:
9237 pipe_config->pipe_bpp = 18;
9238 break;
9239 case PIPECONF_8BPC:
9240 pipe_config->pipe_bpp = 24;
9241 break;
9242 case PIPECONF_10BPC:
9243 pipe_config->pipe_bpp = 30;
9244 break;
9245 case PIPECONF_12BPC:
9246 pipe_config->pipe_bpp = 36;
9247 break;
9248 default:
9249 break;
9250 }
9251
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009252 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9253 pipe_config->limited_color_range = true;
9254
Daniel Vetterab9412b2013-05-03 11:49:46 +02009255 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009256 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009257 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009258
Daniel Vetter88adfff2013-03-28 10:42:01 +01009259 pipe_config->has_pch_encoder = true;
9260
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009261 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9262 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9263 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009264
9265 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009266
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009267 if (HAS_PCH_IBX(dev_priv)) {
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009268 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009269 } else {
9270 tmp = I915_READ(PCH_DPLL_SEL);
9271 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009272 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009273 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009274 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009275 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009276
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009277 pipe_config->shared_dpll =
9278 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9279 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009280
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009281 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9282 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009283
9284 tmp = pipe_config->dpll_hw_state.dpll;
9285 pipe_config->pixel_multiplier =
9286 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9287 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009288
9289 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009290 } else {
9291 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009292 }
9293
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009294 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009295 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009296
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009297 ironlake_get_pfit_config(crtc, pipe_config);
9298
Imre Deak17290502016-02-12 18:55:11 +02009299 ret = true;
9300
9301out:
9302 intel_display_power_put(dev_priv, power_domain);
9303
9304 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009305}
9306
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009307static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9308{
9309 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009310 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009311
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009312 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009313 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009314 pipe_name(crtc->pipe));
9315
Rob Clarke2c719b2014-12-15 13:56:32 -05009316 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9317 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009318 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9319 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009320 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9321 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009322 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009323 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009324 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009325 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009326 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009327 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009328 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009329 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009330 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009331
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009332 /*
9333 * In theory we can still leave IRQs enabled, as long as only the HPD
9334 * interrupts remain enabled. We used to check for that, but since it's
9335 * gen-specific and since we only disable LCPLL after we fully disable
9336 * the interrupts, the check below should be enough.
9337 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009338 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009339}
9340
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009341static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9342{
9343 struct drm_device *dev = dev_priv->dev;
9344
9345 if (IS_HASWELL(dev))
9346 return I915_READ(D_COMP_HSW);
9347 else
9348 return I915_READ(D_COMP_BDW);
9349}
9350
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009351static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9352{
9353 struct drm_device *dev = dev_priv->dev;
9354
9355 if (IS_HASWELL(dev)) {
9356 mutex_lock(&dev_priv->rps.hw_lock);
9357 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9358 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009359 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009360 mutex_unlock(&dev_priv->rps.hw_lock);
9361 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009362 I915_WRITE(D_COMP_BDW, val);
9363 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009364 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009365}
9366
9367/*
9368 * This function implements pieces of two sequences from BSpec:
9369 * - Sequence for display software to disable LCPLL
9370 * - Sequence for display software to allow package C8+
9371 * The steps implemented here are just the steps that actually touch the LCPLL
9372 * register. Callers should take care of disabling all the display engine
9373 * functions, doing the mode unset, fixing interrupts, etc.
9374 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009375static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9376 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009377{
9378 uint32_t val;
9379
9380 assert_can_disable_lcpll(dev_priv);
9381
9382 val = I915_READ(LCPLL_CTL);
9383
9384 if (switch_to_fclk) {
9385 val |= LCPLL_CD_SOURCE_FCLK;
9386 I915_WRITE(LCPLL_CTL, val);
9387
9388 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9389 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9390 DRM_ERROR("Switching to FCLK failed\n");
9391
9392 val = I915_READ(LCPLL_CTL);
9393 }
9394
9395 val |= LCPLL_PLL_DISABLE;
9396 I915_WRITE(LCPLL_CTL, val);
9397 POSTING_READ(LCPLL_CTL);
9398
9399 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9400 DRM_ERROR("LCPLL still locked\n");
9401
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009402 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009403 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009404 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009405 ndelay(100);
9406
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009407 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9408 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009409 DRM_ERROR("D_COMP RCOMP still in progress\n");
9410
9411 if (allow_power_down) {
9412 val = I915_READ(LCPLL_CTL);
9413 val |= LCPLL_POWER_DOWN_ALLOW;
9414 I915_WRITE(LCPLL_CTL, val);
9415 POSTING_READ(LCPLL_CTL);
9416 }
9417}
9418
9419/*
9420 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9421 * source.
9422 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009423static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009424{
9425 uint32_t val;
9426
9427 val = I915_READ(LCPLL_CTL);
9428
9429 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9430 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9431 return;
9432
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009433 /*
9434 * Make sure we're not on PC8 state before disabling PC8, otherwise
9435 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009436 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009437 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009438
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009439 if (val & LCPLL_POWER_DOWN_ALLOW) {
9440 val &= ~LCPLL_POWER_DOWN_ALLOW;
9441 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009442 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009443 }
9444
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009445 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009446 val |= D_COMP_COMP_FORCE;
9447 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009448 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009449
9450 val = I915_READ(LCPLL_CTL);
9451 val &= ~LCPLL_PLL_DISABLE;
9452 I915_WRITE(LCPLL_CTL, val);
9453
9454 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9455 DRM_ERROR("LCPLL not locked yet\n");
9456
9457 if (val & LCPLL_CD_SOURCE_FCLK) {
9458 val = I915_READ(LCPLL_CTL);
9459 val &= ~LCPLL_CD_SOURCE_FCLK;
9460 I915_WRITE(LCPLL_CTL, val);
9461
9462 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9463 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9464 DRM_ERROR("Switching back to LCPLL failed\n");
9465 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009466
Mika Kuoppala59bad942015-01-16 11:34:40 +02009467 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009468 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009469}
9470
Paulo Zanoni765dab672014-03-07 20:08:18 -03009471/*
9472 * Package states C8 and deeper are really deep PC states that can only be
9473 * reached when all the devices on the system allow it, so even if the graphics
9474 * device allows PC8+, it doesn't mean the system will actually get to these
9475 * states. Our driver only allows PC8+ when going into runtime PM.
9476 *
9477 * The requirements for PC8+ are that all the outputs are disabled, the power
9478 * well is disabled and most interrupts are disabled, and these are also
9479 * requirements for runtime PM. When these conditions are met, we manually do
9480 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9481 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9482 * hang the machine.
9483 *
9484 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9485 * the state of some registers, so when we come back from PC8+ we need to
9486 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9487 * need to take care of the registers kept by RC6. Notice that this happens even
9488 * if we don't put the device in PCI D3 state (which is what currently happens
9489 * because of the runtime PM support).
9490 *
9491 * For more, read "Display Sequences for Package C8" on the hardware
9492 * documentation.
9493 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009494void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009495{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009496 struct drm_device *dev = dev_priv->dev;
9497 uint32_t val;
9498
Paulo Zanonic67a4702013-08-19 13:18:09 -03009499 DRM_DEBUG_KMS("Enabling package C8+\n");
9500
Ville Syrjäläc2699522015-08-27 23:55:59 +03009501 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009502 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9503 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9504 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9505 }
9506
9507 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009508 hsw_disable_lcpll(dev_priv, true, true);
9509}
9510
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009511void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009512{
9513 struct drm_device *dev = dev_priv->dev;
9514 uint32_t val;
9515
Paulo Zanonic67a4702013-08-19 13:18:09 -03009516 DRM_DEBUG_KMS("Disabling package C8+\n");
9517
9518 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009519 lpt_init_pch_refclk(dev);
9520
Ville Syrjäläc2699522015-08-27 23:55:59 +03009521 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009522 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9523 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9524 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9525 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009526}
9527
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009528static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309529{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009530 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009531 struct intel_atomic_state *old_intel_state =
9532 to_intel_atomic_state(old_state);
9533 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309534
Imre Deakc6c46962016-04-01 16:02:40 +03009535 broxton_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309536}
9537
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009538/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009539static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009540{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009541 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9542 struct drm_i915_private *dev_priv = state->dev->dev_private;
9543 struct drm_crtc *crtc;
9544 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009545 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009546 unsigned max_pixel_rate = 0, i;
9547 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009548
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009549 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9550 sizeof(intel_state->min_pixclk));
9551
9552 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009553 int pixel_rate;
9554
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009555 crtc_state = to_intel_crtc_state(cstate);
9556 if (!crtc_state->base.enable) {
9557 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009558 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009559 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009560
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009561 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009562
9563 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009564 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009565 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9566
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009567 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009568 }
9569
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009570 for_each_pipe(dev_priv, pipe)
9571 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9572
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009573 return max_pixel_rate;
9574}
9575
9576static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9577{
9578 struct drm_i915_private *dev_priv = dev->dev_private;
9579 uint32_t val, data;
9580 int ret;
9581
9582 if (WARN((I915_READ(LCPLL_CTL) &
9583 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9584 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9585 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9586 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9587 "trying to change cdclk frequency with cdclk not enabled\n"))
9588 return;
9589
9590 mutex_lock(&dev_priv->rps.hw_lock);
9591 ret = sandybridge_pcode_write(dev_priv,
9592 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9593 mutex_unlock(&dev_priv->rps.hw_lock);
9594 if (ret) {
9595 DRM_ERROR("failed to inform pcode about cdclk change\n");
9596 return;
9597 }
9598
9599 val = I915_READ(LCPLL_CTL);
9600 val |= LCPLL_CD_SOURCE_FCLK;
9601 I915_WRITE(LCPLL_CTL, val);
9602
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009603 if (wait_for_us(I915_READ(LCPLL_CTL) &
9604 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009605 DRM_ERROR("Switching to FCLK failed\n");
9606
9607 val = I915_READ(LCPLL_CTL);
9608 val &= ~LCPLL_CLK_FREQ_MASK;
9609
9610 switch (cdclk) {
9611 case 450000:
9612 val |= LCPLL_CLK_FREQ_450;
9613 data = 0;
9614 break;
9615 case 540000:
9616 val |= LCPLL_CLK_FREQ_54O_BDW;
9617 data = 1;
9618 break;
9619 case 337500:
9620 val |= LCPLL_CLK_FREQ_337_5_BDW;
9621 data = 2;
9622 break;
9623 case 675000:
9624 val |= LCPLL_CLK_FREQ_675_BDW;
9625 data = 3;
9626 break;
9627 default:
9628 WARN(1, "invalid cdclk frequency\n");
9629 return;
9630 }
9631
9632 I915_WRITE(LCPLL_CTL, val);
9633
9634 val = I915_READ(LCPLL_CTL);
9635 val &= ~LCPLL_CD_SOURCE_FCLK;
9636 I915_WRITE(LCPLL_CTL, val);
9637
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009638 if (wait_for_us((I915_READ(LCPLL_CTL) &
9639 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009640 DRM_ERROR("Switching back to LCPLL failed\n");
9641
9642 mutex_lock(&dev_priv->rps.hw_lock);
9643 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9644 mutex_unlock(&dev_priv->rps.hw_lock);
9645
9646 intel_update_cdclk(dev);
9647
9648 WARN(cdclk != dev_priv->cdclk_freq,
9649 "cdclk requested %d kHz but got %d kHz\n",
9650 cdclk, dev_priv->cdclk_freq);
9651}
9652
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009653static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009654{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009655 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009656 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009657 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009658 int cdclk;
9659
9660 /*
9661 * FIXME should also account for plane ratio
9662 * once 64bpp pixel formats are supported.
9663 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009664 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009665 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009666 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009667 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009668 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009669 cdclk = 450000;
9670 else
9671 cdclk = 337500;
9672
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009673 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009674 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9675 cdclk, dev_priv->max_cdclk_freq);
9676 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009677 }
9678
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009679 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9680 if (!intel_state->active_crtcs)
9681 intel_state->dev_cdclk = 337500;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009682
9683 return 0;
9684}
9685
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009686static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009687{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009688 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009689 struct intel_atomic_state *old_intel_state =
9690 to_intel_atomic_state(old_state);
9691 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009692
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009693 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009694}
9695
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009696static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9697 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009698{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009699 struct intel_encoder *intel_encoder =
9700 intel_ddi_get_crtc_new_encoder(crtc_state);
9701
9702 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9703 if (!intel_ddi_pll_select(crtc, crtc_state))
9704 return -EINVAL;
9705 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009706
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009707 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009708
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009709 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009710}
9711
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309712static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9713 enum port port,
9714 struct intel_crtc_state *pipe_config)
9715{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009716 enum intel_dpll_id id;
9717
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309718 switch (port) {
9719 case PORT_A:
9720 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009721 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309722 break;
9723 case PORT_B:
9724 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009725 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309726 break;
9727 case PORT_C:
9728 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009729 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309730 break;
9731 default:
9732 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009733 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309734 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009735
9736 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309737}
9738
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009739static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9740 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009741 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009742{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009743 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009744 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009745
9746 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9747 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9748
9749 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009750 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009751 id = DPLL_ID_SKL_DPLL0;
9752 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009753 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009754 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009755 break;
9756 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009757 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009758 break;
9759 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009760 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009761 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009762 default:
9763 MISSING_CASE(pipe_config->ddi_pll_sel);
9764 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009765 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009766
9767 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009768}
9769
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009770static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9771 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009772 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009773{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009774 enum intel_dpll_id id;
9775
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009776 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9777
9778 switch (pipe_config->ddi_pll_sel) {
9779 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009780 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009781 break;
9782 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009783 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009784 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009785 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009786 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009787 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009788 case PORT_CLK_SEL_LCPLL_810:
9789 id = DPLL_ID_LCPLL_810;
9790 break;
9791 case PORT_CLK_SEL_LCPLL_1350:
9792 id = DPLL_ID_LCPLL_1350;
9793 break;
9794 case PORT_CLK_SEL_LCPLL_2700:
9795 id = DPLL_ID_LCPLL_2700;
9796 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009797 default:
9798 MISSING_CASE(pipe_config->ddi_pll_sel);
9799 /* fall through */
9800 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009801 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009802 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009803
9804 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009805}
9806
Jani Nikulacf304292016-03-18 17:05:41 +02009807static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9808 struct intel_crtc_state *pipe_config,
9809 unsigned long *power_domain_mask)
9810{
9811 struct drm_device *dev = crtc->base.dev;
9812 struct drm_i915_private *dev_priv = dev->dev_private;
9813 enum intel_display_power_domain power_domain;
9814 u32 tmp;
9815
9816 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9817
9818 /*
9819 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9820 * consistency and less surprising code; it's in always on power).
9821 */
9822 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9823 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9824 enum pipe trans_edp_pipe;
9825 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9826 default:
9827 WARN(1, "unknown pipe linked to edp transcoder\n");
9828 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9829 case TRANS_DDI_EDP_INPUT_A_ON:
9830 trans_edp_pipe = PIPE_A;
9831 break;
9832 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9833 trans_edp_pipe = PIPE_B;
9834 break;
9835 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9836 trans_edp_pipe = PIPE_C;
9837 break;
9838 }
9839
9840 if (trans_edp_pipe == crtc->pipe)
9841 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9842 }
9843
9844 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9845 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9846 return false;
9847 *power_domain_mask |= BIT(power_domain);
9848
9849 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9850
9851 return tmp & PIPECONF_ENABLE;
9852}
9853
Jani Nikula4d1de972016-03-18 17:05:42 +02009854static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9855 struct intel_crtc_state *pipe_config,
9856 unsigned long *power_domain_mask)
9857{
9858 struct drm_device *dev = crtc->base.dev;
9859 struct drm_i915_private *dev_priv = dev->dev_private;
9860 enum intel_display_power_domain power_domain;
9861 enum port port;
9862 enum transcoder cpu_transcoder;
9863 u32 tmp;
9864
9865 pipe_config->has_dsi_encoder = false;
9866
9867 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9868 if (port == PORT_A)
9869 cpu_transcoder = TRANSCODER_DSI_A;
9870 else
9871 cpu_transcoder = TRANSCODER_DSI_C;
9872
9873 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
9874 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9875 continue;
9876 *power_domain_mask |= BIT(power_domain);
9877
Imre Deakdb18b6a2016-03-24 12:41:40 +02009878 /*
9879 * The PLL needs to be enabled with a valid divider
9880 * configuration, otherwise accessing DSI registers will hang
9881 * the machine. See BSpec North Display Engine
9882 * registers/MIPI[BXT]. We can break out here early, since we
9883 * need the same DSI PLL to be enabled for both DSI ports.
9884 */
9885 if (!intel_dsi_pll_is_enabled(dev_priv))
9886 break;
9887
Jani Nikula4d1de972016-03-18 17:05:42 +02009888 /* XXX: this works for video mode only */
9889 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
9890 if (!(tmp & DPI_ENABLE))
9891 continue;
9892
9893 tmp = I915_READ(MIPI_CTRL(port));
9894 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
9895 continue;
9896
9897 pipe_config->cpu_transcoder = cpu_transcoder;
9898 pipe_config->has_dsi_encoder = true;
9899 break;
9900 }
9901
9902 return pipe_config->has_dsi_encoder;
9903}
9904
Daniel Vetter26804af2014-06-25 22:01:55 +03009905static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009906 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009907{
9908 struct drm_device *dev = crtc->base.dev;
9909 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009910 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009911 enum port port;
9912 uint32_t tmp;
9913
9914 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9915
9916 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9917
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07009918 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009919 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309920 else if (IS_BROXTON(dev))
9921 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009922 else
9923 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009924
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009925 pll = pipe_config->shared_dpll;
9926 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009927 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9928 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009929 }
9930
Daniel Vetter26804af2014-06-25 22:01:55 +03009931 /*
9932 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9933 * DDI E. So just check whether this pipe is wired to DDI E and whether
9934 * the PCH transcoder is on.
9935 */
Damien Lespiauca370452013-12-03 13:56:24 +00009936 if (INTEL_INFO(dev)->gen < 9 &&
9937 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009938 pipe_config->has_pch_encoder = true;
9939
9940 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9941 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9942 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9943
9944 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9945 }
9946}
9947
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009948static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009949 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009950{
9951 struct drm_device *dev = crtc->base.dev;
9952 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009953 enum intel_display_power_domain power_domain;
9954 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +02009955 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009956
Imre Deak17290502016-02-12 18:55:11 +02009957 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9958 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02009959 return false;
Imre Deak17290502016-02-12 18:55:11 +02009960 power_domain_mask = BIT(power_domain);
9961
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009962 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009963
Jani Nikulacf304292016-03-18 17:05:41 +02009964 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +02009965
Jani Nikula4d1de972016-03-18 17:05:42 +02009966 if (IS_BROXTON(dev_priv)) {
9967 bxt_get_dsi_transcoder_state(crtc, pipe_config,
9968 &power_domain_mask);
9969 WARN_ON(active && pipe_config->has_dsi_encoder);
9970 if (pipe_config->has_dsi_encoder)
9971 active = true;
9972 }
9973
Jani Nikulacf304292016-03-18 17:05:41 +02009974 if (!active)
Imre Deak17290502016-02-12 18:55:11 +02009975 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009976
Jani Nikula4d1de972016-03-18 17:05:42 +02009977 if (!pipe_config->has_dsi_encoder) {
9978 haswell_get_ddi_port_state(crtc, pipe_config);
9979 intel_get_pipe_timings(crtc, pipe_config);
9980 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009981
Jani Nikulabc58be62016-03-18 17:05:39 +02009982 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009983
Lionel Landwerlin05dc6982016-03-16 10:57:15 +00009984 pipe_config->gamma_mode =
9985 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
9986
Chandra Kondurua1b22782015-04-07 15:28:45 -07009987 if (INTEL_INFO(dev)->gen >= 9) {
9988 skl_init_scalers(dev, crtc, pipe_config);
9989 }
9990
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009991 if (INTEL_INFO(dev)->gen >= 9) {
9992 pipe_config->scaler_state.scaler_id = -1;
9993 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9994 }
9995
Imre Deak17290502016-02-12 18:55:11 +02009996 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9997 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
9998 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009999 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010000 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010001 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010002 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010003 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010004
Jesse Barnese59150d2014-01-07 13:30:45 -080010005 if (IS_HASWELL(dev))
10006 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10007 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010008
Jani Nikula4d1de972016-03-18 17:05:42 +020010009 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10010 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010011 pipe_config->pixel_multiplier =
10012 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10013 } else {
10014 pipe_config->pixel_multiplier = 1;
10015 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010016
Imre Deak17290502016-02-12 18:55:11 +020010017out:
10018 for_each_power_domain(power_domain, power_domain_mask)
10019 intel_display_power_put(dev_priv, power_domain);
10020
Jani Nikulacf304292016-03-18 17:05:41 +020010021 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010022}
10023
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010024static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10025 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010026{
10027 struct drm_device *dev = crtc->dev;
10028 struct drm_i915_private *dev_priv = dev->dev_private;
10029 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010030 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010031
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010032 if (plane_state && plane_state->visible) {
10033 unsigned int width = plane_state->base.crtc_w;
10034 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010035 unsigned int stride = roundup_pow_of_two(width) * 4;
10036
10037 switch (stride) {
10038 default:
10039 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10040 width, stride);
10041 stride = 256;
10042 /* fallthrough */
10043 case 256:
10044 case 512:
10045 case 1024:
10046 case 2048:
10047 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010048 }
10049
Ville Syrjälädc41c152014-08-13 11:57:05 +030010050 cntl |= CURSOR_ENABLE |
10051 CURSOR_GAMMA_ENABLE |
10052 CURSOR_FORMAT_ARGB |
10053 CURSOR_STRIDE(stride);
10054
10055 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010056 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010057
Ville Syrjälädc41c152014-08-13 11:57:05 +030010058 if (intel_crtc->cursor_cntl != 0 &&
10059 (intel_crtc->cursor_base != base ||
10060 intel_crtc->cursor_size != size ||
10061 intel_crtc->cursor_cntl != cntl)) {
10062 /* On these chipsets we can only modify the base/size/stride
10063 * whilst the cursor is disabled.
10064 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010065 I915_WRITE(CURCNTR(PIPE_A), 0);
10066 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010067 intel_crtc->cursor_cntl = 0;
10068 }
10069
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010070 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010071 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010072 intel_crtc->cursor_base = base;
10073 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010074
10075 if (intel_crtc->cursor_size != size) {
10076 I915_WRITE(CURSIZE, size);
10077 intel_crtc->cursor_size = size;
10078 }
10079
Chris Wilson4b0e3332014-05-30 16:35:26 +030010080 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010081 I915_WRITE(CURCNTR(PIPE_A), cntl);
10082 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010083 intel_crtc->cursor_cntl = cntl;
10084 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010085}
10086
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010087static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10088 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010089{
10090 struct drm_device *dev = crtc->dev;
10091 struct drm_i915_private *dev_priv = dev->dev_private;
10092 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10093 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010094 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010095
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010096 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010097 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010098 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010099 case 64:
10100 cntl |= CURSOR_MODE_64_ARGB_AX;
10101 break;
10102 case 128:
10103 cntl |= CURSOR_MODE_128_ARGB_AX;
10104 break;
10105 case 256:
10106 cntl |= CURSOR_MODE_256_ARGB_AX;
10107 break;
10108 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010109 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010110 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010111 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010112 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010113
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010114 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010115 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010116
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010117 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10118 cntl |= CURSOR_ROTATE_180;
10119 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010120
Chris Wilson4b0e3332014-05-30 16:35:26 +030010121 if (intel_crtc->cursor_cntl != cntl) {
10122 I915_WRITE(CURCNTR(pipe), cntl);
10123 POSTING_READ(CURCNTR(pipe));
10124 intel_crtc->cursor_cntl = cntl;
10125 }
10126
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010127 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010128 I915_WRITE(CURBASE(pipe), base);
10129 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010130
10131 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010132}
10133
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010134/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010135static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010136 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010137{
10138 struct drm_device *dev = crtc->dev;
10139 struct drm_i915_private *dev_priv = dev->dev_private;
10140 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10141 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010142 u32 base = intel_crtc->cursor_addr;
10143 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010144
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010145 if (plane_state) {
10146 int x = plane_state->base.crtc_x;
10147 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010148
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010149 if (x < 0) {
10150 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10151 x = -x;
10152 }
10153 pos |= x << CURSOR_X_SHIFT;
10154
10155 if (y < 0) {
10156 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10157 y = -y;
10158 }
10159 pos |= y << CURSOR_Y_SHIFT;
10160
10161 /* ILK+ do this automagically */
10162 if (HAS_GMCH_DISPLAY(dev) &&
10163 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10164 base += (plane_state->base.crtc_h *
10165 plane_state->base.crtc_w - 1) * 4;
10166 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010167 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010168
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010169 I915_WRITE(CURPOS(pipe), pos);
10170
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010171 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010172 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010173 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010174 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010175}
10176
Ville Syrjälädc41c152014-08-13 11:57:05 +030010177static bool cursor_size_ok(struct drm_device *dev,
10178 uint32_t width, uint32_t height)
10179{
10180 if (width == 0 || height == 0)
10181 return false;
10182
10183 /*
10184 * 845g/865g are special in that they are only limited by
10185 * the width of their cursors, the height is arbitrary up to
10186 * the precision of the register. Everything else requires
10187 * square cursors, limited to a few power-of-two sizes.
10188 */
10189 if (IS_845G(dev) || IS_I865G(dev)) {
10190 if ((width & 63) != 0)
10191 return false;
10192
10193 if (width > (IS_845G(dev) ? 64 : 512))
10194 return false;
10195
10196 if (height > 1023)
10197 return false;
10198 } else {
10199 switch (width | height) {
10200 case 256:
10201 case 128:
10202 if (IS_GEN2(dev))
10203 return false;
10204 case 64:
10205 break;
10206 default:
10207 return false;
10208 }
10209 }
10210
10211 return true;
10212}
10213
Jesse Barnes79e53942008-11-07 14:24:08 -080010214/* VESA 640x480x72Hz mode to set on the pipe */
10215static struct drm_display_mode load_detect_mode = {
10216 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10217 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10218};
10219
Daniel Vettera8bb6812014-02-10 18:00:39 +010010220struct drm_framebuffer *
10221__intel_framebuffer_create(struct drm_device *dev,
10222 struct drm_mode_fb_cmd2 *mode_cmd,
10223 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010224{
10225 struct intel_framebuffer *intel_fb;
10226 int ret;
10227
10228 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010229 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010230 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010231
10232 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010233 if (ret)
10234 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010235
10236 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010237
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010238err:
10239 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010240 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010241}
10242
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010243static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010244intel_framebuffer_create(struct drm_device *dev,
10245 struct drm_mode_fb_cmd2 *mode_cmd,
10246 struct drm_i915_gem_object *obj)
10247{
10248 struct drm_framebuffer *fb;
10249 int ret;
10250
10251 ret = i915_mutex_lock_interruptible(dev);
10252 if (ret)
10253 return ERR_PTR(ret);
10254 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10255 mutex_unlock(&dev->struct_mutex);
10256
10257 return fb;
10258}
10259
Chris Wilsond2dff872011-04-19 08:36:26 +010010260static u32
10261intel_framebuffer_pitch_for_width(int width, int bpp)
10262{
10263 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10264 return ALIGN(pitch, 64);
10265}
10266
10267static u32
10268intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10269{
10270 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010271 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010272}
10273
10274static struct drm_framebuffer *
10275intel_framebuffer_create_for_mode(struct drm_device *dev,
10276 struct drm_display_mode *mode,
10277 int depth, int bpp)
10278{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010279 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010280 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010281 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010282
10283 obj = i915_gem_alloc_object(dev,
10284 intel_framebuffer_size_for_mode(mode, bpp));
10285 if (obj == NULL)
10286 return ERR_PTR(-ENOMEM);
10287
10288 mode_cmd.width = mode->hdisplay;
10289 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010290 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10291 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010292 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010293
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010294 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10295 if (IS_ERR(fb))
10296 drm_gem_object_unreference_unlocked(&obj->base);
10297
10298 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010299}
10300
10301static struct drm_framebuffer *
10302mode_fits_in_fbdev(struct drm_device *dev,
10303 struct drm_display_mode *mode)
10304{
Daniel Vetter06957262015-08-10 13:34:08 +020010305#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010306 struct drm_i915_private *dev_priv = dev->dev_private;
10307 struct drm_i915_gem_object *obj;
10308 struct drm_framebuffer *fb;
10309
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010310 if (!dev_priv->fbdev)
10311 return NULL;
10312
10313 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010314 return NULL;
10315
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010316 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010317 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010318
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010319 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010320 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10321 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010322 return NULL;
10323
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010324 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010325 return NULL;
10326
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010327 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010328 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010329#else
10330 return NULL;
10331#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010332}
10333
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010334static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10335 struct drm_crtc *crtc,
10336 struct drm_display_mode *mode,
10337 struct drm_framebuffer *fb,
10338 int x, int y)
10339{
10340 struct drm_plane_state *plane_state;
10341 int hdisplay, vdisplay;
10342 int ret;
10343
10344 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10345 if (IS_ERR(plane_state))
10346 return PTR_ERR(plane_state);
10347
10348 if (mode)
10349 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10350 else
10351 hdisplay = vdisplay = 0;
10352
10353 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10354 if (ret)
10355 return ret;
10356 drm_atomic_set_fb_for_plane(plane_state, fb);
10357 plane_state->crtc_x = 0;
10358 plane_state->crtc_y = 0;
10359 plane_state->crtc_w = hdisplay;
10360 plane_state->crtc_h = vdisplay;
10361 plane_state->src_x = x << 16;
10362 plane_state->src_y = y << 16;
10363 plane_state->src_w = hdisplay << 16;
10364 plane_state->src_h = vdisplay << 16;
10365
10366 return 0;
10367}
10368
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010369bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010370 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010371 struct intel_load_detect_pipe *old,
10372 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010373{
10374 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010375 struct intel_encoder *intel_encoder =
10376 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010377 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010378 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010379 struct drm_crtc *crtc = NULL;
10380 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010381 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010382 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010383 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010384 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010385 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010386 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010387
Chris Wilsond2dff872011-04-19 08:36:26 +010010388 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010389 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010390 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010391
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010392 old->restore_state = NULL;
10393
Rob Clark51fd3712013-11-19 12:10:12 -050010394retry:
10395 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10396 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010397 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010398
Jesse Barnes79e53942008-11-07 14:24:08 -080010399 /*
10400 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010401 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010402 * - if the connector already has an assigned crtc, use it (but make
10403 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010404 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010405 * - try to find the first unused crtc that can drive this connector,
10406 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010407 */
10408
10409 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010410 if (connector->state->crtc) {
10411 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010412
Rob Clark51fd3712013-11-19 12:10:12 -050010413 ret = drm_modeset_lock(&crtc->mutex, ctx);
10414 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010415 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010416
10417 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010418 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010419 }
10420
10421 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010422 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010423 i++;
10424 if (!(encoder->possible_crtcs & (1 << i)))
10425 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010426
10427 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10428 if (ret)
10429 goto fail;
10430
10431 if (possible_crtc->state->enable) {
10432 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010433 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010434 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010435
10436 crtc = possible_crtc;
10437 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010438 }
10439
10440 /*
10441 * If we didn't find an unused CRTC, don't use any.
10442 */
10443 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010444 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010445 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010446 }
10447
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010448found:
10449 intel_crtc = to_intel_crtc(crtc);
10450
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010451 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10452 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010453 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010454
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010455 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010456 restore_state = drm_atomic_state_alloc(dev);
10457 if (!state || !restore_state) {
10458 ret = -ENOMEM;
10459 goto fail;
10460 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010461
10462 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010463 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010464
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010465 connector_state = drm_atomic_get_connector_state(state, connector);
10466 if (IS_ERR(connector_state)) {
10467 ret = PTR_ERR(connector_state);
10468 goto fail;
10469 }
10470
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010471 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10472 if (ret)
10473 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010474
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010475 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10476 if (IS_ERR(crtc_state)) {
10477 ret = PTR_ERR(crtc_state);
10478 goto fail;
10479 }
10480
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010481 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010482
Chris Wilson64927112011-04-20 07:25:26 +010010483 if (!mode)
10484 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010485
Chris Wilsond2dff872011-04-19 08:36:26 +010010486 /* We need a framebuffer large enough to accommodate all accesses
10487 * that the plane may generate whilst we perform load detection.
10488 * We can not rely on the fbcon either being present (we get called
10489 * during its initialisation to detect all boot displays, or it may
10490 * not even exist) or that it is large enough to satisfy the
10491 * requested mode.
10492 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010493 fb = mode_fits_in_fbdev(dev, mode);
10494 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010495 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010496 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010497 } else
10498 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010499 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010500 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010501 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010502 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010503
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010504 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10505 if (ret)
10506 goto fail;
10507
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010508 drm_framebuffer_unreference(fb);
10509
10510 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10511 if (ret)
10512 goto fail;
10513
10514 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10515 if (!ret)
10516 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10517 if (!ret)
10518 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10519 if (ret) {
10520 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10521 goto fail;
10522 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010523
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010524 ret = drm_atomic_commit(state);
10525 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010526 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010527 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010528 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010529
10530 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010531
Jesse Barnes79e53942008-11-07 14:24:08 -080010532 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010533 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010534 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010535
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010536fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010537 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010538 drm_atomic_state_free(restore_state);
10539 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010540
Rob Clark51fd3712013-11-19 12:10:12 -050010541 if (ret == -EDEADLK) {
10542 drm_modeset_backoff(ctx);
10543 goto retry;
10544 }
10545
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010546 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010547}
10548
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010549void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010550 struct intel_load_detect_pipe *old,
10551 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010552{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010553 struct intel_encoder *intel_encoder =
10554 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010555 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010556 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010557 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010558
Chris Wilsond2dff872011-04-19 08:36:26 +010010559 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010560 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010561 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010562
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010563 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010564 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010565
10566 ret = drm_atomic_commit(state);
10567 if (ret) {
10568 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10569 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010570 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010571}
10572
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010573static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010574 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010575{
10576 struct drm_i915_private *dev_priv = dev->dev_private;
10577 u32 dpll = pipe_config->dpll_hw_state.dpll;
10578
10579 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010580 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010581 else if (HAS_PCH_SPLIT(dev))
10582 return 120000;
10583 else if (!IS_GEN2(dev))
10584 return 96000;
10585 else
10586 return 48000;
10587}
10588
Jesse Barnes79e53942008-11-07 14:24:08 -080010589/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010590static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010591 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010592{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010593 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010594 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010595 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010596 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010597 u32 fp;
10598 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010599 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010600 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010601
10602 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010603 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010604 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010605 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010606
10607 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010608 if (IS_PINEVIEW(dev)) {
10609 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10610 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010611 } else {
10612 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10613 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10614 }
10615
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010616 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010617 if (IS_PINEVIEW(dev))
10618 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10619 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010620 else
10621 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010622 DPLL_FPA01_P1_POST_DIV_SHIFT);
10623
10624 switch (dpll & DPLL_MODE_MASK) {
10625 case DPLLB_MODE_DAC_SERIAL:
10626 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10627 5 : 10;
10628 break;
10629 case DPLLB_MODE_LVDS:
10630 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10631 7 : 14;
10632 break;
10633 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010634 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010635 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010636 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010637 }
10638
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010639 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010640 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010641 else
Imre Deakdccbea32015-06-22 23:35:51 +030010642 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010643 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010644 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010645 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010646
10647 if (is_lvds) {
10648 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10649 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010650
10651 if (lvds & LVDS_CLKB_POWER_UP)
10652 clock.p2 = 7;
10653 else
10654 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010655 } else {
10656 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10657 clock.p1 = 2;
10658 else {
10659 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10660 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10661 }
10662 if (dpll & PLL_P2_DIVIDE_BY_4)
10663 clock.p2 = 4;
10664 else
10665 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010666 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010667
Imre Deakdccbea32015-06-22 23:35:51 +030010668 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010669 }
10670
Ville Syrjälä18442d02013-09-13 16:00:08 +030010671 /*
10672 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010673 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010674 * encoder's get_config() function.
10675 */
Imre Deakdccbea32015-06-22 23:35:51 +030010676 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010677}
10678
Ville Syrjälä6878da02013-09-13 15:59:11 +030010679int intel_dotclock_calculate(int link_freq,
10680 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010681{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010682 /*
10683 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010684 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010685 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010686 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010687 *
10688 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010689 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010690 */
10691
Ville Syrjälä6878da02013-09-13 15:59:11 +030010692 if (!m_n->link_n)
10693 return 0;
10694
10695 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10696}
10697
Ville Syrjälä18442d02013-09-13 16:00:08 +030010698static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010699 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010700{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010701 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010702
10703 /* read out port_clock from the DPLL */
10704 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010705
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010706 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010707 * In case there is an active pipe without active ports,
10708 * we may need some idea for the dotclock anyway.
10709 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010710 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010711 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010712 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010713 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010714}
10715
10716/** Returns the currently programmed mode of the given pipe. */
10717struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10718 struct drm_crtc *crtc)
10719{
Jesse Barnes548f2452011-02-17 10:40:53 -080010720 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010721 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010722 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010723 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010724 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010725 int htot = I915_READ(HTOTAL(cpu_transcoder));
10726 int hsync = I915_READ(HSYNC(cpu_transcoder));
10727 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10728 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010729 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010730
10731 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10732 if (!mode)
10733 return NULL;
10734
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010735 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10736 if (!pipe_config) {
10737 kfree(mode);
10738 return NULL;
10739 }
10740
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010741 /*
10742 * Construct a pipe_config sufficient for getting the clock info
10743 * back out of crtc_clock_get.
10744 *
10745 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10746 * to use a real value here instead.
10747 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010748 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10749 pipe_config->pixel_multiplier = 1;
10750 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10751 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10752 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10753 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010754
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010755 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010756 mode->hdisplay = (htot & 0xffff) + 1;
10757 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10758 mode->hsync_start = (hsync & 0xffff) + 1;
10759 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10760 mode->vdisplay = (vtot & 0xffff) + 1;
10761 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10762 mode->vsync_start = (vsync & 0xffff) + 1;
10763 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10764
10765 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010766
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010767 kfree(pipe_config);
10768
Jesse Barnes79e53942008-11-07 14:24:08 -080010769 return mode;
10770}
10771
Chris Wilsonf047e392012-07-21 12:31:41 +010010772void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010773{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010774 struct drm_i915_private *dev_priv = dev->dev_private;
10775
Chris Wilsonf62a0072014-02-21 17:55:39 +000010776 if (dev_priv->mm.busy)
10777 return;
10778
Paulo Zanoni43694d62014-03-07 20:08:08 -030010779 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010780 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010781 if (INTEL_INFO(dev)->gen >= 6)
10782 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010783 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010784}
10785
10786void intel_mark_idle(struct drm_device *dev)
10787{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010788 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010789
Chris Wilsonf62a0072014-02-21 17:55:39 +000010790 if (!dev_priv->mm.busy)
10791 return;
10792
10793 dev_priv->mm.busy = false;
10794
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010795 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010796 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010797
Paulo Zanoni43694d62014-03-07 20:08:08 -030010798 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010799}
10800
Jesse Barnes79e53942008-11-07 14:24:08 -080010801static void intel_crtc_destroy(struct drm_crtc *crtc)
10802{
10803 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010804 struct drm_device *dev = crtc->dev;
10805 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010806
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010807 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010808 work = intel_crtc->unpin_work;
10809 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010810 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010811
10812 if (work) {
10813 cancel_work_sync(&work->work);
10814 kfree(work);
10815 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010816
10817 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010818
Jesse Barnes79e53942008-11-07 14:24:08 -080010819 kfree(intel_crtc);
10820}
10821
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010822static void intel_unpin_work_fn(struct work_struct *__work)
10823{
10824 struct intel_unpin_work *work =
10825 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010826 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10827 struct drm_device *dev = crtc->base.dev;
10828 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010829
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010830 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020010831 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilson05394f32010-11-08 19:18:58 +000010832 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010833
John Harrisonf06cc1b2014-11-24 18:49:37 +000010834 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010835 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010836 mutex_unlock(&dev->struct_mutex);
10837
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010838 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanoni1eb52232016-01-19 11:35:44 -020010839 intel_fbc_post_update(crtc);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010840 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010841
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010842 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10843 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010844
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010845 kfree(work);
10846}
10847
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010848static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010849 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010850{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010851 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10852 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010853 unsigned long flags;
10854
10855 /* Ignore early vblank irqs */
10856 if (intel_crtc == NULL)
10857 return;
10858
Daniel Vetterf3260382014-09-15 14:55:23 +020010859 /*
10860 * This is called both by irq handlers and the reset code (to complete
10861 * lost pageflips) so needs the full irqsave spinlocks.
10862 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010863 spin_lock_irqsave(&dev->event_lock, flags);
10864 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010865
10866 /* Ensure we don't miss a work->pending update ... */
10867 smp_rmb();
10868
10869 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010870 spin_unlock_irqrestore(&dev->event_lock, flags);
10871 return;
10872 }
10873
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010874 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010875
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010876 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010877}
10878
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010879void intel_finish_page_flip(struct drm_device *dev, int pipe)
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->pipe_to_crtc_mapping[pipe];
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
10887void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10888{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010889 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010890 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10891
Mario Kleiner49b14a52010-12-09 07:00:07 +010010892 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010893}
10894
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010895/* Is 'a' after or equal to 'b'? */
10896static bool g4x_flip_count_after_eq(u32 a, u32 b)
10897{
10898 return !((a - b) & 0x80000000);
10899}
10900
10901static bool page_flip_finished(struct intel_crtc *crtc)
10902{
10903 struct drm_device *dev = crtc->base.dev;
10904 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc19ae982016-04-13 17:35:03 +010010905 unsigned reset_counter;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010906
Chris Wilsonc19ae982016-04-13 17:35:03 +010010907 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
Chris Wilson7f1847e2016-04-13 17:35:04 +010010908 if (crtc->reset_counter != reset_counter)
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010909 return true;
10910
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010911 /*
10912 * The relevant registers doen't exist on pre-ctg.
10913 * As the flip done interrupt doesn't trigger for mmio
10914 * flips on gmch platforms, a flip count check isn't
10915 * really needed there. But since ctg has the registers,
10916 * include it in the check anyway.
10917 */
10918 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10919 return true;
10920
10921 /*
Maarten Lankhorste8861672016-02-24 11:24:26 +010010922 * BDW signals flip done immediately if the plane
10923 * is disabled, even if the plane enable is already
10924 * armed to occur at the next vblank :(
10925 */
10926
10927 /*
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010928 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10929 * used the same base address. In that case the mmio flip might
10930 * have completed, but the CS hasn't even executed the flip yet.
10931 *
10932 * A flip count check isn't enough as the CS might have updated
10933 * the base address just after start of vblank, but before we
10934 * managed to process the interrupt. This means we'd complete the
10935 * CS flip too soon.
10936 *
10937 * Combining both checks should get us a good enough result. It may
10938 * still happen that the CS flip has been executed, but has not
10939 * yet actually completed. But in case the base address is the same
10940 * anyway, we don't really care.
10941 */
10942 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10943 crtc->unpin_work->gtt_offset &&
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030010944 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010945 crtc->unpin_work->flip_count);
10946}
10947
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010948void intel_prepare_page_flip(struct drm_device *dev, int plane)
10949{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010950 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010951 struct intel_crtc *intel_crtc =
10952 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10953 unsigned long flags;
10954
Daniel Vetterf3260382014-09-15 14:55:23 +020010955
10956 /*
10957 * This is called both by irq handlers and the reset code (to complete
10958 * lost pageflips) so needs the full irqsave spinlocks.
10959 *
10960 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010961 * generate a page-flip completion irq, i.e. every modeset
10962 * is also accompanied by a spurious intel_prepare_page_flip().
10963 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010964 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010965 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010966 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010967 spin_unlock_irqrestore(&dev->event_lock, flags);
10968}
10969
Chris Wilson60426392015-10-10 10:44:32 +010010970static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010971{
10972 /* Ensure that the work item is consistent when activating it ... */
10973 smp_wmb();
Chris Wilson60426392015-10-10 10:44:32 +010010974 atomic_set(&work->pending, INTEL_FLIP_PENDING);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010975 /* and that it is marked active as soon as the irq could fire. */
10976 smp_wmb();
10977}
10978
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010979static int intel_gen2_queue_flip(struct drm_device *dev,
10980 struct drm_crtc *crtc,
10981 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010982 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010983 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010984 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010985{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000010986 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010987 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010988 u32 flip_mask;
10989 int ret;
10990
John Harrison5fb9de12015-05-29 17:44:07 +010010991 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010992 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010993 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010994
10995 /* Can't queue multiple flips, so wait for the previous
10996 * one to finish before executing the next.
10997 */
10998 if (intel_crtc->plane)
10999 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11000 else
11001 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011002 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11003 intel_ring_emit(engine, MI_NOOP);
11004 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011005 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011006 intel_ring_emit(engine, fb->pitches[0]);
11007 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11008 intel_ring_emit(engine, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000011009
Chris Wilson60426392015-10-10 10:44:32 +010011010 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011011 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011012}
11013
11014static int intel_gen3_queue_flip(struct drm_device *dev,
11015 struct drm_crtc *crtc,
11016 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011017 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011018 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011019 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011020{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011021 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011022 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011023 u32 flip_mask;
11024 int ret;
11025
John Harrison5fb9de12015-05-29 17:44:07 +010011026 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011027 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011028 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011029
11030 if (intel_crtc->plane)
11031 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11032 else
11033 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011034 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11035 intel_ring_emit(engine, MI_NOOP);
11036 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011037 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011038 intel_ring_emit(engine, fb->pitches[0]);
11039 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11040 intel_ring_emit(engine, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011041
Chris Wilson60426392015-10-10 10:44:32 +010011042 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011043 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011044}
11045
11046static int intel_gen4_queue_flip(struct drm_device *dev,
11047 struct drm_crtc *crtc,
11048 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011049 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011050 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011051 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011052{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011053 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011054 struct drm_i915_private *dev_priv = dev->dev_private;
11055 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11056 uint32_t pf, pipesrc;
11057 int ret;
11058
John Harrison5fb9de12015-05-29 17:44:07 +010011059 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011060 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011061 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011062
11063 /* i965+ uses the linear or tiled offsets from the
11064 * Display Registers (which do not change across a page-flip)
11065 * so we need only reprogram the base address.
11066 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011067 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011068 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011069 intel_ring_emit(engine, fb->pitches[0]);
11070 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011071 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011072
11073 /* XXX Enabling the panel-fitter across page-flip is so far
11074 * untested on non-native modes, so ignore it for now.
11075 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11076 */
11077 pf = 0;
11078 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011079 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011080
Chris Wilson60426392015-10-10 10:44:32 +010011081 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011082 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011083}
11084
11085static int intel_gen6_queue_flip(struct drm_device *dev,
11086 struct drm_crtc *crtc,
11087 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011088 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011089 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011090 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011091{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011092 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011093 struct drm_i915_private *dev_priv = dev->dev_private;
11094 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11095 uint32_t pf, pipesrc;
11096 int ret;
11097
John Harrison5fb9de12015-05-29 17:44:07 +010011098 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011099 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011100 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011101
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011102 intel_ring_emit(engine, MI_DISPLAY_FLIP |
Daniel Vetter6d90c952012-04-26 23:28:05 +020011103 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011104 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11105 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011106
Chris Wilson99d9acd2012-04-17 20:37:00 +010011107 /* Contrary to the suggestions in the documentation,
11108 * "Enable Panel Fitter" does not seem to be required when page
11109 * flipping with a non-native mode, and worse causes a normal
11110 * modeset to fail.
11111 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11112 */
11113 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011114 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011115 intel_ring_emit(engine, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011116
Chris Wilson60426392015-10-10 10:44:32 +010011117 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011118 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011119}
11120
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011121static int intel_gen7_queue_flip(struct drm_device *dev,
11122 struct drm_crtc *crtc,
11123 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011124 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011125 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011126 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011127{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011128 struct intel_engine_cs *engine = req->engine;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011129 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011130 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011131 int len, ret;
11132
Robin Schroereba905b2014-05-18 02:24:50 +020011133 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011134 case PLANE_A:
11135 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11136 break;
11137 case PLANE_B:
11138 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11139 break;
11140 case PLANE_C:
11141 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11142 break;
11143 default:
11144 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011145 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011146 }
11147
Chris Wilsonffe74d72013-08-26 20:58:12 +010011148 len = 4;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011149 if (engine->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011150 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011151 /*
11152 * On Gen 8, SRM is now taking an extra dword to accommodate
11153 * 48bits addresses, and we need a NOOP for the batch size to
11154 * stay even.
11155 */
11156 if (IS_GEN8(dev))
11157 len += 2;
11158 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011159
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011160 /*
11161 * BSpec MI_DISPLAY_FLIP for IVB:
11162 * "The full packet must be contained within the same cache line."
11163 *
11164 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11165 * cacheline, if we ever start emitting more commands before
11166 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11167 * then do the cacheline alignment, and finally emit the
11168 * MI_DISPLAY_FLIP.
11169 */
John Harrisonbba09b12015-05-29 17:44:06 +010011170 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011171 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011172 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011173
John Harrison5fb9de12015-05-29 17:44:07 +010011174 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011175 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011176 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011177
Chris Wilsonffe74d72013-08-26 20:58:12 +010011178 /* Unmask the flip-done completion message. Note that the bspec says that
11179 * we should do this for both the BCS and RCS, and that we must not unmask
11180 * more than one flip event at any time (or ensure that one flip message
11181 * can be sent by waiting for flip-done prior to queueing new flips).
11182 * Experimentation says that BCS works despite DERRMR masking all
11183 * flip-done completion events and that unmasking all planes at once
11184 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11185 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11186 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011187 if (engine->id == RCS) {
11188 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11189 intel_ring_emit_reg(engine, DERRMR);
11190 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11191 DERRMR_PIPEB_PRI_FLIP_DONE |
11192 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011193 if (IS_GEN8(dev))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011194 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011195 MI_SRM_LRM_GLOBAL_GTT);
11196 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011197 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011198 MI_SRM_LRM_GLOBAL_GTT);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011199 intel_ring_emit_reg(engine, DERRMR);
11200 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011201 if (IS_GEN8(dev)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011202 intel_ring_emit(engine, 0);
11203 intel_ring_emit(engine, MI_NOOP);
Damien Lespiauf4768282014-04-07 20:24:34 +010011204 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011205 }
11206
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011207 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11208 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11209 intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
11210 intel_ring_emit(engine, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011211
Chris Wilson60426392015-10-10 10:44:32 +010011212 intel_mark_page_flip_active(intel_crtc->unpin_work);
Chris Wilson83d40922012-04-17 19:35:53 +010011213 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011214}
11215
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011216static bool use_mmio_flip(struct intel_engine_cs *engine,
Sourab Gupta84c33a62014-06-02 16:47:17 +053011217 struct drm_i915_gem_object *obj)
11218{
11219 /*
11220 * This is not being used for older platforms, because
11221 * non-availability of flip done interrupt forces us to use
11222 * CS flips. Older platforms derive flip done using some clever
11223 * tricks involving the flip_pending status bits and vblank irqs.
11224 * So using MMIO flips there would disrupt this mechanism.
11225 */
11226
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011227 if (engine == NULL)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011228 return true;
11229
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000011230 if (INTEL_INFO(engine->dev)->gen < 5)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011231 return false;
11232
11233 if (i915.use_mmio_flip < 0)
11234 return false;
11235 else if (i915.use_mmio_flip > 0)
11236 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011237 else if (i915.enable_execlists)
11238 return true;
Alex Goinsfd8e0582015-11-25 18:43:38 -080011239 else if (obj->base.dma_buf &&
11240 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11241 false))
11242 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011243 else
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011244 return engine != i915_gem_request_get_engine(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011245}
11246
Chris Wilson60426392015-10-10 10:44:32 +010011247static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011248 unsigned int rotation,
Chris Wilson60426392015-10-10 10:44:32 +010011249 struct intel_unpin_work *work)
Damien Lespiauff944562014-11-20 14:58:16 +000011250{
11251 struct drm_device *dev = intel_crtc->base.dev;
11252 struct drm_i915_private *dev_priv = dev->dev_private;
11253 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011254 const enum pipe pipe = intel_crtc->pipe;
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011255 u32 ctl, stride, tile_height;
Damien Lespiauff944562014-11-20 14:58:16 +000011256
11257 ctl = I915_READ(PLANE_CTL(pipe, 0));
11258 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011259 switch (fb->modifier[0]) {
11260 case DRM_FORMAT_MOD_NONE:
11261 break;
11262 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011263 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011264 break;
11265 case I915_FORMAT_MOD_Y_TILED:
11266 ctl |= PLANE_CTL_TILED_Y;
11267 break;
11268 case I915_FORMAT_MOD_Yf_TILED:
11269 ctl |= PLANE_CTL_TILED_YF;
11270 break;
11271 default:
11272 MISSING_CASE(fb->modifier[0]);
11273 }
Damien Lespiauff944562014-11-20 14:58:16 +000011274
11275 /*
11276 * The stride is either expressed as a multiple of 64 bytes chunks for
11277 * linear buffers or in number of tiles for tiled buffers.
11278 */
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011279 if (intel_rotation_90_or_270(rotation)) {
11280 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +020011281 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011282 stride = DIV_ROUND_UP(fb->height, tile_height);
11283 } else {
11284 stride = fb->pitches[0] /
Ville Syrjälä7b49f942016-01-12 21:08:32 +020011285 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11286 fb->pixel_format);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011287 }
Damien Lespiauff944562014-11-20 14:58:16 +000011288
11289 /*
11290 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11291 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11292 */
11293 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11294 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11295
Chris Wilson60426392015-10-10 10:44:32 +010011296 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
Damien Lespiauff944562014-11-20 14:58:16 +000011297 POSTING_READ(PLANE_SURF(pipe, 0));
11298}
11299
Chris Wilson60426392015-10-10 10:44:32 +010011300static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11301 struct intel_unpin_work *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011302{
11303 struct drm_device *dev = intel_crtc->base.dev;
11304 struct drm_i915_private *dev_priv = dev->dev_private;
11305 struct intel_framebuffer *intel_fb =
11306 to_intel_framebuffer(intel_crtc->base.primary->fb);
11307 struct drm_i915_gem_object *obj = intel_fb->obj;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020011308 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011309 u32 dspcntr;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011310
Sourab Gupta84c33a62014-06-02 16:47:17 +053011311 dspcntr = I915_READ(reg);
11312
Damien Lespiauc5d97472014-10-25 00:11:11 +010011313 if (obj->tiling_mode != I915_TILING_NONE)
11314 dspcntr |= DISPPLANE_TILED;
11315 else
11316 dspcntr &= ~DISPPLANE_TILED;
11317
Sourab Gupta84c33a62014-06-02 16:47:17 +053011318 I915_WRITE(reg, dspcntr);
11319
Chris Wilson60426392015-10-10 10:44:32 +010011320 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011321 POSTING_READ(DSPSURF(intel_crtc->plane));
Damien Lespiauff944562014-11-20 14:58:16 +000011322}
11323
11324/*
11325 * XXX: This is the temporary way to update the plane registers until we get
11326 * around to using the usual plane update functions for MMIO flips
11327 */
Chris Wilson60426392015-10-10 10:44:32 +010011328static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
Damien Lespiauff944562014-11-20 14:58:16 +000011329{
Chris Wilson60426392015-10-10 10:44:32 +010011330 struct intel_crtc *crtc = mmio_flip->crtc;
11331 struct intel_unpin_work *work;
Damien Lespiauff944562014-11-20 14:58:16 +000011332
Chris Wilson60426392015-10-10 10:44:32 +010011333 spin_lock_irq(&crtc->base.dev->event_lock);
11334 work = crtc->unpin_work;
11335 spin_unlock_irq(&crtc->base.dev->event_lock);
11336 if (work == NULL)
11337 return;
Damien Lespiauff944562014-11-20 14:58:16 +000011338
Chris Wilson60426392015-10-10 10:44:32 +010011339 intel_mark_page_flip_active(work);
Damien Lespiauff944562014-11-20 14:58:16 +000011340
Chris Wilson60426392015-10-10 10:44:32 +010011341 intel_pipe_update_start(crtc);
11342
11343 if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011344 skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011345 else
11346 /* use_mmio_flip() retricts MMIO flips to ilk+ */
Chris Wilson60426392015-10-10 10:44:32 +010011347 ilk_do_mmio_flip(crtc, work);
Damien Lespiauff944562014-11-20 14:58:16 +000011348
Chris Wilson60426392015-10-10 10:44:32 +010011349 intel_pipe_update_end(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011350}
11351
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011352static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011353{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011354 struct intel_mmio_flip *mmio_flip =
11355 container_of(work, struct intel_mmio_flip, work);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011356 struct intel_framebuffer *intel_fb =
11357 to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
11358 struct drm_i915_gem_object *obj = intel_fb->obj;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011359
Chris Wilson60426392015-10-10 10:44:32 +010011360 if (mmio_flip->req) {
Daniel Vettereed29a52015-05-21 14:21:25 +020011361 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011362 false, NULL,
11363 &mmio_flip->i915->rps.mmioflips));
Chris Wilson60426392015-10-10 10:44:32 +010011364 i915_gem_request_unreference__unlocked(mmio_flip->req);
11365 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011366
Alex Goinsfd8e0582015-11-25 18:43:38 -080011367 /* For framebuffer backed by dmabuf, wait for fence */
11368 if (obj->base.dma_buf)
11369 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11370 false, false,
11371 MAX_SCHEDULE_TIMEOUT) < 0);
11372
Chris Wilson60426392015-10-10 10:44:32 +010011373 intel_do_mmio_flip(mmio_flip);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011374 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011375}
11376
11377static int intel_queue_mmio_flip(struct drm_device *dev,
11378 struct drm_crtc *crtc,
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011379 struct drm_i915_gem_object *obj)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011380{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011381 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011382
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011383 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11384 if (mmio_flip == NULL)
11385 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011386
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011387 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011388 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011389 mmio_flip->crtc = to_intel_crtc(crtc);
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011390 mmio_flip->rotation = crtc->primary->state->rotation;
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011391
11392 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11393 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011394
Sourab Gupta84c33a62014-06-02 16:47:17 +053011395 return 0;
11396}
11397
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011398static int intel_default_queue_flip(struct drm_device *dev,
11399 struct drm_crtc *crtc,
11400 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011401 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011402 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011403 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011404{
11405 return -ENODEV;
11406}
11407
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011408static bool __intel_pageflip_stall_check(struct drm_device *dev,
11409 struct drm_crtc *crtc)
11410{
11411 struct drm_i915_private *dev_priv = dev->dev_private;
11412 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11413 struct intel_unpin_work *work = intel_crtc->unpin_work;
11414 u32 addr;
11415
11416 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11417 return true;
11418
Chris Wilson908565c2015-08-12 13:08:22 +010011419 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11420 return false;
11421
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011422 if (!work->enable_stall_check)
11423 return false;
11424
11425 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011426 if (work->flip_queued_req &&
11427 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011428 return false;
11429
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011430 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011431 }
11432
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011433 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011434 return false;
11435
11436 /* Potential stall - if we see that the flip has happened,
11437 * assume a missed interrupt. */
11438 if (INTEL_INFO(dev)->gen >= 4)
11439 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11440 else
11441 addr = I915_READ(DSPADDR(intel_crtc->plane));
11442
11443 /* There is a potential issue here with a false positive after a flip
11444 * to the same address. We could address this by checking for a
11445 * non-incrementing frame counter.
11446 */
11447 return addr == work->gtt_offset;
11448}
11449
11450void intel_check_page_flip(struct drm_device *dev, int pipe)
11451{
11452 struct drm_i915_private *dev_priv = dev->dev_private;
11453 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11454 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011455 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011456
Dave Gordon6c51d462015-03-06 15:34:26 +000011457 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011458
11459 if (crtc == NULL)
11460 return;
11461
Daniel Vetterf3260382014-09-15 14:55:23 +020011462 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011463 work = intel_crtc->unpin_work;
11464 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011465 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011466 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011467 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011468 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011469 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011470 if (work != NULL &&
11471 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11472 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011473 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011474}
11475
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011476static int intel_crtc_page_flip(struct drm_crtc *crtc,
11477 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011478 struct drm_pending_vblank_event *event,
11479 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011480{
11481 struct drm_device *dev = crtc->dev;
11482 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011483 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011484 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011485 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011486 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011487 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011488 struct intel_unpin_work *work;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011489 struct intel_engine_cs *engine;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011490 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011491 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011492 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011493
Matt Roper2ff8fde2014-07-08 07:50:07 -070011494 /*
11495 * drm_mode_page_flip_ioctl() should already catch this, but double
11496 * check to be safe. In the future we may enable pageflipping from
11497 * a disabled primary plane.
11498 */
11499 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11500 return -EBUSY;
11501
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011502 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011503 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011504 return -EINVAL;
11505
11506 /*
11507 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11508 * Note that pitch changes could also affect these register.
11509 */
11510 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011511 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11512 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011513 return -EINVAL;
11514
Chris Wilsonf900db42014-02-20 09:26:13 +000011515 if (i915_terminally_wedged(&dev_priv->gpu_error))
11516 goto out_hang;
11517
Daniel Vetterb14c5672013-09-19 12:18:32 +020011518 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011519 if (work == NULL)
11520 return -ENOMEM;
11521
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011522 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011523 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011524 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011525 INIT_WORK(&work->work, intel_unpin_work_fn);
11526
Daniel Vetter87b6b102014-05-15 15:33:46 +020011527 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011528 if (ret)
11529 goto free_work;
11530
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011531 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011532 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011533 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011534 /* Before declaring the flip queue wedged, check if
11535 * the hardware completed the operation behind our backs.
11536 */
11537 if (__intel_pageflip_stall_check(dev, crtc)) {
11538 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11539 page_flip_completed(intel_crtc);
11540 } else {
11541 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011542 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011543
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011544 drm_crtc_vblank_put(crtc);
11545 kfree(work);
11546 return -EBUSY;
11547 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011548 }
11549 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011550 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011551
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011552 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11553 flush_workqueue(dev_priv->wq);
11554
Jesse Barnes75dfca82010-02-10 15:09:44 -080011555 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011556 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011557 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011558
Matt Roperf4510a22014-04-01 15:22:40 -070011559 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011560 update_state_fb(crtc->primary);
Paulo Zanonie8216e52016-01-19 11:35:56 -020011561 intel_fbc_pre_update(intel_crtc);
Matt Roper1ed1f962015-01-30 16:22:36 -080011562
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011563 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011564
Chris Wilson89ed88b2015-02-16 14:31:49 +000011565 ret = i915_mutex_lock_interruptible(dev);
11566 if (ret)
11567 goto cleanup;
11568
Chris Wilsonc19ae982016-04-13 17:35:03 +010011569 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
Chris Wilson7f1847e2016-04-13 17:35:04 +010011570 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11571 ret = -EIO;
11572 goto cleanup;
11573 }
11574
11575 atomic_inc(&intel_crtc->unpin_work_count);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011576
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011577 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Ville Syrjäläfd8f507c2015-09-18 20:03:42 +030011578 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011579
Wayne Boyer666a4532015-12-09 12:29:35 -080011580 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011581 engine = &dev_priv->engine[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011582 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011583 /* vlv: DISPLAY_FLIP fails to change tiling */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011584 engine = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011585 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011586 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011587 } else if (INTEL_INFO(dev)->gen >= 7) {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000011588 engine = i915_gem_request_get_engine(obj->last_write_req);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011589 if (engine == NULL || engine->id != RCS)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011590 engine = &dev_priv->engine[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011591 } else {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000011592 engine = &dev_priv->engine[RCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011593 }
11594
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011595 mmio_flip = use_mmio_flip(engine, obj);
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011596
11597 /* When using CS flips, we want to emit semaphores between rings.
11598 * However, when using mmio flips we will create a task to do the
11599 * synchronisation, so all we want here is to pin the framebuffer
11600 * into the display plane and skip any waits.
11601 */
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011602 if (!mmio_flip) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011603 ret = i915_gem_object_sync(obj, engine, &request);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020011604 if (ret)
11605 goto cleanup_pending;
11606 }
11607
Ville Syrjälä3465c582016-02-15 22:54:43 +020011608 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011609 if (ret)
11610 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011611
Tvrtko Ursulindedf2782015-09-21 10:45:35 +010011612 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11613 obj, 0);
11614 work->gtt_offset += intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011615
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011616 if (mmio_flip) {
Tvrtko Ursulin86efe242015-10-20 16:20:21 +010011617 ret = intel_queue_mmio_flip(dev, crtc, obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011618 if (ret)
11619 goto cleanup_unpin;
11620
John Harrisonf06cc1b2014-11-24 18:49:37 +000011621 i915_gem_request_assign(&work->flip_queued_req,
11622 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011623 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011624 if (!request) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000011625 request = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +000011626 if (IS_ERR(request)) {
11627 ret = PTR_ERR(request);
John Harrison6258fbe2015-05-29 17:43:48 +010011628 goto cleanup_unpin;
Dave Gordon26827082016-01-19 19:02:53 +000011629 }
John Harrison6258fbe2015-05-29 17:43:48 +010011630 }
11631
11632 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011633 page_flip_flags);
11634 if (ret)
11635 goto cleanup_unpin;
11636
John Harrison6258fbe2015-05-29 17:43:48 +010011637 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011638 }
11639
John Harrison91af1272015-06-18 13:14:56 +010011640 if (request)
John Harrison75289872015-05-29 17:43:49 +010011641 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011642
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011643 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011644 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011645
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011646 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011647 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011648 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011649
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011650 intel_frontbuffer_flip_prepare(dev,
11651 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011652
Jesse Barnese5510fa2010-07-01 16:48:37 -070011653 trace_i915_flip_request(intel_crtc->plane, obj);
11654
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011655 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011656
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011657cleanup_unpin:
Ville Syrjälä3465c582016-02-15 22:54:43 +020011658 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011659cleanup_pending:
Dave Gordon0aa498d2016-01-28 10:48:09 +000011660 if (!IS_ERR_OR_NULL(request))
Chris Wilsonaa9b7812016-04-13 17:35:15 +010011661 i915_add_request_no_flush(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011662 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011663 mutex_unlock(&dev->struct_mutex);
11664cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011665 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011666 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011667
Chris Wilson89ed88b2015-02-16 14:31:49 +000011668 drm_gem_object_unreference_unlocked(&obj->base);
11669 drm_framebuffer_unreference(work->old_fb);
11670
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011671 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011672 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011673 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011674
Daniel Vetter87b6b102014-05-15 15:33:46 +020011675 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011676free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011677 kfree(work);
11678
Chris Wilsonf900db42014-02-20 09:26:13 +000011679 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011680 struct drm_atomic_state *state;
11681 struct drm_plane_state *plane_state;
11682
Chris Wilsonf900db42014-02-20 09:26:13 +000011683out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011684 state = drm_atomic_state_alloc(dev);
11685 if (!state)
11686 return -ENOMEM;
11687 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11688
11689retry:
11690 plane_state = drm_atomic_get_plane_state(state, primary);
11691 ret = PTR_ERR_OR_ZERO(plane_state);
11692 if (!ret) {
11693 drm_atomic_set_fb_for_plane(plane_state, fb);
11694
11695 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11696 if (!ret)
11697 ret = drm_atomic_commit(state);
11698 }
11699
11700 if (ret == -EDEADLK) {
11701 drm_modeset_backoff(state->acquire_ctx);
11702 drm_atomic_state_clear(state);
11703 goto retry;
11704 }
11705
11706 if (ret)
11707 drm_atomic_state_free(state);
11708
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011709 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011710 spin_lock_irq(&dev->event_lock);
Gustavo Padovan560ce1d2016-04-14 10:48:15 -070011711 drm_crtc_send_vblank_event(crtc, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011712 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011713 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011714 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011715 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011716}
11717
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011718
11719/**
11720 * intel_wm_need_update - Check whether watermarks need updating
11721 * @plane: drm plane
11722 * @state: new plane state
11723 *
11724 * Check current plane state versus the new one to determine whether
11725 * watermarks need to be recalculated.
11726 *
11727 * Returns true or false.
11728 */
11729static bool intel_wm_need_update(struct drm_plane *plane,
11730 struct drm_plane_state *state)
11731{
Matt Roperd21fbe82015-09-24 15:53:12 -070011732 struct intel_plane_state *new = to_intel_plane_state(state);
11733 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11734
11735 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011736 if (new->visible != cur->visible)
11737 return true;
11738
11739 if (!cur->base.fb || !new->base.fb)
11740 return false;
11741
11742 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11743 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011744 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11745 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11746 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11747 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011748 return true;
11749
11750 return false;
11751}
11752
Matt Roperd21fbe82015-09-24 15:53:12 -070011753static bool needs_scaling(struct intel_plane_state *state)
11754{
11755 int src_w = drm_rect_width(&state->src) >> 16;
11756 int src_h = drm_rect_height(&state->src) >> 16;
11757 int dst_w = drm_rect_width(&state->dst);
11758 int dst_h = drm_rect_height(&state->dst);
11759
11760 return (src_w != dst_w || src_h != dst_h);
11761}
11762
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011763int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11764 struct drm_plane_state *plane_state)
11765{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011766 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011767 struct drm_crtc *crtc = crtc_state->crtc;
11768 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11769 struct drm_plane *plane = plane_state->plane;
11770 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011771 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011772 struct intel_plane_state *old_plane_state =
11773 to_intel_plane_state(plane->state);
11774 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011775 bool mode_changed = needs_modeset(crtc_state);
11776 bool was_crtc_enabled = crtc->state->active;
11777 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011778 bool turn_off, turn_on, visible, was_visible;
11779 struct drm_framebuffer *fb = plane_state->fb;
11780
11781 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11782 plane->type != DRM_PLANE_TYPE_CURSOR) {
11783 ret = skl_update_scaler_plane(
11784 to_intel_crtc_state(crtc_state),
11785 to_intel_plane_state(plane_state));
11786 if (ret)
11787 return ret;
11788 }
11789
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011790 was_visible = old_plane_state->visible;
11791 visible = to_intel_plane_state(plane_state)->visible;
11792
11793 if (!was_crtc_enabled && WARN_ON(was_visible))
11794 was_visible = false;
11795
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011796 /*
11797 * Visibility is calculated as if the crtc was on, but
11798 * after scaler setup everything depends on it being off
11799 * when the crtc isn't active.
11800 */
11801 if (!is_crtc_enabled)
11802 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011803
11804 if (!was_visible && !visible)
11805 return 0;
11806
Maarten Lankhorste8861672016-02-24 11:24:26 +010011807 if (fb != old_plane_state->base.fb)
11808 pipe_config->fb_changed = true;
11809
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011810 turn_off = was_visible && (!visible || mode_changed);
11811 turn_on = visible && (!was_visible || mode_changed);
11812
11813 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11814 plane->base.id, fb ? fb->base.id : -1);
11815
11816 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11817 plane->base.id, was_visible, visible,
11818 turn_off, turn_on, mode_changed);
11819
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011820 if (turn_on) {
11821 pipe_config->update_wm_pre = true;
11822
11823 /* must disable cxsr around plane enable/disable */
11824 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11825 pipe_config->disable_cxsr = true;
11826 } else if (turn_off) {
11827 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011828
Ville Syrjälä852eb002015-06-24 22:00:07 +030011829 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011830 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011831 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011832 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011833 /* FIXME bollocks */
11834 pipe_config->update_wm_pre = true;
11835 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011836 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011837
Matt Ropered4a6a72016-02-23 17:20:13 -080011838 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011839 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11840 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011841 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11842
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011843 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011844 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011845
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011846 /*
11847 * WaCxSRDisabledForSpriteScaling:ivb
11848 *
11849 * cstate->update_wm was already set above, so this flag will
11850 * take effect when we commit and program watermarks.
11851 */
11852 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11853 needs_scaling(to_intel_plane_state(plane_state)) &&
11854 !needs_scaling(old_plane_state))
11855 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011856
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011857 return 0;
11858}
11859
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011860static bool encoders_cloneable(const struct intel_encoder *a,
11861 const struct intel_encoder *b)
11862{
11863 /* masks could be asymmetric, so check both ways */
11864 return a == b || (a->cloneable & (1 << b->type) &&
11865 b->cloneable & (1 << a->type));
11866}
11867
11868static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11869 struct intel_crtc *crtc,
11870 struct intel_encoder *encoder)
11871{
11872 struct intel_encoder *source_encoder;
11873 struct drm_connector *connector;
11874 struct drm_connector_state *connector_state;
11875 int i;
11876
11877 for_each_connector_in_state(state, connector, connector_state, i) {
11878 if (connector_state->crtc != &crtc->base)
11879 continue;
11880
11881 source_encoder =
11882 to_intel_encoder(connector_state->best_encoder);
11883 if (!encoders_cloneable(encoder, source_encoder))
11884 return false;
11885 }
11886
11887 return true;
11888}
11889
11890static bool check_encoder_cloning(struct drm_atomic_state *state,
11891 struct intel_crtc *crtc)
11892{
11893 struct intel_encoder *encoder;
11894 struct drm_connector *connector;
11895 struct drm_connector_state *connector_state;
11896 int i;
11897
11898 for_each_connector_in_state(state, connector, connector_state, i) {
11899 if (connector_state->crtc != &crtc->base)
11900 continue;
11901
11902 encoder = to_intel_encoder(connector_state->best_encoder);
11903 if (!check_single_encoder_cloning(state, crtc, encoder))
11904 return false;
11905 }
11906
11907 return true;
11908}
11909
11910static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11911 struct drm_crtc_state *crtc_state)
11912{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011913 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011914 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011915 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011916 struct intel_crtc_state *pipe_config =
11917 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011918 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011919 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011920 bool mode_changed = needs_modeset(crtc_state);
11921
11922 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11923 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11924 return -EINVAL;
11925 }
11926
Ville Syrjälä852eb002015-06-24 22:00:07 +030011927 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011928 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011929
Maarten Lankhorstad421372015-06-15 12:33:42 +020011930 if (mode_changed && crtc_state->enable &&
11931 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011932 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020011933 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11934 pipe_config);
11935 if (ret)
11936 return ret;
11937 }
11938
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000011939 if (crtc_state->color_mgmt_changed) {
11940 ret = intel_color_check(crtc, crtc_state);
11941 if (ret)
11942 return ret;
11943 }
11944
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011945 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070011946 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010011947 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080011948 if (ret) {
11949 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070011950 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080011951 }
11952 }
11953
11954 if (dev_priv->display.compute_intermediate_wm &&
11955 !to_intel_atomic_state(state)->skip_intermediate_wm) {
11956 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11957 return 0;
11958
11959 /*
11960 * Calculate 'intermediate' watermarks that satisfy both the
11961 * old state and the new state. We can program these
11962 * immediately.
11963 */
11964 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
11965 intel_crtc,
11966 pipe_config);
11967 if (ret) {
11968 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
11969 return ret;
11970 }
Matt Roper86c8bbb2015-09-24 15:53:16 -070011971 }
11972
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011973 if (INTEL_INFO(dev)->gen >= 9) {
11974 if (mode_changed)
11975 ret = skl_update_scaler_crtc(pipe_config);
11976
11977 if (!ret)
11978 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11979 pipe_config);
11980 }
11981
11982 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011983}
11984
Jani Nikula65b38e02015-04-13 11:26:56 +030011985static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011986 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Matt Roperea2c67b2014-12-23 10:41:52 -080011987 .atomic_begin = intel_begin_crtc_commit,
11988 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011989 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011990};
11991
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011992static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11993{
11994 struct intel_connector *connector;
11995
11996 for_each_intel_connector(dev, connector) {
11997 if (connector->base.encoder) {
11998 connector->base.state->best_encoder =
11999 connector->base.encoder;
12000 connector->base.state->crtc =
12001 connector->base.encoder->crtc;
12002 } else {
12003 connector->base.state->best_encoder = NULL;
12004 connector->base.state->crtc = NULL;
12005 }
12006 }
12007}
12008
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012009static void
Robin Schroereba905b2014-05-18 02:24:50 +020012010connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012011 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012012{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012013 int bpp = pipe_config->pipe_bpp;
12014
12015 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12016 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012017 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012018
12019 /* Don't use an invalid EDID bpc value */
12020 if (connector->base.display_info.bpc &&
12021 connector->base.display_info.bpc * 3 < bpp) {
12022 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12023 bpp, connector->base.display_info.bpc*3);
12024 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12025 }
12026
Jani Nikula013dd9e2016-01-13 16:35:20 +020012027 /* Clamp bpp to default limit on screens without EDID 1.4 */
12028 if (connector->base.display_info.bpc == 0) {
12029 int type = connector->base.connector_type;
12030 int clamp_bpp = 24;
12031
12032 /* Fall back to 18 bpp when DP sink capability is unknown. */
12033 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12034 type == DRM_MODE_CONNECTOR_eDP)
12035 clamp_bpp = 18;
12036
12037 if (bpp > clamp_bpp) {
12038 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12039 bpp, clamp_bpp);
12040 pipe_config->pipe_bpp = clamp_bpp;
12041 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012042 }
12043}
12044
12045static int
12046compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012047 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012048{
12049 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012050 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012051 struct drm_connector *connector;
12052 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012053 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012054
Wayne Boyer666a4532015-12-09 12:29:35 -080012055 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012056 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012057 else if (INTEL_INFO(dev)->gen >= 5)
12058 bpp = 12*3;
12059 else
12060 bpp = 8*3;
12061
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012062
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012063 pipe_config->pipe_bpp = bpp;
12064
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012065 state = pipe_config->base.state;
12066
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012067 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012068 for_each_connector_in_state(state, connector, connector_state, i) {
12069 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012070 continue;
12071
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012072 connected_sink_compute_bpp(to_intel_connector(connector),
12073 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012074 }
12075
12076 return bpp;
12077}
12078
Daniel Vetter644db712013-09-19 14:53:58 +020012079static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12080{
12081 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12082 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012083 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012084 mode->crtc_hdisplay, mode->crtc_hsync_start,
12085 mode->crtc_hsync_end, mode->crtc_htotal,
12086 mode->crtc_vdisplay, mode->crtc_vsync_start,
12087 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12088}
12089
Daniel Vetterc0b03412013-05-28 12:05:54 +020012090static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012091 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012092 const char *context)
12093{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012094 struct drm_device *dev = crtc->base.dev;
12095 struct drm_plane *plane;
12096 struct intel_plane *intel_plane;
12097 struct intel_plane_state *state;
12098 struct drm_framebuffer *fb;
12099
12100 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12101 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012102
Jani Nikulada205632016-03-15 21:51:10 +020012103 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012104 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12105 pipe_config->pipe_bpp, pipe_config->dither);
12106 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12107 pipe_config->has_pch_encoder,
12108 pipe_config->fdi_lanes,
12109 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12110 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12111 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012112 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 +030012113 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012114 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012115 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12116 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12117 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012118
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012119 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 -070012120 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012121 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012122 pipe_config->dp_m2_n2.gmch_m,
12123 pipe_config->dp_m2_n2.gmch_n,
12124 pipe_config->dp_m2_n2.link_m,
12125 pipe_config->dp_m2_n2.link_n,
12126 pipe_config->dp_m2_n2.tu);
12127
Daniel Vetter55072d12014-11-20 16:10:28 +010012128 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12129 pipe_config->has_audio,
12130 pipe_config->has_infoframe);
12131
Daniel Vetterc0b03412013-05-28 12:05:54 +020012132 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012133 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012134 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012135 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12136 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012137 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012138 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12139 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012140 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12141 crtc->num_scalers,
12142 pipe_config->scaler_state.scaler_users,
12143 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012144 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12145 pipe_config->gmch_pfit.control,
12146 pipe_config->gmch_pfit.pgm_ratios,
12147 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012148 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012149 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012150 pipe_config->pch_pfit.size,
12151 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012152 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012153 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012154
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012155 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012156 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012157 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012158 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012159 pipe_config->ddi_pll_sel,
12160 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012161 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012162 pipe_config->dpll_hw_state.pll0,
12163 pipe_config->dpll_hw_state.pll1,
12164 pipe_config->dpll_hw_state.pll2,
12165 pipe_config->dpll_hw_state.pll3,
12166 pipe_config->dpll_hw_state.pll6,
12167 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012168 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012169 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012170 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012171 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012172 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12173 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12174 pipe_config->ddi_pll_sel,
12175 pipe_config->dpll_hw_state.ctrl1,
12176 pipe_config->dpll_hw_state.cfgcr1,
12177 pipe_config->dpll_hw_state.cfgcr2);
12178 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012179 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 +010012180 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012181 pipe_config->dpll_hw_state.wrpll,
12182 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012183 } else {
12184 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12185 "fp0: 0x%x, fp1: 0x%x\n",
12186 pipe_config->dpll_hw_state.dpll,
12187 pipe_config->dpll_hw_state.dpll_md,
12188 pipe_config->dpll_hw_state.fp0,
12189 pipe_config->dpll_hw_state.fp1);
12190 }
12191
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012192 DRM_DEBUG_KMS("planes on this crtc\n");
12193 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12194 intel_plane = to_intel_plane(plane);
12195 if (intel_plane->pipe != crtc->pipe)
12196 continue;
12197
12198 state = to_intel_plane_state(plane->state);
12199 fb = state->base.fb;
12200 if (!fb) {
12201 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12202 "disabled, scaler_id = %d\n",
12203 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12204 plane->base.id, intel_plane->pipe,
12205 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12206 drm_plane_index(plane), state->scaler_id);
12207 continue;
12208 }
12209
12210 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12211 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12212 plane->base.id, intel_plane->pipe,
12213 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12214 drm_plane_index(plane));
12215 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12216 fb->base.id, fb->width, fb->height, fb->pixel_format);
12217 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12218 state->scaler_id,
12219 state->src.x1 >> 16, state->src.y1 >> 16,
12220 drm_rect_width(&state->src) >> 16,
12221 drm_rect_height(&state->src) >> 16,
12222 state->dst.x1, state->dst.y1,
12223 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12224 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012225}
12226
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012227static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012228{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012229 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012230 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012231 unsigned int used_ports = 0;
12232
12233 /*
12234 * Walk the connector list instead of the encoder
12235 * list to detect the problem on ddi platforms
12236 * where there's just one encoder per digital port.
12237 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012238 drm_for_each_connector(connector, dev) {
12239 struct drm_connector_state *connector_state;
12240 struct intel_encoder *encoder;
12241
12242 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12243 if (!connector_state)
12244 connector_state = connector->state;
12245
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012246 if (!connector_state->best_encoder)
12247 continue;
12248
12249 encoder = to_intel_encoder(connector_state->best_encoder);
12250
12251 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012252
12253 switch (encoder->type) {
12254 unsigned int port_mask;
12255 case INTEL_OUTPUT_UNKNOWN:
12256 if (WARN_ON(!HAS_DDI(dev)))
12257 break;
12258 case INTEL_OUTPUT_DISPLAYPORT:
12259 case INTEL_OUTPUT_HDMI:
12260 case INTEL_OUTPUT_EDP:
12261 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12262
12263 /* the same port mustn't appear more than once */
12264 if (used_ports & port_mask)
12265 return false;
12266
12267 used_ports |= port_mask;
12268 default:
12269 break;
12270 }
12271 }
12272
12273 return true;
12274}
12275
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012276static void
12277clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12278{
12279 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012280 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012281 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012282 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012283 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012284 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012285
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012286 /* FIXME: before the switch to atomic started, a new pipe_config was
12287 * kzalloc'd. Code that depends on any field being zero should be
12288 * fixed, so that the crtc_state can be safely duplicated. For now,
12289 * only fields that are know to not cause problems are preserved. */
12290
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012291 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012292 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012293 shared_dpll = crtc_state->shared_dpll;
12294 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012295 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012296 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012297
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012298 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012299
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012300 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012301 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012302 crtc_state->shared_dpll = shared_dpll;
12303 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012304 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012305 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012306}
12307
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012308static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012309intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012310 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012311{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012312 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012313 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012314 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012315 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012316 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012317 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012318 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012319
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012320 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012321
Daniel Vettere143a212013-07-04 12:01:15 +020012322 pipe_config->cpu_transcoder =
12323 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012324
Imre Deak2960bc92013-07-30 13:36:32 +030012325 /*
12326 * Sanitize sync polarity flags based on requested ones. If neither
12327 * positive or negative polarity is requested, treat this as meaning
12328 * negative polarity.
12329 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012330 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012331 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012332 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012333
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012334 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012335 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012336 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012337
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012338 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12339 pipe_config);
12340 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012341 goto fail;
12342
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012343 /*
12344 * Determine the real pipe dimensions. Note that stereo modes can
12345 * increase the actual pipe size due to the frame doubling and
12346 * insertion of additional space for blanks between the frame. This
12347 * is stored in the crtc timings. We use the requested mode to do this
12348 * computation to clearly distinguish it from the adjusted mode, which
12349 * can be changed by the connectors in the below retry loop.
12350 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012351 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012352 &pipe_config->pipe_src_w,
12353 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012354
Daniel Vettere29c22c2013-02-21 00:00:16 +010012355encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012356 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012357 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012358 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012359
Daniel Vetter135c81b2013-07-21 21:37:09 +020012360 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012361 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12362 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012363
Daniel Vetter7758a112012-07-08 19:40:39 +020012364 /* Pass our mode to the connectors and the CRTC to give them a chance to
12365 * adjust it according to limitations or connector properties, and also
12366 * a chance to reject the mode entirely.
12367 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012368 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012369 if (connector_state->crtc != crtc)
12370 continue;
12371
12372 encoder = to_intel_encoder(connector_state->best_encoder);
12373
Daniel Vetterefea6e82013-07-21 21:36:59 +020012374 if (!(encoder->compute_config(encoder, pipe_config))) {
12375 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012376 goto fail;
12377 }
12378 }
12379
Daniel Vetterff9a6752013-06-01 17:16:21 +020012380 /* Set default port clock if not overwritten by the encoder. Needs to be
12381 * done afterwards in case the encoder adjusts the mode. */
12382 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012383 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012384 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012385
Daniel Vettera43f6e02013-06-07 23:10:32 +020012386 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012387 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012388 DRM_DEBUG_KMS("CRTC fixup failed\n");
12389 goto fail;
12390 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012391
12392 if (ret == RETRY) {
12393 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12394 ret = -EINVAL;
12395 goto fail;
12396 }
12397
12398 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12399 retry = false;
12400 goto encoder_retry;
12401 }
12402
Daniel Vettere8fa4272015-08-12 11:43:34 +020012403 /* Dithering seems to not pass-through bits correctly when it should, so
12404 * only enable it on 6bpc panels. */
12405 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012406 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012407 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012408
Daniel Vetter7758a112012-07-08 19:40:39 +020012409fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012410 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012411}
12412
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012413static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012414intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012415{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012416 struct drm_crtc *crtc;
12417 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012418 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012419
Ville Syrjälä76688512014-01-10 11:28:06 +020012420 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012421 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012422 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012423
12424 /* Update hwmode for vblank functions */
12425 if (crtc->state->active)
12426 crtc->hwmode = crtc->state->adjusted_mode;
12427 else
12428 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012429
12430 /*
12431 * Update legacy state to satisfy fbc code. This can
12432 * be removed when fbc uses the atomic state.
12433 */
12434 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12435 struct drm_plane_state *plane_state = crtc->primary->state;
12436
12437 crtc->primary->fb = plane_state->fb;
12438 crtc->x = plane_state->src_x >> 16;
12439 crtc->y = plane_state->src_y >> 16;
12440 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012441 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012442}
12443
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012444static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012445{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012446 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012447
12448 if (clock1 == clock2)
12449 return true;
12450
12451 if (!clock1 || !clock2)
12452 return false;
12453
12454 diff = abs(clock1 - clock2);
12455
12456 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12457 return true;
12458
12459 return false;
12460}
12461
Daniel Vetter25c5b262012-07-08 22:08:04 +020012462#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12463 list_for_each_entry((intel_crtc), \
12464 &(dev)->mode_config.crtc_list, \
12465 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012466 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012467
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012468static bool
12469intel_compare_m_n(unsigned int m, unsigned int n,
12470 unsigned int m2, unsigned int n2,
12471 bool exact)
12472{
12473 if (m == m2 && n == n2)
12474 return true;
12475
12476 if (exact || !m || !n || !m2 || !n2)
12477 return false;
12478
12479 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12480
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012481 if (n > n2) {
12482 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012483 m2 <<= 1;
12484 n2 <<= 1;
12485 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012486 } else if (n < n2) {
12487 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012488 m <<= 1;
12489 n <<= 1;
12490 }
12491 }
12492
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012493 if (n != n2)
12494 return false;
12495
12496 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012497}
12498
12499static bool
12500intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12501 struct intel_link_m_n *m2_n2,
12502 bool adjust)
12503{
12504 if (m_n->tu == m2_n2->tu &&
12505 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12506 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12507 intel_compare_m_n(m_n->link_m, m_n->link_n,
12508 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12509 if (adjust)
12510 *m2_n2 = *m_n;
12511
12512 return true;
12513 }
12514
12515 return false;
12516}
12517
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012518static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012519intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012520 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012521 struct intel_crtc_state *pipe_config,
12522 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012523{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012524 bool ret = true;
12525
12526#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12527 do { \
12528 if (!adjust) \
12529 DRM_ERROR(fmt, ##__VA_ARGS__); \
12530 else \
12531 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12532 } while (0)
12533
Daniel Vetter66e985c2013-06-05 13:34:20 +020012534#define PIPE_CONF_CHECK_X(name) \
12535 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012536 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012537 "(expected 0x%08x, found 0x%08x)\n", \
12538 current_config->name, \
12539 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012540 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012541 }
12542
Daniel Vetter08a24032013-04-19 11:25:34 +020012543#define PIPE_CONF_CHECK_I(name) \
12544 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012545 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012546 "(expected %i, found %i)\n", \
12547 current_config->name, \
12548 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012549 ret = false; \
12550 }
12551
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012552#define PIPE_CONF_CHECK_P(name) \
12553 if (current_config->name != pipe_config->name) { \
12554 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12555 "(expected %p, found %p)\n", \
12556 current_config->name, \
12557 pipe_config->name); \
12558 ret = false; \
12559 }
12560
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012561#define PIPE_CONF_CHECK_M_N(name) \
12562 if (!intel_compare_link_m_n(&current_config->name, \
12563 &pipe_config->name,\
12564 adjust)) { \
12565 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12566 "(expected tu %i gmch %i/%i link %i/%i, " \
12567 "found tu %i, gmch %i/%i link %i/%i)\n", \
12568 current_config->name.tu, \
12569 current_config->name.gmch_m, \
12570 current_config->name.gmch_n, \
12571 current_config->name.link_m, \
12572 current_config->name.link_n, \
12573 pipe_config->name.tu, \
12574 pipe_config->name.gmch_m, \
12575 pipe_config->name.gmch_n, \
12576 pipe_config->name.link_m, \
12577 pipe_config->name.link_n); \
12578 ret = false; \
12579 }
12580
Daniel Vetter55c561a2016-03-30 11:34:36 +020012581/* This is required for BDW+ where there is only one set of registers for
12582 * switching between high and low RR.
12583 * This macro can be used whenever a comparison has to be made between one
12584 * hw state and multiple sw state variables.
12585 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012586#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12587 if (!intel_compare_link_m_n(&current_config->name, \
12588 &pipe_config->name, adjust) && \
12589 !intel_compare_link_m_n(&current_config->alt_name, \
12590 &pipe_config->name, adjust)) { \
12591 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12592 "(expected tu %i gmch %i/%i link %i/%i, " \
12593 "or tu %i gmch %i/%i link %i/%i, " \
12594 "found tu %i, gmch %i/%i link %i/%i)\n", \
12595 current_config->name.tu, \
12596 current_config->name.gmch_m, \
12597 current_config->name.gmch_n, \
12598 current_config->name.link_m, \
12599 current_config->name.link_n, \
12600 current_config->alt_name.tu, \
12601 current_config->alt_name.gmch_m, \
12602 current_config->alt_name.gmch_n, \
12603 current_config->alt_name.link_m, \
12604 current_config->alt_name.link_n, \
12605 pipe_config->name.tu, \
12606 pipe_config->name.gmch_m, \
12607 pipe_config->name.gmch_n, \
12608 pipe_config->name.link_m, \
12609 pipe_config->name.link_n); \
12610 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012611 }
12612
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012613#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12614 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012615 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012616 "(expected %i, found %i)\n", \
12617 current_config->name & (mask), \
12618 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012619 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012620 }
12621
Ville Syrjälä5e550652013-09-06 23:29:07 +030012622#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12623 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012624 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012625 "(expected %i, found %i)\n", \
12626 current_config->name, \
12627 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012628 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012629 }
12630
Daniel Vetterbb760062013-06-06 14:55:52 +020012631#define PIPE_CONF_QUIRK(quirk) \
12632 ((current_config->quirks | pipe_config->quirks) & (quirk))
12633
Daniel Vettereccb1402013-05-22 00:50:22 +020012634 PIPE_CONF_CHECK_I(cpu_transcoder);
12635
Daniel Vetter08a24032013-04-19 11:25:34 +020012636 PIPE_CONF_CHECK_I(has_pch_encoder);
12637 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012638 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012639
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012640 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012641 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012642
12643 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012644 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012645
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012646 if (current_config->has_drrs)
12647 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12648 } else
12649 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012650
Jani Nikulaa65347b2015-11-27 12:21:46 +020012651 PIPE_CONF_CHECK_I(has_dsi_encoder);
12652
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012653 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12654 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12655 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12656 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12657 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12658 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012659
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012660 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12661 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12662 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12663 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12664 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12665 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012666
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012667 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012668 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012669 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012670 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012671 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012672 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012673
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012674 PIPE_CONF_CHECK_I(has_audio);
12675
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012676 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012677 DRM_MODE_FLAG_INTERLACE);
12678
Daniel Vetterbb760062013-06-06 14:55:52 +020012679 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012680 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012681 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012682 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012683 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012684 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012685 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012686 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012687 DRM_MODE_FLAG_NVSYNC);
12688 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012689
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012690 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012691 /* pfit ratios are autocomputed by the hw on gen4+ */
12692 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020012693 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012694 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012695
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012696 if (!adjust) {
12697 PIPE_CONF_CHECK_I(pipe_src_w);
12698 PIPE_CONF_CHECK_I(pipe_src_h);
12699
12700 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12701 if (current_config->pch_pfit.enabled) {
12702 PIPE_CONF_CHECK_X(pch_pfit.pos);
12703 PIPE_CONF_CHECK_X(pch_pfit.size);
12704 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012705
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012706 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12707 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012708
Jesse Barnese59150d2014-01-07 13:30:45 -080012709 /* BDW+ don't expose a synchronous way to read the state */
12710 if (IS_HASWELL(dev))
12711 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012712
Ville Syrjälä282740f2013-09-04 18:30:03 +030012713 PIPE_CONF_CHECK_I(double_wide);
12714
Daniel Vetter26804af2014-06-25 22:01:55 +030012715 PIPE_CONF_CHECK_X(ddi_pll_sel);
12716
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012717 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012718 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012719 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012720 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12721 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012722 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012723 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012724 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12725 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12726 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012727
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012728 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12729 PIPE_CONF_CHECK_I(pipe_bpp);
12730
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012731 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012732 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012733
Daniel Vetter66e985c2013-06-05 13:34:20 +020012734#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012735#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012736#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012737#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012738#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012739#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012740#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012741
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012742 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012743}
12744
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012745static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12746 const struct intel_crtc_state *pipe_config)
12747{
12748 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012749 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012750 &pipe_config->fdi_m_n);
12751 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12752
12753 /*
12754 * FDI already provided one idea for the dotclock.
12755 * Yell if the encoder disagrees.
12756 */
12757 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12758 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12759 fdi_dotclock, dotclock);
12760 }
12761}
12762
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012763static void verify_wm_state(struct drm_crtc *crtc,
12764 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000012765{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012766 struct drm_device *dev = crtc->dev;
Damien Lespiau08db6652014-11-04 17:06:52 +000012767 struct drm_i915_private *dev_priv = dev->dev_private;
12768 struct skl_ddb_allocation hw_ddb, *sw_ddb;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012769 struct skl_ddb_entry *hw_entry, *sw_entry;
12770 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12771 const enum pipe pipe = intel_crtc->pipe;
Damien Lespiau08db6652014-11-04 17:06:52 +000012772 int plane;
12773
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012774 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000012775 return;
12776
12777 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12778 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12779
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012780 /* planes */
12781 for_each_plane(dev_priv, pipe, plane) {
12782 hw_entry = &hw_ddb.plane[pipe][plane];
12783 sw_entry = &sw_ddb->plane[pipe][plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000012784
12785 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12786 continue;
12787
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012788 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12789 "(expected (%u,%u), found (%u,%u))\n",
12790 pipe_name(pipe), plane + 1,
12791 sw_entry->start, sw_entry->end,
12792 hw_entry->start, hw_entry->end);
12793 }
12794
12795 /* cursor */
12796 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12797 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
12798
12799 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012800 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12801 "(expected (%u,%u), found (%u,%u))\n",
12802 pipe_name(pipe),
12803 sw_entry->start, sw_entry->end,
12804 hw_entry->start, hw_entry->end);
12805 }
12806}
12807
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012808static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012809verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012810{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012811 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012812
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012813 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012814 struct drm_encoder *encoder = connector->encoder;
12815 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012816
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012817 if (state->crtc != crtc)
12818 continue;
12819
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012820 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012821
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012822 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012823 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012824 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012825}
12826
12827static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012828verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012829{
12830 struct intel_encoder *encoder;
12831 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012832
Damien Lespiaub2784e12014-08-05 11:29:37 +010012833 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012834 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012835 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012836
12837 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12838 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012839 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012840
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012841 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012842 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012843 continue;
12844 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012845
12846 I915_STATE_WARN(connector->base.state->crtc !=
12847 encoder->base.crtc,
12848 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012849 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012850
Rob Clarke2c719b2014-12-15 13:56:32 -050012851 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012852 "encoder's enabled state mismatch "
12853 "(expected %i, found %i)\n",
12854 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012855
12856 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012857 bool active;
12858
12859 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012860 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012861 "encoder detached but still enabled on pipe %c.\n",
12862 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012863 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012864 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012865}
12866
12867static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012868verify_crtc_state(struct drm_crtc *crtc,
12869 struct drm_crtc_state *old_crtc_state,
12870 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012871{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012872 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012873 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012874 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012875 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12876 struct intel_crtc_state *pipe_config, *sw_config;
12877 struct drm_atomic_state *old_state;
12878 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012879
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012880 old_state = old_crtc_state->state;
12881 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12882 pipe_config = to_intel_crtc_state(old_crtc_state);
12883 memset(pipe_config, 0, sizeof(*pipe_config));
12884 pipe_config->base.crtc = crtc;
12885 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012886
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012887 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012888
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012889 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012890
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012891 /* 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 = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012895
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012896 I915_STATE_WARN(new_crtc_state->active != active,
12897 "crtc active state doesn't match with hw state "
12898 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012899
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012900 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
12901 "transitional active state does not match atomic hw state "
12902 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012903
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012904 for_each_encoder_on_crtc(dev, crtc, encoder) {
12905 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012906
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012907 active = encoder->get_hw_state(encoder, &pipe);
12908 I915_STATE_WARN(active != new_crtc_state->active,
12909 "[ENCODER:%i] active %i with crtc active %i\n",
12910 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012911
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012912 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12913 "Encoder connected to wrong pipe %c\n",
12914 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012915
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012916 if (active)
12917 encoder->get_config(encoder, pipe_config);
12918 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012919
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012920 if (!new_crtc_state->active)
12921 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012922
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012923 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012924
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012925 sw_config = to_intel_crtc_state(crtc->state);
12926 if (!intel_pipe_config_compare(dev, sw_config,
12927 pipe_config, false)) {
12928 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12929 intel_dump_pipe_config(intel_crtc, pipe_config,
12930 "[hw state]");
12931 intel_dump_pipe_config(intel_crtc, sw_config,
12932 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012933 }
12934}
12935
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012936static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012937verify_single_dpll_state(struct drm_i915_private *dev_priv,
12938 struct intel_shared_dpll *pll,
12939 struct drm_crtc *crtc,
12940 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012941{
12942 struct intel_dpll_hw_state dpll_hw_state;
12943 unsigned crtc_mask;
12944 bool active;
12945
12946 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12947
12948 DRM_DEBUG_KMS("%s\n", pll->name);
12949
12950 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
12951
12952 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
12953 I915_STATE_WARN(!pll->on && pll->active_mask,
12954 "pll in active use but not on in sw tracking\n");
12955 I915_STATE_WARN(pll->on && !pll->active_mask,
12956 "pll is on but not used by any active crtc\n");
12957 I915_STATE_WARN(pll->on != active,
12958 "pll on state mismatch (expected %i, found %i)\n",
12959 pll->on, active);
12960 }
12961
12962 if (!crtc) {
12963 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
12964 "more active pll users than references: %x vs %x\n",
12965 pll->active_mask, pll->config.crtc_mask);
12966
12967 return;
12968 }
12969
12970 crtc_mask = 1 << drm_crtc_index(crtc);
12971
12972 if (new_state->active)
12973 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
12974 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
12975 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12976 else
12977 I915_STATE_WARN(pll->active_mask & crtc_mask,
12978 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
12979 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
12980
12981 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
12982 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
12983 crtc_mask, pll->config.crtc_mask);
12984
12985 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
12986 &dpll_hw_state,
12987 sizeof(dpll_hw_state)),
12988 "pll hw state mismatch\n");
12989}
12990
12991static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012992verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
12993 struct drm_crtc_state *old_crtc_state,
12994 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012995{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012996 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012997 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
12998 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
12999
13000 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013001 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013002
13003 if (old_state->shared_dpll &&
13004 old_state->shared_dpll != new_state->shared_dpll) {
13005 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13006 struct intel_shared_dpll *pll = old_state->shared_dpll;
13007
13008 I915_STATE_WARN(pll->active_mask & crtc_mask,
13009 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13010 pipe_name(drm_crtc_index(crtc)));
13011 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13012 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13013 pipe_name(drm_crtc_index(crtc)));
13014 }
13015}
13016
13017static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013018intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013019 struct drm_crtc_state *old_state,
13020 struct drm_crtc_state *new_state)
13021{
13022 if (!needs_modeset(new_state) &&
13023 !to_intel_crtc_state(new_state)->update_pipe)
13024 return;
13025
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013026 verify_wm_state(crtc, new_state);
13027 verify_connector_state(crtc->dev, crtc);
13028 verify_crtc_state(crtc, old_state, new_state);
13029 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013030}
13031
13032static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013033verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013034{
13035 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013036 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013037
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013038 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013039 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013040}
Daniel Vetter53589012013-06-05 13:34:16 +020013041
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013042static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013043intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013044{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013045 verify_encoder_state(dev);
13046 verify_connector_state(dev, NULL);
13047 verify_disabled_dpll_state(dev);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013048}
13049
Ville Syrjälä80715b22014-05-15 20:23:23 +030013050static void update_scanline_offset(struct intel_crtc *crtc)
13051{
13052 struct drm_device *dev = crtc->base.dev;
13053
13054 /*
13055 * The scanline counter increments at the leading edge of hsync.
13056 *
13057 * On most platforms it starts counting from vtotal-1 on the
13058 * first active line. That means the scanline counter value is
13059 * always one less than what we would expect. Ie. just after
13060 * start of vblank, which also occurs at start of hsync (on the
13061 * last active line), the scanline counter will read vblank_start-1.
13062 *
13063 * On gen2 the scanline counter starts counting from 1 instead
13064 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13065 * to keep the value positive), instead of adding one.
13066 *
13067 * On HSW+ the behaviour of the scanline counter depends on the output
13068 * type. For DP ports it behaves like most other platforms, but on HDMI
13069 * there's an extra 1 line difference. So we need to add two instead of
13070 * one to the value.
13071 */
13072 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013073 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013074 int vtotal;
13075
Ville Syrjälä124abe02015-09-08 13:40:45 +030013076 vtotal = adjusted_mode->crtc_vtotal;
13077 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013078 vtotal /= 2;
13079
13080 crtc->scanline_offset = vtotal - 1;
13081 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013082 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013083 crtc->scanline_offset = 2;
13084 } else
13085 crtc->scanline_offset = 1;
13086}
13087
Maarten Lankhorstad421372015-06-15 12:33:42 +020013088static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013089{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013090 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013091 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013092 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013093 struct drm_crtc *crtc;
13094 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013095 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013096
13097 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013098 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013099
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013100 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013101 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013102 struct intel_shared_dpll *old_dpll =
13103 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013104
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013105 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013106 continue;
13107
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013108 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013109
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013110 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013111 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013112
Maarten Lankhorstad421372015-06-15 12:33:42 +020013113 if (!shared_dpll)
13114 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13115
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013116 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013117 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013118}
13119
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013120/*
13121 * This implements the workaround described in the "notes" section of the mode
13122 * set sequence documentation. When going from no pipes or single pipe to
13123 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13124 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13125 */
13126static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13127{
13128 struct drm_crtc_state *crtc_state;
13129 struct intel_crtc *intel_crtc;
13130 struct drm_crtc *crtc;
13131 struct intel_crtc_state *first_crtc_state = NULL;
13132 struct intel_crtc_state *other_crtc_state = NULL;
13133 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13134 int i;
13135
13136 /* look at all crtc's that are going to be enabled in during modeset */
13137 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13138 intel_crtc = to_intel_crtc(crtc);
13139
13140 if (!crtc_state->active || !needs_modeset(crtc_state))
13141 continue;
13142
13143 if (first_crtc_state) {
13144 other_crtc_state = to_intel_crtc_state(crtc_state);
13145 break;
13146 } else {
13147 first_crtc_state = to_intel_crtc_state(crtc_state);
13148 first_pipe = intel_crtc->pipe;
13149 }
13150 }
13151
13152 /* No workaround needed? */
13153 if (!first_crtc_state)
13154 return 0;
13155
13156 /* w/a possibly needed, check how many crtc's are already enabled. */
13157 for_each_intel_crtc(state->dev, intel_crtc) {
13158 struct intel_crtc_state *pipe_config;
13159
13160 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13161 if (IS_ERR(pipe_config))
13162 return PTR_ERR(pipe_config);
13163
13164 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13165
13166 if (!pipe_config->base.active ||
13167 needs_modeset(&pipe_config->base))
13168 continue;
13169
13170 /* 2 or more enabled crtcs means no need for w/a */
13171 if (enabled_pipe != INVALID_PIPE)
13172 return 0;
13173
13174 enabled_pipe = intel_crtc->pipe;
13175 }
13176
13177 if (enabled_pipe != INVALID_PIPE)
13178 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13179 else if (other_crtc_state)
13180 other_crtc_state->hsw_workaround_pipe = first_pipe;
13181
13182 return 0;
13183}
13184
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013185static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13186{
13187 struct drm_crtc *crtc;
13188 struct drm_crtc_state *crtc_state;
13189 int ret = 0;
13190
13191 /* add all active pipes to the state */
13192 for_each_crtc(state->dev, crtc) {
13193 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13194 if (IS_ERR(crtc_state))
13195 return PTR_ERR(crtc_state);
13196
13197 if (!crtc_state->active || needs_modeset(crtc_state))
13198 continue;
13199
13200 crtc_state->mode_changed = true;
13201
13202 ret = drm_atomic_add_affected_connectors(state, crtc);
13203 if (ret)
13204 break;
13205
13206 ret = drm_atomic_add_affected_planes(state, crtc);
13207 if (ret)
13208 break;
13209 }
13210
13211 return ret;
13212}
13213
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013214static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013215{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013216 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13217 struct drm_i915_private *dev_priv = state->dev->dev_private;
13218 struct drm_crtc *crtc;
13219 struct drm_crtc_state *crtc_state;
13220 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013221
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013222 if (!check_digital_port_conflicts(state)) {
13223 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13224 return -EINVAL;
13225 }
13226
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013227 intel_state->modeset = true;
13228 intel_state->active_crtcs = dev_priv->active_crtcs;
13229
13230 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13231 if (crtc_state->active)
13232 intel_state->active_crtcs |= 1 << i;
13233 else
13234 intel_state->active_crtcs &= ~(1 << i);
13235 }
13236
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013237 /*
13238 * See if the config requires any additional preparation, e.g.
13239 * to adjust global state with pipes off. We need to do this
13240 * here so we can get the modeset_pipe updated config for the new
13241 * mode set on this crtc. For other crtcs we need to use the
13242 * adjusted_mode bits in the crtc directly.
13243 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013244 if (dev_priv->display.modeset_calc_cdclk) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013245 ret = dev_priv->display.modeset_calc_cdclk(state);
13246
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013247 if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013248 ret = intel_modeset_all_pipes(state);
13249
13250 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013251 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013252
13253 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13254 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013255 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013256 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013257
Maarten Lankhorstad421372015-06-15 12:33:42 +020013258 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013259
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013260 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013261 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013262
Maarten Lankhorstad421372015-06-15 12:33:42 +020013263 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013264}
13265
Matt Roperaa363132015-09-24 15:53:18 -070013266/*
13267 * Handle calculation of various watermark data at the end of the atomic check
13268 * phase. The code here should be run after the per-crtc and per-plane 'check'
13269 * handlers to ensure that all derived state has been updated.
13270 */
13271static void calc_watermark_data(struct drm_atomic_state *state)
13272{
13273 struct drm_device *dev = state->dev;
13274 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13275 struct drm_crtc *crtc;
13276 struct drm_crtc_state *cstate;
13277 struct drm_plane *plane;
13278 struct drm_plane_state *pstate;
13279
13280 /*
13281 * Calculate watermark configuration details now that derived
13282 * plane/crtc state is all properly updated.
13283 */
13284 drm_for_each_crtc(crtc, dev) {
13285 cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
13286 crtc->state;
13287
13288 if (cstate->active)
13289 intel_state->wm_config.num_pipes_active++;
13290 }
13291 drm_for_each_legacy_plane(plane, dev) {
13292 pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
13293 plane->state;
13294
13295 if (!to_intel_plane_state(pstate)->visible)
13296 continue;
13297
13298 intel_state->wm_config.sprites_enabled = true;
13299 if (pstate->crtc_w != pstate->src_w >> 16 ||
13300 pstate->crtc_h != pstate->src_h >> 16)
13301 intel_state->wm_config.sprites_scaled = true;
13302 }
13303}
13304
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013305/**
13306 * intel_atomic_check - validate state object
13307 * @dev: drm device
13308 * @state: state to validate
13309 */
13310static int intel_atomic_check(struct drm_device *dev,
13311 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013312{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013313 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013314 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013315 struct drm_crtc *crtc;
13316 struct drm_crtc_state *crtc_state;
13317 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013318 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013319
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013320 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013321 if (ret)
13322 return ret;
13323
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013324 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013325 struct intel_crtc_state *pipe_config =
13326 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013327
13328 /* Catch I915_MODE_FLAG_INHERITED */
13329 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13330 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013331
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013332 if (!crtc_state->enable) {
13333 if (needs_modeset(crtc_state))
13334 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013335 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013336 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013337
Daniel Vetter26495482015-07-15 14:15:52 +020013338 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013339 continue;
13340
Daniel Vetter26495482015-07-15 14:15:52 +020013341 /* FIXME: For only active_changed we shouldn't need to do any
13342 * state recomputation at all. */
13343
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013344 ret = drm_atomic_add_affected_connectors(state, crtc);
13345 if (ret)
13346 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013347
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013348 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013349 if (ret)
13350 return ret;
13351
Jani Nikula73831232015-11-19 10:26:30 +020013352 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013353 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013354 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013355 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013356 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013357 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013358 }
13359
13360 if (needs_modeset(crtc_state)) {
13361 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013362
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013363 ret = drm_atomic_add_affected_planes(state, crtc);
13364 if (ret)
13365 return ret;
13366 }
13367
Daniel Vetter26495482015-07-15 14:15:52 +020013368 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13369 needs_modeset(crtc_state) ?
13370 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013371 }
13372
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013373 if (any_ms) {
13374 ret = intel_modeset_checks(state);
13375
13376 if (ret)
13377 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013378 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013379 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013380
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013381 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013382 if (ret)
13383 return ret;
13384
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013385 intel_fbc_choose_crtc(dev_priv, state);
Matt Roperaa363132015-09-24 15:53:18 -070013386 calc_watermark_data(state);
13387
13388 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013389}
13390
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013391static int intel_atomic_prepare_commit(struct drm_device *dev,
13392 struct drm_atomic_state *state,
13393 bool async)
13394{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013395 struct drm_i915_private *dev_priv = dev->dev_private;
13396 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013397 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013398 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013399 struct drm_crtc *crtc;
13400 int i, ret;
13401
13402 if (async) {
13403 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13404 return -EINVAL;
13405 }
13406
13407 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13408 ret = intel_crtc_wait_for_pending_flips(crtc);
13409 if (ret)
13410 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013411
13412 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13413 flush_workqueue(dev_priv->wq);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013414 }
13415
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013416 ret = mutex_lock_interruptible(&dev->struct_mutex);
13417 if (ret)
13418 return ret;
13419
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013420 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013421 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013422
Chris Wilsonf7e58382016-04-13 17:35:07 +010013423 if (!ret && !async) {
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013424 for_each_plane_in_state(state, plane, plane_state, i) {
13425 struct intel_plane_state *intel_plane_state =
13426 to_intel_plane_state(plane_state);
13427
13428 if (!intel_plane_state->wait_req)
13429 continue;
13430
13431 ret = __i915_wait_request(intel_plane_state->wait_req,
Chris Wilson299259a2016-04-13 17:35:06 +010013432 true, NULL, NULL);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013433 if (ret) {
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013434 /* Any hang should be swallowed by the wait */
13435 WARN_ON(ret == -EIO);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013436 mutex_lock(&dev->struct_mutex);
13437 drm_atomic_helper_cleanup_planes(dev, state);
13438 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013439 break;
Chris Wilsonf7e58382016-04-13 17:35:07 +010013440 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013441 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013442 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013443
13444 return ret;
13445}
13446
Maarten Lankhorste8861672016-02-24 11:24:26 +010013447static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13448 struct drm_i915_private *dev_priv,
13449 unsigned crtc_mask)
13450{
13451 unsigned last_vblank_count[I915_MAX_PIPES];
13452 enum pipe pipe;
13453 int ret;
13454
13455 if (!crtc_mask)
13456 return;
13457
13458 for_each_pipe(dev_priv, pipe) {
13459 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13460
13461 if (!((1 << pipe) & crtc_mask))
13462 continue;
13463
13464 ret = drm_crtc_vblank_get(crtc);
13465 if (WARN_ON(ret != 0)) {
13466 crtc_mask &= ~(1 << pipe);
13467 continue;
13468 }
13469
13470 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13471 }
13472
13473 for_each_pipe(dev_priv, pipe) {
13474 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13475 long lret;
13476
13477 if (!((1 << pipe) & crtc_mask))
13478 continue;
13479
13480 lret = wait_event_timeout(dev->vblank[pipe].queue,
13481 last_vblank_count[pipe] !=
13482 drm_crtc_vblank_count(crtc),
13483 msecs_to_jiffies(50));
13484
13485 WARN_ON(!lret);
13486
13487 drm_crtc_vblank_put(crtc);
13488 }
13489}
13490
13491static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
13492{
13493 /* fb updated, need to unpin old fb */
13494 if (crtc_state->fb_changed)
13495 return true;
13496
13497 /* wm changes, need vblank before final wm's */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013498 if (crtc_state->update_wm_post)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013499 return true;
13500
13501 /*
13502 * cxsr is re-enabled after vblank.
Ville Syrjäläcaed3612016-03-09 19:07:25 +020013503 * This is already handled by crtc_state->update_wm_post,
Maarten Lankhorste8861672016-02-24 11:24:26 +010013504 * but added for clarity.
13505 */
13506 if (crtc_state->disable_cxsr)
13507 return true;
13508
13509 return false;
13510}
13511
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013512/**
13513 * intel_atomic_commit - commit validated state object
13514 * @dev: DRM device
13515 * @state: the top-level driver state object
13516 * @async: asynchronous commit
13517 *
13518 * This function commits a top-level state object that has been validated
13519 * with drm_atomic_helper_check().
13520 *
13521 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13522 * we can only handle plane-related operations and do not yet support
13523 * asynchronous commit.
13524 *
13525 * RETURNS
13526 * Zero for success or -errno.
13527 */
13528static int intel_atomic_commit(struct drm_device *dev,
13529 struct drm_atomic_state *state,
13530 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013531{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013532 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013533 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013534 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013535 struct drm_crtc *crtc;
Matt Ropered4a6a72016-02-23 17:20:13 -080013536 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013537 int ret = 0, i;
13538 bool hw_check = intel_state->modeset;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013539 unsigned long put_domains[I915_MAX_PIPES] = {};
Maarten Lankhorste8861672016-02-24 11:24:26 +010013540 unsigned crtc_vblank_mask = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013541
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013542 ret = intel_atomic_prepare_commit(dev, state, async);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013543 if (ret) {
13544 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013545 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013546 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013547
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013548 drm_atomic_helper_swap_state(dev, state);
Maarten Lankhorsta1475e72016-03-14 09:27:53 +010013549 dev_priv->wm.config = intel_state->wm_config;
13550 intel_shared_dpll_commit(state);
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013551
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013552 if (intel_state->modeset) {
13553 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13554 sizeof(intel_state->min_pixclk));
13555 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013556 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013557
13558 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013559 }
13560
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013561 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013562 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13563
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013564 if (needs_modeset(crtc->state) ||
13565 to_intel_crtc_state(crtc->state)->update_pipe) {
13566 hw_check = true;
13567
13568 put_domains[to_intel_crtc(crtc)->pipe] =
13569 modeset_get_crtc_power_domains(crtc,
13570 to_intel_crtc_state(crtc->state));
13571 }
13572
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013573 if (!needs_modeset(crtc->state))
13574 continue;
13575
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013576 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013577
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013578 if (old_crtc_state->active) {
13579 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013580 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013581 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013582 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013583 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013584
13585 /*
13586 * Underruns don't always raise
13587 * interrupts, so check manually.
13588 */
13589 intel_check_cpu_fifo_underruns(dev_priv);
13590 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013591
13592 if (!crtc->state->active)
13593 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013594 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013595 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013596
Daniel Vetterea9d7582012-07-10 10:42:52 +020013597 /* Only after disabling all output pipelines that will be changed can we
13598 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013599 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013600
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013601 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013602 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013603
13604 if (dev_priv->display.modeset_commit_cdclk &&
13605 intel_state->dev_cdclk != dev_priv->cdclk_freq)
13606 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013607
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013608 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013609 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013610
Daniel Vettera6778b32012-07-02 09:56:42 +020013611 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013612 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013613 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13614 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorste8861672016-02-24 11:24:26 +010013615 struct intel_crtc_state *pipe_config =
13616 to_intel_crtc_state(crtc->state);
13617 bool update_pipe = !modeset && pipe_config->update_pipe;
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013618
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013619 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013620 update_scanline_offset(to_intel_crtc(crtc));
13621 dev_priv->display.crtc_enable(crtc);
13622 }
13623
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013624 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013625 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013626
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010013627 if (crtc->state->active &&
13628 drm_atomic_get_existing_plane_state(state, crtc->primary))
Paulo Zanoni49227c42016-01-19 11:35:52 -020013629 intel_fbc_enable(intel_crtc);
13630
Maarten Lankhorst6173ee22015-09-23 16:29:39 +020013631 if (crtc->state->active &&
13632 (crtc->state->planes_changed || update_pipe))
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013633 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013634
Maarten Lankhorste8861672016-02-24 11:24:26 +010013635 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13636 crtc_vblank_mask |= 1 << i;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013637 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013638
Daniel Vettera6778b32012-07-02 09:56:42 +020013639 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013640
Maarten Lankhorste8861672016-02-24 11:24:26 +010013641 if (!state->legacy_cursor_update)
13642 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013643
Matt Ropered4a6a72016-02-23 17:20:13 -080013644 /*
13645 * Now that the vblank has passed, we can go ahead and program the
13646 * optimal watermarks on platforms that need two-step watermark
13647 * programming.
13648 *
13649 * TODO: Move this (and other cleanup) to an async worker eventually.
13650 */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013651 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Matt Ropered4a6a72016-02-23 17:20:13 -080013652 intel_cstate = to_intel_crtc_state(crtc->state);
13653
13654 if (dev_priv->display.optimize_watermarks)
13655 dev_priv->display.optimize_watermarks(intel_cstate);
13656 }
13657
Matt Roper177246a2016-03-04 15:59:39 -080013658 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13659 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13660
13661 if (put_domains[i])
13662 modeset_put_power_domains(dev_priv, put_domains[i]);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013663
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013664 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
Matt Roper177246a2016-03-04 15:59:39 -080013665 }
13666
13667 if (intel_state->modeset)
13668 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13669
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013670 mutex_lock(&dev->struct_mutex);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013671 drm_atomic_helper_cleanup_planes(dev, state);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013672 mutex_unlock(&dev->struct_mutex);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013673
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013674 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013675
Mika Kuoppala75714942015-12-16 09:26:48 +020013676 /* As one of the primary mmio accessors, KMS has a high likelihood
13677 * of triggering bugs in unclaimed access. After we finish
13678 * modesetting, see if an error has been flagged, and if so
13679 * enable debugging for the next modeset - and hope we catch
13680 * the culprit.
13681 *
13682 * XXX note that we assume display power is on at this point.
13683 * This might hold true now but we need to add pm helper to check
13684 * unclaimed only when the hardware is on, as atomic commits
13685 * can happen also when the device is completely off.
13686 */
13687 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13688
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013689 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013690}
13691
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013692void intel_crtc_restore_mode(struct drm_crtc *crtc)
13693{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013694 struct drm_device *dev = crtc->dev;
13695 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013696 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013697 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013698
13699 state = drm_atomic_state_alloc(dev);
13700 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013701 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013702 crtc->base.id);
13703 return;
13704 }
13705
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013706 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013707
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013708retry:
13709 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13710 ret = PTR_ERR_OR_ZERO(crtc_state);
13711 if (!ret) {
13712 if (!crtc_state->active)
13713 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013714
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013715 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013716 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013717 }
13718
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013719 if (ret == -EDEADLK) {
13720 drm_atomic_state_clear(state);
13721 drm_modeset_backoff(state->acquire_ctx);
13722 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013723 }
13724
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013725 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013726out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013727 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013728}
13729
Daniel Vetter25c5b262012-07-08 22:08:04 +020013730#undef for_each_intel_crtc_masked
13731
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013732static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013733 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013734 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013735 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013736 .destroy = intel_crtc_destroy,
13737 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013738 .atomic_duplicate_state = intel_crtc_duplicate_state,
13739 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013740};
13741
Matt Roper6beb8c232014-12-01 15:40:14 -080013742/**
13743 * intel_prepare_plane_fb - Prepare fb for usage on plane
13744 * @plane: drm plane to prepare for
13745 * @fb: framebuffer to prepare for presentation
13746 *
13747 * Prepares a framebuffer for usage on a display plane. Generally this
13748 * involves pinning the underlying object and updating the frontbuffer tracking
13749 * bits. Some older platforms need special physical address handling for
13750 * cursor planes.
13751 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013752 * Must be called with struct_mutex held.
13753 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013754 * Returns 0 on success, negative error code on failure.
13755 */
13756int
13757intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013758 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013759{
13760 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013761 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013762 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013763 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013764 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013765 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013766
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013767 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013768 return 0;
13769
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013770 if (old_obj) {
13771 struct drm_crtc_state *crtc_state =
13772 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13773
13774 /* Big Hammer, we also need to ensure that any pending
13775 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13776 * current scanout is retired before unpinning the old
13777 * framebuffer. Note that we rely on userspace rendering
13778 * into the buffer attached to the pipe they are waiting
13779 * on. If not, userspace generates a GPU hang with IPEHR
13780 * point to the MI_WAIT_FOR_EVENT.
13781 *
13782 * This should only fail upon a hung GPU, in which case we
13783 * can safely continue.
13784 */
13785 if (needs_modeset(crtc_state))
13786 ret = i915_gem_object_wait_rendering(old_obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013787 if (ret) {
13788 /* GPU hangs should have been swallowed by the wait */
13789 WARN_ON(ret == -EIO);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013790 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013791 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013792 }
13793
Alex Goins3c28ff22015-11-25 18:43:39 -080013794 /* For framebuffer backed by dmabuf, wait for fence */
13795 if (obj && obj->base.dma_buf) {
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013796 long lret;
Alex Goins3c28ff22015-11-25 18:43:39 -080013797
Maarten Lankhorstbcf8be22015-12-08 15:52:56 +010013798 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13799 false, true,
13800 MAX_SCHEDULE_TIMEOUT);
13801 if (lret == -ERESTARTSYS)
13802 return lret;
13803
13804 WARN(lret < 0, "waiting returns %li\n", lret);
Alex Goins3c28ff22015-11-25 18:43:39 -080013805 }
13806
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013807 if (!obj) {
13808 ret = 0;
13809 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013810 INTEL_INFO(dev)->cursor_needs_physical) {
13811 int align = IS_I830(dev) ? 16 * 1024 : 256;
13812 ret = i915_gem_object_attach_phys(obj, align);
13813 if (ret)
13814 DRM_DEBUG_KMS("failed to attach phys object\n");
13815 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020013816 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080013817 }
13818
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013819 if (ret == 0) {
13820 if (obj) {
13821 struct intel_plane_state *plane_state =
13822 to_intel_plane_state(new_state);
13823
13824 i915_gem_request_assign(&plane_state->wait_req,
13825 obj->last_write_req);
13826 }
13827
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013828 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013829 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013830
Matt Roper6beb8c232014-12-01 15:40:14 -080013831 return ret;
13832}
13833
Matt Roper38f3ce32014-12-02 07:45:25 -080013834/**
13835 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13836 * @plane: drm plane to clean up for
13837 * @fb: old framebuffer that was on plane
13838 *
13839 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013840 *
13841 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013842 */
13843void
13844intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013845 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013846{
13847 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013848 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013849 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013850 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13851 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013852
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013853 old_intel_state = to_intel_plane_state(old_state);
13854
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013855 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013856 return;
13857
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013858 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13859 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020013860 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013861
13862 /* prepare_fb aborted? */
13863 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13864 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13865 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013866
13867 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070013868}
13869
Chandra Konduru6156a452015-04-27 13:48:39 -070013870int
13871skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13872{
13873 int max_scale;
13874 struct drm_device *dev;
13875 struct drm_i915_private *dev_priv;
13876 int crtc_clock, cdclk;
13877
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013878 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013879 return DRM_PLANE_HELPER_NO_SCALING;
13880
13881 dev = intel_crtc->base.dev;
13882 dev_priv = dev->dev_private;
13883 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013884 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013885
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010013886 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013887 return DRM_PLANE_HELPER_NO_SCALING;
13888
13889 /*
13890 * skl max scale is lower of:
13891 * close to 3 but not 3, -1 is for that purpose
13892 * or
13893 * cdclk/crtc_clock
13894 */
13895 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13896
13897 return max_scale;
13898}
13899
Matt Roper465c1202014-05-29 08:06:54 -070013900static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013901intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013902 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013903 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013904{
Matt Roper2b875c22014-12-01 15:40:13 -080013905 struct drm_crtc *crtc = state->base.crtc;
13906 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013907 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013908 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13909 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013910
Ville Syrjälä693bdc22016-01-15 20:46:53 +020013911 if (INTEL_INFO(plane->dev)->gen >= 9) {
13912 /* use scaler when colorkey is not required */
13913 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
13914 min_scale = 1;
13915 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13916 }
Sonika Jindald8106362015-04-10 14:37:28 +053013917 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013918 }
Sonika Jindald8106362015-04-10 14:37:28 +053013919
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013920 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13921 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013922 min_scale, max_scale,
13923 can_position, true,
13924 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013925}
13926
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013927static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13928 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013929{
13930 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080013931 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013932 struct intel_crtc_state *old_intel_state =
13933 to_intel_crtc_state(old_crtc_state);
13934 bool modeset = needs_modeset(crtc->state);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013935
Matt Roperc34c9ee2014-12-23 10:41:50 -080013936 /* Perform vblank evasion around commit operation */
Maarten Lankhorst62852622015-09-23 16:29:38 +020013937 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013938
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013939 if (modeset)
13940 return;
13941
Maarten Lankhorst20a34e72016-03-30 17:16:36 +020013942 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
13943 intel_color_set_csc(crtc->state);
13944 intel_color_load_luts(crtc->state);
13945 }
13946
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013947 if (to_intel_crtc_state(crtc->state)->update_pipe)
13948 intel_update_pipe_config(intel_crtc, old_intel_state);
13949 else if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorst05832362015-06-15 12:33:48 +020013950 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013951}
13952
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013953static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13954 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013955{
Matt Roper32b7eee2014-12-24 07:59:06 -080013956 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013957
Maarten Lankhorst62852622015-09-23 16:29:38 +020013958 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013959}
13960
Matt Ropercf4c7c12014-12-04 10:27:42 -080013961/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013962 * intel_plane_destroy - destroy a plane
13963 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013964 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013965 * Common destruction function for all types of planes (primary, cursor,
13966 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013967 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013968void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013969{
13970 struct intel_plane *intel_plane = to_intel_plane(plane);
13971 drm_plane_cleanup(plane);
13972 kfree(intel_plane);
13973}
13974
Matt Roper65a3fea2015-01-21 16:35:42 -080013975const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013976 .update_plane = drm_atomic_helper_update_plane,
13977 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013978 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013979 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013980 .atomic_get_property = intel_plane_atomic_get_property,
13981 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013982 .atomic_duplicate_state = intel_plane_duplicate_state,
13983 .atomic_destroy_state = intel_plane_destroy_state,
13984
Matt Roper465c1202014-05-29 08:06:54 -070013985};
13986
13987static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13988 int pipe)
13989{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013990 struct intel_plane *primary = NULL;
13991 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013992 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020013993 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013994 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070013995
13996 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000013997 if (!primary)
13998 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070013999
Matt Roper8e7d6882015-01-21 16:35:41 -080014000 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014001 if (!state)
14002 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014003 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014004
Matt Roper465c1202014-05-29 08:06:54 -070014005 primary->can_scale = false;
14006 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014007 if (INTEL_INFO(dev)->gen >= 9) {
14008 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014009 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014010 }
Matt Roper465c1202014-05-29 08:06:54 -070014011 primary->pipe = pipe;
14012 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014013 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014014 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014015 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14016 primary->plane = !pipe;
14017
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014018 if (INTEL_INFO(dev)->gen >= 9) {
14019 intel_primary_formats = skl_primary_formats;
14020 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014021
14022 primary->update_plane = skylake_update_primary_plane;
14023 primary->disable_plane = skylake_disable_primary_plane;
14024 } else if (HAS_PCH_SPLIT(dev)) {
14025 intel_primary_formats = i965_primary_formats;
14026 num_formats = ARRAY_SIZE(i965_primary_formats);
14027
14028 primary->update_plane = ironlake_update_primary_plane;
14029 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014030 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014031 intel_primary_formats = i965_primary_formats;
14032 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014033
14034 primary->update_plane = i9xx_update_primary_plane;
14035 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014036 } else {
14037 intel_primary_formats = i8xx_primary_formats;
14038 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014039
14040 primary->update_plane = i9xx_update_primary_plane;
14041 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014042 }
14043
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014044 ret = drm_universal_plane_init(dev, &primary->base, 0,
14045 &intel_plane_funcs,
14046 intel_primary_formats, num_formats,
14047 DRM_PLANE_TYPE_PRIMARY, NULL);
14048 if (ret)
14049 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053014050
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014051 if (INTEL_INFO(dev)->gen >= 4)
14052 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014053
Matt Roperea2c67b2014-12-23 10:41:52 -080014054 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14055
Matt Roper465c1202014-05-29 08:06:54 -070014056 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014057
14058fail:
14059 kfree(state);
14060 kfree(primary);
14061
14062 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014063}
14064
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014065void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14066{
14067 if (!dev->mode_config.rotation_property) {
14068 unsigned long flags = BIT(DRM_ROTATE_0) |
14069 BIT(DRM_ROTATE_180);
14070
14071 if (INTEL_INFO(dev)->gen >= 9)
14072 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14073
14074 dev->mode_config.rotation_property =
14075 drm_mode_create_rotation_property(dev, flags);
14076 }
14077 if (dev->mode_config.rotation_property)
14078 drm_object_attach_property(&plane->base.base,
14079 dev->mode_config.rotation_property,
14080 plane->base.state->rotation);
14081}
14082
Matt Roper3d7d6512014-06-10 08:28:13 -070014083static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014084intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014085 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014086 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014087{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014088 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014089 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014090 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014091 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014092 unsigned stride;
14093 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014094
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014095 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14096 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014097 DRM_PLANE_HELPER_NO_SCALING,
14098 DRM_PLANE_HELPER_NO_SCALING,
14099 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014100 if (ret)
14101 return ret;
14102
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014103 /* if we want to turn off the cursor ignore width and height */
14104 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014105 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014106
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014107 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014108 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014109 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14110 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014111 return -EINVAL;
14112 }
14113
Matt Roperea2c67b2014-12-23 10:41:52 -080014114 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14115 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014116 DRM_DEBUG_KMS("buffer is too small\n");
14117 return -ENOMEM;
14118 }
14119
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014120 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014121 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014122 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014123 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014124
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014125 /*
14126 * There's something wrong with the cursor on CHV pipe C.
14127 * If it straddles the left edge of the screen then
14128 * moving it away from the edge or disabling it often
14129 * results in a pipe underrun, and often that can lead to
14130 * dead pipe (constant underrun reported, and it scans
14131 * out just a solid color). To recover from that, the
14132 * display power well must be turned off and on again.
14133 * Refuse the put the cursor into that compromised position.
14134 */
14135 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14136 state->visible && state->base.crtc_x < 0) {
14137 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14138 return -EINVAL;
14139 }
14140
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014141 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014142}
14143
Matt Roperf4a2cf22014-12-01 15:40:12 -080014144static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014145intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014146 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014147{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14149
14150 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014151 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014152}
14153
14154static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014155intel_update_cursor_plane(struct drm_plane *plane,
14156 const struct intel_crtc_state *crtc_state,
14157 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014158{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014159 struct drm_crtc *crtc = crtc_state->base.crtc;
14160 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014161 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014162 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014163 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014164
Matt Roperf4a2cf22014-12-01 15:40:12 -080014165 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014166 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014167 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014168 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014169 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014170 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014171
Gustavo Padovana912f122014-12-01 15:40:10 -080014172 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014173 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014174}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014175
Matt Roper3d7d6512014-06-10 08:28:13 -070014176static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14177 int pipe)
14178{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014179 struct intel_plane *cursor = NULL;
14180 struct intel_plane_state *state = NULL;
14181 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070014182
14183 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014184 if (!cursor)
14185 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070014186
Matt Roper8e7d6882015-01-21 16:35:41 -080014187 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014188 if (!state)
14189 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014190 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014191
Matt Roper3d7d6512014-06-10 08:28:13 -070014192 cursor->can_scale = false;
14193 cursor->max_downscale = 1;
14194 cursor->pipe = pipe;
14195 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014196 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014197 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014198 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014199 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014200
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014201 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14202 &intel_plane_funcs,
14203 intel_cursor_formats,
14204 ARRAY_SIZE(intel_cursor_formats),
14205 DRM_PLANE_TYPE_CURSOR, NULL);
14206 if (ret)
14207 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014208
14209 if (INTEL_INFO(dev)->gen >= 4) {
14210 if (!dev->mode_config.rotation_property)
14211 dev->mode_config.rotation_property =
14212 drm_mode_create_rotation_property(dev,
14213 BIT(DRM_ROTATE_0) |
14214 BIT(DRM_ROTATE_180));
14215 if (dev->mode_config.rotation_property)
14216 drm_object_attach_property(&cursor->base.base,
14217 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014218 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014219 }
14220
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014221 if (INTEL_INFO(dev)->gen >=9)
14222 state->scaler_id = -1;
14223
Matt Roperea2c67b2014-12-23 10:41:52 -080014224 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14225
Matt Roper3d7d6512014-06-10 08:28:13 -070014226 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014227
14228fail:
14229 kfree(state);
14230 kfree(cursor);
14231
14232 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014233}
14234
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014235static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14236 struct intel_crtc_state *crtc_state)
14237{
14238 int i;
14239 struct intel_scaler *intel_scaler;
14240 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14241
14242 for (i = 0; i < intel_crtc->num_scalers; i++) {
14243 intel_scaler = &scaler_state->scalers[i];
14244 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014245 intel_scaler->mode = PS_SCALER_MODE_DYN;
14246 }
14247
14248 scaler_state->scaler_id = -1;
14249}
14250
Hannes Ederb358d0a2008-12-18 21:18:47 +010014251static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014252{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014253 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014254 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014255 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014256 struct drm_plane *primary = NULL;
14257 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014258 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014259
Daniel Vetter955382f2013-09-19 14:05:45 +020014260 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014261 if (intel_crtc == NULL)
14262 return;
14263
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014264 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14265 if (!crtc_state)
14266 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014267 intel_crtc->config = crtc_state;
14268 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014269 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014270
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014271 /* initialize shared scalers */
14272 if (INTEL_INFO(dev)->gen >= 9) {
14273 if (pipe == PIPE_C)
14274 intel_crtc->num_scalers = 1;
14275 else
14276 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14277
14278 skl_init_scalers(dev, intel_crtc, crtc_state);
14279 }
14280
Matt Roper465c1202014-05-29 08:06:54 -070014281 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014282 if (!primary)
14283 goto fail;
14284
14285 cursor = intel_cursor_plane_create(dev, pipe);
14286 if (!cursor)
14287 goto fail;
14288
Matt Roper465c1202014-05-29 08:06:54 -070014289 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020014290 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070014291 if (ret)
14292 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014293
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014294 /*
14295 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014296 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014297 */
Jesse Barnes80824002009-09-10 15:28:06 -070014298 intel_crtc->pipe = pipe;
14299 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014300 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014301 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014302 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014303 }
14304
Chris Wilson4b0e3332014-05-30 16:35:26 +030014305 intel_crtc->cursor_base = ~0;
14306 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014307 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014308
Ville Syrjälä852eb002015-06-24 22:00:07 +030014309 intel_crtc->wm.cxsr_allowed = true;
14310
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014311 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14312 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14313 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14314 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14315
Jesse Barnes79e53942008-11-07 14:24:08 -080014316 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014317
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014318 intel_color_init(&intel_crtc->base);
14319
Daniel Vetter87b6b102014-05-15 15:33:46 +020014320 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014321 return;
14322
14323fail:
14324 if (primary)
14325 drm_plane_cleanup(primary);
14326 if (cursor)
14327 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014328 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014329 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014330}
14331
Jesse Barnes752aa882013-10-31 18:55:49 +020014332enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14333{
14334 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014335 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014336
Rob Clark51fd3712013-11-19 12:10:12 -050014337 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014338
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014339 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014340 return INVALID_PIPE;
14341
14342 return to_intel_crtc(encoder->crtc)->pipe;
14343}
14344
Carl Worth08d7b3d2009-04-29 14:43:54 -070014345int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014346 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014347{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014348 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014349 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014350 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014351
Rob Clark7707e652014-07-17 23:30:04 -040014352 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014353
Rob Clark7707e652014-07-17 23:30:04 -040014354 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014355 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014356 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014357 }
14358
Rob Clark7707e652014-07-17 23:30:04 -040014359 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014360 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014361
Daniel Vetterc05422d2009-08-11 16:05:30 +020014362 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014363}
14364
Daniel Vetter66a92782012-07-12 20:08:18 +020014365static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014366{
Daniel Vetter66a92782012-07-12 20:08:18 +020014367 struct drm_device *dev = encoder->base.dev;
14368 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014369 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014370 int entry = 0;
14371
Damien Lespiaub2784e12014-08-05 11:29:37 +010014372 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014373 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014374 index_mask |= (1 << entry);
14375
Jesse Barnes79e53942008-11-07 14:24:08 -080014376 entry++;
14377 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014378
Jesse Barnes79e53942008-11-07 14:24:08 -080014379 return index_mask;
14380}
14381
Chris Wilson4d302442010-12-14 19:21:29 +000014382static bool has_edp_a(struct drm_device *dev)
14383{
14384 struct drm_i915_private *dev_priv = dev->dev_private;
14385
14386 if (!IS_MOBILE(dev))
14387 return false;
14388
14389 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14390 return false;
14391
Damien Lespiaue3589902014-02-07 19:12:50 +000014392 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014393 return false;
14394
14395 return true;
14396}
14397
Jesse Barnes84b4e042014-06-25 08:24:29 -070014398static bool intel_crt_present(struct drm_device *dev)
14399{
14400 struct drm_i915_private *dev_priv = dev->dev_private;
14401
Damien Lespiau884497e2013-12-03 13:56:23 +000014402 if (INTEL_INFO(dev)->gen >= 9)
14403 return false;
14404
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014405 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014406 return false;
14407
14408 if (IS_CHERRYVIEW(dev))
14409 return false;
14410
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014411 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14412 return false;
14413
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014414 /* DDI E can't be used if DDI A requires 4 lanes */
14415 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14416 return false;
14417
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014418 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014419 return false;
14420
14421 return true;
14422}
14423
Jesse Barnes79e53942008-11-07 14:24:08 -080014424static void intel_setup_outputs(struct drm_device *dev)
14425{
Eric Anholt725e30a2009-01-22 13:01:02 -080014426 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014427 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014428 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014429
Daniel Vetterc9093352013-06-06 22:22:47 +020014430 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014431
Jesse Barnes84b4e042014-06-25 08:24:29 -070014432 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014433 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014434
Vandana Kannanc776eb22014-08-19 12:05:01 +053014435 if (IS_BROXTON(dev)) {
14436 /*
14437 * FIXME: Broxton doesn't support port detection via the
14438 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14439 * detect the ports.
14440 */
14441 intel_ddi_init(dev, PORT_A);
14442 intel_ddi_init(dev, PORT_B);
14443 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014444
14445 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014446 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014447 int found;
14448
Jesse Barnesde31fac2015-03-06 15:53:32 -080014449 /*
14450 * Haswell uses DDI functions to detect digital outputs.
14451 * On SKL pre-D0 the strap isn't connected, so we assume
14452 * it's there.
14453 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014454 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014455 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014456 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014457 intel_ddi_init(dev, PORT_A);
14458
14459 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14460 * register */
14461 found = I915_READ(SFUSE_STRAP);
14462
14463 if (found & SFUSE_STRAP_DDIB_DETECTED)
14464 intel_ddi_init(dev, PORT_B);
14465 if (found & SFUSE_STRAP_DDIC_DETECTED)
14466 intel_ddi_init(dev, PORT_C);
14467 if (found & SFUSE_STRAP_DDID_DETECTED)
14468 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014469 /*
14470 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14471 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014472 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014473 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14474 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14475 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14476 intel_ddi_init(dev, PORT_E);
14477
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014478 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014479 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014480 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014481
14482 if (has_edp_a(dev))
14483 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014484
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014485 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014486 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014487 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014488 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014489 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014490 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014491 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014492 }
14493
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014494 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014495 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014496
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014497 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014498 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014499
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014500 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014501 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014502
Daniel Vetter270b3042012-10-27 15:52:05 +020014503 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014504 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014505 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014506 /*
14507 * The DP_DETECTED bit is the latched state of the DDC
14508 * SDA pin at boot. However since eDP doesn't require DDC
14509 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14510 * eDP ports may have been muxed to an alternate function.
14511 * Thus we can't rely on the DP_DETECTED bit alone to detect
14512 * eDP ports. Consult the VBT as well as DP_DETECTED to
14513 * detect eDP ports.
14514 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014515 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014516 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014517 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14518 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014519 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014520 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014521
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014522 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014523 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014524 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14525 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014526 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014527 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014528
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014529 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014530 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014531 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14532 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14533 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14534 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014535 }
14536
Jani Nikula3cfca972013-08-27 15:12:26 +030014537 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014538 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014539 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014540
Paulo Zanonie2debe92013-02-18 19:00:27 -030014541 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014542 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014543 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014544 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014545 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014546 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014547 }
Ma Ling27185ae2009-08-24 13:50:23 +080014548
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014549 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014550 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014551 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014552
14553 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014554
Paulo Zanonie2debe92013-02-18 19:00:27 -030014555 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014556 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014557 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014558 }
Ma Ling27185ae2009-08-24 13:50:23 +080014559
Paulo Zanonie2debe92013-02-18 19:00:27 -030014560 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014561
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014562 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014563 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014564 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014565 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014566 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014567 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014568 }
Ma Ling27185ae2009-08-24 13:50:23 +080014569
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014570 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014571 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014572 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014573 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014574 intel_dvo_init(dev);
14575
Zhenyu Wang103a1962009-11-27 11:44:36 +080014576 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014577 intel_tv_init(dev);
14578
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014579 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014580
Damien Lespiaub2784e12014-08-05 11:29:37 +010014581 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014582 encoder->base.possible_crtcs = encoder->crtc_mask;
14583 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014584 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014585 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014586
Paulo Zanonidde86e22012-12-01 12:04:25 -020014587 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014588
14589 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014590}
14591
14592static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14593{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014594 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014595 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014596
Daniel Vetteref2d6332014-02-10 18:00:38 +010014597 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014598 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014599 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014600 drm_gem_object_unreference(&intel_fb->obj->base);
14601 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014602 kfree(intel_fb);
14603}
14604
14605static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014606 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014607 unsigned int *handle)
14608{
14609 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014610 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014611
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014612 if (obj->userptr.mm) {
14613 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14614 return -EINVAL;
14615 }
14616
Chris Wilson05394f32010-11-08 19:18:58 +000014617 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014618}
14619
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014620static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14621 struct drm_file *file,
14622 unsigned flags, unsigned color,
14623 struct drm_clip_rect *clips,
14624 unsigned num_clips)
14625{
14626 struct drm_device *dev = fb->dev;
14627 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14628 struct drm_i915_gem_object *obj = intel_fb->obj;
14629
14630 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014631 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014632 mutex_unlock(&dev->struct_mutex);
14633
14634 return 0;
14635}
14636
Jesse Barnes79e53942008-11-07 14:24:08 -080014637static const struct drm_framebuffer_funcs intel_fb_funcs = {
14638 .destroy = intel_user_framebuffer_destroy,
14639 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014640 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014641};
14642
Damien Lespiaub3218032015-02-27 11:15:18 +000014643static
14644u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14645 uint32_t pixel_format)
14646{
14647 u32 gen = INTEL_INFO(dev)->gen;
14648
14649 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014650 int cpp = drm_format_plane_cpp(pixel_format, 0);
14651
Damien Lespiaub3218032015-02-27 11:15:18 +000014652 /* "The stride in bytes must not exceed the of the size of 8K
14653 * pixels and 32K bytes."
14654 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014655 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014656 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014657 return 32*1024;
14658 } else if (gen >= 4) {
14659 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14660 return 16*1024;
14661 else
14662 return 32*1024;
14663 } else if (gen >= 3) {
14664 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14665 return 8*1024;
14666 else
14667 return 16*1024;
14668 } else {
14669 /* XXX DSPC is limited to 4k tiled */
14670 return 8*1024;
14671 }
14672}
14673
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014674static int intel_framebuffer_init(struct drm_device *dev,
14675 struct intel_framebuffer *intel_fb,
14676 struct drm_mode_fb_cmd2 *mode_cmd,
14677 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014678{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014679 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014680 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014681 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014682 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014683
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014684 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14685
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014686 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14687 /* Enforce that fb modifier and tiling mode match, but only for
14688 * X-tiled. This is needed for FBC. */
14689 if (!!(obj->tiling_mode == I915_TILING_X) !=
14690 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14691 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14692 return -EINVAL;
14693 }
14694 } else {
14695 if (obj->tiling_mode == I915_TILING_X)
14696 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14697 else if (obj->tiling_mode == I915_TILING_Y) {
14698 DRM_DEBUG("No Y tiling for legacy addfb\n");
14699 return -EINVAL;
14700 }
14701 }
14702
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014703 /* Passed in modifier sanity checking. */
14704 switch (mode_cmd->modifier[0]) {
14705 case I915_FORMAT_MOD_Y_TILED:
14706 case I915_FORMAT_MOD_Yf_TILED:
14707 if (INTEL_INFO(dev)->gen < 9) {
14708 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14709 mode_cmd->modifier[0]);
14710 return -EINVAL;
14711 }
14712 case DRM_FORMAT_MOD_NONE:
14713 case I915_FORMAT_MOD_X_TILED:
14714 break;
14715 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014716 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14717 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014718 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014719 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014720
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014721 stride_alignment = intel_fb_stride_alignment(dev_priv,
14722 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014723 mode_cmd->pixel_format);
14724 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14725 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14726 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014727 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014728 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014729
Damien Lespiaub3218032015-02-27 11:15:18 +000014730 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14731 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014732 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014733 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14734 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014735 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014736 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014737 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014738 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014739
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014740 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014741 mode_cmd->pitches[0] != obj->stride) {
14742 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14743 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014744 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014745 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014746
Ville Syrjälä57779d02012-10-31 17:50:14 +020014747 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014748 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014749 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014750 case DRM_FORMAT_RGB565:
14751 case DRM_FORMAT_XRGB8888:
14752 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014753 break;
14754 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014755 if (INTEL_INFO(dev)->gen > 3) {
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));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014758 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014759 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014760 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014761 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014762 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14763 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014764 DRM_DEBUG("unsupported pixel format: %s\n",
14765 drm_get_format_name(mode_cmd->pixel_format));
14766 return -EINVAL;
14767 }
14768 break;
14769 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014770 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014771 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014772 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014773 DRM_DEBUG("unsupported pixel format: %s\n",
14774 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014775 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014776 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014777 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014778 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014779 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014780 DRM_DEBUG("unsupported pixel format: %s\n",
14781 drm_get_format_name(mode_cmd->pixel_format));
14782 return -EINVAL;
14783 }
14784 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014785 case DRM_FORMAT_YUYV:
14786 case DRM_FORMAT_UYVY:
14787 case DRM_FORMAT_YVYU:
14788 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014789 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014790 DRM_DEBUG("unsupported pixel format: %s\n",
14791 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014792 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014793 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014794 break;
14795 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014796 DRM_DEBUG("unsupported pixel format: %s\n",
14797 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014798 return -EINVAL;
14799 }
14800
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014801 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14802 if (mode_cmd->offsets[0] != 0)
14803 return -EINVAL;
14804
Damien Lespiauec2c9812015-01-20 12:51:45 +000014805 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014806 mode_cmd->pixel_format,
14807 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014808 /* FIXME drm helper for size checks (especially planar formats)? */
14809 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14810 return -EINVAL;
14811
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014812 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14813 intel_fb->obj = obj;
14814
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014815 intel_fill_fb_info(dev_priv, &intel_fb->base);
14816
Jesse Barnes79e53942008-11-07 14:24:08 -080014817 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14818 if (ret) {
14819 DRM_ERROR("framebuffer init failed %d\n", ret);
14820 return ret;
14821 }
14822
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020014823 intel_fb->obj->framebuffer_references++;
14824
Jesse Barnes79e53942008-11-07 14:24:08 -080014825 return 0;
14826}
14827
Jesse Barnes79e53942008-11-07 14:24:08 -080014828static struct drm_framebuffer *
14829intel_user_framebuffer_create(struct drm_device *dev,
14830 struct drm_file *filp,
Ville Syrjälä1eb834512015-11-11 19:11:29 +020014831 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014832{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014833 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014834 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014835 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014836
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014837 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014838 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014839 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014840 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014841
Daniel Vetter92907cb2015-11-23 09:04:05 +010014842 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014843 if (IS_ERR(fb))
14844 drm_gem_object_unreference_unlocked(&obj->base);
14845
14846 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014847}
14848
Daniel Vetter06957262015-08-10 13:34:08 +020014849#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014850static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014851{
14852}
14853#endif
14854
Jesse Barnes79e53942008-11-07 14:24:08 -080014855static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014856 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014857 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014858 .atomic_check = intel_atomic_check,
14859 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014860 .atomic_state_alloc = intel_atomic_state_alloc,
14861 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014862};
14863
Imre Deak88212942016-03-16 13:38:53 +020014864/**
14865 * intel_init_display_hooks - initialize the display modesetting hooks
14866 * @dev_priv: device private
14867 */
14868void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014869{
Imre Deak88212942016-03-16 13:38:53 +020014870 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014871 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014872 dev_priv->display.get_initial_plane_config =
14873 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014874 dev_priv->display.crtc_compute_clock =
14875 haswell_crtc_compute_clock;
14876 dev_priv->display.crtc_enable = haswell_crtc_enable;
14877 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014878 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014879 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014880 dev_priv->display.get_initial_plane_config =
14881 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014882 dev_priv->display.crtc_compute_clock =
14883 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014884 dev_priv->display.crtc_enable = haswell_crtc_enable;
14885 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014886 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014887 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014888 dev_priv->display.get_initial_plane_config =
14889 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014890 dev_priv->display.crtc_compute_clock =
14891 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014892 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14893 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014894 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070014895 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014896 dev_priv->display.get_initial_plane_config =
14897 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020014898 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
14899 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14900 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14901 } else if (IS_VALLEYVIEW(dev_priv)) {
14902 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14903 dev_priv->display.get_initial_plane_config =
14904 i9xx_get_initial_plane_config;
14905 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014906 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14907 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020014908 } else if (IS_G4X(dev_priv)) {
14909 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14910 dev_priv->display.get_initial_plane_config =
14911 i9xx_get_initial_plane_config;
14912 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
14913 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14914 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020014915 } else if (IS_PINEVIEW(dev_priv)) {
14916 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14917 dev_priv->display.get_initial_plane_config =
14918 i9xx_get_initial_plane_config;
14919 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
14920 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14921 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014922 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014923 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014924 dev_priv->display.get_initial_plane_config =
14925 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014926 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014927 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14928 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020014929 } else {
14930 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14931 dev_priv->display.get_initial_plane_config =
14932 i9xx_get_initial_plane_config;
14933 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
14934 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14935 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070014936 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014937
Jesse Barnese70236a2009-09-21 10:42:27 -070014938 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020014939 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014940 dev_priv->display.get_display_clock_speed =
14941 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014942 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014943 dev_priv->display.get_display_clock_speed =
14944 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014945 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014946 dev_priv->display.get_display_clock_speed =
14947 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014948 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030014949 dev_priv->display.get_display_clock_speed =
14950 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014951 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014952 dev_priv->display.get_display_clock_speed =
14953 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014954 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014955 dev_priv->display.get_display_clock_speed =
14956 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014957 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
14958 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014959 dev_priv->display.get_display_clock_speed =
14960 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014961 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014962 dev_priv->display.get_display_clock_speed =
14963 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014964 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014965 dev_priv->display.get_display_clock_speed =
14966 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014967 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014968 dev_priv->display.get_display_clock_speed =
14969 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014970 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030014971 dev_priv->display.get_display_clock_speed =
14972 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014973 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014974 dev_priv->display.get_display_clock_speed =
14975 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014976 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014977 dev_priv->display.get_display_clock_speed =
14978 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014979 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014980 dev_priv->display.get_display_clock_speed =
14981 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014982 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014983 dev_priv->display.get_display_clock_speed =
14984 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020014985 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070014986 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014987 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014988 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020014989 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014990 dev_priv->display.get_display_clock_speed =
14991 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014992 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014993
Imre Deak88212942016-03-16 13:38:53 +020014994 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014995 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014996 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014997 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020014998 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014999 /* FIXME: detect B0+ stepping and use auto training */
15000 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015001 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015002 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015003 if (IS_BROADWELL(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015004 dev_priv->display.modeset_commit_cdclk =
15005 broadwell_modeset_commit_cdclk;
15006 dev_priv->display.modeset_calc_cdclk =
15007 broadwell_modeset_calc_cdclk;
15008 }
Imre Deak88212942016-03-16 13:38:53 +020015009 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015010 dev_priv->display.modeset_commit_cdclk =
15011 valleyview_modeset_commit_cdclk;
15012 dev_priv->display.modeset_calc_cdclk =
15013 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015014 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015015 dev_priv->display.modeset_commit_cdclk =
15016 broxton_modeset_commit_cdclk;
15017 dev_priv->display.modeset_calc_cdclk =
15018 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015019 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015020
Imre Deak88212942016-03-16 13:38:53 +020015021 switch (INTEL_INFO(dev_priv)->gen) {
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015022 case 2:
15023 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15024 break;
15025
15026 case 3:
15027 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15028 break;
15029
15030 case 4:
15031 case 5:
15032 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15033 break;
15034
15035 case 6:
15036 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15037 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015038 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070015039 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070015040 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15041 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000015042 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000015043 /* Drop through - unsupported since execlist only. */
15044 default:
15045 /* Default just returns -ENODEV to indicate unsupported */
15046 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070015047 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015048}
15049
Jesse Barnesb690e962010-07-19 13:53:12 -070015050/*
15051 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15052 * resume, or other times. This quirk makes sure that's the case for
15053 * affected systems.
15054 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015055static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015056{
15057 struct drm_i915_private *dev_priv = dev->dev_private;
15058
15059 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015060 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015061}
15062
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015063static void quirk_pipeb_force(struct drm_device *dev)
15064{
15065 struct drm_i915_private *dev_priv = dev->dev_private;
15066
15067 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15068 DRM_INFO("applying pipe b force quirk\n");
15069}
15070
Keith Packard435793d2011-07-12 14:56:22 -070015071/*
15072 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15073 */
15074static void quirk_ssc_force_disable(struct drm_device *dev)
15075{
15076 struct drm_i915_private *dev_priv = dev->dev_private;
15077 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015078 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015079}
15080
Carsten Emde4dca20e2012-03-15 15:56:26 +010015081/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015082 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15083 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015084 */
15085static void quirk_invert_brightness(struct drm_device *dev)
15086{
15087 struct drm_i915_private *dev_priv = dev->dev_private;
15088 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015089 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015090}
15091
Scot Doyle9c72cc62014-07-03 23:27:50 +000015092/* Some VBT's incorrectly indicate no backlight is present */
15093static void quirk_backlight_present(struct drm_device *dev)
15094{
15095 struct drm_i915_private *dev_priv = dev->dev_private;
15096 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15097 DRM_INFO("applying backlight present quirk\n");
15098}
15099
Jesse Barnesb690e962010-07-19 13:53:12 -070015100struct intel_quirk {
15101 int device;
15102 int subsystem_vendor;
15103 int subsystem_device;
15104 void (*hook)(struct drm_device *dev);
15105};
15106
Egbert Eich5f85f172012-10-14 15:46:38 +020015107/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15108struct intel_dmi_quirk {
15109 void (*hook)(struct drm_device *dev);
15110 const struct dmi_system_id (*dmi_id_list)[];
15111};
15112
15113static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15114{
15115 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15116 return 1;
15117}
15118
15119static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15120 {
15121 .dmi_id_list = &(const struct dmi_system_id[]) {
15122 {
15123 .callback = intel_dmi_reverse_brightness,
15124 .ident = "NCR Corporation",
15125 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15126 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15127 },
15128 },
15129 { } /* terminating entry */
15130 },
15131 .hook = quirk_invert_brightness,
15132 },
15133};
15134
Ben Widawskyc43b5632012-04-16 14:07:40 -070015135static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015136 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15137 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15138
Jesse Barnesb690e962010-07-19 13:53:12 -070015139 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15140 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15141
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015142 /* 830 needs to leave pipe A & dpll A up */
15143 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15144
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015145 /* 830 needs to leave pipe B & dpll B up */
15146 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15147
Keith Packard435793d2011-07-12 14:56:22 -070015148 /* Lenovo U160 cannot use SSC on LVDS */
15149 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015150
15151 /* Sony Vaio Y cannot use SSC on LVDS */
15152 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015153
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015154 /* Acer Aspire 5734Z must invert backlight brightness */
15155 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15156
15157 /* Acer/eMachines G725 */
15158 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15159
15160 /* Acer/eMachines e725 */
15161 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15162
15163 /* Acer/Packard Bell NCL20 */
15164 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15165
15166 /* Acer Aspire 4736Z */
15167 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015168
15169 /* Acer Aspire 5336 */
15170 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015171
15172 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15173 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015174
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015175 /* Acer C720 Chromebook (Core i3 4005U) */
15176 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15177
jens steinb2a96012014-10-28 20:25:53 +010015178 /* Apple Macbook 2,1 (Core 2 T7400) */
15179 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15180
Jani Nikula1b9448b02015-11-05 11:49:59 +020015181 /* Apple Macbook 4,1 */
15182 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15183
Scot Doyled4967d82014-07-03 23:27:52 +000015184 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15185 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015186
15187 /* HP Chromebook 14 (Celeron 2955U) */
15188 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015189
15190 /* Dell Chromebook 11 */
15191 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015192
15193 /* Dell Chromebook 11 (2015 version) */
15194 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015195};
15196
15197static void intel_init_quirks(struct drm_device *dev)
15198{
15199 struct pci_dev *d = dev->pdev;
15200 int i;
15201
15202 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15203 struct intel_quirk *q = &intel_quirks[i];
15204
15205 if (d->device == q->device &&
15206 (d->subsystem_vendor == q->subsystem_vendor ||
15207 q->subsystem_vendor == PCI_ANY_ID) &&
15208 (d->subsystem_device == q->subsystem_device ||
15209 q->subsystem_device == PCI_ANY_ID))
15210 q->hook(dev);
15211 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015212 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15213 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15214 intel_dmi_quirks[i].hook(dev);
15215 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015216}
15217
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015218/* Disable the VGA plane that we never use */
15219static void i915_disable_vga(struct drm_device *dev)
15220{
15221 struct drm_i915_private *dev_priv = dev->dev_private;
15222 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015223 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015224
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015225 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015226 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015227 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015228 sr1 = inb(VGA_SR_DATA);
15229 outb(sr1 | 1<<5, VGA_SR_DATA);
15230 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15231 udelay(300);
15232
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015233 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015234 POSTING_READ(vga_reg);
15235}
15236
Daniel Vetterf8175862012-04-10 15:50:11 +020015237void intel_modeset_init_hw(struct drm_device *dev)
15238{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015239 struct drm_i915_private *dev_priv = dev->dev_private;
15240
Ville Syrjäläb6283052015-06-03 15:45:07 +030015241 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015242
15243 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15244
Daniel Vetterf8175862012-04-10 15:50:11 +020015245 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015246 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015247}
15248
Matt Roperd93c0372015-12-03 11:37:41 -080015249/*
15250 * Calculate what we think the watermarks should be for the state we've read
15251 * out of the hardware and then immediately program those watermarks so that
15252 * we ensure the hardware settings match our internal state.
15253 *
15254 * We can calculate what we think WM's should be by creating a duplicate of the
15255 * current state (which was constructed during hardware readout) and running it
15256 * through the atomic check code to calculate new watermark values in the
15257 * state object.
15258 */
15259static void sanitize_watermarks(struct drm_device *dev)
15260{
15261 struct drm_i915_private *dev_priv = to_i915(dev);
15262 struct drm_atomic_state *state;
15263 struct drm_crtc *crtc;
15264 struct drm_crtc_state *cstate;
15265 struct drm_modeset_acquire_ctx ctx;
15266 int ret;
15267 int i;
15268
15269 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015270 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015271 return;
15272
15273 /*
15274 * We need to hold connection_mutex before calling duplicate_state so
15275 * that the connector loop is protected.
15276 */
15277 drm_modeset_acquire_init(&ctx, 0);
15278retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015279 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015280 if (ret == -EDEADLK) {
15281 drm_modeset_backoff(&ctx);
15282 goto retry;
15283 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015284 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015285 }
15286
15287 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15288 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015289 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015290
Matt Ropered4a6a72016-02-23 17:20:13 -080015291 /*
15292 * Hardware readout is the only time we don't want to calculate
15293 * intermediate watermarks (since we don't trust the current
15294 * watermarks).
15295 */
15296 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15297
Matt Roperd93c0372015-12-03 11:37:41 -080015298 ret = intel_atomic_check(dev, state);
15299 if (ret) {
15300 /*
15301 * If we fail here, it means that the hardware appears to be
15302 * programmed in a way that shouldn't be possible, given our
15303 * understanding of watermark requirements. This might mean a
15304 * mistake in the hardware readout code or a mistake in the
15305 * watermark calculations for a given platform. Raise a WARN
15306 * so that this is noticeable.
15307 *
15308 * If this actually happens, we'll have to just leave the
15309 * BIOS-programmed watermarks untouched and hope for the best.
15310 */
15311 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015312 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015313 }
15314
15315 /* Write calculated watermark values back */
15316 to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
15317 for_each_crtc_in_state(state, crtc, cstate, i) {
15318 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15319
Matt Ropered4a6a72016-02-23 17:20:13 -080015320 cs->wm.need_postvbl_update = true;
15321 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015322 }
15323
15324 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015325fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015326 drm_modeset_drop_locks(&ctx);
15327 drm_modeset_acquire_fini(&ctx);
15328}
15329
Jesse Barnes79e53942008-11-07 14:24:08 -080015330void intel_modeset_init(struct drm_device *dev)
15331{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015332 struct drm_i915_private *dev_priv = to_i915(dev);
15333 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015334 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015335 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015336 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015337
15338 drm_mode_config_init(dev);
15339
15340 dev->mode_config.min_width = 0;
15341 dev->mode_config.min_height = 0;
15342
Dave Airlie019d96c2011-09-29 16:20:42 +010015343 dev->mode_config.preferred_depth = 24;
15344 dev->mode_config.prefer_shadow = 1;
15345
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015346 dev->mode_config.allow_fb_modifiers = true;
15347
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015348 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015349
Jesse Barnesb690e962010-07-19 13:53:12 -070015350 intel_init_quirks(dev);
15351
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015352 intel_init_pm(dev);
15353
Ben Widawskye3c74752013-04-05 13:12:39 -070015354 if (INTEL_INFO(dev)->num_pipes == 0)
15355 return;
15356
Lukas Wunner69f92f62015-07-15 13:57:35 +020015357 /*
15358 * There may be no VBT; and if the BIOS enabled SSC we can
15359 * just keep using it to avoid unnecessary flicker. Whereas if the
15360 * BIOS isn't using it, don't assume it will work even if the VBT
15361 * indicates as much.
15362 */
15363 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15364 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15365 DREF_SSC1_ENABLE);
15366
15367 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15368 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15369 bios_lvds_use_ssc ? "en" : "dis",
15370 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15371 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15372 }
15373 }
15374
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015375 if (IS_GEN2(dev)) {
15376 dev->mode_config.max_width = 2048;
15377 dev->mode_config.max_height = 2048;
15378 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015379 dev->mode_config.max_width = 4096;
15380 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015381 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015382 dev->mode_config.max_width = 8192;
15383 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015384 }
Damien Lespiau068be562014-03-28 14:17:49 +000015385
Ville Syrjälädc41c152014-08-13 11:57:05 +030015386 if (IS_845G(dev) || IS_I865G(dev)) {
15387 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15388 dev->mode_config.cursor_height = 1023;
15389 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015390 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15391 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15392 } else {
15393 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15394 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15395 }
15396
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015397 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015398
Zhao Yakui28c97732009-10-09 11:39:41 +080015399 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015400 INTEL_INFO(dev)->num_pipes,
15401 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015402
Damien Lespiau055e3932014-08-18 13:49:10 +010015403 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015404 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015405 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015406 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015407 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015408 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015409 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015410 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015411 }
15412
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015413 intel_update_czclk(dev_priv);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +020015414 intel_update_rawclk(dev_priv);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015415 intel_update_cdclk(dev);
15416
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015417 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015418
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015419 /* Just disable it once at startup */
15420 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015421 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015422
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015423 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015424 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015425 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015426
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015427 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015428 struct intel_initial_plane_config plane_config = {};
15429
Jesse Barnes46f297f2014-03-07 08:57:48 -080015430 if (!crtc->active)
15431 continue;
15432
Jesse Barnes46f297f2014-03-07 08:57:48 -080015433 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015434 * Note that reserving the BIOS fb up front prevents us
15435 * from stuffing other stolen allocations like the ring
15436 * on top. This prevents some ugliness at boot time, and
15437 * can even allow for smooth boot transitions if the BIOS
15438 * fb is large enough for the active pipe configuration.
15439 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015440 dev_priv->display.get_initial_plane_config(crtc,
15441 &plane_config);
15442
15443 /*
15444 * If the fb is shared between multiple heads, we'll
15445 * just get the first one.
15446 */
15447 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015448 }
Matt Roperd93c0372015-12-03 11:37:41 -080015449
15450 /*
15451 * Make sure hardware watermarks really match the state we read out.
15452 * Note that we need to do this after reconstructing the BIOS fb's
15453 * since the watermark calculation done here will use pstate->fb.
15454 */
15455 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015456}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015457
Daniel Vetter7fad7982012-07-04 17:51:47 +020015458static void intel_enable_pipe_a(struct drm_device *dev)
15459{
15460 struct intel_connector *connector;
15461 struct drm_connector *crt = NULL;
15462 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015463 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015464
15465 /* We can't just switch on the pipe A, we need to set things up with a
15466 * proper mode and output configuration. As a gross hack, enable pipe A
15467 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015468 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015469 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15470 crt = &connector->base;
15471 break;
15472 }
15473 }
15474
15475 if (!crt)
15476 return;
15477
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015478 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015479 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015480}
15481
Daniel Vetterfa555832012-10-10 23:14:00 +020015482static bool
15483intel_check_plane_mapping(struct intel_crtc *crtc)
15484{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015485 struct drm_device *dev = crtc->base.dev;
15486 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015487 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015488
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015489 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015490 return true;
15491
Ville Syrjälä649636e2015-09-22 19:50:01 +030015492 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015493
15494 if ((val & DISPLAY_PLANE_ENABLE) &&
15495 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15496 return false;
15497
15498 return true;
15499}
15500
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015501static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15502{
15503 struct drm_device *dev = crtc->base.dev;
15504 struct intel_encoder *encoder;
15505
15506 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15507 return true;
15508
15509 return false;
15510}
15511
Ville Syrjälädd756192016-02-17 21:28:45 +020015512static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15513{
15514 struct drm_device *dev = encoder->base.dev;
15515 struct intel_connector *connector;
15516
15517 for_each_connector_on_encoder(dev, &encoder->base, connector)
15518 return true;
15519
15520 return false;
15521}
15522
Daniel Vetter24929352012-07-02 20:28:59 +020015523static void intel_sanitize_crtc(struct intel_crtc *crtc)
15524{
15525 struct drm_device *dev = crtc->base.dev;
15526 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015527 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015528
Daniel Vetter24929352012-07-02 20:28:59 +020015529 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015530 if (!transcoder_is_dsi(cpu_transcoder)) {
15531 i915_reg_t reg = PIPECONF(cpu_transcoder);
15532
15533 I915_WRITE(reg,
15534 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15535 }
Daniel Vetter24929352012-07-02 20:28:59 +020015536
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015537 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015538 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015539 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015540 struct intel_plane *plane;
15541
Daniel Vetter96256042015-02-13 21:03:42 +010015542 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015543
15544 /* Disable everything but the primary plane */
15545 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15546 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15547 continue;
15548
15549 plane->disable_plane(&plane->base, &crtc->base);
15550 }
Daniel Vetter96256042015-02-13 21:03:42 +010015551 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015552
Daniel Vetter24929352012-07-02 20:28:59 +020015553 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015554 * disable the crtc (and hence change the state) if it is wrong. Note
15555 * that gen4+ has a fixed plane -> pipe mapping. */
15556 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015557 bool plane;
15558
Daniel Vetter24929352012-07-02 20:28:59 +020015559 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15560 crtc->base.base.id);
15561
15562 /* Pipe has the wrong plane attached and the plane is active.
15563 * Temporarily change the plane mapping and disable everything
15564 * ... */
15565 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015566 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015567 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015568 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015569 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015570 }
Daniel Vetter24929352012-07-02 20:28:59 +020015571
Daniel Vetter7fad7982012-07-04 17:51:47 +020015572 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15573 crtc->pipe == PIPE_A && !crtc->active) {
15574 /* BIOS forgot to enable pipe A, this mostly happens after
15575 * resume. Force-enable the pipe to fix this, the update_dpms
15576 * call below we restore the pipe to the right state, but leave
15577 * the required bits on. */
15578 intel_enable_pipe_a(dev);
15579 }
15580
Daniel Vetter24929352012-07-02 20:28:59 +020015581 /* Adjust the state of the output pipe according to whether we
15582 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015583 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015584 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015585
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015586 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015587 /*
15588 * We start out with underrun reporting disabled to avoid races.
15589 * For correct bookkeeping mark this on active crtcs.
15590 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015591 * Also on gmch platforms we dont have any hardware bits to
15592 * disable the underrun reporting. Which means we need to start
15593 * out with underrun reporting disabled also on inactive pipes,
15594 * since otherwise we'll complain about the garbage we read when
15595 * e.g. coming up after runtime pm.
15596 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015597 * No protection against concurrent access is required - at
15598 * worst a fifo underrun happens which also sets this to false.
15599 */
15600 crtc->cpu_fifo_underrun_disabled = true;
15601 crtc->pch_fifo_underrun_disabled = true;
15602 }
Daniel Vetter24929352012-07-02 20:28:59 +020015603}
15604
15605static void intel_sanitize_encoder(struct intel_encoder *encoder)
15606{
15607 struct intel_connector *connector;
15608 struct drm_device *dev = encoder->base.dev;
15609
15610 /* We need to check both for a crtc link (meaning that the
15611 * encoder is active and trying to read from a pipe) and the
15612 * pipe itself being active. */
15613 bool has_active_crtc = encoder->base.crtc &&
15614 to_intel_crtc(encoder->base.crtc)->active;
15615
Ville Syrjälädd756192016-02-17 21:28:45 +020015616 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015617 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15618 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015619 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015620
15621 /* Connector is active, but has no active pipe. This is
15622 * fallout from our resume register restoring. Disable
15623 * the encoder manually again. */
15624 if (encoder->base.crtc) {
15625 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15626 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015627 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015628 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015629 if (encoder->post_disable)
15630 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015631 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015632 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015633
15634 /* Inconsistent output/port/pipe state happens presumably due to
15635 * a bug in one of the get_hw_state functions. Or someplace else
15636 * in our code, like the register restore mess on resume. Clamp
15637 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015638 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015639 if (connector->encoder != encoder)
15640 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015641 connector->base.dpms = DRM_MODE_DPMS_OFF;
15642 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015643 }
15644 }
15645 /* Enabled encoders without active connectors will be fixed in
15646 * the crtc fixup. */
15647}
15648
Imre Deak04098752014-02-18 00:02:16 +020015649void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015650{
15651 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015652 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015653
Imre Deak04098752014-02-18 00:02:16 +020015654 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15655 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15656 i915_disable_vga(dev);
15657 }
15658}
15659
15660void i915_redisable_vga(struct drm_device *dev)
15661{
15662 struct drm_i915_private *dev_priv = dev->dev_private;
15663
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015664 /* This function can be called both from intel_modeset_setup_hw_state or
15665 * at a very early point in our resume sequence, where the power well
15666 * structures are not yet restored. Since this function is at a very
15667 * paranoid "someone might have enabled VGA while we were not looking"
15668 * level, just check if the power well is enabled instead of trying to
15669 * follow the "don't touch the power well if we don't need it" policy
15670 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015671 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015672 return;
15673
Imre Deak04098752014-02-18 00:02:16 +020015674 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015675
15676 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015677}
15678
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015679static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015680{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015681 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015682
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015683 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015684}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015685
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015686/* FIXME read out full plane state for all planes */
15687static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015688{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015689 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015690 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015691 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015692
Matt Roper19b8d382015-09-24 15:53:17 -070015693 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015694 primary_get_hw_state(to_intel_plane(primary));
15695
15696 if (plane_state->visible)
15697 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015698}
15699
Daniel Vetter30e984d2013-06-05 13:34:17 +020015700static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015701{
15702 struct drm_i915_private *dev_priv = dev->dev_private;
15703 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015704 struct intel_crtc *crtc;
15705 struct intel_encoder *encoder;
15706 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015707 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015708
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015709 dev_priv->active_crtcs = 0;
15710
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015711 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015712 struct intel_crtc_state *crtc_state = crtc->config;
15713 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015714
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015715 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15716 memset(crtc_state, 0, sizeof(*crtc_state));
15717 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015718
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015719 crtc_state->base.active = crtc_state->base.enable =
15720 dev_priv->display.get_pipe_config(crtc, crtc_state);
15721
15722 crtc->base.enabled = crtc_state->base.enable;
15723 crtc->active = crtc_state->base.active;
15724
15725 if (crtc_state->base.active) {
15726 dev_priv->active_crtcs |= 1 << crtc->pipe;
15727
15728 if (IS_BROADWELL(dev_priv)) {
15729 pixclk = ilk_pipe_pixel_rate(crtc_state);
15730
15731 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15732 if (crtc_state->ips_enabled)
15733 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
15734 } else if (IS_VALLEYVIEW(dev_priv) ||
15735 IS_CHERRYVIEW(dev_priv) ||
15736 IS_BROXTON(dev_priv))
15737 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15738 else
15739 WARN_ON(dev_priv->display.modeset_calc_cdclk);
15740 }
15741
15742 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015743
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015744 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015745
15746 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15747 crtc->base.base.id,
15748 crtc->active ? "enabled" : "disabled");
15749 }
15750
Daniel Vetter53589012013-06-05 13:34:16 +020015751 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15752 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15753
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015754 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15755 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015756 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015757 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015758 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015759 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015760 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015761 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015762
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015763 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015764 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015765 }
15766
Damien Lespiaub2784e12014-08-05 11:29:37 +010015767 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015768 pipe = 0;
15769
15770 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015771 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15772 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015773 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015774 } else {
15775 encoder->base.crtc = NULL;
15776 }
15777
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015778 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015779 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015780 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015781 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015782 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015783 }
15784
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015785 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015786 if (connector->get_hw_state(connector)) {
15787 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015788
15789 encoder = connector->encoder;
15790 connector->base.encoder = &encoder->base;
15791
15792 if (encoder->base.crtc &&
15793 encoder->base.crtc->state->active) {
15794 /*
15795 * This has to be done during hardware readout
15796 * because anything calling .crtc_disable may
15797 * rely on the connector_mask being accurate.
15798 */
15799 encoder->base.crtc->state->connector_mask |=
15800 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015801 encoder->base.crtc->state->encoder_mask |=
15802 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015803 }
15804
Daniel Vetter24929352012-07-02 20:28:59 +020015805 } else {
15806 connector->base.dpms = DRM_MODE_DPMS_OFF;
15807 connector->base.encoder = NULL;
15808 }
15809 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15810 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015811 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015812 connector->base.encoder ? "enabled" : "disabled");
15813 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015814
15815 for_each_intel_crtc(dev, crtc) {
15816 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15817
15818 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15819 if (crtc->base.state->active) {
15820 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15821 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15822 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15823
15824 /*
15825 * The initial mode needs to be set in order to keep
15826 * the atomic core happy. It wants a valid mode if the
15827 * crtc's enabled, so we do the above call.
15828 *
15829 * At this point some state updated by the connectors
15830 * in their ->detect() callback has not run yet, so
15831 * no recalculation can be done yet.
15832 *
15833 * Even if we could do a recalculation and modeset
15834 * right now it would cause a double modeset if
15835 * fbdev or userspace chooses a different initial mode.
15836 *
15837 * If that happens, someone indicated they wanted a
15838 * mode change, which means it's safe to do a full
15839 * recalculation.
15840 */
15841 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015842
15843 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15844 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015845 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015846
15847 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015848 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015849}
15850
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015851/* Scan out the current hw modeset state,
15852 * and sanitizes it to the current state
15853 */
15854static void
15855intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015856{
15857 struct drm_i915_private *dev_priv = dev->dev_private;
15858 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015859 struct intel_crtc *crtc;
15860 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015861 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015862
15863 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015864
15865 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015866 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015867 intel_sanitize_encoder(encoder);
15868 }
15869
Damien Lespiau055e3932014-08-18 13:49:10 +010015870 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015871 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15872 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015873 intel_dump_pipe_config(crtc, crtc->config,
15874 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015875 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015876
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015877 intel_modeset_update_connector_atomic_state(dev);
15878
Daniel Vetter35c95372013-07-17 06:55:04 +020015879 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15880 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15881
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015882 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015883 continue;
15884
15885 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15886
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015887 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020015888 pll->on = false;
15889 }
15890
Wayne Boyer666a4532015-12-09 12:29:35 -080015891 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015892 vlv_wm_get_hw_state(dev);
15893 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015894 skl_wm_get_hw_state(dev);
15895 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015896 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015897
15898 for_each_intel_crtc(dev, crtc) {
15899 unsigned long put_domains;
15900
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010015901 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015902 if (WARN_ON(put_domains))
15903 modeset_put_power_domains(dev_priv, put_domains);
15904 }
15905 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020015906
15907 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015908}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015909
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015910void intel_display_resume(struct drm_device *dev)
15911{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015912 struct drm_i915_private *dev_priv = to_i915(dev);
15913 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
15914 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015915 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015916 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020015917
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015918 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015919
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015920 /*
15921 * This is a cludge because with real atomic modeset mode_config.mutex
15922 * won't be taken. Unfortunately some probed state like
15923 * audio_codec_enable is still protected by mode_config.mutex, so lock
15924 * it here for now.
15925 */
15926 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015927 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015928
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015929retry:
15930 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015931
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015932 if (ret == 0 && !setup) {
15933 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015934
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015935 intel_modeset_setup_hw_state(dev);
15936 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015937 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015938
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015939 if (ret == 0 && state) {
15940 struct drm_crtc_state *crtc_state;
15941 struct drm_crtc *crtc;
15942 int i;
15943
15944 state->acquire_ctx = &ctx;
15945
15946 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15947 /*
15948 * Force recalculation even if we restore
15949 * current state. With fast modeset this may not result
15950 * in a modeset when the state is compatible.
15951 */
15952 crtc_state->mode_changed = true;
15953 }
15954
15955 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015956 }
15957
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015958 if (ret == -EDEADLK) {
15959 drm_modeset_backoff(&ctx);
15960 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015961 }
15962
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015963 drm_modeset_drop_locks(&ctx);
15964 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010015965 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015966
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010015967 if (ret) {
15968 DRM_ERROR("Restoring old state failed with %i\n", ret);
15969 drm_atomic_state_free(state);
15970 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015971}
15972
15973void intel_modeset_gem_init(struct drm_device *dev)
15974{
Jesse Barnes484b41d2014-03-07 08:57:55 -080015975 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015976 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015977 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015978
Imre Deakae484342014-03-31 15:10:44 +030015979 intel_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +030015980
Chris Wilson1833b132012-05-09 11:56:28 +010015981 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015982
15983 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015984
15985 /*
15986 * Make sure any fbs we allocated at startup are properly
15987 * pinned & fenced. When we do the allocation it's too early
15988 * for this.
15989 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015990 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015991 obj = intel_fb_obj(c->primary->fb);
15992 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015993 continue;
15994
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015995 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020015996 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
15997 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015998 mutex_unlock(&dev->struct_mutex);
15999 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016000 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16001 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016002 drm_framebuffer_unreference(c->primary->fb);
16003 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016004 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080016005 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016006 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016007 }
16008 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016009
16010 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016011}
16012
Imre Deak4932e2c2014-02-11 17:12:48 +020016013void intel_connector_unregister(struct intel_connector *intel_connector)
16014{
16015 struct drm_connector *connector = &intel_connector->base;
16016
16017 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010016018 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016019}
16020
Jesse Barnes79e53942008-11-07 14:24:08 -080016021void intel_modeset_cleanup(struct drm_device *dev)
16022{
Jesse Barnes652c3932009-08-17 13:31:43 -070016023 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020016024 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070016025
Imre Deak2eb52522014-11-19 15:30:05 +020016026 intel_disable_gt_powersave(dev);
16027
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016028 intel_backlight_unregister(dev);
16029
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016030 /*
16031 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016032 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016033 * experience fancy races otherwise.
16034 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016035 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016036
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016037 /*
16038 * Due to the hpd irq storm handling the hotplug work can re-arm the
16039 * poll handlers. Hence disable polling after hpd handling is shut down.
16040 */
Keith Packardf87ea762010-10-03 19:36:26 -070016041 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016042
Jesse Barnes723bfd72010-10-07 16:01:13 -070016043 intel_unregister_dsm_handler();
16044
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016045 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016046
Chris Wilson1630fe72011-07-08 12:22:42 +010016047 /* flush any delayed tasks or pending work */
16048 flush_scheduled_work();
16049
Jani Nikuladb31af1d2013-11-08 16:48:53 +020016050 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020016051 for_each_intel_connector(dev, connector)
16052 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030016053
Jesse Barnes79e53942008-11-07 14:24:08 -080016054 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016055
16056 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030016057
Imre Deakae484342014-03-31 15:10:44 +030016058 intel_cleanup_gt_powersave(dev);
Daniel Vetterf5949142016-01-13 11:55:28 +010016059
16060 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016061}
16062
Dave Airlie28d52042009-09-21 14:33:58 +100016063/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080016064 * Return which encoder is currently attached for connector.
16065 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010016066struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080016067{
Chris Wilsondf0e9242010-09-09 16:20:55 +010016068 return &intel_attached_encoder(connector)->base;
16069}
Jesse Barnes79e53942008-11-07 14:24:08 -080016070
Chris Wilsondf0e9242010-09-09 16:20:55 +010016071void intel_connector_attach_encoder(struct intel_connector *connector,
16072 struct intel_encoder *encoder)
16073{
16074 connector->encoder = encoder;
16075 drm_mode_connector_attach_encoder(&connector->base,
16076 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016077}
Dave Airlie28d52042009-09-21 14:33:58 +100016078
16079/*
16080 * set vga decode state - true == enable VGA decode
16081 */
16082int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16083{
16084 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016085 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016086 u16 gmch_ctrl;
16087
Chris Wilson75fa0412014-02-07 18:37:02 -020016088 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16089 DRM_ERROR("failed to read control word\n");
16090 return -EIO;
16091 }
16092
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016093 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16094 return 0;
16095
Dave Airlie28d52042009-09-21 14:33:58 +100016096 if (state)
16097 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16098 else
16099 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016100
16101 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16102 DRM_ERROR("failed to write control word\n");
16103 return -EIO;
16104 }
16105
Dave Airlie28d52042009-09-21 14:33:58 +100016106 return 0;
16107}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016108
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016109struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016110
16111 u32 power_well_driver;
16112
Chris Wilson63b66e52013-08-08 15:12:06 +020016113 int num_transcoders;
16114
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016115 struct intel_cursor_error_state {
16116 u32 control;
16117 u32 position;
16118 u32 base;
16119 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016120 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016121
16122 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016123 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016124 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030016125 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016126 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016127
16128 struct intel_plane_error_state {
16129 u32 control;
16130 u32 stride;
16131 u32 size;
16132 u32 pos;
16133 u32 addr;
16134 u32 surface;
16135 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016136 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016137
16138 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016139 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016140 enum transcoder cpu_transcoder;
16141
16142 u32 conf;
16143
16144 u32 htotal;
16145 u32 hblank;
16146 u32 hsync;
16147 u32 vtotal;
16148 u32 vblank;
16149 u32 vsync;
16150 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016151};
16152
16153struct intel_display_error_state *
16154intel_display_capture_error_state(struct drm_device *dev)
16155{
Jani Nikulafbee40d2014-03-31 14:27:18 +030016156 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016157 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016158 int transcoders[] = {
16159 TRANSCODER_A,
16160 TRANSCODER_B,
16161 TRANSCODER_C,
16162 TRANSCODER_EDP,
16163 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016164 int i;
16165
Chris Wilson63b66e52013-08-08 15:12:06 +020016166 if (INTEL_INFO(dev)->num_pipes == 0)
16167 return NULL;
16168
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016169 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016170 if (error == NULL)
16171 return NULL;
16172
Imre Deak190be112013-11-25 17:15:31 +020016173 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016174 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16175
Damien Lespiau055e3932014-08-18 13:49:10 +010016176 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016177 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016178 __intel_display_power_is_enabled(dev_priv,
16179 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016180 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016181 continue;
16182
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016183 error->cursor[i].control = I915_READ(CURCNTR(i));
16184 error->cursor[i].position = I915_READ(CURPOS(i));
16185 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016186
16187 error->plane[i].control = I915_READ(DSPCNTR(i));
16188 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016189 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016190 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016191 error->plane[i].pos = I915_READ(DSPPOS(i));
16192 }
Paulo Zanonica291362013-03-06 20:03:14 -030016193 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
16194 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016195 if (INTEL_INFO(dev)->gen >= 4) {
16196 error->plane[i].surface = I915_READ(DSPSURF(i));
16197 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16198 }
16199
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016200 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030016201
Sonika Jindal3abfce72014-07-21 15:23:43 +053016202 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e12014-04-18 15:55:04 +030016203 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016204 }
16205
Jani Nikula4d1de972016-03-18 17:05:42 +020016206 /* Note: this does not include DSI transcoders. */
Chris Wilson63b66e52013-08-08 15:12:06 +020016207 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030016208 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020016209 error->num_transcoders++; /* Account for eDP. */
16210
16211 for (i = 0; i < error->num_transcoders; i++) {
16212 enum transcoder cpu_transcoder = transcoders[i];
16213
Imre Deakddf9c532013-11-27 22:02:02 +020016214 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016215 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016216 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016217 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016218 continue;
16219
Chris Wilson63b66e52013-08-08 15:12:06 +020016220 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16221
16222 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16223 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16224 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16225 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16226 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16227 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16228 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016229 }
16230
16231 return error;
16232}
16233
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016234#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16235
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016236void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016237intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016238 struct drm_device *dev,
16239 struct intel_display_error_state *error)
16240{
Damien Lespiau055e3932014-08-18 13:49:10 +010016241 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016242 int i;
16243
Chris Wilson63b66e52013-08-08 15:12:06 +020016244 if (!error)
16245 return;
16246
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016247 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016248 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016249 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016250 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016251 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016252 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016253 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016254 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016255 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030016256 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016257
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016258 err_printf(m, "Plane [%d]:\n", i);
16259 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16260 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016261 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016262 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16263 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016264 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016265 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016266 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016267 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016268 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16269 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016270 }
16271
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016272 err_printf(m, "Cursor [%d]:\n", i);
16273 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16274 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16275 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016276 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016277
16278 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016279 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016280 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016281 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016282 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016283 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16284 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16285 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16286 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16287 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16288 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16289 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16290 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016291}