blob: b21dc272bbacdb5973b3f0da1712744ad0a5a020 [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"
Chris Wilson5d723d72016-08-04 16:32:35 +010037#include "intel_frontbuffer.h"
David Howells760285e2012-10-02 18:01:07 +010038#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080039#include "i915_drv.h"
Imre Deakdb18b6a2016-03-24 12:41:40 +020040#include "intel_dsi.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070041#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080042#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080043#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010044#include <drm/drm_dp_helper.h>
45#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070046#include <drm/drm_plane_helper.h>
47#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080048#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080049#include <linux/reservation.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080050
Daniel Vetter5a21b662016-05-24 17:13:53 +020051static bool is_mmio_work(struct intel_flip_work *work)
52{
53 return work->mmio_work.func;
54}
55
Matt Roper465c1202014-05-29 08:06:54 -070056/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010057static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010058 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070060 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010061 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070062};
63
64/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010065static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010066 DRM_FORMAT_C8,
67 DRM_FORMAT_RGB565,
68 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070069 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010070 DRM_FORMAT_XRGB2101010,
71 DRM_FORMAT_XBGR2101010,
72};
73
74static const uint32_t skl_primary_formats[] = {
75 DRM_FORMAT_C8,
76 DRM_FORMAT_RGB565,
77 DRM_FORMAT_XRGB8888,
78 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010079 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070080 DRM_FORMAT_ABGR8888,
81 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070082 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053083 DRM_FORMAT_YUYV,
84 DRM_FORMAT_YVYU,
85 DRM_FORMAT_UYVY,
86 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070087};
88
Matt Roper3d7d6512014-06-10 08:28:13 -070089/* Cursor formats */
90static const uint32_t intel_cursor_formats[] = {
91 DRM_FORMAT_ARGB8888,
92};
93
Jesse Barnesf1f644d2013-06-27 00:39:25 +030094static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020095 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030096static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020097 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030098
Jesse Barneseb1bfe82014-02-12 12:26:25 -080099static int intel_framebuffer_init(struct drm_device *dev,
100 struct intel_framebuffer *ifb,
101 struct drm_mode_fb_cmd2 *mode_cmd,
102 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +0200103static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
104static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +0200105static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200106static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700107 struct intel_link_m_n *m_n,
108 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200109static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200110static void haswell_set_pipeconf(struct drm_crtc *crtc);
Jani Nikula391bf042016-03-18 17:05:40 +0200111static void haswell_set_pipemisc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200112static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200113 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200114static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200115 const struct intel_crtc_state *pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +0200116static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
117static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Ville Syrjälä65edccc2016-10-31 22:37:01 +0200118static void skl_init_scalers(struct drm_i915_private *dev_priv,
119 struct intel_crtc *crtc,
120 struct intel_crtc_state *crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200121static void skylake_pfit_enable(struct intel_crtc *crtc);
122static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
123static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200124static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200125static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Ville Syrjälä4e5ca602016-05-11 22:44:44 +0300126static int ilk_max_pixel_rate(struct drm_atomic_state *state);
Imre Deak324513c2016-06-13 16:44:36 +0300127static int bxt_calc_cdclk(int max_pixclk);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100128
Ma Lingd4906092009-03-18 20:13:27 +0800129struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300130 struct {
131 int min, max;
132 } dot, vco, n, m, m1, m2, p, p1;
133
134 struct {
135 int dot_limit;
136 int p2_slow, p2_fast;
137 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800138};
Jesse Barnes79e53942008-11-07 14:24:08 -0800139
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300140/* returns HPLL frequency in kHz */
141static int valleyview_get_vco(struct drm_i915_private *dev_priv)
142{
143 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
144
145 /* Obtain SKU information */
146 mutex_lock(&dev_priv->sb_lock);
147 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
148 CCK_FUSE_HPLL_FREQ_MASK;
149 mutex_unlock(&dev_priv->sb_lock);
150
151 return vco_freq[hpll_freq] * 1000;
152}
153
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200154int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
155 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300156{
157 u32 val;
158 int divider;
159
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300160 mutex_lock(&dev_priv->sb_lock);
161 val = vlv_cck_read(dev_priv, reg);
162 mutex_unlock(&dev_priv->sb_lock);
163
164 divider = val & CCK_FREQUENCY_VALUES;
165
166 WARN((val & CCK_FREQUENCY_STATUS) !=
167 (divider << CCK_FREQUENCY_STATUS_SHIFT),
168 "%s change in progress\n", name);
169
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200170 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
171}
172
173static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
174 const char *name, u32 reg)
175{
176 if (dev_priv->hpll_freq == 0)
177 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
178
179 return vlv_get_cck_clock(dev_priv, name, reg,
180 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300181}
182
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200183static int
184intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200185{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200186 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200187}
188
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200189static int
190intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300191{
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300192 /* RAWCLK_FREQ_VLV register updated from power well code */
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200193 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
194 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200195}
196
197static int
198intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
199{
Jani Nikula79e50a42015-08-26 10:58:20 +0300200 uint32_t clkcfg;
201
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200202 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300203 clkcfg = I915_READ(CLKCFG);
204 switch (clkcfg & CLKCFG_FSB_MASK) {
205 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200206 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300207 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200208 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300209 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200210 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300211 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200212 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300213 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200214 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300215 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200216 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300217 /* these two are just a guess; one of them might be right */
218 case CLKCFG_FSB_1600:
219 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200220 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300221 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200222 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300223 }
224}
225
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300226void intel_update_rawclk(struct drm_i915_private *dev_priv)
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200227{
228 if (HAS_PCH_SPLIT(dev_priv))
229 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
230 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
231 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
232 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
233 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
234 else
235 return; /* no rawclk on other platforms, or no need to know it */
236
237 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
238}
239
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300240static void intel_update_czclk(struct drm_i915_private *dev_priv)
241{
Wayne Boyer666a4532015-12-09 12:29:35 -0800242 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300243 return;
244
245 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
246 CCK_CZ_CLOCK_CONTROL);
247
248 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
249}
250
Chris Wilson021357a2010-09-07 20:54:59 +0100251static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200252intel_fdi_link_freq(struct drm_i915_private *dev_priv,
253 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100254{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200255 if (HAS_DDI(dev_priv))
256 return pipe_config->port_clock; /* SPLL */
257 else if (IS_GEN5(dev_priv))
258 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200259 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200260 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100261}
262
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300263static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400264 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200265 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200266 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400267 .m = { .min = 96, .max = 140 },
268 .m1 = { .min = 18, .max = 26 },
269 .m2 = { .min = 6, .max = 16 },
270 .p = { .min = 4, .max = 128 },
271 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700272 .p2 = { .dot_limit = 165000,
273 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700274};
275
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300276static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200277 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200278 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200279 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200280 .m = { .min = 96, .max = 140 },
281 .m1 = { .min = 18, .max = 26 },
282 .m2 = { .min = 6, .max = 16 },
283 .p = { .min = 4, .max = 128 },
284 .p1 = { .min = 2, .max = 33 },
285 .p2 = { .dot_limit = 165000,
286 .p2_slow = 4, .p2_fast = 4 },
287};
288
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300289static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400290 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200291 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200292 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400293 .m = { .min = 96, .max = 140 },
294 .m1 = { .min = 18, .max = 26 },
295 .m2 = { .min = 6, .max = 16 },
296 .p = { .min = 4, .max = 128 },
297 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700298 .p2 = { .dot_limit = 165000,
299 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700300};
Eric Anholt273e27c2011-03-30 13:01:10 -0700301
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300302static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400303 .dot = { .min = 20000, .max = 400000 },
304 .vco = { .min = 1400000, .max = 2800000 },
305 .n = { .min = 1, .max = 6 },
306 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100307 .m1 = { .min = 8, .max = 18 },
308 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400309 .p = { .min = 5, .max = 80 },
310 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700311 .p2 = { .dot_limit = 200000,
312 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700313};
314
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300315static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400316 .dot = { .min = 20000, .max = 400000 },
317 .vco = { .min = 1400000, .max = 2800000 },
318 .n = { .min = 1, .max = 6 },
319 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100320 .m1 = { .min = 8, .max = 18 },
321 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400322 .p = { .min = 7, .max = 98 },
323 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700324 .p2 = { .dot_limit = 112000,
325 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700326};
327
Eric Anholt273e27c2011-03-30 13:01:10 -0700328
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300329static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700330 .dot = { .min = 25000, .max = 270000 },
331 .vco = { .min = 1750000, .max = 3500000},
332 .n = { .min = 1, .max = 4 },
333 .m = { .min = 104, .max = 138 },
334 .m1 = { .min = 17, .max = 23 },
335 .m2 = { .min = 5, .max = 11 },
336 .p = { .min = 10, .max = 30 },
337 .p1 = { .min = 1, .max = 3},
338 .p2 = { .dot_limit = 270000,
339 .p2_slow = 10,
340 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800341 },
Keith Packarde4b36692009-06-05 19:22:17 -0700342};
343
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300344static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700345 .dot = { .min = 22000, .max = 400000 },
346 .vco = { .min = 1750000, .max = 3500000},
347 .n = { .min = 1, .max = 4 },
348 .m = { .min = 104, .max = 138 },
349 .m1 = { .min = 16, .max = 23 },
350 .m2 = { .min = 5, .max = 11 },
351 .p = { .min = 5, .max = 80 },
352 .p1 = { .min = 1, .max = 8},
353 .p2 = { .dot_limit = 165000,
354 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700355};
356
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300357static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700358 .dot = { .min = 20000, .max = 115000 },
359 .vco = { .min = 1750000, .max = 3500000 },
360 .n = { .min = 1, .max = 3 },
361 .m = { .min = 104, .max = 138 },
362 .m1 = { .min = 17, .max = 23 },
363 .m2 = { .min = 5, .max = 11 },
364 .p = { .min = 28, .max = 112 },
365 .p1 = { .min = 2, .max = 8 },
366 .p2 = { .dot_limit = 0,
367 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800368 },
Keith Packarde4b36692009-06-05 19:22:17 -0700369};
370
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300371static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700372 .dot = { .min = 80000, .max = 224000 },
373 .vco = { .min = 1750000, .max = 3500000 },
374 .n = { .min = 1, .max = 3 },
375 .m = { .min = 104, .max = 138 },
376 .m1 = { .min = 17, .max = 23 },
377 .m2 = { .min = 5, .max = 11 },
378 .p = { .min = 14, .max = 42 },
379 .p1 = { .min = 2, .max = 6 },
380 .p2 = { .dot_limit = 0,
381 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800382 },
Keith Packarde4b36692009-06-05 19:22:17 -0700383};
384
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300385static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400386 .dot = { .min = 20000, .max = 400000},
387 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700388 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400389 .n = { .min = 3, .max = 6 },
390 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700391 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400392 .m1 = { .min = 0, .max = 0 },
393 .m2 = { .min = 0, .max = 254 },
394 .p = { .min = 5, .max = 80 },
395 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700396 .p2 = { .dot_limit = 200000,
397 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700398};
399
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300400static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400401 .dot = { .min = 20000, .max = 400000 },
402 .vco = { .min = 1700000, .max = 3500000 },
403 .n = { .min = 3, .max = 6 },
404 .m = { .min = 2, .max = 256 },
405 .m1 = { .min = 0, .max = 0 },
406 .m2 = { .min = 0, .max = 254 },
407 .p = { .min = 7, .max = 112 },
408 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700409 .p2 = { .dot_limit = 112000,
410 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700411};
412
Eric Anholt273e27c2011-03-30 13:01:10 -0700413/* Ironlake / Sandybridge
414 *
415 * We calculate clock using (register_value + 2) for N/M1/M2, so here
416 * the range value for them is (actual_value - 2).
417 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300418static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700419 .dot = { .min = 25000, .max = 350000 },
420 .vco = { .min = 1760000, .max = 3510000 },
421 .n = { .min = 1, .max = 5 },
422 .m = { .min = 79, .max = 127 },
423 .m1 = { .min = 12, .max = 22 },
424 .m2 = { .min = 5, .max = 9 },
425 .p = { .min = 5, .max = 80 },
426 .p1 = { .min = 1, .max = 8 },
427 .p2 = { .dot_limit = 225000,
428 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700429};
430
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300431static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700432 .dot = { .min = 25000, .max = 350000 },
433 .vco = { .min = 1760000, .max = 3510000 },
434 .n = { .min = 1, .max = 3 },
435 .m = { .min = 79, .max = 118 },
436 .m1 = { .min = 12, .max = 22 },
437 .m2 = { .min = 5, .max = 9 },
438 .p = { .min = 28, .max = 112 },
439 .p1 = { .min = 2, .max = 8 },
440 .p2 = { .dot_limit = 225000,
441 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800442};
443
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300444static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700445 .dot = { .min = 25000, .max = 350000 },
446 .vco = { .min = 1760000, .max = 3510000 },
447 .n = { .min = 1, .max = 3 },
448 .m = { .min = 79, .max = 127 },
449 .m1 = { .min = 12, .max = 22 },
450 .m2 = { .min = 5, .max = 9 },
451 .p = { .min = 14, .max = 56 },
452 .p1 = { .min = 2, .max = 8 },
453 .p2 = { .dot_limit = 225000,
454 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800455};
456
Eric Anholt273e27c2011-03-30 13:01:10 -0700457/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300458static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700459 .dot = { .min = 25000, .max = 350000 },
460 .vco = { .min = 1760000, .max = 3510000 },
461 .n = { .min = 1, .max = 2 },
462 .m = { .min = 79, .max = 126 },
463 .m1 = { .min = 12, .max = 22 },
464 .m2 = { .min = 5, .max = 9 },
465 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400466 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700467 .p2 = { .dot_limit = 225000,
468 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800469};
470
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300471static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700472 .dot = { .min = 25000, .max = 350000 },
473 .vco = { .min = 1760000, .max = 3510000 },
474 .n = { .min = 1, .max = 3 },
475 .m = { .min = 79, .max = 126 },
476 .m1 = { .min = 12, .max = 22 },
477 .m2 = { .min = 5, .max = 9 },
478 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400479 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700480 .p2 = { .dot_limit = 225000,
481 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800482};
483
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300484static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300485 /*
486 * These are the data rate limits (measured in fast clocks)
487 * since those are the strictest limits we have. The fast
488 * clock and actual rate limits are more relaxed, so checking
489 * them would make no difference.
490 */
491 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200492 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700493 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700494 .m1 = { .min = 2, .max = 3 },
495 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300496 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300497 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700498};
499
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300500static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300501 /*
502 * These are the data rate limits (measured in fast clocks)
503 * since those are the strictest limits we have. The fast
504 * clock and actual rate limits are more relaxed, so checking
505 * them would make no difference.
506 */
507 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200508 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300509 .n = { .min = 1, .max = 1 },
510 .m1 = { .min = 2, .max = 2 },
511 .m2 = { .min = 24 << 22, .max = 175 << 22 },
512 .p1 = { .min = 2, .max = 4 },
513 .p2 = { .p2_slow = 1, .p2_fast = 14 },
514};
515
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300516static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200517 /* FIXME: find real dot limits */
518 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530519 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200520 .n = { .min = 1, .max = 1 },
521 .m1 = { .min = 2, .max = 2 },
522 /* FIXME: find real m2 limits */
523 .m2 = { .min = 2 << 22, .max = 255 << 22 },
524 .p1 = { .min = 2, .max = 4 },
525 .p2 = { .p2_slow = 1, .p2_fast = 20 },
526};
527
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200528static bool
529needs_modeset(struct drm_crtc_state *state)
530{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200531 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200532}
533
Imre Deakdccbea32015-06-22 23:35:51 +0300534/*
535 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
536 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
537 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
538 * The helpers' return value is the rate of the clock that is fed to the
539 * display engine's pipe which can be the above fast dot clock rate or a
540 * divided-down version of it.
541 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500542/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300543static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800544{
Shaohua Li21778322009-02-23 15:19:16 +0800545 clock->m = clock->m2 + 2;
546 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200547 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300548 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300549 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
550 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300551
552 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800553}
554
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200555static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
556{
557 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
558}
559
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300560static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800561{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200562 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800563 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200564 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300565 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300566 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
567 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300568
569 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800570}
571
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300572static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300573{
574 clock->m = clock->m1 * clock->m2;
575 clock->p = clock->p1 * clock->p2;
576 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300577 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300578 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
579 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300580
581 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300582}
583
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300584int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300585{
586 clock->m = clock->m1 * clock->m2;
587 clock->p = clock->p1 * clock->p2;
588 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300589 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300590 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
591 clock->n << 22);
592 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300593
594 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300595}
596
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800597#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800598/**
599 * Returns whether the given set of divisors are valid for a given refclk with
600 * the given connectors.
601 */
602
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100603static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300604 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300605 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800606{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300607 if (clock->n < limit->n.min || limit->n.max < clock->n)
608 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800609 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400610 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800611 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400612 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800613 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400614 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300615
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100616 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
617 !IS_CHERRYVIEW(dev_priv) && !IS_BROXTON(dev_priv))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300618 if (clock->m1 <= clock->m2)
619 INTELPllInvalid("m1 <= m2\n");
620
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100621 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
622 !IS_BROXTON(dev_priv)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300623 if (clock->p < limit->p.min || limit->p.max < clock->p)
624 INTELPllInvalid("p out of range\n");
625 if (clock->m < limit->m.min || limit->m.max < clock->m)
626 INTELPllInvalid("m out of range\n");
627 }
628
Jesse Barnes79e53942008-11-07 14:24:08 -0800629 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400630 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800631 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
632 * connector, etc., rather than just a single range.
633 */
634 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400635 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800636
637 return true;
638}
639
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300640static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300641i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300642 const struct intel_crtc_state *crtc_state,
643 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800644{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300645 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800646
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +0300647 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800648 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100649 * For LVDS just rely on its current settings for dual-channel.
650 * We haven't figured out how to reliably set up different
651 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800652 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100653 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300654 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800655 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300656 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800657 } else {
658 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300659 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800660 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300661 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800662 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300663}
664
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200665/*
666 * Returns a set of divisors for the desired target clock with the given
667 * refclk, or FALSE. The returned values represent the clock equation:
668 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
669 *
670 * Target and reference clocks are specified in kHz.
671 *
672 * If match_clock is provided, then best_clock P divider must match the P
673 * divider from @match_clock used for LVDS downclocking.
674 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300675static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300676i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300677 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300678 int target, int refclk, struct dpll *match_clock,
679 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300680{
681 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300682 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300683 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800684
Akshay Joshi0206e352011-08-16 15:34:10 -0400685 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800686
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300687 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
688
Zhao Yakui42158662009-11-20 11:24:18 +0800689 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
690 clock.m1++) {
691 for (clock.m2 = limit->m2.min;
692 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200693 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800694 break;
695 for (clock.n = limit->n.min;
696 clock.n <= limit->n.max; clock.n++) {
697 for (clock.p1 = limit->p1.min;
698 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800699 int this_err;
700
Imre Deakdccbea32015-06-22 23:35:51 +0300701 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100702 if (!intel_PLL_is_valid(to_i915(dev),
703 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000704 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800705 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800706 if (match_clock &&
707 clock.p != match_clock->p)
708 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800709
710 this_err = abs(clock.dot - target);
711 if (this_err < err) {
712 *best_clock = clock;
713 err = this_err;
714 }
715 }
716 }
717 }
718 }
719
720 return (err != target);
721}
722
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200723/*
724 * Returns a set of divisors for the desired target clock with the given
725 * refclk, or FALSE. The returned values represent the clock equation:
726 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
727 *
728 * Target and reference clocks are specified in kHz.
729 *
730 * If match_clock is provided, then best_clock P divider must match the P
731 * divider from @match_clock used for LVDS downclocking.
732 */
Ma Lingd4906092009-03-18 20:13:27 +0800733static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300734pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200735 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300736 int target, int refclk, struct dpll *match_clock,
737 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200738{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300739 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300740 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200741 int err = target;
742
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200743 memset(best_clock, 0, sizeof(*best_clock));
744
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300745 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
746
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200747 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
748 clock.m1++) {
749 for (clock.m2 = limit->m2.min;
750 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200751 for (clock.n = limit->n.min;
752 clock.n <= limit->n.max; clock.n++) {
753 for (clock.p1 = limit->p1.min;
754 clock.p1 <= limit->p1.max; clock.p1++) {
755 int this_err;
756
Imre Deakdccbea32015-06-22 23:35:51 +0300757 pnv_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100758 if (!intel_PLL_is_valid(to_i915(dev),
759 limit,
Jesse Barnes79e53942008-11-07 14:24:08 -0800760 &clock))
761 continue;
762 if (match_clock &&
763 clock.p != match_clock->p)
764 continue;
765
766 this_err = abs(clock.dot - target);
767 if (this_err < err) {
768 *best_clock = clock;
769 err = this_err;
770 }
771 }
772 }
773 }
774 }
775
776 return (err != target);
777}
778
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200779/*
780 * Returns a set of divisors for the desired target clock with the given
781 * refclk, or FALSE. The returned values represent the clock equation:
782 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200783 *
784 * Target and reference clocks are specified in kHz.
785 *
786 * If match_clock is provided, then best_clock P divider must match the P
787 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200788 */
Ma Lingd4906092009-03-18 20:13:27 +0800789static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300790g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200791 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300792 int target, int refclk, struct dpll *match_clock,
793 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800794{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300795 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300796 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800797 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300798 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400799 /* approximately equals target * 0.00585 */
800 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800801
802 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300803
804 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
805
Ma Lingd4906092009-03-18 20:13:27 +0800806 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200807 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800808 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200809 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800810 for (clock.m1 = limit->m1.max;
811 clock.m1 >= limit->m1.min; clock.m1--) {
812 for (clock.m2 = limit->m2.max;
813 clock.m2 >= limit->m2.min; clock.m2--) {
814 for (clock.p1 = limit->p1.max;
815 clock.p1 >= limit->p1.min; clock.p1--) {
816 int this_err;
817
Imre Deakdccbea32015-06-22 23:35:51 +0300818 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100819 if (!intel_PLL_is_valid(to_i915(dev),
820 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000821 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800822 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000823
824 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800825 if (this_err < err_most) {
826 *best_clock = clock;
827 err_most = this_err;
828 max_n = clock.n;
829 found = true;
830 }
831 }
832 }
833 }
834 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800835 return found;
836}
Ma Lingd4906092009-03-18 20:13:27 +0800837
Imre Deakd5dd62b2015-03-17 11:40:03 +0200838/*
839 * Check if the calculated PLL configuration is more optimal compared to the
840 * best configuration and error found so far. Return the calculated error.
841 */
842static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300843 const struct dpll *calculated_clock,
844 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200845 unsigned int best_error_ppm,
846 unsigned int *error_ppm)
847{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200848 /*
849 * For CHV ignore the error and consider only the P value.
850 * Prefer a bigger P value based on HW requirements.
851 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100852 if (IS_CHERRYVIEW(to_i915(dev))) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200853 *error_ppm = 0;
854
855 return calculated_clock->p > best_clock->p;
856 }
857
Imre Deak24be4e42015-03-17 11:40:04 +0200858 if (WARN_ON_ONCE(!target_freq))
859 return false;
860
Imre Deakd5dd62b2015-03-17 11:40:03 +0200861 *error_ppm = div_u64(1000000ULL *
862 abs(target_freq - calculated_clock->dot),
863 target_freq);
864 /*
865 * Prefer a better P value over a better (smaller) error if the error
866 * is small. Ensure this preference for future configurations too by
867 * setting the error to 0.
868 */
869 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
870 *error_ppm = 0;
871
872 return true;
873 }
874
875 return *error_ppm + 10 < best_error_ppm;
876}
877
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200878/*
879 * Returns a set of divisors for the desired target clock with the given
880 * refclk, or FALSE. The returned values represent the clock equation:
881 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
882 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800883static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300884vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200885 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300886 int target, int refclk, struct dpll *match_clock,
887 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700888{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200889 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300890 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300891 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300892 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300893 /* min update 19.2 MHz */
894 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300895 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700896
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300897 target *= 5; /* fast clock */
898
899 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700900
901 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300902 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300903 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300904 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300905 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300906 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700907 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300908 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200909 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300910
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300911 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
912 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300913
Imre Deakdccbea32015-06-22 23:35:51 +0300914 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300915
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100916 if (!intel_PLL_is_valid(to_i915(dev),
917 limit,
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300918 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300919 continue;
920
Imre Deakd5dd62b2015-03-17 11:40:03 +0200921 if (!vlv_PLL_is_optimal(dev, target,
922 &clock,
923 best_clock,
924 bestppm, &ppm))
925 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300926
Imre Deakd5dd62b2015-03-17 11:40:03 +0200927 *best_clock = clock;
928 bestppm = ppm;
929 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700930 }
931 }
932 }
933 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700934
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300935 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700936}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700937
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200938/*
939 * Returns a set of divisors for the desired target clock with the given
940 * refclk, or FALSE. The returned values represent the clock equation:
941 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
942 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300943static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300944chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200945 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300946 int target, int refclk, struct dpll *match_clock,
947 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300948{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200949 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300950 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200951 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300952 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300953 uint64_t m2;
954 int found = false;
955
956 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200957 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300958
959 /*
960 * Based on hardware doc, the n always set to 1, and m1 always
961 * set to 2. If requires to support 200Mhz refclk, we need to
962 * revisit this because n may not 1 anymore.
963 */
964 clock.n = 1, clock.m1 = 2;
965 target *= 5; /* fast clock */
966
967 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
968 for (clock.p2 = limit->p2.p2_fast;
969 clock.p2 >= limit->p2.p2_slow;
970 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200971 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300972
973 clock.p = clock.p1 * clock.p2;
974
975 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
976 clock.n) << 22, refclk * clock.m1);
977
978 if (m2 > INT_MAX/clock.m1)
979 continue;
980
981 clock.m2 = m2;
982
Imre Deakdccbea32015-06-22 23:35:51 +0300983 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300984
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100985 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300986 continue;
987
Imre Deak9ca3ba02015-03-17 11:40:05 +0200988 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
989 best_error_ppm, &error_ppm))
990 continue;
991
992 *best_clock = clock;
993 best_error_ppm = error_ppm;
994 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300995 }
996 }
997
998 return found;
999}
1000
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001001bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03001002 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001003{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001004 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03001005 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001006
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001007 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001008 target_clock, refclk, NULL, best_clock);
1009}
1010
Ville Syrjälä525b9312016-10-31 22:37:02 +02001011bool intel_crtc_active(struct intel_crtc *crtc)
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001012{
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001013 /* Be paranoid as we can arrive here with only partial
1014 * state retrieved from the hardware during setup.
1015 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001016 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001017 * as Haswell has gained clock readout/fastboot support.
1018 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001019 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001020 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001021 *
1022 * FIXME: The intel_crtc->active here should be switched to
1023 * crtc->state->active once we have proper CRTC states wired up
1024 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001025 */
Ville Syrjälä525b9312016-10-31 22:37:02 +02001026 return crtc->active && crtc->base.primary->state->fb &&
1027 crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001028}
1029
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001030enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1031 enum pipe pipe)
1032{
Ville Syrjälä98187832016-10-31 22:37:10 +02001033 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001034
Ville Syrjäläe2af48c2016-10-31 22:37:05 +02001035 return crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001036}
1037
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001038static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1039{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001040 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001041 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001042 u32 line1, line2;
1043 u32 line_mask;
1044
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001045 if (IS_GEN2(dev_priv))
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001046 line_mask = DSL_LINEMASK_GEN2;
1047 else
1048 line_mask = DSL_LINEMASK_GEN3;
1049
1050 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001051 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001052 line2 = I915_READ(reg) & line_mask;
1053
1054 return line1 == line2;
1055}
1056
Keith Packardab7ad7f2010-10-03 00:33:06 -07001057/*
1058 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001059 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001060 *
1061 * After disabling a pipe, we can't wait for vblank in the usual way,
1062 * spinning on the vblank interrupt status bit, since we won't actually
1063 * see an interrupt when the pipe is disabled.
1064 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001065 * On Gen4 and above:
1066 * wait for the pipe register state bit to turn off
1067 *
1068 * Otherwise:
1069 * wait for the display line value to settle (it usually
1070 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001071 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001072 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001073static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001074{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001075 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001076 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001077 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001078 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001079
Keith Packardab7ad7f2010-10-03 00:33:06 -07001080 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001081 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001082
Keith Packardab7ad7f2010-10-03 00:33:06 -07001083 /* Wait for the Pipe State to go off */
Chris Wilsonb8511f52016-06-30 15:32:53 +01001084 if (intel_wait_for_register(dev_priv,
1085 reg, I965_PIPECONF_ACTIVE, 0,
1086 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001087 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001088 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001089 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001090 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001091 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001092 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001093}
1094
Jesse Barnesb24e7172011-01-04 15:09:30 -08001095/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001096void assert_pll(struct drm_i915_private *dev_priv,
1097 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001098{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001099 u32 val;
1100 bool cur_state;
1101
Ville Syrjälä649636e2015-09-22 19:50:01 +03001102 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001103 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001104 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001105 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001106 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001107}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001108
Jani Nikula23538ef2013-08-27 15:12:22 +03001109/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001110void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001111{
1112 u32 val;
1113 bool cur_state;
1114
Ville Syrjäläa5805162015-05-26 20:42:30 +03001115 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001116 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001117 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001118
1119 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001120 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001121 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001122 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001123}
Jani Nikula23538ef2013-08-27 15:12:22 +03001124
Jesse Barnes040484a2011-01-03 12:14:26 -08001125static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1126 enum pipe pipe, bool state)
1127{
Jesse Barnes040484a2011-01-03 12:14:26 -08001128 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001129 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1130 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001131
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001132 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001133 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001134 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001135 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001136 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001137 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001138 cur_state = !!(val & FDI_TX_ENABLE);
1139 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001140 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001141 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001142 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001143}
1144#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1145#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1146
1147static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1148 enum pipe pipe, bool state)
1149{
Jesse Barnes040484a2011-01-03 12:14:26 -08001150 u32 val;
1151 bool cur_state;
1152
Ville Syrjälä649636e2015-09-22 19:50:01 +03001153 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001154 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001155 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001156 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001157 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001158}
1159#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1160#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1161
1162static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1163 enum pipe pipe)
1164{
Jesse Barnes040484a2011-01-03 12:14:26 -08001165 u32 val;
1166
1167 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001168 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001169 return;
1170
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001171 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001172 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001173 return;
1174
Ville Syrjälä649636e2015-09-22 19:50:01 +03001175 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001176 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 -08001177}
1178
Daniel Vetter55607e82013-06-16 21:42:39 +02001179void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1180 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001181{
Jesse Barnes040484a2011-01-03 12:14:26 -08001182 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001183 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001184
Ville Syrjälä649636e2015-09-22 19:50:01 +03001185 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001186 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001187 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001188 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001189 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001190}
1191
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001192void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001193{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001194 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001195 u32 val;
1196 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001197 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001198
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001199 if (WARN_ON(HAS_DDI(dev_priv)))
Jani Nikulabedd4db2014-08-22 15:04:13 +03001200 return;
1201
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001202 if (HAS_PCH_SPLIT(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001203 u32 port_sel;
1204
Imre Deak44cb7342016-08-10 14:07:29 +03001205 pp_reg = PP_CONTROL(0);
1206 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001207
1208 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1209 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1210 panel_pipe = PIPE_B;
1211 /* XXX: else fix for eDP */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001212 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001213 /* presumably write lock depends on pipe, not port select */
Imre Deak44cb7342016-08-10 14:07:29 +03001214 pp_reg = PP_CONTROL(pipe);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001215 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001216 } else {
Imre Deak44cb7342016-08-10 14:07:29 +03001217 pp_reg = PP_CONTROL(0);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001218 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1219 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001220 }
1221
1222 val = I915_READ(pp_reg);
1223 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001224 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001225 locked = false;
1226
Rob Clarke2c719b2014-12-15 13:56:32 -05001227 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001228 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001229 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001230}
1231
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001232static void assert_cursor(struct drm_i915_private *dev_priv,
1233 enum pipe pipe, bool state)
1234{
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001235 bool cur_state;
1236
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001237 if (IS_845G(dev_priv) || IS_I865G(dev_priv))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001238 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001239 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001240 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001241
Rob Clarke2c719b2014-12-15 13:56:32 -05001242 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001243 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001244 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001245}
1246#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1247#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1248
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001249void assert_pipe(struct drm_i915_private *dev_priv,
1250 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001251{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001252 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001253 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1254 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001255 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001256
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001257 /* if we need the pipe quirk it must be always on */
1258 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1259 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001260 state = true;
1261
Imre Deak4feed0e2016-02-12 18:55:14 +02001262 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1263 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001264 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001265 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001266
1267 intel_display_power_put(dev_priv, power_domain);
1268 } else {
1269 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001270 }
1271
Rob Clarke2c719b2014-12-15 13:56:32 -05001272 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001273 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001274 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001275}
1276
Chris Wilson931872f2012-01-16 23:01:13 +00001277static void assert_plane(struct drm_i915_private *dev_priv,
1278 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001279{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001280 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001281 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001282
Ville Syrjälä649636e2015-09-22 19:50:01 +03001283 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001284 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001285 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001286 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001287 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001288}
1289
Chris Wilson931872f2012-01-16 23:01:13 +00001290#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1291#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1292
Jesse Barnesb24e7172011-01-04 15:09:30 -08001293static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1294 enum pipe pipe)
1295{
Chris Wilson91c8a322016-07-05 10:40:23 +01001296 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001297 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001298
Ville Syrjälä653e1022013-06-04 13:49:05 +03001299 /* Primary planes are fixed to pipes on gen4+ */
1300 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001301 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001302 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001303 "plane %c assertion failure, should be disabled but not\n",
1304 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001305 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001306 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001307
Jesse Barnesb24e7172011-01-04 15:09:30 -08001308 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001309 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001310 u32 val = I915_READ(DSPCNTR(i));
1311 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001312 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001313 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001314 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1315 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001316 }
1317}
1318
Jesse Barnes19332d72013-03-28 09:55:38 -07001319static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1320 enum pipe pipe)
1321{
Chris Wilson91c8a322016-07-05 10:40:23 +01001322 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001323 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001324
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001325 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001326 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001327 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001328 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001329 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1330 sprite, pipe_name(pipe));
1331 }
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01001332 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001333 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001334 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001335 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001336 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001337 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001338 }
1339 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001340 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001341 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001342 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001343 plane_name(pipe), pipe_name(pipe));
1344 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001345 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001346 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001347 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1348 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001349 }
1350}
1351
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001352static void assert_vblank_disabled(struct drm_crtc *crtc)
1353{
Rob Clarke2c719b2014-12-15 13:56:32 -05001354 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001355 drm_crtc_vblank_put(crtc);
1356}
1357
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001358void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1359 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001360{
Jesse Barnes92f25842011-01-04 15:09:34 -08001361 u32 val;
1362 bool enabled;
1363
Ville Syrjälä649636e2015-09-22 19:50:01 +03001364 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001365 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001366 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001367 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1368 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001369}
1370
Keith Packard4e634382011-08-06 10:39:45 -07001371static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1372 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001373{
1374 if ((val & DP_PORT_EN) == 0)
1375 return false;
1376
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001377 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001378 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001379 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1380 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001381 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001382 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1383 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001384 } else {
1385 if ((val & DP_PIPE_MASK) != (pipe << 30))
1386 return false;
1387 }
1388 return true;
1389}
1390
Keith Packard1519b992011-08-06 10:35:34 -07001391static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1392 enum pipe pipe, u32 val)
1393{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001394 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001395 return false;
1396
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001397 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001398 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001399 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001400 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001401 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1402 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001403 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001404 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001405 return false;
1406 }
1407 return true;
1408}
1409
1410static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1411 enum pipe pipe, u32 val)
1412{
1413 if ((val & LVDS_PORT_EN) == 0)
1414 return false;
1415
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001416 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001417 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1418 return false;
1419 } else {
1420 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1421 return false;
1422 }
1423 return true;
1424}
1425
1426static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1427 enum pipe pipe, u32 val)
1428{
1429 if ((val & ADPA_DAC_ENABLE) == 0)
1430 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001431 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001432 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1433 return false;
1434 } else {
1435 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1436 return false;
1437 }
1438 return true;
1439}
1440
Jesse Barnes291906f2011-02-02 12:28:03 -08001441static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001442 enum pipe pipe, i915_reg_t reg,
1443 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001444{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001445 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001446 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001447 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001448 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001449
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001450 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001451 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001452 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001453}
1454
1455static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001456 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001457{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001458 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001459 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001460 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001461 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001462
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001463 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001464 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001465 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001466}
1467
1468static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1469 enum pipe pipe)
1470{
Jesse Barnes291906f2011-02-02 12:28:03 -08001471 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001472
Keith Packardf0575e92011-07-25 22:12:43 -07001473 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1474 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1475 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001476
Ville Syrjälä649636e2015-09-22 19:50:01 +03001477 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001478 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001479 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001480 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001481
Ville Syrjälä649636e2015-09-22 19:50:01 +03001482 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001483 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001484 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001485 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001486
Paulo Zanonie2debe92013-02-18 19:00:27 -03001487 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1488 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1489 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001490}
1491
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001492static void _vlv_enable_pll(struct intel_crtc *crtc,
1493 const struct intel_crtc_state *pipe_config)
1494{
1495 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1496 enum pipe pipe = crtc->pipe;
1497
1498 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1499 POSTING_READ(DPLL(pipe));
1500 udelay(150);
1501
Chris Wilson2c30b432016-06-30 15:32:54 +01001502 if (intel_wait_for_register(dev_priv,
1503 DPLL(pipe),
1504 DPLL_LOCK_VLV,
1505 DPLL_LOCK_VLV,
1506 1))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001507 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1508}
1509
Ville Syrjäläd288f652014-10-28 13:20:22 +02001510static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001511 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001512{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001513 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001514 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001515
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001516 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001517
Daniel Vetter87442f72013-06-06 00:52:17 +02001518 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001519 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001520
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001521 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1522 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001523
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001524 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1525 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001526}
1527
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001528
1529static void _chv_enable_pll(struct intel_crtc *crtc,
1530 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001531{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001532 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001533 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001534 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001535 u32 tmp;
1536
Ville Syrjäläa5805162015-05-26 20:42:30 +03001537 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001538
1539 /* Enable back the 10bit clock to display controller */
1540 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1541 tmp |= DPIO_DCLKP_EN;
1542 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1543
Ville Syrjälä54433e92015-05-26 20:42:31 +03001544 mutex_unlock(&dev_priv->sb_lock);
1545
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001546 /*
1547 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1548 */
1549 udelay(1);
1550
1551 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001552 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001553
1554 /* Check PLL is locked */
Chris Wilson6b188262016-06-30 15:32:55 +01001555 if (intel_wait_for_register(dev_priv,
1556 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1557 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001558 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001559}
1560
1561static void chv_enable_pll(struct intel_crtc *crtc,
1562 const struct intel_crtc_state *pipe_config)
1563{
1564 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1565 enum pipe pipe = crtc->pipe;
1566
1567 assert_pipe_disabled(dev_priv, pipe);
1568
1569 /* PLL is protected by panel, make sure we can write it */
1570 assert_panel_unlocked(dev_priv, pipe);
1571
1572 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1573 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001574
Ville Syrjäläc2317752016-03-15 16:39:56 +02001575 if (pipe != PIPE_A) {
1576 /*
1577 * WaPixelRepeatModeFixForC0:chv
1578 *
1579 * DPLLCMD is AWOL. Use chicken bits to propagate
1580 * the value from DPLLBMD to either pipe B or C.
1581 */
1582 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1583 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1584 I915_WRITE(CBR4_VLV, 0);
1585 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1586
1587 /*
1588 * DPLLB VGA mode also seems to cause problems.
1589 * We should always have it disabled.
1590 */
1591 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1592 } else {
1593 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1594 POSTING_READ(DPLL_MD(pipe));
1595 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001596}
1597
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001598static int intel_num_dvo_pipes(struct drm_device *dev)
1599{
1600 struct intel_crtc *crtc;
1601 int count = 0;
1602
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001603 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001604 count += crtc->base.state->active &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001605 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1606 }
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001607
1608 return count;
1609}
1610
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001611static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001612{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001613 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001614 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001615 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001616 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001617
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001618 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001619
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001620 /* PLL is protected by panel, make sure we can write it */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001621 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001622 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001623
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001624 /* Enable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001625 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev) > 0) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001626 /*
1627 * It appears to be important that we don't enable this
1628 * for the current pipe before otherwise configuring the
1629 * PLL. No idea how this should be handled if multiple
1630 * DVO outputs are enabled simultaneosly.
1631 */
1632 dpll |= DPLL_DVO_2X_MODE;
1633 I915_WRITE(DPLL(!crtc->pipe),
1634 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1635 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001636
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001637 /*
1638 * Apparently we need to have VGA mode enabled prior to changing
1639 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1640 * dividers, even though the register value does change.
1641 */
1642 I915_WRITE(reg, 0);
1643
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001644 I915_WRITE(reg, dpll);
1645
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001646 /* Wait for the clocks to stabilize. */
1647 POSTING_READ(reg);
1648 udelay(150);
1649
1650 if (INTEL_INFO(dev)->gen >= 4) {
1651 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001652 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001653 } else {
1654 /* The pixel multiplier can only be updated once the
1655 * DPLL is enabled and the clocks are stable.
1656 *
1657 * So write it again.
1658 */
1659 I915_WRITE(reg, dpll);
1660 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001661
1662 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001663 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001664 POSTING_READ(reg);
1665 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001666 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001667 POSTING_READ(reg);
1668 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001669 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001670 POSTING_READ(reg);
1671 udelay(150); /* wait for warmup */
1672}
1673
1674/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001675 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001676 * @dev_priv: i915 private structure
1677 * @pipe: pipe PLL to disable
1678 *
1679 * Disable the PLL for @pipe, making sure the pipe is off first.
1680 *
1681 * Note! This is for pre-ILK only.
1682 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001683static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001684{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001685 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001686 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001687 enum pipe pipe = crtc->pipe;
1688
1689 /* Disable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001690 if (IS_I830(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001691 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001692 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001693 I915_WRITE(DPLL(PIPE_B),
1694 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1695 I915_WRITE(DPLL(PIPE_A),
1696 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1697 }
1698
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001699 /* Don't disable pipe or pipe PLLs if needed */
1700 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1701 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001702 return;
1703
1704 /* Make sure the pipe isn't still relying on us */
1705 assert_pipe_disabled(dev_priv, pipe);
1706
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001707 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001708 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001709}
1710
Jesse Barnesf6071162013-10-01 10:41:38 -07001711static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1712{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001713 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001714
1715 /* Make sure the pipe isn't still relying on us */
1716 assert_pipe_disabled(dev_priv, pipe);
1717
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001718 val = DPLL_INTEGRATED_REF_CLK_VLV |
1719 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1720 if (pipe != PIPE_A)
1721 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1722
Jesse Barnesf6071162013-10-01 10:41:38 -07001723 I915_WRITE(DPLL(pipe), val);
1724 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001725}
1726
1727static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1728{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001729 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001730 u32 val;
1731
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001732 /* Make sure the pipe isn't still relying on us */
1733 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001734
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001735 val = DPLL_SSC_REF_CLK_CHV |
1736 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001737 if (pipe != PIPE_A)
1738 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001739
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001740 I915_WRITE(DPLL(pipe), val);
1741 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001742
Ville Syrjäläa5805162015-05-26 20:42:30 +03001743 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001744
1745 /* Disable 10bit clock to display controller */
1746 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1747 val &= ~DPIO_DCLKP_EN;
1748 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1749
Ville Syrjäläa5805162015-05-26 20:42:30 +03001750 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001751}
1752
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001753void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001754 struct intel_digital_port *dport,
1755 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001756{
1757 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001758 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001759
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001760 switch (dport->port) {
1761 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001762 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001763 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001764 break;
1765 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001766 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001767 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001768 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001769 break;
1770 case PORT_D:
1771 port_mask = DPLL_PORTD_READY_MASK;
1772 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001773 break;
1774 default:
1775 BUG();
1776 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001777
Chris Wilson370004d2016-06-30 15:32:56 +01001778 if (intel_wait_for_register(dev_priv,
1779 dpll_reg, port_mask, expected_mask,
1780 1000))
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001781 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1782 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001783}
1784
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001785static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1786 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001787{
Ville Syrjälä98187832016-10-31 22:37:10 +02001788 struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1789 pipe);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001790 i915_reg_t reg;
1791 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001792
Jesse Barnes040484a2011-01-03 12:14:26 -08001793 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001794 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001795
1796 /* FDI must be feeding us bits for PCH ports */
1797 assert_fdi_tx_enabled(dev_priv, pipe);
1798 assert_fdi_rx_enabled(dev_priv, pipe);
1799
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001800 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001801 /* Workaround: Set the timing override bit before enabling the
1802 * pch transcoder. */
1803 reg = TRANS_CHICKEN2(pipe);
1804 val = I915_READ(reg);
1805 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1806 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001807 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001808
Daniel Vetterab9412b2013-05-03 11:49:46 +02001809 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001810 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001811 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001812
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001813 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001814 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001815 * Make the BPC in transcoder be consistent with
1816 * that in pipeconf reg. For HDMI we must use 8bpc
1817 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001818 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001819 val &= ~PIPECONF_BPC_MASK;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001820 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001821 val |= PIPECONF_8BPC;
1822 else
1823 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001824 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001825
1826 val &= ~TRANS_INTERLACE_MASK;
1827 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001828 if (HAS_PCH_IBX(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001829 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001830 val |= TRANS_LEGACY_INTERLACED_ILK;
1831 else
1832 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001833 else
1834 val |= TRANS_PROGRESSIVE;
1835
Jesse Barnes040484a2011-01-03 12:14:26 -08001836 I915_WRITE(reg, val | TRANS_ENABLE);
Chris Wilson650fbd82016-06-30 15:32:57 +01001837 if (intel_wait_for_register(dev_priv,
1838 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1839 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001840 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001841}
1842
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001843static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001844 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001845{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001846 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001847
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001848 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001849 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001850 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001851
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001852 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001853 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001854 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001855 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001856
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001857 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001858 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001859
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001860 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1861 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001862 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001863 else
1864 val |= TRANS_PROGRESSIVE;
1865
Daniel Vetterab9412b2013-05-03 11:49:46 +02001866 I915_WRITE(LPT_TRANSCONF, val);
Chris Wilsond9f96242016-06-30 15:32:58 +01001867 if (intel_wait_for_register(dev_priv,
1868 LPT_TRANSCONF,
1869 TRANS_STATE_ENABLE,
1870 TRANS_STATE_ENABLE,
1871 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001872 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001873}
1874
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001875static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1876 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001877{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001878 i915_reg_t reg;
1879 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001880
1881 /* FDI relies on the transcoder */
1882 assert_fdi_tx_disabled(dev_priv, pipe);
1883 assert_fdi_rx_disabled(dev_priv, pipe);
1884
Jesse Barnes291906f2011-02-02 12:28:03 -08001885 /* Ports must be off as well */
1886 assert_pch_ports_disabled(dev_priv, pipe);
1887
Daniel Vetterab9412b2013-05-03 11:49:46 +02001888 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001889 val = I915_READ(reg);
1890 val &= ~TRANS_ENABLE;
1891 I915_WRITE(reg, val);
1892 /* wait for PCH transcoder off, transcoder state */
Chris Wilsona7d04662016-06-30 15:32:59 +01001893 if (intel_wait_for_register(dev_priv,
1894 reg, TRANS_STATE_ENABLE, 0,
1895 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001896 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001897
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001898 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001899 /* Workaround: Clear the timing override chicken bit again. */
1900 reg = TRANS_CHICKEN2(pipe);
1901 val = I915_READ(reg);
1902 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1903 I915_WRITE(reg, val);
1904 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001905}
1906
Maarten Lankhorstb7076542016-08-23 16:18:08 +02001907void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001908{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001909 u32 val;
1910
Daniel Vetterab9412b2013-05-03 11:49:46 +02001911 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001912 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001913 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001914 /* wait for PCH transcoder off, transcoder state */
Chris Wilsondfdb4742016-06-30 15:33:00 +01001915 if (intel_wait_for_register(dev_priv,
1916 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1917 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001918 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001919
1920 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001921 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001922 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001923 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001924}
1925
Ville Syrjälä65f21302016-10-14 20:02:53 +03001926enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1927{
1928 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1929
1930 WARN_ON(!crtc->config->has_pch_encoder);
1931
1932 if (HAS_PCH_LPT(dev_priv))
1933 return TRANSCODER_A;
1934 else
1935 return (enum transcoder) crtc->pipe;
1936}
1937
Jesse Barnes92f25842011-01-04 15:09:34 -08001938/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001939 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001940 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001941 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001942 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001943 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001944 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001945static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001946{
Paulo Zanoni03722642014-01-17 13:51:09 -02001947 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001948 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni03722642014-01-17 13:51:09 -02001949 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001950 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001951 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001952 u32 val;
1953
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001954 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1955
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001956 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001957 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001958 assert_sprites_disabled(dev_priv, pipe);
1959
Jesse Barnesb24e7172011-01-04 15:09:30 -08001960 /*
1961 * A pipe without a PLL won't actually be able to drive bits from
1962 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1963 * need the check.
1964 */
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001965 if (HAS_GMCH_DISPLAY(dev_priv)) {
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03001966 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03001967 assert_dsi_pll_enabled(dev_priv);
1968 else
1969 assert_pll_enabled(dev_priv, pipe);
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001970 } else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001971 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001972 /* if driving the PCH, we need FDI enabled */
Ville Syrjälä65f21302016-10-14 20:02:53 +03001973 assert_fdi_rx_pll_enabled(dev_priv,
1974 (enum pipe) intel_crtc_pch_transcoder(crtc));
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{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002016 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
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
Ville Syrjälä832be822016-01-12 21:08:33 +02002054static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2055{
2056 return IS_GEN2(dev_priv) ? 2048 : 4096;
2057}
2058
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002059static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2060 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002061{
2062 switch (fb_modifier) {
2063 case DRM_FORMAT_MOD_NONE:
2064 return cpp;
2065 case I915_FORMAT_MOD_X_TILED:
2066 if (IS_GEN2(dev_priv))
2067 return 128;
2068 else
2069 return 512;
2070 case I915_FORMAT_MOD_Y_TILED:
2071 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2072 return 128;
2073 else
2074 return 512;
2075 case I915_FORMAT_MOD_Yf_TILED:
2076 switch (cpp) {
2077 case 1:
2078 return 64;
2079 case 2:
2080 case 4:
2081 return 128;
2082 case 8:
2083 case 16:
2084 return 256;
2085 default:
2086 MISSING_CASE(cpp);
2087 return cpp;
2088 }
2089 break;
2090 default:
2091 MISSING_CASE(fb_modifier);
2092 return cpp;
2093 }
2094}
2095
Ville Syrjälä832be822016-01-12 21:08:33 +02002096unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2097 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002098{
Ville Syrjälä832be822016-01-12 21:08:33 +02002099 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2100 return 1;
2101 else
2102 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002103 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002104}
2105
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002106/* Return the tile dimensions in pixel units */
2107static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2108 unsigned int *tile_width,
2109 unsigned int *tile_height,
2110 uint64_t fb_modifier,
2111 unsigned int cpp)
2112{
2113 unsigned int tile_width_bytes =
2114 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2115
2116 *tile_width = tile_width_bytes / cpp;
2117 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2118}
2119
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002120unsigned int
2121intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002122 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002123{
Ville Syrjälä832be822016-01-12 21:08:33 +02002124 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2125 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2126
2127 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002128}
2129
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002130unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2131{
2132 unsigned int size = 0;
2133 int i;
2134
2135 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2136 size += rot_info->plane[i].width * rot_info->plane[i].height;
2137
2138 return size;
2139}
2140
Daniel Vetter75c82a52015-10-14 16:51:04 +02002141static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002142intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2143 const struct drm_framebuffer *fb,
2144 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002145{
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002146 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002147 *view = i915_ggtt_view_rotated;
2148 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2149 } else {
2150 *view = i915_ggtt_view_normal;
2151 }
2152}
2153
Ville Syrjälä603525d2016-01-12 21:08:37 +02002154static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002155{
2156 if (INTEL_INFO(dev_priv)->gen >= 9)
2157 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002158 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002159 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002160 return 128 * 1024;
2161 else if (INTEL_INFO(dev_priv)->gen >= 4)
2162 return 4 * 1024;
2163 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002164 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002165}
2166
Ville Syrjälä603525d2016-01-12 21:08:37 +02002167static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2168 uint64_t fb_modifier)
2169{
2170 switch (fb_modifier) {
2171 case DRM_FORMAT_MOD_NONE:
2172 return intel_linear_alignment(dev_priv);
2173 case I915_FORMAT_MOD_X_TILED:
2174 if (INTEL_INFO(dev_priv)->gen >= 9)
2175 return 256 * 1024;
2176 return 0;
2177 case I915_FORMAT_MOD_Y_TILED:
2178 case I915_FORMAT_MOD_Yf_TILED:
2179 return 1 * 1024 * 1024;
2180 default:
2181 MISSING_CASE(fb_modifier);
2182 return 0;
2183 }
2184}
2185
Chris Wilson058d88c2016-08-15 10:49:06 +01002186struct i915_vma *
2187intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002188{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002189 struct drm_device *dev = fb->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002190 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002191 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002192 struct i915_ggtt_view view;
Chris Wilson058d88c2016-08-15 10:49:06 +01002193 struct i915_vma *vma;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002194 u32 alignment;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002195
Matt Roperebcdd392014-07-09 16:22:11 -07002196 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2197
Ville Syrjälä603525d2016-01-12 21:08:37 +02002198 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002199
Ville Syrjälä3465c582016-02-15 22:54:43 +02002200 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002201
Chris Wilson693db182013-03-05 14:52:39 +00002202 /* Note that the w/a also requires 64 PTE of padding following the
2203 * bo. We currently fill all unused PTE with the shadow page and so
2204 * we should always have valid PTE following the scanout preventing
2205 * the VT-d warning.
2206 */
Chris Wilson48f112f2016-06-24 14:07:14 +01002207 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
Chris Wilson693db182013-03-05 14:52:39 +00002208 alignment = 256 * 1024;
2209
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002210 /*
2211 * Global gtt pte registers are special registers which actually forward
2212 * writes to a chunk of system memory. Which means that there is no risk
2213 * that the register values disappear as soon as we call
2214 * intel_runtime_pm_put(), so it is correct to wrap only the
2215 * pin/unpin/fence and not more.
2216 */
2217 intel_runtime_pm_get(dev_priv);
2218
Chris Wilson058d88c2016-08-15 10:49:06 +01002219 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
Chris Wilson49ef5292016-08-18 17:17:00 +01002220 if (IS_ERR(vma))
2221 goto err;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002222
Chris Wilson05a20d02016-08-18 17:16:55 +01002223 if (i915_vma_is_map_and_fenceable(vma)) {
Chris Wilson49ef5292016-08-18 17:17:00 +01002224 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2225 * fence, whereas 965+ only requires a fence if using
2226 * framebuffer compression. For simplicity, we always, when
2227 * possible, install a fence as the cost is not that onerous.
2228 *
2229 * If we fail to fence the tiled scanout, then either the
2230 * modeset will reject the change (which is highly unlikely as
2231 * the affected systems, all but one, do not have unmappable
2232 * space) or we will not be able to enable full powersaving
2233 * techniques (also likely not to apply due to various limits
2234 * FBC and the like impose on the size of the buffer, which
2235 * presumably we violated anyway with this unmappable buffer).
2236 * Anyway, it is presumably better to stumble onwards with
2237 * something and try to run the system in a "less than optimal"
2238 * mode that matches the user configuration.
2239 */
2240 if (i915_vma_get_fence(vma) == 0)
2241 i915_vma_pin_fence(vma);
Vivek Kasireddy98072162015-10-29 18:54:38 -07002242 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002243
Chris Wilson49ef5292016-08-18 17:17:00 +01002244err:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002245 intel_runtime_pm_put(dev_priv);
Chris Wilson058d88c2016-08-15 10:49:06 +01002246 return vma;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002247}
2248
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +01002249void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002250{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002251 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002252 struct i915_ggtt_view view;
Chris Wilson058d88c2016-08-15 10:49:06 +01002253 struct i915_vma *vma;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002254
Matt Roperebcdd392014-07-09 16:22:11 -07002255 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2256
Ville Syrjälä3465c582016-02-15 22:54:43 +02002257 intel_fill_fb_ggtt_view(&view, fb, rotation);
Chris Wilson05a20d02016-08-18 17:16:55 +01002258 vma = i915_gem_object_to_ggtt(obj, &view);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002259
Chris Wilson49ef5292016-08-18 17:17:00 +01002260 i915_vma_unpin_fence(vma);
Chris Wilson058d88c2016-08-15 10:49:06 +01002261 i915_gem_object_unpin_from_display_plane(vma);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002262}
2263
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002264static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2265 unsigned int rotation)
2266{
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002267 if (drm_rotation_90_or_270(rotation))
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002268 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2269 else
2270 return fb->pitches[plane];
2271}
2272
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002273/*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002274 * Convert the x/y offsets into a linear offset.
2275 * Only valid with 0/180 degree rotation, which is fine since linear
2276 * offset is only used with linear buffers on pre-hsw and tiled buffers
2277 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2278 */
2279u32 intel_fb_xy_to_linear(int x, int y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002280 const struct intel_plane_state *state,
2281 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002282{
Ville Syrjälä29490562016-01-20 18:02:50 +02002283 const struct drm_framebuffer *fb = state->base.fb;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002284 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2285 unsigned int pitch = fb->pitches[plane];
2286
2287 return y * pitch + x * cpp;
2288}
2289
2290/*
2291 * Add the x/y offsets derived from fb->offsets[] to the user
2292 * specified plane src x/y offsets. The resulting x/y offsets
2293 * specify the start of scanout from the beginning of the gtt mapping.
2294 */
2295void intel_add_fb_offsets(int *x, int *y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002296 const struct intel_plane_state *state,
2297 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002298
2299{
Ville Syrjälä29490562016-01-20 18:02:50 +02002300 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2301 unsigned int rotation = state->base.rotation;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002302
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002303 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä6687c902015-09-15 13:16:41 +03002304 *x += intel_fb->rotated[plane].x;
2305 *y += intel_fb->rotated[plane].y;
2306 } else {
2307 *x += intel_fb->normal[plane].x;
2308 *y += intel_fb->normal[plane].y;
2309 }
2310}
2311
2312/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002313 * Input tile dimensions and pitch must already be
2314 * rotated to match x and y, and in pixel units.
2315 */
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002316static u32 _intel_adjust_tile_offset(int *x, int *y,
2317 unsigned int tile_width,
2318 unsigned int tile_height,
2319 unsigned int tile_size,
2320 unsigned int pitch_tiles,
2321 u32 old_offset,
2322 u32 new_offset)
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002323{
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002324 unsigned int pitch_pixels = pitch_tiles * tile_width;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002325 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
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002336 /* minimize x in case it got needlessly big */
2337 *y += *x / pitch_pixels * tile_height;
2338 *x %= pitch_pixels;
2339
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002340 return new_offset;
2341}
2342
2343/*
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002344 * Adjust the tile offset by moving the difference into
2345 * the x/y offsets.
2346 */
2347static u32 intel_adjust_tile_offset(int *x, int *y,
2348 const struct intel_plane_state *state, int plane,
2349 u32 old_offset, u32 new_offset)
2350{
2351 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2352 const struct drm_framebuffer *fb = state->base.fb;
2353 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2354 unsigned int rotation = state->base.rotation;
2355 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2356
2357 WARN_ON(new_offset > old_offset);
2358
2359 if (fb->modifier[plane] != DRM_FORMAT_MOD_NONE) {
2360 unsigned int tile_size, tile_width, tile_height;
2361 unsigned int pitch_tiles;
2362
2363 tile_size = intel_tile_size(dev_priv);
2364 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2365 fb->modifier[plane], cpp);
2366
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002367 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002368 pitch_tiles = pitch / tile_height;
2369 swap(tile_width, tile_height);
2370 } else {
2371 pitch_tiles = pitch / (tile_width * cpp);
2372 }
2373
2374 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2375 tile_size, pitch_tiles,
2376 old_offset, new_offset);
2377 } else {
2378 old_offset += *y * pitch + *x * cpp;
2379
2380 *y = (old_offset - new_offset) / pitch;
2381 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2382 }
2383
2384 return new_offset;
2385}
2386
2387/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002388 * Computes the linear offset to the base tile and adjusts
2389 * x, y. bytes per pixel is assumed to be a power-of-two.
2390 *
2391 * In the 90/270 rotated case, x and y are assumed
2392 * to be already rotated to match the rotated GTT view, and
2393 * pitch is the tile_height aligned framebuffer height.
Ville Syrjälä6687c902015-09-15 13:16:41 +03002394 *
2395 * This function is used when computing the derived information
2396 * under intel_framebuffer, so using any of that information
2397 * here is not allowed. Anything under drm_framebuffer can be
2398 * used. This is why the user has to pass in the pitch since it
2399 * is specified in the rotated orientation.
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002400 */
Ville Syrjälä6687c902015-09-15 13:16:41 +03002401static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2402 int *x, int *y,
2403 const struct drm_framebuffer *fb, int plane,
2404 unsigned int pitch,
2405 unsigned int rotation,
2406 u32 alignment)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002407{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002408 uint64_t fb_modifier = fb->modifier[plane];
2409 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002410 u32 offset, offset_aligned;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002411
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002412 if (alignment)
2413 alignment--;
2414
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002415 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002416 unsigned int tile_size, tile_width, tile_height;
2417 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002418
Ville Syrjäläd8433102016-01-12 21:08:35 +02002419 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002420 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2421 fb_modifier, cpp);
2422
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002423 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002424 pitch_tiles = pitch / tile_height;
2425 swap(tile_width, tile_height);
2426 } else {
2427 pitch_tiles = pitch / (tile_width * cpp);
2428 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002429
Ville Syrjäläd8433102016-01-12 21:08:35 +02002430 tile_rows = *y / tile_height;
2431 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002432
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002433 tiles = *x / tile_width;
2434 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002435
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002436 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2437 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002438
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002439 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2440 tile_size, pitch_tiles,
2441 offset, offset_aligned);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002442 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002443 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002444 offset_aligned = offset & ~alignment;
2445
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002446 *y = (offset & alignment) / pitch;
2447 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002448 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002449
2450 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002451}
2452
Ville Syrjälä6687c902015-09-15 13:16:41 +03002453u32 intel_compute_tile_offset(int *x, int *y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002454 const struct intel_plane_state *state,
2455 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002456{
Ville Syrjälä29490562016-01-20 18:02:50 +02002457 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2458 const struct drm_framebuffer *fb = state->base.fb;
2459 unsigned int rotation = state->base.rotation;
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002460 int pitch = intel_fb_pitch(fb, plane, rotation);
Ville Syrjälä8d970652016-01-28 16:30:28 +02002461 u32 alignment;
2462
2463 /* AUX_DIST needs only 4K alignment */
2464 if (fb->pixel_format == DRM_FORMAT_NV12 && plane == 1)
2465 alignment = 4096;
2466 else
2467 alignment = intel_surf_alignment(dev_priv, fb->modifier[plane]);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002468
2469 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2470 rotation, alignment);
2471}
2472
2473/* Convert the fb->offset[] linear offset into x/y offsets */
2474static void intel_fb_offset_to_xy(int *x, int *y,
2475 const struct drm_framebuffer *fb, int plane)
2476{
2477 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2478 unsigned int pitch = fb->pitches[plane];
2479 u32 linear_offset = fb->offsets[plane];
2480
2481 *y = linear_offset / pitch;
2482 *x = linear_offset % pitch / cpp;
2483}
2484
Ville Syrjälä72618eb2016-02-04 20:38:20 +02002485static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2486{
2487 switch (fb_modifier) {
2488 case I915_FORMAT_MOD_X_TILED:
2489 return I915_TILING_X;
2490 case I915_FORMAT_MOD_Y_TILED:
2491 return I915_TILING_Y;
2492 default:
2493 return I915_TILING_NONE;
2494 }
2495}
2496
Ville Syrjälä6687c902015-09-15 13:16:41 +03002497static int
2498intel_fill_fb_info(struct drm_i915_private *dev_priv,
2499 struct drm_framebuffer *fb)
2500{
2501 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2502 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2503 u32 gtt_offset_rotated = 0;
2504 unsigned int max_size = 0;
2505 uint32_t format = fb->pixel_format;
2506 int i, num_planes = drm_format_num_planes(format);
2507 unsigned int tile_size = intel_tile_size(dev_priv);
2508
2509 for (i = 0; i < num_planes; i++) {
2510 unsigned int width, height;
2511 unsigned int cpp, size;
2512 u32 offset;
2513 int x, y;
2514
2515 cpp = drm_format_plane_cpp(format, i);
2516 width = drm_format_plane_width(fb->width, format, i);
2517 height = drm_format_plane_height(fb->height, format, i);
2518
2519 intel_fb_offset_to_xy(&x, &y, fb, i);
2520
2521 /*
Ville Syrjälä60d5f2a2016-01-22 18:41:24 +02002522 * The fence (if used) is aligned to the start of the object
2523 * so having the framebuffer wrap around across the edge of the
2524 * fenced region doesn't really work. We have no API to configure
2525 * the fence start offset within the object (nor could we probably
2526 * on gen2/3). So it's just easier if we just require that the
2527 * fb layout agrees with the fence layout. We already check that the
2528 * fb stride matches the fence stride elsewhere.
2529 */
2530 if (i915_gem_object_is_tiled(intel_fb->obj) &&
2531 (x + width) * cpp > fb->pitches[i]) {
2532 DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
2533 i, fb->offsets[i]);
2534 return -EINVAL;
2535 }
2536
2537 /*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002538 * First pixel of the framebuffer from
2539 * the start of the normal gtt mapping.
2540 */
2541 intel_fb->normal[i].x = x;
2542 intel_fb->normal[i].y = y;
2543
2544 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
2545 fb, 0, fb->pitches[i],
Daniel Vettercc926382016-08-15 10:41:47 +02002546 DRM_ROTATE_0, tile_size);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002547 offset /= tile_size;
2548
2549 if (fb->modifier[i] != DRM_FORMAT_MOD_NONE) {
2550 unsigned int tile_width, tile_height;
2551 unsigned int pitch_tiles;
2552 struct drm_rect r;
2553
2554 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2555 fb->modifier[i], cpp);
2556
2557 rot_info->plane[i].offset = offset;
2558 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2559 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2560 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2561
2562 intel_fb->rotated[i].pitch =
2563 rot_info->plane[i].height * tile_height;
2564
2565 /* how many tiles does this plane need */
2566 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2567 /*
2568 * If the plane isn't horizontally tile aligned,
2569 * we need one more tile.
2570 */
2571 if (x != 0)
2572 size++;
2573
2574 /* rotate the x/y offsets to match the GTT view */
2575 r.x1 = x;
2576 r.y1 = y;
2577 r.x2 = x + width;
2578 r.y2 = y + height;
2579 drm_rect_rotate(&r,
2580 rot_info->plane[i].width * tile_width,
2581 rot_info->plane[i].height * tile_height,
Daniel Vettercc926382016-08-15 10:41:47 +02002582 DRM_ROTATE_270);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002583 x = r.x1;
2584 y = r.y1;
2585
2586 /* rotate the tile dimensions to match the GTT view */
2587 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2588 swap(tile_width, tile_height);
2589
2590 /*
2591 * We only keep the x/y offsets, so push all of the
2592 * gtt offset into the x/y offsets.
2593 */
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002594 _intel_adjust_tile_offset(&x, &y, tile_size,
2595 tile_width, tile_height, pitch_tiles,
2596 gtt_offset_rotated * tile_size, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002597
2598 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2599
2600 /*
2601 * First pixel of the framebuffer from
2602 * the start of the rotated gtt mapping.
2603 */
2604 intel_fb->rotated[i].x = x;
2605 intel_fb->rotated[i].y = y;
2606 } else {
2607 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2608 x * cpp, tile_size);
2609 }
2610
2611 /* how many tiles in total needed in the bo */
2612 max_size = max(max_size, offset + size);
2613 }
2614
2615 if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
2616 DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
2617 max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
2618 return -EINVAL;
2619 }
2620
2621 return 0;
2622}
2623
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002624static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002625{
2626 switch (format) {
2627 case DISPPLANE_8BPP:
2628 return DRM_FORMAT_C8;
2629 case DISPPLANE_BGRX555:
2630 return DRM_FORMAT_XRGB1555;
2631 case DISPPLANE_BGRX565:
2632 return DRM_FORMAT_RGB565;
2633 default:
2634 case DISPPLANE_BGRX888:
2635 return DRM_FORMAT_XRGB8888;
2636 case DISPPLANE_RGBX888:
2637 return DRM_FORMAT_XBGR8888;
2638 case DISPPLANE_BGRX101010:
2639 return DRM_FORMAT_XRGB2101010;
2640 case DISPPLANE_RGBX101010:
2641 return DRM_FORMAT_XBGR2101010;
2642 }
2643}
2644
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002645static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2646{
2647 switch (format) {
2648 case PLANE_CTL_FORMAT_RGB_565:
2649 return DRM_FORMAT_RGB565;
2650 default:
2651 case PLANE_CTL_FORMAT_XRGB_8888:
2652 if (rgb_order) {
2653 if (alpha)
2654 return DRM_FORMAT_ABGR8888;
2655 else
2656 return DRM_FORMAT_XBGR8888;
2657 } else {
2658 if (alpha)
2659 return DRM_FORMAT_ARGB8888;
2660 else
2661 return DRM_FORMAT_XRGB8888;
2662 }
2663 case PLANE_CTL_FORMAT_XRGB_2101010:
2664 if (rgb_order)
2665 return DRM_FORMAT_XBGR2101010;
2666 else
2667 return DRM_FORMAT_XRGB2101010;
2668 }
2669}
2670
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002671static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002672intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2673 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002674{
2675 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002676 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002677 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002678 struct drm_i915_gem_object *obj = NULL;
2679 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002680 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002681 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2682 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2683 PAGE_SIZE);
2684
2685 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002686
Chris Wilsonff2652e2014-03-10 08:07:02 +00002687 if (plane_config->size == 0)
2688 return false;
2689
Paulo Zanoni3badb492015-09-23 12:52:23 -03002690 /* If the FB is too big, just don't use it since fbdev is not very
2691 * important and we should probably use that space with FBC or other
2692 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002693 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002694 return false;
2695
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002696 mutex_lock(&dev->struct_mutex);
2697
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002698 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2699 base_aligned,
2700 base_aligned,
2701 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002702 if (!obj) {
2703 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002704 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002705 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002706
Chris Wilson3e510a82016-08-05 10:14:23 +01002707 if (plane_config->tiling == I915_TILING_X)
2708 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002709
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002710 mode_cmd.pixel_format = fb->pixel_format;
2711 mode_cmd.width = fb->width;
2712 mode_cmd.height = fb->height;
2713 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002714 mode_cmd.modifier[0] = fb->modifier[0];
2715 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002716
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002717 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002718 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002719 DRM_DEBUG_KMS("intel fb init failed\n");
2720 goto out_unref_obj;
2721 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002722
Jesse Barnes46f297f2014-03-07 08:57:48 -08002723 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002724
Daniel Vetterf6936e22015-03-26 12:17:05 +01002725 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002726 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002727
2728out_unref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002729 i915_gem_object_put(obj);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002730 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002731 return false;
2732}
2733
Daniel Vetter5a21b662016-05-24 17:13:53 +02002734/* Update plane->state->fb to match plane->fb after driver-internal updates */
2735static void
2736update_state_fb(struct drm_plane *plane)
2737{
2738 if (plane->fb == plane->state->fb)
2739 return;
2740
2741 if (plane->state->fb)
2742 drm_framebuffer_unreference(plane->state->fb);
2743 plane->state->fb = plane->fb;
2744 if (plane->state->fb)
2745 drm_framebuffer_reference(plane->state->fb);
2746}
2747
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002748static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002749intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2750 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002751{
2752 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002753 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002754 struct drm_crtc *c;
2755 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002756 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002757 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002758 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002759 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2760 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002761 struct intel_plane_state *intel_state =
2762 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002763 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002764
Damien Lespiau2d140302015-02-05 17:22:18 +00002765 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002766 return;
2767
Daniel Vetterf6936e22015-03-26 12:17:05 +01002768 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002769 fb = &plane_config->fb->base;
2770 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002771 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002772
Damien Lespiau2d140302015-02-05 17:22:18 +00002773 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002774
2775 /*
2776 * Failed to alloc the obj, check to see if we should share
2777 * an fb with another CRTC instead
2778 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002779 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002780 i = to_intel_crtc(c);
2781
2782 if (c == &intel_crtc->base)
2783 continue;
2784
Matt Roper2ff8fde2014-07-08 07:50:07 -07002785 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002786 continue;
2787
Daniel Vetter88595ac2015-03-26 12:42:24 +01002788 fb = c->primary->fb;
2789 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002790 continue;
2791
Daniel Vetter88595ac2015-03-26 12:42:24 +01002792 obj = intel_fb_obj(fb);
Chris Wilson058d88c2016-08-15 10:49:06 +01002793 if (i915_gem_object_ggtt_offset(obj, NULL) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002794 drm_framebuffer_reference(fb);
2795 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002796 }
2797 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002798
Matt Roper200757f2015-12-03 11:37:36 -08002799 /*
2800 * We've failed to reconstruct the BIOS FB. Current display state
2801 * indicates that the primary plane is visible, but has a NULL FB,
2802 * which will lead to problems later if we don't fix it up. The
2803 * simplest solution is to just disable the primary plane now and
2804 * pretend the BIOS never had it enabled.
2805 */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002806 to_intel_plane_state(plane_state)->base.visible = false;
Matt Roper200757f2015-12-03 11:37:36 -08002807 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002808 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002809 intel_plane->disable_plane(primary, &intel_crtc->base);
2810
Daniel Vetter88595ac2015-03-26 12:42:24 +01002811 return;
2812
2813valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002814 plane_state->src_x = 0;
2815 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002816 plane_state->src_w = fb->width << 16;
2817 plane_state->src_h = fb->height << 16;
2818
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002819 plane_state->crtc_x = 0;
2820 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002821 plane_state->crtc_w = fb->width;
2822 plane_state->crtc_h = fb->height;
2823
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002824 intel_state->base.src.x1 = plane_state->src_x;
2825 intel_state->base.src.y1 = plane_state->src_y;
2826 intel_state->base.src.x2 = plane_state->src_x + plane_state->src_w;
2827 intel_state->base.src.y2 = plane_state->src_y + plane_state->src_h;
2828 intel_state->base.dst.x1 = plane_state->crtc_x;
2829 intel_state->base.dst.y1 = plane_state->crtc_y;
2830 intel_state->base.dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2831 intel_state->base.dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
Matt Roper0a8d8a82015-12-03 11:37:38 -08002832
Daniel Vetter88595ac2015-03-26 12:42:24 +01002833 obj = intel_fb_obj(fb);
Chris Wilson3e510a82016-08-05 10:14:23 +01002834 if (i915_gem_object_is_tiled(obj))
Daniel Vetter88595ac2015-03-26 12:42:24 +01002835 dev_priv->preserve_bios_swizzle = true;
2836
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002837 drm_framebuffer_reference(fb);
2838 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002839 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002840 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01002841 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2842 &obj->frontbuffer_bits);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002843}
2844
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002845static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2846 unsigned int rotation)
2847{
2848 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2849
2850 switch (fb->modifier[plane]) {
2851 case DRM_FORMAT_MOD_NONE:
2852 case I915_FORMAT_MOD_X_TILED:
2853 switch (cpp) {
2854 case 8:
2855 return 4096;
2856 case 4:
2857 case 2:
2858 case 1:
2859 return 8192;
2860 default:
2861 MISSING_CASE(cpp);
2862 break;
2863 }
2864 break;
2865 case I915_FORMAT_MOD_Y_TILED:
2866 case I915_FORMAT_MOD_Yf_TILED:
2867 switch (cpp) {
2868 case 8:
2869 return 2048;
2870 case 4:
2871 return 4096;
2872 case 2:
2873 case 1:
2874 return 8192;
2875 default:
2876 MISSING_CASE(cpp);
2877 break;
2878 }
2879 break;
2880 default:
2881 MISSING_CASE(fb->modifier[plane]);
2882 }
2883
2884 return 2048;
2885}
2886
2887static int skl_check_main_surface(struct intel_plane_state *plane_state)
2888{
2889 const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
2890 const struct drm_framebuffer *fb = plane_state->base.fb;
2891 unsigned int rotation = plane_state->base.rotation;
Daniel Vettercc926382016-08-15 10:41:47 +02002892 int x = plane_state->base.src.x1 >> 16;
2893 int y = plane_state->base.src.y1 >> 16;
2894 int w = drm_rect_width(&plane_state->base.src) >> 16;
2895 int h = drm_rect_height(&plane_state->base.src) >> 16;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002896 int max_width = skl_max_plane_width(fb, 0, rotation);
2897 int max_height = 4096;
Ville Syrjälä8d970652016-01-28 16:30:28 +02002898 u32 alignment, offset, aux_offset = plane_state->aux.offset;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002899
2900 if (w > max_width || h > max_height) {
2901 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2902 w, h, max_width, max_height);
2903 return -EINVAL;
2904 }
2905
2906 intel_add_fb_offsets(&x, &y, plane_state, 0);
2907 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
2908
2909 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
2910
2911 /*
Ville Syrjälä8d970652016-01-28 16:30:28 +02002912 * AUX surface offset is specified as the distance from the
2913 * main surface offset, and it must be non-negative. Make
2914 * sure that is what we will get.
2915 */
2916 if (offset > aux_offset)
2917 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2918 offset, aux_offset & ~(alignment - 1));
2919
2920 /*
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002921 * When using an X-tiled surface, the plane blows up
2922 * if the x offset + width exceed the stride.
2923 *
2924 * TODO: linear and Y-tiled seem fine, Yf untested,
2925 */
2926 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED) {
2927 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
2928
2929 while ((x + w) * cpp > fb->pitches[0]) {
2930 if (offset == 0) {
2931 DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
2932 return -EINVAL;
2933 }
2934
2935 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2936 offset, offset - alignment);
2937 }
2938 }
2939
2940 plane_state->main.offset = offset;
2941 plane_state->main.x = x;
2942 plane_state->main.y = y;
2943
2944 return 0;
2945}
2946
Ville Syrjälä8d970652016-01-28 16:30:28 +02002947static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
2948{
2949 const struct drm_framebuffer *fb = plane_state->base.fb;
2950 unsigned int rotation = plane_state->base.rotation;
2951 int max_width = skl_max_plane_width(fb, 1, rotation);
2952 int max_height = 4096;
Daniel Vettercc926382016-08-15 10:41:47 +02002953 int x = plane_state->base.src.x1 >> 17;
2954 int y = plane_state->base.src.y1 >> 17;
2955 int w = drm_rect_width(&plane_state->base.src) >> 17;
2956 int h = drm_rect_height(&plane_state->base.src) >> 17;
Ville Syrjälä8d970652016-01-28 16:30:28 +02002957 u32 offset;
2958
2959 intel_add_fb_offsets(&x, &y, plane_state, 1);
2960 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
2961
2962 /* FIXME not quite sure how/if these apply to the chroma plane */
2963 if (w > max_width || h > max_height) {
2964 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
2965 w, h, max_width, max_height);
2966 return -EINVAL;
2967 }
2968
2969 plane_state->aux.offset = offset;
2970 plane_state->aux.x = x;
2971 plane_state->aux.y = y;
2972
2973 return 0;
2974}
2975
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002976int skl_check_plane_surface(struct intel_plane_state *plane_state)
2977{
2978 const struct drm_framebuffer *fb = plane_state->base.fb;
2979 unsigned int rotation = plane_state->base.rotation;
2980 int ret;
2981
2982 /* Rotate src coordinates to match rotated GTT view */
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002983 if (drm_rotation_90_or_270(rotation))
Daniel Vettercc926382016-08-15 10:41:47 +02002984 drm_rect_rotate(&plane_state->base.src,
Ville Syrjäläda064b42016-10-24 19:13:04 +03002985 fb->width << 16, fb->height << 16,
2986 DRM_ROTATE_270);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002987
Ville Syrjälä8d970652016-01-28 16:30:28 +02002988 /*
2989 * Handle the AUX surface first since
2990 * the main surface setup depends on it.
2991 */
2992 if (fb->pixel_format == DRM_FORMAT_NV12) {
2993 ret = skl_check_nv12_aux_surface(plane_state);
2994 if (ret)
2995 return ret;
2996 } else {
2997 plane_state->aux.offset = ~0xfff;
2998 plane_state->aux.x = 0;
2999 plane_state->aux.y = 0;
3000 }
3001
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003002 ret = skl_check_main_surface(plane_state);
3003 if (ret)
3004 return ret;
3005
3006 return 0;
3007}
3008
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003009static void i9xx_update_primary_plane(struct drm_plane *primary,
3010 const struct intel_crtc_state *crtc_state,
3011 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07003012{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003013 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003014 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003015 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3016 struct drm_framebuffer *fb = plane_state->base.fb;
3017 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07003018 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02003019 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07003020 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003021 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02003022 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003023 int x = plane_state->base.src.x1 >> 16;
3024 int y = plane_state->base.src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03003025
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003026 dspcntr = DISPPLANE_GAMMA_ENABLE;
3027
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03003028 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003029
3030 if (INTEL_INFO(dev)->gen < 4) {
3031 if (intel_crtc->pipe == PIPE_B)
3032 dspcntr |= DISPPLANE_SEL_PIPE_B;
3033
3034 /* pipesrc and dspsize control the size that is scaled from,
3035 * which should always be the user's requested size.
3036 */
3037 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003038 ((crtc_state->pipe_src_h - 1) << 16) |
3039 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003040 I915_WRITE(DSPPOS(plane), 0);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01003041 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
Ville Syrjäläc14b0482014-10-16 20:52:34 +03003042 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003043 ((crtc_state->pipe_src_h - 1) << 16) |
3044 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03003045 I915_WRITE(PRIMPOS(plane), 0);
3046 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003047 }
3048
Ville Syrjälä57779d02012-10-31 17:50:14 +02003049 switch (fb->pixel_format) {
3050 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07003051 dspcntr |= DISPPLANE_8BPP;
3052 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003053 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003054 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07003055 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003056 case DRM_FORMAT_RGB565:
3057 dspcntr |= DISPPLANE_BGRX565;
3058 break;
3059 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003060 dspcntr |= DISPPLANE_BGRX888;
3061 break;
3062 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003063 dspcntr |= DISPPLANE_RGBX888;
3064 break;
3065 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003066 dspcntr |= DISPPLANE_BGRX101010;
3067 break;
3068 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003069 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07003070 break;
3071 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01003072 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07003073 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02003074
Ville Syrjälä72618eb2016-02-04 20:38:20 +02003075 if (INTEL_GEN(dev_priv) >= 4 &&
3076 fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003077 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07003078
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01003079 if (IS_G4X(dev_priv))
Ville Syrjäläde1aa622013-06-07 10:47:01 +03003080 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
3081
Ville Syrjälä29490562016-01-20 18:02:50 +02003082 intel_add_fb_offsets(&x, &y, plane_state, 0);
Jesse Barnes81255562010-08-02 12:07:50 -07003083
Ville Syrjälä6687c902015-09-15 13:16:41 +03003084 if (INTEL_INFO(dev)->gen >= 4)
Daniel Vetterc2c75132012-07-05 12:17:30 +02003085 intel_crtc->dspaddr_offset =
Ville Syrjälä29490562016-01-20 18:02:50 +02003086 intel_compute_tile_offset(&x, &y, plane_state, 0);
Daniel Vettere506a0c2012-07-05 12:17:29 +02003087
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003088 if (rotation == DRM_ROTATE_180) {
Sonika Jindal48404c12014-08-22 14:06:04 +05303089 dspcntr |= DISPPLANE_ROTATE_180;
3090
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003091 x += (crtc_state->pipe_src_w - 1);
3092 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05303093 }
3094
Ville Syrjälä29490562016-01-20 18:02:50 +02003095 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003096
3097 if (INTEL_INFO(dev)->gen < 4)
3098 intel_crtc->dspaddr_offset = linear_offset;
3099
Paulo Zanoni2db33662015-09-14 15:20:03 -03003100 intel_crtc->adjusted_x = x;
3101 intel_crtc->adjusted_y = y;
3102
Sonika Jindal48404c12014-08-22 14:06:04 +05303103 I915_WRITE(reg, dspcntr);
3104
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003105 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003106 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01003107 I915_WRITE(DSPSURF(plane),
Ville Syrjälä6687c902015-09-15 13:16:41 +03003108 intel_fb_gtt_offset(fb, rotation) +
3109 intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01003110 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02003111 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01003112 } else
Chris Wilson058d88c2016-08-15 10:49:06 +01003113 I915_WRITE(DSPADDR(plane), i915_gem_object_ggtt_offset(obj, NULL) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01003114 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003115}
3116
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003117static void i9xx_disable_primary_plane(struct drm_plane *primary,
3118 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07003119{
3120 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003121 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003122 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003123 int plane = intel_crtc->plane;
3124
3125 I915_WRITE(DSPCNTR(plane), 0);
3126 if (INTEL_INFO(dev_priv)->gen >= 4)
3127 I915_WRITE(DSPSURF(plane), 0);
3128 else
3129 I915_WRITE(DSPADDR(plane), 0);
3130 POSTING_READ(DSPCNTR(plane));
3131}
3132
3133static void ironlake_update_primary_plane(struct drm_plane *primary,
3134 const struct intel_crtc_state *crtc_state,
3135 const struct intel_plane_state *plane_state)
3136{
3137 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003138 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003139 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3140 struct drm_framebuffer *fb = plane_state->base.fb;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003141 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02003142 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003143 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003144 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02003145 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003146 int x = plane_state->base.src.x1 >> 16;
3147 int y = plane_state->base.src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03003148
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003149 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03003150 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003151
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003152 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003153 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
3154
Ville Syrjälä57779d02012-10-31 17:50:14 +02003155 switch (fb->pixel_format) {
3156 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07003157 dspcntr |= DISPPLANE_8BPP;
3158 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003159 case DRM_FORMAT_RGB565:
3160 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003161 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003162 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003163 dspcntr |= DISPPLANE_BGRX888;
3164 break;
3165 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003166 dspcntr |= DISPPLANE_RGBX888;
3167 break;
3168 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003169 dspcntr |= DISPPLANE_BGRX101010;
3170 break;
3171 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003172 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003173 break;
3174 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01003175 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07003176 }
3177
Ville Syrjälä72618eb2016-02-04 20:38:20 +02003178 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
Jesse Barnes17638cd2011-06-24 12:19:23 -07003179 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003180
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003181 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03003182 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003183
Ville Syrjälä29490562016-01-20 18:02:50 +02003184 intel_add_fb_offsets(&x, &y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003185
Daniel Vetterc2c75132012-07-05 12:17:30 +02003186 intel_crtc->dspaddr_offset =
Ville Syrjälä29490562016-01-20 18:02:50 +02003187 intel_compute_tile_offset(&x, &y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003188
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003189 if (rotation == DRM_ROTATE_180) {
Sonika Jindal48404c12014-08-22 14:06:04 +05303190 dspcntr |= DISPPLANE_ROTATE_180;
3191
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003192 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003193 x += (crtc_state->pipe_src_w - 1);
3194 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05303195 }
3196 }
3197
Ville Syrjälä29490562016-01-20 18:02:50 +02003198 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003199
Paulo Zanoni2db33662015-09-14 15:20:03 -03003200 intel_crtc->adjusted_x = x;
3201 intel_crtc->adjusted_y = y;
3202
Sonika Jindal48404c12014-08-22 14:06:04 +05303203 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003204
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003205 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01003206 I915_WRITE(DSPSURF(plane),
Ville Syrjälä6687c902015-09-15 13:16:41 +03003207 intel_fb_gtt_offset(fb, rotation) +
3208 intel_crtc->dspaddr_offset);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003209 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00003210 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
3211 } else {
3212 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
3213 I915_WRITE(DSPLINOFF(plane), linear_offset);
3214 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07003215 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003216}
3217
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003218u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
3219 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00003220{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003221 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
3222 return 64;
3223 } else {
3224 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00003225
Ville Syrjälä27ba3912016-02-15 22:54:40 +02003226 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00003227 }
3228}
3229
Ville Syrjälä6687c902015-09-15 13:16:41 +03003230u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
3231 unsigned int rotation)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003232{
Ville Syrjälä6687c902015-09-15 13:16:41 +03003233 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Daniel Vetterce7f1722015-10-14 16:51:06 +02003234 struct i915_ggtt_view view;
Chris Wilson058d88c2016-08-15 10:49:06 +01003235 struct i915_vma *vma;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003236
Ville Syrjälä6687c902015-09-15 13:16:41 +03003237 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003238
Chris Wilson058d88c2016-08-15 10:49:06 +01003239 vma = i915_gem_object_to_ggtt(obj, &view);
3240 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
3241 view.type))
3242 return -1;
3243
Chris Wilsonbde13eb2016-08-15 10:49:07 +01003244 return i915_ggtt_offset(vma);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003245}
3246
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003247static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3248{
3249 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003250 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003251
3252 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3253 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3254 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003255}
3256
Chandra Kondurua1b22782015-04-07 15:28:45 -07003257/*
3258 * This function detaches (aka. unbinds) unused scalers in hardware
3259 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02003260static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07003261{
Chandra Kondurua1b22782015-04-07 15:28:45 -07003262 struct intel_crtc_scaler_state *scaler_state;
3263 int i;
3264
Chandra Kondurua1b22782015-04-07 15:28:45 -07003265 scaler_state = &intel_crtc->config->scaler_state;
3266
3267 /* loop through and disable scalers that aren't in use */
3268 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003269 if (!scaler_state->scalers[i].in_use)
3270 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07003271 }
3272}
3273
Ville Syrjäläd2196772016-01-28 18:33:11 +02003274u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3275 unsigned int rotation)
3276{
3277 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
3278 u32 stride = intel_fb_pitch(fb, plane, rotation);
3279
3280 /*
3281 * The stride is either expressed as a multiple of 64 bytes chunks for
3282 * linear buffers or in number of tiles for tiled buffers.
3283 */
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003284 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjäläd2196772016-01-28 18:33:11 +02003285 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
3286
3287 stride /= intel_tile_height(dev_priv, fb->modifier[0], cpp);
3288 } else {
3289 stride /= intel_fb_stride_alignment(dev_priv, fb->modifier[0],
3290 fb->pixel_format);
3291 }
3292
3293 return stride;
3294}
3295
Chandra Konduru6156a452015-04-27 13:48:39 -07003296u32 skl_plane_ctl_format(uint32_t pixel_format)
3297{
Chandra Konduru6156a452015-04-27 13:48:39 -07003298 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01003299 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003300 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07003301 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003302 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07003303 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003304 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07003305 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003306 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07003307 /*
3308 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3309 * to be already pre-multiplied. We need to add a knob (or a different
3310 * DRM_FORMAT) for user-space to configure that.
3311 */
3312 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003313 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07003314 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003315 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003316 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07003317 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003318 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003319 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003320 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003321 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003322 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003323 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07003324 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003325 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07003326 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003327 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003328 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003329 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003330 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01003331 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07003332 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003333
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003334 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003335}
3336
3337u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3338{
Chandra Konduru6156a452015-04-27 13:48:39 -07003339 switch (fb_modifier) {
3340 case DRM_FORMAT_MOD_NONE:
3341 break;
3342 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003343 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003344 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003345 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003346 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003347 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003348 default:
3349 MISSING_CASE(fb_modifier);
3350 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003351
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003352 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003353}
3354
3355u32 skl_plane_ctl_rotation(unsigned int rotation)
3356{
Chandra Konduru6156a452015-04-27 13:48:39 -07003357 switch (rotation) {
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003358 case DRM_ROTATE_0:
Chandra Konduru6156a452015-04-27 13:48:39 -07003359 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303360 /*
3361 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3362 * while i915 HW rotation is clockwise, thats why this swapping.
3363 */
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003364 case DRM_ROTATE_90:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303365 return PLANE_CTL_ROTATE_270;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003366 case DRM_ROTATE_180:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003367 return PLANE_CTL_ROTATE_180;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003368 case DRM_ROTATE_270:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303369 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003370 default:
3371 MISSING_CASE(rotation);
3372 }
3373
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003374 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003375}
3376
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003377static void skylake_update_primary_plane(struct drm_plane *plane,
3378 const struct intel_crtc_state *crtc_state,
3379 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003380{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003381 struct drm_device *dev = plane->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003382 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003383 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3384 struct drm_framebuffer *fb = plane_state->base.fb;
Lyude62e0fb82016-08-22 12:50:08 -04003385 const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003386 const struct skl_plane_wm *p_wm =
3387 &crtc_state->wm.skl.optimal.planes[0];
Damien Lespiau70d21f02013-07-03 21:06:04 +01003388 int pipe = intel_crtc->pipe;
Ville Syrjäläd2196772016-01-28 18:33:11 +02003389 u32 plane_ctl;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003390 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläd2196772016-01-28 18:33:11 +02003391 u32 stride = skl_plane_stride(fb, 0, rotation);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003392 u32 surf_addr = plane_state->main.offset;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003393 int scaler_id = plane_state->scaler_id;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003394 int src_x = plane_state->main.x;
3395 int src_y = plane_state->main.y;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003396 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3397 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3398 int dst_x = plane_state->base.dst.x1;
3399 int dst_y = plane_state->base.dst.y1;
3400 int dst_w = drm_rect_width(&plane_state->base.dst);
3401 int dst_h = drm_rect_height(&plane_state->base.dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003402
3403 plane_ctl = PLANE_CTL_ENABLE |
3404 PLANE_CTL_PIPE_GAMMA_ENABLE |
3405 PLANE_CTL_PIPE_CSC_ENABLE;
3406
Chandra Konduru6156a452015-04-27 13:48:39 -07003407 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3408 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003409 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003410 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003411
Ville Syrjälä6687c902015-09-15 13:16:41 +03003412 /* Sizes are 0 based */
3413 src_w--;
3414 src_h--;
3415 dst_w--;
3416 dst_h--;
3417
Paulo Zanoni4c0b8a82016-08-19 19:03:23 -03003418 intel_crtc->dspaddr_offset = surf_addr;
3419
Ville Syrjälä6687c902015-09-15 13:16:41 +03003420 intel_crtc->adjusted_x = src_x;
3421 intel_crtc->adjusted_y = src_y;
Paulo Zanoni2db33662015-09-14 15:20:03 -03003422
Lyude62e0fb82016-08-22 12:50:08 -04003423 if (wm->dirty_pipes & drm_crtc_mask(&intel_crtc->base))
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003424 skl_write_plane_wm(intel_crtc, p_wm, &wm->ddb, 0);
Lyude62e0fb82016-08-22 12:50:08 -04003425
Damien Lespiau70d21f02013-07-03 21:06:04 +01003426 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003427 I915_WRITE(PLANE_OFFSET(pipe, 0), (src_y << 16) | src_x);
Ville Syrjäläef78ec92015-10-13 22:48:39 +03003428 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003429 I915_WRITE(PLANE_SIZE(pipe, 0), (src_h << 16) | src_w);
Chandra Konduru6156a452015-04-27 13:48:39 -07003430
3431 if (scaler_id >= 0) {
3432 uint32_t ps_ctrl = 0;
3433
3434 WARN_ON(!dst_w || !dst_h);
3435 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3436 crtc_state->scaler_state.scalers[scaler_id].mode;
3437 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3438 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3439 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3440 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3441 I915_WRITE(PLANE_POS(pipe, 0), 0);
3442 } else {
3443 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3444 }
3445
Ville Syrjälä6687c902015-09-15 13:16:41 +03003446 I915_WRITE(PLANE_SURF(pipe, 0),
3447 intel_fb_gtt_offset(fb, rotation) + surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003448
3449 POSTING_READ(PLANE_SURF(pipe, 0));
3450}
3451
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003452static void skylake_disable_primary_plane(struct drm_plane *primary,
3453 struct drm_crtc *crtc)
3454{
3455 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003456 struct drm_i915_private *dev_priv = to_i915(dev);
Lyude62e0fb82016-08-22 12:50:08 -04003457 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003458 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3459 const struct skl_plane_wm *p_wm = &cstate->wm.skl.optimal.planes[0];
Lyude62e0fb82016-08-22 12:50:08 -04003460 int pipe = intel_crtc->pipe;
3461
Lyudeccebc232016-08-29 12:31:27 -04003462 /*
3463 * We only populate skl_results on watermark updates, and if the
3464 * plane's visiblity isn't actually changing neither is its watermarks.
3465 */
3466 if (!crtc->primary->state->visible)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003467 skl_write_plane_wm(intel_crtc, p_wm,
3468 &dev_priv->wm.skl_results.ddb, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003469
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003470 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3471 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3472 POSTING_READ(PLANE_SURF(pipe, 0));
3473}
3474
Jesse Barnes17638cd2011-06-24 12:19:23 -07003475/* Assume fb object is pinned & idle & fenced and just update base pointers */
3476static int
3477intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3478 int x, int y, enum mode_set_atomic state)
3479{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003480 /* Support for kgdboc is disabled, this needs a major rework. */
3481 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003482
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003483 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003484}
3485
Daniel Vetter5a21b662016-05-24 17:13:53 +02003486static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3487{
3488 struct intel_crtc *crtc;
3489
Chris Wilson91c8a322016-07-05 10:40:23 +01003490 for_each_intel_crtc(&dev_priv->drm, crtc)
Daniel Vetter5a21b662016-05-24 17:13:53 +02003491 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3492}
3493
Ville Syrjälä75147472014-11-24 18:28:11 +02003494static void intel_update_primary_planes(struct drm_device *dev)
3495{
Ville Syrjälä75147472014-11-24 18:28:11 +02003496 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003497
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003498 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003499 struct intel_plane *plane = to_intel_plane(crtc->primary);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003500 struct intel_plane_state *plane_state =
3501 to_intel_plane_state(plane->base.state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003502
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003503 if (plane_state->base.visible)
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003504 plane->update_plane(&plane->base,
3505 to_intel_crtc_state(crtc->state),
3506 plane_state);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003507 }
3508}
3509
Maarten Lankhorst73974892016-08-05 23:28:27 +03003510static int
3511__intel_display_resume(struct drm_device *dev,
3512 struct drm_atomic_state *state)
3513{
3514 struct drm_crtc_state *crtc_state;
3515 struct drm_crtc *crtc;
3516 int i, ret;
3517
3518 intel_modeset_setup_hw_state(dev);
3519 i915_redisable_vga(dev);
3520
3521 if (!state)
3522 return 0;
3523
3524 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3525 /*
3526 * Force recalculation even if we restore
3527 * current state. With fast modeset this may not result
3528 * in a modeset when the state is compatible.
3529 */
3530 crtc_state->mode_changed = true;
3531 }
3532
3533 /* ignore any reset values/BIOS leftovers in the WM registers */
3534 to_intel_atomic_state(state)->skip_intermediate_wm = true;
3535
3536 ret = drm_atomic_commit(state);
3537
3538 WARN_ON(ret == -EDEADLK);
3539 return ret;
3540}
3541
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003542static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3543{
Ville Syrjäläae981042016-08-05 23:28:30 +03003544 return intel_has_gpu_reset(dev_priv) &&
3545 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003546}
3547
Chris Wilsonc0336662016-05-06 15:40:21 +01003548void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003549{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003550 struct drm_device *dev = &dev_priv->drm;
3551 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3552 struct drm_atomic_state *state;
3553 int ret;
3554
Maarten Lankhorst73974892016-08-05 23:28:27 +03003555 /*
3556 * Need mode_config.mutex so that we don't
3557 * trample ongoing ->detect() and whatnot.
3558 */
3559 mutex_lock(&dev->mode_config.mutex);
3560 drm_modeset_acquire_init(ctx, 0);
3561 while (1) {
3562 ret = drm_modeset_lock_all_ctx(dev, ctx);
3563 if (ret != -EDEADLK)
3564 break;
3565
3566 drm_modeset_backoff(ctx);
3567 }
3568
3569 /* reset doesn't touch the display, but flips might get nuked anyway, */
Maarten Lankhorst522a63d2016-08-05 23:28:28 +03003570 if (!i915.force_reset_modeset_test &&
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003571 !gpu_reset_clobbers_display(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003572 return;
3573
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003574 /*
3575 * Disabling the crtcs gracefully seems nicer. Also the
3576 * g33 docs say we should at least disable all the planes.
3577 */
Maarten Lankhorst73974892016-08-05 23:28:27 +03003578 state = drm_atomic_helper_duplicate_state(dev, ctx);
3579 if (IS_ERR(state)) {
3580 ret = PTR_ERR(state);
3581 state = NULL;
3582 DRM_ERROR("Duplicating state failed with %i\n", ret);
3583 goto err;
3584 }
3585
3586 ret = drm_atomic_helper_disable_all(dev, ctx);
3587 if (ret) {
3588 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
3589 goto err;
3590 }
3591
3592 dev_priv->modeset_restore_state = state;
3593 state->acquire_ctx = ctx;
3594 return;
3595
3596err:
Chris Wilson08536952016-10-14 13:18:18 +01003597 drm_atomic_state_put(state);
Ville Syrjälä75147472014-11-24 18:28:11 +02003598}
3599
Chris Wilsonc0336662016-05-06 15:40:21 +01003600void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003601{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003602 struct drm_device *dev = &dev_priv->drm;
3603 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3604 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3605 int ret;
3606
Daniel Vetter5a21b662016-05-24 17:13:53 +02003607 /*
3608 * Flips in the rings will be nuked by the reset,
3609 * so complete all pending flips so that user space
3610 * will get its events and not get stuck.
3611 */
3612 intel_complete_page_flips(dev_priv);
3613
Maarten Lankhorst73974892016-08-05 23:28:27 +03003614 dev_priv->modeset_restore_state = NULL;
3615
Ville Syrjälä75147472014-11-24 18:28:11 +02003616 /* reset doesn't touch the display */
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003617 if (!gpu_reset_clobbers_display(dev_priv)) {
Maarten Lankhorst522a63d2016-08-05 23:28:28 +03003618 if (!state) {
3619 /*
3620 * Flips in the rings have been nuked by the reset,
3621 * so update the base address of all primary
3622 * planes to the the last fb to make sure we're
3623 * showing the correct fb after a reset.
3624 *
3625 * FIXME: Atomic will make this obsolete since we won't schedule
3626 * CS-based flips (which might get lost in gpu resets) any more.
3627 */
3628 intel_update_primary_planes(dev);
3629 } else {
3630 ret = __intel_display_resume(dev, state);
3631 if (ret)
3632 DRM_ERROR("Restoring old state failed with %i\n", ret);
3633 }
Maarten Lankhorst73974892016-08-05 23:28:27 +03003634 } else {
3635 /*
3636 * The display has been reset as well,
3637 * so need a full re-initialization.
3638 */
3639 intel_runtime_pm_disable_interrupts(dev_priv);
3640 intel_runtime_pm_enable_interrupts(dev_priv);
3641
Imre Deak51f59202016-09-14 13:04:13 +03003642 intel_pps_unlock_regs_wa(dev_priv);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003643 intel_modeset_init_hw(dev);
3644
3645 spin_lock_irq(&dev_priv->irq_lock);
3646 if (dev_priv->display.hpd_irq_setup)
3647 dev_priv->display.hpd_irq_setup(dev_priv);
3648 spin_unlock_irq(&dev_priv->irq_lock);
3649
3650 ret = __intel_display_resume(dev, state);
3651 if (ret)
3652 DRM_ERROR("Restoring old state failed with %i\n", ret);
3653
3654 intel_hpd_init(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003655 }
3656
Chris Wilson08536952016-10-14 13:18:18 +01003657 if (state)
3658 drm_atomic_state_put(state);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003659 drm_modeset_drop_locks(ctx);
3660 drm_modeset_acquire_fini(ctx);
3661 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä75147472014-11-24 18:28:11 +02003662}
3663
Chris Wilson8af29b02016-09-09 14:11:47 +01003664static bool abort_flip_on_reset(struct intel_crtc *crtc)
3665{
3666 struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
3667
3668 if (i915_reset_in_progress(error))
3669 return true;
3670
3671 if (crtc->reset_count != i915_reset_count(error))
3672 return true;
3673
3674 return false;
3675}
3676
Chris Wilson7d5e3792014-03-04 13:15:08 +00003677static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3678{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003679 struct drm_device *dev = crtc->dev;
3680 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003681 bool pending;
3682
Chris Wilson8af29b02016-09-09 14:11:47 +01003683 if (abort_flip_on_reset(intel_crtc))
Daniel Vetter5a21b662016-05-24 17:13:53 +02003684 return false;
3685
3686 spin_lock_irq(&dev->event_lock);
3687 pending = to_intel_crtc(crtc)->flip_work != NULL;
3688 spin_unlock_irq(&dev->event_lock);
3689
3690 return pending;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003691}
3692
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003693static void intel_update_pipe_config(struct intel_crtc *crtc,
3694 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003695{
3696 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003697 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003698 struct intel_crtc_state *pipe_config =
3699 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003700
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003701 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3702 crtc->base.mode = crtc->base.state->mode;
3703
3704 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3705 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3706 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003707
3708 /*
3709 * Update pipe size and adjust fitter if needed: the reason for this is
3710 * that in compute_mode_changes we check the native mode (not the pfit
3711 * mode) to see if we can flip rather than do a full mode set. In the
3712 * fastboot case, we'll flip, but if we don't update the pipesrc and
3713 * pfit state, we'll end up with a big fb scanned out into the wrong
3714 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003715 */
3716
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003717 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003718 ((pipe_config->pipe_src_w - 1) << 16) |
3719 (pipe_config->pipe_src_h - 1));
3720
3721 /* on skylake this is done by detaching scalers */
3722 if (INTEL_INFO(dev)->gen >= 9) {
3723 skl_detach_scalers(crtc);
3724
3725 if (pipe_config->pch_pfit.enabled)
3726 skylake_pfit_enable(crtc);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003727 } else if (HAS_PCH_SPLIT(dev_priv)) {
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003728 if (pipe_config->pch_pfit.enabled)
3729 ironlake_pfit_enable(crtc);
3730 else if (old_crtc_state->pch_pfit.enabled)
3731 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003732 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003733}
3734
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003735static void intel_fdi_normal_train(struct drm_crtc *crtc)
3736{
3737 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003738 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3740 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003741 i915_reg_t reg;
3742 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003743
3744 /* enable normal train */
3745 reg = FDI_TX_CTL(pipe);
3746 temp = I915_READ(reg);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003747 if (IS_IVYBRIDGE(dev_priv)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003748 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3749 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003750 } else {
3751 temp &= ~FDI_LINK_TRAIN_NONE;
3752 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003753 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003754 I915_WRITE(reg, temp);
3755
3756 reg = FDI_RX_CTL(pipe);
3757 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003758 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003759 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3760 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3761 } else {
3762 temp &= ~FDI_LINK_TRAIN_NONE;
3763 temp |= FDI_LINK_TRAIN_NONE;
3764 }
3765 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3766
3767 /* wait one idle pattern time */
3768 POSTING_READ(reg);
3769 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003770
3771 /* IVB wants error correction enabled */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003772 if (IS_IVYBRIDGE(dev_priv))
Jesse Barnes357555c2011-04-28 15:09:55 -07003773 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3774 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003775}
3776
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003777/* The FDI link training functions for ILK/Ibexpeak. */
3778static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3779{
3780 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003781 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003782 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3783 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003784 i915_reg_t reg;
3785 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003786
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003787 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003788 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003789
Adam Jacksone1a44742010-06-25 15:32:14 -04003790 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3791 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003792 reg = FDI_RX_IMR(pipe);
3793 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003794 temp &= ~FDI_RX_SYMBOL_LOCK;
3795 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003796 I915_WRITE(reg, temp);
3797 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003798 udelay(150);
3799
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003800 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003801 reg = FDI_TX_CTL(pipe);
3802 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003803 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003804 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003805 temp &= ~FDI_LINK_TRAIN_NONE;
3806 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003807 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003808
Chris Wilson5eddb702010-09-11 13:48:45 +01003809 reg = FDI_RX_CTL(pipe);
3810 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003811 temp &= ~FDI_LINK_TRAIN_NONE;
3812 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003813 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3814
3815 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003816 udelay(150);
3817
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003818 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003819 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3820 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3821 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003822
Chris Wilson5eddb702010-09-11 13:48:45 +01003823 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003824 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003825 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003826 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3827
3828 if ((temp & FDI_RX_BIT_LOCK)) {
3829 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003830 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003831 break;
3832 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003833 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003834 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003835 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003836
3837 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003838 reg = FDI_TX_CTL(pipe);
3839 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003840 temp &= ~FDI_LINK_TRAIN_NONE;
3841 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003842 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003843
Chris Wilson5eddb702010-09-11 13:48:45 +01003844 reg = FDI_RX_CTL(pipe);
3845 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003846 temp &= ~FDI_LINK_TRAIN_NONE;
3847 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003848 I915_WRITE(reg, temp);
3849
3850 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003851 udelay(150);
3852
Chris Wilson5eddb702010-09-11 13:48:45 +01003853 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003854 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003855 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003856 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3857
3858 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003859 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003860 DRM_DEBUG_KMS("FDI train 2 done.\n");
3861 break;
3862 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003863 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003864 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003865 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003866
3867 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003868
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003869}
3870
Akshay Joshi0206e352011-08-16 15:34:10 -04003871static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003872 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3873 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3874 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3875 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3876};
3877
3878/* The FDI link training functions for SNB/Cougarpoint. */
3879static void gen6_fdi_link_train(struct drm_crtc *crtc)
3880{
3881 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003882 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003883 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3884 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003885 i915_reg_t reg;
3886 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003887
Adam Jacksone1a44742010-06-25 15:32:14 -04003888 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3889 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003890 reg = FDI_RX_IMR(pipe);
3891 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003892 temp &= ~FDI_RX_SYMBOL_LOCK;
3893 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003894 I915_WRITE(reg, temp);
3895
3896 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003897 udelay(150);
3898
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003899 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003900 reg = FDI_TX_CTL(pipe);
3901 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003902 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003903 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003904 temp &= ~FDI_LINK_TRAIN_NONE;
3905 temp |= FDI_LINK_TRAIN_PATTERN_1;
3906 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3907 /* SNB-B */
3908 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003909 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003910
Daniel Vetterd74cf322012-10-26 10:58:13 +02003911 I915_WRITE(FDI_RX_MISC(pipe),
3912 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3913
Chris Wilson5eddb702010-09-11 13:48:45 +01003914 reg = FDI_RX_CTL(pipe);
3915 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003916 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003917 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3918 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3919 } else {
3920 temp &= ~FDI_LINK_TRAIN_NONE;
3921 temp |= FDI_LINK_TRAIN_PATTERN_1;
3922 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003923 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3924
3925 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003926 udelay(150);
3927
Akshay Joshi0206e352011-08-16 15:34:10 -04003928 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003929 reg = FDI_TX_CTL(pipe);
3930 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003931 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3932 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003933 I915_WRITE(reg, temp);
3934
3935 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003936 udelay(500);
3937
Sean Paulfa37d392012-03-02 12:53:39 -05003938 for (retry = 0; retry < 5; retry++) {
3939 reg = FDI_RX_IIR(pipe);
3940 temp = I915_READ(reg);
3941 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3942 if (temp & FDI_RX_BIT_LOCK) {
3943 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3944 DRM_DEBUG_KMS("FDI train 1 done.\n");
3945 break;
3946 }
3947 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003948 }
Sean Paulfa37d392012-03-02 12:53:39 -05003949 if (retry < 5)
3950 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003951 }
3952 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003953 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003954
3955 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003956 reg = FDI_TX_CTL(pipe);
3957 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003958 temp &= ~FDI_LINK_TRAIN_NONE;
3959 temp |= FDI_LINK_TRAIN_PATTERN_2;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003960 if (IS_GEN6(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003961 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3962 /* SNB-B */
3963 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3964 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003965 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003966
Chris Wilson5eddb702010-09-11 13:48:45 +01003967 reg = FDI_RX_CTL(pipe);
3968 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003969 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003970 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3971 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3972 } else {
3973 temp &= ~FDI_LINK_TRAIN_NONE;
3974 temp |= FDI_LINK_TRAIN_PATTERN_2;
3975 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003976 I915_WRITE(reg, temp);
3977
3978 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003979 udelay(150);
3980
Akshay Joshi0206e352011-08-16 15:34:10 -04003981 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003982 reg = FDI_TX_CTL(pipe);
3983 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003984 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3985 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003986 I915_WRITE(reg, temp);
3987
3988 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003989 udelay(500);
3990
Sean Paulfa37d392012-03-02 12:53:39 -05003991 for (retry = 0; retry < 5; retry++) {
3992 reg = FDI_RX_IIR(pipe);
3993 temp = I915_READ(reg);
3994 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3995 if (temp & FDI_RX_SYMBOL_LOCK) {
3996 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3997 DRM_DEBUG_KMS("FDI train 2 done.\n");
3998 break;
3999 }
4000 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004001 }
Sean Paulfa37d392012-03-02 12:53:39 -05004002 if (retry < 5)
4003 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004004 }
4005 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01004006 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004007
4008 DRM_DEBUG_KMS("FDI train done.\n");
4009}
4010
Jesse Barnes357555c2011-04-28 15:09:55 -07004011/* Manual link training for Ivy Bridge A0 parts */
4012static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
4013{
4014 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004015 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes357555c2011-04-28 15:09:55 -07004016 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4017 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004018 i915_reg_t reg;
4019 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07004020
4021 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4022 for train result */
4023 reg = FDI_RX_IMR(pipe);
4024 temp = I915_READ(reg);
4025 temp &= ~FDI_RX_SYMBOL_LOCK;
4026 temp &= ~FDI_RX_BIT_LOCK;
4027 I915_WRITE(reg, temp);
4028
4029 POSTING_READ(reg);
4030 udelay(150);
4031
Daniel Vetter01a415f2012-10-27 15:58:40 +02004032 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4033 I915_READ(FDI_RX_IIR(pipe)));
4034
Jesse Barnes139ccd32013-08-19 11:04:55 -07004035 /* Try each vswing and preemphasis setting twice before moving on */
4036 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4037 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07004038 reg = FDI_TX_CTL(pipe);
4039 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004040 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4041 temp &= ~FDI_TX_ENABLE;
4042 I915_WRITE(reg, temp);
4043
4044 reg = FDI_RX_CTL(pipe);
4045 temp = I915_READ(reg);
4046 temp &= ~FDI_LINK_TRAIN_AUTO;
4047 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4048 temp &= ~FDI_RX_ENABLE;
4049 I915_WRITE(reg, temp);
4050
4051 /* enable CPU FDI TX and PCH FDI RX */
4052 reg = FDI_TX_CTL(pipe);
4053 temp = I915_READ(reg);
4054 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004055 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004056 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07004057 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07004058 temp |= snb_b_fdi_train_param[j/2];
4059 temp |= FDI_COMPOSITE_SYNC;
4060 I915_WRITE(reg, temp | FDI_TX_ENABLE);
4061
4062 I915_WRITE(FDI_RX_MISC(pipe),
4063 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
4064
4065 reg = FDI_RX_CTL(pipe);
4066 temp = I915_READ(reg);
4067 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4068 temp |= FDI_COMPOSITE_SYNC;
4069 I915_WRITE(reg, temp | FDI_RX_ENABLE);
4070
4071 POSTING_READ(reg);
4072 udelay(1); /* should be 0.5us */
4073
4074 for (i = 0; i < 4; i++) {
4075 reg = FDI_RX_IIR(pipe);
4076 temp = I915_READ(reg);
4077 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4078
4079 if (temp & FDI_RX_BIT_LOCK ||
4080 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4081 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4082 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4083 i);
4084 break;
4085 }
4086 udelay(1); /* should be 0.5us */
4087 }
4088 if (i == 4) {
4089 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4090 continue;
4091 }
4092
4093 /* Train 2 */
4094 reg = FDI_TX_CTL(pipe);
4095 temp = I915_READ(reg);
4096 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4097 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4098 I915_WRITE(reg, temp);
4099
4100 reg = FDI_RX_CTL(pipe);
4101 temp = I915_READ(reg);
4102 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4103 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07004104 I915_WRITE(reg, temp);
4105
4106 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004107 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004108
Jesse Barnes139ccd32013-08-19 11:04:55 -07004109 for (i = 0; i < 4; i++) {
4110 reg = FDI_RX_IIR(pipe);
4111 temp = I915_READ(reg);
4112 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07004113
Jesse Barnes139ccd32013-08-19 11:04:55 -07004114 if (temp & FDI_RX_SYMBOL_LOCK ||
4115 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4116 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4117 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4118 i);
4119 goto train_done;
4120 }
4121 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004122 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07004123 if (i == 4)
4124 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07004125 }
Jesse Barnes357555c2011-04-28 15:09:55 -07004126
Jesse Barnes139ccd32013-08-19 11:04:55 -07004127train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07004128 DRM_DEBUG_KMS("FDI train done.\n");
4129}
4130
Daniel Vetter88cefb62012-08-12 19:27:14 +02004131static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07004132{
Daniel Vetter88cefb62012-08-12 19:27:14 +02004133 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004134 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004135 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004136 i915_reg_t reg;
4137 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07004138
Jesse Barnes0e23b992010-09-10 11:10:00 -07004139 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01004140 reg = FDI_RX_CTL(pipe);
4141 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02004142 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004143 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004144 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01004145 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4146
4147 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004148 udelay(200);
4149
4150 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01004151 temp = I915_READ(reg);
4152 I915_WRITE(reg, temp | FDI_PCDCLK);
4153
4154 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004155 udelay(200);
4156
Paulo Zanoni20749732012-11-23 15:30:38 -02004157 /* Enable CPU FDI TX PLL, always on for Ironlake */
4158 reg = FDI_TX_CTL(pipe);
4159 temp = I915_READ(reg);
4160 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4161 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01004162
Paulo Zanoni20749732012-11-23 15:30:38 -02004163 POSTING_READ(reg);
4164 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004165 }
4166}
4167
Daniel Vetter88cefb62012-08-12 19:27:14 +02004168static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4169{
4170 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004171 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter88cefb62012-08-12 19:27:14 +02004172 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004173 i915_reg_t reg;
4174 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02004175
4176 /* Switch from PCDclk to Rawclk */
4177 reg = FDI_RX_CTL(pipe);
4178 temp = I915_READ(reg);
4179 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4180
4181 /* Disable CPU FDI TX PLL */
4182 reg = FDI_TX_CTL(pipe);
4183 temp = I915_READ(reg);
4184 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4185
4186 POSTING_READ(reg);
4187 udelay(100);
4188
4189 reg = FDI_RX_CTL(pipe);
4190 temp = I915_READ(reg);
4191 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4192
4193 /* Wait for the clocks to turn off. */
4194 POSTING_READ(reg);
4195 udelay(100);
4196}
4197
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004198static void ironlake_fdi_disable(struct drm_crtc *crtc)
4199{
4200 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004201 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004202 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4203 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004204 i915_reg_t reg;
4205 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004206
4207 /* disable CPU FDI tx and PCH FDI rx */
4208 reg = FDI_TX_CTL(pipe);
4209 temp = I915_READ(reg);
4210 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4211 POSTING_READ(reg);
4212
4213 reg = FDI_RX_CTL(pipe);
4214 temp = I915_READ(reg);
4215 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004216 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004217 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4218
4219 POSTING_READ(reg);
4220 udelay(100);
4221
4222 /* Ironlake workaround, disable clock pointer after downing FDI */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004223 if (HAS_PCH_IBX(dev_priv))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08004224 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004225
4226 /* still set train pattern 1 */
4227 reg = FDI_TX_CTL(pipe);
4228 temp = I915_READ(reg);
4229 temp &= ~FDI_LINK_TRAIN_NONE;
4230 temp |= FDI_LINK_TRAIN_PATTERN_1;
4231 I915_WRITE(reg, temp);
4232
4233 reg = FDI_RX_CTL(pipe);
4234 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004235 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004236 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4237 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4238 } else {
4239 temp &= ~FDI_LINK_TRAIN_NONE;
4240 temp |= FDI_LINK_TRAIN_PATTERN_1;
4241 }
4242 /* BPC in FDI rx is consistent with that in PIPECONF */
4243 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004244 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004245 I915_WRITE(reg, temp);
4246
4247 POSTING_READ(reg);
4248 udelay(100);
4249}
4250
Chris Wilson5dce5b932014-01-20 10:17:36 +00004251bool intel_has_pending_fb_unpin(struct drm_device *dev)
4252{
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02004253 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson5dce5b932014-01-20 10:17:36 +00004254 struct intel_crtc *crtc;
4255
4256 /* Note that we don't need to be called with mode_config.lock here
4257 * as our list of CRTC objects is static for the lifetime of the
4258 * device and so cannot disappear as we iterate. Similarly, we can
4259 * happily treat the predicates as racy, atomic checks as userspace
4260 * cannot claim and pin a new fb without at least acquring the
4261 * struct_mutex and so serialising with us.
4262 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004263 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00004264 if (atomic_read(&crtc->unpin_work_count) == 0)
4265 continue;
4266
Daniel Vetter5a21b662016-05-24 17:13:53 +02004267 if (crtc->flip_work)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02004268 intel_wait_for_vblank(dev_priv, crtc->pipe);
Chris Wilson5dce5b932014-01-20 10:17:36 +00004269
4270 return true;
4271 }
4272
4273 return false;
4274}
4275
Daniel Vetter5a21b662016-05-24 17:13:53 +02004276static void page_flip_completed(struct intel_crtc *intel_crtc)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004277{
4278 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004279 struct intel_flip_work *work = intel_crtc->flip_work;
4280
4281 intel_crtc->flip_work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004282
4283 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07004284 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004285
4286 drm_crtc_vblank_put(&intel_crtc->base);
4287
Daniel Vetter5a21b662016-05-24 17:13:53 +02004288 wake_up_all(&dev_priv->pending_flip_queue);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +02004289 queue_work(dev_priv->wq, &work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004290
4291 trace_i915_flip_complete(intel_crtc->plane,
4292 work->pending_flip_obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004293}
4294
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004295static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004296{
Chris Wilson0f911282012-04-17 10:05:38 +01004297 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004298 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004299 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004300
Daniel Vetter2c10d572012-12-20 21:24:07 +01004301 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004302
4303 ret = wait_event_interruptible_timeout(
4304 dev_priv->pending_flip_queue,
4305 !intel_crtc_has_pending_flip(crtc),
4306 60*HZ);
4307
4308 if (ret < 0)
4309 return ret;
4310
Daniel Vetter5a21b662016-05-24 17:13:53 +02004311 if (ret == 0) {
4312 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4313 struct intel_flip_work *work;
4314
4315 spin_lock_irq(&dev->event_lock);
4316 work = intel_crtc->flip_work;
4317 if (work && !is_mmio_work(work)) {
4318 WARN_ONCE(1, "Removing stuck page flip\n");
4319 page_flip_completed(intel_crtc);
4320 }
4321 spin_unlock_irq(&dev->event_lock);
4322 }
Chris Wilson5bb61642012-09-27 21:25:58 +01004323
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004324 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004325}
4326
Maarten Lankhorstb7076542016-08-23 16:18:08 +02004327void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004328{
4329 u32 temp;
4330
4331 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4332
4333 mutex_lock(&dev_priv->sb_lock);
4334
4335 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4336 temp |= SBI_SSCCTL_DISABLE;
4337 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4338
4339 mutex_unlock(&dev_priv->sb_lock);
4340}
4341
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004342/* Program iCLKIP clock to the desired frequency */
4343static void lpt_program_iclkip(struct drm_crtc *crtc)
4344{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004345 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004346 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004347 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4348 u32 temp;
4349
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004350 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004351
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004352 /* The iCLK virtual clock root frequency is in MHz,
4353 * but the adjusted_mode->crtc_clock in in KHz. To get the
4354 * divisors, it is necessary to divide one by another, so we
4355 * convert the virtual clock precision to KHz here for higher
4356 * precision.
4357 */
4358 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004359 u32 iclk_virtual_root_freq = 172800 * 1000;
4360 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004361 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004362
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004363 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4364 clock << auxdiv);
4365 divsel = (desired_divisor / iclk_pi_range) - 2;
4366 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004367
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004368 /*
4369 * Near 20MHz is a corner case which is
4370 * out of range for the 7-bit divisor
4371 */
4372 if (divsel <= 0x7f)
4373 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004374 }
4375
4376 /* This should not happen with any sane values */
4377 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4378 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4379 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4380 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4381
4382 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 +03004383 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004384 auxdiv,
4385 divsel,
4386 phasedir,
4387 phaseinc);
4388
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004389 mutex_lock(&dev_priv->sb_lock);
4390
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004391 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004392 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004393 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4394 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4395 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4396 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4397 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4398 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004399 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004400
4401 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004402 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004403 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4404 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004405 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004406
4407 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004408 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004409 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004410 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004411
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004412 mutex_unlock(&dev_priv->sb_lock);
4413
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004414 /* Wait for initialization time */
4415 udelay(24);
4416
4417 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4418}
4419
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02004420int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4421{
4422 u32 divsel, phaseinc, auxdiv;
4423 u32 iclk_virtual_root_freq = 172800 * 1000;
4424 u32 iclk_pi_range = 64;
4425 u32 desired_divisor;
4426 u32 temp;
4427
4428 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4429 return 0;
4430
4431 mutex_lock(&dev_priv->sb_lock);
4432
4433 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4434 if (temp & SBI_SSCCTL_DISABLE) {
4435 mutex_unlock(&dev_priv->sb_lock);
4436 return 0;
4437 }
4438
4439 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4440 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4441 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4442 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4443 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4444
4445 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4446 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4447 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4448
4449 mutex_unlock(&dev_priv->sb_lock);
4450
4451 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4452
4453 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4454 desired_divisor << auxdiv);
4455}
4456
Daniel Vetter275f01b22013-05-03 11:49:47 +02004457static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4458 enum pipe pch_transcoder)
4459{
4460 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004461 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004462 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004463
4464 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4465 I915_READ(HTOTAL(cpu_transcoder)));
4466 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4467 I915_READ(HBLANK(cpu_transcoder)));
4468 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4469 I915_READ(HSYNC(cpu_transcoder)));
4470
4471 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4472 I915_READ(VTOTAL(cpu_transcoder)));
4473 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4474 I915_READ(VBLANK(cpu_transcoder)));
4475 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4476 I915_READ(VSYNC(cpu_transcoder)));
4477 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4478 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4479}
4480
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004481static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004482{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004483 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004484 uint32_t temp;
4485
4486 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004487 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004488 return;
4489
4490 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4491 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4492
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004493 temp &= ~FDI_BC_BIFURCATION_SELECT;
4494 if (enable)
4495 temp |= FDI_BC_BIFURCATION_SELECT;
4496
4497 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004498 I915_WRITE(SOUTH_CHICKEN1, temp);
4499 POSTING_READ(SOUTH_CHICKEN1);
4500}
4501
4502static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4503{
4504 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004505
4506 switch (intel_crtc->pipe) {
4507 case PIPE_A:
4508 break;
4509 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004510 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004511 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004512 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004513 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004514
4515 break;
4516 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004517 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004518
4519 break;
4520 default:
4521 BUG();
4522 }
4523}
4524
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004525/* Return which DP Port should be selected for Transcoder DP control */
4526static enum port
4527intel_trans_dp_port_sel(struct drm_crtc *crtc)
4528{
4529 struct drm_device *dev = crtc->dev;
4530 struct intel_encoder *encoder;
4531
4532 for_each_encoder_on_crtc(dev, crtc, encoder) {
Ville Syrjäläcca05022016-06-22 21:57:06 +03004533 if (encoder->type == INTEL_OUTPUT_DP ||
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004534 encoder->type == INTEL_OUTPUT_EDP)
4535 return enc_to_dig_port(&encoder->base)->port;
4536 }
4537
4538 return -1;
4539}
4540
Jesse Barnesf67a5592011-01-05 10:31:48 -08004541/*
4542 * Enable PCH resources required for PCH ports:
4543 * - PCH PLLs
4544 * - FDI training & RX/TX
4545 * - update transcoder timings
4546 * - DP transcoding bits
4547 * - transcoder
4548 */
4549static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004550{
4551 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004552 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004553 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4554 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004555 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004556
Daniel Vetterab9412b2013-05-03 11:49:46 +02004557 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004558
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004559 if (IS_IVYBRIDGE(dev_priv))
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004560 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4561
Daniel Vettercd986ab2012-10-26 10:58:12 +02004562 /* Write the TU size bits before fdi link training, so that error
4563 * detection works. */
4564 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4565 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4566
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004567 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004568 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004569
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004570 /* We need to program the right clock selection before writing the pixel
4571 * mutliplier into the DPLL. */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004572 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004573 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004574
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004575 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004576 temp |= TRANS_DPLL_ENABLE(pipe);
4577 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004578 if (intel_crtc->config->shared_dpll ==
4579 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004580 temp |= sel;
4581 else
4582 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004583 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004584 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004585
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004586 /* XXX: pch pll's can be enabled any time before we enable the PCH
4587 * transcoder, and we actually should do this to not upset any PCH
4588 * transcoder that already use the clock when we share it.
4589 *
4590 * Note that enable_shared_dpll tries to do the right thing, but
4591 * get_shared_dpll unconditionally resets the pll - we need that to have
4592 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004593 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004594
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004595 /* set transcoder timing, panel must allow it */
4596 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004597 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004598
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004599 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004600
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004601 /* For PCH DP, enable TRANS_DP_CTL */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004602 if (HAS_PCH_CPT(dev_priv) &&
4603 intel_crtc_has_dp_encoder(intel_crtc->config)) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004604 const struct drm_display_mode *adjusted_mode =
4605 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004606 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004607 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004608 temp = I915_READ(reg);
4609 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004610 TRANS_DP_SYNC_MASK |
4611 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004612 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004613 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004614
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004615 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004616 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004617 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004618 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004619
4620 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004621 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004622 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004623 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004624 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004625 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004626 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004627 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004628 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004629 break;
4630 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004631 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004632 }
4633
Chris Wilson5eddb702010-09-11 13:48:45 +01004634 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004635 }
4636
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004637 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004638}
4639
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004640static void lpt_pch_enable(struct drm_crtc *crtc)
4641{
4642 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004643 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004644 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004645 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004646
Daniel Vetterab9412b2013-05-03 11:49:46 +02004647 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004648
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004649 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004650
Paulo Zanoni0540e482012-10-31 18:12:40 -02004651 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004652 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004653
Paulo Zanoni937bb612012-10-31 18:12:47 -02004654 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004655}
4656
Daniel Vettera1520312013-05-03 11:49:50 +02004657static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004658{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004659 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004660 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004661 u32 temp;
4662
4663 temp = I915_READ(dslreg);
4664 udelay(500);
4665 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004666 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004667 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004668 }
4669}
4670
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004671static int
4672skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4673 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4674 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004675{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004676 struct intel_crtc_scaler_state *scaler_state =
4677 &crtc_state->scaler_state;
4678 struct intel_crtc *intel_crtc =
4679 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004680 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004681
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03004682 need_scaling = drm_rotation_90_or_270(rotation) ?
Chandra Konduru6156a452015-04-27 13:48:39 -07004683 (src_h != dst_w || src_w != dst_h):
4684 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004685
4686 /*
4687 * if plane is being disabled or scaler is no more required or force detach
4688 * - free scaler binded to this plane/crtc
4689 * - in order to do this, update crtc->scaler_usage
4690 *
4691 * Here scaler state in crtc_state is set free so that
4692 * scaler can be assigned to other user. Actual register
4693 * update to free the scaler is done in plane/panel-fit programming.
4694 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4695 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004696 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004697 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004698 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004699 scaler_state->scalers[*scaler_id].in_use = 0;
4700
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004701 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4702 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4703 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004704 scaler_state->scaler_users);
4705 *scaler_id = -1;
4706 }
4707 return 0;
4708 }
4709
4710 /* range checks */
4711 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4712 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4713
4714 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4715 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004716 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004717 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004718 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004719 return -EINVAL;
4720 }
4721
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004722 /* mark this plane as a scaler user in crtc_state */
4723 scaler_state->scaler_users |= (1 << scaler_user);
4724 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4725 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4726 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4727 scaler_state->scaler_users);
4728
4729 return 0;
4730}
4731
4732/**
4733 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4734 *
4735 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004736 *
4737 * Return
4738 * 0 - scaler_usage updated successfully
4739 * error - requested scaling cannot be supported or other error condition
4740 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004741int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004742{
4743 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004744 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004745
Ville Syrjälä78108b72016-05-27 20:59:19 +03004746 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4747 intel_crtc->base.base.id, intel_crtc->base.name,
4748 intel_crtc->pipe, SKL_CRTC_INDEX);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004749
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004750 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03004751 &state->scaler_state.scaler_id, DRM_ROTATE_0,
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004752 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004753 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004754}
4755
4756/**
4757 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4758 *
4759 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004760 * @plane_state: atomic plane state to update
4761 *
4762 * Return
4763 * 0 - scaler_usage updated successfully
4764 * error - requested scaling cannot be supported or other error condition
4765 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004766static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4767 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004768{
4769
4770 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004771 struct intel_plane *intel_plane =
4772 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004773 struct drm_framebuffer *fb = plane_state->base.fb;
4774 int ret;
4775
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004776 bool force_detach = !fb || !plane_state->base.visible;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004777
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004778 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4779 intel_plane->base.base.id, intel_plane->base.name,
4780 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004781
4782 ret = skl_update_scaler(crtc_state, force_detach,
4783 drm_plane_index(&intel_plane->base),
4784 &plane_state->scaler_id,
4785 plane_state->base.rotation,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004786 drm_rect_width(&plane_state->base.src) >> 16,
4787 drm_rect_height(&plane_state->base.src) >> 16,
4788 drm_rect_width(&plane_state->base.dst),
4789 drm_rect_height(&plane_state->base.dst));
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004790
4791 if (ret || plane_state->scaler_id < 0)
4792 return ret;
4793
Chandra Kondurua1b22782015-04-07 15:28:45 -07004794 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004795 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004796 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4797 intel_plane->base.base.id,
4798 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004799 return -EINVAL;
4800 }
4801
4802 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004803 switch (fb->pixel_format) {
4804 case DRM_FORMAT_RGB565:
4805 case DRM_FORMAT_XBGR8888:
4806 case DRM_FORMAT_XRGB8888:
4807 case DRM_FORMAT_ABGR8888:
4808 case DRM_FORMAT_ARGB8888:
4809 case DRM_FORMAT_XRGB2101010:
4810 case DRM_FORMAT_XBGR2101010:
4811 case DRM_FORMAT_YUYV:
4812 case DRM_FORMAT_YVYU:
4813 case DRM_FORMAT_UYVY:
4814 case DRM_FORMAT_VYUY:
4815 break;
4816 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004817 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4818 intel_plane->base.base.id, intel_plane->base.name,
4819 fb->base.id, fb->pixel_format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004820 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004821 }
4822
Chandra Kondurua1b22782015-04-07 15:28:45 -07004823 return 0;
4824}
4825
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004826static void skylake_scaler_disable(struct intel_crtc *crtc)
4827{
4828 int i;
4829
4830 for (i = 0; i < crtc->num_scalers; i++)
4831 skl_detach_scaler(crtc, i);
4832}
4833
4834static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004835{
4836 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004837 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004838 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004839 struct intel_crtc_scaler_state *scaler_state =
4840 &crtc->config->scaler_state;
4841
4842 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4843
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004844 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004845 int id;
4846
4847 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4848 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4849 return;
4850 }
4851
4852 id = scaler_state->scaler_id;
4853 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4854 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4855 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4856 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4857
4858 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004859 }
4860}
4861
Jesse Barnesb074cec2013-04-25 12:55:02 -07004862static void ironlake_pfit_enable(struct intel_crtc *crtc)
4863{
4864 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004865 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb074cec2013-04-25 12:55:02 -07004866 int pipe = crtc->pipe;
4867
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004868 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004869 /* Force use of hard-coded filter coefficients
4870 * as some pre-programmed values are broken,
4871 * e.g. x201.
4872 */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004873 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
Jesse Barnesb074cec2013-04-25 12:55:02 -07004874 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4875 PF_PIPE_SEL_IVB(pipe));
4876 else
4877 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004878 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4879 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004880 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004881}
4882
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004883void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004884{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004885 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004886 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004887
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004888 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004889 return;
4890
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004891 /*
4892 * We can only enable IPS after we enable a plane and wait for a vblank
4893 * This function is called from post_plane_update, which is run after
4894 * a vblank wait.
4895 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004896
Paulo Zanonid77e4532013-09-24 13:52:55 -03004897 assert_plane_enabled(dev_priv, crtc->plane);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004898 if (IS_BROADWELL(dev_priv)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004899 mutex_lock(&dev_priv->rps.hw_lock);
4900 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4901 mutex_unlock(&dev_priv->rps.hw_lock);
4902 /* Quoting Art Runyan: "its not safe to expect any particular
4903 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004904 * mailbox." Moreover, the mailbox may return a bogus state,
4905 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004906 */
4907 } else {
4908 I915_WRITE(IPS_CTL, IPS_ENABLE);
4909 /* The bit only becomes 1 in the next vblank, so this wait here
4910 * is essentially intel_wait_for_vblank. If we don't have this
4911 * and don't wait for vblanks until the end of crtc_enable, then
4912 * the HW state readout code will complain that the expected
4913 * IPS_CTL value is not the one we read. */
Chris Wilson2ec9ba32016-06-30 15:33:01 +01004914 if (intel_wait_for_register(dev_priv,
4915 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4916 50))
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004917 DRM_ERROR("Timed out waiting for IPS enable\n");
4918 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004919}
4920
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004921void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004922{
4923 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004924 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004925
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004926 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004927 return;
4928
4929 assert_plane_enabled(dev_priv, crtc->plane);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004930 if (IS_BROADWELL(dev_priv)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004931 mutex_lock(&dev_priv->rps.hw_lock);
4932 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4933 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004934 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
Chris Wilsonb85c1ec2016-06-30 15:33:02 +01004935 if (intel_wait_for_register(dev_priv,
4936 IPS_CTL, IPS_ENABLE, 0,
4937 42))
Ben Widawsky23d0b132014-04-10 14:32:41 -07004938 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004939 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004940 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004941 POSTING_READ(IPS_CTL);
4942 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004943
4944 /* We need to wait for a vblank before we can disable the plane. */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02004945 intel_wait_for_vblank(dev_priv, crtc->pipe);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004946}
4947
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004948static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004949{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004950 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004951 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004952 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004953
4954 mutex_lock(&dev->struct_mutex);
4955 dev_priv->mm.interruptible = false;
4956 (void) intel_overlay_switch_off(intel_crtc->overlay);
4957 dev_priv->mm.interruptible = true;
4958 mutex_unlock(&dev->struct_mutex);
4959 }
4960
4961 /* Let userspace switch the overlay on again. In most cases userspace
4962 * has to recompute where to put it anyway.
4963 */
4964}
4965
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004966/**
4967 * intel_post_enable_primary - Perform operations after enabling primary plane
4968 * @crtc: the CRTC whose primary plane was just enabled
4969 *
4970 * Performs potentially sleeping operations that must be done after the primary
4971 * plane is enabled, such as updating FBC and IPS. Note that this may be
4972 * called due to an explicit primary plane update, or due to an implicit
4973 * re-enable that is caused when a sprite plane is updated to no longer
4974 * completely hide the primary plane.
4975 */
4976static void
4977intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004978{
4979 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004980 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004981 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4982 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004983
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004984 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004985 * FIXME IPS should be fine as long as one plane is
4986 * enabled, but in practice it seems to have problems
4987 * when going from primary only to sprite only and vice
4988 * versa.
4989 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004990 hsw_enable_ips(intel_crtc);
4991
Daniel Vetterf99d7062014-06-19 16:01:59 +02004992 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004993 * Gen2 reports pipe underruns whenever all planes are disabled.
4994 * So don't enable underrun reporting before at least some planes
4995 * are enabled.
4996 * FIXME: Need to fix the logic to work when we turn off all planes
4997 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004998 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004999 if (IS_GEN2(dev_priv))
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005000 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5001
Ville Syrjäläaca7b682015-10-30 19:22:21 +02005002 /* Underruns don't always raise interrupts, so check manually. */
5003 intel_check_cpu_fifo_underruns(dev_priv);
5004 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005005}
5006
Ville Syrjälä2622a082016-03-09 19:07:26 +02005007/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005008static void
5009intel_pre_disable_primary(struct drm_crtc *crtc)
5010{
5011 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005012 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005013 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5014 int pipe = intel_crtc->pipe;
5015
5016 /*
5017 * Gen2 reports pipe underruns whenever all planes are disabled.
5018 * So diasble underrun reporting before all the planes get disabled.
5019 * FIXME: Need to fix the logic to work when we turn off all planes
5020 * but leave the pipe running.
5021 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005022 if (IS_GEN2(dev_priv))
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005023 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5024
5025 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02005026 * FIXME IPS should be fine as long as one plane is
5027 * enabled, but in practice it seems to have problems
5028 * when going from primary only to sprite only and vice
5029 * versa.
5030 */
5031 hsw_disable_ips(intel_crtc);
5032}
5033
5034/* FIXME get rid of this and use pre_plane_update */
5035static void
5036intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
5037{
5038 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005039 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +02005040 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5041 int pipe = intel_crtc->pipe;
5042
5043 intel_pre_disable_primary(crtc);
5044
5045 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005046 * Vblank time updates from the shadow to live plane control register
5047 * are blocked if the memory self-refresh mode is active at that
5048 * moment. So to make sure the plane gets truly disabled, disable
5049 * first the self-refresh mode. The self-refresh enable bit in turn
5050 * will be checked/applied by the HW only at the next frame start
5051 * event which is after the vblank start event, so we need to have a
5052 * wait-for-vblank between disabling the plane and the pipe.
5053 */
Tvrtko Ursulin49cff962016-10-13 11:02:54 +01005054 if (HAS_GMCH_DISPLAY(dev_priv)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005055 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03005056 dev_priv->wm.vlv.cxsr = false;
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005057 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03005058 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005059}
5060
Daniel Vetter5a21b662016-05-24 17:13:53 +02005061static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5062{
5063 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
5064 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5065 struct intel_crtc_state *pipe_config =
5066 to_intel_crtc_state(crtc->base.state);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005067 struct drm_plane *primary = crtc->base.primary;
5068 struct drm_plane_state *old_pri_state =
5069 drm_atomic_get_existing_plane_state(old_state, primary);
5070
Chris Wilson5748b6a2016-08-04 16:32:38 +01005071 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005072
5073 crtc->wm.cxsr_allowed = true;
5074
5075 if (pipe_config->update_wm_post && pipe_config->base.active)
Ville Syrjälä432081b2016-10-31 22:37:03 +02005076 intel_update_watermarks(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005077
5078 if (old_pri_state) {
5079 struct intel_plane_state *primary_state =
5080 to_intel_plane_state(primary->state);
5081 struct intel_plane_state *old_primary_state =
5082 to_intel_plane_state(old_pri_state);
5083
5084 intel_fbc_post_update(crtc);
5085
Ville Syrjälä936e71e2016-07-26 19:06:59 +03005086 if (primary_state->base.visible &&
Daniel Vetter5a21b662016-05-24 17:13:53 +02005087 (needs_modeset(&pipe_config->base) ||
Ville Syrjälä936e71e2016-07-26 19:06:59 +03005088 !old_primary_state->base.visible))
Daniel Vetter5a21b662016-05-24 17:13:53 +02005089 intel_post_enable_primary(&crtc->base);
5090 }
5091}
5092
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005093static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005094{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005095 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005096 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005097 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01005098 struct intel_crtc_state *pipe_config =
5099 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005100 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5101 struct drm_plane *primary = crtc->base.primary;
5102 struct drm_plane_state *old_pri_state =
5103 drm_atomic_get_existing_plane_state(old_state, primary);
5104 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005105
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005106 if (old_pri_state) {
5107 struct intel_plane_state *primary_state =
5108 to_intel_plane_state(primary->state);
5109 struct intel_plane_state *old_primary_state =
5110 to_intel_plane_state(old_pri_state);
5111
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +02005112 intel_fbc_pre_update(crtc, pipe_config, primary_state);
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01005113
Ville Syrjälä936e71e2016-07-26 19:06:59 +03005114 if (old_primary_state->base.visible &&
5115 (modeset || !primary_state->base.visible))
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005116 intel_pre_disable_primary(&crtc->base);
5117 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03005118
Tvrtko Ursulin49cff962016-10-13 11:02:54 +01005119 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03005120 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01005121
Ville Syrjälä2622a082016-03-09 19:07:26 +02005122 /*
5123 * Vblank time updates from the shadow to live plane control register
5124 * are blocked if the memory self-refresh mode is active at that
5125 * moment. So to make sure the plane gets truly disabled, disable
5126 * first the self-refresh mode. The self-refresh enable bit in turn
5127 * will be checked/applied by the HW only at the next frame start
5128 * event which is after the vblank start event, so we need to have a
5129 * wait-for-vblank between disabling the plane and the pipe.
5130 */
5131 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01005132 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02005133 dev_priv->wm.vlv.cxsr = false;
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005134 intel_wait_for_vblank(dev_priv, crtc->pipe);
Ville Syrjälä2622a082016-03-09 19:07:26 +02005135 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03005136 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01005137
Matt Ropered4a6a72016-02-23 17:20:13 -08005138 /*
5139 * IVB workaround: must disable low power watermarks for at least
5140 * one frame before enabling scaling. LP watermarks can be re-enabled
5141 * when scaling is disabled.
5142 *
5143 * WaCxSRDisabledForSpriteScaling:ivb
5144 */
5145 if (pipe_config->disable_lp_wm) {
5146 ilk_disable_lp_wm(dev);
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005147 intel_wait_for_vblank(dev_priv, crtc->pipe);
Matt Ropered4a6a72016-02-23 17:20:13 -08005148 }
5149
5150 /*
5151 * If we're doing a modeset, we're done. No need to do any pre-vblank
5152 * watermark programming here.
5153 */
5154 if (needs_modeset(&pipe_config->base))
5155 return;
5156
5157 /*
5158 * For platforms that support atomic watermarks, program the
5159 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5160 * will be the intermediate values that are safe for both pre- and
5161 * post- vblank; when vblank happens, the 'active' values will be set
5162 * to the final 'target' values and we'll do this again to get the
5163 * optimal watermarks. For gen9+ platforms, the values we program here
5164 * will be the final target values which will get automatically latched
5165 * at vblank time; no further programming will be necessary.
5166 *
5167 * If a platform hasn't been transitioned to atomic watermarks yet,
5168 * we'll continue to update watermarks the old way, if flags tell
5169 * us to.
5170 */
5171 if (dev_priv->display.initial_watermarks != NULL)
5172 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02005173 else if (pipe_config->update_wm_pre)
Ville Syrjälä432081b2016-10-31 22:37:03 +02005174 intel_update_watermarks(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005175}
5176
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005177static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005178{
5179 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005181 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005182 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005183
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03005184 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03005185
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005186 drm_for_each_plane_mask(p, dev, plane_mask)
5187 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03005188
Daniel Vetterf99d7062014-06-19 16:01:59 +02005189 /*
5190 * FIXME: Once we grow proper nuclear flip support out of this we need
5191 * to compute the mask of flip planes precisely. For the time being
5192 * consider this a flip to a NULL plane.
5193 */
Chris Wilson5748b6a2016-08-04 16:32:38 +01005194 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005195}
5196
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005197static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005198 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005199 struct drm_atomic_state *old_state)
5200{
5201 struct drm_connector_state *old_conn_state;
5202 struct drm_connector *conn;
5203 int i;
5204
5205 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5206 struct drm_connector_state *conn_state = conn->state;
5207 struct intel_encoder *encoder =
5208 to_intel_encoder(conn_state->best_encoder);
5209
5210 if (conn_state->crtc != crtc)
5211 continue;
5212
5213 if (encoder->pre_pll_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005214 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005215 }
5216}
5217
5218static void intel_encoders_pre_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005219 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005220 struct drm_atomic_state *old_state)
5221{
5222 struct drm_connector_state *old_conn_state;
5223 struct drm_connector *conn;
5224 int i;
5225
5226 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5227 struct drm_connector_state *conn_state = conn->state;
5228 struct intel_encoder *encoder =
5229 to_intel_encoder(conn_state->best_encoder);
5230
5231 if (conn_state->crtc != crtc)
5232 continue;
5233
5234 if (encoder->pre_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005235 encoder->pre_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005236 }
5237}
5238
5239static void intel_encoders_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005240 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005241 struct drm_atomic_state *old_state)
5242{
5243 struct drm_connector_state *old_conn_state;
5244 struct drm_connector *conn;
5245 int i;
5246
5247 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5248 struct drm_connector_state *conn_state = conn->state;
5249 struct intel_encoder *encoder =
5250 to_intel_encoder(conn_state->best_encoder);
5251
5252 if (conn_state->crtc != crtc)
5253 continue;
5254
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005255 encoder->enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005256 intel_opregion_notify_encoder(encoder, true);
5257 }
5258}
5259
5260static void intel_encoders_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005261 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005262 struct drm_atomic_state *old_state)
5263{
5264 struct drm_connector_state *old_conn_state;
5265 struct drm_connector *conn;
5266 int i;
5267
5268 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5269 struct intel_encoder *encoder =
5270 to_intel_encoder(old_conn_state->best_encoder);
5271
5272 if (old_conn_state->crtc != crtc)
5273 continue;
5274
5275 intel_opregion_notify_encoder(encoder, false);
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005276 encoder->disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005277 }
5278}
5279
5280static void intel_encoders_post_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005281 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005282 struct drm_atomic_state *old_state)
5283{
5284 struct drm_connector_state *old_conn_state;
5285 struct drm_connector *conn;
5286 int i;
5287
5288 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5289 struct intel_encoder *encoder =
5290 to_intel_encoder(old_conn_state->best_encoder);
5291
5292 if (old_conn_state->crtc != crtc)
5293 continue;
5294
5295 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005296 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005297 }
5298}
5299
5300static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005301 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005302 struct drm_atomic_state *old_state)
5303{
5304 struct drm_connector_state *old_conn_state;
5305 struct drm_connector *conn;
5306 int i;
5307
5308 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5309 struct intel_encoder *encoder =
5310 to_intel_encoder(old_conn_state->best_encoder);
5311
5312 if (old_conn_state->crtc != crtc)
5313 continue;
5314
5315 if (encoder->post_pll_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005316 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005317 }
5318}
5319
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005320static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5321 struct drm_atomic_state *old_state)
Jesse Barnesf67a5592011-01-05 10:31:48 -08005322{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005323 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnesf67a5592011-01-05 10:31:48 -08005324 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005325 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005326 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5327 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08005328
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005329 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08005330 return;
5331
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005332 /*
5333 * Sometimes spurious CPU pipe underruns happen during FDI
5334 * training, at least with VGA+HDMI cloning. Suppress them.
5335 *
5336 * On ILK we get an occasional spurious CPU pipe underruns
5337 * between eDP port A enable and vdd enable. Also PCH port
5338 * enable seems to result in the occasional CPU pipe underrun.
5339 *
5340 * Spurious PCH underruns also occur during PCH enabling.
5341 */
5342 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5343 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005344 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005345 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5346
5347 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02005348 intel_prepare_shared_dpll(intel_crtc);
5349
Ville Syrjälä37a56502016-06-22 21:57:04 +03005350 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305351 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005352
5353 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02005354 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005355
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005356 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02005357 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005358 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005359 }
5360
5361 ironlake_set_pipeconf(crtc);
5362
Jesse Barnesf67a5592011-01-05 10:31:48 -08005363 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005364
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005365 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005366
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005367 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02005368 /* Note: FDI PLL enabling _must_ be done before we enable the
5369 * cpu pipes, hence this is separate from all the other fdi/pch
5370 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02005371 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02005372 } else {
5373 assert_fdi_tx_disabled(dev_priv, pipe);
5374 assert_fdi_rx_disabled(dev_priv, pipe);
5375 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08005376
Jesse Barnesb074cec2013-04-25 12:55:02 -07005377 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005378
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005379 /*
5380 * On ILK+ LUT must be loaded before the pipe is running but with
5381 * clocks enabled
5382 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005383 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005384
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005385 if (dev_priv->display.initial_watermarks != NULL)
5386 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005387 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005388
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005389 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08005390 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005391
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005392 assert_vblank_disabled(crtc);
5393 drm_crtc_vblank_on(crtc);
5394
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005395 intel_encoders_enable(crtc, pipe_config, old_state);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02005396
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005397 if (HAS_PCH_CPT(dev_priv))
Daniel Vettera1520312013-05-03 11:49:50 +02005398 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005399
5400 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5401 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005402 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005403 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005404 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005405}
5406
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005407/* IPS only exists on ULT machines and is tied to pipe A. */
5408static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5409{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005410 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005411}
5412
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005413static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5414 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005415{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005416 struct drm_crtc *crtc = pipe_config->base.crtc;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005417 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005418 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005419 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005420 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02005421 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005422
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005423 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005424 return;
5425
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005426 if (intel_crtc->config->has_pch_encoder)
5427 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5428 false);
5429
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005430 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Imre Deak95a7a2a2016-06-13 16:44:35 +03005431
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02005432 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005433 intel_enable_shared_dpll(intel_crtc);
5434
Ville Syrjälä37a56502016-06-22 21:57:04 +03005435 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305436 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005437
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005438 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005439 intel_set_pipe_timings(intel_crtc);
5440
Jani Nikulabc58be62016-03-18 17:05:39 +02005441 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02005442
Jani Nikula4d1de972016-03-18 17:05:42 +02005443 if (cpu_transcoder != TRANSCODER_EDP &&
5444 !transcoder_is_dsi(cpu_transcoder)) {
5445 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005446 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005447 }
5448
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005449 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005450 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005451 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005452 }
5453
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005454 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005455 haswell_set_pipeconf(crtc);
5456
Jani Nikula391bf042016-03-18 17:05:40 +02005457 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02005458
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005459 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02005460
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005461 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005462
Daniel Vetter6b698512015-11-28 11:05:39 +01005463 if (intel_crtc->config->has_pch_encoder)
5464 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5465 else
5466 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5467
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005468 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005469
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005470 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03005471 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03005472
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005473 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305474 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005475
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005476 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005477 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005478 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005479 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005480
5481 /*
5482 * On ILK+ LUT must be loaded before the pipe is running but with
5483 * clocks enabled
5484 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005485 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005486
Paulo Zanoni1f544382012-10-24 11:32:00 -02005487 intel_ddi_set_pipe_settings(crtc);
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005488 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305489 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005490
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005491 if (dev_priv->display.initial_watermarks != NULL)
5492 dev_priv->display.initial_watermarks(pipe_config);
5493 else
Ville Syrjälä432081b2016-10-31 22:37:03 +02005494 intel_update_watermarks(intel_crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02005495
5496 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005497 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005498 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005499
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005500 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005501 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005502
Jani Nikulaa65347b2015-11-27 12:21:46 +02005503 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005504 intel_ddi_set_vc_payload_alloc(crtc, true);
5505
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005506 assert_vblank_disabled(crtc);
5507 drm_crtc_vblank_on(crtc);
5508
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005509 intel_encoders_enable(crtc, pipe_config, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005510
Daniel Vetter6b698512015-11-28 11:05:39 +01005511 if (intel_crtc->config->has_pch_encoder) {
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005512 intel_wait_for_vblank(dev_priv, pipe);
5513 intel_wait_for_vblank(dev_priv, pipe);
Daniel Vetter6b698512015-11-28 11:05:39 +01005514 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005515 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5516 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01005517 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005518
Paulo Zanonie4916942013-09-20 16:21:19 -03005519 /* If we change the relative order between pipe/planes enabling, we need
5520 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005521 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005522 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02005523 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5524 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005525 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005526}
5527
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005528static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005529{
5530 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005531 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005532 int pipe = crtc->pipe;
5533
5534 /* To avoid upsetting the power well on haswell only disable the pfit if
5535 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005536 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005537 I915_WRITE(PF_CTL(pipe), 0);
5538 I915_WRITE(PF_WIN_POS(pipe), 0);
5539 I915_WRITE(PF_WIN_SZ(pipe), 0);
5540 }
5541}
5542
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005543static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5544 struct drm_atomic_state *old_state)
Jesse Barnes6be4a602010-09-10 10:26:01 -07005545{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005546 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005547 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005548 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005549 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5550 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005551
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005552 /*
5553 * Sometimes spurious CPU pipe underruns happen when the
5554 * pipe is already disabled, but FDI RX/TX is still enabled.
5555 * Happens at least with VGA+HDMI cloning. Suppress them.
5556 */
5557 if (intel_crtc->config->has_pch_encoder) {
5558 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005559 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005560 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005561
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005562 intel_encoders_disable(crtc, old_crtc_state, old_state);
Daniel Vetterea9d7582012-07-10 10:42:52 +02005563
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005564 drm_crtc_vblank_off(crtc);
5565 assert_vblank_disabled(crtc);
5566
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005567 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005568
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005569 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005570
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005571 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005572 ironlake_fdi_disable(crtc);
5573
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005574 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005575
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005576 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005577 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005578
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005579 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005580 i915_reg_t reg;
5581 u32 temp;
5582
Daniel Vetterd925c592013-06-05 13:34:04 +02005583 /* disable TRANS_DP_CTL */
5584 reg = TRANS_DP_CTL(pipe);
5585 temp = I915_READ(reg);
5586 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5587 TRANS_DP_PORT_SEL_MASK);
5588 temp |= TRANS_DP_PORT_SEL_NONE;
5589 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005590
Daniel Vetterd925c592013-06-05 13:34:04 +02005591 /* disable DPLL_SEL */
5592 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005593 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005594 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005595 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005596
Daniel Vetterd925c592013-06-05 13:34:04 +02005597 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005598 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005599
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005600 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005601 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005602}
5603
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005604static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5605 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005606{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005607 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005608 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005609 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005610 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005611 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005612
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005613 if (intel_crtc->config->has_pch_encoder)
5614 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5615 false);
5616
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005617 intel_encoders_disable(crtc, old_crtc_state, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005618
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005619 drm_crtc_vblank_off(crtc);
5620 assert_vblank_disabled(crtc);
5621
Jani Nikula4d1de972016-03-18 17:05:42 +02005622 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005623 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005624 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005625
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005626 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005627 intel_ddi_set_vc_payload_alloc(crtc, false);
5628
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005629 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305630 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005631
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005632 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005633 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005634 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005635 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005636
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005637 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305638 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005639
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005640 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005641
Maarten Lankhorstb7076542016-08-23 16:18:08 +02005642 if (old_crtc_state->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005643 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5644 true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005645}
5646
Jesse Barnes2dd24552013-04-25 12:55:01 -07005647static void i9xx_pfit_enable(struct intel_crtc *crtc)
5648{
5649 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005650 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005651 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005652
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005653 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005654 return;
5655
Daniel Vetterc0b03412013-05-28 12:05:54 +02005656 /*
5657 * The panel fitter should only be adjusted whilst the pipe is disabled,
5658 * according to register description and PRM.
5659 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005660 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5661 assert_pipe_disabled(dev_priv, crtc->pipe);
5662
Jesse Barnesb074cec2013-04-25 12:55:02 -07005663 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5664 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005665
5666 /* Border color in case we don't scale up to the full screen. Black by
5667 * default, change to something else for debugging. */
5668 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005669}
5670
Dave Airlied05410f2014-06-05 13:22:59 +10005671static enum intel_display_power_domain port_to_power_domain(enum port port)
5672{
5673 switch (port) {
5674 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005675 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005676 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005677 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005678 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005679 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005680 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005681 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005682 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005683 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005684 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005685 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005686 return POWER_DOMAIN_PORT_OTHER;
5687 }
5688}
5689
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005690static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5691{
5692 switch (port) {
5693 case PORT_A:
5694 return POWER_DOMAIN_AUX_A;
5695 case PORT_B:
5696 return POWER_DOMAIN_AUX_B;
5697 case PORT_C:
5698 return POWER_DOMAIN_AUX_C;
5699 case PORT_D:
5700 return POWER_DOMAIN_AUX_D;
5701 case PORT_E:
5702 /* FIXME: Check VBT for actual wiring of PORT E */
5703 return POWER_DOMAIN_AUX_D;
5704 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005705 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005706 return POWER_DOMAIN_AUX_A;
5707 }
5708}
5709
Imre Deak319be8a2014-03-04 19:22:57 +02005710enum intel_display_power_domain
5711intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005712{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01005713 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
Imre Deak319be8a2014-03-04 19:22:57 +02005714 struct intel_digital_port *intel_dig_port;
5715
5716 switch (intel_encoder->type) {
5717 case INTEL_OUTPUT_UNKNOWN:
5718 /* Only DDI platforms should ever use this output type */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01005719 WARN_ON_ONCE(!HAS_DDI(dev_priv));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005720 case INTEL_OUTPUT_DP:
Imre Deak319be8a2014-03-04 19:22:57 +02005721 case INTEL_OUTPUT_HDMI:
5722 case INTEL_OUTPUT_EDP:
5723 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005724 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005725 case INTEL_OUTPUT_DP_MST:
5726 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5727 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005728 case INTEL_OUTPUT_ANALOG:
5729 return POWER_DOMAIN_PORT_CRT;
5730 case INTEL_OUTPUT_DSI:
5731 return POWER_DOMAIN_PORT_DSI;
5732 default:
5733 return POWER_DOMAIN_PORT_OTHER;
5734 }
5735}
5736
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005737enum intel_display_power_domain
5738intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5739{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01005740 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005741 struct intel_digital_port *intel_dig_port;
5742
5743 switch (intel_encoder->type) {
5744 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005745 case INTEL_OUTPUT_HDMI:
5746 /*
5747 * Only DDI platforms should ever use these output types.
5748 * We can get here after the HDMI detect code has already set
5749 * the type of the shared encoder. Since we can't be sure
5750 * what's the status of the given connectors, play safe and
5751 * run the DP detection too.
5752 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01005753 WARN_ON_ONCE(!HAS_DDI(dev_priv));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005754 case INTEL_OUTPUT_DP:
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005755 case INTEL_OUTPUT_EDP:
5756 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5757 return port_to_aux_power_domain(intel_dig_port->port);
5758 case INTEL_OUTPUT_DP_MST:
5759 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5760 return port_to_aux_power_domain(intel_dig_port->port);
5761 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005762 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005763 return POWER_DOMAIN_AUX_A;
5764 }
5765}
5766
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005767static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5768 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005769{
5770 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005771 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5773 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005774 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005775 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005776
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005777 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005778 return 0;
5779
Imre Deak77d22dc2014-03-05 16:20:52 +02005780 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5781 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005782 if (crtc_state->pch_pfit.enabled ||
5783 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005784 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5785
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005786 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5787 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5788
Imre Deak319be8a2014-03-04 19:22:57 +02005789 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005790 }
Imre Deak319be8a2014-03-04 19:22:57 +02005791
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005792 if (crtc_state->shared_dpll)
5793 mask |= BIT(POWER_DOMAIN_PLLS);
5794
Imre Deak77d22dc2014-03-05 16:20:52 +02005795 return mask;
5796}
5797
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005798static unsigned long
5799modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5800 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005801{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005802 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005803 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5804 enum intel_display_power_domain domain;
Daniel Vetter5a21b662016-05-24 17:13:53 +02005805 unsigned long domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005806
5807 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005808 intel_crtc->enabled_power_domains = new_domains =
5809 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005810
Daniel Vetter5a21b662016-05-24 17:13:53 +02005811 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005812
5813 for_each_power_domain(domain, domains)
5814 intel_display_power_get(dev_priv, domain);
5815
Daniel Vetter5a21b662016-05-24 17:13:53 +02005816 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005817}
5818
5819static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5820 unsigned long domains)
5821{
5822 enum intel_display_power_domain domain;
5823
5824 for_each_power_domain(domain, domains)
5825 intel_display_power_put(dev_priv, domain);
5826}
5827
Mika Kaholaadafdc62015-08-18 14:36:59 +03005828static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5829{
5830 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5831
5832 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5833 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5834 return max_cdclk_freq;
5835 else if (IS_CHERRYVIEW(dev_priv))
5836 return max_cdclk_freq*95/100;
5837 else if (INTEL_INFO(dev_priv)->gen < 4)
5838 return 2*max_cdclk_freq*90/100;
5839 else
5840 return max_cdclk_freq*90/100;
5841}
5842
Ville Syrjäläb2045352016-05-13 23:41:27 +03005843static int skl_calc_cdclk(int max_pixclk, int vco);
5844
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005845static void intel_update_max_cdclk(struct drm_device *dev)
5846{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005847 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005848
Tvrtko Ursulin08537232016-10-13 11:03:02 +01005849 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005850 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005851 int max_cdclk, vco;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005852
Ville Syrjäläb2045352016-05-13 23:41:27 +03005853 vco = dev_priv->skl_preferred_vco_freq;
Ville Syrjälä63911d72016-05-13 23:41:32 +03005854 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005855
5856 /*
5857 * Use the lower (vco 8640) cdclk values as a
5858 * first guess. skl_calc_cdclk() will correct it
5859 * if the preferred vco is 8100 instead.
5860 */
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005861 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005862 max_cdclk = 617143;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005863 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005864 max_cdclk = 540000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005865 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005866 max_cdclk = 432000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005867 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005868 max_cdclk = 308571;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005869
5870 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01005871 } else if (IS_BROXTON(dev_priv)) {
Matt Roper281c1142016-04-05 14:37:19 -07005872 dev_priv->max_cdclk_freq = 624000;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005873 } else if (IS_BROADWELL(dev_priv)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005874 /*
5875 * FIXME with extra cooling we can allow
5876 * 540 MHz for ULX and 675 Mhz for ULT.
5877 * How can we know if extra cooling is
5878 * available? PCI ID, VTB, something else?
5879 */
5880 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5881 dev_priv->max_cdclk_freq = 450000;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005882 else if (IS_BDW_ULX(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005883 dev_priv->max_cdclk_freq = 450000;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005884 else if (IS_BDW_ULT(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005885 dev_priv->max_cdclk_freq = 540000;
5886 else
5887 dev_priv->max_cdclk_freq = 675000;
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01005888 } else if (IS_CHERRYVIEW(dev_priv)) {
Mika Kahola0904dea2015-06-12 10:11:32 +03005889 dev_priv->max_cdclk_freq = 320000;
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01005890 } else if (IS_VALLEYVIEW(dev_priv)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005891 dev_priv->max_cdclk_freq = 400000;
5892 } else {
5893 /* otherwise assume cdclk is fixed */
5894 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5895 }
5896
Mika Kaholaadafdc62015-08-18 14:36:59 +03005897 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5898
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005899 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5900 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005901
5902 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5903 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005904}
5905
5906static void intel_update_cdclk(struct drm_device *dev)
5907{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005908 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005909
5910 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005911
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005912 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005913 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5914 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5915 dev_priv->cdclk_pll.ref);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005916 else
5917 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5918 dev_priv->cdclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005919
5920 /*
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005921 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5922 * Programmng [sic] note: bit[9:2] should be programmed to the number
5923 * of cdclk that generates 4MHz reference clock freq which is used to
5924 * generate GMBus clock. This will vary with the cdclk freq.
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005925 */
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005926 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005927 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005928}
5929
Ville Syrjälä92891e42016-05-11 22:44:45 +03005930/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5931static int skl_cdclk_decimal(int cdclk)
5932{
5933 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5934}
5935
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005936static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5937{
5938 int ratio;
5939
5940 if (cdclk == dev_priv->cdclk_pll.ref)
5941 return 0;
5942
5943 switch (cdclk) {
5944 default:
5945 MISSING_CASE(cdclk);
5946 case 144000:
5947 case 288000:
5948 case 384000:
5949 case 576000:
5950 ratio = 60;
5951 break;
5952 case 624000:
5953 ratio = 65;
5954 break;
5955 }
5956
5957 return dev_priv->cdclk_pll.ref * ratio;
5958}
5959
Ville Syrjälä2b730012016-05-13 23:41:34 +03005960static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5961{
5962 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5963
5964 /* Timeout 200us */
Chris Wilson95cac282016-06-30 15:33:03 +01005965 if (intel_wait_for_register(dev_priv,
5966 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5967 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005968 DRM_ERROR("timeout waiting for DE PLL unlock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005969
5970 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005971}
5972
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005973static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005974{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005975 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005976 u32 val;
5977
5978 val = I915_READ(BXT_DE_PLL_CTL);
5979 val &= ~BXT_DE_PLL_RATIO_MASK;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005980 val |= BXT_DE_PLL_RATIO(ratio);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005981 I915_WRITE(BXT_DE_PLL_CTL, val);
5982
5983 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5984
5985 /* Timeout 200us */
Chris Wilsone084e1b2016-06-30 15:33:04 +01005986 if (intel_wait_for_register(dev_priv,
5987 BXT_DE_PLL_ENABLE,
5988 BXT_DE_PLL_LOCK,
5989 BXT_DE_PLL_LOCK,
5990 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005991 DRM_ERROR("timeout waiting for DE PLL lock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005992
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005993 dev_priv->cdclk_pll.vco = vco;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005994}
5995
Imre Deak324513c2016-06-13 16:44:36 +03005996static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305997{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005998 u32 val, divider;
5999 int vco, ret;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306000
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006001 vco = bxt_de_pll_vco(dev_priv, cdclk);
6002
6003 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
6004
6005 /* cdclk = vco / 2 / div{1,1.5,2,4} */
6006 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
6007 case 8:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306008 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306009 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006010 case 4:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306011 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306012 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006013 case 3:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306014 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306015 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006016 case 2:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306017 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306018 break;
6019 default:
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006020 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
6021 WARN_ON(vco != 0);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306022
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006023 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
6024 break;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306025 }
6026
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306027 /* Inform power controller of upcoming frequency change */
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006028 mutex_lock(&dev_priv->rps.hw_lock);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306029 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
6030 0x80000000);
6031 mutex_unlock(&dev_priv->rps.hw_lock);
6032
6033 if (ret) {
6034 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006035 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306036 return;
6037 }
6038
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006039 if (dev_priv->cdclk_pll.vco != 0 &&
6040 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03006041 bxt_de_pll_disable(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306042
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006043 if (dev_priv->cdclk_pll.vco != vco)
6044 bxt_de_pll_enable(dev_priv, vco);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306045
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006046 val = divider | skl_cdclk_decimal(cdclk);
6047 /*
6048 * FIXME if only the cd2x divider needs changing, it could be done
6049 * without shutting off the pipe (if only one pipe is active).
6050 */
6051 val |= BXT_CDCLK_CD2X_PIPE_NONE;
6052 /*
6053 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6054 * enable otherwise.
6055 */
6056 if (cdclk >= 500000)
6057 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6058 I915_WRITE(CDCLK_CTL, val);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306059
6060 mutex_lock(&dev_priv->rps.hw_lock);
6061 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006062 DIV_ROUND_UP(cdclk, 25000));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306063 mutex_unlock(&dev_priv->rps.hw_lock);
6064
6065 if (ret) {
6066 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006067 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306068 return;
6069 }
6070
Chris Wilson91c8a322016-07-05 10:40:23 +01006071 intel_update_cdclk(&dev_priv->drm);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306072}
6073
Imre Deakd66a2192016-05-24 15:38:33 +03006074static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306075{
Imre Deakd66a2192016-05-24 15:38:33 +03006076 u32 cdctl, expected;
6077
Chris Wilson91c8a322016-07-05 10:40:23 +01006078 intel_update_cdclk(&dev_priv->drm);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306079
Imre Deakd66a2192016-05-24 15:38:33 +03006080 if (dev_priv->cdclk_pll.vco == 0 ||
6081 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
6082 goto sanitize;
6083
6084 /* DPLL okay; verify the cdclock
6085 *
6086 * Some BIOS versions leave an incorrect decimal frequency value and
6087 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
6088 * so sanitize this register.
6089 */
6090 cdctl = I915_READ(CDCLK_CTL);
6091 /*
6092 * Let's ignore the pipe field, since BIOS could have configured the
6093 * dividers both synching to an active pipe, or asynchronously
6094 * (PIPE_NONE).
6095 */
6096 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
6097
6098 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
6099 skl_cdclk_decimal(dev_priv->cdclk_freq);
6100 /*
6101 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6102 * enable otherwise.
6103 */
6104 if (dev_priv->cdclk_freq >= 500000)
6105 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6106
6107 if (cdctl == expected)
6108 /* All well; nothing to sanitize */
6109 return;
6110
6111sanitize:
6112 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
6113
6114 /* force cdclk programming */
6115 dev_priv->cdclk_freq = 0;
6116
6117 /* force full PLL disable + enable */
6118 dev_priv->cdclk_pll.vco = -1;
6119}
6120
Imre Deak324513c2016-06-13 16:44:36 +03006121void bxt_init_cdclk(struct drm_i915_private *dev_priv)
Imre Deakd66a2192016-05-24 15:38:33 +03006122{
6123 bxt_sanitize_cdclk(dev_priv);
6124
6125 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03006126 return;
Imre Deakc2e001e2016-04-01 16:02:43 +03006127
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306128 /*
6129 * FIXME:
6130 * - The initial CDCLK needs to be read from VBT.
6131 * Need to make this change after VBT has changes for BXT.
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306132 */
Imre Deak324513c2016-06-13 16:44:36 +03006133 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306134}
6135
Imre Deak324513c2016-06-13 16:44:36 +03006136void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306137{
Imre Deak324513c2016-06-13 16:44:36 +03006138 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306139}
6140
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006141static int skl_calc_cdclk(int max_pixclk, int vco)
6142{
Ville Syrjälä63911d72016-05-13 23:41:32 +03006143 if (vco == 8640000) {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006144 if (max_pixclk > 540000)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006145 return 617143;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006146 else if (max_pixclk > 432000)
6147 return 540000;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006148 else if (max_pixclk > 308571)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006149 return 432000;
6150 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006151 return 308571;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006152 } else {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006153 if (max_pixclk > 540000)
6154 return 675000;
6155 else if (max_pixclk > 450000)
6156 return 540000;
6157 else if (max_pixclk > 337500)
6158 return 450000;
6159 else
6160 return 337500;
6161 }
6162}
6163
Ville Syrjäläea617912016-05-13 23:41:24 +03006164static void
6165skl_dpll0_update(struct drm_i915_private *dev_priv)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006166{
Ville Syrjäläea617912016-05-13 23:41:24 +03006167 u32 val;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006168
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006169 dev_priv->cdclk_pll.ref = 24000;
Imre Deak1c3f7702016-05-24 15:38:32 +03006170 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006171
Ville Syrjäläea617912016-05-13 23:41:24 +03006172 val = I915_READ(LCPLL1_CTL);
Imre Deak1c3f7702016-05-24 15:38:32 +03006173 if ((val & LCPLL_PLL_ENABLE) == 0)
Ville Syrjäläea617912016-05-13 23:41:24 +03006174 return;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006175
Imre Deak1c3f7702016-05-24 15:38:32 +03006176 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
6177 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006178
Ville Syrjäläea617912016-05-13 23:41:24 +03006179 val = I915_READ(DPLL_CTRL1);
6180
Imre Deak1c3f7702016-05-24 15:38:32 +03006181 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
6182 DPLL_CTRL1_SSC(SKL_DPLL0) |
6183 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
6184 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
6185 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006186
Ville Syrjäläea617912016-05-13 23:41:24 +03006187 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
6188 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
6189 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
6190 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
6191 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03006192 dev_priv->cdclk_pll.vco = 8100000;
Ville Syrjäläea617912016-05-13 23:41:24 +03006193 break;
6194 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
6195 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03006196 dev_priv->cdclk_pll.vco = 8640000;
Ville Syrjäläea617912016-05-13 23:41:24 +03006197 break;
6198 default:
6199 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
Ville Syrjäläea617912016-05-13 23:41:24 +03006200 break;
6201 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006202}
6203
Ville Syrjäläb2045352016-05-13 23:41:27 +03006204void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
6205{
6206 bool changed = dev_priv->skl_preferred_vco_freq != vco;
6207
6208 dev_priv->skl_preferred_vco_freq = vco;
6209
6210 if (changed)
Chris Wilson91c8a322016-07-05 10:40:23 +01006211 intel_update_max_cdclk(&dev_priv->drm);
Ville Syrjäläb2045352016-05-13 23:41:27 +03006212}
6213
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006214static void
Ville Syrjälä3861fc62016-05-11 22:44:50 +03006215skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006216{
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006217 int min_cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006218 u32 val;
6219
Ville Syrjälä63911d72016-05-13 23:41:32 +03006220 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03006221
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006222 /* select the minimum CDCLK before enabling DPLL 0 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006223 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006224 I915_WRITE(CDCLK_CTL, val);
6225 POSTING_READ(CDCLK_CTL);
6226
6227 /*
6228 * We always enable DPLL0 with the lowest link rate possible, but still
6229 * taking into account the VCO required to operate the eDP panel at the
6230 * desired frequency. The usual DP link rates operate with a VCO of
6231 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
6232 * The modeset code is responsible for the selection of the exact link
6233 * rate later on, with the constraint of choosing a frequency that
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006234 * works with vco.
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006235 */
6236 val = I915_READ(DPLL_CTRL1);
6237
6238 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
6239 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
6240 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Ville Syrjälä63911d72016-05-13 23:41:32 +03006241 if (vco == 8640000)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006242 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
6243 SKL_DPLL0);
6244 else
6245 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
6246 SKL_DPLL0);
6247
6248 I915_WRITE(DPLL_CTRL1, val);
6249 POSTING_READ(DPLL_CTRL1);
6250
6251 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
6252
Chris Wilsone24ca052016-06-30 15:33:05 +01006253 if (intel_wait_for_register(dev_priv,
6254 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
6255 5))
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006256 DRM_ERROR("DPLL0 not locked\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006257
Ville Syrjälä63911d72016-05-13 23:41:32 +03006258 dev_priv->cdclk_pll.vco = vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +03006259
6260 /* We'll want to keep using the current vco from now on. */
6261 skl_set_preferred_cdclk_vco(dev_priv, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006262}
6263
Ville Syrjälä430e05d2016-05-11 22:44:47 +03006264static void
6265skl_dpll0_disable(struct drm_i915_private *dev_priv)
6266{
6267 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
Chris Wilson8ad32a052016-06-30 15:33:06 +01006268 if (intel_wait_for_register(dev_priv,
6269 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
6270 1))
Ville Syrjälä430e05d2016-05-11 22:44:47 +03006271 DRM_ERROR("Couldn't disable DPLL0\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006272
Ville Syrjälä63911d72016-05-13 23:41:32 +03006273 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä430e05d2016-05-11 22:44:47 +03006274}
6275
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006276static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
6277{
6278 int ret;
6279 u32 val;
6280
6281 /* inform PCU we want to change CDCLK */
6282 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
6283 mutex_lock(&dev_priv->rps.hw_lock);
6284 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
6285 mutex_unlock(&dev_priv->rps.hw_lock);
6286
6287 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
6288}
6289
6290static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
6291{
Ville Syrjälä848496e2016-07-13 16:32:03 +03006292 return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006293}
6294
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006295static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006296{
Chris Wilson91c8a322016-07-05 10:40:23 +01006297 struct drm_device *dev = &dev_priv->drm;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006298 u32 freq_select, pcu_ack;
6299
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006300 WARN_ON((cdclk == 24000) != (vco == 0));
6301
Ville Syrjälä63911d72016-05-13 23:41:32 +03006302 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006303
6304 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
6305 DRM_ERROR("failed to inform PCU about cdclk change\n");
6306 return;
6307 }
6308
6309 /* set CDCLK_CTL */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006310 switch (cdclk) {
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006311 case 450000:
6312 case 432000:
6313 freq_select = CDCLK_FREQ_450_432;
6314 pcu_ack = 1;
6315 break;
6316 case 540000:
6317 freq_select = CDCLK_FREQ_540;
6318 pcu_ack = 2;
6319 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006320 case 308571:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006321 case 337500:
6322 default:
6323 freq_select = CDCLK_FREQ_337_308;
6324 pcu_ack = 0;
6325 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006326 case 617143:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006327 case 675000:
6328 freq_select = CDCLK_FREQ_675_617;
6329 pcu_ack = 3;
6330 break;
6331 }
6332
Ville Syrjälä63911d72016-05-13 23:41:32 +03006333 if (dev_priv->cdclk_pll.vco != 0 &&
6334 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006335 skl_dpll0_disable(dev_priv);
6336
Ville Syrjälä63911d72016-05-13 23:41:32 +03006337 if (dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006338 skl_dpll0_enable(dev_priv, vco);
6339
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006340 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006341 POSTING_READ(CDCLK_CTL);
6342
6343 /* inform PCU of the change */
6344 mutex_lock(&dev_priv->rps.hw_lock);
6345 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
6346 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01006347
6348 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006349}
6350
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006351static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
6352
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006353void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
6354{
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006355 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006356}
6357
6358void skl_init_cdclk(struct drm_i915_private *dev_priv)
6359{
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006360 int cdclk, vco;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006361
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006362 skl_sanitize_cdclk(dev_priv);
6363
Ville Syrjälä63911d72016-05-13 23:41:32 +03006364 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006365 /*
6366 * Use the current vco as our initial
6367 * guess as to what the preferred vco is.
6368 */
6369 if (dev_priv->skl_preferred_vco_freq == 0)
6370 skl_set_preferred_cdclk_vco(dev_priv,
Ville Syrjälä63911d72016-05-13 23:41:32 +03006371 dev_priv->cdclk_pll.vco);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006372 return;
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006373 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006374
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006375 vco = dev_priv->skl_preferred_vco_freq;
6376 if (vco == 0)
Ville Syrjälä63911d72016-05-13 23:41:32 +03006377 vco = 8100000;
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006378 cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006379
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006380 skl_set_cdclk(dev_priv, cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006381}
6382
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006383static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306384{
Ville Syrjälä09492492016-05-13 23:41:28 +03006385 uint32_t cdctl, expected;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306386
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05306387 /*
6388 * check if the pre-os intialized the display
6389 * There is SWF18 scratchpad register defined which is set by the
6390 * pre-os which can be used by the OS drivers to check the status
6391 */
6392 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
6393 goto sanitize;
6394
Chris Wilson91c8a322016-07-05 10:40:23 +01006395 intel_update_cdclk(&dev_priv->drm);
Imre Deak1c3f7702016-05-24 15:38:32 +03006396 /* Is PLL enabled and locked ? */
6397 if (dev_priv->cdclk_pll.vco == 0 ||
6398 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
6399 goto sanitize;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006400
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306401 /* DPLL okay; verify the cdclock
6402 *
6403 * Noticed in some instances that the freq selection is correct but
6404 * decimal part is programmed wrong from BIOS where pre-os does not
6405 * enable display. Verify the same as well.
6406 */
Ville Syrjälä09492492016-05-13 23:41:28 +03006407 cdctl = I915_READ(CDCLK_CTL);
6408 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
6409 skl_cdclk_decimal(dev_priv->cdclk_freq);
6410 if (cdctl == expected)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306411 /* All well; nothing to sanitize */
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006412 return;
6413
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306414sanitize:
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006415 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
Clint Taylorc89e39f2016-05-13 23:41:21 +03006416
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006417 /* force cdclk programming */
6418 dev_priv->cdclk_freq = 0;
6419 /* force full PLL disable + enable */
Ville Syrjälä63911d72016-05-13 23:41:32 +03006420 dev_priv->cdclk_pll.vco = -1;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306421}
6422
Jesse Barnes30a970c2013-11-04 13:48:12 -08006423/* Adjust CDclk dividers to allow high res or save power if possible */
6424static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
6425{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006426 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006427 u32 val, cmd;
6428
Vandana Kannan164dfd22014-11-24 13:37:41 +05306429 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
6430 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02006431
Ville Syrjälädfcab172014-06-13 13:37:47 +03006432 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08006433 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03006434 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006435 cmd = 1;
6436 else
6437 cmd = 0;
6438
6439 mutex_lock(&dev_priv->rps.hw_lock);
6440 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6441 val &= ~DSPFREQGUAR_MASK;
6442 val |= (cmd << DSPFREQGUAR_SHIFT);
6443 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6444 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6445 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
6446 50)) {
6447 DRM_ERROR("timed out waiting for CDclk change\n");
6448 }
6449 mutex_unlock(&dev_priv->rps.hw_lock);
6450
Ville Syrjälä54433e92015-05-26 20:42:31 +03006451 mutex_lock(&dev_priv->sb_lock);
6452
Ville Syrjälädfcab172014-06-13 13:37:47 +03006453 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006454 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006455
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006456 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006457
Jesse Barnes30a970c2013-11-04 13:48:12 -08006458 /* adjust cdclk divider */
6459 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03006460 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006461 val |= divider;
6462 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03006463
6464 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03006465 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03006466 50))
6467 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08006468 }
6469
Jesse Barnes30a970c2013-11-04 13:48:12 -08006470 /* adjust self-refresh exit latency value */
6471 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
6472 val &= ~0x7f;
6473
6474 /*
6475 * For high bandwidth configs, we set a higher latency in the bunit
6476 * so that the core display fetch happens in time to avoid underruns.
6477 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03006478 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006479 val |= 4500 / 250; /* 4.5 usec */
6480 else
6481 val |= 3000 / 250; /* 3.0 usec */
6482 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03006483
Ville Syrjäläa5805162015-05-26 20:42:30 +03006484 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006485
Ville Syrjäläb6283052015-06-03 15:45:07 +03006486 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006487}
6488
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006489static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
6490{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006491 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006492 u32 val, cmd;
6493
Vandana Kannan164dfd22014-11-24 13:37:41 +05306494 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
6495 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006496
6497 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006498 case 333333:
6499 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006500 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006501 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006502 break;
6503 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01006504 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006505 return;
6506 }
6507
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02006508 /*
6509 * Specs are full of misinformation, but testing on actual
6510 * hardware has shown that we just need to write the desired
6511 * CCK divider into the Punit register.
6512 */
6513 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
6514
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006515 mutex_lock(&dev_priv->rps.hw_lock);
6516 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6517 val &= ~DSPFREQGUAR_MASK_CHV;
6518 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
6519 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6520 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6521 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
6522 50)) {
6523 DRM_ERROR("timed out waiting for CDclk change\n");
6524 }
6525 mutex_unlock(&dev_priv->rps.hw_lock);
6526
Ville Syrjäläb6283052015-06-03 15:45:07 +03006527 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006528}
6529
Jesse Barnes30a970c2013-11-04 13:48:12 -08006530static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6531 int max_pixclk)
6532{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006533 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006534 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006535
Jesse Barnes30a970c2013-11-04 13:48:12 -08006536 /*
6537 * Really only a few cases to deal with, as only 4 CDclks are supported:
6538 * 200MHz
6539 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006540 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006541 * 400MHz (VLV only)
6542 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6543 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006544 *
6545 * We seem to get an unstable or solid color picture at 200MHz.
6546 * Not sure what's wrong. For now use 200MHz only when all pipes
6547 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08006548 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006549 if (!IS_CHERRYVIEW(dev_priv) &&
6550 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006551 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006552 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006553 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006554 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006555 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006556 else
6557 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006558}
6559
Imre Deak324513c2016-06-13 16:44:36 +03006560static int bxt_calc_cdclk(int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006561{
Ville Syrjälä760e1472016-05-11 22:44:46 +03006562 if (max_pixclk > 576000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306563 return 624000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006564 else if (max_pixclk > 384000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306565 return 576000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006566 else if (max_pixclk > 288000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306567 return 384000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006568 else if (max_pixclk > 144000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306569 return 288000;
6570 else
6571 return 144000;
6572}
6573
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01006574/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006575static int intel_mode_max_pixclk(struct drm_device *dev,
6576 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006577{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006578 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +01006579 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006580 struct drm_crtc *crtc;
6581 struct drm_crtc_state *crtc_state;
6582 unsigned max_pixclk = 0, i;
6583 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006584
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006585 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6586 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006587
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006588 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6589 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006590
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006591 if (crtc_state->enable)
6592 pixclk = crtc_state->adjusted_mode.crtc_clock;
6593
6594 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006595 }
6596
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006597 for_each_pipe(dev_priv, pipe)
6598 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6599
Jesse Barnes30a970c2013-11-04 13:48:12 -08006600 return max_pixclk;
6601}
6602
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006603static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006604{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006605 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006606 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006607 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006608 struct intel_atomic_state *intel_state =
6609 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006610
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006611 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006612 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306613
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006614 if (!intel_state->active_crtcs)
6615 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6616
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006617 return 0;
6618}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006619
Imre Deak324513c2016-06-13 16:44:36 +03006620static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006621{
Ville Syrjälä4e5ca602016-05-11 22:44:44 +03006622 int max_pixclk = ilk_max_pixel_rate(state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006623 struct intel_atomic_state *intel_state =
6624 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006625
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006626 intel_state->cdclk = intel_state->dev_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +03006627 bxt_calc_cdclk(max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006628
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006629 if (!intel_state->active_crtcs)
Imre Deak324513c2016-06-13 16:44:36 +03006630 intel_state->dev_cdclk = bxt_calc_cdclk(0);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006631
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006632 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006633}
6634
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006635static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6636{
6637 unsigned int credits, default_credits;
6638
6639 if (IS_CHERRYVIEW(dev_priv))
6640 default_credits = PFI_CREDIT(12);
6641 else
6642 default_credits = PFI_CREDIT(8);
6643
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006644 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006645 /* CHV suggested value is 31 or 63 */
6646 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006647 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006648 else
6649 credits = PFI_CREDIT(15);
6650 } else {
6651 credits = default_credits;
6652 }
6653
6654 /*
6655 * WA - write default credits before re-programming
6656 * FIXME: should we also set the resend bit here?
6657 */
6658 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6659 default_credits);
6660
6661 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6662 credits | PFI_CREDIT_RESEND);
6663
6664 /*
6665 * FIXME is this guaranteed to clear
6666 * immediately or should we poll for it?
6667 */
6668 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6669}
6670
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006671static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006672{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006673 struct drm_device *dev = old_state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006674 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006675 struct intel_atomic_state *old_intel_state =
6676 to_intel_atomic_state(old_state);
6677 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006678
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006679 /*
6680 * FIXME: We can end up here with all power domains off, yet
6681 * with a CDCLK frequency other than the minimum. To account
6682 * for this take the PIPE-A power domain, which covers the HW
6683 * blocks needed for the following programming. This can be
6684 * removed once it's guaranteed that we get here either with
6685 * the minimum CDCLK set, or the required power domains
6686 * enabled.
6687 */
6688 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006689
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006690 if (IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006691 cherryview_set_cdclk(dev, req_cdclk);
6692 else
6693 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006694
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006695 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006696
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006697 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006698}
6699
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006700static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
6701 struct drm_atomic_state *old_state)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006702{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006703 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006704 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006705 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006706 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006707 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006708
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006709 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006710 return;
6711
Ville Syrjälä37a56502016-06-22 21:57:04 +03006712 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306713 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006714
6715 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006716 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006717
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006718 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
Chris Wilsonfac5e232016-07-04 11:34:36 +01006719 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006720
6721 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6722 I915_WRITE(CHV_CANVAS(pipe), 0);
6723 }
6724
Daniel Vetter5b18e572014-04-24 23:55:06 +02006725 i9xx_set_pipeconf(intel_crtc);
6726
Jesse Barnes89b667f2013-04-18 14:51:36 -07006727 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006728
Daniel Vettera72e4c92014-09-30 10:56:47 +02006729 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006730
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006731 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006732
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006733 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006734 chv_prepare_pll(intel_crtc, intel_crtc->config);
6735 chv_enable_pll(intel_crtc, intel_crtc->config);
6736 } else {
6737 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6738 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006739 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006740
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006741 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006742
Jesse Barnes2dd24552013-04-25 12:55:01 -07006743 i9xx_pfit_enable(intel_crtc);
6744
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006745 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006746
Ville Syrjälä432081b2016-10-31 22:37:03 +02006747 intel_update_watermarks(intel_crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006748 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006749
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006750 assert_vblank_disabled(crtc);
6751 drm_crtc_vblank_on(crtc);
6752
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006753 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006754}
6755
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006756static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6757{
6758 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006759 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006760
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006761 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6762 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006763}
6764
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006765static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
6766 struct drm_atomic_state *old_state)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006767{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006768 struct drm_crtc *crtc = pipe_config->base.crtc;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006769 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006770 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006771 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006772 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006773
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006774 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006775 return;
6776
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006777 i9xx_set_pll_dividers(intel_crtc);
6778
Ville Syrjälä37a56502016-06-22 21:57:04 +03006779 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306780 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006781
6782 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006783 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006784
Daniel Vetter5b18e572014-04-24 23:55:06 +02006785 i9xx_set_pipeconf(intel_crtc);
6786
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006787 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006788
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006789 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006790 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006791
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006792 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006793
Daniel Vetterf6736a12013-06-05 13:34:30 +02006794 i9xx_enable_pll(intel_crtc);
6795
Jesse Barnes2dd24552013-04-25 12:55:01 -07006796 i9xx_pfit_enable(intel_crtc);
6797
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006798 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006799
Ville Syrjälä432081b2016-10-31 22:37:03 +02006800 intel_update_watermarks(intel_crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006801 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006802
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006803 assert_vblank_disabled(crtc);
6804 drm_crtc_vblank_on(crtc);
6805
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006806 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006807}
6808
Daniel Vetter87476d62013-04-11 16:29:06 +02006809static void i9xx_pfit_disable(struct intel_crtc *crtc)
6810{
6811 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006812 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter328d8e82013-05-08 10:36:31 +02006813
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006814 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006815 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006816
6817 assert_pipe_disabled(dev_priv, crtc->pipe);
6818
Daniel Vetter328d8e82013-05-08 10:36:31 +02006819 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6820 I915_READ(PFIT_CONTROL));
6821 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006822}
6823
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006824static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
6825 struct drm_atomic_state *old_state)
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006826{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006827 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006828 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006829 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006830 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6831 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006832
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006833 /*
6834 * On gen2 planes are double buffered but the pipe isn't, so we must
6835 * wait for planes to fully turn off before disabling the pipe.
6836 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006837 if (IS_GEN2(dev_priv))
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02006838 intel_wait_for_vblank(dev_priv, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006839
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006840 intel_encoders_disable(crtc, old_crtc_state, old_state);
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006841
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006842 drm_crtc_vblank_off(crtc);
6843 assert_vblank_disabled(crtc);
6844
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006845 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006846
Daniel Vetter87476d62013-04-11 16:29:06 +02006847 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006848
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006849 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006850
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006851 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006852 if (IS_CHERRYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006853 chv_disable_pll(dev_priv, pipe);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01006854 else if (IS_VALLEYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006855 vlv_disable_pll(dev_priv, pipe);
6856 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006857 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006858 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006859
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006860 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006861
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006862 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006863 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006864}
6865
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006866static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006867{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006868 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006869 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006870 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006871 enum intel_display_power_domain domain;
6872 unsigned long domains;
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006873 struct drm_atomic_state *state;
6874 struct intel_crtc_state *crtc_state;
6875 int ret;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006876
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006877 if (!intel_crtc->active)
6878 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006879
Ville Syrjälä936e71e2016-07-26 19:06:59 +03006880 if (to_intel_plane_state(crtc->primary->state)->base.visible) {
Daniel Vetter5a21b662016-05-24 17:13:53 +02006881 WARN_ON(intel_crtc->flip_work);
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006882
Ville Syrjälä2622a082016-03-09 19:07:26 +02006883 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006884
6885 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
Ville Syrjälä936e71e2016-07-26 19:06:59 +03006886 to_intel_plane_state(crtc->primary->state)->base.visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006887 }
6888
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006889 state = drm_atomic_state_alloc(crtc->dev);
6890 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
6891
6892 /* Everything's already locked, -EDEADLK can't happen. */
6893 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
6894 ret = drm_atomic_add_affected_connectors(state, crtc);
6895
6896 WARN_ON(IS_ERR(crtc_state) || ret);
6897
6898 dev_priv->display.crtc_disable(crtc_state, state);
6899
Chris Wilson08536952016-10-14 13:18:18 +01006900 drm_atomic_state_put(state);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006901
Ville Syrjälä78108b72016-05-27 20:59:19 +03006902 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6903 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006904
6905 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6906 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006907 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006908 crtc->enabled = false;
6909 crtc->state->connector_mask = 0;
6910 crtc->state->encoder_mask = 0;
6911
6912 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6913 encoder->base.crtc = NULL;
6914
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006915 intel_fbc_disable(intel_crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02006916 intel_update_watermarks(intel_crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006917 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006918
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006919 domains = intel_crtc->enabled_power_domains;
6920 for_each_power_domain(domain, domains)
6921 intel_display_power_put(dev_priv, domain);
6922 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006923
6924 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6925 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006926}
6927
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006928/*
6929 * turn all crtc's off, but do not adjust state
6930 * This has to be paired with a call to intel_modeset_setup_hw_state.
6931 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006932int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006933{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006934 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006935 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006936 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006937
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006938 state = drm_atomic_helper_suspend(dev);
6939 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006940 if (ret)
6941 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006942 else
6943 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006944 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006945}
6946
Chris Wilsonea5b2132010-08-04 13:50:23 +01006947void intel_encoder_destroy(struct drm_encoder *encoder)
6948{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006949 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006950
Chris Wilsonea5b2132010-08-04 13:50:23 +01006951 drm_encoder_cleanup(encoder);
6952 kfree(intel_encoder);
6953}
6954
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006955/* Cross check the actual hw state with our own modeset state tracking (and it's
6956 * internal consistency). */
Daniel Vetter5a21b662016-05-24 17:13:53 +02006957static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006958{
Daniel Vetter5a21b662016-05-24 17:13:53 +02006959 struct drm_crtc *crtc = connector->base.state->crtc;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006960
6961 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6962 connector->base.base.id,
6963 connector->base.name);
6964
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006965 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006966 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter5a21b662016-05-24 17:13:53 +02006967 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006968
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006969 I915_STATE_WARN(!crtc,
6970 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006971
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006972 if (!crtc)
6973 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006974
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006975 I915_STATE_WARN(!crtc->state->active,
6976 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006977
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006978 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006979 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006980
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006981 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006982 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006983
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006984 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006985 "attached encoder crtc differs from connector crtc\n");
6986 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006987 I915_STATE_WARN(crtc && crtc->state->active,
6988 "attached crtc is active, but connector isn't\n");
Daniel Vetter5a21b662016-05-24 17:13:53 +02006989 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006990 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006991 }
6992}
6993
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006994int intel_connector_init(struct intel_connector *connector)
6995{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006996 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006997
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006998 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006999 return -ENOMEM;
7000
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03007001 return 0;
7002}
7003
7004struct intel_connector *intel_connector_alloc(void)
7005{
7006 struct intel_connector *connector;
7007
7008 connector = kzalloc(sizeof *connector, GFP_KERNEL);
7009 if (!connector)
7010 return NULL;
7011
7012 if (intel_connector_init(connector) < 0) {
7013 kfree(connector);
7014 return NULL;
7015 }
7016
7017 return connector;
7018}
7019
Daniel Vetterf0947c32012-07-02 13:10:34 +02007020/* Simple connector->get_hw_state implementation for encoders that support only
7021 * one connector and no cloning and hence the encoder state determines the state
7022 * of the connector. */
7023bool intel_connector_get_hw_state(struct intel_connector *connector)
7024{
Daniel Vetter24929352012-07-02 20:28:59 +02007025 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02007026 struct intel_encoder *encoder = connector->encoder;
7027
7028 return encoder->get_hw_state(encoder, &pipe);
7029}
7030
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007031static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02007032{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007033 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
7034 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02007035
7036 return 0;
7037}
7038
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007039static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007040 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007041{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01007042 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007043 struct drm_atomic_state *state = pipe_config->base.state;
7044 struct intel_crtc *other_crtc;
7045 struct intel_crtc_state *other_crtc_state;
7046
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007047 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
7048 pipe_name(pipe), pipe_config->fdi_lanes);
7049 if (pipe_config->fdi_lanes > 4) {
7050 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
7051 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007052 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007053 }
7054
Tvrtko Ursulin86527442016-10-13 11:03:00 +01007055 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007056 if (pipe_config->fdi_lanes > 2) {
7057 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
7058 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007059 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007060 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007061 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007062 }
7063 }
7064
7065 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007066 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007067
7068 /* Ivybridge 3 pipe is really complicated */
7069 switch (pipe) {
7070 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007071 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007072 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007073 if (pipe_config->fdi_lanes <= 2)
7074 return 0;
7075
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02007076 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007077 other_crtc_state =
7078 intel_atomic_get_crtc_state(state, other_crtc);
7079 if (IS_ERR(other_crtc_state))
7080 return PTR_ERR(other_crtc_state);
7081
7082 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007083 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
7084 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007085 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007086 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007087 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007088 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02007089 if (pipe_config->fdi_lanes > 2) {
7090 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
7091 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007092 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02007093 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007094
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02007095 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007096 other_crtc_state =
7097 intel_atomic_get_crtc_state(state, other_crtc);
7098 if (IS_ERR(other_crtc_state))
7099 return PTR_ERR(other_crtc_state);
7100
7101 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007102 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007103 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007104 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007105 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007106 default:
7107 BUG();
7108 }
7109}
7110
Daniel Vettere29c22c2013-02-21 00:00:16 +01007111#define RETRY 1
7112static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007113 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02007114{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007115 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007116 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007117 int lane, link_bw, fdi_dotclock, ret;
7118 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02007119
Daniel Vettere29c22c2013-02-21 00:00:16 +01007120retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02007121 /* FDI is a binary signal running at ~2.7GHz, encoding
7122 * each output octet as 10 bits. The actual frequency
7123 * is stored as a divider into a 100MHz clock, and the
7124 * mode pixel clock is stored in units of 1KHz.
7125 * Hence the bw of each lane in terms of the mode signal
7126 * is:
7127 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02007128 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02007129
Damien Lespiau241bfc32013-09-25 16:45:37 +01007130 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02007131
Daniel Vetter2bd89a02013-06-01 17:16:19 +02007132 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02007133 pipe_config->pipe_bpp);
7134
7135 pipe_config->fdi_lanes = lane;
7136
Daniel Vetter2bd89a02013-06-01 17:16:19 +02007137 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02007138 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007139
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02007140 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007141 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01007142 pipe_config->pipe_bpp -= 2*3;
7143 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
7144 pipe_config->pipe_bpp);
7145 needs_recompute = true;
7146 pipe_config->bw_constrained = true;
7147
7148 goto retry;
7149 }
7150
7151 if (needs_recompute)
7152 return RETRY;
7153
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007154 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02007155}
7156
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007157static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
7158 struct intel_crtc_state *pipe_config)
7159{
7160 if (pipe_config->pipe_bpp > 24)
7161 return false;
7162
7163 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007164 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007165 return true;
7166
7167 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03007168 * We compare against max which means we must take
7169 * the increased cdclk requirement into account when
7170 * calculating the new cdclk.
7171 *
7172 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007173 */
7174 return ilk_pipe_pixel_rate(pipe_config) <=
7175 dev_priv->max_cdclk_freq * 95 / 100;
7176}
7177
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007178static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007179 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007180{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007181 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007182 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007183
Jani Nikulad330a952014-01-21 11:24:25 +02007184 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007185 hsw_crtc_supports_ips(crtc) &&
7186 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007187}
7188
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02007189static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
7190{
7191 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7192
7193 /* GDG double wide on either pipe, otherwise pipe A only */
7194 return INTEL_INFO(dev_priv)->gen < 4 &&
7195 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
7196}
7197
Daniel Vettera43f6e02013-06-07 23:10:32 +02007198static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007199 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08007200{
Daniel Vettera43f6e02013-06-07 23:10:32 +02007201 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007202 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007203 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03007204 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01007205
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007206 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03007207 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007208
7209 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02007210 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007211 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007212 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02007213 if (intel_crtc_supports_double_wide(crtc) &&
7214 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03007215 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007216 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03007217 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03007218 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03007219
Ville Syrjäläf3261152016-05-24 21:34:18 +03007220 if (adjusted_mode->crtc_clock > clock_limit) {
7221 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
7222 adjusted_mode->crtc_clock, clock_limit,
7223 yesno(pipe_config->double_wide));
7224 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08007225 }
Chris Wilson89749352010-09-12 18:25:19 +01007226
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03007227 /*
7228 * Pipe horizontal size must be even in:
7229 * - DVO ganged mode
7230 * - LVDS dual channel mode
7231 * - Double wide pipe
7232 */
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007233 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03007234 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
7235 pipe_config->pipe_src_w &= ~1;
7236
Damien Lespiau8693a822013-05-03 18:48:11 +01007237 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
7238 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03007239 */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007240 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03007241 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01007242 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03007243
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007244 if (HAS_IPS(dev_priv))
Daniel Vettera43f6e02013-06-07 23:10:32 +02007245 hsw_compute_ips_config(crtc, pipe_config);
7246
Daniel Vetter877d48d2013-04-19 11:24:43 +02007247 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02007248 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02007249
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02007250 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007251}
7252
Ville Syrjälä1652d192015-03-31 14:12:01 +03007253static int skylake_get_display_clock_speed(struct drm_device *dev)
7254{
7255 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläea617912016-05-13 23:41:24 +03007256 uint32_t cdctl;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007257
Ville Syrjäläea617912016-05-13 23:41:24 +03007258 skl_dpll0_update(dev_priv);
7259
Ville Syrjälä63911d72016-05-13 23:41:32 +03007260 if (dev_priv->cdclk_pll.vco == 0)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03007261 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007262
Ville Syrjäläea617912016-05-13 23:41:24 +03007263 cdctl = I915_READ(CDCLK_CTL);
Ville Syrjälä1652d192015-03-31 14:12:01 +03007264
Ville Syrjälä63911d72016-05-13 23:41:32 +03007265 if (dev_priv->cdclk_pll.vco == 8640000) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03007266 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7267 case CDCLK_FREQ_450_432:
7268 return 432000;
7269 case CDCLK_FREQ_337_308:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03007270 return 308571;
Ville Syrjäläea617912016-05-13 23:41:24 +03007271 case CDCLK_FREQ_540:
7272 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007273 case CDCLK_FREQ_675_617:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03007274 return 617143;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007275 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03007276 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03007277 }
7278 } else {
Ville Syrjälä1652d192015-03-31 14:12:01 +03007279 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7280 case CDCLK_FREQ_450_432:
7281 return 450000;
7282 case CDCLK_FREQ_337_308:
7283 return 337500;
Ville Syrjäläea617912016-05-13 23:41:24 +03007284 case CDCLK_FREQ_540:
7285 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007286 case CDCLK_FREQ_675_617:
7287 return 675000;
7288 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03007289 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03007290 }
7291 }
7292
Ville Syrjälä709e05c2016-05-13 23:41:33 +03007293 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007294}
7295
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007296static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
7297{
7298 u32 val;
7299
7300 dev_priv->cdclk_pll.ref = 19200;
Imre Deak1c3f7702016-05-24 15:38:32 +03007301 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007302
7303 val = I915_READ(BXT_DE_PLL_ENABLE);
Imre Deak1c3f7702016-05-24 15:38:32 +03007304 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007305 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007306
Imre Deak1c3f7702016-05-24 15:38:32 +03007307 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
7308 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007309
7310 val = I915_READ(BXT_DE_PLL_CTL);
7311 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
7312 dev_priv->cdclk_pll.ref;
7313}
7314
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007315static int broxton_get_display_clock_speed(struct drm_device *dev)
7316{
7317 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf5986242016-05-13 23:41:37 +03007318 u32 divider;
7319 int div, vco;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007320
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007321 bxt_de_pll_update(dev_priv);
7322
Ville Syrjäläf5986242016-05-13 23:41:37 +03007323 vco = dev_priv->cdclk_pll.vco;
7324 if (vco == 0)
7325 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007326
Ville Syrjäläf5986242016-05-13 23:41:37 +03007327 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007328
Ville Syrjäläf5986242016-05-13 23:41:37 +03007329 switch (divider) {
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007330 case BXT_CDCLK_CD2X_DIV_SEL_1:
Ville Syrjäläf5986242016-05-13 23:41:37 +03007331 div = 2;
7332 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007333 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
Ville Syrjäläf5986242016-05-13 23:41:37 +03007334 div = 3;
7335 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007336 case BXT_CDCLK_CD2X_DIV_SEL_2:
Ville Syrjäläf5986242016-05-13 23:41:37 +03007337 div = 4;
7338 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007339 case BXT_CDCLK_CD2X_DIV_SEL_4:
Ville Syrjäläf5986242016-05-13 23:41:37 +03007340 div = 8;
7341 break;
7342 default:
7343 MISSING_CASE(divider);
7344 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007345 }
7346
Ville Syrjäläf5986242016-05-13 23:41:37 +03007347 return DIV_ROUND_CLOSEST(vco, div);
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007348}
7349
Ville Syrjälä1652d192015-03-31 14:12:01 +03007350static int broadwell_get_display_clock_speed(struct drm_device *dev)
7351{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007352 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1652d192015-03-31 14:12:01 +03007353 uint32_t lcpll = I915_READ(LCPLL_CTL);
7354 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7355
7356 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7357 return 800000;
7358 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7359 return 450000;
7360 else if (freq == LCPLL_CLK_FREQ_450)
7361 return 450000;
7362 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
7363 return 540000;
7364 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
7365 return 337500;
7366 else
7367 return 675000;
7368}
7369
7370static int haswell_get_display_clock_speed(struct drm_device *dev)
7371{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007372 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1652d192015-03-31 14:12:01 +03007373 uint32_t lcpll = I915_READ(LCPLL_CTL);
7374 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7375
7376 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7377 return 800000;
7378 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7379 return 450000;
7380 else if (freq == LCPLL_CLK_FREQ_450)
7381 return 450000;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007382 else if (IS_HSW_ULT(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +03007383 return 337500;
7384 else
7385 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007386}
7387
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07007388static int valleyview_get_display_clock_speed(struct drm_device *dev)
7389{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007390 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
7391 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07007392}
7393
Ville Syrjäläb37a6432015-03-31 14:11:54 +03007394static int ilk_get_display_clock_speed(struct drm_device *dev)
7395{
7396 return 450000;
7397}
7398
Jesse Barnese70236a2009-09-21 10:42:27 -07007399static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08007400{
Jesse Barnese70236a2009-09-21 10:42:27 -07007401 return 400000;
7402}
Jesse Barnes79e53942008-11-07 14:24:08 -08007403
Jesse Barnese70236a2009-09-21 10:42:27 -07007404static int i915_get_display_clock_speed(struct drm_device *dev)
7405{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007406 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07007407}
Jesse Barnes79e53942008-11-07 14:24:08 -08007408
Jesse Barnese70236a2009-09-21 10:42:27 -07007409static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
7410{
7411 return 200000;
7412}
Jesse Barnes79e53942008-11-07 14:24:08 -08007413
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007414static int pnv_get_display_clock_speed(struct drm_device *dev)
7415{
David Weinehall52a05c32016-08-22 13:32:44 +03007416 struct pci_dev *pdev = dev->pdev;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007417 u16 gcfgc = 0;
7418
David Weinehall52a05c32016-08-22 13:32:44 +03007419 pci_read_config_word(pdev, GCFGC, &gcfgc);
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007420
7421 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7422 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007423 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007424 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007425 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007426 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007427 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007428 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
7429 return 200000;
7430 default:
7431 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
7432 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007433 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007434 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007435 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007436 }
7437}
7438
Jesse Barnese70236a2009-09-21 10:42:27 -07007439static int i915gm_get_display_clock_speed(struct drm_device *dev)
7440{
David Weinehall52a05c32016-08-22 13:32:44 +03007441 struct pci_dev *pdev = dev->pdev;
Jesse Barnese70236a2009-09-21 10:42:27 -07007442 u16 gcfgc = 0;
7443
David Weinehall52a05c32016-08-22 13:32:44 +03007444 pci_read_config_word(pdev, GCFGC, &gcfgc);
Jesse Barnese70236a2009-09-21 10:42:27 -07007445
7446 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03007447 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07007448 else {
7449 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7450 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007451 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07007452 default:
7453 case GC_DISPLAY_CLOCK_190_200_MHZ:
7454 return 190000;
7455 }
7456 }
7457}
Jesse Barnes79e53942008-11-07 14:24:08 -08007458
Jesse Barnese70236a2009-09-21 10:42:27 -07007459static int i865_get_display_clock_speed(struct drm_device *dev)
7460{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007461 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07007462}
7463
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007464static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07007465{
David Weinehall52a05c32016-08-22 13:32:44 +03007466 struct pci_dev *pdev = dev->pdev;
Jesse Barnese70236a2009-09-21 10:42:27 -07007467 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007468
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03007469 /*
7470 * 852GM/852GMV only supports 133 MHz and the HPLLCC
7471 * encoding is different :(
7472 * FIXME is this the right way to detect 852GM/852GMV?
7473 */
David Weinehall52a05c32016-08-22 13:32:44 +03007474 if (pdev->revision == 0x1)
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03007475 return 133333;
7476
David Weinehall52a05c32016-08-22 13:32:44 +03007477 pci_bus_read_config_word(pdev->bus,
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007478 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
7479
Jesse Barnese70236a2009-09-21 10:42:27 -07007480 /* Assume that the hardware is in the high speed state. This
7481 * should be the default.
7482 */
7483 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
7484 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007485 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07007486 case GC_CLOCK_100_200:
7487 return 200000;
7488 case GC_CLOCK_166_250:
7489 return 250000;
7490 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007491 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007492 case GC_CLOCK_133_266:
7493 case GC_CLOCK_133_266_2:
7494 case GC_CLOCK_166_266:
7495 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07007496 }
7497
7498 /* Shouldn't happen */
7499 return 0;
7500}
7501
7502static int i830_get_display_clock_speed(struct drm_device *dev)
7503{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007504 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08007505}
7506
Ville Syrjälä34edce22015-05-22 11:22:33 +03007507static unsigned int intel_hpll_vco(struct drm_device *dev)
7508{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007509 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007510 static const unsigned int blb_vco[8] = {
7511 [0] = 3200000,
7512 [1] = 4000000,
7513 [2] = 5333333,
7514 [3] = 4800000,
7515 [4] = 6400000,
7516 };
7517 static const unsigned int pnv_vco[8] = {
7518 [0] = 3200000,
7519 [1] = 4000000,
7520 [2] = 5333333,
7521 [3] = 4800000,
7522 [4] = 2666667,
7523 };
7524 static const unsigned int cl_vco[8] = {
7525 [0] = 3200000,
7526 [1] = 4000000,
7527 [2] = 5333333,
7528 [3] = 6400000,
7529 [4] = 3333333,
7530 [5] = 3566667,
7531 [6] = 4266667,
7532 };
7533 static const unsigned int elk_vco[8] = {
7534 [0] = 3200000,
7535 [1] = 4000000,
7536 [2] = 5333333,
7537 [3] = 4800000,
7538 };
7539 static const unsigned int ctg_vco[8] = {
7540 [0] = 3200000,
7541 [1] = 4000000,
7542 [2] = 5333333,
7543 [3] = 6400000,
7544 [4] = 2666667,
7545 [5] = 4266667,
7546 };
7547 const unsigned int *vco_table;
7548 unsigned int vco;
7549 uint8_t tmp = 0;
7550
7551 /* FIXME other chipsets? */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007552 if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +03007553 vco_table = ctg_vco;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007554 else if (IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +03007555 vco_table = elk_vco;
7556 else if (IS_CRESTLINE(dev))
7557 vco_table = cl_vco;
7558 else if (IS_PINEVIEW(dev))
7559 vco_table = pnv_vco;
7560 else if (IS_G33(dev))
7561 vco_table = blb_vco;
7562 else
7563 return 0;
7564
7565 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7566
7567 vco = vco_table[tmp & 0x7];
7568 if (vco == 0)
7569 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7570 else
7571 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7572
7573 return vco;
7574}
7575
7576static int gm45_get_display_clock_speed(struct drm_device *dev)
7577{
David Weinehall52a05c32016-08-22 13:32:44 +03007578 struct pci_dev *pdev = dev->pdev;
Ville Syrjälä34edce22015-05-22 11:22:33 +03007579 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7580 uint16_t tmp = 0;
7581
David Weinehall52a05c32016-08-22 13:32:44 +03007582 pci_read_config_word(pdev, GCFGC, &tmp);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007583
7584 cdclk_sel = (tmp >> 12) & 0x1;
7585
7586 switch (vco) {
7587 case 2666667:
7588 case 4000000:
7589 case 5333333:
7590 return cdclk_sel ? 333333 : 222222;
7591 case 3200000:
7592 return cdclk_sel ? 320000 : 228571;
7593 default:
7594 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7595 return 222222;
7596 }
7597}
7598
7599static int i965gm_get_display_clock_speed(struct drm_device *dev)
7600{
David Weinehall52a05c32016-08-22 13:32:44 +03007601 struct pci_dev *pdev = dev->pdev;
Ville Syrjälä34edce22015-05-22 11:22:33 +03007602 static const uint8_t div_3200[] = { 16, 10, 8 };
7603 static const uint8_t div_4000[] = { 20, 12, 10 };
7604 static const uint8_t div_5333[] = { 24, 16, 14 };
7605 const uint8_t *div_table;
7606 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7607 uint16_t tmp = 0;
7608
David Weinehall52a05c32016-08-22 13:32:44 +03007609 pci_read_config_word(pdev, GCFGC, &tmp);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007610
7611 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7612
7613 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7614 goto fail;
7615
7616 switch (vco) {
7617 case 3200000:
7618 div_table = div_3200;
7619 break;
7620 case 4000000:
7621 div_table = div_4000;
7622 break;
7623 case 5333333:
7624 div_table = div_5333;
7625 break;
7626 default:
7627 goto fail;
7628 }
7629
7630 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7631
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007632fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007633 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7634 return 200000;
7635}
7636
7637static int g33_get_display_clock_speed(struct drm_device *dev)
7638{
David Weinehall52a05c32016-08-22 13:32:44 +03007639 struct pci_dev *pdev = dev->pdev;
Ville Syrjälä34edce22015-05-22 11:22:33 +03007640 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7641 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7642 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7643 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7644 const uint8_t *div_table;
7645 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7646 uint16_t tmp = 0;
7647
David Weinehall52a05c32016-08-22 13:32:44 +03007648 pci_read_config_word(pdev, GCFGC, &tmp);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007649
7650 cdclk_sel = (tmp >> 4) & 0x7;
7651
7652 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7653 goto fail;
7654
7655 switch (vco) {
7656 case 3200000:
7657 div_table = div_3200;
7658 break;
7659 case 4000000:
7660 div_table = div_4000;
7661 break;
7662 case 4800000:
7663 div_table = div_4800;
7664 break;
7665 case 5333333:
7666 div_table = div_5333;
7667 break;
7668 default:
7669 goto fail;
7670 }
7671
7672 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7673
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007674fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007675 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7676 return 190476;
7677}
7678
Zhenyu Wang2c072452009-06-05 15:38:42 +08007679static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007680intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007681{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007682 while (*num > DATA_LINK_M_N_MASK ||
7683 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007684 *num >>= 1;
7685 *den >>= 1;
7686 }
7687}
7688
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007689static void compute_m_n(unsigned int m, unsigned int n,
7690 uint32_t *ret_m, uint32_t *ret_n)
7691{
7692 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7693 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7694 intel_reduce_m_n_ratio(ret_m, ret_n);
7695}
7696
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007697void
7698intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7699 int pixel_clock, int link_clock,
7700 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007701{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007702 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007703
7704 compute_m_n(bits_per_pixel * pixel_clock,
7705 link_clock * nlanes * 8,
7706 &m_n->gmch_m, &m_n->gmch_n);
7707
7708 compute_m_n(pixel_clock, link_clock,
7709 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007710}
7711
Chris Wilsona7615032011-01-12 17:04:08 +00007712static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7713{
Jani Nikulad330a952014-01-21 11:24:25 +02007714 if (i915.panel_use_ssc >= 0)
7715 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007716 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007717 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007718}
7719
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007720static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007721{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007722 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007723}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007724
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007725static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7726{
7727 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007728}
7729
Daniel Vetterf47709a2013-03-28 10:42:02 +01007730static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007731 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007732 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08007733{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007734 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007735 u32 fp, fp2 = 0;
7736
7737 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007738 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007739 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007740 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007741 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007742 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007743 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007744 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007745 }
7746
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007747 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007748
Daniel Vetterf47709a2013-03-28 10:42:02 +01007749 crtc->lowfreq_avail = false;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007750 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007751 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007752 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007753 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007754 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007755 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007756 }
7757}
7758
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007759static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7760 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007761{
7762 u32 reg_val;
7763
7764 /*
7765 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7766 * and set it to a reasonable value instead.
7767 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007768 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007769 reg_val &= 0xffffff00;
7770 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007771 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007772
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007773 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007774 reg_val &= 0x8cffffff;
7775 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007776 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007777
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007778 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007779 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007780 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007781
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007782 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007783 reg_val &= 0x00ffffff;
7784 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007785 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007786}
7787
Daniel Vetterb5518422013-05-03 11:49:48 +02007788static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7789 struct intel_link_m_n *m_n)
7790{
7791 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007792 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007793 int pipe = crtc->pipe;
7794
Daniel Vettere3b95f12013-05-03 11:49:49 +02007795 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7796 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7797 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7798 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007799}
7800
7801static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007802 struct intel_link_m_n *m_n,
7803 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007804{
7805 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007806 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007807 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007808 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007809
7810 if (INTEL_INFO(dev)->gen >= 5) {
7811 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7812 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7813 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7814 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007815 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7816 * for gen < 8) and if DRRS is supported (to make sure the
7817 * registers are not unnecessarily accessed).
7818 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007819 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
7820 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007821 I915_WRITE(PIPE_DATA_M2(transcoder),
7822 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7823 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7824 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7825 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7826 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007827 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007828 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7829 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7830 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7831 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007832 }
7833}
7834
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307835void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007836{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307837 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7838
7839 if (m_n == M1_N1) {
7840 dp_m_n = &crtc->config->dp_m_n;
7841 dp_m2_n2 = &crtc->config->dp_m2_n2;
7842 } else if (m_n == M2_N2) {
7843
7844 /*
7845 * M2_N2 registers are not supported. Hence m2_n2 divider value
7846 * needs to be programmed into M1_N1.
7847 */
7848 dp_m_n = &crtc->config->dp_m2_n2;
7849 } else {
7850 DRM_ERROR("Unsupported divider value\n");
7851 return;
7852 }
7853
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007854 if (crtc->config->has_pch_encoder)
7855 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007856 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307857 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007858}
7859
Daniel Vetter251ac862015-06-18 10:30:24 +02007860static void vlv_compute_dpll(struct intel_crtc *crtc,
7861 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007862{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007863 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007864 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007865 if (crtc->pipe != PIPE_A)
7866 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007867
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007868 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007869 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007870 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7871 DPLL_EXT_BUFFER_ENABLE_VLV;
7872
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007873 pipe_config->dpll_hw_state.dpll_md =
7874 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7875}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007876
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007877static void chv_compute_dpll(struct intel_crtc *crtc,
7878 struct intel_crtc_state *pipe_config)
7879{
7880 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007881 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007882 if (crtc->pipe != PIPE_A)
7883 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7884
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007885 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007886 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007887 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7888
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007889 pipe_config->dpll_hw_state.dpll_md =
7890 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007891}
7892
Ville Syrjäläd288f652014-10-28 13:20:22 +02007893static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007894 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007895{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007896 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007897 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007898 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007899 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007900 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007901 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007902
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007903 /* Enable Refclk */
7904 I915_WRITE(DPLL(pipe),
7905 pipe_config->dpll_hw_state.dpll &
7906 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7907
7908 /* No need to actually set up the DPLL with DSI */
7909 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7910 return;
7911
Ville Syrjäläa5805162015-05-26 20:42:30 +03007912 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007913
Ville Syrjäläd288f652014-10-28 13:20:22 +02007914 bestn = pipe_config->dpll.n;
7915 bestm1 = pipe_config->dpll.m1;
7916 bestm2 = pipe_config->dpll.m2;
7917 bestp1 = pipe_config->dpll.p1;
7918 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007919
Jesse Barnes89b667f2013-04-18 14:51:36 -07007920 /* See eDP HDMI DPIO driver vbios notes doc */
7921
7922 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007923 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007924 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007925
7926 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007927 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007928
7929 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007930 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007931 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007932 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007933
7934 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007935 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007936
7937 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007938 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7939 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7940 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007941 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007942
7943 /*
7944 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7945 * but we don't support that).
7946 * Note: don't use the DAC post divider as it seems unstable.
7947 */
7948 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007949 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007950
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007951 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007952 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007953
Jesse Barnes89b667f2013-04-18 14:51:36 -07007954 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007955 if (pipe_config->port_clock == 162000 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007956 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7957 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007958 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007959 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007960 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007961 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007962 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007963
Ville Syrjälä37a56502016-06-22 21:57:04 +03007964 if (intel_crtc_has_dp_encoder(pipe_config)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007965 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007966 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007967 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007968 0x0df40000);
7969 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007970 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007971 0x0df70000);
7972 } else { /* HDMI or VGA */
7973 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007974 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007975 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007976 0x0df70000);
7977 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007978 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007979 0x0df40000);
7980 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007981
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007982 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007983 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ville Syrjälä2210ce72016-06-22 21:57:05 +03007984 if (intel_crtc_has_dp_encoder(crtc->config))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007985 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007986 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007987
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007988 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007989 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007990}
7991
Ville Syrjäläd288f652014-10-28 13:20:22 +02007992static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007993 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007994{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007995 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007996 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007997 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007998 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307999 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008000 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05308001 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308002 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008003
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03008004 /* Enable Refclk and SSC */
8005 I915_WRITE(DPLL(pipe),
8006 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
8007
8008 /* No need to actually set up the DPLL with DSI */
8009 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8010 return;
8011
Ville Syrjäläd288f652014-10-28 13:20:22 +02008012 bestn = pipe_config->dpll.n;
8013 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
8014 bestm1 = pipe_config->dpll.m1;
8015 bestm2 = pipe_config->dpll.m2 >> 22;
8016 bestp1 = pipe_config->dpll.p1;
8017 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308018 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05308019 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308020 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008021
Ville Syrjäläa5805162015-05-26 20:42:30 +03008022 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008023
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008024 /* p1 and p2 divider */
8025 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
8026 5 << DPIO_CHV_S1_DIV_SHIFT |
8027 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
8028 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
8029 1 << DPIO_CHV_K_DIV_SHIFT);
8030
8031 /* Feedback post-divider - m2 */
8032 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
8033
8034 /* Feedback refclk divider - n and m1 */
8035 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
8036 DPIO_CHV_M1_DIV_BY_2 |
8037 1 << DPIO_CHV_N_DIV_SHIFT);
8038
8039 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03008040 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008041
8042 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05308043 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8044 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
8045 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
8046 if (bestm2_frac)
8047 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
8048 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008049
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05308050 /* Program digital lock detect threshold */
8051 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
8052 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
8053 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
8054 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
8055 if (!bestm2_frac)
8056 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
8057 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
8058
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008059 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308060 if (vco == 5400000) {
8061 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
8062 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
8063 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
8064 tribuf_calcntr = 0x9;
8065 } else if (vco <= 6200000) {
8066 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
8067 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
8068 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8069 tribuf_calcntr = 0x9;
8070 } else if (vco <= 6480000) {
8071 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8072 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8073 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8074 tribuf_calcntr = 0x8;
8075 } else {
8076 /* Not supported. Apply the same limits as in the max case */
8077 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8078 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8079 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8080 tribuf_calcntr = 0;
8081 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008082 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
8083
Ville Syrjälä968040b2015-03-11 22:52:08 +02008084 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308085 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
8086 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
8087 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
8088
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008089 /* AFC Recal */
8090 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
8091 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
8092 DPIO_AFC_RECAL);
8093
Ville Syrjäläa5805162015-05-26 20:42:30 +03008094 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008095}
8096
Ville Syrjäläd288f652014-10-28 13:20:22 +02008097/**
8098 * vlv_force_pll_on - forcibly enable just the PLL
8099 * @dev_priv: i915 private structure
8100 * @pipe: pipe PLL to enable
8101 * @dpll: PLL configuration
8102 *
8103 * Enable the PLL for @pipe using the supplied @dpll config. To be used
8104 * in cases where we need the PLL enabled even when @pipe is not going to
8105 * be enabled.
8106 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02008107int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008108 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02008109{
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02008110 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008111 struct intel_crtc_state *pipe_config;
8112
8113 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8114 if (!pipe_config)
8115 return -ENOMEM;
8116
8117 pipe_config->base.crtc = &crtc->base;
8118 pipe_config->pixel_multiplier = 1;
8119 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02008120
Ville Syrjälä30ad9812016-10-31 22:37:07 +02008121 if (IS_CHERRYVIEW(dev_priv)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008122 chv_compute_dpll(crtc, pipe_config);
8123 chv_prepare_pll(crtc, pipe_config);
8124 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02008125 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008126 vlv_compute_dpll(crtc, pipe_config);
8127 vlv_prepare_pll(crtc, pipe_config);
8128 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02008129 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008130
8131 kfree(pipe_config);
8132
8133 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02008134}
8135
8136/**
8137 * vlv_force_pll_off - forcibly disable just the PLL
8138 * @dev_priv: i915 private structure
8139 * @pipe: pipe PLL to disable
8140 *
8141 * Disable the PLL for @pipe. To be used in cases where we need
8142 * the PLL enabled even when @pipe is not going to be enabled.
8143 */
Ville Syrjälä30ad9812016-10-31 22:37:07 +02008144void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
Ville Syrjäläd288f652014-10-28 13:20:22 +02008145{
Ville Syrjälä30ad9812016-10-31 22:37:07 +02008146 if (IS_CHERRYVIEW(dev_priv))
8147 chv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02008148 else
Ville Syrjälä30ad9812016-10-31 22:37:07 +02008149 vlv_disable_pll(dev_priv, pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02008150}
8151
Daniel Vetter251ac862015-06-18 10:30:24 +02008152static void i9xx_compute_dpll(struct intel_crtc *crtc,
8153 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008154 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008155{
Daniel Vetterf47709a2013-03-28 10:42:02 +01008156 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008157 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008158 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008159 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008160
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008161 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05308162
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008163 dpll = DPLL_VGA_MODE_DIS;
8164
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008165 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008166 dpll |= DPLLB_MODE_LVDS;
8167 else
8168 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01008169
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008170 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) || IS_G33(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008171 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02008172 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008173 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02008174
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008175 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8176 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008177 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008178
Ville Syrjälä37a56502016-06-22 21:57:04 +03008179 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008180 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008181
8182 /* compute bitmask from p1 value */
8183 if (IS_PINEVIEW(dev))
8184 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8185 else {
8186 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01008187 if (IS_G4X(dev_priv) && reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008188 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8189 }
8190 switch (clock->p2) {
8191 case 5:
8192 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8193 break;
8194 case 7:
8195 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8196 break;
8197 case 10:
8198 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8199 break;
8200 case 14:
8201 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8202 break;
8203 }
8204 if (INTEL_INFO(dev)->gen >= 4)
8205 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8206
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008207 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008208 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008209 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008210 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008211 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8212 else
8213 dpll |= PLL_REF_INPUT_DREFCLK;
8214
8215 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008216 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008217
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008218 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008219 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008220 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008221 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008222 }
8223}
8224
Daniel Vetter251ac862015-06-18 10:30:24 +02008225static void i8xx_compute_dpll(struct intel_crtc *crtc,
8226 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008227 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008228{
Daniel Vetterf47709a2013-03-28 10:42:02 +01008229 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008230 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008231 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008232 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008233
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008234 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05308235
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008236 dpll = DPLL_VGA_MODE_DIS;
8237
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008238 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008239 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8240 } else {
8241 if (clock->p1 == 2)
8242 dpll |= PLL_P1_DIVIDE_BY_TWO;
8243 else
8244 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8245 if (clock->p2 == 4)
8246 dpll |= PLL_P2_DIVIDE_BY_4;
8247 }
8248
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008249 if (!IS_I830(dev_priv) &&
8250 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008251 dpll |= DPLL_DVO_2X_MODE;
8252
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008253 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008254 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008255 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8256 else
8257 dpll |= PLL_REF_INPUT_DREFCLK;
8258
8259 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008260 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008261}
8262
Daniel Vetter8a654f32013-06-01 17:16:22 +02008263static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008264{
8265 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008266 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008267 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008268 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03008269 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02008270 uint32_t crtc_vtotal, crtc_vblank_end;
8271 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02008272
8273 /* We need to be careful not to changed the adjusted mode, for otherwise
8274 * the hw state checker will get angry at the mismatch. */
8275 crtc_vtotal = adjusted_mode->crtc_vtotal;
8276 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008277
Ville Syrjälä609aeac2014-03-28 23:29:30 +02008278 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008279 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02008280 crtc_vtotal -= 1;
8281 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02008282
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008283 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02008284 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
8285 else
8286 vsyncshift = adjusted_mode->crtc_hsync_start -
8287 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02008288 if (vsyncshift < 0)
8289 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008290 }
8291
8292 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008293 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008294
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008295 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008296 (adjusted_mode->crtc_hdisplay - 1) |
8297 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008298 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008299 (adjusted_mode->crtc_hblank_start - 1) |
8300 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008301 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008302 (adjusted_mode->crtc_hsync_start - 1) |
8303 ((adjusted_mode->crtc_hsync_end - 1) << 16));
8304
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008305 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008306 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02008307 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008308 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008309 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02008310 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008311 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008312 (adjusted_mode->crtc_vsync_start - 1) |
8313 ((adjusted_mode->crtc_vsync_end - 1) << 16));
8314
Paulo Zanonib5e508d2012-10-24 11:34:43 -02008315 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
8316 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
8317 * documented on the DDI_FUNC_CTL register description, EDP Input Select
8318 * bits. */
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01008319 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
Paulo Zanonib5e508d2012-10-24 11:34:43 -02008320 (pipe == PIPE_B || pipe == PIPE_C))
8321 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
8322
Jani Nikulabc58be62016-03-18 17:05:39 +02008323}
8324
8325static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
8326{
8327 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008328 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02008329 enum pipe pipe = intel_crtc->pipe;
8330
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008331 /* pipesrc controls the size that is scaled from, which should
8332 * always be the user's requested size.
8333 */
8334 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008335 ((intel_crtc->config->pipe_src_w - 1) << 16) |
8336 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008337}
8338
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008339static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008340 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008341{
8342 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008343 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008344 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
8345 uint32_t tmp;
8346
8347 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008348 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
8349 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008350 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008351 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
8352 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008353 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008354 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
8355 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008356
8357 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008358 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
8359 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008360 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008361 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
8362 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008363 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008364 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
8365 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008366
8367 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008368 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
8369 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
8370 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008371 }
Jani Nikulabc58be62016-03-18 17:05:39 +02008372}
8373
8374static void intel_get_pipe_src_size(struct intel_crtc *crtc,
8375 struct intel_crtc_state *pipe_config)
8376{
8377 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008378 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02008379 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008380
8381 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008382 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
8383 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
8384
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008385 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
8386 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008387}
8388
Daniel Vetterf6a83282014-02-11 15:28:57 -08008389void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008390 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03008391{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008392 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
8393 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
8394 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
8395 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03008396
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008397 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
8398 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
8399 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
8400 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03008401
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008402 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02008403 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03008404
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008405 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
8406 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02008407
8408 mode->hsync = drm_mode_hsync(mode);
8409 mode->vrefresh = drm_mode_vrefresh(mode);
8410 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03008411}
8412
Daniel Vetter84b046f2013-02-19 18:48:54 +01008413static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
8414{
8415 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008416 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter84b046f2013-02-19 18:48:54 +01008417 uint32_t pipeconf;
8418
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02008419 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01008420
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03008421 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
8422 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8423 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02008424
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008425 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03008426 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01008427
Daniel Vetterff9ce462013-04-24 14:57:17 +02008428 /* only g4x and later have fancy bpc/dither controls */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01008429 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8430 IS_CHERRYVIEW(dev_priv)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02008431 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008432 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02008433 pipeconf |= PIPECONF_DITHER_EN |
8434 PIPECONF_DITHER_TYPE_SP;
8435
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008436 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02008437 case 18:
8438 pipeconf |= PIPECONF_6BPC;
8439 break;
8440 case 24:
8441 pipeconf |= PIPECONF_8BPC;
8442 break;
8443 case 30:
8444 pipeconf |= PIPECONF_10BPC;
8445 break;
8446 default:
8447 /* Case prevented by intel_choose_pipe_bpp_dither. */
8448 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01008449 }
8450 }
8451
8452 if (HAS_PIPE_CXSR(dev)) {
8453 if (intel_crtc->lowfreq_avail) {
8454 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
8455 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
8456 } else {
8457 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01008458 }
8459 }
8460
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008461 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02008462 if (INTEL_INFO(dev)->gen < 4 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008463 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02008464 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
8465 else
8466 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
8467 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01008468 pipeconf |= PIPECONF_PROGRESSIVE;
8469
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008470 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08008471 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02008472 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03008473
Daniel Vetter84b046f2013-02-19 18:48:54 +01008474 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
8475 POSTING_READ(PIPECONF(intel_crtc->pipe));
8476}
8477
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008478static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
8479 struct intel_crtc_state *crtc_state)
8480{
8481 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008482 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008483 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008484 int refclk = 48000;
8485
8486 memset(&crtc_state->dpll_hw_state, 0,
8487 sizeof(crtc_state->dpll_hw_state));
8488
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008489 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008490 if (intel_panel_use_ssc(dev_priv)) {
8491 refclk = dev_priv->vbt.lvds_ssc_freq;
8492 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8493 }
8494
8495 limit = &intel_limits_i8xx_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008496 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008497 limit = &intel_limits_i8xx_dvo;
8498 } else {
8499 limit = &intel_limits_i8xx_dac;
8500 }
8501
8502 if (!crtc_state->clock_set &&
8503 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8504 refclk, NULL, &crtc_state->dpll)) {
8505 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8506 return -EINVAL;
8507 }
8508
8509 i8xx_compute_dpll(crtc, crtc_state, NULL);
8510
8511 return 0;
8512}
8513
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008514static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
8515 struct intel_crtc_state *crtc_state)
8516{
8517 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008518 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008519 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008520 int refclk = 96000;
8521
8522 memset(&crtc_state->dpll_hw_state, 0,
8523 sizeof(crtc_state->dpll_hw_state));
8524
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008525 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008526 if (intel_panel_use_ssc(dev_priv)) {
8527 refclk = dev_priv->vbt.lvds_ssc_freq;
8528 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8529 }
8530
8531 if (intel_is_dual_link_lvds(dev))
8532 limit = &intel_limits_g4x_dual_channel_lvds;
8533 else
8534 limit = &intel_limits_g4x_single_channel_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008535 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
8536 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008537 limit = &intel_limits_g4x_hdmi;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008538 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008539 limit = &intel_limits_g4x_sdvo;
8540 } else {
8541 /* The option is for other outputs */
8542 limit = &intel_limits_i9xx_sdvo;
8543 }
8544
8545 if (!crtc_state->clock_set &&
8546 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8547 refclk, NULL, &crtc_state->dpll)) {
8548 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8549 return -EINVAL;
8550 }
8551
8552 i9xx_compute_dpll(crtc, crtc_state, NULL);
8553
8554 return 0;
8555}
8556
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008557static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8558 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008559{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008560 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008561 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008562 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008563 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008564
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008565 memset(&crtc_state->dpll_hw_state, 0,
8566 sizeof(crtc_state->dpll_hw_state));
8567
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008568 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008569 if (intel_panel_use_ssc(dev_priv)) {
8570 refclk = dev_priv->vbt.lvds_ssc_freq;
8571 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8572 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008573
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008574 limit = &intel_limits_pineview_lvds;
8575 } else {
8576 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008577 }
Jani Nikulaf2335332013-09-13 11:03:09 +03008578
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008579 if (!crtc_state->clock_set &&
8580 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8581 refclk, NULL, &crtc_state->dpll)) {
8582 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8583 return -EINVAL;
8584 }
8585
8586 i9xx_compute_dpll(crtc, crtc_state, NULL);
8587
8588 return 0;
8589}
8590
8591static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8592 struct intel_crtc_state *crtc_state)
8593{
8594 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008595 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008596 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008597 int refclk = 96000;
8598
8599 memset(&crtc_state->dpll_hw_state, 0,
8600 sizeof(crtc_state->dpll_hw_state));
8601
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008602 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008603 if (intel_panel_use_ssc(dev_priv)) {
8604 refclk = dev_priv->vbt.lvds_ssc_freq;
8605 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008606 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008607
8608 limit = &intel_limits_i9xx_lvds;
8609 } else {
8610 limit = &intel_limits_i9xx_sdvo;
8611 }
8612
8613 if (!crtc_state->clock_set &&
8614 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8615 refclk, NULL, &crtc_state->dpll)) {
8616 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8617 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008618 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008619
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008620 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07008621
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008622 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008623}
8624
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008625static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8626 struct intel_crtc_state *crtc_state)
8627{
8628 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008629 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008630
8631 memset(&crtc_state->dpll_hw_state, 0,
8632 sizeof(crtc_state->dpll_hw_state));
8633
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008634 if (!crtc_state->clock_set &&
8635 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8636 refclk, NULL, &crtc_state->dpll)) {
8637 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8638 return -EINVAL;
8639 }
8640
8641 chv_compute_dpll(crtc, crtc_state);
8642
8643 return 0;
8644}
8645
8646static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8647 struct intel_crtc_state *crtc_state)
8648{
8649 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008650 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008651
8652 memset(&crtc_state->dpll_hw_state, 0,
8653 sizeof(crtc_state->dpll_hw_state));
8654
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008655 if (!crtc_state->clock_set &&
8656 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8657 refclk, NULL, &crtc_state->dpll)) {
8658 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8659 return -EINVAL;
8660 }
8661
8662 vlv_compute_dpll(crtc, crtc_state);
8663
8664 return 0;
8665}
8666
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008667static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008668 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008669{
8670 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008671 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008672 uint32_t tmp;
8673
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008674 if (INTEL_GEN(dev_priv) <= 3 &&
8675 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008676 return;
8677
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008678 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008679 if (!(tmp & PFIT_ENABLE))
8680 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008681
Daniel Vetter06922822013-07-11 13:35:40 +02008682 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008683 if (INTEL_INFO(dev)->gen < 4) {
8684 if (crtc->pipe != PIPE_B)
8685 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008686 } else {
8687 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8688 return;
8689 }
8690
Daniel Vetter06922822013-07-11 13:35:40 +02008691 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008692 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008693}
8694
Jesse Barnesacbec812013-09-20 11:29:32 -07008695static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008696 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008697{
8698 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008699 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesacbec812013-09-20 11:29:32 -07008700 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008701 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07008702 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008703 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008704
Ville Syrjäläb5219732016-03-15 16:40:01 +02008705 /* In case of DSI, DPLL will not be used */
8706 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308707 return;
8708
Ville Syrjäläa5805162015-05-26 20:42:30 +03008709 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008710 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008711 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008712
8713 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8714 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8715 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8716 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8717 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8718
Imre Deakdccbea32015-06-22 23:35:51 +03008719 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008720}
8721
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008722static void
8723i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8724 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008725{
8726 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008727 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008728 u32 val, base, offset;
8729 int pipe = crtc->pipe, plane = crtc->plane;
8730 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008731 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008732 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008733 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008734
Damien Lespiau42a7b082015-02-05 19:35:13 +00008735 val = I915_READ(DSPCNTR(plane));
8736 if (!(val & DISPLAY_PLANE_ENABLE))
8737 return;
8738
Damien Lespiaud9806c92015-01-21 14:07:19 +00008739 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008740 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008741 DRM_DEBUG_KMS("failed to alloc fb\n");
8742 return;
8743 }
8744
Damien Lespiau1b842c82015-01-21 13:50:54 +00008745 fb = &intel_fb->base;
8746
Daniel Vetter18c52472015-02-10 17:16:09 +00008747 if (INTEL_INFO(dev)->gen >= 4) {
8748 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008749 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008750 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8751 }
8752 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008753
8754 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008755 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008756 fb->pixel_format = fourcc;
8757 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008758
8759 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008760 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008761 offset = I915_READ(DSPTILEOFF(plane));
8762 else
8763 offset = I915_READ(DSPLINOFF(plane));
8764 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8765 } else {
8766 base = I915_READ(DSPADDR(plane));
8767 }
8768 plane_config->base = base;
8769
8770 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008771 fb->width = ((val >> 16) & 0xfff) + 1;
8772 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008773
8774 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008775 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008776
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008777 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008778 fb->pixel_format,
8779 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008780
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008781 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008782
Damien Lespiau2844a922015-01-20 12:51:48 +00008783 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8784 pipe_name(pipe), plane, fb->width, fb->height,
8785 fb->bits_per_pixel, base, fb->pitches[0],
8786 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008787
Damien Lespiau2d140302015-02-05 17:22:18 +00008788 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008789}
8790
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008791static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008792 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008793{
8794 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008795 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008796 int pipe = pipe_config->cpu_transcoder;
8797 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008798 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008799 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008800 int refclk = 100000;
8801
Ville Syrjäläb5219732016-03-15 16:40:01 +02008802 /* In case of DSI, DPLL will not be used */
8803 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8804 return;
8805
Ville Syrjäläa5805162015-05-26 20:42:30 +03008806 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008807 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8808 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8809 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8810 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008811 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008812 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008813
8814 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008815 clock.m2 = (pll_dw0 & 0xff) << 22;
8816 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8817 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008818 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8819 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8820 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8821
Imre Deakdccbea32015-06-22 23:35:51 +03008822 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008823}
8824
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008825static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008826 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008827{
8828 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008829 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02008830 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008831 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008832 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008833
Imre Deak17290502016-02-12 18:55:11 +02008834 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8835 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008836 return false;
8837
Daniel Vettere143a212013-07-04 12:01:15 +02008838 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008839 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008840
Imre Deak17290502016-02-12 18:55:11 +02008841 ret = false;
8842
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008843 tmp = I915_READ(PIPECONF(crtc->pipe));
8844 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008845 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008846
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01008847 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8848 IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008849 switch (tmp & PIPECONF_BPC_MASK) {
8850 case PIPECONF_6BPC:
8851 pipe_config->pipe_bpp = 18;
8852 break;
8853 case PIPECONF_8BPC:
8854 pipe_config->pipe_bpp = 24;
8855 break;
8856 case PIPECONF_10BPC:
8857 pipe_config->pipe_bpp = 30;
8858 break;
8859 default:
8860 break;
8861 }
8862 }
8863
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008864 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08008865 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008866 pipe_config->limited_color_range = true;
8867
Ville Syrjälä282740f2013-09-04 18:30:03 +03008868 if (INTEL_INFO(dev)->gen < 4)
8869 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8870
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008871 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008872 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008873
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008874 i9xx_get_pfit_config(crtc, pipe_config);
8875
Daniel Vetter6c49f242013-06-06 12:45:25 +02008876 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008877 /* No way to read it out on pipes B and C */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008878 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
Ville Syrjäläc2317752016-03-15 16:39:56 +02008879 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8880 else
8881 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008882 pipe_config->pixel_multiplier =
8883 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8884 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008885 pipe_config->dpll_hw_state.dpll_md = tmp;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008886 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
8887 IS_G33(dev_priv)) {
Daniel Vetter6c49f242013-06-06 12:45:25 +02008888 tmp = I915_READ(DPLL(crtc->pipe));
8889 pipe_config->pixel_multiplier =
8890 ((tmp & SDVO_MULTIPLIER_MASK)
8891 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8892 } else {
8893 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8894 * port and will be fixed up in the encoder->get_config
8895 * function. */
8896 pipe_config->pixel_multiplier = 1;
8897 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008898 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008899 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008900 /*
8901 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8902 * on 830. Filter it out here so that we don't
8903 * report errors due to that.
8904 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008905 if (IS_I830(dev_priv))
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008906 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8907
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008908 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8909 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008910 } else {
8911 /* Mask out read-only status bits. */
8912 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8913 DPLL_PORTC_READY_MASK |
8914 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008915 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008916
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008917 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008918 chv_crtc_clock_get(crtc, pipe_config);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01008919 else if (IS_VALLEYVIEW(dev_priv))
Jesse Barnesacbec812013-09-20 11:29:32 -07008920 vlv_crtc_clock_get(crtc, pipe_config);
8921 else
8922 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008923
Ville Syrjälä0f646142015-08-26 19:39:18 +03008924 /*
8925 * Normally the dotclock is filled in by the encoder .get_config()
8926 * but in case the pipe is enabled w/o any ports we need a sane
8927 * default.
8928 */
8929 pipe_config->base.adjusted_mode.crtc_clock =
8930 pipe_config->port_clock / pipe_config->pixel_multiplier;
8931
Imre Deak17290502016-02-12 18:55:11 +02008932 ret = true;
8933
8934out:
8935 intel_display_power_put(dev_priv, power_domain);
8936
8937 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008938}
8939
Paulo Zanonidde86e22012-12-01 12:04:25 -02008940static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008941{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008942 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008943 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04008944 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008945 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008946 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008947 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008948 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008949 bool has_ck505 = false;
8950 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04008951 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008952
8953 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008954 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008955 switch (encoder->type) {
8956 case INTEL_OUTPUT_LVDS:
8957 has_panel = true;
8958 has_lvds = true;
8959 break;
8960 case INTEL_OUTPUT_EDP:
8961 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008962 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008963 has_cpu_edp = true;
8964 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008965 default:
8966 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008967 }
8968 }
8969
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008970 if (HAS_PCH_IBX(dev_priv)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008971 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008972 can_ssc = has_ck505;
8973 } else {
8974 has_ck505 = false;
8975 can_ssc = true;
8976 }
8977
Lyude1c1a24d2016-06-14 11:04:09 -04008978 /* Check if any DPLLs are using the SSC source */
8979 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8980 u32 temp = I915_READ(PCH_DPLL(i));
8981
8982 if (!(temp & DPLL_VCO_ENABLE))
8983 continue;
8984
8985 if ((temp & PLL_REF_INPUT_MASK) ==
8986 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8987 using_ssc_source = true;
8988 break;
8989 }
8990 }
8991
8992 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8993 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008994
8995 /* Ironlake: try to setup display ref clock before DPLL
8996 * enabling. This is only under driver's control after
8997 * PCH B stepping, previous chipset stepping should be
8998 * ignoring this setting.
8999 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009000 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07009001
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009002 /* As we must carefully and slowly disable/enable each source in turn,
9003 * compute the final state we want first and check if we need to
9004 * make any changes at all.
9005 */
9006 final = val;
9007 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07009008 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009009 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07009010 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009011 final |= DREF_NONSPREAD_SOURCE_ENABLE;
9012
Daniel Vetter8c07eb62016-06-09 18:39:07 +02009013 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009014 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02009015 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07009016
Keith Packard199e5d72011-09-22 12:01:57 -07009017 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009018 final |= DREF_SSC_SOURCE_ENABLE;
9019
9020 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9021 final |= DREF_SSC1_ENABLE;
9022
9023 if (has_cpu_edp) {
9024 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9025 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9026 else
9027 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9028 } else
9029 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04009030 } else if (using_ssc_source) {
9031 final |= DREF_SSC_SOURCE_ENABLE;
9032 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009033 }
9034
9035 if (final == val)
9036 return;
9037
9038 /* Always enable nonspread source */
9039 val &= ~DREF_NONSPREAD_SOURCE_MASK;
9040
9041 if (has_ck505)
9042 val |= DREF_NONSPREAD_CK505_ENABLE;
9043 else
9044 val |= DREF_NONSPREAD_SOURCE_ENABLE;
9045
9046 if (has_panel) {
9047 val &= ~DREF_SSC_SOURCE_MASK;
9048 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07009049
Keith Packard199e5d72011-09-22 12:01:57 -07009050 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07009051 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07009052 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009053 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02009054 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009055 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07009056
9057 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009058 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07009059 POSTING_READ(PCH_DREF_CONTROL);
9060 udelay(200);
9061
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009062 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07009063
9064 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07009065 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07009066 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07009067 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009068 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02009069 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009070 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07009071 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009072 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07009073
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009074 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07009075 POSTING_READ(PCH_DREF_CONTROL);
9076 udelay(200);
9077 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04009078 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07009079
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009080 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07009081
9082 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009083 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07009084
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009085 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07009086 POSTING_READ(PCH_DREF_CONTROL);
9087 udelay(200);
9088
Lyude1c1a24d2016-06-14 11:04:09 -04009089 if (!using_ssc_source) {
9090 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07009091
Lyude1c1a24d2016-06-14 11:04:09 -04009092 /* Turn off the SSC source */
9093 val &= ~DREF_SSC_SOURCE_MASK;
9094 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07009095
Lyude1c1a24d2016-06-14 11:04:09 -04009096 /* Turn off SSC1 */
9097 val &= ~DREF_SSC1_ENABLE;
9098
9099 I915_WRITE(PCH_DREF_CONTROL, val);
9100 POSTING_READ(PCH_DREF_CONTROL);
9101 udelay(200);
9102 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07009103 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009104
9105 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07009106}
9107
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009108static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02009109{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009110 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02009111
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009112 tmp = I915_READ(SOUTH_CHICKEN2);
9113 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
9114 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009115
Imre Deakcf3598c2016-06-28 13:37:31 +03009116 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
9117 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009118 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02009119
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009120 tmp = I915_READ(SOUTH_CHICKEN2);
9121 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
9122 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009123
Imre Deakcf3598c2016-06-28 13:37:31 +03009124 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
9125 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009126 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009127}
9128
9129/* WaMPhyProgramming:hsw */
9130static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9131{
9132 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02009133
9134 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9135 tmp &= ~(0xFF << 24);
9136 tmp |= (0x12 << 24);
9137 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9138
Paulo Zanonidde86e22012-12-01 12:04:25 -02009139 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9140 tmp |= (1 << 11);
9141 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9142
9143 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9144 tmp |= (1 << 11);
9145 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9146
Paulo Zanonidde86e22012-12-01 12:04:25 -02009147 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9148 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9149 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9150
9151 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9152 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9153 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9154
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009155 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9156 tmp &= ~(7 << 13);
9157 tmp |= (5 << 13);
9158 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009159
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009160 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9161 tmp &= ~(7 << 13);
9162 tmp |= (5 << 13);
9163 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009164
9165 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9166 tmp &= ~0xFF;
9167 tmp |= 0x1C;
9168 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9169
9170 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9171 tmp &= ~0xFF;
9172 tmp |= 0x1C;
9173 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
9174
9175 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
9176 tmp &= ~(0xFF << 16);
9177 tmp |= (0x1C << 16);
9178 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
9179
9180 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
9181 tmp &= ~(0xFF << 16);
9182 tmp |= (0x1C << 16);
9183 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
9184
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009185 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
9186 tmp |= (1 << 27);
9187 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009188
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009189 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
9190 tmp |= (1 << 27);
9191 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009192
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009193 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
9194 tmp &= ~(0xF << 28);
9195 tmp |= (4 << 28);
9196 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009197
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009198 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
9199 tmp &= ~(0xF << 28);
9200 tmp |= (4 << 28);
9201 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009202}
9203
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009204/* Implements 3 different sequences from BSpec chapter "Display iCLK
9205 * Programming" based on the parameters passed:
9206 * - Sequence to enable CLKOUT_DP
9207 * - Sequence to enable CLKOUT_DP without spread
9208 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
9209 */
9210static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
9211 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009212{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009213 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009214 uint32_t reg, tmp;
9215
9216 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
9217 with_spread = true;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009218 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
9219 with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009220 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009221
Ville Syrjäläa5805162015-05-26 20:42:30 +03009222 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009223
9224 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9225 tmp &= ~SBI_SSCCTL_DISABLE;
9226 tmp |= SBI_SSCCTL_PATHALT;
9227 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9228
9229 udelay(24);
9230
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009231 if (with_spread) {
9232 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9233 tmp &= ~SBI_SSCCTL_PATHALT;
9234 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009235
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009236 if (with_fdi) {
9237 lpt_reset_fdi_mphy(dev_priv);
9238 lpt_program_fdi_mphy(dev_priv);
9239 }
9240 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02009241
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009242 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009243 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9244 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9245 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01009246
Ville Syrjäläa5805162015-05-26 20:42:30 +03009247 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009248}
9249
Paulo Zanoni47701c32013-07-23 11:19:25 -03009250/* Sequence to disable CLKOUT_DP */
9251static void lpt_disable_clkout_dp(struct drm_device *dev)
9252{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009253 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni47701c32013-07-23 11:19:25 -03009254 uint32_t reg, tmp;
9255
Ville Syrjäläa5805162015-05-26 20:42:30 +03009256 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03009257
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009258 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03009259 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9260 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9261 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9262
9263 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9264 if (!(tmp & SBI_SSCCTL_DISABLE)) {
9265 if (!(tmp & SBI_SSCCTL_PATHALT)) {
9266 tmp |= SBI_SSCCTL_PATHALT;
9267 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9268 udelay(32);
9269 }
9270 tmp |= SBI_SSCCTL_DISABLE;
9271 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9272 }
9273
Ville Syrjäläa5805162015-05-26 20:42:30 +03009274 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03009275}
9276
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02009277#define BEND_IDX(steps) ((50 + (steps)) / 5)
9278
9279static const uint16_t sscdivintphase[] = {
9280 [BEND_IDX( 50)] = 0x3B23,
9281 [BEND_IDX( 45)] = 0x3B23,
9282 [BEND_IDX( 40)] = 0x3C23,
9283 [BEND_IDX( 35)] = 0x3C23,
9284 [BEND_IDX( 30)] = 0x3D23,
9285 [BEND_IDX( 25)] = 0x3D23,
9286 [BEND_IDX( 20)] = 0x3E23,
9287 [BEND_IDX( 15)] = 0x3E23,
9288 [BEND_IDX( 10)] = 0x3F23,
9289 [BEND_IDX( 5)] = 0x3F23,
9290 [BEND_IDX( 0)] = 0x0025,
9291 [BEND_IDX( -5)] = 0x0025,
9292 [BEND_IDX(-10)] = 0x0125,
9293 [BEND_IDX(-15)] = 0x0125,
9294 [BEND_IDX(-20)] = 0x0225,
9295 [BEND_IDX(-25)] = 0x0225,
9296 [BEND_IDX(-30)] = 0x0325,
9297 [BEND_IDX(-35)] = 0x0325,
9298 [BEND_IDX(-40)] = 0x0425,
9299 [BEND_IDX(-45)] = 0x0425,
9300 [BEND_IDX(-50)] = 0x0525,
9301};
9302
9303/*
9304 * Bend CLKOUT_DP
9305 * steps -50 to 50 inclusive, in steps of 5
9306 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
9307 * change in clock period = -(steps / 10) * 5.787 ps
9308 */
9309static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
9310{
9311 uint32_t tmp;
9312 int idx = BEND_IDX(steps);
9313
9314 if (WARN_ON(steps % 5 != 0))
9315 return;
9316
9317 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
9318 return;
9319
9320 mutex_lock(&dev_priv->sb_lock);
9321
9322 if (steps % 10 != 0)
9323 tmp = 0xAAAAAAAB;
9324 else
9325 tmp = 0x00000000;
9326 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
9327
9328 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
9329 tmp &= 0xffff0000;
9330 tmp |= sscdivintphase[idx];
9331 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
9332
9333 mutex_unlock(&dev_priv->sb_lock);
9334}
9335
9336#undef BEND_IDX
9337
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009338static void lpt_init_pch_refclk(struct drm_device *dev)
9339{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009340 struct intel_encoder *encoder;
9341 bool has_vga = false;
9342
Damien Lespiaub2784e12014-08-05 11:29:37 +01009343 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009344 switch (encoder->type) {
9345 case INTEL_OUTPUT_ANALOG:
9346 has_vga = true;
9347 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02009348 default:
9349 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009350 }
9351 }
9352
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02009353 if (has_vga) {
9354 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03009355 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02009356 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03009357 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02009358 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009359}
9360
Paulo Zanonidde86e22012-12-01 12:04:25 -02009361/*
9362 * Initialize reference clocks when the driver loads
9363 */
9364void intel_init_pch_refclk(struct drm_device *dev)
9365{
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009366 struct drm_i915_private *dev_priv = to_i915(dev);
9367
9368 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
Paulo Zanonidde86e22012-12-01 12:04:25 -02009369 ironlake_init_pch_refclk(dev);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009370 else if (HAS_PCH_LPT(dev_priv))
Paulo Zanonidde86e22012-12-01 12:04:25 -02009371 lpt_init_pch_refclk(dev);
9372}
9373
Daniel Vetter6ff93602013-04-19 11:24:36 +02009374static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03009375{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009376 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanonic8203562012-09-12 10:06:29 -03009377 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9378 int pipe = intel_crtc->pipe;
9379 uint32_t val;
9380
Daniel Vetter78114072013-06-13 00:54:57 +02009381 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03009382
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009383 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03009384 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01009385 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009386 break;
9387 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01009388 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009389 break;
9390 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01009391 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009392 break;
9393 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01009394 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009395 break;
9396 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03009397 /* Case prevented by intel_choose_pipe_bpp_dither. */
9398 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03009399 }
9400
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009401 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03009402 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9403
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009404 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03009405 val |= PIPECONF_INTERLACED_ILK;
9406 else
9407 val |= PIPECONF_PROGRESSIVE;
9408
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009409 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02009410 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02009411
Paulo Zanonic8203562012-09-12 10:06:29 -03009412 I915_WRITE(PIPECONF(pipe), val);
9413 POSTING_READ(PIPECONF(pipe));
9414}
9415
Daniel Vetter6ff93602013-04-19 11:24:36 +02009416static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009417{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009418 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009419 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009420 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02009421 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009422
Jani Nikula391bf042016-03-18 17:05:40 +02009423 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009424 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9425
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009426 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009427 val |= PIPECONF_INTERLACED_ILK;
9428 else
9429 val |= PIPECONF_PROGRESSIVE;
9430
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009431 I915_WRITE(PIPECONF(cpu_transcoder), val);
9432 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02009433}
9434
Jani Nikula391bf042016-03-18 17:05:40 +02009435static void haswell_set_pipemisc(struct drm_crtc *crtc)
9436{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009437 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Jani Nikula391bf042016-03-18 17:05:40 +02009438 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9439
9440 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
9441 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009442
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009443 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009444 case 18:
9445 val |= PIPEMISC_DITHER_6_BPC;
9446 break;
9447 case 24:
9448 val |= PIPEMISC_DITHER_8_BPC;
9449 break;
9450 case 30:
9451 val |= PIPEMISC_DITHER_10_BPC;
9452 break;
9453 case 36:
9454 val |= PIPEMISC_DITHER_12_BPC;
9455 break;
9456 default:
9457 /* Case prevented by pipe_config_set_bpp. */
9458 BUG();
9459 }
9460
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009461 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009462 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
9463
Jani Nikula391bf042016-03-18 17:05:40 +02009464 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009465 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009466}
9467
Paulo Zanonid4b19312012-11-29 11:29:32 -02009468int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
9469{
9470 /*
9471 * Account for spread spectrum to avoid
9472 * oversubscribing the link. Max center spread
9473 * is 2.5%; use 5% for safety's sake.
9474 */
9475 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02009476 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02009477}
9478
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009479static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02009480{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009481 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03009482}
9483
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009484static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
9485 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03009486 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009487{
9488 struct drm_crtc *crtc = &intel_crtc->base;
9489 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009490 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009491 u32 dpll, fp, fp2;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009492 int factor;
Jesse Barnes79e53942008-11-07 14:24:08 -08009493
Chris Wilsonc1858122010-12-03 21:35:48 +00009494 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07009495 factor = 21;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009496 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Eric Anholt8febb292011-03-30 13:01:07 -07009497 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009498 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009499 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07009500 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009501 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07009502 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00009503
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009504 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00009505
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009506 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
9507 fp |= FP_CB_TUNE;
9508
9509 if (reduced_clock) {
9510 fp2 = i9xx_dpll_compute_fp(reduced_clock);
9511
9512 if (reduced_clock->m < factor * reduced_clock->n)
9513 fp2 |= FP_CB_TUNE;
9514 } else {
9515 fp2 = fp;
9516 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02009517
Chris Wilson5eddb702010-09-11 13:48:45 +01009518 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08009519
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009520 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Eric Anholta07d6782011-03-30 13:01:08 -07009521 dpll |= DPLLB_MODE_LVDS;
9522 else
9523 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009524
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009525 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02009526 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009527
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009528 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
9529 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02009530 dpll |= DPLL_SDVO_HIGH_SPEED;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009531
Ville Syrjälä37a56502016-06-22 21:57:04 +03009532 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02009533 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08009534
Ville Syrjälä7d7f8632016-09-26 11:30:46 +03009535 /*
9536 * The high speed IO clock is only really required for
9537 * SDVO/HDMI/DP, but we also enable it for CRT to make it
9538 * possible to share the DPLL between CRT and HDMI. Enabling
9539 * the clock needlessly does no real harm, except use up a
9540 * bit of power potentially.
9541 *
9542 * We'll limit this to IVB with 3 pipes, since it has only two
9543 * DPLLs and so DPLL sharing is the only way to get three pipes
9544 * driving PCH ports at the same time. On SNB we could do this,
9545 * and potentially avoid enabling the second DPLL, but it's not
9546 * clear if it''s a win or loss power wise. No point in doing
9547 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
9548 */
9549 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
9550 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
9551 dpll |= DPLL_SDVO_HIGH_SPEED;
9552
Eric Anholta07d6782011-03-30 13:01:08 -07009553 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009554 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009555 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009556 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009557
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009558 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07009559 case 5:
9560 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9561 break;
9562 case 7:
9563 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9564 break;
9565 case 10:
9566 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9567 break;
9568 case 14:
9569 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9570 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009571 }
9572
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009573 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9574 intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05009575 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08009576 else
9577 dpll |= PLL_REF_INPUT_DREFCLK;
9578
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009579 dpll |= DPLL_VCO_ENABLE;
9580
9581 crtc_state->dpll_hw_state.dpll = dpll;
9582 crtc_state->dpll_hw_state.fp0 = fp;
9583 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009584}
9585
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009586static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9587 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08009588{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009589 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009590 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03009591 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02009592 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009593 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03009594 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009595 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08009596
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03009597 memset(&crtc_state->dpll_hw_state, 0,
9598 sizeof(crtc_state->dpll_hw_state));
9599
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009600 crtc->lowfreq_avail = false;
9601
9602 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9603 if (!crtc_state->has_pch_encoder)
9604 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009605
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03009606 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009607 if (intel_panel_use_ssc(dev_priv)) {
9608 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9609 dev_priv->vbt.lvds_ssc_freq);
9610 refclk = dev_priv->vbt.lvds_ssc_freq;
9611 }
9612
9613 if (intel_is_dual_link_lvds(dev)) {
9614 if (refclk == 100000)
9615 limit = &intel_limits_ironlake_dual_lvds_100m;
9616 else
9617 limit = &intel_limits_ironlake_dual_lvds;
9618 } else {
9619 if (refclk == 100000)
9620 limit = &intel_limits_ironlake_single_lvds_100m;
9621 else
9622 limit = &intel_limits_ironlake_single_lvds;
9623 }
9624 } else {
9625 limit = &intel_limits_ironlake_dac;
9626 }
9627
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009628 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009629 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9630 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009631 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9632 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009633 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009634
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009635 ironlake_compute_dpll(crtc, crtc_state,
9636 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009637
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009638 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9639 if (pll == NULL) {
9640 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9641 pipe_name(crtc->pipe));
9642 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009643 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009644
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03009645 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009646 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009647 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02009648
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009649 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009650}
9651
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009652static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9653 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009654{
9655 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009656 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009657 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009658
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009659 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9660 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9661 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9662 & ~TU_SIZE_MASK;
9663 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9664 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9665 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9666}
9667
9668static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9669 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009670 struct intel_link_m_n *m_n,
9671 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009672{
9673 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009674 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009675 enum pipe pipe = crtc->pipe;
9676
9677 if (INTEL_INFO(dev)->gen >= 5) {
9678 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9679 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9680 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9681 & ~TU_SIZE_MASK;
9682 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9683 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9684 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009685 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9686 * gen < 8) and if DRRS is supported (to make sure the
9687 * registers are not unnecessarily read).
9688 */
9689 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009690 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009691 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9692 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9693 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9694 & ~TU_SIZE_MASK;
9695 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9696 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9697 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9698 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009699 } else {
9700 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9701 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9702 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9703 & ~TU_SIZE_MASK;
9704 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9705 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9706 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9707 }
9708}
9709
9710void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009711 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009712{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009713 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009714 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9715 else
9716 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009717 &pipe_config->dp_m_n,
9718 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009719}
9720
Daniel Vetter72419202013-04-04 13:28:53 +02009721static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009722 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009723{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009724 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009725 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009726}
9727
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009728static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009729 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009730{
9731 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009732 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Kondurua1b22782015-04-07 15:28:45 -07009733 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9734 uint32_t ps_ctrl = 0;
9735 int id = -1;
9736 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009737
Chandra Kondurua1b22782015-04-07 15:28:45 -07009738 /* find scaler attached to this pipe */
9739 for (i = 0; i < crtc->num_scalers; i++) {
9740 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9741 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9742 id = i;
9743 pipe_config->pch_pfit.enabled = true;
9744 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9745 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9746 break;
9747 }
9748 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009749
Chandra Kondurua1b22782015-04-07 15:28:45 -07009750 scaler_state->scaler_id = id;
9751 if (id >= 0) {
9752 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9753 } else {
9754 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009755 }
9756}
9757
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009758static void
9759skylake_get_initial_plane_config(struct intel_crtc *crtc,
9760 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009761{
9762 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009763 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau40f46282015-02-27 11:15:21 +00009764 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009765 int pipe = crtc->pipe;
9766 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009767 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009768 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009769 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009770
Damien Lespiaud9806c92015-01-21 14:07:19 +00009771 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009772 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009773 DRM_DEBUG_KMS("failed to alloc fb\n");
9774 return;
9775 }
9776
Damien Lespiau1b842c82015-01-21 13:50:54 +00009777 fb = &intel_fb->base;
9778
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009779 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009780 if (!(val & PLANE_CTL_ENABLE))
9781 goto error;
9782
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009783 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9784 fourcc = skl_format_to_fourcc(pixel_format,
9785 val & PLANE_CTL_ORDER_RGBX,
9786 val & PLANE_CTL_ALPHA_MASK);
9787 fb->pixel_format = fourcc;
9788 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9789
Damien Lespiau40f46282015-02-27 11:15:21 +00009790 tiling = val & PLANE_CTL_TILED_MASK;
9791 switch (tiling) {
9792 case PLANE_CTL_TILED_LINEAR:
9793 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9794 break;
9795 case PLANE_CTL_TILED_X:
9796 plane_config->tiling = I915_TILING_X;
9797 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9798 break;
9799 case PLANE_CTL_TILED_Y:
9800 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9801 break;
9802 case PLANE_CTL_TILED_YF:
9803 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9804 break;
9805 default:
9806 MISSING_CASE(tiling);
9807 goto error;
9808 }
9809
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009810 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9811 plane_config->base = base;
9812
9813 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9814
9815 val = I915_READ(PLANE_SIZE(pipe, 0));
9816 fb->height = ((val >> 16) & 0xfff) + 1;
9817 fb->width = ((val >> 0) & 0x1fff) + 1;
9818
9819 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009820 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009821 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009822 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9823
9824 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009825 fb->pixel_format,
9826 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009827
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009828 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009829
9830 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9831 pipe_name(pipe), fb->width, fb->height,
9832 fb->bits_per_pixel, base, fb->pitches[0],
9833 plane_config->size);
9834
Damien Lespiau2d140302015-02-05 17:22:18 +00009835 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009836 return;
9837
9838error:
Matthew Auldd1a3a032016-08-23 16:00:44 +01009839 kfree(intel_fb);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009840}
9841
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009842static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009843 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009844{
9845 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009846 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009847 uint32_t tmp;
9848
9849 tmp = I915_READ(PF_CTL(crtc->pipe));
9850
9851 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009852 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009853 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9854 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009855
9856 /* We currently do not free assignements of panel fitters on
9857 * ivb/hsw (since we don't use the higher upscaling modes which
9858 * differentiates them) so just WARN about this case for now. */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009859 if (IS_GEN7(dev_priv)) {
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009860 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9861 PF_PIPE_SEL_IVB(crtc->pipe));
9862 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009863 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009864}
9865
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009866static void
9867ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9868 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009869{
9870 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009871 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009872 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009873 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009874 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009875 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009876 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009877 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009878
Damien Lespiau42a7b082015-02-05 19:35:13 +00009879 val = I915_READ(DSPCNTR(pipe));
9880 if (!(val & DISPLAY_PLANE_ENABLE))
9881 return;
9882
Damien Lespiaud9806c92015-01-21 14:07:19 +00009883 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009884 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009885 DRM_DEBUG_KMS("failed to alloc fb\n");
9886 return;
9887 }
9888
Damien Lespiau1b842c82015-01-21 13:50:54 +00009889 fb = &intel_fb->base;
9890
Daniel Vetter18c52472015-02-10 17:16:09 +00009891 if (INTEL_INFO(dev)->gen >= 4) {
9892 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009893 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009894 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9895 }
9896 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009897
9898 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009899 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009900 fb->pixel_format = fourcc;
9901 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009902
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009903 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01009904 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009905 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009906 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009907 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009908 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009909 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009910 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009911 }
9912 plane_config->base = base;
9913
9914 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009915 fb->width = ((val >> 16) & 0xfff) + 1;
9916 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009917
9918 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009919 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009920
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009921 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009922 fb->pixel_format,
9923 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009924
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009925 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009926
Damien Lespiau2844a922015-01-20 12:51:48 +00009927 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9928 pipe_name(pipe), fb->width, fb->height,
9929 fb->bits_per_pixel, base, fb->pitches[0],
9930 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009931
Damien Lespiau2d140302015-02-05 17:22:18 +00009932 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009933}
9934
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009935static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009936 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009937{
9938 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009939 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02009940 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009941 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009942 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009943
Imre Deak17290502016-02-12 18:55:11 +02009944 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9945 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009946 return false;
9947
Daniel Vettere143a212013-07-04 12:01:15 +02009948 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009949 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009950
Imre Deak17290502016-02-12 18:55:11 +02009951 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009952 tmp = I915_READ(PIPECONF(crtc->pipe));
9953 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009954 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009955
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009956 switch (tmp & PIPECONF_BPC_MASK) {
9957 case PIPECONF_6BPC:
9958 pipe_config->pipe_bpp = 18;
9959 break;
9960 case PIPECONF_8BPC:
9961 pipe_config->pipe_bpp = 24;
9962 break;
9963 case PIPECONF_10BPC:
9964 pipe_config->pipe_bpp = 30;
9965 break;
9966 case PIPECONF_12BPC:
9967 pipe_config->pipe_bpp = 36;
9968 break;
9969 default:
9970 break;
9971 }
9972
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009973 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9974 pipe_config->limited_color_range = true;
9975
Daniel Vetterab9412b2013-05-03 11:49:46 +02009976 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009977 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009978 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009979
Daniel Vetter88adfff2013-03-28 10:42:01 +01009980 pipe_config->has_pch_encoder = true;
9981
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009982 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9983 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9984 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009985
9986 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009987
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009988 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03009989 /*
9990 * The pipe->pch transcoder and pch transcoder->pll
9991 * mapping is fixed.
9992 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009993 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009994 } else {
9995 tmp = I915_READ(PCH_DPLL_SEL);
9996 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009997 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009998 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009999 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010000 }
Daniel Vetter66e985c2013-06-05 13:34:20 +020010001
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010002 pipe_config->shared_dpll =
10003 intel_get_shared_dpll_by_id(dev_priv, pll_id);
10004 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +020010005
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010006 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10007 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +020010008
10009 tmp = pipe_config->dpll_hw_state.dpll;
10010 pipe_config->pixel_multiplier =
10011 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
10012 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010013
10014 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +020010015 } else {
10016 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010017 }
10018
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010019 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +020010020 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010021
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010022 ironlake_get_pfit_config(crtc, pipe_config);
10023
Imre Deak17290502016-02-12 18:55:11 +020010024 ret = true;
10025
10026out:
10027 intel_display_power_put(dev_priv, power_domain);
10028
10029 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010030}
10031
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010032static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
10033{
Chris Wilson91c8a322016-07-05 10:40:23 +010010034 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010035 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010036
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010037 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -050010038 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010039 pipe_name(crtc->pipe));
10040
Rob Clarke2c719b2014-12-15 13:56:32 -050010041 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
10042 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +030010043 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
10044 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Imre Deak44cb7342016-08-10 14:07:29 +030010045 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010046 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010047 "CPU PWM1 enabled\n");
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010010048 if (IS_HASWELL(dev_priv))
Rob Clarke2c719b2014-12-15 13:56:32 -050010049 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -030010050 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010051 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010052 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010053 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010054 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010055 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010056
Paulo Zanoni9926ada2014-04-01 19:39:47 -030010057 /*
10058 * In theory we can still leave IRQs enabled, as long as only the HPD
10059 * interrupts remain enabled. We used to check for that, but since it's
10060 * gen-specific and since we only disable LCPLL after we fully disable
10061 * the interrupts, the check below should be enough.
10062 */
Rob Clarke2c719b2014-12-15 13:56:32 -050010063 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010064}
10065
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010066static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
10067{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010010068 if (IS_HASWELL(dev_priv))
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010069 return I915_READ(D_COMP_HSW);
10070 else
10071 return I915_READ(D_COMP_BDW);
10072}
10073
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010074static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
10075{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010010076 if (IS_HASWELL(dev_priv)) {
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010077 mutex_lock(&dev_priv->rps.hw_lock);
10078 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
10079 val))
Chris Wilson79cf2192016-08-24 11:16:07 +010010080 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010081 mutex_unlock(&dev_priv->rps.hw_lock);
10082 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010083 I915_WRITE(D_COMP_BDW, val);
10084 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010085 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010086}
10087
10088/*
10089 * This function implements pieces of two sequences from BSpec:
10090 * - Sequence for display software to disable LCPLL
10091 * - Sequence for display software to allow package C8+
10092 * The steps implemented here are just the steps that actually touch the LCPLL
10093 * register. Callers should take care of disabling all the display engine
10094 * functions, doing the mode unset, fixing interrupts, etc.
10095 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -030010096static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
10097 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010098{
10099 uint32_t val;
10100
10101 assert_can_disable_lcpll(dev_priv);
10102
10103 val = I915_READ(LCPLL_CTL);
10104
10105 if (switch_to_fclk) {
10106 val |= LCPLL_CD_SOURCE_FCLK;
10107 I915_WRITE(LCPLL_CTL, val);
10108
Imre Deakf53dd632016-06-28 13:37:32 +030010109 if (wait_for_us(I915_READ(LCPLL_CTL) &
10110 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010111 DRM_ERROR("Switching to FCLK failed\n");
10112
10113 val = I915_READ(LCPLL_CTL);
10114 }
10115
10116 val |= LCPLL_PLL_DISABLE;
10117 I915_WRITE(LCPLL_CTL, val);
10118 POSTING_READ(LCPLL_CTL);
10119
Chris Wilson24d84412016-06-30 15:33:07 +010010120 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010121 DRM_ERROR("LCPLL still locked\n");
10122
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010123 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010124 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010125 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010126 ndelay(100);
10127
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010128 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
10129 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010130 DRM_ERROR("D_COMP RCOMP still in progress\n");
10131
10132 if (allow_power_down) {
10133 val = I915_READ(LCPLL_CTL);
10134 val |= LCPLL_POWER_DOWN_ALLOW;
10135 I915_WRITE(LCPLL_CTL, val);
10136 POSTING_READ(LCPLL_CTL);
10137 }
10138}
10139
10140/*
10141 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
10142 * source.
10143 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -030010144static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010145{
10146 uint32_t val;
10147
10148 val = I915_READ(LCPLL_CTL);
10149
10150 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
10151 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
10152 return;
10153
Paulo Zanonia8a8bd52014-03-07 20:08:05 -030010154 /*
10155 * Make sure we're not on PC8 state before disabling PC8, otherwise
10156 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -030010157 */
Mika Kuoppala59bad942015-01-16 11:34:40 +020010158 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -030010159
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010160 if (val & LCPLL_POWER_DOWN_ALLOW) {
10161 val &= ~LCPLL_POWER_DOWN_ALLOW;
10162 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +020010163 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010164 }
10165
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010166 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010167 val |= D_COMP_COMP_FORCE;
10168 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010169 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010170
10171 val = I915_READ(LCPLL_CTL);
10172 val &= ~LCPLL_PLL_DISABLE;
10173 I915_WRITE(LCPLL_CTL, val);
10174
Chris Wilson93220c02016-06-30 15:33:08 +010010175 if (intel_wait_for_register(dev_priv,
10176 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
10177 5))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010178 DRM_ERROR("LCPLL not locked yet\n");
10179
10180 if (val & LCPLL_CD_SOURCE_FCLK) {
10181 val = I915_READ(LCPLL_CTL);
10182 val &= ~LCPLL_CD_SOURCE_FCLK;
10183 I915_WRITE(LCPLL_CTL, val);
10184
Imre Deakf53dd632016-06-28 13:37:32 +030010185 if (wait_for_us((I915_READ(LCPLL_CTL) &
10186 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010187 DRM_ERROR("Switching back to LCPLL failed\n");
10188 }
Paulo Zanoni215733f2013-08-19 13:18:07 -030010189
Mika Kuoppala59bad942015-01-16 11:34:40 +020010190 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Chris Wilson91c8a322016-07-05 10:40:23 +010010191 intel_update_cdclk(&dev_priv->drm);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010192}
10193
Paulo Zanoni765dab672014-03-07 20:08:18 -030010194/*
10195 * Package states C8 and deeper are really deep PC states that can only be
10196 * reached when all the devices on the system allow it, so even if the graphics
10197 * device allows PC8+, it doesn't mean the system will actually get to these
10198 * states. Our driver only allows PC8+ when going into runtime PM.
10199 *
10200 * The requirements for PC8+ are that all the outputs are disabled, the power
10201 * well is disabled and most interrupts are disabled, and these are also
10202 * requirements for runtime PM. When these conditions are met, we manually do
10203 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
10204 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
10205 * hang the machine.
10206 *
10207 * When we really reach PC8 or deeper states (not just when we allow it) we lose
10208 * the state of some registers, so when we come back from PC8+ we need to
10209 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
10210 * need to take care of the registers kept by RC6. Notice that this happens even
10211 * if we don't put the device in PCI D3 state (which is what currently happens
10212 * because of the runtime PM support).
10213 *
10214 * For more, read "Display Sequences for Package C8" on the hardware
10215 * documentation.
10216 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -030010217void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -030010218{
Chris Wilson91c8a322016-07-05 10:40:23 +010010219 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonic67a4702013-08-19 13:18:09 -030010220 uint32_t val;
10221
Paulo Zanonic67a4702013-08-19 13:18:09 -030010222 DRM_DEBUG_KMS("Enabling package C8+\n");
10223
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010010224 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -030010225 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10226 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
10227 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10228 }
10229
10230 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010231 hsw_disable_lcpll(dev_priv, true, true);
10232}
10233
Paulo Zanonia14cb6f2014-03-07 20:08:17 -030010234void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -030010235{
Chris Wilson91c8a322016-07-05 10:40:23 +010010236 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonic67a4702013-08-19 13:18:09 -030010237 uint32_t val;
10238
Paulo Zanonic67a4702013-08-19 13:18:09 -030010239 DRM_DEBUG_KMS("Disabling package C8+\n");
10240
10241 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010242 lpt_init_pch_refclk(dev);
10243
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010010244 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -030010245 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10246 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
10247 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10248 }
Paulo Zanonic67a4702013-08-19 13:18:09 -030010249}
10250
Imre Deak324513c2016-06-13 16:44:36 +030010251static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +053010252{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030010253 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010254 struct intel_atomic_state *old_intel_state =
10255 to_intel_atomic_state(old_state);
10256 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053010257
Imre Deak324513c2016-06-13 16:44:36 +030010258 bxt_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +053010259}
10260
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010261/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010262static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010263{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010264 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010010265 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010266 struct drm_crtc *crtc;
10267 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010268 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010269 unsigned max_pixel_rate = 0, i;
10270 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010271
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010272 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
10273 sizeof(intel_state->min_pixclk));
10274
10275 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010276 int pixel_rate;
10277
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010278 crtc_state = to_intel_crtc_state(cstate);
10279 if (!crtc_state->base.enable) {
10280 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010281 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010282 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010283
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010284 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010285
10286 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010287 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010288 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
10289
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010290 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010291 }
10292
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010293 for_each_pipe(dev_priv, pipe)
10294 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
10295
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010296 return max_pixel_rate;
10297}
10298
10299static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
10300{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010301 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010302 uint32_t val, data;
10303 int ret;
10304
10305 if (WARN((I915_READ(LCPLL_CTL) &
10306 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
10307 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
10308 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
10309 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
10310 "trying to change cdclk frequency with cdclk not enabled\n"))
10311 return;
10312
10313 mutex_lock(&dev_priv->rps.hw_lock);
10314 ret = sandybridge_pcode_write(dev_priv,
10315 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
10316 mutex_unlock(&dev_priv->rps.hw_lock);
10317 if (ret) {
10318 DRM_ERROR("failed to inform pcode about cdclk change\n");
10319 return;
10320 }
10321
10322 val = I915_READ(LCPLL_CTL);
10323 val |= LCPLL_CD_SOURCE_FCLK;
10324 I915_WRITE(LCPLL_CTL, val);
10325
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +000010326 if (wait_for_us(I915_READ(LCPLL_CTL) &
10327 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010328 DRM_ERROR("Switching to FCLK failed\n");
10329
10330 val = I915_READ(LCPLL_CTL);
10331 val &= ~LCPLL_CLK_FREQ_MASK;
10332
10333 switch (cdclk) {
10334 case 450000:
10335 val |= LCPLL_CLK_FREQ_450;
10336 data = 0;
10337 break;
10338 case 540000:
10339 val |= LCPLL_CLK_FREQ_54O_BDW;
10340 data = 1;
10341 break;
10342 case 337500:
10343 val |= LCPLL_CLK_FREQ_337_5_BDW;
10344 data = 2;
10345 break;
10346 case 675000:
10347 val |= LCPLL_CLK_FREQ_675_BDW;
10348 data = 3;
10349 break;
10350 default:
10351 WARN(1, "invalid cdclk frequency\n");
10352 return;
10353 }
10354
10355 I915_WRITE(LCPLL_CTL, val);
10356
10357 val = I915_READ(LCPLL_CTL);
10358 val &= ~LCPLL_CD_SOURCE_FCLK;
10359 I915_WRITE(LCPLL_CTL, val);
10360
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +000010361 if (wait_for_us((I915_READ(LCPLL_CTL) &
10362 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010363 DRM_ERROR("Switching back to LCPLL failed\n");
10364
10365 mutex_lock(&dev_priv->rps.hw_lock);
10366 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
10367 mutex_unlock(&dev_priv->rps.hw_lock);
10368
Ville Syrjälä7f1052a2016-04-26 19:46:32 +030010369 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
10370
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010371 intel_update_cdclk(dev);
10372
10373 WARN(cdclk != dev_priv->cdclk_freq,
10374 "cdclk requested %d kHz but got %d kHz\n",
10375 cdclk, dev_priv->cdclk_freq);
10376}
10377
Ville Syrjälä587c7912016-05-11 22:44:41 +030010378static int broadwell_calc_cdclk(int max_pixclk)
10379{
10380 if (max_pixclk > 540000)
10381 return 675000;
10382 else if (max_pixclk > 450000)
10383 return 540000;
10384 else if (max_pixclk > 337500)
10385 return 450000;
10386 else
10387 return 337500;
10388}
10389
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010390static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010391{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010392 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010393 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010394 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010395 int cdclk;
10396
10397 /*
10398 * FIXME should also account for plane ratio
10399 * once 64bpp pixel formats are supported.
10400 */
Ville Syrjälä587c7912016-05-11 22:44:41 +030010401 cdclk = broadwell_calc_cdclk(max_pixclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010402
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010403 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +010010404 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10405 cdclk, dev_priv->max_cdclk_freq);
10406 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010407 }
10408
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010409 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10410 if (!intel_state->active_crtcs)
Ville Syrjälä587c7912016-05-11 22:44:41 +030010411 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010412
10413 return 0;
10414}
10415
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010416static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010417{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010418 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010419 struct intel_atomic_state *old_intel_state =
10420 to_intel_atomic_state(old_state);
10421 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010422
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010423 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010424}
10425
Clint Taylorc89e39f2016-05-13 23:41:21 +030010426static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
10427{
10428 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
10429 struct drm_i915_private *dev_priv = to_i915(state->dev);
10430 const int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläa8ca4932016-05-13 23:41:23 +030010431 int vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +030010432 int cdclk;
10433
10434 /*
10435 * FIXME should also account for plane ratio
10436 * once 64bpp pixel formats are supported.
10437 */
Ville Syrjäläa8ca4932016-05-13 23:41:23 +030010438 cdclk = skl_calc_cdclk(max_pixclk, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +030010439
10440 /*
10441 * FIXME move the cdclk caclulation to
10442 * compute_config() so we can fail gracegully.
10443 */
10444 if (cdclk > dev_priv->max_cdclk_freq) {
10445 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10446 cdclk, dev_priv->max_cdclk_freq);
10447 cdclk = dev_priv->max_cdclk_freq;
10448 }
10449
10450 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10451 if (!intel_state->active_crtcs)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +030010452 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +030010453
10454 return 0;
10455}
10456
10457static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
10458{
Ville Syrjälä1cd593e2016-05-13 23:41:26 +030010459 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
10460 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
10461 unsigned int req_cdclk = intel_state->dev_cdclk;
10462 unsigned int req_vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +030010463
Ville Syrjälä1cd593e2016-05-13 23:41:26 +030010464 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +030010465}
10466
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +020010467static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
10468 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010469{
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010470 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
Mika Kaholaaf3997b2016-02-05 13:29:28 +020010471 if (!intel_ddi_pll_select(crtc, crtc_state))
10472 return -EINVAL;
10473 }
Daniel Vetter716c2e52014-06-25 22:02:02 +030010474
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +030010475 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +020010476
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +020010477 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010478}
10479
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010480static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
10481 enum port port,
10482 struct intel_crtc_state *pipe_config)
10483{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010484 enum intel_dpll_id id;
10485
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010486 switch (port) {
10487 case PORT_A:
Imre Deak08250c42016-03-14 19:55:34 +020010488 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010489 break;
10490 case PORT_B:
Imre Deak08250c42016-03-14 19:55:34 +020010491 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010492 break;
10493 case PORT_C:
Imre Deak08250c42016-03-14 19:55:34 +020010494 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010495 break;
10496 default:
10497 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010498 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010499 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010500
10501 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010502}
10503
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010504static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
10505 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010506 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010507{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010508 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +020010509 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010510
10511 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010512 id = temp >> (port * 3 + 1);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010513
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010514 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010515 return;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010516
10517 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010518}
10519
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010520static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
10521 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010522 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010523{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010524 enum intel_dpll_id id;
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010525 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010526
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010527 switch (ddi_pll_sel) {
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010528 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010529 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010530 break;
10531 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010532 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010533 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +010010534 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010535 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +020010536 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020010537 case PORT_CLK_SEL_LCPLL_810:
10538 id = DPLL_ID_LCPLL_810;
10539 break;
10540 case PORT_CLK_SEL_LCPLL_1350:
10541 id = DPLL_ID_LCPLL_1350;
10542 break;
10543 case PORT_CLK_SEL_LCPLL_2700:
10544 id = DPLL_ID_LCPLL_2700;
10545 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010546 default:
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010547 MISSING_CASE(ddi_pll_sel);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010548 /* fall through */
10549 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010550 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010551 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010552
10553 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010554}
10555
Jani Nikulacf304292016-03-18 17:05:41 +020010556static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10557 struct intel_crtc_state *pipe_config,
10558 unsigned long *power_domain_mask)
10559{
10560 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010561 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulacf304292016-03-18 17:05:41 +020010562 enum intel_display_power_domain power_domain;
10563 u32 tmp;
10564
Imre Deakd9a7bc62016-05-12 16:18:50 +030010565 /*
10566 * The pipe->transcoder mapping is fixed with the exception of the eDP
10567 * transcoder handled below.
10568 */
Jani Nikulacf304292016-03-18 17:05:41 +020010569 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10570
10571 /*
10572 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10573 * consistency and less surprising code; it's in always on power).
10574 */
10575 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10576 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10577 enum pipe trans_edp_pipe;
10578 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10579 default:
10580 WARN(1, "unknown pipe linked to edp transcoder\n");
10581 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10582 case TRANS_DDI_EDP_INPUT_A_ON:
10583 trans_edp_pipe = PIPE_A;
10584 break;
10585 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10586 trans_edp_pipe = PIPE_B;
10587 break;
10588 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10589 trans_edp_pipe = PIPE_C;
10590 break;
10591 }
10592
10593 if (trans_edp_pipe == crtc->pipe)
10594 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10595 }
10596
10597 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10598 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10599 return false;
10600 *power_domain_mask |= BIT(power_domain);
10601
10602 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10603
10604 return tmp & PIPECONF_ENABLE;
10605}
10606
Jani Nikula4d1de972016-03-18 17:05:42 +020010607static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10608 struct intel_crtc_state *pipe_config,
10609 unsigned long *power_domain_mask)
10610{
10611 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010612 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020010613 enum intel_display_power_domain power_domain;
10614 enum port port;
10615 enum transcoder cpu_transcoder;
10616 u32 tmp;
10617
Jani Nikula4d1de972016-03-18 17:05:42 +020010618 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10619 if (port == PORT_A)
10620 cpu_transcoder = TRANSCODER_DSI_A;
10621 else
10622 cpu_transcoder = TRANSCODER_DSI_C;
10623
10624 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10625 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10626 continue;
10627 *power_domain_mask |= BIT(power_domain);
10628
Imre Deakdb18b6a2016-03-24 12:41:40 +020010629 /*
10630 * The PLL needs to be enabled with a valid divider
10631 * configuration, otherwise accessing DSI registers will hang
10632 * the machine. See BSpec North Display Engine
10633 * registers/MIPI[BXT]. We can break out here early, since we
10634 * need the same DSI PLL to be enabled for both DSI ports.
10635 */
10636 if (!intel_dsi_pll_is_enabled(dev_priv))
10637 break;
10638
Jani Nikula4d1de972016-03-18 17:05:42 +020010639 /* XXX: this works for video mode only */
10640 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10641 if (!(tmp & DPI_ENABLE))
10642 continue;
10643
10644 tmp = I915_READ(MIPI_CTRL(port));
10645 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10646 continue;
10647
10648 pipe_config->cpu_transcoder = cpu_transcoder;
Jani Nikula4d1de972016-03-18 17:05:42 +020010649 break;
10650 }
10651
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010652 return transcoder_is_dsi(pipe_config->cpu_transcoder);
Jani Nikula4d1de972016-03-18 17:05:42 +020010653}
10654
Daniel Vetter26804af2014-06-25 22:01:55 +030010655static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010656 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +030010657{
10658 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010659 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010660 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +030010661 enum port port;
10662 uint32_t tmp;
10663
10664 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10665
10666 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10667
Tvrtko Ursulin08537232016-10-13 11:03:02 +010010668 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010669 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +010010670 else if (IS_BROXTON(dev_priv))
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010671 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010672 else
10673 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010674
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010675 pll = pipe_config->shared_dpll;
10676 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010677 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10678 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010679 }
10680
Daniel Vetter26804af2014-06-25 22:01:55 +030010681 /*
10682 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10683 * DDI E. So just check whether this pipe is wired to DDI E and whether
10684 * the PCH transcoder is on.
10685 */
Damien Lespiauca370452013-12-03 13:56:24 +000010686 if (INTEL_INFO(dev)->gen < 9 &&
10687 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010688 pipe_config->has_pch_encoder = true;
10689
10690 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10691 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10692 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10693
10694 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10695 }
10696}
10697
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010698static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010699 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010700{
10701 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010702 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +020010703 enum intel_display_power_domain power_domain;
10704 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +020010705 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010706
Imre Deak17290502016-02-12 18:55:11 +020010707 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10708 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010709 return false;
Imre Deak17290502016-02-12 18:55:11 +020010710 power_domain_mask = BIT(power_domain);
10711
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010712 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010713
Jani Nikulacf304292016-03-18 17:05:41 +020010714 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +020010715
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010716 if (IS_BROXTON(dev_priv) &&
10717 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10718 WARN_ON(active);
10719 active = true;
Jani Nikula4d1de972016-03-18 17:05:42 +020010720 }
10721
Jani Nikulacf304292016-03-18 17:05:41 +020010722 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010723 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010724
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010725 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +020010726 haswell_get_ddi_port_state(crtc, pipe_config);
10727 intel_get_pipe_timings(crtc, pipe_config);
10728 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010729
Jani Nikulabc58be62016-03-18 17:05:39 +020010730 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010731
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010732 pipe_config->gamma_mode =
10733 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10734
Chandra Kondurua1b22782015-04-07 15:28:45 -070010735 if (INTEL_INFO(dev)->gen >= 9) {
Ville Syrjälä65edccc2016-10-31 22:37:01 +020010736 skl_init_scalers(dev_priv, crtc, pipe_config);
Chandra Kondurua1b22782015-04-07 15:28:45 -070010737
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070010738 pipe_config->scaler_state.scaler_id = -1;
10739 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10740 }
10741
Imre Deak17290502016-02-12 18:55:11 +020010742 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10743 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10744 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010745 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010746 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010747 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010748 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010749 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010750
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010010751 if (IS_HASWELL(dev_priv))
Jesse Barnese59150d2014-01-07 13:30:45 -080010752 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10753 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010754
Jani Nikula4d1de972016-03-18 17:05:42 +020010755 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10756 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010757 pipe_config->pixel_multiplier =
10758 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10759 } else {
10760 pipe_config->pixel_multiplier = 1;
10761 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010762
Imre Deak17290502016-02-12 18:55:11 +020010763out:
10764 for_each_power_domain(power_domain, power_domain_mask)
10765 intel_display_power_put(dev_priv, power_domain);
10766
Jani Nikulacf304292016-03-18 17:05:41 +020010767 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010768}
10769
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010770static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10771 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010772{
10773 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010774 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010775 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010776 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010777
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010778 if (plane_state && plane_state->base.visible) {
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010779 unsigned int width = plane_state->base.crtc_w;
10780 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010781 unsigned int stride = roundup_pow_of_two(width) * 4;
10782
10783 switch (stride) {
10784 default:
10785 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10786 width, stride);
10787 stride = 256;
10788 /* fallthrough */
10789 case 256:
10790 case 512:
10791 case 1024:
10792 case 2048:
10793 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010794 }
10795
Ville Syrjälädc41c152014-08-13 11:57:05 +030010796 cntl |= CURSOR_ENABLE |
10797 CURSOR_GAMMA_ENABLE |
10798 CURSOR_FORMAT_ARGB |
10799 CURSOR_STRIDE(stride);
10800
10801 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010802 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010803
Ville Syrjälädc41c152014-08-13 11:57:05 +030010804 if (intel_crtc->cursor_cntl != 0 &&
10805 (intel_crtc->cursor_base != base ||
10806 intel_crtc->cursor_size != size ||
10807 intel_crtc->cursor_cntl != cntl)) {
10808 /* On these chipsets we can only modify the base/size/stride
10809 * whilst the cursor is disabled.
10810 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010811 I915_WRITE(CURCNTR(PIPE_A), 0);
10812 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010813 intel_crtc->cursor_cntl = 0;
10814 }
10815
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010816 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010817 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010818 intel_crtc->cursor_base = base;
10819 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010820
10821 if (intel_crtc->cursor_size != size) {
10822 I915_WRITE(CURSIZE, size);
10823 intel_crtc->cursor_size = size;
10824 }
10825
Chris Wilson4b0e3332014-05-30 16:35:26 +030010826 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010827 I915_WRITE(CURCNTR(PIPE_A), cntl);
10828 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010829 intel_crtc->cursor_cntl = cntl;
10830 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010831}
10832
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010833static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10834 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010835{
10836 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010837 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010838 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -020010839 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Lyude62e0fb82016-08-22 12:50:08 -040010840 const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -020010841 const struct skl_plane_wm *p_wm =
10842 &cstate->wm.skl.optimal.planes[PLANE_CURSOR];
Chris Wilson560b85b2010-08-07 11:01:38 +010010843 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010844 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010845
Lyude62e0fb82016-08-22 12:50:08 -040010846 if (INTEL_GEN(dev_priv) >= 9 && wm->dirty_pipes & drm_crtc_mask(crtc))
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -020010847 skl_write_cursor_wm(intel_crtc, p_wm, &wm->ddb);
Lyude62e0fb82016-08-22 12:50:08 -040010848
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010849 if (plane_state && plane_state->base.visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010850 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010851 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010852 case 64:
10853 cntl |= CURSOR_MODE_64_ARGB_AX;
10854 break;
10855 case 128:
10856 cntl |= CURSOR_MODE_128_ARGB_AX;
10857 break;
10858 case 256:
10859 cntl |= CURSOR_MODE_256_ARGB_AX;
10860 break;
10861 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010862 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010863 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010864 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010865 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010866
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010010867 if (HAS_DDI(dev_priv))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010868 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010869
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +030010870 if (plane_state->base.rotation == DRM_ROTATE_180)
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010871 cntl |= CURSOR_ROTATE_180;
10872 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010873
Chris Wilson4b0e3332014-05-30 16:35:26 +030010874 if (intel_crtc->cursor_cntl != cntl) {
10875 I915_WRITE(CURCNTR(pipe), cntl);
10876 POSTING_READ(CURCNTR(pipe));
10877 intel_crtc->cursor_cntl = cntl;
10878 }
10879
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010880 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010881 I915_WRITE(CURBASE(pipe), base);
10882 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010883
10884 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010885}
10886
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010887/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010888static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010889 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010890{
10891 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010892 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010893 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10894 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010895 u32 base = intel_crtc->cursor_addr;
10896 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010897
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010898 if (plane_state) {
10899 int x = plane_state->base.crtc_x;
10900 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010901
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010902 if (x < 0) {
10903 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10904 x = -x;
10905 }
10906 pos |= x << CURSOR_X_SHIFT;
10907
10908 if (y < 0) {
10909 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10910 y = -y;
10911 }
10912 pos |= y << CURSOR_Y_SHIFT;
10913
10914 /* ILK+ do this automagically */
Tvrtko Ursulin49cff962016-10-13 11:02:54 +010010915 if (HAS_GMCH_DISPLAY(dev_priv) &&
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +030010916 plane_state->base.rotation == DRM_ROTATE_180) {
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010917 base += (plane_state->base.crtc_h *
10918 plane_state->base.crtc_w - 1) * 4;
10919 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010920 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010921
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010922 I915_WRITE(CURPOS(pipe), pos);
10923
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010924 if (IS_845G(dev_priv) || IS_I865G(dev_priv))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010925 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010926 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010927 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010928}
10929
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010930static bool cursor_size_ok(struct drm_i915_private *dev_priv,
Ville Syrjälädc41c152014-08-13 11:57:05 +030010931 uint32_t width, uint32_t height)
10932{
10933 if (width == 0 || height == 0)
10934 return false;
10935
10936 /*
10937 * 845g/865g are special in that they are only limited by
10938 * the width of their cursors, the height is arbitrary up to
10939 * the precision of the register. Everything else requires
10940 * square cursors, limited to a few power-of-two sizes.
10941 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010942 if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
Ville Syrjälädc41c152014-08-13 11:57:05 +030010943 if ((width & 63) != 0)
10944 return false;
10945
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010946 if (width > (IS_845G(dev_priv) ? 64 : 512))
Ville Syrjälädc41c152014-08-13 11:57:05 +030010947 return false;
10948
10949 if (height > 1023)
10950 return false;
10951 } else {
10952 switch (width | height) {
10953 case 256:
10954 case 128:
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010955 if (IS_GEN2(dev_priv))
Ville Syrjälädc41c152014-08-13 11:57:05 +030010956 return false;
10957 case 64:
10958 break;
10959 default:
10960 return false;
10961 }
10962 }
10963
10964 return true;
10965}
10966
Jesse Barnes79e53942008-11-07 14:24:08 -080010967/* VESA 640x480x72Hz mode to set on the pipe */
10968static struct drm_display_mode load_detect_mode = {
10969 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10970 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10971};
10972
Daniel Vettera8bb6812014-02-10 18:00:39 +010010973struct drm_framebuffer *
10974__intel_framebuffer_create(struct drm_device *dev,
10975 struct drm_mode_fb_cmd2 *mode_cmd,
10976 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010977{
10978 struct intel_framebuffer *intel_fb;
10979 int ret;
10980
10981 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010982 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010983 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010984
10985 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010986 if (ret)
10987 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010988
10989 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010990
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010991err:
10992 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010993 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010994}
10995
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010996static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010997intel_framebuffer_create(struct drm_device *dev,
10998 struct drm_mode_fb_cmd2 *mode_cmd,
10999 struct drm_i915_gem_object *obj)
11000{
11001 struct drm_framebuffer *fb;
11002 int ret;
11003
11004 ret = i915_mutex_lock_interruptible(dev);
11005 if (ret)
11006 return ERR_PTR(ret);
11007 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
11008 mutex_unlock(&dev->struct_mutex);
11009
11010 return fb;
11011}
11012
Chris Wilsond2dff872011-04-19 08:36:26 +010011013static u32
11014intel_framebuffer_pitch_for_width(int width, int bpp)
11015{
11016 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
11017 return ALIGN(pitch, 64);
11018}
11019
11020static u32
11021intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
11022{
11023 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020011024 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010011025}
11026
11027static struct drm_framebuffer *
11028intel_framebuffer_create_for_mode(struct drm_device *dev,
11029 struct drm_display_mode *mode,
11030 int depth, int bpp)
11031{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020011032 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010011033 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000011034 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010011035
Dave Gordond37cd8a2016-04-22 19:14:32 +010011036 obj = i915_gem_object_create(dev,
Chris Wilsond2dff872011-04-19 08:36:26 +010011037 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +010011038 if (IS_ERR(obj))
11039 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010011040
11041 mode_cmd.width = mode->hdisplay;
11042 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011043 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
11044 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000011045 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010011046
Lukas Wunnerdcb13942015-07-04 11:50:58 +020011047 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
11048 if (IS_ERR(fb))
Chris Wilsonf0cd5182016-10-28 13:58:43 +010011049 i915_gem_object_put(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020011050
11051 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010011052}
11053
11054static struct drm_framebuffer *
11055mode_fits_in_fbdev(struct drm_device *dev,
11056 struct drm_display_mode *mode)
11057{
Daniel Vetter06957262015-08-10 13:34:08 +020011058#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsonfac5e232016-07-04 11:34:36 +010011059 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsond2dff872011-04-19 08:36:26 +010011060 struct drm_i915_gem_object *obj;
11061 struct drm_framebuffer *fb;
11062
Daniel Vetter4c0e5522014-02-14 16:35:54 +010011063 if (!dev_priv->fbdev)
11064 return NULL;
11065
11066 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010011067 return NULL;
11068
Jesse Barnes8bcd4552014-02-07 12:10:38 -080011069 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010011070 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010011071
Jesse Barnes8bcd4552014-02-07 12:10:38 -080011072 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011073 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
11074 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010011075 return NULL;
11076
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011077 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010011078 return NULL;
11079
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011080 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010011081 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020011082#else
11083 return NULL;
11084#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010011085}
11086
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030011087static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
11088 struct drm_crtc *crtc,
11089 struct drm_display_mode *mode,
11090 struct drm_framebuffer *fb,
11091 int x, int y)
11092{
11093 struct drm_plane_state *plane_state;
11094 int hdisplay, vdisplay;
11095 int ret;
11096
11097 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
11098 if (IS_ERR(plane_state))
11099 return PTR_ERR(plane_state);
11100
11101 if (mode)
11102 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
11103 else
11104 hdisplay = vdisplay = 0;
11105
11106 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
11107 if (ret)
11108 return ret;
11109 drm_atomic_set_fb_for_plane(plane_state, fb);
11110 plane_state->crtc_x = 0;
11111 plane_state->crtc_y = 0;
11112 plane_state->crtc_w = hdisplay;
11113 plane_state->crtc_h = vdisplay;
11114 plane_state->src_x = x << 16;
11115 plane_state->src_y = y << 16;
11116 plane_state->src_w = hdisplay << 16;
11117 plane_state->src_h = vdisplay << 16;
11118
11119 return 0;
11120}
11121
Daniel Vetterd2434ab2012-08-12 21:20:10 +020011122bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010011123 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050011124 struct intel_load_detect_pipe *old,
11125 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080011126{
11127 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020011128 struct intel_encoder *intel_encoder =
11129 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080011130 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010011131 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080011132 struct drm_crtc *crtc = NULL;
11133 struct drm_device *dev = encoder->dev;
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020011134 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94352cf2012-07-05 22:51:56 +020011135 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050011136 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011137 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020011138 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030011139 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050011140 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080011141
Chris Wilsond2dff872011-04-19 08:36:26 +010011142 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030011143 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030011144 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010011145
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011146 old->restore_state = NULL;
11147
Rob Clark51fd3712013-11-19 12:10:12 -050011148retry:
11149 ret = drm_modeset_lock(&config->connection_mutex, ctx);
11150 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011151 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020011152
Jesse Barnes79e53942008-11-07 14:24:08 -080011153 /*
11154 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010011155 *
Jesse Barnes79e53942008-11-07 14:24:08 -080011156 * - if the connector already has an assigned crtc, use it (but make
11157 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010011158 *
Jesse Barnes79e53942008-11-07 14:24:08 -080011159 * - try to find the first unused crtc that can drive this connector,
11160 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080011161 */
11162
11163 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011164 if (connector->state->crtc) {
11165 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010011166
Rob Clark51fd3712013-11-19 12:10:12 -050011167 ret = drm_modeset_lock(&crtc->mutex, ctx);
11168 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011169 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010011170
11171 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011172 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080011173 }
11174
11175 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010011176 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080011177 i++;
11178 if (!(encoder->possible_crtcs & (1 << i)))
11179 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011180
11181 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
11182 if (ret)
11183 goto fail;
11184
11185 if (possible_crtc->state->enable) {
11186 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030011187 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011188 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030011189
11190 crtc = possible_crtc;
11191 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080011192 }
11193
11194 /*
11195 * If we didn't find an unused CRTC, don't use any.
11196 */
11197 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010011198 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011199 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011200 }
11201
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011202found:
11203 intel_crtc = to_intel_crtc(crtc);
11204
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010011205 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
11206 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011207 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011208
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011209 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011210 restore_state = drm_atomic_state_alloc(dev);
11211 if (!state || !restore_state) {
11212 ret = -ENOMEM;
11213 goto fail;
11214 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011215
11216 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011217 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011218
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020011219 connector_state = drm_atomic_get_connector_state(state, connector);
11220 if (IS_ERR(connector_state)) {
11221 ret = PTR_ERR(connector_state);
11222 goto fail;
11223 }
11224
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011225 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
11226 if (ret)
11227 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020011228
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030011229 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
11230 if (IS_ERR(crtc_state)) {
11231 ret = PTR_ERR(crtc_state);
11232 goto fail;
11233 }
11234
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020011235 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030011236
Chris Wilson64927112011-04-20 07:25:26 +010011237 if (!mode)
11238 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080011239
Chris Wilsond2dff872011-04-19 08:36:26 +010011240 /* We need a framebuffer large enough to accommodate all accesses
11241 * that the plane may generate whilst we perform load detection.
11242 * We can not rely on the fbcon either being present (we get called
11243 * during its initialisation to detect all boot displays, or it may
11244 * not even exist) or that it is large enough to satisfy the
11245 * requested mode.
11246 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020011247 fb = mode_fits_in_fbdev(dev, mode);
11248 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010011249 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020011250 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010011251 } else
11252 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020011253 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010011254 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020011255 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011256 }
Chris Wilsond2dff872011-04-19 08:36:26 +010011257
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030011258 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
11259 if (ret)
11260 goto fail;
11261
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011262 drm_framebuffer_unreference(fb);
11263
11264 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
11265 if (ret)
11266 goto fail;
11267
11268 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
11269 if (!ret)
11270 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
11271 if (!ret)
11272 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
11273 if (ret) {
11274 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
11275 goto fail;
11276 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030011277
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010011278 ret = drm_atomic_commit(state);
11279 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010011280 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020011281 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011282 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011283
11284 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010011285
Jesse Barnes79e53942008-11-07 14:24:08 -080011286 /* let the connector get through one full cycle before testing */
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020011287 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010011288 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020011289
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011290fail:
Chris Wilson7fb71c82016-10-19 12:37:43 +010011291 if (state) {
11292 drm_atomic_state_put(state);
11293 state = NULL;
11294 }
11295 if (restore_state) {
11296 drm_atomic_state_put(restore_state);
11297 restore_state = NULL;
11298 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011299
Rob Clark51fd3712013-11-19 12:10:12 -050011300 if (ret == -EDEADLK) {
11301 drm_modeset_backoff(ctx);
11302 goto retry;
11303 }
11304
Ville Syrjälä412b61d2014-01-17 15:59:39 +020011305 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080011306}
11307
Daniel Vetterd2434ab2012-08-12 21:20:10 +020011308void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020011309 struct intel_load_detect_pipe *old,
11310 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080011311{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020011312 struct intel_encoder *intel_encoder =
11313 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010011314 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011315 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030011316 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080011317
Chris Wilsond2dff872011-04-19 08:36:26 +010011318 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030011319 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030011320 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010011321
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011322 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010011323 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011324
11325 ret = drm_atomic_commit(state);
Chris Wilson08536952016-10-14 13:18:18 +010011326 if (ret)
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011327 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
Chris Wilson08536952016-10-14 13:18:18 +010011328 drm_atomic_state_put(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080011329}
11330
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011331static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011332 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011333{
Chris Wilsonfac5e232016-07-04 11:34:36 +010011334 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011335 u32 dpll = pipe_config->dpll_hw_state.dpll;
11336
11337 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020011338 return dev_priv->vbt.lvds_ssc_freq;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010011339 else if (HAS_PCH_SPLIT(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011340 return 120000;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011341 else if (!IS_GEN2(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011342 return 96000;
11343 else
11344 return 48000;
11345}
11346
Jesse Barnes79e53942008-11-07 14:24:08 -080011347/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011348static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011349 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080011350{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011351 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011352 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011353 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030011354 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080011355 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030011356 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030011357 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011358 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080011359
11360 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030011361 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080011362 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030011363 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080011364
11365 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050011366 if (IS_PINEVIEW(dev)) {
11367 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
11368 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080011369 } else {
11370 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
11371 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
11372 }
11373
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011374 if (!IS_GEN2(dev_priv)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050011375 if (IS_PINEVIEW(dev))
11376 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
11377 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080011378 else
11379 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080011380 DPLL_FPA01_P1_POST_DIV_SHIFT);
11381
11382 switch (dpll & DPLL_MODE_MASK) {
11383 case DPLLB_MODE_DAC_SERIAL:
11384 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
11385 5 : 10;
11386 break;
11387 case DPLLB_MODE_LVDS:
11388 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
11389 7 : 14;
11390 break;
11391 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080011392 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080011393 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011394 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080011395 }
11396
Daniel Vetterac58c3f2013-06-01 17:16:17 +020011397 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030011398 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020011399 else
Imre Deakdccbea32015-06-22 23:35:51 +030011400 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080011401 } else {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010011402 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020011403 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080011404
11405 if (is_lvds) {
11406 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
11407 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020011408
11409 if (lvds & LVDS_CLKB_POWER_UP)
11410 clock.p2 = 7;
11411 else
11412 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080011413 } else {
11414 if (dpll & PLL_P1_DIVIDE_BY_TWO)
11415 clock.p1 = 2;
11416 else {
11417 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
11418 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
11419 }
11420 if (dpll & PLL_P2_DIVIDE_BY_4)
11421 clock.p2 = 4;
11422 else
11423 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080011424 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011425
Imre Deakdccbea32015-06-22 23:35:51 +030011426 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080011427 }
11428
Ville Syrjälä18442d02013-09-13 16:00:08 +030011429 /*
11430 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010011431 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030011432 * encoder's get_config() function.
11433 */
Imre Deakdccbea32015-06-22 23:35:51 +030011434 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011435}
11436
Ville Syrjälä6878da02013-09-13 15:59:11 +030011437int intel_dotclock_calculate(int link_freq,
11438 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011439{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011440 /*
11441 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030011442 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011443 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030011444 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011445 *
11446 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030011447 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080011448 */
11449
Ville Syrjälä6878da02013-09-13 15:59:11 +030011450 if (!m_n->link_n)
11451 return 0;
11452
11453 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
11454}
11455
Ville Syrjälä18442d02013-09-13 16:00:08 +030011456static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011457 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030011458{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011459 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030011460
11461 /* read out port_clock from the DPLL */
11462 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030011463
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011464 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011465 * In case there is an active pipe without active ports,
11466 * we may need some idea for the dotclock anyway.
11467 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011468 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011469 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020011470 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030011471 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080011472}
11473
11474/** Returns the currently programmed mode of the given pipe. */
11475struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
11476 struct drm_crtc *crtc)
11477{
Chris Wilsonfac5e232016-07-04 11:34:36 +010011478 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011479 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011480 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080011481 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011482 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020011483 int htot = I915_READ(HTOTAL(cpu_transcoder));
11484 int hsync = I915_READ(HSYNC(cpu_transcoder));
11485 int vtot = I915_READ(VTOTAL(cpu_transcoder));
11486 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030011487 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080011488
11489 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
11490 if (!mode)
11491 return NULL;
11492
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011493 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
11494 if (!pipe_config) {
11495 kfree(mode);
11496 return NULL;
11497 }
11498
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011499 /*
11500 * Construct a pipe_config sufficient for getting the clock info
11501 * back out of crtc_clock_get.
11502 *
11503 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
11504 * to use a real value here instead.
11505 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011506 pipe_config->cpu_transcoder = (enum transcoder) pipe;
11507 pipe_config->pixel_multiplier = 1;
11508 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
11509 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
11510 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
11511 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011512
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011513 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080011514 mode->hdisplay = (htot & 0xffff) + 1;
11515 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
11516 mode->hsync_start = (hsync & 0xffff) + 1;
11517 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
11518 mode->vdisplay = (vtot & 0xffff) + 1;
11519 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
11520 mode->vsync_start = (vsync & 0xffff) + 1;
11521 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
11522
11523 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080011524
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011525 kfree(pipe_config);
11526
Jesse Barnes79e53942008-11-07 14:24:08 -080011527 return mode;
11528}
11529
11530static void intel_crtc_destroy(struct drm_crtc *crtc)
11531{
11532 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011533 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011534 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020011535
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011536 spin_lock_irq(&dev->event_lock);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011537 work = intel_crtc->flip_work;
11538 intel_crtc->flip_work = NULL;
11539 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011540
Daniel Vetter5a21b662016-05-24 17:13:53 +020011541 if (work) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011542 cancel_work_sync(&work->mmio_work);
11543 cancel_work_sync(&work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011544 kfree(work);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011545 }
Jesse Barnes79e53942008-11-07 14:24:08 -080011546
11547 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011548
Jesse Barnes79e53942008-11-07 14:24:08 -080011549 kfree(intel_crtc);
11550}
11551
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011552static void intel_unpin_work_fn(struct work_struct *__work)
11553{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011554 struct intel_flip_work *work =
11555 container_of(__work, struct intel_flip_work, unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011556 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11557 struct drm_device *dev = crtc->base.dev;
11558 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011559
Daniel Vetter5a21b662016-05-24 17:13:53 +020011560 if (is_mmio_work(work))
11561 flush_work(&work->mmio_work);
11562
11563 mutex_lock(&dev->struct_mutex);
11564 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilsonf8c417c2016-07-20 13:31:53 +010011565 i915_gem_object_put(work->pending_flip_obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011566 mutex_unlock(&dev->struct_mutex);
11567
Chris Wilsone8a261e2016-07-20 13:31:49 +010011568 i915_gem_request_put(work->flip_queued_req);
11569
Chris Wilson5748b6a2016-08-04 16:32:38 +010011570 intel_frontbuffer_flip_complete(to_i915(dev),
11571 to_intel_plane(primary)->frontbuffer_bit);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011572 intel_fbc_post_update(crtc);
11573 drm_framebuffer_unreference(work->old_fb);
11574
11575 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11576 atomic_dec(&crtc->unpin_work_count);
11577
11578 kfree(work);
11579}
11580
11581/* Is 'a' after or equal to 'b'? */
11582static bool g4x_flip_count_after_eq(u32 a, u32 b)
11583{
11584 return !((a - b) & 0x80000000);
11585}
11586
11587static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11588 struct intel_flip_work *work)
11589{
11590 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011591 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011592
Chris Wilson8af29b02016-09-09 14:11:47 +010011593 if (abort_flip_on_reset(crtc))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011594 return true;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011595
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011596 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011597 * The relevant registers doen't exist on pre-ctg.
11598 * As the flip done interrupt doesn't trigger for mmio
11599 * flips on gmch platforms, a flip count check isn't
11600 * really needed there. But since ctg has the registers,
11601 * include it in the check anyway.
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011602 */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010011603 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011604 return true;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011605
Daniel Vetter5a21b662016-05-24 17:13:53 +020011606 /*
11607 * BDW signals flip done immediately if the plane
11608 * is disabled, even if the plane enable is already
11609 * armed to occur at the next vblank :(
11610 */
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011611
Daniel Vetter5a21b662016-05-24 17:13:53 +020011612 /*
11613 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11614 * used the same base address. In that case the mmio flip might
11615 * have completed, but the CS hasn't even executed the flip yet.
11616 *
11617 * A flip count check isn't enough as the CS might have updated
11618 * the base address just after start of vblank, but before we
11619 * managed to process the interrupt. This means we'd complete the
11620 * CS flip too soon.
11621 *
11622 * Combining both checks should get us a good enough result. It may
11623 * still happen that the CS flip has been executed, but has not
11624 * yet actually completed. But in case the base address is the same
11625 * anyway, we don't really care.
11626 */
11627 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11628 crtc->flip_work->gtt_offset &&
11629 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11630 crtc->flip_work->flip_count);
11631}
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011632
Daniel Vetter5a21b662016-05-24 17:13:53 +020011633static bool
11634__pageflip_finished_mmio(struct intel_crtc *crtc,
11635 struct intel_flip_work *work)
11636{
11637 /*
11638 * MMIO work completes when vblank is different from
11639 * flip_queued_vblank.
11640 *
11641 * Reset counter value doesn't matter, this is handled by
11642 * i915_wait_request finishing early, so no need to handle
11643 * reset here.
11644 */
11645 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011646}
11647
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011648
11649static bool pageflip_finished(struct intel_crtc *crtc,
11650 struct intel_flip_work *work)
11651{
11652 if (!atomic_read(&work->pending))
11653 return false;
11654
11655 smp_rmb();
11656
Daniel Vetter5a21b662016-05-24 17:13:53 +020011657 if (is_mmio_work(work))
11658 return __pageflip_finished_mmio(crtc, work);
11659 else
11660 return __pageflip_finished_cs(crtc, work);
11661}
11662
11663void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11664{
Chris Wilson91c8a322016-07-05 10:40:23 +010011665 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä98187832016-10-31 22:37:10 +020011666 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011667 struct intel_flip_work *work;
11668 unsigned long flags;
11669
11670 /* Ignore early vblank irqs */
11671 if (!crtc)
11672 return;
11673
Daniel Vetterf3260382014-09-15 14:55:23 +020011674 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011675 * This is called both by irq handlers and the reset code (to complete
11676 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000011677 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011678 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020011679 work = crtc->flip_work;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011680
11681 if (work != NULL &&
11682 !is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020011683 pageflip_finished(crtc, work))
11684 page_flip_completed(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011685
11686 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011687}
11688
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011689void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011690{
Chris Wilson91c8a322016-07-05 10:40:23 +010011691 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä98187832016-10-31 22:37:10 +020011692 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011693 struct intel_flip_work *work;
11694 unsigned long flags;
11695
11696 /* Ignore early vblank irqs */
11697 if (!crtc)
11698 return;
11699
11700 /*
11701 * This is called both by irq handlers and the reset code (to complete
11702 * lost pageflips) so needs the full irqsave spinlocks.
11703 */
11704 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020011705 work = crtc->flip_work;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011706
Daniel Vetter5a21b662016-05-24 17:13:53 +020011707 if (work != NULL &&
11708 is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020011709 pageflip_finished(crtc, work))
11710 page_flip_completed(crtc);
Maarten Lankhorst68858432016-05-17 15:07:52 +020011711
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011712 spin_unlock_irqrestore(&dev->event_lock, flags);
11713}
11714
Daniel Vetter5a21b662016-05-24 17:13:53 +020011715static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11716 struct intel_flip_work *work)
11717{
11718 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
11719
11720 /* Ensure that the work item is consistent when activating it ... */
11721 smp_mb__before_atomic();
11722 atomic_set(&work->pending, 1);
11723}
11724
11725static int intel_gen2_queue_flip(struct drm_device *dev,
11726 struct drm_crtc *crtc,
11727 struct drm_framebuffer *fb,
11728 struct drm_i915_gem_object *obj,
11729 struct drm_i915_gem_request *req,
11730 uint32_t flags)
11731{
Chris Wilson7e37f882016-08-02 22:50:21 +010011732 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11734 u32 flip_mask;
11735 int ret;
11736
11737 ret = intel_ring_begin(req, 6);
11738 if (ret)
11739 return ret;
11740
11741 /* Can't queue multiple flips, so wait for the previous
11742 * one to finish before executing the next.
11743 */
11744 if (intel_crtc->plane)
11745 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11746 else
11747 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011748 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11749 intel_ring_emit(ring, MI_NOOP);
11750 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011751 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011752 intel_ring_emit(ring, fb->pitches[0]);
11753 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11754 intel_ring_emit(ring, 0); /* aux display base address, unused */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011755
11756 return 0;
11757}
11758
11759static int intel_gen3_queue_flip(struct drm_device *dev,
11760 struct drm_crtc *crtc,
11761 struct drm_framebuffer *fb,
11762 struct drm_i915_gem_object *obj,
11763 struct drm_i915_gem_request *req,
11764 uint32_t flags)
11765{
Chris Wilson7e37f882016-08-02 22:50:21 +010011766 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011767 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11768 u32 flip_mask;
11769 int ret;
11770
11771 ret = intel_ring_begin(req, 6);
11772 if (ret)
11773 return ret;
11774
11775 if (intel_crtc->plane)
11776 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11777 else
11778 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011779 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11780 intel_ring_emit(ring, MI_NOOP);
11781 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011782 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011783 intel_ring_emit(ring, fb->pitches[0]);
11784 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11785 intel_ring_emit(ring, MI_NOOP);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011786
11787 return 0;
11788}
11789
11790static int intel_gen4_queue_flip(struct drm_device *dev,
11791 struct drm_crtc *crtc,
11792 struct drm_framebuffer *fb,
11793 struct drm_i915_gem_object *obj,
11794 struct drm_i915_gem_request *req,
11795 uint32_t flags)
11796{
Chris Wilson7e37f882016-08-02 22:50:21 +010011797 struct intel_ring *ring = req->ring;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011798 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011799 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11800 uint32_t pf, pipesrc;
11801 int ret;
11802
11803 ret = intel_ring_begin(req, 4);
11804 if (ret)
11805 return ret;
11806
11807 /* i965+ uses the linear or tiled offsets from the
11808 * Display Registers (which do not change across a page-flip)
11809 * so we need only reprogram the base address.
11810 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010011811 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011812 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011813 intel_ring_emit(ring, fb->pitches[0]);
11814 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
Ville Syrjälä72618eb2016-02-04 20:38:20 +020011815 intel_fb_modifier_to_tiling(fb->modifier[0]));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011816
11817 /* XXX Enabling the panel-fitter across page-flip is so far
11818 * untested on non-native modes, so ignore it for now.
11819 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11820 */
11821 pf = 0;
11822 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011823 intel_ring_emit(ring, pf | pipesrc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011824
11825 return 0;
11826}
11827
11828static int intel_gen6_queue_flip(struct drm_device *dev,
11829 struct drm_crtc *crtc,
11830 struct drm_framebuffer *fb,
11831 struct drm_i915_gem_object *obj,
11832 struct drm_i915_gem_request *req,
11833 uint32_t flags)
11834{
Chris Wilson7e37f882016-08-02 22:50:21 +010011835 struct intel_ring *ring = req->ring;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011836 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011837 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11838 uint32_t pf, pipesrc;
11839 int ret;
11840
11841 ret = intel_ring_begin(req, 4);
11842 if (ret)
11843 return ret;
11844
Chris Wilsonb5321f32016-08-02 22:50:18 +010011845 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011846 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä72618eb2016-02-04 20:38:20 +020011847 intel_ring_emit(ring, fb->pitches[0] |
11848 intel_fb_modifier_to_tiling(fb->modifier[0]));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011849 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011850
11851 /* Contrary to the suggestions in the documentation,
11852 * "Enable Panel Fitter" does not seem to be required when page
11853 * flipping with a non-native mode, and worse causes a normal
11854 * modeset to fail.
11855 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11856 */
11857 pf = 0;
11858 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011859 intel_ring_emit(ring, pf | pipesrc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011860
11861 return 0;
11862}
11863
11864static int intel_gen7_queue_flip(struct drm_device *dev,
11865 struct drm_crtc *crtc,
11866 struct drm_framebuffer *fb,
11867 struct drm_i915_gem_object *obj,
11868 struct drm_i915_gem_request *req,
11869 uint32_t flags)
11870{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011871 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson7e37f882016-08-02 22:50:21 +010011872 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011873 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11874 uint32_t plane_bit = 0;
11875 int len, ret;
11876
11877 switch (intel_crtc->plane) {
11878 case PLANE_A:
11879 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11880 break;
11881 case PLANE_B:
11882 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11883 break;
11884 case PLANE_C:
11885 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11886 break;
11887 default:
11888 WARN_ONCE(1, "unknown plane in flip command\n");
11889 return -ENODEV;
11890 }
11891
11892 len = 4;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011893 if (req->engine->id == RCS) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020011894 len += 6;
11895 /*
11896 * On Gen 8, SRM is now taking an extra dword to accommodate
11897 * 48bits addresses, and we need a NOOP for the batch size to
11898 * stay even.
11899 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011900 if (IS_GEN8(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011901 len += 2;
11902 }
11903
11904 /*
11905 * BSpec MI_DISPLAY_FLIP for IVB:
11906 * "The full packet must be contained within the same cache line."
11907 *
11908 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11909 * cacheline, if we ever start emitting more commands before
11910 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11911 * then do the cacheline alignment, and finally emit the
11912 * MI_DISPLAY_FLIP.
11913 */
11914 ret = intel_ring_cacheline_align(req);
11915 if (ret)
11916 return ret;
11917
11918 ret = intel_ring_begin(req, len);
11919 if (ret)
11920 return ret;
11921
11922 /* Unmask the flip-done completion message. Note that the bspec says that
11923 * we should do this for both the BCS and RCS, and that we must not unmask
11924 * more than one flip event at any time (or ensure that one flip message
11925 * can be sent by waiting for flip-done prior to queueing new flips).
11926 * Experimentation says that BCS works despite DERRMR masking all
11927 * flip-done completion events and that unmasking all planes at once
11928 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11929 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11930 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010011931 if (req->engine->id == RCS) {
11932 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11933 intel_ring_emit_reg(ring, DERRMR);
11934 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011935 DERRMR_PIPEB_PRI_FLIP_DONE |
11936 DERRMR_PIPEC_PRI_FLIP_DONE));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011937 if (IS_GEN8(dev_priv))
Chris Wilsonb5321f32016-08-02 22:50:18 +010011938 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011939 MI_SRM_LRM_GLOBAL_GTT);
11940 else
Chris Wilsonb5321f32016-08-02 22:50:18 +010011941 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011942 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonb5321f32016-08-02 22:50:18 +010011943 intel_ring_emit_reg(ring, DERRMR);
Chris Wilsonbde13eb2016-08-15 10:49:07 +010011944 intel_ring_emit(ring,
11945 i915_ggtt_offset(req->engine->scratch) + 256);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011946 if (IS_GEN8(dev_priv)) {
Chris Wilsonb5321f32016-08-02 22:50:18 +010011947 intel_ring_emit(ring, 0);
11948 intel_ring_emit(ring, MI_NOOP);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011949 }
11950 }
11951
Chris Wilsonb5321f32016-08-02 22:50:18 +010011952 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä72618eb2016-02-04 20:38:20 +020011953 intel_ring_emit(ring, fb->pitches[0] |
11954 intel_fb_modifier_to_tiling(fb->modifier[0]));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011955 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11956 intel_ring_emit(ring, (MI_NOOP));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011957
11958 return 0;
11959}
11960
11961static bool use_mmio_flip(struct intel_engine_cs *engine,
11962 struct drm_i915_gem_object *obj)
11963{
11964 /*
11965 * This is not being used for older platforms, because
11966 * non-availability of flip done interrupt forces us to use
11967 * CS flips. Older platforms derive flip done using some clever
11968 * tricks involving the flip_pending status bits and vblank irqs.
11969 * So using MMIO flips there would disrupt this mechanism.
11970 */
11971
11972 if (engine == NULL)
11973 return true;
11974
11975 if (INTEL_GEN(engine->i915) < 5)
11976 return false;
11977
11978 if (i915.use_mmio_flip < 0)
11979 return false;
11980 else if (i915.use_mmio_flip > 0)
11981 return true;
11982 else if (i915.enable_execlists)
11983 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011984
Chris Wilsond07f0e52016-10-28 13:58:44 +010011985 return engine != i915_gem_object_last_write_engine(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011986}
11987
11988static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11989 unsigned int rotation,
11990 struct intel_flip_work *work)
11991{
11992 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011993 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011994 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11995 const enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd2196772016-01-28 18:33:11 +020011996 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011997
11998 ctl = I915_READ(PLANE_CTL(pipe, 0));
11999 ctl &= ~PLANE_CTL_TILED_MASK;
12000 switch (fb->modifier[0]) {
12001 case DRM_FORMAT_MOD_NONE:
12002 break;
12003 case I915_FORMAT_MOD_X_TILED:
12004 ctl |= PLANE_CTL_TILED_X;
12005 break;
12006 case I915_FORMAT_MOD_Y_TILED:
12007 ctl |= PLANE_CTL_TILED_Y;
12008 break;
12009 case I915_FORMAT_MOD_Yf_TILED:
12010 ctl |= PLANE_CTL_TILED_YF;
12011 break;
12012 default:
12013 MISSING_CASE(fb->modifier[0]);
12014 }
12015
12016 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020012017 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
12018 * PLANE_SURF updates, the update is then guaranteed to be atomic.
12019 */
12020 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
12021 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
12022
12023 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
12024 POSTING_READ(PLANE_SURF(pipe, 0));
12025}
12026
12027static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
12028 struct intel_flip_work *work)
12029{
12030 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012031 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä72618eb2016-02-04 20:38:20 +020012032 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012033 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
12034 u32 dspcntr;
12035
12036 dspcntr = I915_READ(reg);
12037
Ville Syrjälä72618eb2016-02-04 20:38:20 +020012038 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
Daniel Vetter5a21b662016-05-24 17:13:53 +020012039 dspcntr |= DISPPLANE_TILED;
12040 else
12041 dspcntr &= ~DISPPLANE_TILED;
12042
12043 I915_WRITE(reg, dspcntr);
12044
12045 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
12046 POSTING_READ(DSPSURF(intel_crtc->plane));
12047}
12048
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020012049static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000012050{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020012051 struct intel_flip_work *work =
12052 container_of(w, struct intel_flip_work, mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012053 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
12054 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12055 struct intel_framebuffer *intel_fb =
12056 to_intel_framebuffer(crtc->base.primary->fb);
12057 struct drm_i915_gem_object *obj = intel_fb->obj;
12058
Chris Wilsond07f0e52016-10-28 13:58:44 +010012059 WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012060
12061 intel_pipe_update_start(crtc);
12062
12063 if (INTEL_GEN(dev_priv) >= 9)
12064 skl_do_mmio_flip(crtc, work->rotation, work);
12065 else
12066 /* use_mmio_flip() retricts MMIO flips to ilk+ */
12067 ilk_do_mmio_flip(crtc, work);
12068
12069 intel_pipe_update_end(crtc, work);
12070}
12071
12072static int intel_default_queue_flip(struct drm_device *dev,
12073 struct drm_crtc *crtc,
12074 struct drm_framebuffer *fb,
12075 struct drm_i915_gem_object *obj,
12076 struct drm_i915_gem_request *req,
12077 uint32_t flags)
12078{
12079 return -ENODEV;
12080}
12081
12082static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
12083 struct intel_crtc *intel_crtc,
12084 struct intel_flip_work *work)
12085{
12086 u32 addr, vblank;
12087
12088 if (!atomic_read(&work->pending))
12089 return false;
12090
12091 smp_rmb();
12092
12093 vblank = intel_crtc_get_vblank_counter(intel_crtc);
12094 if (work->flip_ready_vblank == 0) {
12095 if (work->flip_queued_req &&
Chris Wilsonf69a02c2016-07-01 17:23:16 +010012096 !i915_gem_request_completed(work->flip_queued_req))
Daniel Vetter5a21b662016-05-24 17:13:53 +020012097 return false;
12098
12099 work->flip_ready_vblank = vblank;
12100 }
12101
12102 if (vblank - work->flip_ready_vblank < 3)
12103 return false;
12104
12105 /* Potential stall - if we see that the flip has happened,
12106 * assume a missed interrupt. */
12107 if (INTEL_GEN(dev_priv) >= 4)
12108 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
12109 else
12110 addr = I915_READ(DSPADDR(intel_crtc->plane));
12111
12112 /* There is a potential issue here with a false positive after a flip
12113 * to the same address. We could address this by checking for a
12114 * non-incrementing frame counter.
12115 */
12116 return addr == work->gtt_offset;
12117}
12118
12119void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
12120{
Chris Wilson91c8a322016-07-05 10:40:23 +010012121 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä98187832016-10-31 22:37:10 +020012122 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012123 struct intel_flip_work *work;
12124
12125 WARN_ON(!in_interrupt());
12126
12127 if (crtc == NULL)
12128 return;
12129
12130 spin_lock(&dev->event_lock);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012131 work = crtc->flip_work;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012132
12133 if (work != NULL && !is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012134 __pageflip_stall_check_cs(dev_priv, crtc, work)) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020012135 WARN_ONCE(1,
12136 "Kicking stuck page flip: queued at %d, now %d\n",
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012137 work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
12138 page_flip_completed(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012139 work = NULL;
12140 }
12141
12142 if (work != NULL && !is_mmio_work(work) &&
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020012143 intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
Daniel Vetter5a21b662016-05-24 17:13:53 +020012144 intel_queue_rps_boost_for_request(work->flip_queued_req);
12145 spin_unlock(&dev->event_lock);
12146}
12147
12148static int intel_crtc_page_flip(struct drm_crtc *crtc,
12149 struct drm_framebuffer *fb,
12150 struct drm_pending_vblank_event *event,
12151 uint32_t page_flip_flags)
12152{
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012153 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012154 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012155 struct drm_framebuffer *old_fb = crtc->primary->fb;
12156 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12157 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12158 struct drm_plane *primary = crtc->primary;
12159 enum pipe pipe = intel_crtc->pipe;
12160 struct intel_flip_work *work;
12161 struct intel_engine_cs *engine;
12162 bool mmio_flip;
Chris Wilson8e637172016-08-02 22:50:26 +010012163 struct drm_i915_gem_request *request;
Chris Wilson058d88c2016-08-15 10:49:06 +010012164 struct i915_vma *vma;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012165 int ret;
Sourab Gupta84c33a62014-06-02 16:47:17 +053012166
Daniel Vetter5a21b662016-05-24 17:13:53 +020012167 /*
12168 * drm_mode_page_flip_ioctl() should already catch this, but double
12169 * check to be safe. In the future we may enable pageflipping from
12170 * a disabled primary plane.
12171 */
12172 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
12173 return -EBUSY;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012174
Daniel Vetter5a21b662016-05-24 17:13:53 +020012175 /* Can't change pixel format via MI display flips. */
12176 if (fb->pixel_format != crtc->primary->fb->pixel_format)
12177 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012178
Daniel Vetter5a21b662016-05-24 17:13:53 +020012179 /*
12180 * TILEOFF/LINOFF registers can't be changed via MI display flips.
12181 * Note that pitch changes could also affect these register.
12182 */
12183 if (INTEL_INFO(dev)->gen > 3 &&
12184 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
12185 fb->pitches[0] != crtc->primary->fb->pitches[0]))
12186 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012187
Daniel Vetter5a21b662016-05-24 17:13:53 +020012188 if (i915_terminally_wedged(&dev_priv->gpu_error))
12189 goto out_hang;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012190
Daniel Vetter5a21b662016-05-24 17:13:53 +020012191 work = kzalloc(sizeof(*work), GFP_KERNEL);
12192 if (work == NULL)
12193 return -ENOMEM;
12194
12195 work->event = event;
12196 work->crtc = crtc;
12197 work->old_fb = old_fb;
12198 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
Sourab Gupta84c33a62014-06-02 16:47:17 +053012199
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012200 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012201 if (ret)
12202 goto free_work;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012203
Daniel Vetter5a21b662016-05-24 17:13:53 +020012204 /* We borrow the event spin lock for protecting flip_work */
12205 spin_lock_irq(&dev->event_lock);
12206 if (intel_crtc->flip_work) {
12207 /* Before declaring the flip queue wedged, check if
12208 * the hardware completed the operation behind our backs.
12209 */
12210 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
12211 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
12212 page_flip_completed(intel_crtc);
12213 } else {
12214 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
12215 spin_unlock_irq(&dev->event_lock);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012216
Daniel Vetter5a21b662016-05-24 17:13:53 +020012217 drm_crtc_vblank_put(crtc);
12218 kfree(work);
12219 return -EBUSY;
12220 }
12221 }
12222 intel_crtc->flip_work = work;
12223 spin_unlock_irq(&dev->event_lock);
Alex Goinsfd8e0582015-11-25 18:43:38 -080012224
Daniel Vetter5a21b662016-05-24 17:13:53 +020012225 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
12226 flush_workqueue(dev_priv->wq);
12227
12228 /* Reference the objects for the scheduled work. */
12229 drm_framebuffer_reference(work->old_fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012230
12231 crtc->primary->fb = fb;
12232 update_state_fb(crtc->primary);
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020012233
Chris Wilson25dc5562016-07-20 13:31:52 +010012234 work->pending_flip_obj = i915_gem_object_get(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012235
12236 ret = i915_mutex_lock_interruptible(dev);
12237 if (ret)
12238 goto cleanup;
12239
Chris Wilson8af29b02016-09-09 14:11:47 +010012240 intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
12241 if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020012242 ret = -EIO;
12243 goto cleanup;
12244 }
12245
12246 atomic_inc(&intel_crtc->unpin_work_count);
12247
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010012248 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +020012249 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
12250
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010012251 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Akash Goel3b3f1652016-10-13 22:44:48 +053012252 engine = dev_priv->engine[BCS];
Ville Syrjälä72618eb2016-02-04 20:38:20 +020012253 if (fb->modifier[0] != old_fb->modifier[0])
Daniel Vetter5a21b662016-05-24 17:13:53 +020012254 /* vlv: DISPLAY_FLIP fails to change tiling */
12255 engine = NULL;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +010012256 } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
Akash Goel3b3f1652016-10-13 22:44:48 +053012257 engine = dev_priv->engine[BCS];
Daniel Vetter5a21b662016-05-24 17:13:53 +020012258 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsond07f0e52016-10-28 13:58:44 +010012259 engine = i915_gem_object_last_write_engine(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012260 if (engine == NULL || engine->id != RCS)
Akash Goel3b3f1652016-10-13 22:44:48 +053012261 engine = dev_priv->engine[BCS];
Daniel Vetter5a21b662016-05-24 17:13:53 +020012262 } else {
Akash Goel3b3f1652016-10-13 22:44:48 +053012263 engine = dev_priv->engine[RCS];
Daniel Vetter5a21b662016-05-24 17:13:53 +020012264 }
12265
12266 mmio_flip = use_mmio_flip(engine, obj);
12267
Chris Wilson058d88c2016-08-15 10:49:06 +010012268 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
12269 if (IS_ERR(vma)) {
12270 ret = PTR_ERR(vma);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012271 goto cleanup_pending;
Chris Wilson058d88c2016-08-15 10:49:06 +010012272 }
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012273
Ville Syrjälä6687c902015-09-15 13:16:41 +030012274 work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012275 work->gtt_offset += intel_crtc->dspaddr_offset;
12276 work->rotation = crtc->primary->state->rotation;
12277
Paulo Zanoni1f0613162016-08-17 16:41:44 -030012278 /*
12279 * There's the potential that the next frame will not be compatible with
12280 * FBC, so we want to call pre_update() before the actual page flip.
12281 * The problem is that pre_update() caches some information about the fb
12282 * object, so we want to do this only after the object is pinned. Let's
12283 * be on the safe side and do this immediately before scheduling the
12284 * flip.
12285 */
12286 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
12287 to_intel_plane_state(primary->state));
12288
Daniel Vetter5a21b662016-05-24 17:13:53 +020012289 if (mmio_flip) {
12290 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
Imre Deak6277c8d2016-09-20 14:58:19 +030012291 queue_work(system_unbound_wq, &work->mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012292 } else {
Chris Wilson8e637172016-08-02 22:50:26 +010012293 request = i915_gem_request_alloc(engine, engine->last_context);
12294 if (IS_ERR(request)) {
12295 ret = PTR_ERR(request);
12296 goto cleanup_unpin;
12297 }
12298
Chris Wilsona2bc4692016-09-09 14:11:56 +010012299 ret = i915_gem_request_await_object(request, obj, false);
Chris Wilson8e637172016-08-02 22:50:26 +010012300 if (ret)
12301 goto cleanup_request;
12302
Daniel Vetter5a21b662016-05-24 17:13:53 +020012303 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
12304 page_flip_flags);
12305 if (ret)
Chris Wilson8e637172016-08-02 22:50:26 +010012306 goto cleanup_request;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012307
12308 intel_mark_page_flip_active(intel_crtc, work);
12309
Chris Wilson8e637172016-08-02 22:50:26 +010012310 work->flip_queued_req = i915_gem_request_get(request);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012311 i915_add_request_no_flush(request);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020012312 }
12313
Daniel Vetter5a21b662016-05-24 17:13:53 +020012314 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
12315 to_intel_plane(primary)->frontbuffer_bit);
12316 mutex_unlock(&dev->struct_mutex);
12317
Chris Wilson5748b6a2016-08-04 16:32:38 +010012318 intel_frontbuffer_flip_prepare(to_i915(dev),
Daniel Vetter5a21b662016-05-24 17:13:53 +020012319 to_intel_plane(primary)->frontbuffer_bit);
12320
12321 trace_i915_flip_request(intel_crtc->plane, obj);
12322
12323 return 0;
12324
Chris Wilson8e637172016-08-02 22:50:26 +010012325cleanup_request:
12326 i915_add_request_no_flush(request);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012327cleanup_unpin:
12328 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
12329cleanup_pending:
Daniel Vetter5a21b662016-05-24 17:13:53 +020012330 atomic_dec(&intel_crtc->unpin_work_count);
12331 mutex_unlock(&dev->struct_mutex);
12332cleanup:
12333 crtc->primary->fb = old_fb;
12334 update_state_fb(crtc->primary);
12335
Chris Wilsonf0cd5182016-10-28 13:58:43 +010012336 i915_gem_object_put(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012337 drm_framebuffer_unreference(work->old_fb);
12338
12339 spin_lock_irq(&dev->event_lock);
12340 intel_crtc->flip_work = NULL;
12341 spin_unlock_irq(&dev->event_lock);
12342
12343 drm_crtc_vblank_put(crtc);
12344free_work:
12345 kfree(work);
12346
12347 if (ret == -EIO) {
12348 struct drm_atomic_state *state;
12349 struct drm_plane_state *plane_state;
12350
12351out_hang:
12352 state = drm_atomic_state_alloc(dev);
12353 if (!state)
12354 return -ENOMEM;
12355 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
12356
12357retry:
12358 plane_state = drm_atomic_get_plane_state(state, primary);
12359 ret = PTR_ERR_OR_ZERO(plane_state);
12360 if (!ret) {
12361 drm_atomic_set_fb_for_plane(plane_state, fb);
12362
12363 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
12364 if (!ret)
12365 ret = drm_atomic_commit(state);
12366 }
12367
12368 if (ret == -EDEADLK) {
12369 drm_modeset_backoff(state->acquire_ctx);
12370 drm_atomic_state_clear(state);
12371 goto retry;
12372 }
12373
Chris Wilson08536952016-10-14 13:18:18 +010012374 drm_atomic_state_put(state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012375
12376 if (ret == 0 && event) {
12377 spin_lock_irq(&dev->event_lock);
12378 drm_crtc_send_vblank_event(crtc, event);
12379 spin_unlock_irq(&dev->event_lock);
12380 }
12381 }
12382 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070012383}
12384
Daniel Vetter5a21b662016-05-24 17:13:53 +020012385
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012386/**
12387 * intel_wm_need_update - Check whether watermarks need updating
12388 * @plane: drm plane
12389 * @state: new plane state
12390 *
12391 * Check current plane state versus the new one to determine whether
12392 * watermarks need to be recalculated.
12393 *
12394 * Returns true or false.
12395 */
12396static bool intel_wm_need_update(struct drm_plane *plane,
12397 struct drm_plane_state *state)
12398{
Matt Roperd21fbe82015-09-24 15:53:12 -070012399 struct intel_plane_state *new = to_intel_plane_state(state);
12400 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
12401
12402 /* Update watermarks on tiling or size changes. */
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012403 if (new->base.visible != cur->base.visible)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012404 return true;
12405
12406 if (!cur->base.fb || !new->base.fb)
12407 return false;
12408
12409 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
12410 cur->base.rotation != new->base.rotation ||
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012411 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
12412 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
12413 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
12414 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012415 return true;
12416
12417 return false;
12418}
12419
Matt Roperd21fbe82015-09-24 15:53:12 -070012420static bool needs_scaling(struct intel_plane_state *state)
12421{
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012422 int src_w = drm_rect_width(&state->base.src) >> 16;
12423 int src_h = drm_rect_height(&state->base.src) >> 16;
12424 int dst_w = drm_rect_width(&state->base.dst);
12425 int dst_h = drm_rect_height(&state->base.dst);
Matt Roperd21fbe82015-09-24 15:53:12 -070012426
12427 return (src_w != dst_w || src_h != dst_h);
12428}
12429
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012430int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
12431 struct drm_plane_state *plane_state)
12432{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010012433 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012434 struct drm_crtc *crtc = crtc_state->crtc;
12435 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12436 struct drm_plane *plane = plane_state->plane;
12437 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080012438 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012439 struct intel_plane_state *old_plane_state =
12440 to_intel_plane_state(plane->state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012441 bool mode_changed = needs_modeset(crtc_state);
12442 bool was_crtc_enabled = crtc->state->active;
12443 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012444 bool turn_off, turn_on, visible, was_visible;
12445 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030012446 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012447
Tvrtko Ursulin55b8f2a2016-10-14 09:17:22 +010012448 if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012449 ret = skl_update_scaler_plane(
12450 to_intel_crtc_state(crtc_state),
12451 to_intel_plane_state(plane_state));
12452 if (ret)
12453 return ret;
12454 }
12455
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012456 was_visible = old_plane_state->base.visible;
12457 visible = to_intel_plane_state(plane_state)->base.visible;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012458
12459 if (!was_crtc_enabled && WARN_ON(was_visible))
12460 was_visible = false;
12461
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010012462 /*
12463 * Visibility is calculated as if the crtc was on, but
12464 * after scaler setup everything depends on it being off
12465 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030012466 *
12467 * FIXME this is wrong for watermarks. Watermarks should also
12468 * be computed as if the pipe would be active. Perhaps move
12469 * per-plane wm computation to the .check_plane() hook, and
12470 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010012471 */
12472 if (!is_crtc_enabled)
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012473 to_intel_plane_state(plane_state)->base.visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012474
12475 if (!was_visible && !visible)
12476 return 0;
12477
Maarten Lankhorste8861672016-02-24 11:24:26 +010012478 if (fb != old_plane_state->base.fb)
12479 pipe_config->fb_changed = true;
12480
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012481 turn_off = was_visible && (!visible || mode_changed);
12482 turn_on = visible && (!was_visible || mode_changed);
12483
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012484 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjälä78108b72016-05-27 20:59:19 +030012485 intel_crtc->base.base.id,
12486 intel_crtc->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012487 plane->base.id, plane->name,
12488 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012489
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012490 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12491 plane->base.id, plane->name,
12492 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012493 turn_off, turn_on, mode_changed);
12494
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012495 if (turn_on) {
12496 pipe_config->update_wm_pre = true;
12497
12498 /* must disable cxsr around plane enable/disable */
12499 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12500 pipe_config->disable_cxsr = true;
12501 } else if (turn_off) {
12502 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012503
Ville Syrjälä852eb002015-06-24 22:00:07 +030012504 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010012505 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010012506 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012507 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012508 /* FIXME bollocks */
12509 pipe_config->update_wm_pre = true;
12510 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012511 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012512
Matt Ropered4a6a72016-02-23 17:20:13 -080012513 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012514 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
12515 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080012516 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12517
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070012518 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010012519 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030012520
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010012521 /*
12522 * WaCxSRDisabledForSpriteScaling:ivb
12523 *
12524 * cstate->update_wm was already set above, so this flag will
12525 * take effect when we commit and program watermarks.
12526 */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +010012527 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010012528 needs_scaling(to_intel_plane_state(plane_state)) &&
12529 !needs_scaling(old_plane_state))
12530 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012531
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012532 return 0;
12533}
12534
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012535static bool encoders_cloneable(const struct intel_encoder *a,
12536 const struct intel_encoder *b)
12537{
12538 /* masks could be asymmetric, so check both ways */
12539 return a == b || (a->cloneable & (1 << b->type) &&
12540 b->cloneable & (1 << a->type));
12541}
12542
12543static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12544 struct intel_crtc *crtc,
12545 struct intel_encoder *encoder)
12546{
12547 struct intel_encoder *source_encoder;
12548 struct drm_connector *connector;
12549 struct drm_connector_state *connector_state;
12550 int i;
12551
12552 for_each_connector_in_state(state, connector, connector_state, i) {
12553 if (connector_state->crtc != &crtc->base)
12554 continue;
12555
12556 source_encoder =
12557 to_intel_encoder(connector_state->best_encoder);
12558 if (!encoders_cloneable(encoder, source_encoder))
12559 return false;
12560 }
12561
12562 return true;
12563}
12564
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012565static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12566 struct drm_crtc_state *crtc_state)
12567{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012568 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012569 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012570 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012571 struct intel_crtc_state *pipe_config =
12572 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012573 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012574 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012575 bool mode_changed = needs_modeset(crtc_state);
12576
Ville Syrjälä852eb002015-06-24 22:00:07 +030012577 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012578 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012579
Maarten Lankhorstad421372015-06-15 12:33:42 +020012580 if (mode_changed && crtc_state->enable &&
12581 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012582 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012583 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12584 pipe_config);
12585 if (ret)
12586 return ret;
12587 }
12588
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012589 if (crtc_state->color_mgmt_changed) {
12590 ret = intel_color_check(crtc, crtc_state);
12591 if (ret)
12592 return ret;
Lionel Landwerline7852a42016-05-25 14:30:41 +010012593
12594 /*
12595 * Changing color management on Intel hardware is
12596 * handled as part of planes update.
12597 */
12598 crtc_state->planes_changed = true;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012599 }
12600
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012601 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012602 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010012603 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080012604 if (ret) {
12605 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012606 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080012607 }
12608 }
12609
12610 if (dev_priv->display.compute_intermediate_wm &&
12611 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12612 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12613 return 0;
12614
12615 /*
12616 * Calculate 'intermediate' watermarks that satisfy both the
12617 * old state and the new state. We can program these
12618 * immediately.
12619 */
12620 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12621 intel_crtc,
12622 pipe_config);
12623 if (ret) {
12624 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12625 return ret;
12626 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070012627 } else if (dev_priv->display.compute_intermediate_wm) {
12628 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12629 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012630 }
12631
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012632 if (INTEL_INFO(dev)->gen >= 9) {
12633 if (mode_changed)
12634 ret = skl_update_scaler_crtc(pipe_config);
12635
12636 if (!ret)
12637 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12638 pipe_config);
12639 }
12640
12641 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012642}
12643
Jani Nikula65b38e02015-04-13 11:26:56 +030012644static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012645 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Daniel Vetter5a21b662016-05-24 17:13:53 +020012646 .atomic_begin = intel_begin_crtc_commit,
12647 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012648 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012649};
12650
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012651static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12652{
12653 struct intel_connector *connector;
12654
12655 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020012656 if (connector->base.state->crtc)
12657 drm_connector_unreference(&connector->base);
12658
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012659 if (connector->base.encoder) {
12660 connector->base.state->best_encoder =
12661 connector->base.encoder;
12662 connector->base.state->crtc =
12663 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020012664
12665 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012666 } else {
12667 connector->base.state->best_encoder = NULL;
12668 connector->base.state->crtc = NULL;
12669 }
12670 }
12671}
12672
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012673static void
Robin Schroereba905b2014-05-18 02:24:50 +020012674connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012675 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012676{
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012677 const struct drm_display_info *info = &connector->base.display_info;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012678 int bpp = pipe_config->pipe_bpp;
12679
12680 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012681 connector->base.base.id,
12682 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012683
12684 /* Don't use an invalid EDID bpc value */
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012685 if (info->bpc != 0 && info->bpc * 3 < bpp) {
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012686 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012687 bpp, info->bpc * 3);
12688 pipe_config->pipe_bpp = info->bpc * 3;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012689 }
12690
Mario Kleiner196f9542016-07-06 12:05:45 +020012691 /* Clamp bpp to 8 on screens without EDID 1.4 */
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012692 if (info->bpc == 0 && bpp > 24) {
Mario Kleiner196f9542016-07-06 12:05:45 +020012693 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
12694 bpp);
12695 pipe_config->pipe_bpp = 24;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012696 }
12697}
12698
12699static int
12700compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012701 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012702{
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010012703 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012704 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012705 struct drm_connector *connector;
12706 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012707 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012708
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010012709 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
12710 IS_CHERRYVIEW(dev_priv)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012711 bpp = 10*3;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010012712 else if (INTEL_GEN(dev_priv) >= 5)
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012713 bpp = 12*3;
12714 else
12715 bpp = 8*3;
12716
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012717
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012718 pipe_config->pipe_bpp = bpp;
12719
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012720 state = pipe_config->base.state;
12721
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012722 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012723 for_each_connector_in_state(state, connector, connector_state, i) {
12724 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012725 continue;
12726
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012727 connected_sink_compute_bpp(to_intel_connector(connector),
12728 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012729 }
12730
12731 return bpp;
12732}
12733
Daniel Vetter644db712013-09-19 14:53:58 +020012734static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12735{
12736 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12737 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012738 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012739 mode->crtc_hdisplay, mode->crtc_hsync_start,
12740 mode->crtc_hsync_end, mode->crtc_htotal,
12741 mode->crtc_vdisplay, mode->crtc_vsync_start,
12742 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12743}
12744
Daniel Vetterc0b03412013-05-28 12:05:54 +020012745static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012746 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012747 const char *context)
12748{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012749 struct drm_device *dev = crtc->base.dev;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012750 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012751 struct drm_plane *plane;
12752 struct intel_plane *intel_plane;
12753 struct intel_plane_state *state;
12754 struct drm_framebuffer *fb;
12755
Ville Syrjälä78108b72016-05-27 20:59:19 +030012756 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12757 crtc->base.base.id, crtc->base.name,
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012758 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012759
Jani Nikulada205632016-03-15 21:51:10 +020012760 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012761 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12762 pipe_config->pipe_bpp, pipe_config->dither);
12763 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12764 pipe_config->has_pch_encoder,
12765 pipe_config->fdi_lanes,
12766 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12767 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12768 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012769 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ä37a56502016-06-22 21:57:04 +030012770 intel_crtc_has_dp_encoder(pipe_config),
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012771 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012772 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12773 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12774 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012775
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012776 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
Ville Syrjälä37a56502016-06-22 21:57:04 +030012777 intel_crtc_has_dp_encoder(pipe_config),
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012778 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012779 pipe_config->dp_m2_n2.gmch_m,
12780 pipe_config->dp_m2_n2.gmch_n,
12781 pipe_config->dp_m2_n2.link_m,
12782 pipe_config->dp_m2_n2.link_n,
12783 pipe_config->dp_m2_n2.tu);
12784
Daniel Vetter55072d12014-11-20 16:10:28 +010012785 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12786 pipe_config->has_audio,
12787 pipe_config->has_infoframe);
12788
Daniel Vetterc0b03412013-05-28 12:05:54 +020012789 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012790 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012791 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012792 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12793 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012794 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012795 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12796 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012797 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12798 crtc->num_scalers,
12799 pipe_config->scaler_state.scaler_users,
12800 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012801 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12802 pipe_config->gmch_pfit.control,
12803 pipe_config->gmch_pfit.pgm_ratios,
12804 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012805 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012806 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012807 pipe_config->pch_pfit.size,
12808 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012809 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012810 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012811
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +010012812 if (IS_BROXTON(dev_priv)) {
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070012813 DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012814 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012815 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012816 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012817 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012818 pipe_config->dpll_hw_state.pll0,
12819 pipe_config->dpll_hw_state.pll1,
12820 pipe_config->dpll_hw_state.pll2,
12821 pipe_config->dpll_hw_state.pll3,
12822 pipe_config->dpll_hw_state.pll6,
12823 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012824 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012825 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012826 pipe_config->dpll_hw_state.pcsdw12);
Tvrtko Ursulin08537232016-10-13 11:03:02 +010012827 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070012828 DRM_DEBUG_KMS("dpll_hw_state: "
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012829 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012830 pipe_config->dpll_hw_state.ctrl1,
12831 pipe_config->dpll_hw_state.cfgcr1,
12832 pipe_config->dpll_hw_state.cfgcr2);
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012833 } else if (HAS_DDI(dev_priv)) {
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070012834 DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012835 pipe_config->dpll_hw_state.wrpll,
12836 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012837 } else {
12838 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12839 "fp0: 0x%x, fp1: 0x%x\n",
12840 pipe_config->dpll_hw_state.dpll,
12841 pipe_config->dpll_hw_state.dpll_md,
12842 pipe_config->dpll_hw_state.fp0,
12843 pipe_config->dpll_hw_state.fp1);
12844 }
12845
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012846 DRM_DEBUG_KMS("planes on this crtc\n");
12847 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Eric Engestromd3828142016-08-15 16:29:55 +010012848 char *format_name;
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012849 intel_plane = to_intel_plane(plane);
12850 if (intel_plane->pipe != crtc->pipe)
12851 continue;
12852
12853 state = to_intel_plane_state(plane->state);
12854 fb = state->base.fb;
12855 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012856 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12857 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012858 continue;
12859 }
12860
Eric Engestrom90844f02016-08-15 01:02:38 +010012861 format_name = drm_get_format_name(fb->pixel_format);
12862
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012863 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12864 plane->base.id, plane->name);
12865 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
Eric Engestrom90844f02016-08-15 01:02:38 +010012866 fb->base.id, fb->width, fb->height, format_name);
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012867 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12868 state->scaler_id,
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012869 state->base.src.x1 >> 16,
12870 state->base.src.y1 >> 16,
12871 drm_rect_width(&state->base.src) >> 16,
12872 drm_rect_height(&state->base.src) >> 16,
12873 state->base.dst.x1, state->base.dst.y1,
12874 drm_rect_width(&state->base.dst),
12875 drm_rect_height(&state->base.dst));
Eric Engestrom90844f02016-08-15 01:02:38 +010012876
12877 kfree(format_name);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012878 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012879}
12880
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012881static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012882{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012883 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012884 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012885 unsigned int used_ports = 0;
Ville Syrjälä477321e2016-07-28 17:50:40 +030012886 unsigned int used_mst_ports = 0;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012887
12888 /*
12889 * Walk the connector list instead of the encoder
12890 * list to detect the problem on ddi platforms
12891 * where there's just one encoder per digital port.
12892 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012893 drm_for_each_connector(connector, dev) {
12894 struct drm_connector_state *connector_state;
12895 struct intel_encoder *encoder;
12896
12897 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12898 if (!connector_state)
12899 connector_state = connector->state;
12900
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012901 if (!connector_state->best_encoder)
12902 continue;
12903
12904 encoder = to_intel_encoder(connector_state->best_encoder);
12905
12906 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012907
12908 switch (encoder->type) {
12909 unsigned int port_mask;
12910 case INTEL_OUTPUT_UNKNOWN:
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012911 if (WARN_ON(!HAS_DDI(to_i915(dev))))
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012912 break;
Ville Syrjäläcca05022016-06-22 21:57:06 +030012913 case INTEL_OUTPUT_DP:
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012914 case INTEL_OUTPUT_HDMI:
12915 case INTEL_OUTPUT_EDP:
12916 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12917
12918 /* the same port mustn't appear more than once */
12919 if (used_ports & port_mask)
12920 return false;
12921
12922 used_ports |= port_mask;
Ville Syrjälä477321e2016-07-28 17:50:40 +030012923 break;
12924 case INTEL_OUTPUT_DP_MST:
12925 used_mst_ports |=
12926 1 << enc_to_mst(&encoder->base)->primary->port;
12927 break;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012928 default:
12929 break;
12930 }
12931 }
12932
Ville Syrjälä477321e2016-07-28 17:50:40 +030012933 /* can't mix MST and SST/HDMI on the same port */
12934 if (used_ports & used_mst_ports)
12935 return false;
12936
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012937 return true;
12938}
12939
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012940static void
12941clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12942{
12943 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012944 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012945 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012946 struct intel_shared_dpll *shared_dpll;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012947 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012948
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012949 /* FIXME: before the switch to atomic started, a new pipe_config was
12950 * kzalloc'd. Code that depends on any field being zero should be
12951 * fixed, so that the crtc_state can be safely duplicated. For now,
12952 * only fields that are know to not cause problems are preserved. */
12953
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012954 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012955 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012956 shared_dpll = crtc_state->shared_dpll;
12957 dpll_hw_state = crtc_state->dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012958 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012959
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012960 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012961
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012962 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012963 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012964 crtc_state->shared_dpll = shared_dpll;
12965 crtc_state->dpll_hw_state = dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012966 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012967}
12968
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012969static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012970intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012971 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012972{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012973 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012974 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012975 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012976 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012977 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012978 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012979 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012980
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012981 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012982
Daniel Vettere143a212013-07-04 12:01:15 +020012983 pipe_config->cpu_transcoder =
12984 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012985
Imre Deak2960bc92013-07-30 13:36:32 +030012986 /*
12987 * Sanitize sync polarity flags based on requested ones. If neither
12988 * positive or negative polarity is requested, treat this as meaning
12989 * negative polarity.
12990 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012991 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012992 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012993 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012994
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012995 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012996 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012997 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012998
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012999 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
13000 pipe_config);
13001 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010013002 goto fail;
13003
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030013004 /*
13005 * Determine the real pipe dimensions. Note that stereo modes can
13006 * increase the actual pipe size due to the frame doubling and
13007 * insertion of additional space for blanks between the frame. This
13008 * is stored in the crtc timings. We use the requested mode to do this
13009 * computation to clearly distinguish it from the adjusted mode, which
13010 * can be changed by the connectors in the below retry loop.
13011 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013012 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080013013 &pipe_config->pipe_src_w,
13014 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030013015
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013016 for_each_connector_in_state(state, connector, connector_state, i) {
13017 if (connector_state->crtc != crtc)
13018 continue;
13019
13020 encoder = to_intel_encoder(connector_state->best_encoder);
13021
Ville Syrjäläe25148d2016-06-22 21:57:09 +030013022 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
13023 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
13024 goto fail;
13025 }
13026
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013027 /*
13028 * Determine output_types before calling the .compute_config()
13029 * hooks so that the hooks can use this information safely.
13030 */
13031 pipe_config->output_types |= 1 << encoder->type;
13032 }
13033
Daniel Vettere29c22c2013-02-21 00:00:16 +010013034encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020013035 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020013036 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020013037 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020013038
Daniel Vetter135c81b2013-07-21 21:37:09 +020013039 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013040 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
13041 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020013042
Daniel Vetter7758a112012-07-08 19:40:39 +020013043 /* Pass our mode to the connectors and the CRTC to give them a chance to
13044 * adjust it according to limitations or connector properties, and also
13045 * a chance to reject the mode entirely.
13046 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030013047 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020013048 if (connector_state->crtc != crtc)
13049 continue;
13050
13051 encoder = to_intel_encoder(connector_state->best_encoder);
13052
Maarten Lankhorst0a478c22016-08-09 17:04:05 +020013053 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
Daniel Vetterefea6e82013-07-21 21:36:59 +020013054 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020013055 goto fail;
13056 }
13057 }
13058
Daniel Vetterff9a6752013-06-01 17:16:21 +020013059 /* Set default port clock if not overwritten by the encoder. Needs to be
13060 * done afterwards in case the encoder adjusts the mode. */
13061 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013062 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010013063 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020013064
Daniel Vettera43f6e02013-06-07 23:10:32 +020013065 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010013066 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020013067 DRM_DEBUG_KMS("CRTC fixup failed\n");
13068 goto fail;
13069 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010013070
13071 if (ret == RETRY) {
13072 if (WARN(!retry, "loop in pipe configuration computation\n")) {
13073 ret = -EINVAL;
13074 goto fail;
13075 }
13076
13077 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
13078 retry = false;
13079 goto encoder_retry;
13080 }
13081
Daniel Vettere8fa4272015-08-12 11:43:34 +020013082 /* Dithering seems to not pass-through bits correctly when it should, so
13083 * only enable it on 6bpc panels. */
13084 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020013085 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020013086 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010013087
Daniel Vetter7758a112012-07-08 19:40:39 +020013088fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030013089 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020013090}
13091
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013092static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013093intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013094{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013095 struct drm_crtc *crtc;
13096 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020013097 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020013098
Ville Syrjälä76688512014-01-10 11:28:06 +020013099 /* Double check state. */
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020013100 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020013101 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020013102
13103 /* Update hwmode for vblank functions */
13104 if (crtc->state->active)
13105 crtc->hwmode = crtc->state->adjusted_mode;
13106 else
13107 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020013108
13109 /*
13110 * Update legacy state to satisfy fbc code. This can
13111 * be removed when fbc uses the atomic state.
13112 */
13113 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
13114 struct drm_plane_state *plane_state = crtc->primary->state;
13115
13116 crtc->primary->fb = plane_state->fb;
13117 crtc->x = plane_state->src_x >> 16;
13118 crtc->y = plane_state->src_y >> 16;
13119 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020013120 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020013121}
13122
Ville Syrjälä3bd26262013-09-06 23:29:02 +030013123static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030013124{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030013125 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030013126
13127 if (clock1 == clock2)
13128 return true;
13129
13130 if (!clock1 || !clock2)
13131 return false;
13132
13133 diff = abs(clock1 - clock2);
13134
13135 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
13136 return true;
13137
13138 return false;
13139}
13140
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013141static bool
13142intel_compare_m_n(unsigned int m, unsigned int n,
13143 unsigned int m2, unsigned int n2,
13144 bool exact)
13145{
13146 if (m == m2 && n == n2)
13147 return true;
13148
13149 if (exact || !m || !n || !m2 || !n2)
13150 return false;
13151
13152 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
13153
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010013154 if (n > n2) {
13155 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013156 m2 <<= 1;
13157 n2 <<= 1;
13158 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010013159 } else if (n < n2) {
13160 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013161 m <<= 1;
13162 n <<= 1;
13163 }
13164 }
13165
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010013166 if (n != n2)
13167 return false;
13168
13169 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013170}
13171
13172static bool
13173intel_compare_link_m_n(const struct intel_link_m_n *m_n,
13174 struct intel_link_m_n *m2_n2,
13175 bool adjust)
13176{
13177 if (m_n->tu == m2_n2->tu &&
13178 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
13179 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
13180 intel_compare_m_n(m_n->link_m, m_n->link_n,
13181 m2_n2->link_m, m2_n2->link_n, !adjust)) {
13182 if (adjust)
13183 *m2_n2 = *m_n;
13184
13185 return true;
13186 }
13187
13188 return false;
13189}
13190
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013191static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020013192intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020013193 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013194 struct intel_crtc_state *pipe_config,
13195 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013196{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010013197 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013198 bool ret = true;
13199
13200#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
13201 do { \
13202 if (!adjust) \
13203 DRM_ERROR(fmt, ##__VA_ARGS__); \
13204 else \
13205 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
13206 } while (0)
13207
Daniel Vetter66e985c2013-06-05 13:34:20 +020013208#define PIPE_CONF_CHECK_X(name) \
13209 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013210 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020013211 "(expected 0x%08x, found 0x%08x)\n", \
13212 current_config->name, \
13213 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013214 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020013215 }
13216
Daniel Vetter08a24032013-04-19 11:25:34 +020013217#define PIPE_CONF_CHECK_I(name) \
13218 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013219 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020013220 "(expected %i, found %i)\n", \
13221 current_config->name, \
13222 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013223 ret = false; \
13224 }
13225
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013226#define PIPE_CONF_CHECK_P(name) \
13227 if (current_config->name != pipe_config->name) { \
13228 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13229 "(expected %p, found %p)\n", \
13230 current_config->name, \
13231 pipe_config->name); \
13232 ret = false; \
13233 }
13234
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013235#define PIPE_CONF_CHECK_M_N(name) \
13236 if (!intel_compare_link_m_n(&current_config->name, \
13237 &pipe_config->name,\
13238 adjust)) { \
13239 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13240 "(expected tu %i gmch %i/%i link %i/%i, " \
13241 "found tu %i, gmch %i/%i link %i/%i)\n", \
13242 current_config->name.tu, \
13243 current_config->name.gmch_m, \
13244 current_config->name.gmch_n, \
13245 current_config->name.link_m, \
13246 current_config->name.link_n, \
13247 pipe_config->name.tu, \
13248 pipe_config->name.gmch_m, \
13249 pipe_config->name.gmch_n, \
13250 pipe_config->name.link_m, \
13251 pipe_config->name.link_n); \
13252 ret = false; \
13253 }
13254
Daniel Vetter55c561a2016-03-30 11:34:36 +020013255/* This is required for BDW+ where there is only one set of registers for
13256 * switching between high and low RR.
13257 * This macro can be used whenever a comparison has to be made between one
13258 * hw state and multiple sw state variables.
13259 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013260#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
13261 if (!intel_compare_link_m_n(&current_config->name, \
13262 &pipe_config->name, adjust) && \
13263 !intel_compare_link_m_n(&current_config->alt_name, \
13264 &pipe_config->name, adjust)) { \
13265 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13266 "(expected tu %i gmch %i/%i link %i/%i, " \
13267 "or tu %i gmch %i/%i link %i/%i, " \
13268 "found tu %i, gmch %i/%i link %i/%i)\n", \
13269 current_config->name.tu, \
13270 current_config->name.gmch_m, \
13271 current_config->name.gmch_n, \
13272 current_config->name.link_m, \
13273 current_config->name.link_n, \
13274 current_config->alt_name.tu, \
13275 current_config->alt_name.gmch_m, \
13276 current_config->alt_name.gmch_n, \
13277 current_config->alt_name.link_m, \
13278 current_config->alt_name.link_n, \
13279 pipe_config->name.tu, \
13280 pipe_config->name.gmch_m, \
13281 pipe_config->name.gmch_n, \
13282 pipe_config->name.link_m, \
13283 pipe_config->name.link_n); \
13284 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010013285 }
13286
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013287#define PIPE_CONF_CHECK_FLAGS(name, mask) \
13288 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013289 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013290 "(expected %i, found %i)\n", \
13291 current_config->name & (mask), \
13292 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013293 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013294 }
13295
Ville Syrjälä5e550652013-09-06 23:29:07 +030013296#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
13297 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013298 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030013299 "(expected %i, found %i)\n", \
13300 current_config->name, \
13301 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013302 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030013303 }
13304
Daniel Vetterbb760062013-06-06 14:55:52 +020013305#define PIPE_CONF_QUIRK(quirk) \
13306 ((current_config->quirks | pipe_config->quirks) & (quirk))
13307
Daniel Vettereccb1402013-05-22 00:50:22 +020013308 PIPE_CONF_CHECK_I(cpu_transcoder);
13309
Daniel Vetter08a24032013-04-19 11:25:34 +020013310 PIPE_CONF_CHECK_I(has_pch_encoder);
13311 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013312 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020013313
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030013314 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030013315 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070013316
13317 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013318 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070013319
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013320 if (current_config->has_drrs)
13321 PIPE_CONF_CHECK_M_N(dp_m2_n2);
13322 } else
13323 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030013324
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013325 PIPE_CONF_CHECK_X(output_types);
Jani Nikulaa65347b2015-11-27 12:21:46 +020013326
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013327 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
13328 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
13329 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
13330 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
13331 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
13332 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013333
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013334 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
13335 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
13336 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
13337 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
13338 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
13339 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013340
Daniel Vetterc93f54c2013-06-27 19:47:19 +020013341 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020013342 PIPE_CONF_CHECK_I(has_hdmi_sink);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010013343 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010013344 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020013345 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080013346 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020013347
Daniel Vetter9ed109a2014-04-24 23:54:52 +020013348 PIPE_CONF_CHECK_I(has_audio);
13349
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013350 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013351 DRM_MODE_FLAG_INTERLACE);
13352
Daniel Vetterbb760062013-06-06 14:55:52 +020013353 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013354 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013355 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013356 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013357 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013358 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013359 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013360 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013361 DRM_MODE_FLAG_NVSYNC);
13362 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070013363
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030013364 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020013365 /* pfit ratios are autocomputed by the hw on gen4+ */
13366 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020013367 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030013368 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020013369
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013370 if (!adjust) {
13371 PIPE_CONF_CHECK_I(pipe_src_w);
13372 PIPE_CONF_CHECK_I(pipe_src_h);
13373
13374 PIPE_CONF_CHECK_I(pch_pfit.enabled);
13375 if (current_config->pch_pfit.enabled) {
13376 PIPE_CONF_CHECK_X(pch_pfit.pos);
13377 PIPE_CONF_CHECK_X(pch_pfit.size);
13378 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020013379
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020013380 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
13381 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070013382
Jesse Barnese59150d2014-01-07 13:30:45 -080013383 /* BDW+ don't expose a synchronous way to read the state */
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010013384 if (IS_HASWELL(dev_priv))
Jesse Barnese59150d2014-01-07 13:30:45 -080013385 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030013386
Ville Syrjälä282740f2013-09-04 18:30:03 +030013387 PIPE_CONF_CHECK_I(double_wide);
13388
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013389 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020013390 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020013391 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020013392 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
13393 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030013394 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010013395 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000013396 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
13397 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
13398 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020013399
Ville Syrjälä47eacba2016-04-12 22:14:35 +030013400 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
13401 PIPE_CONF_CHECK_X(dsi_pll.div);
13402
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010013403 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
Ville Syrjälä42571ae2013-09-06 23:29:00 +030013404 PIPE_CONF_CHECK_I(pipe_bpp);
13405
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013406 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080013407 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030013408
Daniel Vetter66e985c2013-06-05 13:34:20 +020013409#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020013410#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013411#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013412#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030013413#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020013414#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013415#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020013416
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013417 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013418}
13419
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020013420static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
13421 const struct intel_crtc_state *pipe_config)
13422{
13423 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020013424 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020013425 &pipe_config->fdi_m_n);
13426 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
13427
13428 /*
13429 * FDI already provided one idea for the dotclock.
13430 * Yell if the encoder disagrees.
13431 */
13432 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
13433 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
13434 fdi_dotclock, dotclock);
13435 }
13436}
13437
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013438static void verify_wm_state(struct drm_crtc *crtc,
13439 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000013440{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013441 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010013442 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau08db6652014-11-04 17:06:52 +000013443 struct skl_ddb_allocation hw_ddb, *sw_ddb;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013444 struct skl_pipe_wm hw_wm, *sw_wm;
13445 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
13446 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013447 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13448 const enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013449 int plane, level, max_level = ilk_wm_max_level(dev_priv);
Damien Lespiau08db6652014-11-04 17:06:52 +000013450
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013451 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000013452 return;
13453
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013454 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +020013455 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013456
Damien Lespiau08db6652014-11-04 17:06:52 +000013457 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
13458 sw_ddb = &dev_priv->wm.skl_hw.ddb;
13459
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013460 /* planes */
Matt Roper8b364b42016-10-26 15:51:28 -070013461 for_each_universal_plane(dev_priv, pipe, plane) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013462 hw_plane_wm = &hw_wm.planes[plane];
13463 sw_plane_wm = &sw_wm->planes[plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000013464
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013465 /* Watermarks */
13466 for (level = 0; level <= max_level; level++) {
13467 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
13468 &sw_plane_wm->wm[level]))
13469 continue;
Damien Lespiau08db6652014-11-04 17:06:52 +000013470
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013471 DRM_ERROR("mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13472 pipe_name(pipe), plane + 1, level,
13473 sw_plane_wm->wm[level].plane_en,
13474 sw_plane_wm->wm[level].plane_res_b,
13475 sw_plane_wm->wm[level].plane_res_l,
13476 hw_plane_wm->wm[level].plane_en,
13477 hw_plane_wm->wm[level].plane_res_b,
13478 hw_plane_wm->wm[level].plane_res_l);
13479 }
13480
13481 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
13482 &sw_plane_wm->trans_wm)) {
13483 DRM_ERROR("mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13484 pipe_name(pipe), plane + 1,
13485 sw_plane_wm->trans_wm.plane_en,
13486 sw_plane_wm->trans_wm.plane_res_b,
13487 sw_plane_wm->trans_wm.plane_res_l,
13488 hw_plane_wm->trans_wm.plane_en,
13489 hw_plane_wm->trans_wm.plane_res_b,
13490 hw_plane_wm->trans_wm.plane_res_l);
13491 }
13492
13493 /* DDB */
13494 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
13495 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
13496
13497 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040013498 DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013499 pipe_name(pipe), plane + 1,
13500 sw_ddb_entry->start, sw_ddb_entry->end,
13501 hw_ddb_entry->start, hw_ddb_entry->end);
13502 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013503 }
13504
Lyude27082492016-08-24 07:48:10 +020013505 /*
13506 * cursor
13507 * If the cursor plane isn't active, we may not have updated it's ddb
13508 * allocation. In that case since the ddb allocation will be updated
13509 * once the plane becomes visible, we can skip this check
13510 */
13511 if (intel_crtc->cursor_addr) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013512 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
13513 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013514
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013515 /* Watermarks */
13516 for (level = 0; level <= max_level; level++) {
13517 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
13518 &sw_plane_wm->wm[level]))
13519 continue;
13520
13521 DRM_ERROR("mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13522 pipe_name(pipe), level,
13523 sw_plane_wm->wm[level].plane_en,
13524 sw_plane_wm->wm[level].plane_res_b,
13525 sw_plane_wm->wm[level].plane_res_l,
13526 hw_plane_wm->wm[level].plane_en,
13527 hw_plane_wm->wm[level].plane_res_b,
13528 hw_plane_wm->wm[level].plane_res_l);
13529 }
13530
13531 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
13532 &sw_plane_wm->trans_wm)) {
13533 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13534 pipe_name(pipe),
13535 sw_plane_wm->trans_wm.plane_en,
13536 sw_plane_wm->trans_wm.plane_res_b,
13537 sw_plane_wm->trans_wm.plane_res_l,
13538 hw_plane_wm->trans_wm.plane_en,
13539 hw_plane_wm->trans_wm.plane_res_b,
13540 hw_plane_wm->trans_wm.plane_res_l);
13541 }
13542
13543 /* DDB */
13544 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
13545 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
13546
13547 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040013548 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
Lyude27082492016-08-24 07:48:10 +020013549 pipe_name(pipe),
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013550 sw_ddb_entry->start, sw_ddb_entry->end,
13551 hw_ddb_entry->start, hw_ddb_entry->end);
Lyude27082492016-08-24 07:48:10 +020013552 }
Damien Lespiau08db6652014-11-04 17:06:52 +000013553 }
13554}
13555
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013556static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013557verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013558{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013559 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013560
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013561 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013562 struct drm_encoder *encoder = connector->encoder;
13563 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013564
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013565 if (state->crtc != crtc)
13566 continue;
13567
Daniel Vetter5a21b662016-05-24 17:13:53 +020013568 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013569
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013570 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013571 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013572 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013573}
13574
13575static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013576verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013577{
13578 struct intel_encoder *encoder;
13579 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013580
Damien Lespiaub2784e12014-08-05 11:29:37 +010013581 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013582 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013583 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013584
13585 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13586 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013587 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013588
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013589 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013590 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013591 continue;
13592 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013593
13594 I915_STATE_WARN(connector->base.state->crtc !=
13595 encoder->base.crtc,
13596 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013597 }
Dave Airlie0e32b392014-05-02 14:02:48 +100013598
Rob Clarke2c719b2014-12-15 13:56:32 -050013599 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013600 "encoder's enabled state mismatch "
13601 "(expected %i, found %i)\n",
13602 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013603
13604 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013605 bool active;
13606
13607 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013608 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013609 "encoder detached but still enabled on pipe %c.\n",
13610 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013611 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013612 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013613}
13614
13615static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013616verify_crtc_state(struct drm_crtc *crtc,
13617 struct drm_crtc_state *old_crtc_state,
13618 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013619{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013620 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010013621 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013622 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13624 struct intel_crtc_state *pipe_config, *sw_config;
13625 struct drm_atomic_state *old_state;
13626 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013627
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013628 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020013629 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013630 pipe_config = to_intel_crtc_state(old_crtc_state);
13631 memset(pipe_config, 0, sizeof(*pipe_config));
13632 pipe_config->base.crtc = crtc;
13633 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013634
Ville Syrjälä78108b72016-05-27 20:59:19 +030013635 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013636
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013637 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013638
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013639 /* hw state is inconsistent with the pipe quirk */
13640 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13641 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13642 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013643
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013644 I915_STATE_WARN(new_crtc_state->active != active,
13645 "crtc active state doesn't match with hw state "
13646 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013647
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013648 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13649 "transitional active state does not match atomic hw state "
13650 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013651
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013652 for_each_encoder_on_crtc(dev, crtc, encoder) {
13653 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013654
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013655 active = encoder->get_hw_state(encoder, &pipe);
13656 I915_STATE_WARN(active != new_crtc_state->active,
13657 "[ENCODER:%i] active %i with crtc active %i\n",
13658 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013659
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013660 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13661 "Encoder connected to wrong pipe %c\n",
13662 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013663
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013664 if (active) {
13665 pipe_config->output_types |= 1 << encoder->type;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013666 encoder->get_config(encoder, pipe_config);
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013667 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013668 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013669
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013670 if (!new_crtc_state->active)
13671 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013672
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013673 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013674
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013675 sw_config = to_intel_crtc_state(crtc->state);
13676 if (!intel_pipe_config_compare(dev, sw_config,
13677 pipe_config, false)) {
13678 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13679 intel_dump_pipe_config(intel_crtc, pipe_config,
13680 "[hw state]");
13681 intel_dump_pipe_config(intel_crtc, sw_config,
13682 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013683 }
13684}
13685
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013686static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013687verify_single_dpll_state(struct drm_i915_private *dev_priv,
13688 struct intel_shared_dpll *pll,
13689 struct drm_crtc *crtc,
13690 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013691{
13692 struct intel_dpll_hw_state dpll_hw_state;
13693 unsigned crtc_mask;
13694 bool active;
13695
13696 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13697
13698 DRM_DEBUG_KMS("%s\n", pll->name);
13699
13700 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
13701
13702 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13703 I915_STATE_WARN(!pll->on && pll->active_mask,
13704 "pll in active use but not on in sw tracking\n");
13705 I915_STATE_WARN(pll->on && !pll->active_mask,
13706 "pll is on but not used by any active crtc\n");
13707 I915_STATE_WARN(pll->on != active,
13708 "pll on state mismatch (expected %i, found %i)\n",
13709 pll->on, active);
13710 }
13711
13712 if (!crtc) {
13713 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13714 "more active pll users than references: %x vs %x\n",
13715 pll->active_mask, pll->config.crtc_mask);
13716
13717 return;
13718 }
13719
13720 crtc_mask = 1 << drm_crtc_index(crtc);
13721
13722 if (new_state->active)
13723 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13724 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13725 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13726 else
13727 I915_STATE_WARN(pll->active_mask & crtc_mask,
13728 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13729 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13730
13731 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13732 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13733 crtc_mask, pll->config.crtc_mask);
13734
13735 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13736 &dpll_hw_state,
13737 sizeof(dpll_hw_state)),
13738 "pll hw state mismatch\n");
13739}
13740
13741static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013742verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13743 struct drm_crtc_state *old_crtc_state,
13744 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013745{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013746 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013747 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13748 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13749
13750 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013751 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013752
13753 if (old_state->shared_dpll &&
13754 old_state->shared_dpll != new_state->shared_dpll) {
13755 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13756 struct intel_shared_dpll *pll = old_state->shared_dpll;
13757
13758 I915_STATE_WARN(pll->active_mask & crtc_mask,
13759 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13760 pipe_name(drm_crtc_index(crtc)));
13761 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13762 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13763 pipe_name(drm_crtc_index(crtc)));
13764 }
13765}
13766
13767static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013768intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013769 struct drm_crtc_state *old_state,
13770 struct drm_crtc_state *new_state)
13771{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013772 if (!needs_modeset(new_state) &&
13773 !to_intel_crtc_state(new_state)->update_pipe)
13774 return;
13775
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013776 verify_wm_state(crtc, new_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013777 verify_connector_state(crtc->dev, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013778 verify_crtc_state(crtc, old_state, new_state);
13779 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013780}
13781
13782static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013783verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013784{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013785 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013786 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013787
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013788 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013789 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013790}
Daniel Vetter53589012013-06-05 13:34:16 +020013791
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013792static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013793intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013794{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013795 verify_encoder_state(dev);
13796 verify_connector_state(dev, NULL);
13797 verify_disabled_dpll_state(dev);
Daniel Vetter25c5b262012-07-08 22:08:04 +020013798}
13799
Ville Syrjälä80715b22014-05-15 20:23:23 +030013800static void update_scanline_offset(struct intel_crtc *crtc)
13801{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013802 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013803
13804 /*
13805 * The scanline counter increments at the leading edge of hsync.
13806 *
13807 * On most platforms it starts counting from vtotal-1 on the
13808 * first active line. That means the scanline counter value is
13809 * always one less than what we would expect. Ie. just after
13810 * start of vblank, which also occurs at start of hsync (on the
13811 * last active line), the scanline counter will read vblank_start-1.
13812 *
13813 * On gen2 the scanline counter starts counting from 1 instead
13814 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13815 * to keep the value positive), instead of adding one.
13816 *
13817 * On HSW+ the behaviour of the scanline counter depends on the output
13818 * type. For DP ports it behaves like most other platforms, but on HDMI
13819 * there's an extra 1 line difference. So we need to add two instead of
13820 * one to the value.
13821 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013822 if (IS_GEN2(dev_priv)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013823 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013824 int vtotal;
13825
Ville Syrjälä124abe02015-09-08 13:40:45 +030013826 vtotal = adjusted_mode->crtc_vtotal;
13827 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013828 vtotal /= 2;
13829
13830 crtc->scanline_offset = vtotal - 1;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013831 } else if (HAS_DDI(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +030013832 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013833 crtc->scanline_offset = 2;
13834 } else
13835 crtc->scanline_offset = 1;
13836}
13837
Maarten Lankhorstad421372015-06-15 12:33:42 +020013838static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013839{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013840 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013841 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013842 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013843 struct drm_crtc *crtc;
13844 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013845 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013846
13847 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013848 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013849
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013850 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013851 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013852 struct intel_shared_dpll *old_dpll =
13853 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013854
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013855 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013856 continue;
13857
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013858 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013859
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013860 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013861 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013862
Maarten Lankhorstad421372015-06-15 12:33:42 +020013863 if (!shared_dpll)
13864 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13865
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013866 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013867 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013868}
13869
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013870/*
13871 * This implements the workaround described in the "notes" section of the mode
13872 * set sequence documentation. When going from no pipes or single pipe to
13873 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13874 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13875 */
13876static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13877{
13878 struct drm_crtc_state *crtc_state;
13879 struct intel_crtc *intel_crtc;
13880 struct drm_crtc *crtc;
13881 struct intel_crtc_state *first_crtc_state = NULL;
13882 struct intel_crtc_state *other_crtc_state = NULL;
13883 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13884 int i;
13885
13886 /* look at all crtc's that are going to be enabled in during modeset */
13887 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13888 intel_crtc = to_intel_crtc(crtc);
13889
13890 if (!crtc_state->active || !needs_modeset(crtc_state))
13891 continue;
13892
13893 if (first_crtc_state) {
13894 other_crtc_state = to_intel_crtc_state(crtc_state);
13895 break;
13896 } else {
13897 first_crtc_state = to_intel_crtc_state(crtc_state);
13898 first_pipe = intel_crtc->pipe;
13899 }
13900 }
13901
13902 /* No workaround needed? */
13903 if (!first_crtc_state)
13904 return 0;
13905
13906 /* w/a possibly needed, check how many crtc's are already enabled. */
13907 for_each_intel_crtc(state->dev, intel_crtc) {
13908 struct intel_crtc_state *pipe_config;
13909
13910 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13911 if (IS_ERR(pipe_config))
13912 return PTR_ERR(pipe_config);
13913
13914 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13915
13916 if (!pipe_config->base.active ||
13917 needs_modeset(&pipe_config->base))
13918 continue;
13919
13920 /* 2 or more enabled crtcs means no need for w/a */
13921 if (enabled_pipe != INVALID_PIPE)
13922 return 0;
13923
13924 enabled_pipe = intel_crtc->pipe;
13925 }
13926
13927 if (enabled_pipe != INVALID_PIPE)
13928 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13929 else if (other_crtc_state)
13930 other_crtc_state->hsw_workaround_pipe = first_pipe;
13931
13932 return 0;
13933}
13934
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013935static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13936{
13937 struct drm_crtc *crtc;
13938 struct drm_crtc_state *crtc_state;
13939 int ret = 0;
13940
13941 /* add all active pipes to the state */
13942 for_each_crtc(state->dev, crtc) {
13943 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13944 if (IS_ERR(crtc_state))
13945 return PTR_ERR(crtc_state);
13946
13947 if (!crtc_state->active || needs_modeset(crtc_state))
13948 continue;
13949
13950 crtc_state->mode_changed = true;
13951
13952 ret = drm_atomic_add_affected_connectors(state, crtc);
13953 if (ret)
13954 break;
13955
13956 ret = drm_atomic_add_affected_planes(state, crtc);
13957 if (ret)
13958 break;
13959 }
13960
13961 return ret;
13962}
13963
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013964static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013965{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013966 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013967 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013968 struct drm_crtc *crtc;
13969 struct drm_crtc_state *crtc_state;
13970 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013971
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013972 if (!check_digital_port_conflicts(state)) {
13973 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13974 return -EINVAL;
13975 }
13976
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013977 intel_state->modeset = true;
13978 intel_state->active_crtcs = dev_priv->active_crtcs;
13979
13980 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13981 if (crtc_state->active)
13982 intel_state->active_crtcs |= 1 << i;
13983 else
13984 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070013985
13986 if (crtc_state->active != crtc->state->active)
13987 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013988 }
13989
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013990 /*
13991 * See if the config requires any additional preparation, e.g.
13992 * to adjust global state with pipes off. We need to do this
13993 * here so we can get the modeset_pipe updated config for the new
13994 * mode set on this crtc. For other crtcs we need to use the
13995 * adjusted_mode bits in the crtc directly.
13996 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013997 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030013998 if (!intel_state->cdclk_pll_vco)
Ville Syrjälä63911d72016-05-13 23:41:32 +030013999 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +030014000 if (!intel_state->cdclk_pll_vco)
14001 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014002
Clint Taylorc89e39f2016-05-13 23:41:21 +030014003 ret = dev_priv->display.modeset_calc_cdclk(state);
14004 if (ret < 0)
14005 return ret;
14006
14007 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030014008 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014009 ret = intel_modeset_all_pipes(state);
14010
14011 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030014012 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010014013
14014 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
14015 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014016 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010014017 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030014018
Maarten Lankhorstad421372015-06-15 12:33:42 +020014019 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030014020
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014021 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020014022 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020014023
Maarten Lankhorstad421372015-06-15 12:33:42 +020014024 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030014025}
14026
Matt Roperaa363132015-09-24 15:53:18 -070014027/*
14028 * Handle calculation of various watermark data at the end of the atomic check
14029 * phase. The code here should be run after the per-crtc and per-plane 'check'
14030 * handlers to ensure that all derived state has been updated.
14031 */
Matt Roper55994c22016-05-12 07:06:08 -070014032static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070014033{
14034 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070014035 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070014036
14037 /* Is there platform-specific watermark information to calculate? */
14038 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070014039 return dev_priv->display.compute_global_watermarks(state);
14040
14041 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070014042}
14043
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014044/**
14045 * intel_atomic_check - validate state object
14046 * @dev: drm device
14047 * @state: state to validate
14048 */
14049static int intel_atomic_check(struct drm_device *dev,
14050 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020014051{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020014052 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070014053 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014054 struct drm_crtc *crtc;
14055 struct drm_crtc_state *crtc_state;
14056 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020014057 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014058
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014059 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020014060 if (ret)
14061 return ret;
14062
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014063 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014064 struct intel_crtc_state *pipe_config =
14065 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020014066
14067 /* Catch I915_MODE_FLAG_INHERITED */
14068 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
14069 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014070
Daniel Vetter26495482015-07-15 14:15:52 +020014071 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014072 continue;
14073
Daniel Vetteraf4a8792016-05-09 09:31:25 +020014074 if (!crtc_state->enable) {
14075 any_ms = true;
14076 continue;
14077 }
14078
Daniel Vetter26495482015-07-15 14:15:52 +020014079 /* FIXME: For only active_changed we shouldn't need to do any
14080 * state recomputation at all. */
14081
Daniel Vetter1ed51de2015-07-15 14:15:51 +020014082 ret = drm_atomic_add_affected_connectors(state, crtc);
14083 if (ret)
14084 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020014085
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014086 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020014087 if (ret) {
14088 intel_dump_pipe_config(to_intel_crtc(crtc),
14089 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014090 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020014091 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014092
Jani Nikula73831232015-11-19 10:26:30 +020014093 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020014094 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014095 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020014096 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020014097 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020014098 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020014099 }
14100
Daniel Vetteraf4a8792016-05-09 09:31:25 +020014101 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020014102 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020014103
Daniel Vetteraf4a8792016-05-09 09:31:25 +020014104 ret = drm_atomic_add_affected_planes(state, crtc);
14105 if (ret)
14106 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014107
Daniel Vetter26495482015-07-15 14:15:52 +020014108 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
14109 needs_modeset(crtc_state) ?
14110 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014111 }
14112
Maarten Lankhorst61333b62015-06-15 12:33:50 +020014113 if (any_ms) {
14114 ret = intel_modeset_checks(state);
14115
14116 if (ret)
14117 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014118 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020014119 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014120
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020014121 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070014122 if (ret)
14123 return ret;
14124
Paulo Zanonif51be2e2016-01-19 11:35:50 -020014125 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070014126 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020014127}
14128
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014129static int intel_atomic_prepare_commit(struct drm_device *dev,
Chris Wilsond07f0e52016-10-28 13:58:44 +010014130 struct drm_atomic_state *state)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014131{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014132 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014133 struct drm_crtc_state *crtc_state;
14134 struct drm_crtc *crtc;
14135 int i, ret;
14136
Daniel Vetter5a21b662016-05-24 17:13:53 +020014137 for_each_crtc_in_state(state, crtc, crtc_state, i) {
14138 if (state->legacy_cursor_update)
14139 continue;
14140
14141 ret = intel_crtc_wait_for_pending_flips(crtc);
14142 if (ret)
14143 return ret;
14144
14145 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
14146 flush_workqueue(dev_priv->wq);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020014147 }
14148
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014149 ret = mutex_lock_interruptible(&dev->struct_mutex);
14150 if (ret)
14151 return ret;
14152
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014153 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010014154 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014155
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014156 return ret;
14157}
14158
Maarten Lankhorsta2991412016-05-17 15:07:48 +020014159u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
14160{
14161 struct drm_device *dev = crtc->base.dev;
14162
14163 if (!dev->max_vblank_count)
14164 return drm_accurate_vblank_count(&crtc->base);
14165
14166 return dev->driver->get_vblank_counter(dev, crtc->pipe);
14167}
14168
Daniel Vetter5a21b662016-05-24 17:13:53 +020014169static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
14170 struct drm_i915_private *dev_priv,
14171 unsigned crtc_mask)
Maarten Lankhorste8861672016-02-24 11:24:26 +010014172{
Daniel Vetter5a21b662016-05-24 17:13:53 +020014173 unsigned last_vblank_count[I915_MAX_PIPES];
14174 enum pipe pipe;
14175 int ret;
Maarten Lankhorste8861672016-02-24 11:24:26 +010014176
Daniel Vetter5a21b662016-05-24 17:13:53 +020014177 if (!crtc_mask)
14178 return;
Maarten Lankhorste8861672016-02-24 11:24:26 +010014179
Daniel Vetter5a21b662016-05-24 17:13:53 +020014180 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä98187832016-10-31 22:37:10 +020014181 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
14182 pipe);
Maarten Lankhorste8861672016-02-24 11:24:26 +010014183
Daniel Vetter5a21b662016-05-24 17:13:53 +020014184 if (!((1 << pipe) & crtc_mask))
Maarten Lankhorste8861672016-02-24 11:24:26 +010014185 continue;
14186
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020014187 ret = drm_crtc_vblank_get(&crtc->base);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014188 if (WARN_ON(ret != 0)) {
14189 crtc_mask &= ~(1 << pipe);
14190 continue;
14191 }
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020014192
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020014193 last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014194 }
14195
14196 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä98187832016-10-31 22:37:10 +020014197 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
14198 pipe);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014199 long lret;
14200
14201 if (!((1 << pipe) & crtc_mask))
14202 continue;
14203
14204 lret = wait_event_timeout(dev->vblank[pipe].queue,
14205 last_vblank_count[pipe] !=
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020014206 drm_crtc_vblank_count(&crtc->base),
Daniel Vetter5a21b662016-05-24 17:13:53 +020014207 msecs_to_jiffies(50));
14208
14209 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
14210
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020014211 drm_crtc_vblank_put(&crtc->base);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020014212 }
14213}
14214
Daniel Vetter5a21b662016-05-24 17:13:53 +020014215static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020014216{
Daniel Vetter5a21b662016-05-24 17:13:53 +020014217 /* fb updated, need to unpin old fb */
14218 if (crtc_state->fb_changed)
14219 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020014220
Daniel Vetter5a21b662016-05-24 17:13:53 +020014221 /* wm changes, need vblank before final wm's */
14222 if (crtc_state->update_wm_post)
14223 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020014224
Daniel Vetter5a21b662016-05-24 17:13:53 +020014225 /*
14226 * cxsr is re-enabled after vblank.
14227 * This is already handled by crtc_state->update_wm_post,
14228 * but added for clarity.
14229 */
14230 if (crtc_state->disable_cxsr)
14231 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020014232
Daniel Vetter5a21b662016-05-24 17:13:53 +020014233 return false;
Maarten Lankhorste8861672016-02-24 11:24:26 +010014234}
14235
Lyude896e5bb2016-08-24 07:48:09 +020014236static void intel_update_crtc(struct drm_crtc *crtc,
14237 struct drm_atomic_state *state,
14238 struct drm_crtc_state *old_crtc_state,
14239 unsigned int *crtc_vblank_mask)
14240{
14241 struct drm_device *dev = crtc->dev;
14242 struct drm_i915_private *dev_priv = to_i915(dev);
14243 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14244 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
14245 bool modeset = needs_modeset(crtc->state);
14246
14247 if (modeset) {
14248 update_scanline_offset(intel_crtc);
14249 dev_priv->display.crtc_enable(pipe_config, state);
14250 } else {
14251 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
14252 }
14253
14254 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
14255 intel_fbc_enable(
14256 intel_crtc, pipe_config,
14257 to_intel_plane_state(crtc->primary->state));
14258 }
14259
14260 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
14261
14262 if (needs_vblank_wait(pipe_config))
14263 *crtc_vblank_mask |= drm_crtc_mask(crtc);
14264}
14265
14266static void intel_update_crtcs(struct drm_atomic_state *state,
14267 unsigned int *crtc_vblank_mask)
14268{
14269 struct drm_crtc *crtc;
14270 struct drm_crtc_state *old_crtc_state;
14271 int i;
14272
14273 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14274 if (!crtc->state->active)
14275 continue;
14276
14277 intel_update_crtc(crtc, state, old_crtc_state,
14278 crtc_vblank_mask);
14279 }
14280}
14281
Lyude27082492016-08-24 07:48:10 +020014282static void skl_update_crtcs(struct drm_atomic_state *state,
14283 unsigned int *crtc_vblank_mask)
14284{
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020014285 struct drm_i915_private *dev_priv = to_i915(state->dev);
Lyude27082492016-08-24 07:48:10 +020014286 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14287 struct drm_crtc *crtc;
Lyudece0ba282016-09-15 10:46:35 -040014288 struct intel_crtc *intel_crtc;
Lyude27082492016-08-24 07:48:10 +020014289 struct drm_crtc_state *old_crtc_state;
Lyudece0ba282016-09-15 10:46:35 -040014290 struct intel_crtc_state *cstate;
Lyude27082492016-08-24 07:48:10 +020014291 unsigned int updated = 0;
14292 bool progress;
14293 enum pipe pipe;
14294
14295 /*
14296 * Whenever the number of active pipes changes, we need to make sure we
14297 * update the pipes in the right order so that their ddb allocations
14298 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
14299 * cause pipe underruns and other bad stuff.
14300 */
14301 do {
14302 int i;
14303 progress = false;
14304
14305 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14306 bool vbl_wait = false;
14307 unsigned int cmask = drm_crtc_mask(crtc);
Lyudece0ba282016-09-15 10:46:35 -040014308
14309 intel_crtc = to_intel_crtc(crtc);
14310 cstate = to_intel_crtc_state(crtc->state);
14311 pipe = intel_crtc->pipe;
Lyude27082492016-08-24 07:48:10 +020014312
14313 if (updated & cmask || !crtc->state->active)
14314 continue;
Lyudece0ba282016-09-15 10:46:35 -040014315 if (skl_ddb_allocation_overlaps(state, intel_crtc))
Lyude27082492016-08-24 07:48:10 +020014316 continue;
14317
14318 updated |= cmask;
14319
14320 /*
14321 * If this is an already active pipe, it's DDB changed,
14322 * and this isn't the last pipe that needs updating
14323 * then we need to wait for a vblank to pass for the
14324 * new ddb allocation to take effect.
14325 */
Lyudece0ba282016-09-15 10:46:35 -040014326 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
14327 &intel_crtc->hw_ddb) &&
Lyude27082492016-08-24 07:48:10 +020014328 !crtc->state->active_changed &&
14329 intel_state->wm_results.dirty_pipes != updated)
14330 vbl_wait = true;
14331
14332 intel_update_crtc(crtc, state, old_crtc_state,
14333 crtc_vblank_mask);
14334
14335 if (vbl_wait)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +020014336 intel_wait_for_vblank(dev_priv, pipe);
Lyude27082492016-08-24 07:48:10 +020014337
14338 progress = true;
14339 }
14340 } while (progress);
14341}
14342
Daniel Vetter94f05022016-06-14 18:01:00 +020014343static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020014344{
Daniel Vetter94f05022016-06-14 18:01:00 +020014345 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014346 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010014347 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014348 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014349 struct drm_crtc *crtc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020014350 struct intel_crtc_state *intel_cstate;
Daniel Vetter5a21b662016-05-24 17:13:53 +020014351 bool hw_check = intel_state->modeset;
14352 unsigned long put_domains[I915_MAX_PIPES] = {};
14353 unsigned crtc_vblank_mask = 0;
Chris Wilsone95433c2016-10-28 13:58:27 +010014354 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020014355
Daniel Vetterea0000f2016-06-13 16:13:46 +020014356 drm_atomic_helper_wait_for_dependencies(state);
14357
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014358 if (intel_state->modeset) {
14359 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
14360 sizeof(intel_state->min_pixclk));
14361 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010014362 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Daniel Vetter5a21b662016-05-24 17:13:53 +020014363
14364 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014365 }
14366
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014367 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020014368 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14369
Daniel Vetter5a21b662016-05-24 17:13:53 +020014370 if (needs_modeset(crtc->state) ||
14371 to_intel_crtc_state(crtc->state)->update_pipe) {
14372 hw_check = true;
14373
14374 put_domains[to_intel_crtc(crtc)->pipe] =
14375 modeset_get_crtc_power_domains(crtc,
14376 to_intel_crtc_state(crtc->state));
14377 }
14378
Maarten Lankhorst61333b62015-06-15 12:33:50 +020014379 if (!needs_modeset(crtc->state))
14380 continue;
14381
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014382 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010014383
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014384 if (old_crtc_state->active) {
14385 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorst4a806552016-08-09 17:04:01 +020014386 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020014387 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020014388 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020014389 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020014390
14391 /*
14392 * Underruns don't always raise
14393 * interrupts, so check manually.
14394 */
14395 intel_check_cpu_fifo_underruns(dev_priv);
14396 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010014397
14398 if (!crtc->state->active)
Ville Syrjälä432081b2016-10-31 22:37:03 +020014399 intel_update_watermarks(intel_crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020014400 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010014401 }
Daniel Vetter7758a112012-07-08 19:40:39 +020014402
Daniel Vetterea9d7582012-07-10 10:42:52 +020014403 /* Only after disabling all output pipelines that will be changed can we
14404 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020014405 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020014406
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014407 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020014408 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010014409
14410 if (dev_priv->display.modeset_commit_cdclk &&
Clint Taylorc89e39f2016-05-13 23:41:21 +030014411 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030014412 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010014413 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010014414
Lyude656d1b82016-08-17 15:55:54 -040014415 /*
14416 * SKL workaround: bspec recommends we disable the SAGV when we
14417 * have more then one pipe enabled
14418 */
Paulo Zanoni56feca92016-09-22 18:00:28 -030014419 if (!intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030014420 intel_disable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040014421
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020014422 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020014423 }
Daniel Vetter47fab732012-10-26 10:58:18 +020014424
Lyude896e5bb2016-08-24 07:48:09 +020014425 /* Complete the events for pipes that have now been disabled */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014426 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020014427 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020014428
Daniel Vetter1f7528c2016-06-13 16:13:45 +020014429 /* Complete events for now disable pipes here. */
14430 if (modeset && !crtc->state->active && crtc->state->event) {
14431 spin_lock_irq(&dev->event_lock);
14432 drm_crtc_send_vblank_event(crtc, crtc->state->event);
14433 spin_unlock_irq(&dev->event_lock);
14434
14435 crtc->state->event = NULL;
14436 }
Matt Ropered4a6a72016-02-23 17:20:13 -080014437 }
14438
Lyude896e5bb2016-08-24 07:48:09 +020014439 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
14440 dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
14441
Daniel Vetter94f05022016-06-14 18:01:00 +020014442 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
14443 * already, but still need the state for the delayed optimization. To
14444 * fix this:
14445 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
14446 * - schedule that vblank worker _before_ calling hw_done
14447 * - at the start of commit_tail, cancel it _synchrously
14448 * - switch over to the vblank wait helper in the core after that since
14449 * we don't need out special handling any more.
14450 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020014451 if (!state->legacy_cursor_update)
14452 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
14453
14454 /*
14455 * Now that the vblank has passed, we can go ahead and program the
14456 * optimal watermarks on platforms that need two-step watermark
14457 * programming.
14458 *
14459 * TODO: Move this (and other cleanup) to an async worker eventually.
14460 */
14461 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14462 intel_cstate = to_intel_crtc_state(crtc->state);
14463
14464 if (dev_priv->display.optimize_watermarks)
14465 dev_priv->display.optimize_watermarks(intel_cstate);
14466 }
14467
14468 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14469 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
14470
14471 if (put_domains[i])
14472 modeset_put_power_domains(dev_priv, put_domains[i]);
14473
14474 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
14475 }
14476
Paulo Zanoni56feca92016-09-22 18:00:28 -030014477 if (intel_state->modeset && intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030014478 intel_enable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040014479
Daniel Vetter94f05022016-06-14 18:01:00 +020014480 drm_atomic_helper_commit_hw_done(state);
14481
Daniel Vetter5a21b662016-05-24 17:13:53 +020014482 if (intel_state->modeset)
14483 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
14484
14485 mutex_lock(&dev->struct_mutex);
14486 drm_atomic_helper_cleanup_planes(dev, state);
14487 mutex_unlock(&dev->struct_mutex);
14488
Daniel Vetterea0000f2016-06-13 16:13:46 +020014489 drm_atomic_helper_commit_cleanup_done(state);
14490
Chris Wilson08536952016-10-14 13:18:18 +010014491 drm_atomic_state_put(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080014492
Mika Kuoppala75714942015-12-16 09:26:48 +020014493 /* As one of the primary mmio accessors, KMS has a high likelihood
14494 * of triggering bugs in unclaimed access. After we finish
14495 * modesetting, see if an error has been flagged, and if so
14496 * enable debugging for the next modeset - and hope we catch
14497 * the culprit.
14498 *
14499 * XXX note that we assume display power is on at this point.
14500 * This might hold true now but we need to add pm helper to check
14501 * unclaimed only when the hardware is on, as atomic commits
14502 * can happen also when the device is completely off.
14503 */
14504 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter94f05022016-06-14 18:01:00 +020014505}
14506
14507static void intel_atomic_commit_work(struct work_struct *work)
14508{
Chris Wilsonc004a902016-10-28 13:58:45 +010014509 struct drm_atomic_state *state =
14510 container_of(work, struct drm_atomic_state, commit_work);
14511
Daniel Vetter94f05022016-06-14 18:01:00 +020014512 intel_atomic_commit_tail(state);
14513}
14514
Chris Wilsonc004a902016-10-28 13:58:45 +010014515static int __i915_sw_fence_call
14516intel_atomic_commit_ready(struct i915_sw_fence *fence,
14517 enum i915_sw_fence_notify notify)
14518{
14519 struct intel_atomic_state *state =
14520 container_of(fence, struct intel_atomic_state, commit_ready);
14521
14522 switch (notify) {
14523 case FENCE_COMPLETE:
14524 if (state->base.commit_work.func)
14525 queue_work(system_unbound_wq, &state->base.commit_work);
14526 break;
14527
14528 case FENCE_FREE:
14529 drm_atomic_state_put(&state->base);
14530 break;
14531 }
14532
14533 return NOTIFY_DONE;
14534}
14535
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014536static void intel_atomic_track_fbs(struct drm_atomic_state *state)
14537{
14538 struct drm_plane_state *old_plane_state;
14539 struct drm_plane *plane;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014540 int i;
14541
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010014542 for_each_plane_in_state(state, plane, old_plane_state, i)
14543 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
14544 intel_fb_obj(plane->state->fb),
14545 to_intel_plane(plane)->frontbuffer_bit);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014546}
14547
Daniel Vetter94f05022016-06-14 18:01:00 +020014548/**
14549 * intel_atomic_commit - commit validated state object
14550 * @dev: DRM device
14551 * @state: the top-level driver state object
14552 * @nonblock: nonblocking commit
14553 *
14554 * This function commits a top-level state object that has been validated
14555 * with drm_atomic_helper_check().
14556 *
14557 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
14558 * nonblocking commits are only safe for pure plane updates. Everything else
14559 * should work though.
14560 *
14561 * RETURNS
14562 * Zero for success or -errno.
14563 */
14564static int intel_atomic_commit(struct drm_device *dev,
14565 struct drm_atomic_state *state,
14566 bool nonblock)
14567{
14568 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010014569 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94f05022016-06-14 18:01:00 +020014570 int ret = 0;
14571
14572 if (intel_state->modeset && nonblock) {
14573 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
14574 return -EINVAL;
14575 }
14576
14577 ret = drm_atomic_helper_setup_commit(state, nonblock);
14578 if (ret)
14579 return ret;
14580
Chris Wilsonc004a902016-10-28 13:58:45 +010014581 drm_atomic_state_get(state);
14582 i915_sw_fence_init(&intel_state->commit_ready,
14583 intel_atomic_commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020014584
Chris Wilsond07f0e52016-10-28 13:58:44 +010014585 ret = intel_atomic_prepare_commit(dev, state);
Daniel Vetter94f05022016-06-14 18:01:00 +020014586 if (ret) {
14587 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Chris Wilsonc004a902016-10-28 13:58:45 +010014588 i915_sw_fence_commit(&intel_state->commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020014589 return ret;
14590 }
14591
14592 drm_atomic_helper_swap_state(state, true);
14593 dev_priv->wm.distrust_bios_wm = false;
14594 dev_priv->wm.skl_results = intel_state->wm_results;
14595 intel_shared_dpll_commit(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014596 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020014597
Chris Wilson08536952016-10-14 13:18:18 +010014598 drm_atomic_state_get(state);
Chris Wilsonc004a902016-10-28 13:58:45 +010014599 INIT_WORK(&state->commit_work,
14600 nonblock ? intel_atomic_commit_work : NULL);
14601
14602 i915_sw_fence_commit(&intel_state->commit_ready);
14603 if (!nonblock) {
14604 i915_sw_fence_wait(&intel_state->commit_ready);
Daniel Vetter94f05022016-06-14 18:01:00 +020014605 intel_atomic_commit_tail(state);
Chris Wilsonc004a902016-10-28 13:58:45 +010014606 }
Mika Kuoppala75714942015-12-16 09:26:48 +020014607
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014608 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020014609}
14610
Chris Wilsonc0c36b942012-12-19 16:08:43 +000014611void intel_crtc_restore_mode(struct drm_crtc *crtc)
14612{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014613 struct drm_device *dev = crtc->dev;
14614 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014615 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030014616 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014617
14618 state = drm_atomic_state_alloc(dev);
14619 if (!state) {
Ville Syrjälä78108b72016-05-27 20:59:19 +030014620 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
14621 crtc->base.id, crtc->name);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014622 return;
14623 }
14624
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014625 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014626
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014627retry:
14628 crtc_state = drm_atomic_get_crtc_state(state, crtc);
14629 ret = PTR_ERR_OR_ZERO(crtc_state);
14630 if (!ret) {
14631 if (!crtc_state->active)
14632 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014633
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014634 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014635 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014636 }
14637
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014638 if (ret == -EDEADLK) {
14639 drm_atomic_state_clear(state);
14640 drm_modeset_backoff(state->acquire_ctx);
14641 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030014642 }
14643
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014644out:
Chris Wilson08536952016-10-14 13:18:18 +010014645 drm_atomic_state_put(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000014646}
14647
Bob Paauwea8784872016-07-15 14:59:02 +010014648/*
14649 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
14650 * drm_atomic_helper_legacy_gamma_set() directly.
14651 */
14652static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
14653 u16 *red, u16 *green, u16 *blue,
14654 uint32_t size)
14655{
14656 struct drm_device *dev = crtc->dev;
14657 struct drm_mode_config *config = &dev->mode_config;
14658 struct drm_crtc_state *state;
14659 int ret;
14660
14661 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
14662 if (ret)
14663 return ret;
14664
14665 /*
14666 * Make sure we update the legacy properties so this works when
14667 * atomic is not enabled.
14668 */
14669
14670 state = crtc->state;
14671
14672 drm_object_property_set_value(&crtc->base,
14673 config->degamma_lut_property,
14674 (state->degamma_lut) ?
14675 state->degamma_lut->base.id : 0);
14676
14677 drm_object_property_set_value(&crtc->base,
14678 config->ctm_property,
14679 (state->ctm) ?
14680 state->ctm->base.id : 0);
14681
14682 drm_object_property_set_value(&crtc->base,
14683 config->gamma_lut_property,
14684 (state->gamma_lut) ?
14685 state->gamma_lut->base.id : 0);
14686
14687 return 0;
14688}
14689
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014690static const struct drm_crtc_funcs intel_crtc_funcs = {
Bob Paauwea8784872016-07-15 14:59:02 +010014691 .gamma_set = intel_atomic_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014692 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000014693 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014694 .destroy = intel_crtc_destroy,
Chris Wilson527b6ab2016-06-24 13:44:03 +010014695 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080014696 .atomic_duplicate_state = intel_crtc_duplicate_state,
14697 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014698};
14699
Matt Roper6beb8c232014-12-01 15:40:14 -080014700/**
14701 * intel_prepare_plane_fb - Prepare fb for usage on plane
14702 * @plane: drm plane to prepare for
14703 * @fb: framebuffer to prepare for presentation
14704 *
14705 * Prepares a framebuffer for usage on a display plane. Generally this
14706 * involves pinning the underlying object and updating the frontbuffer tracking
14707 * bits. Some older platforms need special physical address handling for
14708 * cursor planes.
14709 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014710 * Must be called with struct_mutex held.
14711 *
Matt Roper6beb8c232014-12-01 15:40:14 -080014712 * Returns 0 on success, negative error code on failure.
14713 */
14714int
14715intel_prepare_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010014716 struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070014717{
Chris Wilsonc004a902016-10-28 13:58:45 +010014718 struct intel_atomic_state *intel_state =
14719 to_intel_atomic_state(new_state->state);
Matt Roper465c1202014-05-29 08:06:54 -070014720 struct drm_device *dev = plane->dev;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010014721 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst844f9112015-09-02 10:42:40 +020014722 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080014723 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014724 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc004a902016-10-28 13:58:45 +010014725 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014726
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014727 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070014728 return 0;
14729
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014730 if (old_obj) {
14731 struct drm_crtc_state *crtc_state =
Chris Wilsonc004a902016-10-28 13:58:45 +010014732 drm_atomic_get_existing_crtc_state(new_state->state,
14733 plane->state->crtc);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014734
14735 /* Big Hammer, we also need to ensure that any pending
14736 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14737 * current scanout is retired before unpinning the old
14738 * framebuffer. Note that we rely on userspace rendering
14739 * into the buffer attached to the pipe they are waiting
14740 * on. If not, userspace generates a GPU hang with IPEHR
14741 * point to the MI_WAIT_FOR_EVENT.
14742 *
14743 * This should only fail upon a hung GPU, in which case we
14744 * can safely continue.
14745 */
Chris Wilsonc004a902016-10-28 13:58:45 +010014746 if (needs_modeset(crtc_state)) {
14747 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
14748 old_obj->resv, NULL,
14749 false, 0,
14750 GFP_KERNEL);
14751 if (ret < 0)
14752 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014753 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014754 }
14755
Chris Wilsonc004a902016-10-28 13:58:45 +010014756 if (new_state->fence) { /* explicit fencing */
14757 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
14758 new_state->fence,
14759 I915_FENCE_TIMEOUT,
14760 GFP_KERNEL);
14761 if (ret < 0)
14762 return ret;
14763 }
14764
Chris Wilsonc37efb92016-06-17 08:28:47 +010014765 if (!obj)
14766 return 0;
14767
Chris Wilsonc004a902016-10-28 13:58:45 +010014768 if (!new_state->fence) { /* implicit fencing */
14769 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
14770 obj->resv, NULL,
14771 false, I915_FENCE_TIMEOUT,
14772 GFP_KERNEL);
14773 if (ret < 0)
14774 return ret;
14775 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020014776
Chris Wilsonc37efb92016-06-17 08:28:47 +010014777 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080014778 INTEL_INFO(dev)->cursor_needs_physical) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010014779 int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
Matt Roper6beb8c232014-12-01 15:40:14 -080014780 ret = i915_gem_object_attach_phys(obj, align);
Chris Wilsond07f0e52016-10-28 13:58:44 +010014781 if (ret) {
Matt Roper6beb8c232014-12-01 15:40:14 -080014782 DRM_DEBUG_KMS("failed to attach phys object\n");
Chris Wilsond07f0e52016-10-28 13:58:44 +010014783 return ret;
14784 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014785 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +010014786 struct i915_vma *vma;
14787
14788 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Chris Wilsond07f0e52016-10-28 13:58:44 +010014789 if (IS_ERR(vma)) {
14790 DRM_DEBUG_KMS("failed to pin object\n");
14791 return PTR_ERR(vma);
14792 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014793 }
14794
Chris Wilsond07f0e52016-10-28 13:58:44 +010014795 return 0;
Matt Roper6beb8c232014-12-01 15:40:14 -080014796}
14797
Matt Roper38f3ce32014-12-02 07:45:25 -080014798/**
14799 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14800 * @plane: drm plane to clean up for
14801 * @fb: old framebuffer that was on plane
14802 *
14803 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014804 *
14805 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080014806 */
14807void
14808intel_cleanup_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010014809 struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080014810{
14811 struct drm_device *dev = plane->dev;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014812 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014813 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14814 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080014815
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014816 old_intel_state = to_intel_plane_state(old_state);
14817
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014818 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080014819 return;
14820
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014821 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14822 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020014823 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Matt Roper465c1202014-05-29 08:06:54 -070014824}
14825
Chandra Konduru6156a452015-04-27 13:48:39 -070014826int
14827skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14828{
14829 int max_scale;
Chandra Konduru6156a452015-04-27 13:48:39 -070014830 int crtc_clock, cdclk;
14831
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010014832 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070014833 return DRM_PLANE_HELPER_NO_SCALING;
14834
Chandra Konduru6156a452015-04-27 13:48:39 -070014835 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014836 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070014837
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010014838 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070014839 return DRM_PLANE_HELPER_NO_SCALING;
14840
14841 /*
14842 * skl max scale is lower of:
14843 * close to 3 but not 3, -1 is for that purpose
14844 * or
14845 * cdclk/crtc_clock
14846 */
14847 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14848
14849 return max_scale;
14850}
14851
Matt Roper465c1202014-05-29 08:06:54 -070014852static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014853intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014854 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014855 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014856{
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014857 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Matt Roper2b875c22014-12-01 15:40:13 -080014858 struct drm_crtc *crtc = state->base.crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070014859 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014860 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14861 bool can_position = false;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014862 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014863
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014864 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjälä693bdc22016-01-15 20:46:53 +020014865 /* use scaler when colorkey is not required */
14866 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14867 min_scale = 1;
14868 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14869 }
Sonika Jindald8106362015-04-10 14:37:28 +053014870 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014871 }
Sonika Jindald8106362015-04-10 14:37:28 +053014872
Daniel Vettercc926382016-08-15 10:41:47 +020014873 ret = drm_plane_helper_check_state(&state->base,
14874 &state->clip,
14875 min_scale, max_scale,
14876 can_position, true);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014877 if (ret)
14878 return ret;
14879
Daniel Vettercc926382016-08-15 10:41:47 +020014880 if (!state->base.fb)
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014881 return 0;
14882
14883 if (INTEL_GEN(dev_priv) >= 9) {
14884 ret = skl_check_plane_surface(state);
14885 if (ret)
14886 return ret;
14887 }
14888
14889 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070014890}
14891
Daniel Vetter5a21b662016-05-24 17:13:53 +020014892static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14893 struct drm_crtc_state *old_crtc_state)
14894{
14895 struct drm_device *dev = crtc->dev;
Lyude62e0fb82016-08-22 12:50:08 -040014896 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014897 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Lyudeb707aa52016-09-15 10:56:06 -040014898 struct intel_crtc_state *intel_cstate =
14899 to_intel_crtc_state(crtc->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014900 struct intel_crtc_state *old_intel_state =
14901 to_intel_crtc_state(old_crtc_state);
14902 bool modeset = needs_modeset(crtc->state);
Lyude62e0fb82016-08-22 12:50:08 -040014903 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter5a21b662016-05-24 17:13:53 +020014904
14905 /* Perform vblank evasion around commit operation */
14906 intel_pipe_update_start(intel_crtc);
14907
14908 if (modeset)
14909 return;
14910
14911 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14912 intel_color_set_csc(crtc->state);
14913 intel_color_load_luts(crtc->state);
14914 }
14915
Lyudeb707aa52016-09-15 10:56:06 -040014916 if (intel_cstate->update_pipe) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020014917 intel_update_pipe_config(intel_crtc, old_intel_state);
Lyudeb707aa52016-09-15 10:56:06 -040014918 } else if (INTEL_GEN(dev_priv) >= 9) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020014919 skl_detach_scalers(intel_crtc);
Lyude62e0fb82016-08-22 12:50:08 -040014920
14921 I915_WRITE(PIPE_WM_LINETIME(pipe),
Lyudeb707aa52016-09-15 10:56:06 -040014922 intel_cstate->wm.skl.optimal.linetime);
Lyude62e0fb82016-08-22 12:50:08 -040014923 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020014924}
14925
14926static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14927 struct drm_crtc_state *old_crtc_state)
14928{
14929 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14930
14931 intel_pipe_update_end(intel_crtc, NULL);
14932}
14933
Matt Ropercf4c7c12014-12-04 10:27:42 -080014934/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014935 * intel_plane_destroy - destroy a plane
14936 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014937 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014938 * Common destruction function for all types of planes (primary, cursor,
14939 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014940 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014941void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014942{
Matt Roper465c1202014-05-29 08:06:54 -070014943 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014944 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070014945}
14946
Matt Roper65a3fea2015-01-21 16:35:42 -080014947const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014948 .update_plane = drm_atomic_helper_update_plane,
14949 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014950 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014951 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014952 .atomic_get_property = intel_plane_atomic_get_property,
14953 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014954 .atomic_duplicate_state = intel_plane_duplicate_state,
14955 .atomic_destroy_state = intel_plane_destroy_state,
Matt Roper465c1202014-05-29 08:06:54 -070014956};
14957
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014958static struct intel_plane *
Ville Syrjälä580503c2016-10-31 22:37:00 +020014959intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
Matt Roper465c1202014-05-29 08:06:54 -070014960{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014961 struct intel_plane *primary = NULL;
14962 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014963 const uint32_t *intel_primary_formats;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030014964 unsigned int supported_rotations;
Thierry Reding45e37432015-08-12 16:54:28 +020014965 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014966 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014967
14968 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014969 if (!primary) {
14970 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014971 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014972 }
Matt Roper465c1202014-05-29 08:06:54 -070014973
Matt Roper8e7d6882015-01-21 16:35:41 -080014974 state = intel_create_plane_state(&primary->base);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014975 if (!state) {
14976 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014977 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030014978 }
14979
Matt Roper8e7d6882015-01-21 16:35:41 -080014980 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014981
Matt Roper465c1202014-05-29 08:06:54 -070014982 primary->can_scale = false;
14983 primary->max_downscale = 1;
Ville Syrjälä580503c2016-10-31 22:37:00 +020014984 if (INTEL_GEN(dev_priv) >= 9) {
Chandra Konduru6156a452015-04-27 13:48:39 -070014985 primary->can_scale = true;
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070014986 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014987 }
Matt Roper465c1202014-05-29 08:06:54 -070014988 primary->pipe = pipe;
14989 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014990 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014991 primary->check_plane = intel_check_primary_plane;
Ville Syrjälä580503c2016-10-31 22:37:00 +020014992 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
Matt Roper465c1202014-05-29 08:06:54 -070014993 primary->plane = !pipe;
14994
Ville Syrjälä580503c2016-10-31 22:37:00 +020014995 if (INTEL_GEN(dev_priv) >= 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014996 intel_primary_formats = skl_primary_formats;
14997 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014998
14999 primary->update_plane = skylake_update_primary_plane;
15000 primary->disable_plane = skylake_disable_primary_plane;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010015001 } else if (HAS_PCH_SPLIT(dev_priv)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010015002 intel_primary_formats = i965_primary_formats;
15003 num_formats = ARRAY_SIZE(i965_primary_formats);
15004
15005 primary->update_plane = ironlake_update_primary_plane;
15006 primary->disable_plane = i9xx_disable_primary_plane;
Ville Syrjälä580503c2016-10-31 22:37:00 +020015007 } else if (INTEL_GEN(dev_priv) >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010015008 intel_primary_formats = i965_primary_formats;
15009 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010015010
15011 primary->update_plane = i9xx_update_primary_plane;
15012 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015013 } else {
15014 intel_primary_formats = i8xx_primary_formats;
15015 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010015016
15017 primary->update_plane = i9xx_update_primary_plane;
15018 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070015019 }
15020
Ville Syrjälä580503c2016-10-31 22:37:00 +020015021 if (INTEL_GEN(dev_priv) >= 9)
15022 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15023 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030015024 intel_primary_formats, num_formats,
15025 DRM_PLANE_TYPE_PRIMARY,
15026 "plane 1%c", pipe_name(pipe));
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015027 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä580503c2016-10-31 22:37:00 +020015028 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15029 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030015030 intel_primary_formats, num_formats,
15031 DRM_PLANE_TYPE_PRIMARY,
15032 "primary %c", pipe_name(pipe));
15033 else
Ville Syrjälä580503c2016-10-31 22:37:00 +020015034 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15035 0, &intel_plane_funcs,
Ville Syrjälä38573dc2016-05-27 20:59:23 +030015036 intel_primary_formats, num_formats,
15037 DRM_PLANE_TYPE_PRIMARY,
15038 "plane %c", plane_name(primary->plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015039 if (ret)
15040 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053015041
Dave Airlie5481e272016-10-25 16:36:13 +100015042 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030015043 supported_rotations =
15044 DRM_ROTATE_0 | DRM_ROTATE_90 |
15045 DRM_ROTATE_180 | DRM_ROTATE_270;
Dave Airlie5481e272016-10-25 16:36:13 +100015046 } else if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030015047 supported_rotations =
15048 DRM_ROTATE_0 | DRM_ROTATE_180;
15049 } else {
15050 supported_rotations = DRM_ROTATE_0;
15051 }
15052
Dave Airlie5481e272016-10-25 16:36:13 +100015053 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030015054 drm_plane_create_rotation_property(&primary->base,
15055 DRM_ROTATE_0,
15056 supported_rotations);
Sonika Jindal48404c12014-08-22 14:06:04 +053015057
Matt Roperea2c67b2014-12-23 10:41:52 -080015058 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
15059
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015060 return primary;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015061
15062fail:
15063 kfree(state);
15064 kfree(primary);
15065
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015066 return ERR_PTR(ret);
Matt Roper465c1202014-05-29 08:06:54 -070015067}
15068
Matt Roper3d7d6512014-06-10 08:28:13 -070015069static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030015070intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020015071 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030015072 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070015073{
Matt Roper2b875c22014-12-01 15:40:13 -080015074 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015075 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020015076 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015077 unsigned stride;
15078 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030015079
Ville Syrjäläf8856a42016-07-26 19:07:00 +030015080 ret = drm_plane_helper_check_state(&state->base,
15081 &state->clip,
15082 DRM_PLANE_HELPER_NO_SCALING,
15083 DRM_PLANE_HELPER_NO_SCALING,
15084 true, true);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015085 if (ret)
15086 return ret;
15087
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015088 /* if we want to turn off the cursor ignore width and height */
15089 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020015090 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015091
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015092 /* Check for which cursor types we support */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010015093 if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
15094 state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080015095 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
15096 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015097 return -EINVAL;
15098 }
15099
Matt Roperea2c67b2014-12-23 10:41:52 -080015100 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
15101 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015102 DRM_DEBUG_KMS("buffer is too small\n");
15103 return -ENOMEM;
15104 }
15105
Ville Syrjälä3a656b52015-03-09 21:08:37 +020015106 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015107 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020015108 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015109 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015110
Ville Syrjäläb29ec922015-12-18 19:24:39 +020015111 /*
15112 * There's something wrong with the cursor on CHV pipe C.
15113 * If it straddles the left edge of the screen then
15114 * moving it away from the edge or disabling it often
15115 * results in a pipe underrun, and often that can lead to
15116 * dead pipe (constant underrun reported, and it scans
15117 * out just a solid color). To recover from that, the
15118 * display power well must be turned off and on again.
15119 * Refuse the put the cursor into that compromised position.
15120 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015121 if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
Ville Syrjälä936e71e2016-07-26 19:06:59 +030015122 state->base.visible && state->base.crtc_x < 0) {
Ville Syrjäläb29ec922015-12-18 19:24:39 +020015123 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
15124 return -EINVAL;
15125 }
15126
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020015127 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030015128}
15129
Matt Roperf4a2cf22014-12-01 15:40:12 -080015130static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030015131intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020015132 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030015133{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010015134 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
15135
15136 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015137 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030015138}
15139
15140static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015141intel_update_cursor_plane(struct drm_plane *plane,
15142 const struct intel_crtc_state *crtc_state,
15143 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030015144{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015145 struct drm_crtc *crtc = crtc_state->base.crtc;
15146 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080015147 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080015148 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080015149 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070015150
Matt Roperf4a2cf22014-12-01 15:40:12 -080015151 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080015152 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080015153 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Chris Wilson058d88c2016-08-15 10:49:06 +010015154 addr = i915_gem_object_ggtt_offset(obj, NULL);
Matt Roperf4a2cf22014-12-01 15:40:12 -080015155 else
Gustavo Padovana912f122014-12-01 15:40:10 -080015156 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080015157
Gustavo Padovana912f122014-12-01 15:40:10 -080015158 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015159 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070015160}
Gustavo Padovan852e7872014-09-05 17:22:31 -030015161
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015162static struct intel_plane *
Ville Syrjälä580503c2016-10-31 22:37:00 +020015163intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
Matt Roper3d7d6512014-06-10 08:28:13 -070015164{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015165 struct intel_plane *cursor = NULL;
15166 struct intel_plane_state *state = NULL;
15167 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070015168
15169 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015170 if (!cursor) {
15171 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015172 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015173 }
Matt Roper3d7d6512014-06-10 08:28:13 -070015174
Matt Roper8e7d6882015-01-21 16:35:41 -080015175 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015176 if (!state) {
15177 ret = -ENOMEM;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015178 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015179 }
15180
Matt Roper8e7d6882015-01-21 16:35:41 -080015181 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080015182
Matt Roper3d7d6512014-06-10 08:28:13 -070015183 cursor->can_scale = false;
15184 cursor->max_downscale = 1;
15185 cursor->pipe = pipe;
15186 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030015187 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080015188 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015189 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030015190 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070015191
Ville Syrjälä580503c2016-10-31 22:37:00 +020015192 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
15193 0, &intel_plane_funcs,
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015194 intel_cursor_formats,
15195 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjälä38573dc2016-05-27 20:59:23 +030015196 DRM_PLANE_TYPE_CURSOR,
15197 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015198 if (ret)
15199 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070015200
Dave Airlie5481e272016-10-25 16:36:13 +100015201 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030015202 drm_plane_create_rotation_property(&cursor->base,
15203 DRM_ROTATE_0,
15204 DRM_ROTATE_0 |
15205 DRM_ROTATE_180);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070015206
Ville Syrjälä580503c2016-10-31 22:37:00 +020015207 if (INTEL_GEN(dev_priv) >= 9)
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070015208 state->scaler_id = -1;
15209
Matt Roperea2c67b2014-12-23 10:41:52 -080015210 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
15211
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015212 return cursor;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015213
15214fail:
15215 kfree(state);
15216 kfree(cursor);
15217
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015218 return ERR_PTR(ret);
Matt Roper3d7d6512014-06-10 08:28:13 -070015219}
15220
Ville Syrjälä65edccc2016-10-31 22:37:01 +020015221static void skl_init_scalers(struct drm_i915_private *dev_priv,
15222 struct intel_crtc *crtc,
15223 struct intel_crtc_state *crtc_state)
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015224{
Ville Syrjälä65edccc2016-10-31 22:37:01 +020015225 struct intel_crtc_scaler_state *scaler_state =
15226 &crtc_state->scaler_state;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015227 int i;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015228
Ville Syrjälä65edccc2016-10-31 22:37:01 +020015229 for (i = 0; i < crtc->num_scalers; i++) {
15230 struct intel_scaler *scaler = &scaler_state->scalers[i];
15231
15232 scaler->in_use = 0;
15233 scaler->mode = PS_SCALER_MODE_DYN;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015234 }
15235
15236 scaler_state->scaler_id = -1;
15237}
15238
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015239static int intel_crtc_init(struct drm_device *dev, enum pipe pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080015240{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015241 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015242 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015243 struct intel_crtc_state *crtc_state = NULL;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015244 struct intel_plane *primary = NULL;
15245 struct intel_plane *cursor = NULL;
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030015246 int sprite, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080015247
Daniel Vetter955382f2013-09-19 14:05:45 +020015248 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015249 if (!intel_crtc)
15250 return -ENOMEM;
Jesse Barnes79e53942008-11-07 14:24:08 -080015251
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015252 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015253 if (!crtc_state) {
15254 ret = -ENOMEM;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015255 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015256 }
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030015257 intel_crtc->config = crtc_state;
15258 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080015259 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015260
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015261 /* initialize shared scalers */
15262 if (INTEL_INFO(dev)->gen >= 9) {
15263 if (pipe == PIPE_C)
15264 intel_crtc->num_scalers = 1;
15265 else
15266 intel_crtc->num_scalers = SKL_NUM_SCALERS;
15267
Ville Syrjälä65edccc2016-10-31 22:37:01 +020015268 skl_init_scalers(dev_priv, intel_crtc, crtc_state);
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015269 }
15270
Ville Syrjälä580503c2016-10-31 22:37:00 +020015271 primary = intel_primary_plane_create(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015272 if (IS_ERR(primary)) {
15273 ret = PTR_ERR(primary);
Matt Roper3d7d6512014-06-10 08:28:13 -070015274 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015275 }
Matt Roper3d7d6512014-06-10 08:28:13 -070015276
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030015277 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015278 struct intel_plane *plane;
15279
Ville Syrjälä580503c2016-10-31 22:37:00 +020015280 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015281 if (!plane) {
15282 ret = PTR_ERR(plane);
15283 goto fail;
15284 }
Ville Syrjäläa81d6fa2016-10-25 18:58:01 +030015285 }
15286
Ville Syrjälä580503c2016-10-31 22:37:00 +020015287 cursor = intel_cursor_plane_create(dev_priv, pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015288 if (!cursor) {
15289 ret = PTR_ERR(cursor);
Matt Roper3d7d6512014-06-10 08:28:13 -070015290 goto fail;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015291 }
Matt Roper3d7d6512014-06-10 08:28:13 -070015292
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015293 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base,
15294 &primary->base, &cursor->base,
15295 &intel_crtc_funcs,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030015296 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070015297 if (ret)
15298 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080015299
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020015300 /*
15301 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020015302 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020015303 */
Jesse Barnes80824002009-09-10 15:28:06 -070015304 intel_crtc->pipe = pipe;
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015305 intel_crtc->plane = (enum plane) pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010015306 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080015307 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010015308 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070015309 }
15310
Chris Wilson4b0e3332014-05-30 16:35:26 +030015311 intel_crtc->cursor_base = ~0;
15312 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030015313 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030015314
Ville Syrjälä852eb002015-06-24 22:00:07 +030015315 intel_crtc->wm.cxsr_allowed = true;
15316
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080015317 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
15318 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020015319 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
15320 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080015321
Jesse Barnes79e53942008-11-07 14:24:08 -080015322 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020015323
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000015324 intel_color_init(&intel_crtc->base);
15325
Daniel Vetter87b6b102014-05-15 15:33:46 +020015326 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015327
15328 return 0;
Matt Roper3d7d6512014-06-10 08:28:13 -070015329
15330fail:
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015331 /*
15332 * drm_mode_config_cleanup() will free up any
15333 * crtcs/planes already initialized.
15334 */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015335 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070015336 kfree(intel_crtc);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030015337
15338 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080015339}
15340
Jesse Barnes752aa882013-10-31 18:55:49 +020015341enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
15342{
15343 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015344 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020015345
Rob Clark51fd3712013-11-19 12:10:12 -050015346 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020015347
Ville Syrjäläd3babd32014-11-07 11:16:01 +020015348 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020015349 return INVALID_PIPE;
15350
15351 return to_intel_crtc(encoder->crtc)->pipe;
15352}
15353
Carl Worth08d7b3d2009-04-29 14:43:54 -070015354int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000015355 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070015356{
Carl Worth08d7b3d2009-04-29 14:43:54 -070015357 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040015358 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020015359 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070015360
Rob Clark7707e652014-07-17 23:30:04 -040015361 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010015362 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030015363 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070015364
Rob Clark7707e652014-07-17 23:30:04 -040015365 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020015366 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070015367
Daniel Vetterc05422d2009-08-11 16:05:30 +020015368 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070015369}
15370
Daniel Vetter66a92782012-07-12 20:08:18 +020015371static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080015372{
Daniel Vetter66a92782012-07-12 20:08:18 +020015373 struct drm_device *dev = encoder->base.dev;
15374 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080015375 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080015376 int entry = 0;
15377
Damien Lespiaub2784e12014-08-05 11:29:37 +010015378 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020015379 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020015380 index_mask |= (1 << entry);
15381
Jesse Barnes79e53942008-11-07 14:24:08 -080015382 entry++;
15383 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010015384
Jesse Barnes79e53942008-11-07 14:24:08 -080015385 return index_mask;
15386}
15387
Chris Wilson4d302442010-12-14 19:21:29 +000015388static bool has_edp_a(struct drm_device *dev)
15389{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015390 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson4d302442010-12-14 19:21:29 +000015391
15392 if (!IS_MOBILE(dev))
15393 return false;
15394
15395 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
15396 return false;
15397
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015398 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000015399 return false;
15400
15401 return true;
15402}
15403
Jesse Barnes84b4e042014-06-25 08:24:29 -070015404static bool intel_crt_present(struct drm_device *dev)
15405{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015406 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes84b4e042014-06-25 08:24:29 -070015407
Damien Lespiau884497e2013-12-03 13:56:23 +000015408 if (INTEL_INFO(dev)->gen >= 9)
15409 return false;
15410
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010015411 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070015412 return false;
15413
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015414 if (IS_CHERRYVIEW(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070015415 return false;
15416
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010015417 if (HAS_PCH_LPT_H(dev_priv) &&
15418 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
Ville Syrjälä65e472e2015-12-01 23:28:55 +020015419 return false;
15420
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020015421 /* DDI E can't be used if DDI A requires 4 lanes */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010015422 if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020015423 return false;
15424
Ville Syrjäläe4abb732015-12-01 23:31:33 +020015425 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070015426 return false;
15427
15428 return true;
15429}
15430
Imre Deak8090ba82016-08-10 14:07:33 +030015431void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
15432{
15433 int pps_num;
15434 int pps_idx;
15435
15436 if (HAS_DDI(dev_priv))
15437 return;
15438 /*
15439 * This w/a is needed at least on CPT/PPT, but to be sure apply it
15440 * everywhere where registers can be write protected.
15441 */
15442 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15443 pps_num = 2;
15444 else
15445 pps_num = 1;
15446
15447 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
15448 u32 val = I915_READ(PP_CONTROL(pps_idx));
15449
15450 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
15451 I915_WRITE(PP_CONTROL(pps_idx), val);
15452 }
15453}
15454
Imre Deak44cb7342016-08-10 14:07:29 +030015455static void intel_pps_init(struct drm_i915_private *dev_priv)
15456{
15457 if (HAS_PCH_SPLIT(dev_priv) || IS_BROXTON(dev_priv))
15458 dev_priv->pps_mmio_base = PCH_PPS_BASE;
15459 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15460 dev_priv->pps_mmio_base = VLV_PPS_BASE;
15461 else
15462 dev_priv->pps_mmio_base = PPS_BASE;
Imre Deak8090ba82016-08-10 14:07:33 +030015463
15464 intel_pps_unlock_regs_wa(dev_priv);
Imre Deak44cb7342016-08-10 14:07:29 +030015465}
15466
Jesse Barnes79e53942008-11-07 14:24:08 -080015467static void intel_setup_outputs(struct drm_device *dev)
15468{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015469 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson4ef69c72010-09-09 15:14:28 +010015470 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040015471 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080015472
Imre Deak44cb7342016-08-10 14:07:29 +030015473 intel_pps_init(dev_priv);
15474
Imre Deak97a824e12016-06-21 11:51:47 +030015475 /*
15476 * intel_edp_init_connector() depends on this completing first, to
15477 * prevent the registeration of both eDP and LVDS and the incorrect
15478 * sharing of the PPS.
15479 */
Daniel Vetterc9093352013-06-06 22:22:47 +020015480 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015481
Jesse Barnes84b4e042014-06-25 08:24:29 -070015482 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020015483 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040015484
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +010015485 if (IS_BROXTON(dev_priv)) {
Vandana Kannanc776eb22014-08-19 12:05:01 +053015486 /*
15487 * FIXME: Broxton doesn't support port detection via the
15488 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
15489 * detect the ports.
15490 */
15491 intel_ddi_init(dev, PORT_A);
15492 intel_ddi_init(dev, PORT_B);
15493 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020015494
15495 intel_dsi_init(dev);
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010015496 } else if (HAS_DDI(dev_priv)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030015497 int found;
15498
Jesse Barnesde31fac2015-03-06 15:53:32 -080015499 /*
15500 * Haswell uses DDI functions to detect digital outputs.
15501 * On SKL pre-D0 the strap isn't connected, so we assume
15502 * it's there.
15503 */
Ville Syrjälä77179402015-09-18 20:03:35 +030015504 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080015505 /* WaIgnoreDDIAStrap: skl */
Tvrtko Ursulin08537232016-10-13 11:03:02 +010015506 if (found || IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030015507 intel_ddi_init(dev, PORT_A);
15508
15509 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
15510 * register */
15511 found = I915_READ(SFUSE_STRAP);
15512
15513 if (found & SFUSE_STRAP_DDIB_DETECTED)
15514 intel_ddi_init(dev, PORT_B);
15515 if (found & SFUSE_STRAP_DDIC_DETECTED)
15516 intel_ddi_init(dev, PORT_C);
15517 if (found & SFUSE_STRAP_DDID_DETECTED)
15518 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070015519 /*
15520 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
15521 */
Tvrtko Ursulin08537232016-10-13 11:03:02 +010015522 if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070015523 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
15524 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
15525 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
15526 intel_ddi_init(dev, PORT_E);
15527
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010015528 } else if (HAS_PCH_SPLIT(dev_priv)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040015529 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020015530 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020015531
15532 if (has_edp_a(dev))
15533 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040015534
Paulo Zanonidc0fa712013-02-19 16:21:46 -030015535 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080015536 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020015537 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080015538 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030015539 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080015540 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030015541 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080015542 }
15543
Paulo Zanonidc0fa712013-02-19 16:21:46 -030015544 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030015545 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080015546
Paulo Zanonidc0fa712013-02-19 16:21:46 -030015547 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030015548 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080015549
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080015550 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030015551 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080015552
Daniel Vetter270b3042012-10-27 15:52:05 +020015553 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030015554 intel_dp_init(dev, PCH_DP_D, PORT_D);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015555 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030015556 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010015557
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030015558 /*
15559 * The DP_DETECTED bit is the latched state of the DDC
15560 * SDA pin at boot. However since eDP doesn't require DDC
15561 * (no way to plug in a DP->HDMI dongle) the DDC pins for
15562 * eDP ports may have been muxed to an alternate function.
15563 * Thus we can't rely on the DP_DETECTED bit alone to detect
15564 * eDP ports. Consult the VBT as well as DP_DETECTED to
15565 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030015566 *
15567 * Sadly the straps seem to be missing sometimes even for HDMI
15568 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
15569 * and VBT for the presence of the port. Additionally we can't
15570 * trust the port type the VBT declares as we've seen at least
15571 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030015572 */
Chris Wilson457c52d2016-06-01 08:27:50 +010015573 has_edp = intel_dp_is_edp(dev, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015574 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
15575 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010015576 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015577 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030015578 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030015579
Chris Wilson457c52d2016-06-01 08:27:50 +010015580 has_edp = intel_dp_is_edp(dev, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015581 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
15582 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010015583 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015584 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030015585 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053015586
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015587 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030015588 /*
15589 * eDP not supported on port D,
15590 * so no need to worry about it
15591 */
15592 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
15593 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030015594 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015595 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
15596 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030015597 }
15598
Jani Nikula3cfca972013-08-27 15:12:26 +030015599 intel_dsi_init(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015600 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
Ma Ling27185ae2009-08-24 13:50:23 +080015601 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080015602
Paulo Zanonie2debe92013-02-18 19:00:27 -030015603 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015604 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020015605 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015606 if (!found && IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015607 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030015608 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015609 }
Ma Ling27185ae2009-08-24 13:50:23 +080015610
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015611 if (!found && IS_G4X(dev_priv))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030015612 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080015613 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040015614
15615 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040015616
Paulo Zanonie2debe92013-02-18 19:00:27 -030015617 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015618 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020015619 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015620 }
Ma Ling27185ae2009-08-24 13:50:23 +080015621
Paulo Zanonie2debe92013-02-18 19:00:27 -030015622 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080015623
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015624 if (IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015625 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030015626 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015627 }
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015628 if (IS_G4X(dev_priv))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030015629 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080015630 }
Ma Ling27185ae2009-08-24 13:50:23 +080015631
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015632 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030015633 intel_dp_init(dev, DP_D, PORT_D);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015634 } else if (IS_GEN2(dev_priv))
Jesse Barnes79e53942008-11-07 14:24:08 -080015635 intel_dvo_init(dev);
15636
Zhenyu Wang103a1962009-11-27 11:44:36 +080015637 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080015638 intel_tv_init(dev);
15639
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080015640 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070015641
Damien Lespiaub2784e12014-08-05 11:29:37 +010015642 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010015643 encoder->base.possible_crtcs = encoder->crtc_mask;
15644 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020015645 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080015646 }
Chris Wilson47356eb2011-01-11 17:06:04 +000015647
Paulo Zanonidde86e22012-12-01 12:04:25 -020015648 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020015649
15650 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015651}
15652
15653static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
15654{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030015655 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080015656 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080015657
Daniel Vetteref2d6332014-02-10 18:00:38 +010015658 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030015659 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010015660 WARN_ON(!intel_fb->obj->framebuffer_references--);
Chris Wilsonf8c417c2016-07-20 13:31:53 +010015661 i915_gem_object_put(intel_fb->obj);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030015662 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015663 kfree(intel_fb);
15664}
15665
15666static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000015667 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080015668 unsigned int *handle)
15669{
15670 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000015671 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080015672
Chris Wilsoncc917ab2015-10-13 14:22:26 +010015673 if (obj->userptr.mm) {
15674 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
15675 return -EINVAL;
15676 }
15677
Chris Wilson05394f32010-11-08 19:18:58 +000015678 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080015679}
15680
Rodrigo Vivi86c98582015-07-08 16:22:45 -070015681static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
15682 struct drm_file *file,
15683 unsigned flags, unsigned color,
15684 struct drm_clip_rect *clips,
15685 unsigned num_clips)
15686{
15687 struct drm_device *dev = fb->dev;
15688 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
15689 struct drm_i915_gem_object *obj = intel_fb->obj;
15690
15691 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030015692 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070015693 mutex_unlock(&dev->struct_mutex);
15694
15695 return 0;
15696}
15697
Jesse Barnes79e53942008-11-07 14:24:08 -080015698static const struct drm_framebuffer_funcs intel_fb_funcs = {
15699 .destroy = intel_user_framebuffer_destroy,
15700 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070015701 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080015702};
15703
Damien Lespiaub3218032015-02-27 11:15:18 +000015704static
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015705u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
15706 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +000015707{
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015708 u32 gen = INTEL_INFO(dev_priv)->gen;
Damien Lespiaub3218032015-02-27 11:15:18 +000015709
15710 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020015711 int cpp = drm_format_plane_cpp(pixel_format, 0);
15712
Damien Lespiaub3218032015-02-27 11:15:18 +000015713 /* "The stride in bytes must not exceed the of the size of 8K
15714 * pixels and 32K bytes."
15715 */
Ville Syrjäläac484962016-01-20 21:05:26 +020015716 return min(8192 * cpp, 32768);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015717 } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
15718 !IS_CHERRYVIEW(dev_priv)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000015719 return 32*1024;
15720 } else if (gen >= 4) {
15721 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15722 return 16*1024;
15723 else
15724 return 32*1024;
15725 } else if (gen >= 3) {
15726 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15727 return 8*1024;
15728 else
15729 return 16*1024;
15730 } else {
15731 /* XXX DSPC is limited to 4k tiled */
15732 return 8*1024;
15733 }
15734}
15735
Daniel Vetterb5ea6422014-03-02 21:18:00 +010015736static int intel_framebuffer_init(struct drm_device *dev,
15737 struct intel_framebuffer *intel_fb,
15738 struct drm_mode_fb_cmd2 *mode_cmd,
15739 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080015740{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015741 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015742 unsigned int tiling = i915_gem_object_get_tiling(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080015743 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000015744 u32 pitch_limit, stride_alignment;
Eric Engestromd3828142016-08-15 16:29:55 +010015745 char *format_name;
Jesse Barnes79e53942008-11-07 14:24:08 -080015746
Daniel Vetterdd4916c2013-10-09 21:23:51 +020015747 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
15748
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015749 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015750 /*
15751 * If there's a fence, enforce that
15752 * the fb modifier and tiling mode match.
15753 */
15754 if (tiling != I915_TILING_NONE &&
15755 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015756 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
15757 return -EINVAL;
15758 }
15759 } else {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015760 if (tiling == I915_TILING_X) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015761 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015762 } else if (tiling == I915_TILING_Y) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015763 DRM_DEBUG("No Y tiling for legacy addfb\n");
15764 return -EINVAL;
15765 }
15766 }
15767
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000015768 /* Passed in modifier sanity checking. */
15769 switch (mode_cmd->modifier[0]) {
15770 case I915_FORMAT_MOD_Y_TILED:
15771 case I915_FORMAT_MOD_Yf_TILED:
15772 if (INTEL_INFO(dev)->gen < 9) {
15773 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15774 mode_cmd->modifier[0]);
15775 return -EINVAL;
15776 }
15777 case DRM_FORMAT_MOD_NONE:
15778 case I915_FORMAT_MOD_X_TILED:
15779 break;
15780 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070015781 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15782 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010015783 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015784 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015785
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015786 /*
15787 * gen2/3 display engine uses the fence if present,
15788 * so the tiling mode must match the fb modifier exactly.
15789 */
15790 if (INTEL_INFO(dev_priv)->gen < 4 &&
15791 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
15792 DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
15793 return -EINVAL;
15794 }
15795
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015796 stride_alignment = intel_fb_stride_alignment(dev_priv,
15797 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000015798 mode_cmd->pixel_format);
15799 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15800 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15801 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010015802 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015803 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015804
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015805 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000015806 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015807 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000015808 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15809 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015810 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015811 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015812 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015813 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015814
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015815 /*
15816 * If there's a fence, enforce that
15817 * the fb pitch and fence stride match.
15818 */
15819 if (tiling != I915_TILING_NONE &&
Chris Wilson3e510a82016-08-05 10:14:23 +010015820 mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015821 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
Chris Wilson3e510a82016-08-05 10:14:23 +010015822 mode_cmd->pitches[0],
15823 i915_gem_object_get_stride(obj));
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015824 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015825 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015826
Ville Syrjälä57779d02012-10-31 17:50:14 +020015827 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080015828 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020015829 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015830 case DRM_FORMAT_RGB565:
15831 case DRM_FORMAT_XRGB8888:
15832 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015833 break;
15834 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015835 if (INTEL_INFO(dev)->gen > 3) {
Eric Engestrom90844f02016-08-15 01:02:38 +010015836 format_name = drm_get_format_name(mode_cmd->pixel_format);
15837 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15838 kfree(format_name);
Ville Syrjälä57779d02012-10-31 17:50:14 +020015839 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015840 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020015841 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020015842 case DRM_FORMAT_ABGR8888:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015843 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
Wayne Boyer666a4532015-12-09 12:29:35 -080015844 INTEL_INFO(dev)->gen < 9) {
Eric Engestrom90844f02016-08-15 01:02:38 +010015845 format_name = drm_get_format_name(mode_cmd->pixel_format);
15846 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15847 kfree(format_name);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015848 return -EINVAL;
15849 }
15850 break;
15851 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015852 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015853 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015854 if (INTEL_INFO(dev)->gen < 4) {
Eric Engestrom90844f02016-08-15 01:02:38 +010015855 format_name = drm_get_format_name(mode_cmd->pixel_format);
15856 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15857 kfree(format_name);
Ville Syrjälä57779d02012-10-31 17:50:14 +020015858 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015859 }
Jesse Barnesb5626742011-06-24 12:19:27 -070015860 break;
Damien Lespiau75312082015-05-15 19:06:01 +010015861 case DRM_FORMAT_ABGR2101010:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015862 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Eric Engestrom90844f02016-08-15 01:02:38 +010015863 format_name = drm_get_format_name(mode_cmd->pixel_format);
15864 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15865 kfree(format_name);
Damien Lespiau75312082015-05-15 19:06:01 +010015866 return -EINVAL;
15867 }
15868 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020015869 case DRM_FORMAT_YUYV:
15870 case DRM_FORMAT_UYVY:
15871 case DRM_FORMAT_YVYU:
15872 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015873 if (INTEL_INFO(dev)->gen < 5) {
Eric Engestrom90844f02016-08-15 01:02:38 +010015874 format_name = drm_get_format_name(mode_cmd->pixel_format);
15875 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15876 kfree(format_name);
Ville Syrjälä57779d02012-10-31 17:50:14 +020015877 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015878 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015879 break;
15880 default:
Eric Engestrom90844f02016-08-15 01:02:38 +010015881 format_name = drm_get_format_name(mode_cmd->pixel_format);
15882 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15883 kfree(format_name);
Chris Wilson57cd6502010-08-08 12:34:44 +010015884 return -EINVAL;
15885 }
15886
Ville Syrjälä90f9a332012-10-31 17:50:19 +020015887 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15888 if (mode_cmd->offsets[0] != 0)
15889 return -EINVAL;
15890
Daniel Vetterc7d73f62012-12-13 23:38:38 +010015891 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15892 intel_fb->obj = obj;
15893
Ville Syrjälä6687c902015-09-15 13:16:41 +030015894 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
15895 if (ret)
15896 return ret;
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020015897
Jesse Barnes79e53942008-11-07 14:24:08 -080015898 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15899 if (ret) {
15900 DRM_ERROR("framebuffer init failed %d\n", ret);
15901 return ret;
15902 }
15903
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020015904 intel_fb->obj->framebuffer_references++;
15905
Jesse Barnes79e53942008-11-07 14:24:08 -080015906 return 0;
15907}
15908
Jesse Barnes79e53942008-11-07 14:24:08 -080015909static struct drm_framebuffer *
15910intel_user_framebuffer_create(struct drm_device *dev,
15911 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020015912 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080015913{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015914 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000015915 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020015916 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080015917
Chris Wilson03ac0642016-07-20 13:31:51 +010015918 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15919 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010015920 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080015921
Daniel Vetter92907cb2015-11-23 09:04:05 +010015922 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015923 if (IS_ERR(fb))
Chris Wilsonf0cd5182016-10-28 13:58:43 +010015924 i915_gem_object_put(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015925
15926 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080015927}
15928
Jesse Barnes79e53942008-11-07 14:24:08 -080015929static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080015930 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020015931 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080015932 .atomic_check = intel_atomic_check,
15933 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020015934 .atomic_state_alloc = intel_atomic_state_alloc,
15935 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080015936};
15937
Imre Deak88212942016-03-16 13:38:53 +020015938/**
15939 * intel_init_display_hooks - initialize the display modesetting hooks
15940 * @dev_priv: device private
15941 */
15942void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070015943{
Imre Deak88212942016-03-16 13:38:53 +020015944 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015945 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015946 dev_priv->display.get_initial_plane_config =
15947 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015948 dev_priv->display.crtc_compute_clock =
15949 haswell_crtc_compute_clock;
15950 dev_priv->display.crtc_enable = haswell_crtc_enable;
15951 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015952 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015953 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015954 dev_priv->display.get_initial_plane_config =
15955 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020015956 dev_priv->display.crtc_compute_clock =
15957 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020015958 dev_priv->display.crtc_enable = haswell_crtc_enable;
15959 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015960 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015961 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015962 dev_priv->display.get_initial_plane_config =
15963 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020015964 dev_priv->display.crtc_compute_clock =
15965 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015966 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15967 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015968 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070015969 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015970 dev_priv->display.get_initial_plane_config =
15971 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015972 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15973 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15974 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15975 } else if (IS_VALLEYVIEW(dev_priv)) {
15976 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15977 dev_priv->display.get_initial_plane_config =
15978 i9xx_get_initial_plane_config;
15979 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070015980 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15981 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020015982 } else if (IS_G4X(dev_priv)) {
15983 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15984 dev_priv->display.get_initial_plane_config =
15985 i9xx_get_initial_plane_config;
15986 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15987 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15988 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020015989 } else if (IS_PINEVIEW(dev_priv)) {
15990 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15991 dev_priv->display.get_initial_plane_config =
15992 i9xx_get_initial_plane_config;
15993 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15994 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15995 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015996 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015997 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015998 dev_priv->display.get_initial_plane_config =
15999 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020016000 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020016001 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16002 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020016003 } else {
16004 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16005 dev_priv->display.get_initial_plane_config =
16006 i9xx_get_initial_plane_config;
16007 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
16008 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16009 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070016010 }
Jesse Barnese70236a2009-09-21 10:42:27 -070016011
Jesse Barnese70236a2009-09-21 10:42:27 -070016012 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020016013 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030016014 dev_priv->display.get_display_clock_speed =
16015 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016016 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070016017 dev_priv->display.get_display_clock_speed =
16018 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016019 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030016020 dev_priv->display.get_display_clock_speed =
16021 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016022 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030016023 dev_priv->display.get_display_clock_speed =
16024 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016025 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070016026 dev_priv->display.get_display_clock_speed =
16027 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016028 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030016029 dev_priv->display.get_display_clock_speed =
16030 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016031 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
16032 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016033 dev_priv->display.get_display_clock_speed =
16034 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016035 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030016036 dev_priv->display.get_display_clock_speed =
16037 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016038 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030016039 dev_priv->display.get_display_clock_speed =
16040 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016041 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030016042 dev_priv->display.get_display_clock_speed =
16043 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016044 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030016045 dev_priv->display.get_display_clock_speed =
16046 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016047 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016048 dev_priv->display.get_display_clock_speed =
16049 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016050 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016051 dev_priv->display.get_display_clock_speed =
16052 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016053 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016054 dev_priv->display.get_display_clock_speed =
16055 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016056 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016057 dev_priv->display.get_display_clock_speed =
16058 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016059 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016060 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030016061 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030016062 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020016063 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070016064 dev_priv->display.get_display_clock_speed =
16065 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030016066 }
Jesse Barnese70236a2009-09-21 10:42:27 -070016067
Imre Deak88212942016-03-16 13:38:53 +020016068 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053016069 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020016070 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053016071 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020016072 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053016073 /* FIXME: detect B0+ stepping and use auto training */
16074 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020016075 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053016076 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030016077 }
16078
16079 if (IS_BROADWELL(dev_priv)) {
16080 dev_priv->display.modeset_commit_cdclk =
16081 broadwell_modeset_commit_cdclk;
16082 dev_priv->display.modeset_calc_cdclk =
16083 broadwell_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020016084 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020016085 dev_priv->display.modeset_commit_cdclk =
16086 valleyview_modeset_commit_cdclk;
16087 dev_priv->display.modeset_calc_cdclk =
16088 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020016089 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020016090 dev_priv->display.modeset_commit_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030016091 bxt_modeset_commit_cdclk;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020016092 dev_priv->display.modeset_calc_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030016093 bxt_modeset_calc_cdclk;
Clint Taylorc89e39f2016-05-13 23:41:21 +030016094 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
16095 dev_priv->display.modeset_commit_cdclk =
16096 skl_modeset_commit_cdclk;
16097 dev_priv->display.modeset_calc_cdclk =
16098 skl_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070016099 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020016100
Lyude27082492016-08-24 07:48:10 +020016101 if (dev_priv->info.gen >= 9)
16102 dev_priv->display.update_crtcs = skl_update_crtcs;
16103 else
16104 dev_priv->display.update_crtcs = intel_update_crtcs;
16105
Daniel Vetter5a21b662016-05-24 17:13:53 +020016106 switch (INTEL_INFO(dev_priv)->gen) {
16107 case 2:
16108 dev_priv->display.queue_flip = intel_gen2_queue_flip;
16109 break;
16110
16111 case 3:
16112 dev_priv->display.queue_flip = intel_gen3_queue_flip;
16113 break;
16114
16115 case 4:
16116 case 5:
16117 dev_priv->display.queue_flip = intel_gen4_queue_flip;
16118 break;
16119
16120 case 6:
16121 dev_priv->display.queue_flip = intel_gen6_queue_flip;
16122 break;
16123 case 7:
16124 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
16125 dev_priv->display.queue_flip = intel_gen7_queue_flip;
16126 break;
16127 case 9:
16128 /* Drop through - unsupported since execlist only. */
16129 default:
16130 /* Default just returns -ENODEV to indicate unsupported */
16131 dev_priv->display.queue_flip = intel_default_queue_flip;
16132 }
Jesse Barnese70236a2009-09-21 10:42:27 -070016133}
16134
Jesse Barnesb690e962010-07-19 13:53:12 -070016135/*
16136 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
16137 * resume, or other times. This quirk makes sure that's the case for
16138 * affected systems.
16139 */
Akshay Joshi0206e352011-08-16 15:34:10 -040016140static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070016141{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016142 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb690e962010-07-19 13:53:12 -070016143
16144 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020016145 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070016146}
16147
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030016148static void quirk_pipeb_force(struct drm_device *dev)
16149{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016150 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030016151
16152 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
16153 DRM_INFO("applying pipe b force quirk\n");
16154}
16155
Keith Packard435793d2011-07-12 14:56:22 -070016156/*
16157 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
16158 */
16159static void quirk_ssc_force_disable(struct drm_device *dev)
16160{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016161 struct drm_i915_private *dev_priv = to_i915(dev);
Keith Packard435793d2011-07-12 14:56:22 -070016162 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020016163 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070016164}
16165
Carsten Emde4dca20e2012-03-15 15:56:26 +010016166/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010016167 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
16168 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010016169 */
16170static void quirk_invert_brightness(struct drm_device *dev)
16171{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016172 struct drm_i915_private *dev_priv = to_i915(dev);
Carsten Emde4dca20e2012-03-15 15:56:26 +010016173 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020016174 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070016175}
16176
Scot Doyle9c72cc62014-07-03 23:27:50 +000016177/* Some VBT's incorrectly indicate no backlight is present */
16178static void quirk_backlight_present(struct drm_device *dev)
16179{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016180 struct drm_i915_private *dev_priv = to_i915(dev);
Scot Doyle9c72cc62014-07-03 23:27:50 +000016181 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
16182 DRM_INFO("applying backlight present quirk\n");
16183}
16184
Jesse Barnesb690e962010-07-19 13:53:12 -070016185struct intel_quirk {
16186 int device;
16187 int subsystem_vendor;
16188 int subsystem_device;
16189 void (*hook)(struct drm_device *dev);
16190};
16191
Egbert Eich5f85f172012-10-14 15:46:38 +020016192/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
16193struct intel_dmi_quirk {
16194 void (*hook)(struct drm_device *dev);
16195 const struct dmi_system_id (*dmi_id_list)[];
16196};
16197
16198static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
16199{
16200 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
16201 return 1;
16202}
16203
16204static const struct intel_dmi_quirk intel_dmi_quirks[] = {
16205 {
16206 .dmi_id_list = &(const struct dmi_system_id[]) {
16207 {
16208 .callback = intel_dmi_reverse_brightness,
16209 .ident = "NCR Corporation",
16210 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
16211 DMI_MATCH(DMI_PRODUCT_NAME, ""),
16212 },
16213 },
16214 { } /* terminating entry */
16215 },
16216 .hook = quirk_invert_brightness,
16217 },
16218};
16219
Ben Widawskyc43b5632012-04-16 14:07:40 -070016220static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070016221 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
16222 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
16223
Jesse Barnesb690e962010-07-19 13:53:12 -070016224 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
16225 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
16226
Ville Syrjälä5f080c02014-08-15 01:22:06 +030016227 /* 830 needs to leave pipe A & dpll A up */
16228 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
16229
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030016230 /* 830 needs to leave pipe B & dpll B up */
16231 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
16232
Keith Packard435793d2011-07-12 14:56:22 -070016233 /* Lenovo U160 cannot use SSC on LVDS */
16234 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020016235
16236 /* Sony Vaio Y cannot use SSC on LVDS */
16237 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010016238
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010016239 /* Acer Aspire 5734Z must invert backlight brightness */
16240 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
16241
16242 /* Acer/eMachines G725 */
16243 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
16244
16245 /* Acer/eMachines e725 */
16246 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
16247
16248 /* Acer/Packard Bell NCL20 */
16249 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
16250
16251 /* Acer Aspire 4736Z */
16252 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020016253
16254 /* Acer Aspire 5336 */
16255 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000016256
16257 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
16258 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000016259
Scot Doyledfb3d47b2014-08-21 16:08:02 +000016260 /* Acer C720 Chromebook (Core i3 4005U) */
16261 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
16262
jens steinb2a96012014-10-28 20:25:53 +010016263 /* Apple Macbook 2,1 (Core 2 T7400) */
16264 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
16265
Jani Nikula1b9448b2015-11-05 11:49:59 +020016266 /* Apple Macbook 4,1 */
16267 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
16268
Scot Doyled4967d82014-07-03 23:27:52 +000016269 /* Toshiba CB35 Chromebook (Celeron 2955U) */
16270 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000016271
16272 /* HP Chromebook 14 (Celeron 2955U) */
16273 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020016274
16275 /* Dell Chromebook 11 */
16276 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020016277
16278 /* Dell Chromebook 11 (2015 version) */
16279 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070016280};
16281
16282static void intel_init_quirks(struct drm_device *dev)
16283{
16284 struct pci_dev *d = dev->pdev;
16285 int i;
16286
16287 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
16288 struct intel_quirk *q = &intel_quirks[i];
16289
16290 if (d->device == q->device &&
16291 (d->subsystem_vendor == q->subsystem_vendor ||
16292 q->subsystem_vendor == PCI_ANY_ID) &&
16293 (d->subsystem_device == q->subsystem_device ||
16294 q->subsystem_device == PCI_ANY_ID))
16295 q->hook(dev);
16296 }
Egbert Eich5f85f172012-10-14 15:46:38 +020016297 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
16298 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
16299 intel_dmi_quirks[i].hook(dev);
16300 }
Jesse Barnesb690e962010-07-19 13:53:12 -070016301}
16302
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016303/* Disable the VGA plane that we never use */
16304static void i915_disable_vga(struct drm_device *dev)
16305{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016306 struct drm_i915_private *dev_priv = to_i915(dev);
David Weinehall52a05c32016-08-22 13:32:44 +030016307 struct pci_dev *pdev = dev_priv->drm.pdev;
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016308 u8 sr1;
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010016309 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016310
Ville Syrjälä2b37c612014-01-22 21:32:38 +020016311 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
David Weinehall52a05c32016-08-22 13:32:44 +030016312 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070016313 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016314 sr1 = inb(VGA_SR_DATA);
16315 outb(sr1 | 1<<5, VGA_SR_DATA);
David Weinehall52a05c32016-08-22 13:32:44 +030016316 vga_put(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016317 udelay(300);
16318
Ville Syrjälä01f5a622014-12-16 18:38:37 +020016319 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016320 POSTING_READ(vga_reg);
16321}
16322
Daniel Vetterf8175862012-04-10 15:50:11 +020016323void intel_modeset_init_hw(struct drm_device *dev)
16324{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016325 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010016326
Ville Syrjäläb6283052015-06-03 15:45:07 +030016327 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010016328
16329 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
16330
Daniel Vetterf8175862012-04-10 15:50:11 +020016331 intel_init_clock_gating(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020016332}
16333
Matt Roperd93c0372015-12-03 11:37:41 -080016334/*
16335 * Calculate what we think the watermarks should be for the state we've read
16336 * out of the hardware and then immediately program those watermarks so that
16337 * we ensure the hardware settings match our internal state.
16338 *
16339 * We can calculate what we think WM's should be by creating a duplicate of the
16340 * current state (which was constructed during hardware readout) and running it
16341 * through the atomic check code to calculate new watermark values in the
16342 * state object.
16343 */
16344static void sanitize_watermarks(struct drm_device *dev)
16345{
16346 struct drm_i915_private *dev_priv = to_i915(dev);
16347 struct drm_atomic_state *state;
16348 struct drm_crtc *crtc;
16349 struct drm_crtc_state *cstate;
16350 struct drm_modeset_acquire_ctx ctx;
16351 int ret;
16352 int i;
16353
16354 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080016355 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080016356 return;
16357
16358 /*
16359 * We need to hold connection_mutex before calling duplicate_state so
16360 * that the connector loop is protected.
16361 */
16362 drm_modeset_acquire_init(&ctx, 0);
16363retry:
Matt Roper0cd12622016-01-12 07:13:37 -080016364 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080016365 if (ret == -EDEADLK) {
16366 drm_modeset_backoff(&ctx);
16367 goto retry;
16368 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080016369 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080016370 }
16371
16372 state = drm_atomic_helper_duplicate_state(dev, &ctx);
16373 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080016374 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080016375
Matt Ropered4a6a72016-02-23 17:20:13 -080016376 /*
16377 * Hardware readout is the only time we don't want to calculate
16378 * intermediate watermarks (since we don't trust the current
16379 * watermarks).
16380 */
16381 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16382
Matt Roperd93c0372015-12-03 11:37:41 -080016383 ret = intel_atomic_check(dev, state);
16384 if (ret) {
16385 /*
16386 * If we fail here, it means that the hardware appears to be
16387 * programmed in a way that shouldn't be possible, given our
16388 * understanding of watermark requirements. This might mean a
16389 * mistake in the hardware readout code or a mistake in the
16390 * watermark calculations for a given platform. Raise a WARN
16391 * so that this is noticeable.
16392 *
16393 * If this actually happens, we'll have to just leave the
16394 * BIOS-programmed watermarks untouched and hope for the best.
16395 */
16396 WARN(true, "Could not determine valid watermarks for inherited state\n");
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020016397 goto put_state;
Matt Roperd93c0372015-12-03 11:37:41 -080016398 }
16399
16400 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080016401 for_each_crtc_in_state(state, crtc, cstate, i) {
16402 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
16403
Matt Ropered4a6a72016-02-23 17:20:13 -080016404 cs->wm.need_postvbl_update = true;
16405 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080016406 }
16407
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020016408put_state:
Chris Wilson08536952016-10-14 13:18:18 +010016409 drm_atomic_state_put(state);
Matt Roper0cd12622016-01-12 07:13:37 -080016410fail:
Matt Roperd93c0372015-12-03 11:37:41 -080016411 drm_modeset_drop_locks(&ctx);
16412 drm_modeset_acquire_fini(&ctx);
16413}
16414
Ville Syrjäläb079bd172016-10-25 18:58:02 +030016415int intel_modeset_init(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -080016416{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030016417 struct drm_i915_private *dev_priv = to_i915(dev);
16418 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000016419 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080016420 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080016421
16422 drm_mode_config_init(dev);
16423
16424 dev->mode_config.min_width = 0;
16425 dev->mode_config.min_height = 0;
16426
Dave Airlie019d96c2011-09-29 16:20:42 +010016427 dev->mode_config.preferred_depth = 24;
16428 dev->mode_config.prefer_shadow = 1;
16429
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000016430 dev->mode_config.allow_fb_modifiers = true;
16431
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020016432 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080016433
Jesse Barnesb690e962010-07-19 13:53:12 -070016434 intel_init_quirks(dev);
16435
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030016436 intel_init_pm(dev);
16437
Ben Widawskye3c74752013-04-05 13:12:39 -070016438 if (INTEL_INFO(dev)->num_pipes == 0)
Ville Syrjäläb079bd172016-10-25 18:58:02 +030016439 return 0;
Ben Widawskye3c74752013-04-05 13:12:39 -070016440
Lukas Wunner69f92f62015-07-15 13:57:35 +020016441 /*
16442 * There may be no VBT; and if the BIOS enabled SSC we can
16443 * just keep using it to avoid unnecessary flicker. Whereas if the
16444 * BIOS isn't using it, don't assume it will work even if the VBT
16445 * indicates as much.
16446 */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010016447 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
Lukas Wunner69f92f62015-07-15 13:57:35 +020016448 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
16449 DREF_SSC1_ENABLE);
16450
16451 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
16452 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
16453 bios_lvds_use_ssc ? "en" : "dis",
16454 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
16455 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
16456 }
16457 }
16458
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010016459 if (IS_GEN2(dev_priv)) {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010016460 dev->mode_config.max_width = 2048;
16461 dev->mode_config.max_height = 2048;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010016462 } else if (IS_GEN3(dev_priv)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070016463 dev->mode_config.max_width = 4096;
16464 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080016465 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010016466 dev->mode_config.max_width = 8192;
16467 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080016468 }
Damien Lespiau068be562014-03-28 14:17:49 +000016469
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010016470 if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
16471 dev->mode_config.cursor_width = IS_845G(dev_priv) ? 64 : 512;
Ville Syrjälädc41c152014-08-13 11:57:05 +030016472 dev->mode_config.cursor_height = 1023;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010016473 } else if (IS_GEN2(dev_priv)) {
Damien Lespiau068be562014-03-28 14:17:49 +000016474 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
16475 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
16476 } else {
16477 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
16478 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
16479 }
16480
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030016481 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080016482
Zhao Yakui28c97732009-10-09 11:39:41 +080016483 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070016484 INTEL_INFO(dev)->num_pipes,
16485 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080016486
Damien Lespiau055e3932014-08-18 13:49:10 +010016487 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläb079bd172016-10-25 18:58:02 +030016488 int ret;
16489
16490 ret = intel_crtc_init(dev, pipe);
16491 if (ret) {
16492 drm_mode_config_cleanup(dev);
16493 return ret;
16494 }
Jesse Barnes79e53942008-11-07 14:24:08 -080016495 }
16496
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030016497 intel_update_czclk(dev_priv);
16498 intel_update_cdclk(dev);
16499
Daniel Vettere72f9fb2013-06-05 13:34:06 +020016500 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010016501
Ville Syrjäläb2045352016-05-13 23:41:27 +030016502 if (dev_priv->max_cdclk_freq == 0)
16503 intel_update_max_cdclk(dev);
16504
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016505 /* Just disable it once at startup */
16506 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016507 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000016508
Daniel Vetter6e9f7982014-05-29 23:54:47 +020016509 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016510 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020016511 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080016512
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016513 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020016514 struct intel_initial_plane_config plane_config = {};
16515
Jesse Barnes46f297f2014-03-07 08:57:48 -080016516 if (!crtc->active)
16517 continue;
16518
Jesse Barnes46f297f2014-03-07 08:57:48 -080016519 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080016520 * Note that reserving the BIOS fb up front prevents us
16521 * from stuffing other stolen allocations like the ring
16522 * on top. This prevents some ugliness at boot time, and
16523 * can even allow for smooth boot transitions if the BIOS
16524 * fb is large enough for the active pipe configuration.
16525 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020016526 dev_priv->display.get_initial_plane_config(crtc,
16527 &plane_config);
16528
16529 /*
16530 * If the fb is shared between multiple heads, we'll
16531 * just get the first one.
16532 */
16533 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080016534 }
Matt Roperd93c0372015-12-03 11:37:41 -080016535
16536 /*
16537 * Make sure hardware watermarks really match the state we read out.
16538 * Note that we need to do this after reconstructing the BIOS fb's
16539 * since the watermark calculation done here will use pstate->fb.
16540 */
16541 sanitize_watermarks(dev);
Ville Syrjäläb079bd172016-10-25 18:58:02 +030016542
16543 return 0;
Chris Wilson2c7111d2011-03-29 10:40:27 +010016544}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080016545
Daniel Vetter7fad7982012-07-04 17:51:47 +020016546static void intel_enable_pipe_a(struct drm_device *dev)
16547{
16548 struct intel_connector *connector;
16549 struct drm_connector *crt = NULL;
16550 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030016551 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020016552
16553 /* We can't just switch on the pipe A, we need to set things up with a
16554 * proper mode and output configuration. As a gross hack, enable pipe A
16555 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016556 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020016557 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
16558 crt = &connector->base;
16559 break;
16560 }
16561 }
16562
16563 if (!crt)
16564 return;
16565
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030016566 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020016567 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020016568}
16569
Daniel Vetterfa555832012-10-10 23:14:00 +020016570static bool
16571intel_check_plane_mapping(struct intel_crtc *crtc)
16572{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070016573 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010016574 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä649636e2015-09-22 19:50:01 +030016575 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020016576
Ben Widawsky7eb552a2013-03-13 14:05:41 -070016577 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020016578 return true;
16579
Ville Syrjälä649636e2015-09-22 19:50:01 +030016580 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020016581
16582 if ((val & DISPLAY_PLANE_ENABLE) &&
16583 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
16584 return false;
16585
16586 return true;
16587}
16588
Ville Syrjälä02e93c32015-08-26 19:39:19 +030016589static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
16590{
16591 struct drm_device *dev = crtc->base.dev;
16592 struct intel_encoder *encoder;
16593
16594 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
16595 return true;
16596
16597 return false;
16598}
16599
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020016600static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
16601{
16602 struct drm_device *dev = encoder->base.dev;
16603 struct intel_connector *connector;
16604
16605 for_each_connector_on_encoder(dev, &encoder->base, connector)
16606 return connector;
16607
16608 return NULL;
16609}
16610
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030016611static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
16612 enum transcoder pch_transcoder)
16613{
16614 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
16615 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
16616}
16617
Daniel Vetter24929352012-07-02 20:28:59 +020016618static void intel_sanitize_crtc(struct intel_crtc *crtc)
16619{
16620 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010016621 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020016622 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020016623
Daniel Vetter24929352012-07-02 20:28:59 +020016624 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020016625 if (!transcoder_is_dsi(cpu_transcoder)) {
16626 i915_reg_t reg = PIPECONF(cpu_transcoder);
16627
16628 I915_WRITE(reg,
16629 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
16630 }
Daniel Vetter24929352012-07-02 20:28:59 +020016631
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030016632 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010016633 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030016634 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016635 struct intel_plane *plane;
16636
Daniel Vetter96256042015-02-13 21:03:42 +010016637 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016638
16639 /* Disable everything but the primary plane */
16640 for_each_intel_plane_on_crtc(dev, crtc, plane) {
16641 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
16642 continue;
16643
16644 plane->disable_plane(&plane->base, &crtc->base);
16645 }
Daniel Vetter96256042015-02-13 21:03:42 +010016646 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030016647
Daniel Vetter24929352012-07-02 20:28:59 +020016648 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020016649 * disable the crtc (and hence change the state) if it is wrong. Note
16650 * that gen4+ has a fixed plane -> pipe mapping. */
16651 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020016652 bool plane;
16653
Ville Syrjälä78108b72016-05-27 20:59:19 +030016654 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
16655 crtc->base.base.id, crtc->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020016656
16657 /* Pipe has the wrong plane attached and the plane is active.
16658 * Temporarily change the plane mapping and disable everything
16659 * ... */
16660 plane = crtc->plane;
Ville Syrjälä936e71e2016-07-26 19:06:59 +030016661 to_intel_plane_state(crtc->base.primary->state)->base.visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020016662 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020016663 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020016664 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020016665 }
Daniel Vetter24929352012-07-02 20:28:59 +020016666
Daniel Vetter7fad7982012-07-04 17:51:47 +020016667 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
16668 crtc->pipe == PIPE_A && !crtc->active) {
16669 /* BIOS forgot to enable pipe A, this mostly happens after
16670 * resume. Force-enable the pipe to fix this, the update_dpms
16671 * call below we restore the pipe to the right state, but leave
16672 * the required bits on. */
16673 intel_enable_pipe_a(dev);
16674 }
16675
Daniel Vetter24929352012-07-02 20:28:59 +020016676 /* Adjust the state of the output pipe according to whether we
16677 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010016678 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020016679 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020016680
Tvrtko Ursulin49cff962016-10-13 11:02:54 +010016681 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010016682 /*
16683 * We start out with underrun reporting disabled to avoid races.
16684 * For correct bookkeeping mark this on active crtcs.
16685 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020016686 * Also on gmch platforms we dont have any hardware bits to
16687 * disable the underrun reporting. Which means we need to start
16688 * out with underrun reporting disabled also on inactive pipes,
16689 * since otherwise we'll complain about the garbage we read when
16690 * e.g. coming up after runtime pm.
16691 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010016692 * No protection against concurrent access is required - at
16693 * worst a fifo underrun happens which also sets this to false.
16694 */
16695 crtc->cpu_fifo_underrun_disabled = true;
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030016696 /*
16697 * We track the PCH trancoder underrun reporting state
16698 * within the crtc. With crtc for pipe A housing the underrun
16699 * reporting state for PCH transcoder A, crtc for pipe B housing
16700 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
16701 * and marking underrun reporting as disabled for the non-existing
16702 * PCH transcoders B and C would prevent enabling the south
16703 * error interrupt (see cpt_can_enable_serr_int()).
16704 */
16705 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
16706 crtc->pch_fifo_underrun_disabled = true;
Daniel Vetter4cc31482014-03-24 00:01:41 +010016707 }
Daniel Vetter24929352012-07-02 20:28:59 +020016708}
16709
16710static void intel_sanitize_encoder(struct intel_encoder *encoder)
16711{
16712 struct intel_connector *connector;
Daniel Vetter24929352012-07-02 20:28:59 +020016713
16714 /* We need to check both for a crtc link (meaning that the
16715 * encoder is active and trying to read from a pipe) and the
16716 * pipe itself being active. */
16717 bool has_active_crtc = encoder->base.crtc &&
16718 to_intel_crtc(encoder->base.crtc)->active;
16719
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020016720 connector = intel_encoder_find_connector(encoder);
16721 if (connector && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020016722 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
16723 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016724 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020016725
16726 /* Connector is active, but has no active pipe. This is
16727 * fallout from our resume register restoring. Disable
16728 * the encoder manually again. */
16729 if (encoder->base.crtc) {
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020016730 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
16731
Daniel Vetter24929352012-07-02 20:28:59 +020016732 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
16733 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016734 encoder->base.name);
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020016735 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030016736 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020016737 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Daniel Vetter24929352012-07-02 20:28:59 +020016738 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020016739 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020016740
16741 /* Inconsistent output/port/pipe state happens presumably due to
16742 * a bug in one of the get_hw_state functions. Or someplace else
16743 * in our code, like the register restore mess on resume. Clamp
16744 * things to off as a safer default. */
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020016745
16746 connector->base.dpms = DRM_MODE_DPMS_OFF;
16747 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020016748 }
16749 /* Enabled encoders without active connectors will be fixed in
16750 * the crtc fixup. */
16751}
16752
Imre Deak04098752014-02-18 00:02:16 +020016753void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016754{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016755 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010016756 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016757
Imre Deak04098752014-02-18 00:02:16 +020016758 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
16759 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
16760 i915_disable_vga(dev);
16761 }
16762}
16763
16764void i915_redisable_vga(struct drm_device *dev)
16765{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016766 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak04098752014-02-18 00:02:16 +020016767
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030016768 /* This function can be called both from intel_modeset_setup_hw_state or
16769 * at a very early point in our resume sequence, where the power well
16770 * structures are not yet restored. Since this function is at a very
16771 * paranoid "someone might have enabled VGA while we were not looking"
16772 * level, just check if the power well is enabled instead of trying to
16773 * follow the "don't touch the power well if we don't need it" policy
16774 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020016775 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030016776 return;
16777
Imre Deak04098752014-02-18 00:02:16 +020016778 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020016779
16780 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016781}
16782
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016783static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016784{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016785 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016786
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016787 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016788}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016789
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016790/* FIXME read out full plane state for all planes */
16791static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016792{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016793 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016794 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016795 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016796
Ville Syrjälä936e71e2016-07-26 19:06:59 +030016797 plane_state->base.visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016798 primary_get_hw_state(to_intel_plane(primary));
16799
Ville Syrjälä936e71e2016-07-26 19:06:59 +030016800 if (plane_state->base.visible)
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016801 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016802}
16803
Daniel Vetter30e984d2013-06-05 13:34:17 +020016804static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020016805{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016806 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016807 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020016808 struct intel_crtc *crtc;
16809 struct intel_encoder *encoder;
16810 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020016811 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020016812
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016813 dev_priv->active_crtcs = 0;
16814
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016815 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016816 struct intel_crtc_state *crtc_state = crtc->config;
16817 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020016818
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020016819 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016820 memset(crtc_state, 0, sizeof(*crtc_state));
16821 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020016822
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016823 crtc_state->base.active = crtc_state->base.enable =
16824 dev_priv->display.get_pipe_config(crtc, crtc_state);
16825
16826 crtc->base.enabled = crtc_state->base.enable;
16827 crtc->active = crtc_state->base.active;
16828
16829 if (crtc_state->base.active) {
16830 dev_priv->active_crtcs |= 1 << crtc->pipe;
16831
Clint Taylorc89e39f2016-05-13 23:41:21 +030016832 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016833 pixclk = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016834 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016835 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16836 else
16837 WARN_ON(dev_priv->display.modeset_calc_cdclk);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016838
16839 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16840 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16841 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016842 }
16843
16844 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030016845
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016846 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020016847
Ville Syrjälä78108b72016-05-27 20:59:19 +030016848 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16849 crtc->base.base.id, crtc->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016850 crtc->active ? "enabled" : "disabled");
16851 }
16852
Daniel Vetter53589012013-06-05 13:34:16 +020016853 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16854 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16855
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016856 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16857 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016858 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016859 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016860 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016861 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020016862 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016863 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020016864
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020016865 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016866 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020016867 }
16868
Damien Lespiaub2784e12014-08-05 11:29:37 +010016869 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016870 pipe = 0;
16871
16872 if (encoder->get_hw_state(encoder, &pipe)) {
Ville Syrjälä98187832016-10-31 22:37:10 +020016873 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020016874
Jesse Barnes045ac3b2013-05-14 17:08:26 -070016875 encoder->base.crtc = &crtc->base;
Ville Syrjälä253c84c2016-06-22 21:57:01 +030016876 crtc->config->output_types |= 1 << encoder->type;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016877 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020016878 } else {
16879 encoder->base.crtc = NULL;
16880 }
16881
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016882 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020016883 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016884 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016885 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016886 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020016887 }
16888
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016889 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020016890 if (connector->get_hw_state(connector)) {
16891 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016892
16893 encoder = connector->encoder;
16894 connector->base.encoder = &encoder->base;
16895
16896 if (encoder->base.crtc &&
16897 encoder->base.crtc->state->active) {
16898 /*
16899 * This has to be done during hardware readout
16900 * because anything calling .crtc_disable may
16901 * rely on the connector_mask being accurate.
16902 */
16903 encoder->base.crtc->state->connector_mask |=
16904 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010016905 encoder->base.crtc->state->encoder_mask |=
16906 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016907 }
16908
Daniel Vetter24929352012-07-02 20:28:59 +020016909 } else {
16910 connector->base.dpms = DRM_MODE_DPMS_OFF;
16911 connector->base.encoder = NULL;
16912 }
16913 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16914 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030016915 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016916 connector->base.encoder ? "enabled" : "disabled");
16917 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016918
16919 for_each_intel_crtc(dev, crtc) {
16920 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16921
16922 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16923 if (crtc->base.state->active) {
16924 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16925 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16926 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16927
16928 /*
16929 * The initial mode needs to be set in order to keep
16930 * the atomic core happy. It wants a valid mode if the
16931 * crtc's enabled, so we do the above call.
16932 *
16933 * At this point some state updated by the connectors
16934 * in their ->detect() callback has not run yet, so
16935 * no recalculation can be done yet.
16936 *
16937 * Even if we could do a recalculation and modeset
16938 * right now it would cause a double modeset if
16939 * fbdev or userspace chooses a different initial mode.
16940 *
16941 * If that happens, someone indicated they wanted a
16942 * mode change, which means it's safe to do a full
16943 * recalculation.
16944 */
16945 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030016946
16947 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16948 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016949 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020016950
16951 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016952 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020016953}
16954
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016955/* Scan out the current hw modeset state,
16956 * and sanitizes it to the current state
16957 */
16958static void
16959intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020016960{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016961 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter30e984d2013-06-05 13:34:17 +020016962 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016963 struct intel_crtc *crtc;
16964 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020016965 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016966
16967 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016968
16969 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010016970 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016971 intel_sanitize_encoder(encoder);
16972 }
16973
Damien Lespiau055e3932014-08-18 13:49:10 +010016974 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä98187832016-10-31 22:37:10 +020016975 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
Ville Syrjäläe2af48c2016-10-31 22:37:05 +020016976
Daniel Vetter24929352012-07-02 20:28:59 +020016977 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016978 intel_dump_pipe_config(crtc, crtc->config,
16979 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020016980 }
Daniel Vetter9a935852012-07-05 22:34:27 +020016981
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020016982 intel_modeset_update_connector_atomic_state(dev);
16983
Daniel Vetter35c95372013-07-17 06:55:04 +020016984 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16985 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16986
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016987 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020016988 continue;
16989
16990 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16991
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016992 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020016993 pll->on = false;
16994 }
16995
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010016996 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030016997 vlv_wm_get_hw_state(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010016998 else if (IS_GEN9(dev_priv))
Pradeep Bhat30789992014-11-04 17:06:45 +000016999 skl_wm_get_hw_state(dev);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010017000 else if (HAS_PCH_SPLIT(dev_priv))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030017001 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020017002
17003 for_each_intel_crtc(dev, crtc) {
17004 unsigned long put_domains;
17005
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010017006 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020017007 if (WARN_ON(put_domains))
17008 modeset_put_power_domains(dev_priv, put_domains);
17009 }
17010 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020017011
17012 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017013}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030017014
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017015void intel_display_resume(struct drm_device *dev)
17016{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017017 struct drm_i915_private *dev_priv = to_i915(dev);
17018 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
17019 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017020 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020017021
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017022 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst73974892016-08-05 23:28:27 +030017023 if (state)
17024 state->acquire_ctx = &ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017025
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010017026 /*
17027 * This is a cludge because with real atomic modeset mode_config.mutex
17028 * won't be taken. Unfortunately some probed state like
17029 * audio_codec_enable is still protected by mode_config.mutex, so lock
17030 * it here for now.
17031 */
17032 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017033 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017034
Maarten Lankhorst73974892016-08-05 23:28:27 +030017035 while (1) {
17036 ret = drm_modeset_lock_all_ctx(dev, &ctx);
17037 if (ret != -EDEADLK)
17038 break;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017039
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017040 drm_modeset_backoff(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017041 }
17042
Maarten Lankhorst73974892016-08-05 23:28:27 +030017043 if (!ret)
17044 ret = __intel_display_resume(dev, state);
17045
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017046 drm_modeset_drop_locks(&ctx);
17047 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010017048 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020017049
Chris Wilson08536952016-10-14 13:18:18 +010017050 if (ret)
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010017051 DRM_ERROR("Restoring old state failed with %i\n", ret);
Chris Wilson08536952016-10-14 13:18:18 +010017052 drm_atomic_state_put(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010017053}
17054
17055void intel_modeset_gem_init(struct drm_device *dev)
17056{
Chris Wilsondc979972016-05-10 14:10:04 +010017057 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080017058 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070017059 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -080017060
Chris Wilsondc979972016-05-10 14:10:04 +010017061 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030017062
Chris Wilson1833b132012-05-09 11:56:28 +010017063 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020017064
Chris Wilson1ee8da62016-05-12 12:43:23 +010017065 intel_setup_overlay(dev_priv);
Jesse Barnes484b41d2014-03-07 08:57:55 -080017066
17067 /*
17068 * Make sure any fbs we allocated at startup are properly
17069 * pinned & fenced. When we do the allocation it's too early
17070 * for this.
17071 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010017072 for_each_crtc(dev, c) {
Chris Wilson058d88c2016-08-15 10:49:06 +010017073 struct i915_vma *vma;
17074
Matt Roper2ff8fde2014-07-08 07:50:07 -070017075 obj = intel_fb_obj(c->primary->fb);
17076 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080017077 continue;
17078
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010017079 mutex_lock(&dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +010017080 vma = intel_pin_and_fence_fb_obj(c->primary->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +020017081 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010017082 mutex_unlock(&dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +010017083 if (IS_ERR(vma)) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080017084 DRM_ERROR("failed to pin boot fb on pipe %d\n",
17085 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100017086 drm_framebuffer_unreference(c->primary->fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020017087 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020017088 c->primary->crtc = c->primary->state->crtc = NULL;
Daniel Vetter5a21b662016-05-24 17:13:53 +020017089 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020017090 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080017091 }
17092 }
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010017093}
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020017094
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010017095int intel_connector_register(struct drm_connector *connector)
17096{
17097 struct intel_connector *intel_connector = to_intel_connector(connector);
17098 int ret;
17099
17100 ret = intel_backlight_device_register(intel_connector);
17101 if (ret)
17102 goto err;
17103
17104 return 0;
17105
17106err:
17107 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080017108}
17109
Chris Wilsonc191eca2016-06-17 11:40:33 +010017110void intel_connector_unregister(struct drm_connector *connector)
Imre Deak4932e2c2014-02-11 17:12:48 +020017111{
Chris Wilsone63d87c2016-06-17 11:40:34 +010017112 struct intel_connector *intel_connector = to_intel_connector(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020017113
Chris Wilsone63d87c2016-06-17 11:40:34 +010017114 intel_backlight_device_unregister(intel_connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020017115 intel_panel_destroy_backlight(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020017116}
17117
Jesse Barnes79e53942008-11-07 14:24:08 -080017118void intel_modeset_cleanup(struct drm_device *dev)
17119{
Chris Wilsonfac5e232016-07-04 11:34:36 +010017120 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -070017121
Chris Wilsondc979972016-05-10 14:10:04 +010017122 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020017123
Daniel Vetterfd0c0642013-04-24 11:13:35 +020017124 /*
17125 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020017126 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020017127 * experience fancy races otherwise.
17128 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020017129 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070017130
Daniel Vetterfd0c0642013-04-24 11:13:35 +020017131 /*
17132 * Due to the hpd irq storm handling the hotplug work can re-arm the
17133 * poll handlers. Hence disable polling after hpd handling is shut down.
17134 */
Keith Packardf87ea762010-10-03 19:36:26 -070017135 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020017136
Jesse Barnes723bfd72010-10-07 16:01:13 -070017137 intel_unregister_dsm_handler();
17138
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020017139 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050017140
Chris Wilson1630fe72011-07-08 12:22:42 +010017141 /* flush any delayed tasks or pending work */
17142 flush_scheduled_work();
17143
Jesse Barnes79e53942008-11-07 14:24:08 -080017144 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010017145
Chris Wilson1ee8da62016-05-12 12:43:23 +010017146 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030017147
Chris Wilsondc979972016-05-10 14:10:04 +010017148 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010017149
17150 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080017151}
17152
Chris Wilsondf0e9242010-09-09 16:20:55 +010017153void intel_connector_attach_encoder(struct intel_connector *connector,
17154 struct intel_encoder *encoder)
17155{
17156 connector->encoder = encoder;
17157 drm_mode_connector_attach_encoder(&connector->base,
17158 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080017159}
Dave Airlie28d52042009-09-21 14:33:58 +100017160
17161/*
17162 * set vga decode state - true == enable VGA decode
17163 */
17164int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
17165{
Chris Wilsonfac5e232016-07-04 11:34:36 +010017166 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsona885b3c2013-12-17 14:34:50 +000017167 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100017168 u16 gmch_ctrl;
17169
Chris Wilson75fa0412014-02-07 18:37:02 -020017170 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
17171 DRM_ERROR("failed to read control word\n");
17172 return -EIO;
17173 }
17174
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020017175 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
17176 return 0;
17177
Dave Airlie28d52042009-09-21 14:33:58 +100017178 if (state)
17179 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
17180 else
17181 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020017182
17183 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
17184 DRM_ERROR("failed to write control word\n");
17185 return -EIO;
17186 }
17187
Dave Airlie28d52042009-09-21 14:33:58 +100017188 return 0;
17189}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017190
Chris Wilson98a2f412016-10-12 10:05:18 +010017191#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
17192
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017193struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030017194
17195 u32 power_well_driver;
17196
Chris Wilson63b66e52013-08-08 15:12:06 +020017197 int num_transcoders;
17198
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017199 struct intel_cursor_error_state {
17200 u32 control;
17201 u32 position;
17202 u32 base;
17203 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010017204 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017205
17206 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020017207 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017208 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030017209 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010017210 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017211
17212 struct intel_plane_error_state {
17213 u32 control;
17214 u32 stride;
17215 u32 size;
17216 u32 pos;
17217 u32 addr;
17218 u32 surface;
17219 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010017220 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020017221
17222 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020017223 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020017224 enum transcoder cpu_transcoder;
17225
17226 u32 conf;
17227
17228 u32 htotal;
17229 u32 hblank;
17230 u32 hsync;
17231 u32 vtotal;
17232 u32 vblank;
17233 u32 vsync;
17234 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017235};
17236
17237struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010017238intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017239{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017240 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020017241 int transcoders[] = {
17242 TRANSCODER_A,
17243 TRANSCODER_B,
17244 TRANSCODER_C,
17245 TRANSCODER_EDP,
17246 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017247 int i;
17248
Chris Wilsonc0336662016-05-06 15:40:21 +010017249 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020017250 return NULL;
17251
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020017252 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017253 if (error == NULL)
17254 return NULL;
17255
Chris Wilsonc0336662016-05-06 15:40:21 +010017256 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030017257 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
17258
Damien Lespiau055e3932014-08-18 13:49:10 +010017259 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020017260 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020017261 __intel_display_power_is_enabled(dev_priv,
17262 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020017263 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020017264 continue;
17265
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030017266 error->cursor[i].control = I915_READ(CURCNTR(i));
17267 error->cursor[i].position = I915_READ(CURPOS(i));
17268 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017269
17270 error->plane[i].control = I915_READ(DSPCNTR(i));
17271 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010017272 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030017273 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030017274 error->plane[i].pos = I915_READ(DSPPOS(i));
17275 }
Chris Wilsonc0336662016-05-06 15:40:21 +010017276 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030017277 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010017278 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017279 error->plane[i].surface = I915_READ(DSPSURF(i));
17280 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
17281 }
17282
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017283 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030017284
Chris Wilsonc0336662016-05-06 15:40:21 +010017285 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e2014-04-18 15:55:04 +030017286 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020017287 }
17288
Jani Nikula4d1de972016-03-18 17:05:42 +020017289 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010017290 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030017291 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020017292 error->num_transcoders++; /* Account for eDP. */
17293
17294 for (i = 0; i < error->num_transcoders; i++) {
17295 enum transcoder cpu_transcoder = transcoders[i];
17296
Imre Deakddf9c532013-11-27 22:02:02 +020017297 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020017298 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020017299 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020017300 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020017301 continue;
17302
Chris Wilson63b66e52013-08-08 15:12:06 +020017303 error->transcoder[i].cpu_transcoder = cpu_transcoder;
17304
17305 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
17306 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
17307 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
17308 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
17309 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
17310 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
17311 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017312 }
17313
17314 return error;
17315}
17316
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017317#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
17318
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017319void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017320intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017321 struct drm_device *dev,
17322 struct intel_display_error_state *error)
17323{
Chris Wilsonfac5e232016-07-04 11:34:36 +010017324 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017325 int i;
17326
Chris Wilson63b66e52013-08-08 15:12:06 +020017327 if (!error)
17328 return;
17329
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017330 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Tvrtko Ursulin86527442016-10-13 11:03:00 +010017331 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017332 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030017333 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010017334 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017335 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020017336 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020017337 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017338 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030017339 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017340
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017341 err_printf(m, "Plane [%d]:\n", i);
17342 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
17343 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030017344 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017345 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
17346 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030017347 }
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010017348 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017349 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017350 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017351 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
17352 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017353 }
17354
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017355 err_printf(m, "Cursor [%d]:\n", i);
17356 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
17357 err_printf(m, " POS: %08x\n", error->cursor[i].position);
17358 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017359 }
Chris Wilson63b66e52013-08-08 15:12:06 +020017360
17361 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020017362 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020017363 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020017364 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020017365 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020017366 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
17367 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
17368 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
17369 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
17370 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
17371 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
17372 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
17373 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017374}
Chris Wilson98a2f412016-10-12 10:05:18 +010017375
17376#endif