blob: 622f733b934780327462360d0f2b6deeefcc7e6d [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 *);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700118static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
119 struct intel_crtc_state *crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200120static void skylake_pfit_enable(struct intel_crtc *crtc);
121static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
122static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200123static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200124static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Ville Syrjälä4e5ca602016-05-11 22:44:44 +0300125static int ilk_max_pixel_rate(struct drm_atomic_state *state);
Imre Deak324513c2016-06-13 16:44:36 +0300126static int bxt_calc_cdclk(int max_pixclk);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100127
Ma Lingd4906092009-03-18 20:13:27 +0800128struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300129 struct {
130 int min, max;
131 } dot, vco, n, m, m1, m2, p, p1;
132
133 struct {
134 int dot_limit;
135 int p2_slow, p2_fast;
136 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800137};
Jesse Barnes79e53942008-11-07 14:24:08 -0800138
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300139/* returns HPLL frequency in kHz */
140static int valleyview_get_vco(struct drm_i915_private *dev_priv)
141{
142 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
143
144 /* Obtain SKU information */
145 mutex_lock(&dev_priv->sb_lock);
146 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
147 CCK_FUSE_HPLL_FREQ_MASK;
148 mutex_unlock(&dev_priv->sb_lock);
149
150 return vco_freq[hpll_freq] * 1000;
151}
152
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200153int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
154 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300155{
156 u32 val;
157 int divider;
158
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300159 mutex_lock(&dev_priv->sb_lock);
160 val = vlv_cck_read(dev_priv, reg);
161 mutex_unlock(&dev_priv->sb_lock);
162
163 divider = val & CCK_FREQUENCY_VALUES;
164
165 WARN((val & CCK_FREQUENCY_STATUS) !=
166 (divider << CCK_FREQUENCY_STATUS_SHIFT),
167 "%s change in progress\n", name);
168
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200169 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
170}
171
172static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
173 const char *name, u32 reg)
174{
175 if (dev_priv->hpll_freq == 0)
176 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
177
178 return vlv_get_cck_clock(dev_priv, name, reg,
179 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300180}
181
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200182static int
183intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200184{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200185 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200186}
187
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200188static int
189intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300190{
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300191 /* RAWCLK_FREQ_VLV register updated from power well code */
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200192 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
193 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200194}
195
196static int
197intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
198{
Jani Nikula79e50a42015-08-26 10:58:20 +0300199 uint32_t clkcfg;
200
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200201 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300202 clkcfg = I915_READ(CLKCFG);
203 switch (clkcfg & CLKCFG_FSB_MASK) {
204 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200205 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300206 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200207 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300208 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200209 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300210 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200211 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300212 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200213 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300214 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200215 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300216 /* these two are just a guess; one of them might be right */
217 case CLKCFG_FSB_1600:
218 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200219 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300220 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200221 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300222 }
223}
224
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300225void intel_update_rawclk(struct drm_i915_private *dev_priv)
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200226{
227 if (HAS_PCH_SPLIT(dev_priv))
228 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
229 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
230 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
231 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
232 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
233 else
234 return; /* no rawclk on other platforms, or no need to know it */
235
236 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
237}
238
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300239static void intel_update_czclk(struct drm_i915_private *dev_priv)
240{
Wayne Boyer666a4532015-12-09 12:29:35 -0800241 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300242 return;
243
244 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
245 CCK_CZ_CLOCK_CONTROL);
246
247 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
248}
249
Chris Wilson021357a2010-09-07 20:54:59 +0100250static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200251intel_fdi_link_freq(struct drm_i915_private *dev_priv,
252 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100253{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200254 if (HAS_DDI(dev_priv))
255 return pipe_config->port_clock; /* SPLL */
256 else if (IS_GEN5(dev_priv))
257 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200258 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200259 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100260}
261
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300262static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400263 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200264 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200265 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400266 .m = { .min = 96, .max = 140 },
267 .m1 = { .min = 18, .max = 26 },
268 .m2 = { .min = 6, .max = 16 },
269 .p = { .min = 4, .max = 128 },
270 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700271 .p2 = { .dot_limit = 165000,
272 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700273};
274
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300275static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200276 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200277 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200278 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200279 .m = { .min = 96, .max = 140 },
280 .m1 = { .min = 18, .max = 26 },
281 .m2 = { .min = 6, .max = 16 },
282 .p = { .min = 4, .max = 128 },
283 .p1 = { .min = 2, .max = 33 },
284 .p2 = { .dot_limit = 165000,
285 .p2_slow = 4, .p2_fast = 4 },
286};
287
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300288static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400289 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200290 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200291 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400292 .m = { .min = 96, .max = 140 },
293 .m1 = { .min = 18, .max = 26 },
294 .m2 = { .min = 6, .max = 16 },
295 .p = { .min = 4, .max = 128 },
296 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700297 .p2 = { .dot_limit = 165000,
298 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700299};
Eric Anholt273e27c2011-03-30 13:01:10 -0700300
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300301static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400302 .dot = { .min = 20000, .max = 400000 },
303 .vco = { .min = 1400000, .max = 2800000 },
304 .n = { .min = 1, .max = 6 },
305 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100306 .m1 = { .min = 8, .max = 18 },
307 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400308 .p = { .min = 5, .max = 80 },
309 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700310 .p2 = { .dot_limit = 200000,
311 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700312};
313
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300314static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400315 .dot = { .min = 20000, .max = 400000 },
316 .vco = { .min = 1400000, .max = 2800000 },
317 .n = { .min = 1, .max = 6 },
318 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100319 .m1 = { .min = 8, .max = 18 },
320 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400321 .p = { .min = 7, .max = 98 },
322 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700323 .p2 = { .dot_limit = 112000,
324 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700325};
326
Eric Anholt273e27c2011-03-30 13:01:10 -0700327
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300328static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700329 .dot = { .min = 25000, .max = 270000 },
330 .vco = { .min = 1750000, .max = 3500000},
331 .n = { .min = 1, .max = 4 },
332 .m = { .min = 104, .max = 138 },
333 .m1 = { .min = 17, .max = 23 },
334 .m2 = { .min = 5, .max = 11 },
335 .p = { .min = 10, .max = 30 },
336 .p1 = { .min = 1, .max = 3},
337 .p2 = { .dot_limit = 270000,
338 .p2_slow = 10,
339 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800340 },
Keith Packarde4b36692009-06-05 19:22:17 -0700341};
342
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300343static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700344 .dot = { .min = 22000, .max = 400000 },
345 .vco = { .min = 1750000, .max = 3500000},
346 .n = { .min = 1, .max = 4 },
347 .m = { .min = 104, .max = 138 },
348 .m1 = { .min = 16, .max = 23 },
349 .m2 = { .min = 5, .max = 11 },
350 .p = { .min = 5, .max = 80 },
351 .p1 = { .min = 1, .max = 8},
352 .p2 = { .dot_limit = 165000,
353 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700354};
355
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300356static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700357 .dot = { .min = 20000, .max = 115000 },
358 .vco = { .min = 1750000, .max = 3500000 },
359 .n = { .min = 1, .max = 3 },
360 .m = { .min = 104, .max = 138 },
361 .m1 = { .min = 17, .max = 23 },
362 .m2 = { .min = 5, .max = 11 },
363 .p = { .min = 28, .max = 112 },
364 .p1 = { .min = 2, .max = 8 },
365 .p2 = { .dot_limit = 0,
366 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800367 },
Keith Packarde4b36692009-06-05 19:22:17 -0700368};
369
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300370static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700371 .dot = { .min = 80000, .max = 224000 },
372 .vco = { .min = 1750000, .max = 3500000 },
373 .n = { .min = 1, .max = 3 },
374 .m = { .min = 104, .max = 138 },
375 .m1 = { .min = 17, .max = 23 },
376 .m2 = { .min = 5, .max = 11 },
377 .p = { .min = 14, .max = 42 },
378 .p1 = { .min = 2, .max = 6 },
379 .p2 = { .dot_limit = 0,
380 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800381 },
Keith Packarde4b36692009-06-05 19:22:17 -0700382};
383
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300384static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400385 .dot = { .min = 20000, .max = 400000},
386 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700387 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400388 .n = { .min = 3, .max = 6 },
389 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700390 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400391 .m1 = { .min = 0, .max = 0 },
392 .m2 = { .min = 0, .max = 254 },
393 .p = { .min = 5, .max = 80 },
394 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700395 .p2 = { .dot_limit = 200000,
396 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700397};
398
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300399static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400400 .dot = { .min = 20000, .max = 400000 },
401 .vco = { .min = 1700000, .max = 3500000 },
402 .n = { .min = 3, .max = 6 },
403 .m = { .min = 2, .max = 256 },
404 .m1 = { .min = 0, .max = 0 },
405 .m2 = { .min = 0, .max = 254 },
406 .p = { .min = 7, .max = 112 },
407 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700408 .p2 = { .dot_limit = 112000,
409 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700410};
411
Eric Anholt273e27c2011-03-30 13:01:10 -0700412/* Ironlake / Sandybridge
413 *
414 * We calculate clock using (register_value + 2) for N/M1/M2, so here
415 * the range value for them is (actual_value - 2).
416 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300417static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700418 .dot = { .min = 25000, .max = 350000 },
419 .vco = { .min = 1760000, .max = 3510000 },
420 .n = { .min = 1, .max = 5 },
421 .m = { .min = 79, .max = 127 },
422 .m1 = { .min = 12, .max = 22 },
423 .m2 = { .min = 5, .max = 9 },
424 .p = { .min = 5, .max = 80 },
425 .p1 = { .min = 1, .max = 8 },
426 .p2 = { .dot_limit = 225000,
427 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700428};
429
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300430static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700431 .dot = { .min = 25000, .max = 350000 },
432 .vco = { .min = 1760000, .max = 3510000 },
433 .n = { .min = 1, .max = 3 },
434 .m = { .min = 79, .max = 118 },
435 .m1 = { .min = 12, .max = 22 },
436 .m2 = { .min = 5, .max = 9 },
437 .p = { .min = 28, .max = 112 },
438 .p1 = { .min = 2, .max = 8 },
439 .p2 = { .dot_limit = 225000,
440 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800441};
442
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300443static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700444 .dot = { .min = 25000, .max = 350000 },
445 .vco = { .min = 1760000, .max = 3510000 },
446 .n = { .min = 1, .max = 3 },
447 .m = { .min = 79, .max = 127 },
448 .m1 = { .min = 12, .max = 22 },
449 .m2 = { .min = 5, .max = 9 },
450 .p = { .min = 14, .max = 56 },
451 .p1 = { .min = 2, .max = 8 },
452 .p2 = { .dot_limit = 225000,
453 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800454};
455
Eric Anholt273e27c2011-03-30 13:01:10 -0700456/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300457static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700458 .dot = { .min = 25000, .max = 350000 },
459 .vco = { .min = 1760000, .max = 3510000 },
460 .n = { .min = 1, .max = 2 },
461 .m = { .min = 79, .max = 126 },
462 .m1 = { .min = 12, .max = 22 },
463 .m2 = { .min = 5, .max = 9 },
464 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400465 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700466 .p2 = { .dot_limit = 225000,
467 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800468};
469
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300470static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700471 .dot = { .min = 25000, .max = 350000 },
472 .vco = { .min = 1760000, .max = 3510000 },
473 .n = { .min = 1, .max = 3 },
474 .m = { .min = 79, .max = 126 },
475 .m1 = { .min = 12, .max = 22 },
476 .m2 = { .min = 5, .max = 9 },
477 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400478 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700479 .p2 = { .dot_limit = 225000,
480 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800481};
482
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300483static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300484 /*
485 * These are the data rate limits (measured in fast clocks)
486 * since those are the strictest limits we have. The fast
487 * clock and actual rate limits are more relaxed, so checking
488 * them would make no difference.
489 */
490 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200491 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700492 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700493 .m1 = { .min = 2, .max = 3 },
494 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300495 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300496 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700497};
498
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300499static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300500 /*
501 * These are the data rate limits (measured in fast clocks)
502 * since those are the strictest limits we have. The fast
503 * clock and actual rate limits are more relaxed, so checking
504 * them would make no difference.
505 */
506 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200507 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300508 .n = { .min = 1, .max = 1 },
509 .m1 = { .min = 2, .max = 2 },
510 .m2 = { .min = 24 << 22, .max = 175 << 22 },
511 .p1 = { .min = 2, .max = 4 },
512 .p2 = { .p2_slow = 1, .p2_fast = 14 },
513};
514
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300515static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200516 /* FIXME: find real dot limits */
517 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530518 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200519 .n = { .min = 1, .max = 1 },
520 .m1 = { .min = 2, .max = 2 },
521 /* FIXME: find real m2 limits */
522 .m2 = { .min = 2 << 22, .max = 255 << 22 },
523 .p1 = { .min = 2, .max = 4 },
524 .p2 = { .p2_slow = 1, .p2_fast = 20 },
525};
526
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200527static bool
528needs_modeset(struct drm_crtc_state *state)
529{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200530 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200531}
532
Imre Deakdccbea32015-06-22 23:35:51 +0300533/*
534 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
535 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
536 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
537 * The helpers' return value is the rate of the clock that is fed to the
538 * display engine's pipe which can be the above fast dot clock rate or a
539 * divided-down version of it.
540 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500541/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300542static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800543{
Shaohua Li21778322009-02-23 15:19:16 +0800544 clock->m = clock->m2 + 2;
545 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200546 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300547 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300548 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
549 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300550
551 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800552}
553
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200554static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
555{
556 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
557}
558
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300559static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800560{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200561 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800562 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200563 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300564 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300565 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
566 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300567
568 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800569}
570
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300571static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300572{
573 clock->m = clock->m1 * clock->m2;
574 clock->p = clock->p1 * clock->p2;
575 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300576 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300577 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
578 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300579
580 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300581}
582
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300583int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300584{
585 clock->m = clock->m1 * clock->m2;
586 clock->p = clock->p1 * clock->p2;
587 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300588 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300589 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
590 clock->n << 22);
591 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300592
593 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300594}
595
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800596#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800597/**
598 * Returns whether the given set of divisors are valid for a given refclk with
599 * the given connectors.
600 */
601
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100602static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300603 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300604 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800605{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300606 if (clock->n < limit->n.min || limit->n.max < clock->n)
607 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800608 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400609 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800610 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400611 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400613 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300614
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100615 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
616 !IS_CHERRYVIEW(dev_priv) && !IS_BROXTON(dev_priv))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300617 if (clock->m1 <= clock->m2)
618 INTELPllInvalid("m1 <= m2\n");
619
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100620 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
621 !IS_BROXTON(dev_priv)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300622 if (clock->p < limit->p.min || limit->p.max < clock->p)
623 INTELPllInvalid("p out of range\n");
624 if (clock->m < limit->m.min || limit->m.max < clock->m)
625 INTELPllInvalid("m out of range\n");
626 }
627
Jesse Barnes79e53942008-11-07 14:24:08 -0800628 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400629 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800630 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
631 * connector, etc., rather than just a single range.
632 */
633 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400634 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800635
636 return true;
637}
638
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300639static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300640i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300641 const struct intel_crtc_state *crtc_state,
642 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800643{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300644 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800645
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +0300646 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800647 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100648 * For LVDS just rely on its current settings for dual-channel.
649 * We haven't figured out how to reliably set up different
650 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800651 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100652 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300653 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800654 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300655 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800656 } else {
657 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300658 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800659 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300660 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800661 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300662}
663
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200664/*
665 * Returns a set of divisors for the desired target clock with the given
666 * refclk, or FALSE. The returned values represent the clock equation:
667 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
668 *
669 * Target and reference clocks are specified in kHz.
670 *
671 * If match_clock is provided, then best_clock P divider must match the P
672 * divider from @match_clock used for LVDS downclocking.
673 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300674static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300675i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300676 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300677 int target, int refclk, struct dpll *match_clock,
678 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300679{
680 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300681 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300682 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800683
Akshay Joshi0206e352011-08-16 15:34:10 -0400684 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800685
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300686 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
687
Zhao Yakui42158662009-11-20 11:24:18 +0800688 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
689 clock.m1++) {
690 for (clock.m2 = limit->m2.min;
691 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200692 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800693 break;
694 for (clock.n = limit->n.min;
695 clock.n <= limit->n.max; clock.n++) {
696 for (clock.p1 = limit->p1.min;
697 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800698 int this_err;
699
Imre Deakdccbea32015-06-22 23:35:51 +0300700 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100701 if (!intel_PLL_is_valid(to_i915(dev),
702 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000703 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800704 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800705 if (match_clock &&
706 clock.p != match_clock->p)
707 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800708
709 this_err = abs(clock.dot - target);
710 if (this_err < err) {
711 *best_clock = clock;
712 err = this_err;
713 }
714 }
715 }
716 }
717 }
718
719 return (err != target);
720}
721
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200722/*
723 * Returns a set of divisors for the desired target clock with the given
724 * refclk, or FALSE. The returned values represent the clock equation:
725 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
726 *
727 * Target and reference clocks are specified in kHz.
728 *
729 * If match_clock is provided, then best_clock P divider must match the P
730 * divider from @match_clock used for LVDS downclocking.
731 */
Ma Lingd4906092009-03-18 20:13:27 +0800732static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300733pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200734 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300735 int target, int refclk, struct dpll *match_clock,
736 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200737{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300738 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300739 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200740 int err = target;
741
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200742 memset(best_clock, 0, sizeof(*best_clock));
743
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300744 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
745
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200746 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
747 clock.m1++) {
748 for (clock.m2 = limit->m2.min;
749 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200750 for (clock.n = limit->n.min;
751 clock.n <= limit->n.max; clock.n++) {
752 for (clock.p1 = limit->p1.min;
753 clock.p1 <= limit->p1.max; clock.p1++) {
754 int this_err;
755
Imre Deakdccbea32015-06-22 23:35:51 +0300756 pnv_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100757 if (!intel_PLL_is_valid(to_i915(dev),
758 limit,
Jesse Barnes79e53942008-11-07 14:24:08 -0800759 &clock))
760 continue;
761 if (match_clock &&
762 clock.p != match_clock->p)
763 continue;
764
765 this_err = abs(clock.dot - target);
766 if (this_err < err) {
767 *best_clock = clock;
768 err = this_err;
769 }
770 }
771 }
772 }
773 }
774
775 return (err != target);
776}
777
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200778/*
779 * Returns a set of divisors for the desired target clock with the given
780 * refclk, or FALSE. The returned values represent the clock equation:
781 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200782 *
783 * Target and reference clocks are specified in kHz.
784 *
785 * If match_clock is provided, then best_clock P divider must match the P
786 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200787 */
Ma Lingd4906092009-03-18 20:13:27 +0800788static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300789g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200790 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300791 int target, int refclk, struct dpll *match_clock,
792 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800793{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300794 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300795 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800796 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300797 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400798 /* approximately equals target * 0.00585 */
799 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800800
801 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300802
803 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
804
Ma Lingd4906092009-03-18 20:13:27 +0800805 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200806 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800807 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200808 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800809 for (clock.m1 = limit->m1.max;
810 clock.m1 >= limit->m1.min; clock.m1--) {
811 for (clock.m2 = limit->m2.max;
812 clock.m2 >= limit->m2.min; clock.m2--) {
813 for (clock.p1 = limit->p1.max;
814 clock.p1 >= limit->p1.min; clock.p1--) {
815 int this_err;
816
Imre Deakdccbea32015-06-22 23:35:51 +0300817 i9xx_calc_dpll_params(refclk, &clock);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100818 if (!intel_PLL_is_valid(to_i915(dev),
819 limit,
Chris Wilson1b894b52010-12-14 20:04:54 +0000820 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800821 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000822
823 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800824 if (this_err < err_most) {
825 *best_clock = clock;
826 err_most = this_err;
827 max_n = clock.n;
828 found = true;
829 }
830 }
831 }
832 }
833 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800834 return found;
835}
Ma Lingd4906092009-03-18 20:13:27 +0800836
Imre Deakd5dd62b2015-03-17 11:40:03 +0200837/*
838 * Check if the calculated PLL configuration is more optimal compared to the
839 * best configuration and error found so far. Return the calculated error.
840 */
841static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300842 const struct dpll *calculated_clock,
843 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200844 unsigned int best_error_ppm,
845 unsigned int *error_ppm)
846{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200847 /*
848 * For CHV ignore the error and consider only the P value.
849 * Prefer a bigger P value based on HW requirements.
850 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100851 if (IS_CHERRYVIEW(to_i915(dev))) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200852 *error_ppm = 0;
853
854 return calculated_clock->p > best_clock->p;
855 }
856
Imre Deak24be4e42015-03-17 11:40:04 +0200857 if (WARN_ON_ONCE(!target_freq))
858 return false;
859
Imre Deakd5dd62b2015-03-17 11:40:03 +0200860 *error_ppm = div_u64(1000000ULL *
861 abs(target_freq - calculated_clock->dot),
862 target_freq);
863 /*
864 * Prefer a better P value over a better (smaller) error if the error
865 * is small. Ensure this preference for future configurations too by
866 * setting the error to 0.
867 */
868 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
869 *error_ppm = 0;
870
871 return true;
872 }
873
874 return *error_ppm + 10 < best_error_ppm;
875}
876
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200877/*
878 * Returns a set of divisors for the desired target clock with the given
879 * refclk, or FALSE. The returned values represent the clock equation:
880 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
881 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800882static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300883vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200884 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300885 int target, int refclk, struct dpll *match_clock,
886 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700887{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200888 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300889 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300890 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300891 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300892 /* min update 19.2 MHz */
893 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300894 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700895
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300896 target *= 5; /* fast clock */
897
898 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700899
900 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300901 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300902 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300903 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300904 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300905 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700906 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300907 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200908 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300909
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300910 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
911 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300912
Imre Deakdccbea32015-06-22 23:35:51 +0300913 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300914
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100915 if (!intel_PLL_is_valid(to_i915(dev),
916 limit,
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300917 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300918 continue;
919
Imre Deakd5dd62b2015-03-17 11:40:03 +0200920 if (!vlv_PLL_is_optimal(dev, target,
921 &clock,
922 best_clock,
923 bestppm, &ppm))
924 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300925
Imre Deakd5dd62b2015-03-17 11:40:03 +0200926 *best_clock = clock;
927 bestppm = ppm;
928 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700929 }
930 }
931 }
932 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700933
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300934 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700935}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700936
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200937/*
938 * Returns a set of divisors for the desired target clock with the given
939 * refclk, or FALSE. The returned values represent the clock equation:
940 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
941 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300942static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300943chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200944 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300945 int target, int refclk, struct dpll *match_clock,
946 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300947{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200948 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300949 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200950 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300951 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300952 uint64_t m2;
953 int found = false;
954
955 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200956 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300957
958 /*
959 * Based on hardware doc, the n always set to 1, and m1 always
960 * set to 2. If requires to support 200Mhz refclk, we need to
961 * revisit this because n may not 1 anymore.
962 */
963 clock.n = 1, clock.m1 = 2;
964 target *= 5; /* fast clock */
965
966 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
967 for (clock.p2 = limit->p2.p2_fast;
968 clock.p2 >= limit->p2.p2_slow;
969 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200970 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300971
972 clock.p = clock.p1 * clock.p2;
973
974 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
975 clock.n) << 22, refclk * clock.m1);
976
977 if (m2 > INT_MAX/clock.m1)
978 continue;
979
980 clock.m2 = m2;
981
Imre Deakdccbea32015-06-22 23:35:51 +0300982 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300983
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100984 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300985 continue;
986
Imre Deak9ca3ba02015-03-17 11:40:05 +0200987 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
988 best_error_ppm, &error_ppm))
989 continue;
990
991 *best_clock = clock;
992 best_error_ppm = error_ppm;
993 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300994 }
995 }
996
997 return found;
998}
999
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001000bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03001001 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001002{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001003 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03001004 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001005
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001006 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001007 target_clock, refclk, NULL, best_clock);
1008}
1009
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001010bool intel_crtc_active(struct drm_crtc *crtc)
1011{
1012 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1013
1014 /* Be paranoid as we can arrive here with only partial
1015 * state retrieved from the hardware during setup.
1016 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001017 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001018 * as Haswell has gained clock readout/fastboot support.
1019 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001020 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001021 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001022 *
1023 * FIXME: The intel_crtc->active here should be switched to
1024 * crtc->state->active once we have proper CRTC states wired up
1025 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001026 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001027 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001028 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001029}
1030
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001031enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1032 enum pipe pipe)
1033{
1034 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1035 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1036
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001037 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001038}
1039
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001040static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1041{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001042 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001043 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001044 u32 line1, line2;
1045 u32 line_mask;
1046
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001047 if (IS_GEN2(dev_priv))
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001048 line_mask = DSL_LINEMASK_GEN2;
1049 else
1050 line_mask = DSL_LINEMASK_GEN3;
1051
1052 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001053 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001054 line2 = I915_READ(reg) & line_mask;
1055
1056 return line1 == line2;
1057}
1058
Keith Packardab7ad7f2010-10-03 00:33:06 -07001059/*
1060 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001061 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001062 *
1063 * After disabling a pipe, we can't wait for vblank in the usual way,
1064 * spinning on the vblank interrupt status bit, since we won't actually
1065 * see an interrupt when the pipe is disabled.
1066 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001067 * On Gen4 and above:
1068 * wait for the pipe register state bit to turn off
1069 *
1070 * Otherwise:
1071 * wait for the display line value to settle (it usually
1072 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001073 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001074 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001075static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001076{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001077 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001078 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001079 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001080 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001081
Keith Packardab7ad7f2010-10-03 00:33:06 -07001082 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001083 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001084
Keith Packardab7ad7f2010-10-03 00:33:06 -07001085 /* Wait for the Pipe State to go off */
Chris Wilsonb8511f52016-06-30 15:32:53 +01001086 if (intel_wait_for_register(dev_priv,
1087 reg, I965_PIPECONF_ACTIVE, 0,
1088 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001089 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001090 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001091 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001092 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001093 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001094 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001095}
1096
Jesse Barnesb24e7172011-01-04 15:09:30 -08001097/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001098void assert_pll(struct drm_i915_private *dev_priv,
1099 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001100{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001101 u32 val;
1102 bool cur_state;
1103
Ville Syrjälä649636e2015-09-22 19:50:01 +03001104 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001105 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001106 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001107 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001108 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001109}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001110
Jani Nikula23538ef2013-08-27 15:12:22 +03001111/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001112void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001113{
1114 u32 val;
1115 bool cur_state;
1116
Ville Syrjäläa5805162015-05-26 20:42:30 +03001117 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001118 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001119 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001120
1121 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001122 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001123 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001124 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001125}
Jani Nikula23538ef2013-08-27 15:12:22 +03001126
Jesse Barnes040484a2011-01-03 12:14:26 -08001127static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1128 enum pipe pipe, bool state)
1129{
Jesse Barnes040484a2011-01-03 12:14:26 -08001130 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001131 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1132 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001133
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001134 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001135 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001136 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001137 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001138 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001139 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001140 cur_state = !!(val & FDI_TX_ENABLE);
1141 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001142 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001143 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001144 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001145}
1146#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1147#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1148
1149static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1150 enum pipe pipe, bool state)
1151{
Jesse Barnes040484a2011-01-03 12:14:26 -08001152 u32 val;
1153 bool cur_state;
1154
Ville Syrjälä649636e2015-09-22 19:50:01 +03001155 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001156 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001157 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001158 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001159 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001160}
1161#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1162#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1163
1164static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1165 enum pipe pipe)
1166{
Jesse Barnes040484a2011-01-03 12:14:26 -08001167 u32 val;
1168
1169 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001170 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001171 return;
1172
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001173 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001174 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001175 return;
1176
Ville Syrjälä649636e2015-09-22 19:50:01 +03001177 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001178 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 -08001179}
1180
Daniel Vetter55607e82013-06-16 21:42:39 +02001181void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1182 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001183{
Jesse Barnes040484a2011-01-03 12:14:26 -08001184 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001185 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001186
Ville Syrjälä649636e2015-09-22 19:50:01 +03001187 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001188 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001189 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001190 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001191 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001192}
1193
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001194void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001195{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001196 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001197 u32 val;
1198 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001199 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001200
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001201 if (WARN_ON(HAS_DDI(dev_priv)))
Jani Nikulabedd4db2014-08-22 15:04:13 +03001202 return;
1203
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001204 if (HAS_PCH_SPLIT(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001205 u32 port_sel;
1206
Imre Deak44cb7342016-08-10 14:07:29 +03001207 pp_reg = PP_CONTROL(0);
1208 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001209
1210 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1211 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1212 panel_pipe = PIPE_B;
1213 /* XXX: else fix for eDP */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01001214 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001215 /* presumably write lock depends on pipe, not port select */
Imre Deak44cb7342016-08-10 14:07:29 +03001216 pp_reg = PP_CONTROL(pipe);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001217 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001218 } else {
Imre Deak44cb7342016-08-10 14:07:29 +03001219 pp_reg = PP_CONTROL(0);
Jani Nikulabedd4db2014-08-22 15:04:13 +03001220 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1221 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001222 }
1223
1224 val = I915_READ(pp_reg);
1225 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001226 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001227 locked = false;
1228
Rob Clarke2c719b2014-12-15 13:56:32 -05001229 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001230 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001231 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001232}
1233
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001234static void assert_cursor(struct drm_i915_private *dev_priv,
1235 enum pipe pipe, bool state)
1236{
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001237 bool cur_state;
1238
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001239 if (IS_845G(dev_priv) || IS_I865G(dev_priv))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001240 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001241 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001242 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001243
Rob Clarke2c719b2014-12-15 13:56:32 -05001244 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001245 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001246 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001247}
1248#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1249#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1250
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001251void assert_pipe(struct drm_i915_private *dev_priv,
1252 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001253{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001254 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001255 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1256 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001257 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001258
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001259 /* if we need the pipe quirk it must be always on */
1260 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1261 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001262 state = true;
1263
Imre Deak4feed0e2016-02-12 18:55:14 +02001264 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1265 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001266 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001267 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001268
1269 intel_display_power_put(dev_priv, power_domain);
1270 } else {
1271 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001272 }
1273
Rob Clarke2c719b2014-12-15 13:56:32 -05001274 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001275 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001276 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001277}
1278
Chris Wilson931872f2012-01-16 23:01:13 +00001279static void assert_plane(struct drm_i915_private *dev_priv,
1280 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001281{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001282 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001283 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001284
Ville Syrjälä649636e2015-09-22 19:50:01 +03001285 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001286 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001287 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001288 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001289 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001290}
1291
Chris Wilson931872f2012-01-16 23:01:13 +00001292#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1293#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1294
Jesse Barnesb24e7172011-01-04 15:09:30 -08001295static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1296 enum pipe pipe)
1297{
Chris Wilson91c8a322016-07-05 10:40:23 +01001298 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001299 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001300
Ville Syrjälä653e1022013-06-04 13:49:05 +03001301 /* Primary planes are fixed to pipes on gen4+ */
1302 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001303 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001304 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001305 "plane %c assertion failure, should be disabled but not\n",
1306 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001307 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001308 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001309
Jesse Barnesb24e7172011-01-04 15:09:30 -08001310 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001311 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001312 u32 val = I915_READ(DSPCNTR(i));
1313 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001314 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001315 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001316 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1317 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001318 }
1319}
1320
Jesse Barnes19332d72013-03-28 09:55:38 -07001321static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1322 enum pipe pipe)
1323{
Chris Wilson91c8a322016-07-05 10:40:23 +01001324 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001325 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001326
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001327 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001328 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001329 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001330 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001331 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1332 sprite, pipe_name(pipe));
1333 }
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01001334 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001335 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001336 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001337 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001338 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001339 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001340 }
1341 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001342 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001343 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001344 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001345 plane_name(pipe), pipe_name(pipe));
1346 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001347 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001348 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001349 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1350 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001351 }
1352}
1353
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001354static void assert_vblank_disabled(struct drm_crtc *crtc)
1355{
Rob Clarke2c719b2014-12-15 13:56:32 -05001356 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001357 drm_crtc_vblank_put(crtc);
1358}
1359
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001360void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1361 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001362{
Jesse Barnes92f25842011-01-04 15:09:34 -08001363 u32 val;
1364 bool enabled;
1365
Ville Syrjälä649636e2015-09-22 19:50:01 +03001366 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001367 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001368 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001369 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1370 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001371}
1372
Keith Packard4e634382011-08-06 10:39:45 -07001373static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1374 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001375{
1376 if ((val & DP_PORT_EN) == 0)
1377 return false;
1378
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001379 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001380 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001381 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1382 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001383 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001384 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1385 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001386 } else {
1387 if ((val & DP_PIPE_MASK) != (pipe << 30))
1388 return false;
1389 }
1390 return true;
1391}
1392
Keith Packard1519b992011-08-06 10:35:34 -07001393static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1394 enum pipe pipe, u32 val)
1395{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001396 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001397 return false;
1398
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001399 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001400 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001401 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001402 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001403 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1404 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001405 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001406 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001407 return false;
1408 }
1409 return true;
1410}
1411
1412static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1413 enum pipe pipe, u32 val)
1414{
1415 if ((val & LVDS_PORT_EN) == 0)
1416 return false;
1417
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001418 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001419 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1420 return false;
1421 } else {
1422 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1423 return false;
1424 }
1425 return true;
1426}
1427
1428static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1429 enum pipe pipe, u32 val)
1430{
1431 if ((val & ADPA_DAC_ENABLE) == 0)
1432 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001433 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001434 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1435 return false;
1436 } else {
1437 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1438 return false;
1439 }
1440 return true;
1441}
1442
Jesse Barnes291906f2011-02-02 12:28:03 -08001443static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001444 enum pipe pipe, i915_reg_t reg,
1445 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001446{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001447 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001448 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001449 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001450 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001451
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001452 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001453 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001454 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001455}
1456
1457static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001458 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001459{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001460 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001461 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001462 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001463 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001464
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001465 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001466 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001467 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001468}
1469
1470static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1471 enum pipe pipe)
1472{
Jesse Barnes291906f2011-02-02 12:28:03 -08001473 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001474
Keith Packardf0575e92011-07-25 22:12:43 -07001475 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1476 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1477 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001478
Ville Syrjälä649636e2015-09-22 19:50:01 +03001479 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001480 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001481 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001482 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001483
Ville Syrjälä649636e2015-09-22 19:50:01 +03001484 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001485 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001486 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001487 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001488
Paulo Zanonie2debe92013-02-18 19:00:27 -03001489 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1490 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1491 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001492}
1493
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001494static void _vlv_enable_pll(struct intel_crtc *crtc,
1495 const struct intel_crtc_state *pipe_config)
1496{
1497 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1498 enum pipe pipe = crtc->pipe;
1499
1500 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1501 POSTING_READ(DPLL(pipe));
1502 udelay(150);
1503
Chris Wilson2c30b432016-06-30 15:32:54 +01001504 if (intel_wait_for_register(dev_priv,
1505 DPLL(pipe),
1506 DPLL_LOCK_VLV,
1507 DPLL_LOCK_VLV,
1508 1))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001509 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1510}
1511
Ville Syrjäläd288f652014-10-28 13:20:22 +02001512static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001513 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001514{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001515 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001516 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001517
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001518 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001519
Daniel Vetter87442f72013-06-06 00:52:17 +02001520 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001521 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001522
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001523 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1524 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001525
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001526 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1527 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001528}
1529
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001530
1531static void _chv_enable_pll(struct intel_crtc *crtc,
1532 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001533{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001534 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001535 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001536 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001537 u32 tmp;
1538
Ville Syrjäläa5805162015-05-26 20:42:30 +03001539 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001540
1541 /* Enable back the 10bit clock to display controller */
1542 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1543 tmp |= DPIO_DCLKP_EN;
1544 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1545
Ville Syrjälä54433e92015-05-26 20:42:31 +03001546 mutex_unlock(&dev_priv->sb_lock);
1547
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001548 /*
1549 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1550 */
1551 udelay(1);
1552
1553 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001554 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001555
1556 /* Check PLL is locked */
Chris Wilson6b188262016-06-30 15:32:55 +01001557 if (intel_wait_for_register(dev_priv,
1558 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1559 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001560 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001561}
1562
1563static void chv_enable_pll(struct intel_crtc *crtc,
1564 const struct intel_crtc_state *pipe_config)
1565{
1566 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1567 enum pipe pipe = crtc->pipe;
1568
1569 assert_pipe_disabled(dev_priv, pipe);
1570
1571 /* PLL is protected by panel, make sure we can write it */
1572 assert_panel_unlocked(dev_priv, pipe);
1573
1574 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1575 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001576
Ville Syrjäläc2317752016-03-15 16:39:56 +02001577 if (pipe != PIPE_A) {
1578 /*
1579 * WaPixelRepeatModeFixForC0:chv
1580 *
1581 * DPLLCMD is AWOL. Use chicken bits to propagate
1582 * the value from DPLLBMD to either pipe B or C.
1583 */
1584 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1585 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1586 I915_WRITE(CBR4_VLV, 0);
1587 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1588
1589 /*
1590 * DPLLB VGA mode also seems to cause problems.
1591 * We should always have it disabled.
1592 */
1593 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1594 } else {
1595 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1596 POSTING_READ(DPLL_MD(pipe));
1597 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001598}
1599
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001600static int intel_num_dvo_pipes(struct drm_device *dev)
1601{
1602 struct intel_crtc *crtc;
1603 int count = 0;
1604
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001605 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001606 count += crtc->base.state->active &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001607 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1608 }
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001609
1610 return count;
1611}
1612
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001613static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001614{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001615 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001616 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001617 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001618 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001619
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001620 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001621
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001622 /* PLL is protected by panel, make sure we can write it */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001623 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001624 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001625
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001626 /* Enable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001627 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev) > 0) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001628 /*
1629 * It appears to be important that we don't enable this
1630 * for the current pipe before otherwise configuring the
1631 * PLL. No idea how this should be handled if multiple
1632 * DVO outputs are enabled simultaneosly.
1633 */
1634 dpll |= DPLL_DVO_2X_MODE;
1635 I915_WRITE(DPLL(!crtc->pipe),
1636 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1637 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001638
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001639 /*
1640 * Apparently we need to have VGA mode enabled prior to changing
1641 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1642 * dividers, even though the register value does change.
1643 */
1644 I915_WRITE(reg, 0);
1645
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001646 I915_WRITE(reg, dpll);
1647
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001648 /* Wait for the clocks to stabilize. */
1649 POSTING_READ(reg);
1650 udelay(150);
1651
1652 if (INTEL_INFO(dev)->gen >= 4) {
1653 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001654 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001655 } else {
1656 /* The pixel multiplier can only be updated once the
1657 * DPLL is enabled and the clocks are stable.
1658 *
1659 * So write it again.
1660 */
1661 I915_WRITE(reg, dpll);
1662 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001663
1664 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001665 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001666 POSTING_READ(reg);
1667 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001668 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001669 POSTING_READ(reg);
1670 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001671 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001672 POSTING_READ(reg);
1673 udelay(150); /* wait for warmup */
1674}
1675
1676/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001677 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001678 * @dev_priv: i915 private structure
1679 * @pipe: pipe PLL to disable
1680 *
1681 * Disable the PLL for @pipe, making sure the pipe is off first.
1682 *
1683 * Note! This is for pre-ILK only.
1684 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001685static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001686{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001687 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001688 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001689 enum pipe pipe = crtc->pipe;
1690
1691 /* Disable DVO 2x clock on both PLLs if necessary */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001692 if (IS_I830(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001693 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001694 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001695 I915_WRITE(DPLL(PIPE_B),
1696 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1697 I915_WRITE(DPLL(PIPE_A),
1698 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1699 }
1700
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001701 /* Don't disable pipe or pipe PLLs if needed */
1702 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1703 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001704 return;
1705
1706 /* Make sure the pipe isn't still relying on us */
1707 assert_pipe_disabled(dev_priv, pipe);
1708
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001709 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001710 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001711}
1712
Jesse Barnesf6071162013-10-01 10:41:38 -07001713static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1714{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001715 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001716
1717 /* Make sure the pipe isn't still relying on us */
1718 assert_pipe_disabled(dev_priv, pipe);
1719
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02001720 val = DPLL_INTEGRATED_REF_CLK_VLV |
1721 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1722 if (pipe != PIPE_A)
1723 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1724
Jesse Barnesf6071162013-10-01 10:41:38 -07001725 I915_WRITE(DPLL(pipe), val);
1726 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001727}
1728
1729static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1730{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001731 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001732 u32 val;
1733
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001734 /* Make sure the pipe isn't still relying on us */
1735 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001736
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001737 val = DPLL_SSC_REF_CLK_CHV |
1738 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001739 if (pipe != PIPE_A)
1740 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02001741
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001742 I915_WRITE(DPLL(pipe), val);
1743 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001744
Ville Syrjäläa5805162015-05-26 20:42:30 +03001745 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001746
1747 /* Disable 10bit clock to display controller */
1748 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1749 val &= ~DPIO_DCLKP_EN;
1750 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1751
Ville Syrjäläa5805162015-05-26 20:42:30 +03001752 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001753}
1754
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001755void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001756 struct intel_digital_port *dport,
1757 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001758{
1759 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001760 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001761
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001762 switch (dport->port) {
1763 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001764 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001765 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001766 break;
1767 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001768 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001769 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001770 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001771 break;
1772 case PORT_D:
1773 port_mask = DPLL_PORTD_READY_MASK;
1774 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001775 break;
1776 default:
1777 BUG();
1778 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001779
Chris Wilson370004d2016-06-30 15:32:56 +01001780 if (intel_wait_for_register(dev_priv,
1781 dpll_reg, port_mask, expected_mask,
1782 1000))
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001783 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1784 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001785}
1786
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001787static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1788 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001789{
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001790 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001791 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001792 i915_reg_t reg;
1793 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001794
Jesse Barnes040484a2011-01-03 12:14:26 -08001795 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001796 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001797
1798 /* FDI must be feeding us bits for PCH ports */
1799 assert_fdi_tx_enabled(dev_priv, pipe);
1800 assert_fdi_rx_enabled(dev_priv, pipe);
1801
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001802 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001803 /* Workaround: Set the timing override bit before enabling the
1804 * pch transcoder. */
1805 reg = TRANS_CHICKEN2(pipe);
1806 val = I915_READ(reg);
1807 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1808 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001809 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001810
Daniel Vetterab9412b2013-05-03 11:49:46 +02001811 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001812 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001813 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001814
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001815 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001816 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001817 * Make the BPC in transcoder be consistent with
1818 * that in pipeconf reg. For HDMI we must use 8bpc
1819 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001820 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001821 val &= ~PIPECONF_BPC_MASK;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001822 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001823 val |= PIPECONF_8BPC;
1824 else
1825 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001826 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001827
1828 val &= ~TRANS_INTERLACE_MASK;
1829 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001830 if (HAS_PCH_IBX(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001831 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001832 val |= TRANS_LEGACY_INTERLACED_ILK;
1833 else
1834 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001835 else
1836 val |= TRANS_PROGRESSIVE;
1837
Jesse Barnes040484a2011-01-03 12:14:26 -08001838 I915_WRITE(reg, val | TRANS_ENABLE);
Chris Wilson650fbd82016-06-30 15:32:57 +01001839 if (intel_wait_for_register(dev_priv,
1840 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1841 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001842 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001843}
1844
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001845static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001846 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001847{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001848 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001849
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001850 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001851 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001852 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001853
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001854 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001855 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001856 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001857 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001858
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001859 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001860 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001861
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001862 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1863 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001864 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001865 else
1866 val |= TRANS_PROGRESSIVE;
1867
Daniel Vetterab9412b2013-05-03 11:49:46 +02001868 I915_WRITE(LPT_TRANSCONF, val);
Chris Wilsond9f96242016-06-30 15:32:58 +01001869 if (intel_wait_for_register(dev_priv,
1870 LPT_TRANSCONF,
1871 TRANS_STATE_ENABLE,
1872 TRANS_STATE_ENABLE,
1873 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001874 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001875}
1876
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001877static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1878 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001879{
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001880 i915_reg_t reg;
1881 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001882
1883 /* FDI relies on the transcoder */
1884 assert_fdi_tx_disabled(dev_priv, pipe);
1885 assert_fdi_rx_disabled(dev_priv, pipe);
1886
Jesse Barnes291906f2011-02-02 12:28:03 -08001887 /* Ports must be off as well */
1888 assert_pch_ports_disabled(dev_priv, pipe);
1889
Daniel Vetterab9412b2013-05-03 11:49:46 +02001890 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001891 val = I915_READ(reg);
1892 val &= ~TRANS_ENABLE;
1893 I915_WRITE(reg, val);
1894 /* wait for PCH transcoder off, transcoder state */
Chris Wilsona7d04662016-06-30 15:32:59 +01001895 if (intel_wait_for_register(dev_priv,
1896 reg, TRANS_STATE_ENABLE, 0,
1897 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001898 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001899
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001900 if (HAS_PCH_CPT(dev_priv)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001901 /* Workaround: Clear the timing override chicken bit again. */
1902 reg = TRANS_CHICKEN2(pipe);
1903 val = I915_READ(reg);
1904 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1905 I915_WRITE(reg, val);
1906 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001907}
1908
Maarten Lankhorstb7076542016-08-23 16:18:08 +02001909void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001910{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001911 u32 val;
1912
Daniel Vetterab9412b2013-05-03 11:49:46 +02001913 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001914 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001915 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001916 /* wait for PCH transcoder off, transcoder state */
Chris Wilsondfdb4742016-06-30 15:33:00 +01001917 if (intel_wait_for_register(dev_priv,
1918 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1919 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001920 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001921
1922 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001923 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001924 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001925 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001926}
1927
Ville Syrjälä65f21302016-10-14 20:02:53 +03001928enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1929{
1930 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1931
1932 WARN_ON(!crtc->config->has_pch_encoder);
1933
1934 if (HAS_PCH_LPT(dev_priv))
1935 return TRANSCODER_A;
1936 else
1937 return (enum transcoder) crtc->pipe;
1938}
1939
Jesse Barnes92f25842011-01-04 15:09:34 -08001940/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001941 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001942 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001943 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001944 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001945 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001946 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001947static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001948{
Paulo Zanoni03722642014-01-17 13:51:09 -02001949 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001950 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni03722642014-01-17 13:51:09 -02001951 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001952 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001953 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001954 u32 val;
1955
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001956 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1957
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001958 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001959 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001960 assert_sprites_disabled(dev_priv, pipe);
1961
Jesse Barnesb24e7172011-01-04 15:09:30 -08001962 /*
1963 * A pipe without a PLL won't actually be able to drive bits from
1964 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1965 * need the check.
1966 */
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001967 if (HAS_GMCH_DISPLAY(dev_priv)) {
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03001968 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03001969 assert_dsi_pll_enabled(dev_priv);
1970 else
1971 assert_pll_enabled(dev_priv, pipe);
Ville Syrjälä09fa8bb2016-08-05 20:41:34 +03001972 } else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001973 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001974 /* if driving the PCH, we need FDI enabled */
Ville Syrjälä65f21302016-10-14 20:02:53 +03001975 assert_fdi_rx_pll_enabled(dev_priv,
1976 (enum pipe) intel_crtc_pch_transcoder(crtc));
Daniel Vetter1a240d42012-11-29 22:18:51 +01001977 assert_fdi_tx_pll_enabled(dev_priv,
1978 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001979 }
1980 /* FIXME: assert CPU port conditions for SNB+ */
1981 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001982
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001983 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001984 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001985 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001986 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1987 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00001988 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001989 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001990
1991 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001992 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001993
1994 /*
1995 * Until the pipe starts DSL will read as 0, which would cause
1996 * an apparent vblank timestamp jump, which messes up also the
1997 * frame count when it's derived from the timestamps. So let's
1998 * wait for the pipe to start properly before we call
1999 * drm_crtc_vblank_on()
2000 */
2001 if (dev->max_vblank_count == 0 &&
2002 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2003 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002004}
2005
2006/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002007 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002008 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002009 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002010 * Disable the pipe of @crtc, making sure that various hardware
2011 * specific requirements are met, if applicable, e.g. plane
2012 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002013 *
2014 * Will wait until the pipe has shut down before returning.
2015 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002016static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002017{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002018 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002019 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002020 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002021 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002022 u32 val;
2023
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002024 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2025
Jesse Barnesb24e7172011-01-04 15:09:30 -08002026 /*
2027 * Make sure planes won't keep trying to pump pixels to us,
2028 * or we might hang the display.
2029 */
2030 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002031 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002032 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002033
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002034 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002035 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002036 if ((val & PIPECONF_ENABLE) == 0)
2037 return;
2038
Ville Syrjälä67adc642014-08-15 01:21:57 +03002039 /*
2040 * Double wide has implications for planes
2041 * so best keep it disabled when not needed.
2042 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002043 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002044 val &= ~PIPECONF_DOUBLE_WIDE;
2045
2046 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002047 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2048 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002049 val &= ~PIPECONF_ENABLE;
2050
2051 I915_WRITE(reg, val);
2052 if ((val & PIPECONF_ENABLE) == 0)
2053 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002054}
2055
Ville Syrjälä832be822016-01-12 21:08:33 +02002056static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2057{
2058 return IS_GEN2(dev_priv) ? 2048 : 4096;
2059}
2060
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002061static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2062 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002063{
2064 switch (fb_modifier) {
2065 case DRM_FORMAT_MOD_NONE:
2066 return cpp;
2067 case I915_FORMAT_MOD_X_TILED:
2068 if (IS_GEN2(dev_priv))
2069 return 128;
2070 else
2071 return 512;
2072 case I915_FORMAT_MOD_Y_TILED:
2073 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2074 return 128;
2075 else
2076 return 512;
2077 case I915_FORMAT_MOD_Yf_TILED:
2078 switch (cpp) {
2079 case 1:
2080 return 64;
2081 case 2:
2082 case 4:
2083 return 128;
2084 case 8:
2085 case 16:
2086 return 256;
2087 default:
2088 MISSING_CASE(cpp);
2089 return cpp;
2090 }
2091 break;
2092 default:
2093 MISSING_CASE(fb_modifier);
2094 return cpp;
2095 }
2096}
2097
Ville Syrjälä832be822016-01-12 21:08:33 +02002098unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2099 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002100{
Ville Syrjälä832be822016-01-12 21:08:33 +02002101 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2102 return 1;
2103 else
2104 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002105 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002106}
2107
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002108/* Return the tile dimensions in pixel units */
2109static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2110 unsigned int *tile_width,
2111 unsigned int *tile_height,
2112 uint64_t fb_modifier,
2113 unsigned int cpp)
2114{
2115 unsigned int tile_width_bytes =
2116 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2117
2118 *tile_width = tile_width_bytes / cpp;
2119 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2120}
2121
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002122unsigned int
2123intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002124 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002125{
Ville Syrjälä832be822016-01-12 21:08:33 +02002126 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2127 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2128
2129 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002130}
2131
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002132unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2133{
2134 unsigned int size = 0;
2135 int i;
2136
2137 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2138 size += rot_info->plane[i].width * rot_info->plane[i].height;
2139
2140 return size;
2141}
2142
Daniel Vetter75c82a52015-10-14 16:51:04 +02002143static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002144intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2145 const struct drm_framebuffer *fb,
2146 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002147{
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002148 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002149 *view = i915_ggtt_view_rotated;
2150 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2151 } else {
2152 *view = i915_ggtt_view_normal;
2153 }
2154}
2155
Ville Syrjälä603525d2016-01-12 21:08:37 +02002156static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002157{
2158 if (INTEL_INFO(dev_priv)->gen >= 9)
2159 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002160 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002161 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002162 return 128 * 1024;
2163 else if (INTEL_INFO(dev_priv)->gen >= 4)
2164 return 4 * 1024;
2165 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002166 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002167}
2168
Ville Syrjälä603525d2016-01-12 21:08:37 +02002169static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2170 uint64_t fb_modifier)
2171{
2172 switch (fb_modifier) {
2173 case DRM_FORMAT_MOD_NONE:
2174 return intel_linear_alignment(dev_priv);
2175 case I915_FORMAT_MOD_X_TILED:
2176 if (INTEL_INFO(dev_priv)->gen >= 9)
2177 return 256 * 1024;
2178 return 0;
2179 case I915_FORMAT_MOD_Y_TILED:
2180 case I915_FORMAT_MOD_Yf_TILED:
2181 return 1 * 1024 * 1024;
2182 default:
2183 MISSING_CASE(fb_modifier);
2184 return 0;
2185 }
2186}
2187
Chris Wilson058d88c2016-08-15 10:49:06 +01002188struct i915_vma *
2189intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002190{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002191 struct drm_device *dev = fb->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002192 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002193 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002194 struct i915_ggtt_view view;
Chris Wilson058d88c2016-08-15 10:49:06 +01002195 struct i915_vma *vma;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002196 u32 alignment;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002197
Matt Roperebcdd392014-07-09 16:22:11 -07002198 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2199
Ville Syrjälä603525d2016-01-12 21:08:37 +02002200 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002201
Ville Syrjälä3465c582016-02-15 22:54:43 +02002202 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002203
Chris Wilson693db182013-03-05 14:52:39 +00002204 /* Note that the w/a also requires 64 PTE of padding following the
2205 * bo. We currently fill all unused PTE with the shadow page and so
2206 * we should always have valid PTE following the scanout preventing
2207 * the VT-d warning.
2208 */
Chris Wilson48f112f2016-06-24 14:07:14 +01002209 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
Chris Wilson693db182013-03-05 14:52:39 +00002210 alignment = 256 * 1024;
2211
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002212 /*
2213 * Global gtt pte registers are special registers which actually forward
2214 * writes to a chunk of system memory. Which means that there is no risk
2215 * that the register values disappear as soon as we call
2216 * intel_runtime_pm_put(), so it is correct to wrap only the
2217 * pin/unpin/fence and not more.
2218 */
2219 intel_runtime_pm_get(dev_priv);
2220
Chris Wilson058d88c2016-08-15 10:49:06 +01002221 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
Chris Wilson49ef5292016-08-18 17:17:00 +01002222 if (IS_ERR(vma))
2223 goto err;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002224
Chris Wilson05a20d02016-08-18 17:16:55 +01002225 if (i915_vma_is_map_and_fenceable(vma)) {
Chris Wilson49ef5292016-08-18 17:17:00 +01002226 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2227 * fence, whereas 965+ only requires a fence if using
2228 * framebuffer compression. For simplicity, we always, when
2229 * possible, install a fence as the cost is not that onerous.
2230 *
2231 * If we fail to fence the tiled scanout, then either the
2232 * modeset will reject the change (which is highly unlikely as
2233 * the affected systems, all but one, do not have unmappable
2234 * space) or we will not be able to enable full powersaving
2235 * techniques (also likely not to apply due to various limits
2236 * FBC and the like impose on the size of the buffer, which
2237 * presumably we violated anyway with this unmappable buffer).
2238 * Anyway, it is presumably better to stumble onwards with
2239 * something and try to run the system in a "less than optimal"
2240 * mode that matches the user configuration.
2241 */
2242 if (i915_vma_get_fence(vma) == 0)
2243 i915_vma_pin_fence(vma);
Vivek Kasireddy98072162015-10-29 18:54:38 -07002244 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002245
Chris Wilson49ef5292016-08-18 17:17:00 +01002246err:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002247 intel_runtime_pm_put(dev_priv);
Chris Wilson058d88c2016-08-15 10:49:06 +01002248 return vma;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002249}
2250
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +01002251void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002252{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002253 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002254 struct i915_ggtt_view view;
Chris Wilson058d88c2016-08-15 10:49:06 +01002255 struct i915_vma *vma;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002256
Matt Roperebcdd392014-07-09 16:22:11 -07002257 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2258
Ville Syrjälä3465c582016-02-15 22:54:43 +02002259 intel_fill_fb_ggtt_view(&view, fb, rotation);
Chris Wilson05a20d02016-08-18 17:16:55 +01002260 vma = i915_gem_object_to_ggtt(obj, &view);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002261
Chris Wilson49ef5292016-08-18 17:17:00 +01002262 i915_vma_unpin_fence(vma);
Chris Wilson058d88c2016-08-15 10:49:06 +01002263 i915_gem_object_unpin_from_display_plane(vma);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002264}
2265
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002266static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2267 unsigned int rotation)
2268{
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002269 if (drm_rotation_90_or_270(rotation))
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002270 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2271 else
2272 return fb->pitches[plane];
2273}
2274
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002275/*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002276 * Convert the x/y offsets into a linear offset.
2277 * Only valid with 0/180 degree rotation, which is fine since linear
2278 * offset is only used with linear buffers on pre-hsw and tiled buffers
2279 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2280 */
2281u32 intel_fb_xy_to_linear(int x, int y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002282 const struct intel_plane_state *state,
2283 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002284{
Ville Syrjälä29490562016-01-20 18:02:50 +02002285 const struct drm_framebuffer *fb = state->base.fb;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002286 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2287 unsigned int pitch = fb->pitches[plane];
2288
2289 return y * pitch + x * cpp;
2290}
2291
2292/*
2293 * Add the x/y offsets derived from fb->offsets[] to the user
2294 * specified plane src x/y offsets. The resulting x/y offsets
2295 * specify the start of scanout from the beginning of the gtt mapping.
2296 */
2297void intel_add_fb_offsets(int *x, int *y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002298 const struct intel_plane_state *state,
2299 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002300
2301{
Ville Syrjälä29490562016-01-20 18:02:50 +02002302 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2303 unsigned int rotation = state->base.rotation;
Ville Syrjälä6687c902015-09-15 13:16:41 +03002304
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002305 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä6687c902015-09-15 13:16:41 +03002306 *x += intel_fb->rotated[plane].x;
2307 *y += intel_fb->rotated[plane].y;
2308 } else {
2309 *x += intel_fb->normal[plane].x;
2310 *y += intel_fb->normal[plane].y;
2311 }
2312}
2313
2314/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002315 * Input tile dimensions and pitch must already be
2316 * rotated to match x and y, and in pixel units.
2317 */
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002318static u32 _intel_adjust_tile_offset(int *x, int *y,
2319 unsigned int tile_width,
2320 unsigned int tile_height,
2321 unsigned int tile_size,
2322 unsigned int pitch_tiles,
2323 u32 old_offset,
2324 u32 new_offset)
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002325{
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002326 unsigned int pitch_pixels = pitch_tiles * tile_width;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002327 unsigned int tiles;
2328
2329 WARN_ON(old_offset & (tile_size - 1));
2330 WARN_ON(new_offset & (tile_size - 1));
2331 WARN_ON(new_offset > old_offset);
2332
2333 tiles = (old_offset - new_offset) / tile_size;
2334
2335 *y += tiles / pitch_tiles * tile_height;
2336 *x += tiles % pitch_tiles * tile_width;
2337
Ville Syrjäläb9b24032016-02-08 18:28:00 +02002338 /* minimize x in case it got needlessly big */
2339 *y += *x / pitch_pixels * tile_height;
2340 *x %= pitch_pixels;
2341
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002342 return new_offset;
2343}
2344
2345/*
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002346 * Adjust the tile offset by moving the difference into
2347 * the x/y offsets.
2348 */
2349static u32 intel_adjust_tile_offset(int *x, int *y,
2350 const struct intel_plane_state *state, int plane,
2351 u32 old_offset, u32 new_offset)
2352{
2353 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2354 const struct drm_framebuffer *fb = state->base.fb;
2355 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2356 unsigned int rotation = state->base.rotation;
2357 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2358
2359 WARN_ON(new_offset > old_offset);
2360
2361 if (fb->modifier[plane] != DRM_FORMAT_MOD_NONE) {
2362 unsigned int tile_size, tile_width, tile_height;
2363 unsigned int pitch_tiles;
2364
2365 tile_size = intel_tile_size(dev_priv);
2366 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2367 fb->modifier[plane], cpp);
2368
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002369 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002370 pitch_tiles = pitch / tile_height;
2371 swap(tile_width, tile_height);
2372 } else {
2373 pitch_tiles = pitch / (tile_width * cpp);
2374 }
2375
2376 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2377 tile_size, pitch_tiles,
2378 old_offset, new_offset);
2379 } else {
2380 old_offset += *y * pitch + *x * cpp;
2381
2382 *y = (old_offset - new_offset) / pitch;
2383 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2384 }
2385
2386 return new_offset;
2387}
2388
2389/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002390 * Computes the linear offset to the base tile and adjusts
2391 * x, y. bytes per pixel is assumed to be a power-of-two.
2392 *
2393 * In the 90/270 rotated case, x and y are assumed
2394 * to be already rotated to match the rotated GTT view, and
2395 * pitch is the tile_height aligned framebuffer height.
Ville Syrjälä6687c902015-09-15 13:16:41 +03002396 *
2397 * This function is used when computing the derived information
2398 * under intel_framebuffer, so using any of that information
2399 * here is not allowed. Anything under drm_framebuffer can be
2400 * used. This is why the user has to pass in the pitch since it
2401 * is specified in the rotated orientation.
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002402 */
Ville Syrjälä6687c902015-09-15 13:16:41 +03002403static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2404 int *x, int *y,
2405 const struct drm_framebuffer *fb, int plane,
2406 unsigned int pitch,
2407 unsigned int rotation,
2408 u32 alignment)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002409{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002410 uint64_t fb_modifier = fb->modifier[plane];
2411 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002412 u32 offset, offset_aligned;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002413
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002414 if (alignment)
2415 alignment--;
2416
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002417 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002418 unsigned int tile_size, tile_width, tile_height;
2419 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002420
Ville Syrjäläd8433102016-01-12 21:08:35 +02002421 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002422 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2423 fb_modifier, cpp);
2424
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002425 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002426 pitch_tiles = pitch / tile_height;
2427 swap(tile_width, tile_height);
2428 } else {
2429 pitch_tiles = pitch / (tile_width * cpp);
2430 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002431
Ville Syrjäläd8433102016-01-12 21:08:35 +02002432 tile_rows = *y / tile_height;
2433 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002434
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002435 tiles = *x / tile_width;
2436 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002437
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002438 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2439 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002440
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002441 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2442 tile_size, pitch_tiles,
2443 offset, offset_aligned);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002444 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002445 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002446 offset_aligned = offset & ~alignment;
2447
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002448 *y = (offset & alignment) / pitch;
2449 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002450 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002451
2452 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002453}
2454
Ville Syrjälä6687c902015-09-15 13:16:41 +03002455u32 intel_compute_tile_offset(int *x, int *y,
Ville Syrjälä29490562016-01-20 18:02:50 +02002456 const struct intel_plane_state *state,
2457 int plane)
Ville Syrjälä6687c902015-09-15 13:16:41 +03002458{
Ville Syrjälä29490562016-01-20 18:02:50 +02002459 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2460 const struct drm_framebuffer *fb = state->base.fb;
2461 unsigned int rotation = state->base.rotation;
Ville Syrjäläef78ec92015-10-13 22:48:39 +03002462 int pitch = intel_fb_pitch(fb, plane, rotation);
Ville Syrjälä8d970652016-01-28 16:30:28 +02002463 u32 alignment;
2464
2465 /* AUX_DIST needs only 4K alignment */
2466 if (fb->pixel_format == DRM_FORMAT_NV12 && plane == 1)
2467 alignment = 4096;
2468 else
2469 alignment = intel_surf_alignment(dev_priv, fb->modifier[plane]);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002470
2471 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2472 rotation, alignment);
2473}
2474
2475/* Convert the fb->offset[] linear offset into x/y offsets */
2476static void intel_fb_offset_to_xy(int *x, int *y,
2477 const struct drm_framebuffer *fb, int plane)
2478{
2479 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2480 unsigned int pitch = fb->pitches[plane];
2481 u32 linear_offset = fb->offsets[plane];
2482
2483 *y = linear_offset / pitch;
2484 *x = linear_offset % pitch / cpp;
2485}
2486
Ville Syrjälä72618eb2016-02-04 20:38:20 +02002487static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2488{
2489 switch (fb_modifier) {
2490 case I915_FORMAT_MOD_X_TILED:
2491 return I915_TILING_X;
2492 case I915_FORMAT_MOD_Y_TILED:
2493 return I915_TILING_Y;
2494 default:
2495 return I915_TILING_NONE;
2496 }
2497}
2498
Ville Syrjälä6687c902015-09-15 13:16:41 +03002499static int
2500intel_fill_fb_info(struct drm_i915_private *dev_priv,
2501 struct drm_framebuffer *fb)
2502{
2503 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2504 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2505 u32 gtt_offset_rotated = 0;
2506 unsigned int max_size = 0;
2507 uint32_t format = fb->pixel_format;
2508 int i, num_planes = drm_format_num_planes(format);
2509 unsigned int tile_size = intel_tile_size(dev_priv);
2510
2511 for (i = 0; i < num_planes; i++) {
2512 unsigned int width, height;
2513 unsigned int cpp, size;
2514 u32 offset;
2515 int x, y;
2516
2517 cpp = drm_format_plane_cpp(format, i);
2518 width = drm_format_plane_width(fb->width, format, i);
2519 height = drm_format_plane_height(fb->height, format, i);
2520
2521 intel_fb_offset_to_xy(&x, &y, fb, i);
2522
2523 /*
Ville Syrjälä60d5f2a2016-01-22 18:41:24 +02002524 * The fence (if used) is aligned to the start of the object
2525 * so having the framebuffer wrap around across the edge of the
2526 * fenced region doesn't really work. We have no API to configure
2527 * the fence start offset within the object (nor could we probably
2528 * on gen2/3). So it's just easier if we just require that the
2529 * fb layout agrees with the fence layout. We already check that the
2530 * fb stride matches the fence stride elsewhere.
2531 */
2532 if (i915_gem_object_is_tiled(intel_fb->obj) &&
2533 (x + width) * cpp > fb->pitches[i]) {
2534 DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
2535 i, fb->offsets[i]);
2536 return -EINVAL;
2537 }
2538
2539 /*
Ville Syrjälä6687c902015-09-15 13:16:41 +03002540 * First pixel of the framebuffer from
2541 * the start of the normal gtt mapping.
2542 */
2543 intel_fb->normal[i].x = x;
2544 intel_fb->normal[i].y = y;
2545
2546 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
2547 fb, 0, fb->pitches[i],
Daniel Vettercc926382016-08-15 10:41:47 +02002548 DRM_ROTATE_0, tile_size);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002549 offset /= tile_size;
2550
2551 if (fb->modifier[i] != DRM_FORMAT_MOD_NONE) {
2552 unsigned int tile_width, tile_height;
2553 unsigned int pitch_tiles;
2554 struct drm_rect r;
2555
2556 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2557 fb->modifier[i], cpp);
2558
2559 rot_info->plane[i].offset = offset;
2560 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2561 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2562 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2563
2564 intel_fb->rotated[i].pitch =
2565 rot_info->plane[i].height * tile_height;
2566
2567 /* how many tiles does this plane need */
2568 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2569 /*
2570 * If the plane isn't horizontally tile aligned,
2571 * we need one more tile.
2572 */
2573 if (x != 0)
2574 size++;
2575
2576 /* rotate the x/y offsets to match the GTT view */
2577 r.x1 = x;
2578 r.y1 = y;
2579 r.x2 = x + width;
2580 r.y2 = y + height;
2581 drm_rect_rotate(&r,
2582 rot_info->plane[i].width * tile_width,
2583 rot_info->plane[i].height * tile_height,
Daniel Vettercc926382016-08-15 10:41:47 +02002584 DRM_ROTATE_270);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002585 x = r.x1;
2586 y = r.y1;
2587
2588 /* rotate the tile dimensions to match the GTT view */
2589 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2590 swap(tile_width, tile_height);
2591
2592 /*
2593 * We only keep the x/y offsets, so push all of the
2594 * gtt offset into the x/y offsets.
2595 */
Ville Syrjälä66a2d922016-02-05 18:44:05 +02002596 _intel_adjust_tile_offset(&x, &y, tile_size,
2597 tile_width, tile_height, pitch_tiles,
2598 gtt_offset_rotated * tile_size, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03002599
2600 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2601
2602 /*
2603 * First pixel of the framebuffer from
2604 * the start of the rotated gtt mapping.
2605 */
2606 intel_fb->rotated[i].x = x;
2607 intel_fb->rotated[i].y = y;
2608 } else {
2609 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2610 x * cpp, tile_size);
2611 }
2612
2613 /* how many tiles in total needed in the bo */
2614 max_size = max(max_size, offset + size);
2615 }
2616
2617 if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
2618 DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
2619 max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
2620 return -EINVAL;
2621 }
2622
2623 return 0;
2624}
2625
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002626static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002627{
2628 switch (format) {
2629 case DISPPLANE_8BPP:
2630 return DRM_FORMAT_C8;
2631 case DISPPLANE_BGRX555:
2632 return DRM_FORMAT_XRGB1555;
2633 case DISPPLANE_BGRX565:
2634 return DRM_FORMAT_RGB565;
2635 default:
2636 case DISPPLANE_BGRX888:
2637 return DRM_FORMAT_XRGB8888;
2638 case DISPPLANE_RGBX888:
2639 return DRM_FORMAT_XBGR8888;
2640 case DISPPLANE_BGRX101010:
2641 return DRM_FORMAT_XRGB2101010;
2642 case DISPPLANE_RGBX101010:
2643 return DRM_FORMAT_XBGR2101010;
2644 }
2645}
2646
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002647static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2648{
2649 switch (format) {
2650 case PLANE_CTL_FORMAT_RGB_565:
2651 return DRM_FORMAT_RGB565;
2652 default:
2653 case PLANE_CTL_FORMAT_XRGB_8888:
2654 if (rgb_order) {
2655 if (alpha)
2656 return DRM_FORMAT_ABGR8888;
2657 else
2658 return DRM_FORMAT_XBGR8888;
2659 } else {
2660 if (alpha)
2661 return DRM_FORMAT_ARGB8888;
2662 else
2663 return DRM_FORMAT_XRGB8888;
2664 }
2665 case PLANE_CTL_FORMAT_XRGB_2101010:
2666 if (rgb_order)
2667 return DRM_FORMAT_XBGR2101010;
2668 else
2669 return DRM_FORMAT_XRGB2101010;
2670 }
2671}
2672
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002673static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002674intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2675 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002676{
2677 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002678 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002679 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002680 struct drm_i915_gem_object *obj = NULL;
2681 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002682 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002683 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2684 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2685 PAGE_SIZE);
2686
2687 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002688
Chris Wilsonff2652e2014-03-10 08:07:02 +00002689 if (plane_config->size == 0)
2690 return false;
2691
Paulo Zanoni3badb492015-09-23 12:52:23 -03002692 /* If the FB is too big, just don't use it since fbdev is not very
2693 * important and we should probably use that space with FBC or other
2694 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002695 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002696 return false;
2697
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002698 mutex_lock(&dev->struct_mutex);
2699
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002700 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2701 base_aligned,
2702 base_aligned,
2703 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002704 if (!obj) {
2705 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002706 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002707 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002708
Chris Wilson3e510a82016-08-05 10:14:23 +01002709 if (plane_config->tiling == I915_TILING_X)
2710 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002711
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002712 mode_cmd.pixel_format = fb->pixel_format;
2713 mode_cmd.width = fb->width;
2714 mode_cmd.height = fb->height;
2715 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002716 mode_cmd.modifier[0] = fb->modifier[0];
2717 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002718
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002719 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002720 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002721 DRM_DEBUG_KMS("intel fb init failed\n");
2722 goto out_unref_obj;
2723 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002724
Jesse Barnes46f297f2014-03-07 08:57:48 -08002725 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002726
Daniel Vetterf6936e22015-03-26 12:17:05 +01002727 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002728 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002729
2730out_unref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002731 i915_gem_object_put(obj);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002732 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002733 return false;
2734}
2735
Daniel Vetter5a21b662016-05-24 17:13:53 +02002736/* Update plane->state->fb to match plane->fb after driver-internal updates */
2737static void
2738update_state_fb(struct drm_plane *plane)
2739{
2740 if (plane->fb == plane->state->fb)
2741 return;
2742
2743 if (plane->state->fb)
2744 drm_framebuffer_unreference(plane->state->fb);
2745 plane->state->fb = plane->fb;
2746 if (plane->state->fb)
2747 drm_framebuffer_reference(plane->state->fb);
2748}
2749
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002750static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002751intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2752 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002753{
2754 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002755 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002756 struct drm_crtc *c;
2757 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002758 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002759 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002760 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002761 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2762 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002763 struct intel_plane_state *intel_state =
2764 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002765 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002766
Damien Lespiau2d140302015-02-05 17:22:18 +00002767 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002768 return;
2769
Daniel Vetterf6936e22015-03-26 12:17:05 +01002770 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002771 fb = &plane_config->fb->base;
2772 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002773 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002774
Damien Lespiau2d140302015-02-05 17:22:18 +00002775 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002776
2777 /*
2778 * Failed to alloc the obj, check to see if we should share
2779 * an fb with another CRTC instead
2780 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002781 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002782 i = to_intel_crtc(c);
2783
2784 if (c == &intel_crtc->base)
2785 continue;
2786
Matt Roper2ff8fde2014-07-08 07:50:07 -07002787 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002788 continue;
2789
Daniel Vetter88595ac2015-03-26 12:42:24 +01002790 fb = c->primary->fb;
2791 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002792 continue;
2793
Daniel Vetter88595ac2015-03-26 12:42:24 +01002794 obj = intel_fb_obj(fb);
Chris Wilson058d88c2016-08-15 10:49:06 +01002795 if (i915_gem_object_ggtt_offset(obj, NULL) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002796 drm_framebuffer_reference(fb);
2797 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002798 }
2799 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002800
Matt Roper200757f2015-12-03 11:37:36 -08002801 /*
2802 * We've failed to reconstruct the BIOS FB. Current display state
2803 * indicates that the primary plane is visible, but has a NULL FB,
2804 * which will lead to problems later if we don't fix it up. The
2805 * simplest solution is to just disable the primary plane now and
2806 * pretend the BIOS never had it enabled.
2807 */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002808 to_intel_plane_state(plane_state)->base.visible = false;
Matt Roper200757f2015-12-03 11:37:36 -08002809 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002810 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002811 intel_plane->disable_plane(primary, &intel_crtc->base);
2812
Daniel Vetter88595ac2015-03-26 12:42:24 +01002813 return;
2814
2815valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002816 plane_state->src_x = 0;
2817 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002818 plane_state->src_w = fb->width << 16;
2819 plane_state->src_h = fb->height << 16;
2820
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002821 plane_state->crtc_x = 0;
2822 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002823 plane_state->crtc_w = fb->width;
2824 plane_state->crtc_h = fb->height;
2825
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002826 intel_state->base.src.x1 = plane_state->src_x;
2827 intel_state->base.src.y1 = plane_state->src_y;
2828 intel_state->base.src.x2 = plane_state->src_x + plane_state->src_w;
2829 intel_state->base.src.y2 = plane_state->src_y + plane_state->src_h;
2830 intel_state->base.dst.x1 = plane_state->crtc_x;
2831 intel_state->base.dst.y1 = plane_state->crtc_y;
2832 intel_state->base.dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2833 intel_state->base.dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
Matt Roper0a8d8a82015-12-03 11:37:38 -08002834
Daniel Vetter88595ac2015-03-26 12:42:24 +01002835 obj = intel_fb_obj(fb);
Chris Wilson3e510a82016-08-05 10:14:23 +01002836 if (i915_gem_object_is_tiled(obj))
Daniel Vetter88595ac2015-03-26 12:42:24 +01002837 dev_priv->preserve_bios_swizzle = true;
2838
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002839 drm_framebuffer_reference(fb);
2840 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002841 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002842 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01002843 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2844 &obj->frontbuffer_bits);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002845}
2846
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002847static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2848 unsigned int rotation)
2849{
2850 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2851
2852 switch (fb->modifier[plane]) {
2853 case DRM_FORMAT_MOD_NONE:
2854 case I915_FORMAT_MOD_X_TILED:
2855 switch (cpp) {
2856 case 8:
2857 return 4096;
2858 case 4:
2859 case 2:
2860 case 1:
2861 return 8192;
2862 default:
2863 MISSING_CASE(cpp);
2864 break;
2865 }
2866 break;
2867 case I915_FORMAT_MOD_Y_TILED:
2868 case I915_FORMAT_MOD_Yf_TILED:
2869 switch (cpp) {
2870 case 8:
2871 return 2048;
2872 case 4:
2873 return 4096;
2874 case 2:
2875 case 1:
2876 return 8192;
2877 default:
2878 MISSING_CASE(cpp);
2879 break;
2880 }
2881 break;
2882 default:
2883 MISSING_CASE(fb->modifier[plane]);
2884 }
2885
2886 return 2048;
2887}
2888
2889static int skl_check_main_surface(struct intel_plane_state *plane_state)
2890{
2891 const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
2892 const struct drm_framebuffer *fb = plane_state->base.fb;
2893 unsigned int rotation = plane_state->base.rotation;
Daniel Vettercc926382016-08-15 10:41:47 +02002894 int x = plane_state->base.src.x1 >> 16;
2895 int y = plane_state->base.src.y1 >> 16;
2896 int w = drm_rect_width(&plane_state->base.src) >> 16;
2897 int h = drm_rect_height(&plane_state->base.src) >> 16;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002898 int max_width = skl_max_plane_width(fb, 0, rotation);
2899 int max_height = 4096;
Ville Syrjälä8d970652016-01-28 16:30:28 +02002900 u32 alignment, offset, aux_offset = plane_state->aux.offset;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002901
2902 if (w > max_width || h > max_height) {
2903 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2904 w, h, max_width, max_height);
2905 return -EINVAL;
2906 }
2907
2908 intel_add_fb_offsets(&x, &y, plane_state, 0);
2909 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
2910
2911 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
2912
2913 /*
Ville Syrjälä8d970652016-01-28 16:30:28 +02002914 * AUX surface offset is specified as the distance from the
2915 * main surface offset, and it must be non-negative. Make
2916 * sure that is what we will get.
2917 */
2918 if (offset > aux_offset)
2919 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2920 offset, aux_offset & ~(alignment - 1));
2921
2922 /*
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002923 * When using an X-tiled surface, the plane blows up
2924 * if the x offset + width exceed the stride.
2925 *
2926 * TODO: linear and Y-tiled seem fine, Yf untested,
2927 */
2928 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED) {
2929 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
2930
2931 while ((x + w) * cpp > fb->pitches[0]) {
2932 if (offset == 0) {
2933 DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
2934 return -EINVAL;
2935 }
2936
2937 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2938 offset, offset - alignment);
2939 }
2940 }
2941
2942 plane_state->main.offset = offset;
2943 plane_state->main.x = x;
2944 plane_state->main.y = y;
2945
2946 return 0;
2947}
2948
Ville Syrjälä8d970652016-01-28 16:30:28 +02002949static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
2950{
2951 const struct drm_framebuffer *fb = plane_state->base.fb;
2952 unsigned int rotation = plane_state->base.rotation;
2953 int max_width = skl_max_plane_width(fb, 1, rotation);
2954 int max_height = 4096;
Daniel Vettercc926382016-08-15 10:41:47 +02002955 int x = plane_state->base.src.x1 >> 17;
2956 int y = plane_state->base.src.y1 >> 17;
2957 int w = drm_rect_width(&plane_state->base.src) >> 17;
2958 int h = drm_rect_height(&plane_state->base.src) >> 17;
Ville Syrjälä8d970652016-01-28 16:30:28 +02002959 u32 offset;
2960
2961 intel_add_fb_offsets(&x, &y, plane_state, 1);
2962 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
2963
2964 /* FIXME not quite sure how/if these apply to the chroma plane */
2965 if (w > max_width || h > max_height) {
2966 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
2967 w, h, max_width, max_height);
2968 return -EINVAL;
2969 }
2970
2971 plane_state->aux.offset = offset;
2972 plane_state->aux.x = x;
2973 plane_state->aux.y = y;
2974
2975 return 0;
2976}
2977
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002978int skl_check_plane_surface(struct intel_plane_state *plane_state)
2979{
2980 const struct drm_framebuffer *fb = plane_state->base.fb;
2981 unsigned int rotation = plane_state->base.rotation;
2982 int ret;
2983
2984 /* Rotate src coordinates to match rotated GTT view */
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002985 if (drm_rotation_90_or_270(rotation))
Daniel Vettercc926382016-08-15 10:41:47 +02002986 drm_rect_rotate(&plane_state->base.src,
Ville Syrjäläda064b42016-10-24 19:13:04 +03002987 fb->width << 16, fb->height << 16,
2988 DRM_ROTATE_270);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02002989
Ville Syrjälä8d970652016-01-28 16:30:28 +02002990 /*
2991 * Handle the AUX surface first since
2992 * the main surface setup depends on it.
2993 */
2994 if (fb->pixel_format == DRM_FORMAT_NV12) {
2995 ret = skl_check_nv12_aux_surface(plane_state);
2996 if (ret)
2997 return ret;
2998 } else {
2999 plane_state->aux.offset = ~0xfff;
3000 plane_state->aux.x = 0;
3001 plane_state->aux.y = 0;
3002 }
3003
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003004 ret = skl_check_main_surface(plane_state);
3005 if (ret)
3006 return ret;
3007
3008 return 0;
3009}
3010
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003011static void i9xx_update_primary_plane(struct drm_plane *primary,
3012 const struct intel_crtc_state *crtc_state,
3013 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07003014{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003015 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003016 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003017 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3018 struct drm_framebuffer *fb = plane_state->base.fb;
3019 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07003020 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02003021 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07003022 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003023 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02003024 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003025 int x = plane_state->base.src.x1 >> 16;
3026 int y = plane_state->base.src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03003027
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003028 dspcntr = DISPPLANE_GAMMA_ENABLE;
3029
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03003030 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003031
3032 if (INTEL_INFO(dev)->gen < 4) {
3033 if (intel_crtc->pipe == PIPE_B)
3034 dspcntr |= DISPPLANE_SEL_PIPE_B;
3035
3036 /* pipesrc and dspsize control the size that is scaled from,
3037 * which should always be the user's requested size.
3038 */
3039 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003040 ((crtc_state->pipe_src_h - 1) << 16) |
3041 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003042 I915_WRITE(DSPPOS(plane), 0);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01003043 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
Ville Syrjäläc14b0482014-10-16 20:52:34 +03003044 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003045 ((crtc_state->pipe_src_h - 1) << 16) |
3046 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03003047 I915_WRITE(PRIMPOS(plane), 0);
3048 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003049 }
3050
Ville Syrjälä57779d02012-10-31 17:50:14 +02003051 switch (fb->pixel_format) {
3052 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07003053 dspcntr |= DISPPLANE_8BPP;
3054 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003055 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003056 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07003057 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003058 case DRM_FORMAT_RGB565:
3059 dspcntr |= DISPPLANE_BGRX565;
3060 break;
3061 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003062 dspcntr |= DISPPLANE_BGRX888;
3063 break;
3064 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003065 dspcntr |= DISPPLANE_RGBX888;
3066 break;
3067 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003068 dspcntr |= DISPPLANE_BGRX101010;
3069 break;
3070 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003071 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07003072 break;
3073 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01003074 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07003075 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02003076
Ville Syrjälä72618eb2016-02-04 20:38:20 +02003077 if (INTEL_GEN(dev_priv) >= 4 &&
3078 fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003079 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07003080
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01003081 if (IS_G4X(dev_priv))
Ville Syrjäläde1aa622013-06-07 10:47:01 +03003082 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
3083
Ville Syrjälä29490562016-01-20 18:02:50 +02003084 intel_add_fb_offsets(&x, &y, plane_state, 0);
Jesse Barnes81255562010-08-02 12:07:50 -07003085
Ville Syrjälä6687c902015-09-15 13:16:41 +03003086 if (INTEL_INFO(dev)->gen >= 4)
Daniel Vetterc2c75132012-07-05 12:17:30 +02003087 intel_crtc->dspaddr_offset =
Ville Syrjälä29490562016-01-20 18:02:50 +02003088 intel_compute_tile_offset(&x, &y, plane_state, 0);
Daniel Vettere506a0c2012-07-05 12:17:29 +02003089
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003090 if (rotation == DRM_ROTATE_180) {
Sonika Jindal48404c12014-08-22 14:06:04 +05303091 dspcntr |= DISPPLANE_ROTATE_180;
3092
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003093 x += (crtc_state->pipe_src_w - 1);
3094 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05303095 }
3096
Ville Syrjälä29490562016-01-20 18:02:50 +02003097 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003098
3099 if (INTEL_INFO(dev)->gen < 4)
3100 intel_crtc->dspaddr_offset = linear_offset;
3101
Paulo Zanoni2db33662015-09-14 15:20:03 -03003102 intel_crtc->adjusted_x = x;
3103 intel_crtc->adjusted_y = y;
3104
Sonika Jindal48404c12014-08-22 14:06:04 +05303105 I915_WRITE(reg, dspcntr);
3106
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003107 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003108 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01003109 I915_WRITE(DSPSURF(plane),
Ville Syrjälä6687c902015-09-15 13:16:41 +03003110 intel_fb_gtt_offset(fb, rotation) +
3111 intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01003112 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02003113 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01003114 } else
Chris Wilson058d88c2016-08-15 10:49:06 +01003115 I915_WRITE(DSPADDR(plane), i915_gem_object_ggtt_offset(obj, NULL) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01003116 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003117}
3118
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003119static void i9xx_disable_primary_plane(struct drm_plane *primary,
3120 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07003121{
3122 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003123 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003124 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003125 int plane = intel_crtc->plane;
3126
3127 I915_WRITE(DSPCNTR(plane), 0);
3128 if (INTEL_INFO(dev_priv)->gen >= 4)
3129 I915_WRITE(DSPSURF(plane), 0);
3130 else
3131 I915_WRITE(DSPADDR(plane), 0);
3132 POSTING_READ(DSPCNTR(plane));
3133}
3134
3135static void ironlake_update_primary_plane(struct drm_plane *primary,
3136 const struct intel_crtc_state *crtc_state,
3137 const struct intel_plane_state *plane_state)
3138{
3139 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003140 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003141 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3142 struct drm_framebuffer *fb = plane_state->base.fb;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003143 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02003144 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003145 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003146 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02003147 unsigned int rotation = plane_state->base.rotation;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003148 int x = plane_state->base.src.x1 >> 16;
3149 int y = plane_state->base.src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03003150
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003151 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03003152 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003153
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003154 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläf45651b2014-08-08 21:51:10 +03003155 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
3156
Ville Syrjälä57779d02012-10-31 17:50:14 +02003157 switch (fb->pixel_format) {
3158 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07003159 dspcntr |= DISPPLANE_8BPP;
3160 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003161 case DRM_FORMAT_RGB565:
3162 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003163 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02003164 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003165 dspcntr |= DISPPLANE_BGRX888;
3166 break;
3167 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003168 dspcntr |= DISPPLANE_RGBX888;
3169 break;
3170 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003171 dspcntr |= DISPPLANE_BGRX101010;
3172 break;
3173 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02003174 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003175 break;
3176 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01003177 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07003178 }
3179
Ville Syrjälä72618eb2016-02-04 20:38:20 +02003180 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
Jesse Barnes17638cd2011-06-24 12:19:23 -07003181 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003182
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003183 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03003184 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003185
Ville Syrjälä29490562016-01-20 18:02:50 +02003186 intel_add_fb_offsets(&x, &y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003187
Daniel Vetterc2c75132012-07-05 12:17:30 +02003188 intel_crtc->dspaddr_offset =
Ville Syrjälä29490562016-01-20 18:02:50 +02003189 intel_compute_tile_offset(&x, &y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003190
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003191 if (rotation == DRM_ROTATE_180) {
Sonika Jindal48404c12014-08-22 14:06:04 +05303192 dspcntr |= DISPPLANE_ROTATE_180;
3193
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003194 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003195 x += (crtc_state->pipe_src_w - 1);
3196 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05303197 }
3198 }
3199
Ville Syrjälä29490562016-01-20 18:02:50 +02003200 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003201
Paulo Zanoni2db33662015-09-14 15:20:03 -03003202 intel_crtc->adjusted_x = x;
3203 intel_crtc->adjusted_y = y;
3204
Sonika Jindal48404c12014-08-22 14:06:04 +05303205 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003206
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003207 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01003208 I915_WRITE(DSPSURF(plane),
Ville Syrjälä6687c902015-09-15 13:16:41 +03003209 intel_fb_gtt_offset(fb, rotation) +
3210 intel_crtc->dspaddr_offset);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003211 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00003212 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
3213 } else {
3214 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
3215 I915_WRITE(DSPLINOFF(plane), linear_offset);
3216 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07003217 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07003218}
3219
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003220u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
3221 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00003222{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003223 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
3224 return 64;
3225 } else {
3226 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00003227
Ville Syrjälä27ba3912016-02-15 22:54:40 +02003228 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00003229 }
3230}
3231
Ville Syrjälä6687c902015-09-15 13:16:41 +03003232u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
3233 unsigned int rotation)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003234{
Ville Syrjälä6687c902015-09-15 13:16:41 +03003235 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Daniel Vetterce7f1722015-10-14 16:51:06 +02003236 struct i915_ggtt_view view;
Chris Wilson058d88c2016-08-15 10:49:06 +01003237 struct i915_vma *vma;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003238
Ville Syrjälä6687c902015-09-15 13:16:41 +03003239 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003240
Chris Wilson058d88c2016-08-15 10:49:06 +01003241 vma = i915_gem_object_to_ggtt(obj, &view);
3242 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
3243 view.type))
3244 return -1;
3245
Chris Wilsonbde13eb2016-08-15 10:49:07 +01003246 return i915_ggtt_offset(vma);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003247}
3248
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003249static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3250{
3251 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003252 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003253
3254 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3255 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3256 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003257}
3258
Chandra Kondurua1b22782015-04-07 15:28:45 -07003259/*
3260 * This function detaches (aka. unbinds) unused scalers in hardware
3261 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02003262static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07003263{
Chandra Kondurua1b22782015-04-07 15:28:45 -07003264 struct intel_crtc_scaler_state *scaler_state;
3265 int i;
3266
Chandra Kondurua1b22782015-04-07 15:28:45 -07003267 scaler_state = &intel_crtc->config->scaler_state;
3268
3269 /* loop through and disable scalers that aren't in use */
3270 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02003271 if (!scaler_state->scalers[i].in_use)
3272 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07003273 }
3274}
3275
Ville Syrjäläd2196772016-01-28 18:33:11 +02003276u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3277 unsigned int rotation)
3278{
3279 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
3280 u32 stride = intel_fb_pitch(fb, plane, rotation);
3281
3282 /*
3283 * The stride is either expressed as a multiple of 64 bytes chunks for
3284 * linear buffers or in number of tiles for tiled buffers.
3285 */
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003286 if (drm_rotation_90_or_270(rotation)) {
Ville Syrjäläd2196772016-01-28 18:33:11 +02003287 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
3288
3289 stride /= intel_tile_height(dev_priv, fb->modifier[0], cpp);
3290 } else {
3291 stride /= intel_fb_stride_alignment(dev_priv, fb->modifier[0],
3292 fb->pixel_format);
3293 }
3294
3295 return stride;
3296}
3297
Chandra Konduru6156a452015-04-27 13:48:39 -07003298u32 skl_plane_ctl_format(uint32_t pixel_format)
3299{
Chandra Konduru6156a452015-04-27 13:48:39 -07003300 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01003301 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003302 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07003303 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003304 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07003305 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003306 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07003307 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003308 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07003309 /*
3310 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3311 * to be already pre-multiplied. We need to add a knob (or a different
3312 * DRM_FORMAT) for user-space to configure that.
3313 */
3314 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003315 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07003316 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003317 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003318 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07003319 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003320 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003321 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003322 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003323 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07003324 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003325 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07003326 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003327 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07003328 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003329 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003330 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003331 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07003332 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01003333 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07003334 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003335
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003336 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003337}
3338
3339u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3340{
Chandra Konduru6156a452015-04-27 13:48:39 -07003341 switch (fb_modifier) {
3342 case DRM_FORMAT_MOD_NONE:
3343 break;
3344 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003345 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003346 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003347 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003348 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003349 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003350 default:
3351 MISSING_CASE(fb_modifier);
3352 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003353
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003354 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003355}
3356
3357u32 skl_plane_ctl_rotation(unsigned int rotation)
3358{
Chandra Konduru6156a452015-04-27 13:48:39 -07003359 switch (rotation) {
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003360 case DRM_ROTATE_0:
Chandra Konduru6156a452015-04-27 13:48:39 -07003361 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303362 /*
3363 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3364 * while i915 HW rotation is clockwise, thats why this swapping.
3365 */
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003366 case DRM_ROTATE_90:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303367 return PLANE_CTL_ROTATE_270;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003368 case DRM_ROTATE_180:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003369 return PLANE_CTL_ROTATE_180;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003370 case DRM_ROTATE_270:
Sonika Jindal1e8df162015-05-20 13:40:48 +05303371 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003372 default:
3373 MISSING_CASE(rotation);
3374 }
3375
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003376 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003377}
3378
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003379static void skylake_update_primary_plane(struct drm_plane *plane,
3380 const struct intel_crtc_state *crtc_state,
3381 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003382{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003383 struct drm_device *dev = plane->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003384 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3386 struct drm_framebuffer *fb = plane_state->base.fb;
Lyude62e0fb82016-08-22 12:50:08 -04003387 const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003388 const struct skl_plane_wm *p_wm =
3389 &crtc_state->wm.skl.optimal.planes[0];
Damien Lespiau70d21f02013-07-03 21:06:04 +01003390 int pipe = intel_crtc->pipe;
Ville Syrjäläd2196772016-01-28 18:33:11 +02003391 u32 plane_ctl;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003392 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläd2196772016-01-28 18:33:11 +02003393 u32 stride = skl_plane_stride(fb, 0, rotation);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003394 u32 surf_addr = plane_state->main.offset;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003395 int scaler_id = plane_state->scaler_id;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +02003396 int src_x = plane_state->main.x;
3397 int src_y = plane_state->main.y;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003398 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3399 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3400 int dst_x = plane_state->base.dst.x1;
3401 int dst_y = plane_state->base.dst.y1;
3402 int dst_w = drm_rect_width(&plane_state->base.dst);
3403 int dst_h = drm_rect_height(&plane_state->base.dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003404
3405 plane_ctl = PLANE_CTL_ENABLE |
3406 PLANE_CTL_PIPE_GAMMA_ENABLE |
3407 PLANE_CTL_PIPE_CSC_ENABLE;
3408
Chandra Konduru6156a452015-04-27 13:48:39 -07003409 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3410 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003411 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003412 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003413
Ville Syrjälä6687c902015-09-15 13:16:41 +03003414 /* Sizes are 0 based */
3415 src_w--;
3416 src_h--;
3417 dst_w--;
3418 dst_h--;
3419
Paulo Zanoni4c0b8a82016-08-19 19:03:23 -03003420 intel_crtc->dspaddr_offset = surf_addr;
3421
Ville Syrjälä6687c902015-09-15 13:16:41 +03003422 intel_crtc->adjusted_x = src_x;
3423 intel_crtc->adjusted_y = src_y;
Paulo Zanoni2db33662015-09-14 15:20:03 -03003424
Lyude62e0fb82016-08-22 12:50:08 -04003425 if (wm->dirty_pipes & drm_crtc_mask(&intel_crtc->base))
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003426 skl_write_plane_wm(intel_crtc, p_wm, &wm->ddb, 0);
Lyude62e0fb82016-08-22 12:50:08 -04003427
Damien Lespiau70d21f02013-07-03 21:06:04 +01003428 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003429 I915_WRITE(PLANE_OFFSET(pipe, 0), (src_y << 16) | src_x);
Ville Syrjäläef78ec92015-10-13 22:48:39 +03003430 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Ville Syrjälä6687c902015-09-15 13:16:41 +03003431 I915_WRITE(PLANE_SIZE(pipe, 0), (src_h << 16) | src_w);
Chandra Konduru6156a452015-04-27 13:48:39 -07003432
3433 if (scaler_id >= 0) {
3434 uint32_t ps_ctrl = 0;
3435
3436 WARN_ON(!dst_w || !dst_h);
3437 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3438 crtc_state->scaler_state.scalers[scaler_id].mode;
3439 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3440 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3441 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3442 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3443 I915_WRITE(PLANE_POS(pipe, 0), 0);
3444 } else {
3445 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3446 }
3447
Ville Syrjälä6687c902015-09-15 13:16:41 +03003448 I915_WRITE(PLANE_SURF(pipe, 0),
3449 intel_fb_gtt_offset(fb, rotation) + surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003450
3451 POSTING_READ(PLANE_SURF(pipe, 0));
3452}
3453
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003454static void skylake_disable_primary_plane(struct drm_plane *primary,
3455 struct drm_crtc *crtc)
3456{
3457 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003458 struct drm_i915_private *dev_priv = to_i915(dev);
Lyude62e0fb82016-08-22 12:50:08 -04003459 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003460 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3461 const struct skl_plane_wm *p_wm = &cstate->wm.skl.optimal.planes[0];
Lyude62e0fb82016-08-22 12:50:08 -04003462 int pipe = intel_crtc->pipe;
3463
Lyudeccebc232016-08-29 12:31:27 -04003464 /*
3465 * We only populate skl_results on watermark updates, and if the
3466 * plane's visiblity isn't actually changing neither is its watermarks.
3467 */
3468 if (!crtc->primary->state->visible)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003469 skl_write_plane_wm(intel_crtc, p_wm,
3470 &dev_priv->wm.skl_results.ddb, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003471
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003472 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3473 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3474 POSTING_READ(PLANE_SURF(pipe, 0));
3475}
3476
Jesse Barnes17638cd2011-06-24 12:19:23 -07003477/* Assume fb object is pinned & idle & fenced and just update base pointers */
3478static int
3479intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3480 int x, int y, enum mode_set_atomic state)
3481{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003482 /* Support for kgdboc is disabled, this needs a major rework. */
3483 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003484
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003485 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003486}
3487
Daniel Vetter5a21b662016-05-24 17:13:53 +02003488static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3489{
3490 struct intel_crtc *crtc;
3491
Chris Wilson91c8a322016-07-05 10:40:23 +01003492 for_each_intel_crtc(&dev_priv->drm, crtc)
Daniel Vetter5a21b662016-05-24 17:13:53 +02003493 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3494}
3495
Ville Syrjälä75147472014-11-24 18:28:11 +02003496static void intel_update_primary_planes(struct drm_device *dev)
3497{
Ville Syrjälä75147472014-11-24 18:28:11 +02003498 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003499
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003500 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003501 struct intel_plane *plane = to_intel_plane(crtc->primary);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003502 struct intel_plane_state *plane_state =
3503 to_intel_plane_state(plane->base.state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003504
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003505 if (plane_state->base.visible)
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003506 plane->update_plane(&plane->base,
3507 to_intel_crtc_state(crtc->state),
3508 plane_state);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003509 }
3510}
3511
Maarten Lankhorst73974892016-08-05 23:28:27 +03003512static int
3513__intel_display_resume(struct drm_device *dev,
3514 struct drm_atomic_state *state)
3515{
3516 struct drm_crtc_state *crtc_state;
3517 struct drm_crtc *crtc;
3518 int i, ret;
3519
3520 intel_modeset_setup_hw_state(dev);
3521 i915_redisable_vga(dev);
3522
3523 if (!state)
3524 return 0;
3525
3526 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3527 /*
3528 * Force recalculation even if we restore
3529 * current state. With fast modeset this may not result
3530 * in a modeset when the state is compatible.
3531 */
3532 crtc_state->mode_changed = true;
3533 }
3534
3535 /* ignore any reset values/BIOS leftovers in the WM registers */
3536 to_intel_atomic_state(state)->skip_intermediate_wm = true;
3537
3538 ret = drm_atomic_commit(state);
3539
3540 WARN_ON(ret == -EDEADLK);
3541 return ret;
3542}
3543
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003544static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3545{
Ville Syrjäläae981042016-08-05 23:28:30 +03003546 return intel_has_gpu_reset(dev_priv) &&
3547 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003548}
3549
Chris Wilsonc0336662016-05-06 15:40:21 +01003550void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003551{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003552 struct drm_device *dev = &dev_priv->drm;
3553 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3554 struct drm_atomic_state *state;
3555 int ret;
3556
Maarten Lankhorst73974892016-08-05 23:28:27 +03003557 /*
3558 * Need mode_config.mutex so that we don't
3559 * trample ongoing ->detect() and whatnot.
3560 */
3561 mutex_lock(&dev->mode_config.mutex);
3562 drm_modeset_acquire_init(ctx, 0);
3563 while (1) {
3564 ret = drm_modeset_lock_all_ctx(dev, ctx);
3565 if (ret != -EDEADLK)
3566 break;
3567
3568 drm_modeset_backoff(ctx);
3569 }
3570
3571 /* reset doesn't touch the display, but flips might get nuked anyway, */
Maarten Lankhorst522a63d2016-08-05 23:28:28 +03003572 if (!i915.force_reset_modeset_test &&
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003573 !gpu_reset_clobbers_display(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003574 return;
3575
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003576 /*
3577 * Disabling the crtcs gracefully seems nicer. Also the
3578 * g33 docs say we should at least disable all the planes.
3579 */
Maarten Lankhorst73974892016-08-05 23:28:27 +03003580 state = drm_atomic_helper_duplicate_state(dev, ctx);
3581 if (IS_ERR(state)) {
3582 ret = PTR_ERR(state);
3583 state = NULL;
3584 DRM_ERROR("Duplicating state failed with %i\n", ret);
3585 goto err;
3586 }
3587
3588 ret = drm_atomic_helper_disable_all(dev, ctx);
3589 if (ret) {
3590 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
3591 goto err;
3592 }
3593
3594 dev_priv->modeset_restore_state = state;
3595 state->acquire_ctx = ctx;
3596 return;
3597
3598err:
Chris Wilson08536952016-10-14 13:18:18 +01003599 drm_atomic_state_put(state);
Ville Syrjälä75147472014-11-24 18:28:11 +02003600}
3601
Chris Wilsonc0336662016-05-06 15:40:21 +01003602void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003603{
Maarten Lankhorst73974892016-08-05 23:28:27 +03003604 struct drm_device *dev = &dev_priv->drm;
3605 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3606 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3607 int ret;
3608
Daniel Vetter5a21b662016-05-24 17:13:53 +02003609 /*
3610 * Flips in the rings will be nuked by the reset,
3611 * so complete all pending flips so that user space
3612 * will get its events and not get stuck.
3613 */
3614 intel_complete_page_flips(dev_priv);
3615
Maarten Lankhorst73974892016-08-05 23:28:27 +03003616 dev_priv->modeset_restore_state = NULL;
3617
Ville Syrjälä75147472014-11-24 18:28:11 +02003618 /* reset doesn't touch the display */
Ville Syrjälä4ac2ba22016-08-05 23:28:29 +03003619 if (!gpu_reset_clobbers_display(dev_priv)) {
Maarten Lankhorst522a63d2016-08-05 23:28:28 +03003620 if (!state) {
3621 /*
3622 * Flips in the rings have been nuked by the reset,
3623 * so update the base address of all primary
3624 * planes to the the last fb to make sure we're
3625 * showing the correct fb after a reset.
3626 *
3627 * FIXME: Atomic will make this obsolete since we won't schedule
3628 * CS-based flips (which might get lost in gpu resets) any more.
3629 */
3630 intel_update_primary_planes(dev);
3631 } else {
3632 ret = __intel_display_resume(dev, state);
3633 if (ret)
3634 DRM_ERROR("Restoring old state failed with %i\n", ret);
3635 }
Maarten Lankhorst73974892016-08-05 23:28:27 +03003636 } else {
3637 /*
3638 * The display has been reset as well,
3639 * so need a full re-initialization.
3640 */
3641 intel_runtime_pm_disable_interrupts(dev_priv);
3642 intel_runtime_pm_enable_interrupts(dev_priv);
3643
Imre Deak51f59202016-09-14 13:04:13 +03003644 intel_pps_unlock_regs_wa(dev_priv);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003645 intel_modeset_init_hw(dev);
3646
3647 spin_lock_irq(&dev_priv->irq_lock);
3648 if (dev_priv->display.hpd_irq_setup)
3649 dev_priv->display.hpd_irq_setup(dev_priv);
3650 spin_unlock_irq(&dev_priv->irq_lock);
3651
3652 ret = __intel_display_resume(dev, state);
3653 if (ret)
3654 DRM_ERROR("Restoring old state failed with %i\n", ret);
3655
3656 intel_hpd_init(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003657 }
3658
Chris Wilson08536952016-10-14 13:18:18 +01003659 if (state)
3660 drm_atomic_state_put(state);
Maarten Lankhorst73974892016-08-05 23:28:27 +03003661 drm_modeset_drop_locks(ctx);
3662 drm_modeset_acquire_fini(ctx);
3663 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä75147472014-11-24 18:28:11 +02003664}
3665
Chris Wilson8af29b02016-09-09 14:11:47 +01003666static bool abort_flip_on_reset(struct intel_crtc *crtc)
3667{
3668 struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
3669
3670 if (i915_reset_in_progress(error))
3671 return true;
3672
3673 if (crtc->reset_count != i915_reset_count(error))
3674 return true;
3675
3676 return false;
3677}
3678
Chris Wilson7d5e3792014-03-04 13:15:08 +00003679static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3680{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003681 struct drm_device *dev = crtc->dev;
3682 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003683 bool pending;
3684
Chris Wilson8af29b02016-09-09 14:11:47 +01003685 if (abort_flip_on_reset(intel_crtc))
Daniel Vetter5a21b662016-05-24 17:13:53 +02003686 return false;
3687
3688 spin_lock_irq(&dev->event_lock);
3689 pending = to_intel_crtc(crtc)->flip_work != NULL;
3690 spin_unlock_irq(&dev->event_lock);
3691
3692 return pending;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003693}
3694
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003695static void intel_update_pipe_config(struct intel_crtc *crtc,
3696 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003697{
3698 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003699 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003700 struct intel_crtc_state *pipe_config =
3701 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003702
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003703 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3704 crtc->base.mode = crtc->base.state->mode;
3705
3706 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3707 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3708 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003709
3710 /*
3711 * Update pipe size and adjust fitter if needed: the reason for this is
3712 * that in compute_mode_changes we check the native mode (not the pfit
3713 * mode) to see if we can flip rather than do a full mode set. In the
3714 * fastboot case, we'll flip, but if we don't update the pipesrc and
3715 * pfit state, we'll end up with a big fb scanned out into the wrong
3716 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003717 */
3718
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003719 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003720 ((pipe_config->pipe_src_w - 1) << 16) |
3721 (pipe_config->pipe_src_h - 1));
3722
3723 /* on skylake this is done by detaching scalers */
3724 if (INTEL_INFO(dev)->gen >= 9) {
3725 skl_detach_scalers(crtc);
3726
3727 if (pipe_config->pch_pfit.enabled)
3728 skylake_pfit_enable(crtc);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003729 } else if (HAS_PCH_SPLIT(dev_priv)) {
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003730 if (pipe_config->pch_pfit.enabled)
3731 ironlake_pfit_enable(crtc);
3732 else if (old_crtc_state->pch_pfit.enabled)
3733 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003734 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003735}
3736
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003737static void intel_fdi_normal_train(struct drm_crtc *crtc)
3738{
3739 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003740 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003741 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3742 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003743 i915_reg_t reg;
3744 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003745
3746 /* enable normal train */
3747 reg = FDI_TX_CTL(pipe);
3748 temp = I915_READ(reg);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003749 if (IS_IVYBRIDGE(dev_priv)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003750 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3751 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003752 } else {
3753 temp &= ~FDI_LINK_TRAIN_NONE;
3754 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003755 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003756 I915_WRITE(reg, temp);
3757
3758 reg = FDI_RX_CTL(pipe);
3759 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003760 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003761 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3762 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3763 } else {
3764 temp &= ~FDI_LINK_TRAIN_NONE;
3765 temp |= FDI_LINK_TRAIN_NONE;
3766 }
3767 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3768
3769 /* wait one idle pattern time */
3770 POSTING_READ(reg);
3771 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003772
3773 /* IVB wants error correction enabled */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01003774 if (IS_IVYBRIDGE(dev_priv))
Jesse Barnes357555c2011-04-28 15:09:55 -07003775 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3776 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003777}
3778
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003779/* The FDI link training functions for ILK/Ibexpeak. */
3780static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3781{
3782 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003783 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003784 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3785 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003786 i915_reg_t reg;
3787 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003788
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003789 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003790 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003791
Adam Jacksone1a44742010-06-25 15:32:14 -04003792 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3793 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003794 reg = FDI_RX_IMR(pipe);
3795 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003796 temp &= ~FDI_RX_SYMBOL_LOCK;
3797 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003798 I915_WRITE(reg, temp);
3799 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003800 udelay(150);
3801
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003802 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003803 reg = FDI_TX_CTL(pipe);
3804 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003805 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003806 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003807 temp &= ~FDI_LINK_TRAIN_NONE;
3808 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003809 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003810
Chris Wilson5eddb702010-09-11 13:48:45 +01003811 reg = FDI_RX_CTL(pipe);
3812 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003813 temp &= ~FDI_LINK_TRAIN_NONE;
3814 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003815 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3816
3817 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003818 udelay(150);
3819
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003820 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003821 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3822 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3823 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003824
Chris Wilson5eddb702010-09-11 13:48:45 +01003825 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003826 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003827 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003828 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3829
3830 if ((temp & FDI_RX_BIT_LOCK)) {
3831 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003832 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003833 break;
3834 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003835 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003836 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003837 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003838
3839 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003840 reg = FDI_TX_CTL(pipe);
3841 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003842 temp &= ~FDI_LINK_TRAIN_NONE;
3843 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003844 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003845
Chris Wilson5eddb702010-09-11 13:48:45 +01003846 reg = FDI_RX_CTL(pipe);
3847 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003848 temp &= ~FDI_LINK_TRAIN_NONE;
3849 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003850 I915_WRITE(reg, temp);
3851
3852 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003853 udelay(150);
3854
Chris Wilson5eddb702010-09-11 13:48:45 +01003855 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003856 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003857 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003858 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3859
3860 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003861 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003862 DRM_DEBUG_KMS("FDI train 2 done.\n");
3863 break;
3864 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003865 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003866 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003867 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003868
3869 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003870
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003871}
3872
Akshay Joshi0206e352011-08-16 15:34:10 -04003873static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003874 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3875 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3876 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3877 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3878};
3879
3880/* The FDI link training functions for SNB/Cougarpoint. */
3881static void gen6_fdi_link_train(struct drm_crtc *crtc)
3882{
3883 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003884 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3886 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003887 i915_reg_t reg;
3888 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003889
Adam Jacksone1a44742010-06-25 15:32:14 -04003890 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3891 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003892 reg = FDI_RX_IMR(pipe);
3893 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003894 temp &= ~FDI_RX_SYMBOL_LOCK;
3895 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003896 I915_WRITE(reg, temp);
3897
3898 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003899 udelay(150);
3900
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003901 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003902 reg = FDI_TX_CTL(pipe);
3903 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003904 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003905 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003906 temp &= ~FDI_LINK_TRAIN_NONE;
3907 temp |= FDI_LINK_TRAIN_PATTERN_1;
3908 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3909 /* SNB-B */
3910 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003911 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003912
Daniel Vetterd74cf322012-10-26 10:58:13 +02003913 I915_WRITE(FDI_RX_MISC(pipe),
3914 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3915
Chris Wilson5eddb702010-09-11 13:48:45 +01003916 reg = FDI_RX_CTL(pipe);
3917 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003918 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003919 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3920 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3921 } else {
3922 temp &= ~FDI_LINK_TRAIN_NONE;
3923 temp |= FDI_LINK_TRAIN_PATTERN_1;
3924 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003925 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3926
3927 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003928 udelay(150);
3929
Akshay Joshi0206e352011-08-16 15:34:10 -04003930 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003931 reg = FDI_TX_CTL(pipe);
3932 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003933 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3934 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003935 I915_WRITE(reg, temp);
3936
3937 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003938 udelay(500);
3939
Sean Paulfa37d392012-03-02 12:53:39 -05003940 for (retry = 0; retry < 5; retry++) {
3941 reg = FDI_RX_IIR(pipe);
3942 temp = I915_READ(reg);
3943 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3944 if (temp & FDI_RX_BIT_LOCK) {
3945 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3946 DRM_DEBUG_KMS("FDI train 1 done.\n");
3947 break;
3948 }
3949 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003950 }
Sean Paulfa37d392012-03-02 12:53:39 -05003951 if (retry < 5)
3952 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003953 }
3954 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003955 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003956
3957 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003958 reg = FDI_TX_CTL(pipe);
3959 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003960 temp &= ~FDI_LINK_TRAIN_NONE;
3961 temp |= FDI_LINK_TRAIN_PATTERN_2;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003962 if (IS_GEN6(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003963 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3964 /* SNB-B */
3965 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3966 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003967 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003968
Chris Wilson5eddb702010-09-11 13:48:45 +01003969 reg = FDI_RX_CTL(pipe);
3970 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003971 if (HAS_PCH_CPT(dev_priv)) {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003972 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3973 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3974 } else {
3975 temp &= ~FDI_LINK_TRAIN_NONE;
3976 temp |= FDI_LINK_TRAIN_PATTERN_2;
3977 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003978 I915_WRITE(reg, temp);
3979
3980 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003981 udelay(150);
3982
Akshay Joshi0206e352011-08-16 15:34:10 -04003983 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003984 reg = FDI_TX_CTL(pipe);
3985 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003986 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3987 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003988 I915_WRITE(reg, temp);
3989
3990 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003991 udelay(500);
3992
Sean Paulfa37d392012-03-02 12:53:39 -05003993 for (retry = 0; retry < 5; retry++) {
3994 reg = FDI_RX_IIR(pipe);
3995 temp = I915_READ(reg);
3996 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3997 if (temp & FDI_RX_SYMBOL_LOCK) {
3998 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3999 DRM_DEBUG_KMS("FDI train 2 done.\n");
4000 break;
4001 }
4002 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004003 }
Sean Paulfa37d392012-03-02 12:53:39 -05004004 if (retry < 5)
4005 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004006 }
4007 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01004008 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004009
4010 DRM_DEBUG_KMS("FDI train done.\n");
4011}
4012
Jesse Barnes357555c2011-04-28 15:09:55 -07004013/* Manual link training for Ivy Bridge A0 parts */
4014static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
4015{
4016 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004017 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes357555c2011-04-28 15:09:55 -07004018 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4019 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004020 i915_reg_t reg;
4021 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07004022
4023 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4024 for train result */
4025 reg = FDI_RX_IMR(pipe);
4026 temp = I915_READ(reg);
4027 temp &= ~FDI_RX_SYMBOL_LOCK;
4028 temp &= ~FDI_RX_BIT_LOCK;
4029 I915_WRITE(reg, temp);
4030
4031 POSTING_READ(reg);
4032 udelay(150);
4033
Daniel Vetter01a415f2012-10-27 15:58:40 +02004034 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4035 I915_READ(FDI_RX_IIR(pipe)));
4036
Jesse Barnes139ccd32013-08-19 11:04:55 -07004037 /* Try each vswing and preemphasis setting twice before moving on */
4038 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4039 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07004040 reg = FDI_TX_CTL(pipe);
4041 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004042 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4043 temp &= ~FDI_TX_ENABLE;
4044 I915_WRITE(reg, temp);
4045
4046 reg = FDI_RX_CTL(pipe);
4047 temp = I915_READ(reg);
4048 temp &= ~FDI_LINK_TRAIN_AUTO;
4049 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4050 temp &= ~FDI_RX_ENABLE;
4051 I915_WRITE(reg, temp);
4052
4053 /* enable CPU FDI TX and PCH FDI RX */
4054 reg = FDI_TX_CTL(pipe);
4055 temp = I915_READ(reg);
4056 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004057 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004058 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07004059 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07004060 temp |= snb_b_fdi_train_param[j/2];
4061 temp |= FDI_COMPOSITE_SYNC;
4062 I915_WRITE(reg, temp | FDI_TX_ENABLE);
4063
4064 I915_WRITE(FDI_RX_MISC(pipe),
4065 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
4066
4067 reg = FDI_RX_CTL(pipe);
4068 temp = I915_READ(reg);
4069 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4070 temp |= FDI_COMPOSITE_SYNC;
4071 I915_WRITE(reg, temp | FDI_RX_ENABLE);
4072
4073 POSTING_READ(reg);
4074 udelay(1); /* should be 0.5us */
4075
4076 for (i = 0; i < 4; i++) {
4077 reg = FDI_RX_IIR(pipe);
4078 temp = I915_READ(reg);
4079 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4080
4081 if (temp & FDI_RX_BIT_LOCK ||
4082 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4083 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4084 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4085 i);
4086 break;
4087 }
4088 udelay(1); /* should be 0.5us */
4089 }
4090 if (i == 4) {
4091 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4092 continue;
4093 }
4094
4095 /* Train 2 */
4096 reg = FDI_TX_CTL(pipe);
4097 temp = I915_READ(reg);
4098 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4099 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4100 I915_WRITE(reg, temp);
4101
4102 reg = FDI_RX_CTL(pipe);
4103 temp = I915_READ(reg);
4104 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4105 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07004106 I915_WRITE(reg, temp);
4107
4108 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07004109 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004110
Jesse Barnes139ccd32013-08-19 11:04:55 -07004111 for (i = 0; i < 4; i++) {
4112 reg = FDI_RX_IIR(pipe);
4113 temp = I915_READ(reg);
4114 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07004115
Jesse Barnes139ccd32013-08-19 11:04:55 -07004116 if (temp & FDI_RX_SYMBOL_LOCK ||
4117 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4118 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4119 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4120 i);
4121 goto train_done;
4122 }
4123 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07004124 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07004125 if (i == 4)
4126 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07004127 }
Jesse Barnes357555c2011-04-28 15:09:55 -07004128
Jesse Barnes139ccd32013-08-19 11:04:55 -07004129train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07004130 DRM_DEBUG_KMS("FDI train done.\n");
4131}
4132
Daniel Vetter88cefb62012-08-12 19:27:14 +02004133static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07004134{
Daniel Vetter88cefb62012-08-12 19:27:14 +02004135 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004136 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004137 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004138 i915_reg_t reg;
4139 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07004140
Jesse Barnes0e23b992010-09-10 11:10:00 -07004141 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01004142 reg = FDI_RX_CTL(pipe);
4143 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02004144 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004145 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004146 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01004147 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4148
4149 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004150 udelay(200);
4151
4152 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01004153 temp = I915_READ(reg);
4154 I915_WRITE(reg, temp | FDI_PCDCLK);
4155
4156 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004157 udelay(200);
4158
Paulo Zanoni20749732012-11-23 15:30:38 -02004159 /* Enable CPU FDI TX PLL, always on for Ironlake */
4160 reg = FDI_TX_CTL(pipe);
4161 temp = I915_READ(reg);
4162 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4163 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01004164
Paulo Zanoni20749732012-11-23 15:30:38 -02004165 POSTING_READ(reg);
4166 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07004167 }
4168}
4169
Daniel Vetter88cefb62012-08-12 19:27:14 +02004170static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4171{
4172 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004173 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter88cefb62012-08-12 19:27:14 +02004174 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004175 i915_reg_t reg;
4176 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02004177
4178 /* Switch from PCDclk to Rawclk */
4179 reg = FDI_RX_CTL(pipe);
4180 temp = I915_READ(reg);
4181 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4182
4183 /* Disable CPU FDI TX PLL */
4184 reg = FDI_TX_CTL(pipe);
4185 temp = I915_READ(reg);
4186 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4187
4188 POSTING_READ(reg);
4189 udelay(100);
4190
4191 reg = FDI_RX_CTL(pipe);
4192 temp = I915_READ(reg);
4193 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4194
4195 /* Wait for the clocks to turn off. */
4196 POSTING_READ(reg);
4197 udelay(100);
4198}
4199
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004200static void ironlake_fdi_disable(struct drm_crtc *crtc)
4201{
4202 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004203 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004204 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4205 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004206 i915_reg_t reg;
4207 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004208
4209 /* disable CPU FDI tx and PCH FDI rx */
4210 reg = FDI_TX_CTL(pipe);
4211 temp = I915_READ(reg);
4212 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4213 POSTING_READ(reg);
4214
4215 reg = FDI_RX_CTL(pipe);
4216 temp = I915_READ(reg);
4217 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004218 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004219 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4220
4221 POSTING_READ(reg);
4222 udelay(100);
4223
4224 /* Ironlake workaround, disable clock pointer after downing FDI */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004225 if (HAS_PCH_IBX(dev_priv))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08004226 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004227
4228 /* still set train pattern 1 */
4229 reg = FDI_TX_CTL(pipe);
4230 temp = I915_READ(reg);
4231 temp &= ~FDI_LINK_TRAIN_NONE;
4232 temp |= FDI_LINK_TRAIN_PATTERN_1;
4233 I915_WRITE(reg, temp);
4234
4235 reg = FDI_RX_CTL(pipe);
4236 temp = I915_READ(reg);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004237 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004238 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4239 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4240 } else {
4241 temp &= ~FDI_LINK_TRAIN_NONE;
4242 temp |= FDI_LINK_TRAIN_PATTERN_1;
4243 }
4244 /* BPC in FDI rx is consistent with that in PIPECONF */
4245 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004246 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08004247 I915_WRITE(reg, temp);
4248
4249 POSTING_READ(reg);
4250 udelay(100);
4251}
4252
Chris Wilson5dce5b932014-01-20 10:17:36 +00004253bool intel_has_pending_fb_unpin(struct drm_device *dev)
4254{
4255 struct intel_crtc *crtc;
4256
4257 /* Note that we don't need to be called with mode_config.lock here
4258 * as our list of CRTC objects is static for the lifetime of the
4259 * device and so cannot disappear as we iterate. Similarly, we can
4260 * happily treat the predicates as racy, atomic checks as userspace
4261 * cannot claim and pin a new fb without at least acquring the
4262 * struct_mutex and so serialising with us.
4263 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004264 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00004265 if (atomic_read(&crtc->unpin_work_count) == 0)
4266 continue;
4267
Daniel Vetter5a21b662016-05-24 17:13:53 +02004268 if (crtc->flip_work)
Chris Wilson5dce5b932014-01-20 10:17:36 +00004269 intel_wait_for_vblank(dev, crtc->pipe);
4270
4271 return true;
4272 }
4273
4274 return false;
4275}
4276
Daniel Vetter5a21b662016-05-24 17:13:53 +02004277static void page_flip_completed(struct intel_crtc *intel_crtc)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004278{
4279 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004280 struct intel_flip_work *work = intel_crtc->flip_work;
4281
4282 intel_crtc->flip_work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004283
4284 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07004285 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004286
4287 drm_crtc_vblank_put(&intel_crtc->base);
4288
Daniel Vetter5a21b662016-05-24 17:13:53 +02004289 wake_up_all(&dev_priv->pending_flip_queue);
Maarten Lankhorst143f73b2016-05-17 15:07:54 +02004290 queue_work(dev_priv->wq, &work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +02004291
4292 trace_i915_flip_complete(intel_crtc->plane,
4293 work->pending_flip_obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01004294}
4295
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004296static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004297{
Chris Wilson0f911282012-04-17 10:05:38 +01004298 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004299 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004300 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004301
Daniel Vetter2c10d572012-12-20 21:24:07 +01004302 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004303
4304 ret = wait_event_interruptible_timeout(
4305 dev_priv->pending_flip_queue,
4306 !intel_crtc_has_pending_flip(crtc),
4307 60*HZ);
4308
4309 if (ret < 0)
4310 return ret;
4311
Daniel Vetter5a21b662016-05-24 17:13:53 +02004312 if (ret == 0) {
4313 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4314 struct intel_flip_work *work;
4315
4316 spin_lock_irq(&dev->event_lock);
4317 work = intel_crtc->flip_work;
4318 if (work && !is_mmio_work(work)) {
4319 WARN_ONCE(1, "Removing stuck page flip\n");
4320 page_flip_completed(intel_crtc);
4321 }
4322 spin_unlock_irq(&dev->event_lock);
4323 }
Chris Wilson5bb61642012-09-27 21:25:58 +01004324
Maarten Lankhorst5008e872015-08-18 13:40:05 +02004325 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01004326}
4327
Maarten Lankhorstb7076542016-08-23 16:18:08 +02004328void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004329{
4330 u32 temp;
4331
4332 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4333
4334 mutex_lock(&dev_priv->sb_lock);
4335
4336 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4337 temp |= SBI_SSCCTL_DISABLE;
4338 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4339
4340 mutex_unlock(&dev_priv->sb_lock);
4341}
4342
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004343/* Program iCLKIP clock to the desired frequency */
4344static void lpt_program_iclkip(struct drm_crtc *crtc)
4345{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004346 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004347 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004348 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4349 u32 temp;
4350
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004351 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004352
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004353 /* The iCLK virtual clock root frequency is in MHz,
4354 * but the adjusted_mode->crtc_clock in in KHz. To get the
4355 * divisors, it is necessary to divide one by another, so we
4356 * convert the virtual clock precision to KHz here for higher
4357 * precision.
4358 */
4359 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004360 u32 iclk_virtual_root_freq = 172800 * 1000;
4361 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004362 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004363
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004364 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4365 clock << auxdiv);
4366 divsel = (desired_divisor / iclk_pi_range) - 2;
4367 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004368
Ville Syrjälä64b46a02016-02-17 21:41:11 +02004369 /*
4370 * Near 20MHz is a corner case which is
4371 * out of range for the 7-bit divisor
4372 */
4373 if (divsel <= 0x7f)
4374 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004375 }
4376
4377 /* This should not happen with any sane values */
4378 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4379 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4380 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4381 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4382
4383 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 +03004384 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004385 auxdiv,
4386 divsel,
4387 phasedir,
4388 phaseinc);
4389
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004390 mutex_lock(&dev_priv->sb_lock);
4391
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004392 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004393 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004394 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4395 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4396 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4397 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4398 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4399 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004400 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004401
4402 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004403 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004404 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4405 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004406 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004407
4408 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004409 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004410 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004411 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004412
Ville Syrjälä060f02d2015-12-04 22:21:34 +02004413 mutex_unlock(&dev_priv->sb_lock);
4414
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004415 /* Wait for initialization time */
4416 udelay(24);
4417
4418 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4419}
4420
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02004421int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4422{
4423 u32 divsel, phaseinc, auxdiv;
4424 u32 iclk_virtual_root_freq = 172800 * 1000;
4425 u32 iclk_pi_range = 64;
4426 u32 desired_divisor;
4427 u32 temp;
4428
4429 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4430 return 0;
4431
4432 mutex_lock(&dev_priv->sb_lock);
4433
4434 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4435 if (temp & SBI_SSCCTL_DISABLE) {
4436 mutex_unlock(&dev_priv->sb_lock);
4437 return 0;
4438 }
4439
4440 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4441 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4442 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4443 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4444 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4445
4446 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4447 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4448 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4449
4450 mutex_unlock(&dev_priv->sb_lock);
4451
4452 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4453
4454 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4455 desired_divisor << auxdiv);
4456}
4457
Daniel Vetter275f01b22013-05-03 11:49:47 +02004458static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4459 enum pipe pch_transcoder)
4460{
4461 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004462 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004463 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004464
4465 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4466 I915_READ(HTOTAL(cpu_transcoder)));
4467 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4468 I915_READ(HBLANK(cpu_transcoder)));
4469 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4470 I915_READ(HSYNC(cpu_transcoder)));
4471
4472 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4473 I915_READ(VTOTAL(cpu_transcoder)));
4474 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4475 I915_READ(VBLANK(cpu_transcoder)));
4476 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4477 I915_READ(VSYNC(cpu_transcoder)));
4478 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4479 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4480}
4481
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004482static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004483{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004484 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004485 uint32_t temp;
4486
4487 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004488 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004489 return;
4490
4491 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4492 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4493
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004494 temp &= ~FDI_BC_BIFURCATION_SELECT;
4495 if (enable)
4496 temp |= FDI_BC_BIFURCATION_SELECT;
4497
4498 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004499 I915_WRITE(SOUTH_CHICKEN1, temp);
4500 POSTING_READ(SOUTH_CHICKEN1);
4501}
4502
4503static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4504{
4505 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004506
4507 switch (intel_crtc->pipe) {
4508 case PIPE_A:
4509 break;
4510 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004511 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004512 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004513 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004514 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004515
4516 break;
4517 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004518 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004519
4520 break;
4521 default:
4522 BUG();
4523 }
4524}
4525
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004526/* Return which DP Port should be selected for Transcoder DP control */
4527static enum port
4528intel_trans_dp_port_sel(struct drm_crtc *crtc)
4529{
4530 struct drm_device *dev = crtc->dev;
4531 struct intel_encoder *encoder;
4532
4533 for_each_encoder_on_crtc(dev, crtc, encoder) {
Ville Syrjäläcca05022016-06-22 21:57:06 +03004534 if (encoder->type == INTEL_OUTPUT_DP ||
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004535 encoder->type == INTEL_OUTPUT_EDP)
4536 return enc_to_dig_port(&encoder->base)->port;
4537 }
4538
4539 return -1;
4540}
4541
Jesse Barnesf67a5592011-01-05 10:31:48 -08004542/*
4543 * Enable PCH resources required for PCH ports:
4544 * - PCH PLLs
4545 * - FDI training & RX/TX
4546 * - update transcoder timings
4547 * - DP transcoding bits
4548 * - transcoder
4549 */
4550static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004551{
4552 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004553 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004554 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4555 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004556 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004557
Daniel Vetterab9412b2013-05-03 11:49:46 +02004558 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004559
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004560 if (IS_IVYBRIDGE(dev_priv))
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004561 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4562
Daniel Vettercd986ab2012-10-26 10:58:12 +02004563 /* Write the TU size bits before fdi link training, so that error
4564 * detection works. */
4565 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4566 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4567
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004568 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004569 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004570
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004571 /* We need to program the right clock selection before writing the pixel
4572 * mutliplier into the DPLL. */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004573 if (HAS_PCH_CPT(dev_priv)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004574 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004575
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004576 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004577 temp |= TRANS_DPLL_ENABLE(pipe);
4578 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004579 if (intel_crtc->config->shared_dpll ==
4580 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004581 temp |= sel;
4582 else
4583 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004584 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004585 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004586
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004587 /* XXX: pch pll's can be enabled any time before we enable the PCH
4588 * transcoder, and we actually should do this to not upset any PCH
4589 * transcoder that already use the clock when we share it.
4590 *
4591 * Note that enable_shared_dpll tries to do the right thing, but
4592 * get_shared_dpll unconditionally resets the pll - we need that to have
4593 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004594 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004595
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004596 /* set transcoder timing, panel must allow it */
4597 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004598 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004599
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004600 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004601
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004602 /* For PCH DP, enable TRANS_DP_CTL */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004603 if (HAS_PCH_CPT(dev_priv) &&
4604 intel_crtc_has_dp_encoder(intel_crtc->config)) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004605 const struct drm_display_mode *adjusted_mode =
4606 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004607 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004608 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004609 temp = I915_READ(reg);
4610 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004611 TRANS_DP_SYNC_MASK |
4612 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004613 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004614 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004615
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004616 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004617 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004618 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004619 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004620
4621 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004622 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004623 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004624 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004625 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004626 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004627 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004628 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004629 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004630 break;
4631 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004632 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004633 }
4634
Chris Wilson5eddb702010-09-11 13:48:45 +01004635 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004636 }
4637
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004638 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004639}
4640
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004641static void lpt_pch_enable(struct drm_crtc *crtc)
4642{
4643 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004644 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004645 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004646 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004647
Daniel Vetterab9412b2013-05-03 11:49:46 +02004648 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004649
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004650 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004651
Paulo Zanoni0540e482012-10-31 18:12:40 -02004652 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004653 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004654
Paulo Zanoni937bb612012-10-31 18:12:47 -02004655 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004656}
4657
Daniel Vettera1520312013-05-03 11:49:50 +02004658static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004659{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004660 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004661 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004662 u32 temp;
4663
4664 temp = I915_READ(dslreg);
4665 udelay(500);
4666 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004667 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004668 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004669 }
4670}
4671
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004672static int
4673skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4674 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4675 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004676{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004677 struct intel_crtc_scaler_state *scaler_state =
4678 &crtc_state->scaler_state;
4679 struct intel_crtc *intel_crtc =
4680 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004681 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004682
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03004683 need_scaling = drm_rotation_90_or_270(rotation) ?
Chandra Konduru6156a452015-04-27 13:48:39 -07004684 (src_h != dst_w || src_w != dst_h):
4685 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004686
4687 /*
4688 * if plane is being disabled or scaler is no more required or force detach
4689 * - free scaler binded to this plane/crtc
4690 * - in order to do this, update crtc->scaler_usage
4691 *
4692 * Here scaler state in crtc_state is set free so that
4693 * scaler can be assigned to other user. Actual register
4694 * update to free the scaler is done in plane/panel-fit programming.
4695 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4696 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004697 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004698 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004699 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004700 scaler_state->scalers[*scaler_id].in_use = 0;
4701
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004702 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4703 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4704 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004705 scaler_state->scaler_users);
4706 *scaler_id = -1;
4707 }
4708 return 0;
4709 }
4710
4711 /* range checks */
4712 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4713 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4714
4715 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4716 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004717 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004718 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004719 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004720 return -EINVAL;
4721 }
4722
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004723 /* mark this plane as a scaler user in crtc_state */
4724 scaler_state->scaler_users |= (1 << scaler_user);
4725 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4726 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4727 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4728 scaler_state->scaler_users);
4729
4730 return 0;
4731}
4732
4733/**
4734 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4735 *
4736 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004737 *
4738 * Return
4739 * 0 - scaler_usage updated successfully
4740 * error - requested scaling cannot be supported or other error condition
4741 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004742int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004743{
4744 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004745 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004746
Ville Syrjälä78108b72016-05-27 20:59:19 +03004747 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4748 intel_crtc->base.base.id, intel_crtc->base.name,
4749 intel_crtc->pipe, SKL_CRTC_INDEX);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004750
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004751 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03004752 &state->scaler_state.scaler_id, DRM_ROTATE_0,
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004753 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004754 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004755}
4756
4757/**
4758 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4759 *
4760 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004761 * @plane_state: atomic plane state to update
4762 *
4763 * Return
4764 * 0 - scaler_usage updated successfully
4765 * error - requested scaling cannot be supported or other error condition
4766 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004767static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4768 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004769{
4770
4771 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004772 struct intel_plane *intel_plane =
4773 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004774 struct drm_framebuffer *fb = plane_state->base.fb;
4775 int ret;
4776
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004777 bool force_detach = !fb || !plane_state->base.visible;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004778
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004779 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4780 intel_plane->base.base.id, intel_plane->base.name,
4781 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004782
4783 ret = skl_update_scaler(crtc_state, force_detach,
4784 drm_plane_index(&intel_plane->base),
4785 &plane_state->scaler_id,
4786 plane_state->base.rotation,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03004787 drm_rect_width(&plane_state->base.src) >> 16,
4788 drm_rect_height(&plane_state->base.src) >> 16,
4789 drm_rect_width(&plane_state->base.dst),
4790 drm_rect_height(&plane_state->base.dst));
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004791
4792 if (ret || plane_state->scaler_id < 0)
4793 return ret;
4794
Chandra Kondurua1b22782015-04-07 15:28:45 -07004795 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004796 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004797 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4798 intel_plane->base.base.id,
4799 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004800 return -EINVAL;
4801 }
4802
4803 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004804 switch (fb->pixel_format) {
4805 case DRM_FORMAT_RGB565:
4806 case DRM_FORMAT_XBGR8888:
4807 case DRM_FORMAT_XRGB8888:
4808 case DRM_FORMAT_ABGR8888:
4809 case DRM_FORMAT_ARGB8888:
4810 case DRM_FORMAT_XRGB2101010:
4811 case DRM_FORMAT_XBGR2101010:
4812 case DRM_FORMAT_YUYV:
4813 case DRM_FORMAT_YVYU:
4814 case DRM_FORMAT_UYVY:
4815 case DRM_FORMAT_VYUY:
4816 break;
4817 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004818 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4819 intel_plane->base.base.id, intel_plane->base.name,
4820 fb->base.id, fb->pixel_format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004821 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004822 }
4823
Chandra Kondurua1b22782015-04-07 15:28:45 -07004824 return 0;
4825}
4826
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004827static void skylake_scaler_disable(struct intel_crtc *crtc)
4828{
4829 int i;
4830
4831 for (i = 0; i < crtc->num_scalers; i++)
4832 skl_detach_scaler(crtc, i);
4833}
4834
4835static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004836{
4837 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004838 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004839 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004840 struct intel_crtc_scaler_state *scaler_state =
4841 &crtc->config->scaler_state;
4842
4843 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4844
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004845 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004846 int id;
4847
4848 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4849 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4850 return;
4851 }
4852
4853 id = scaler_state->scaler_id;
4854 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4855 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4856 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4857 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4858
4859 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004860 }
4861}
4862
Jesse Barnesb074cec2013-04-25 12:55:02 -07004863static void ironlake_pfit_enable(struct intel_crtc *crtc)
4864{
4865 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004866 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb074cec2013-04-25 12:55:02 -07004867 int pipe = crtc->pipe;
4868
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004869 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004870 /* Force use of hard-coded filter coefficients
4871 * as some pre-programmed values are broken,
4872 * e.g. x201.
4873 */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004874 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
Jesse Barnesb074cec2013-04-25 12:55:02 -07004875 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4876 PF_PIPE_SEL_IVB(pipe));
4877 else
4878 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004879 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4880 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004881 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004882}
4883
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004884void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004885{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004886 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004887 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004888
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004889 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004890 return;
4891
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004892 /*
4893 * We can only enable IPS after we enable a plane and wait for a vblank
4894 * This function is called from post_plane_update, which is run after
4895 * a vblank wait.
4896 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004897
Paulo Zanonid77e4532013-09-24 13:52:55 -03004898 assert_plane_enabled(dev_priv, crtc->plane);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004899 if (IS_BROADWELL(dev_priv)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004900 mutex_lock(&dev_priv->rps.hw_lock);
4901 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4902 mutex_unlock(&dev_priv->rps.hw_lock);
4903 /* Quoting Art Runyan: "its not safe to expect any particular
4904 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004905 * mailbox." Moreover, the mailbox may return a bogus state,
4906 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004907 */
4908 } else {
4909 I915_WRITE(IPS_CTL, IPS_ENABLE);
4910 /* The bit only becomes 1 in the next vblank, so this wait here
4911 * is essentially intel_wait_for_vblank. If we don't have this
4912 * and don't wait for vblanks until the end of crtc_enable, then
4913 * the HW state readout code will complain that the expected
4914 * IPS_CTL value is not the one we read. */
Chris Wilson2ec9ba32016-06-30 15:33:01 +01004915 if (intel_wait_for_register(dev_priv,
4916 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4917 50))
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004918 DRM_ERROR("Timed out waiting for IPS enable\n");
4919 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004920}
4921
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004922void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004923{
4924 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004925 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004926
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004927 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004928 return;
4929
4930 assert_plane_enabled(dev_priv, crtc->plane);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004931 if (IS_BROADWELL(dev_priv)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004932 mutex_lock(&dev_priv->rps.hw_lock);
4933 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4934 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004935 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
Chris Wilsonb85c1ec2016-06-30 15:33:02 +01004936 if (intel_wait_for_register(dev_priv,
4937 IPS_CTL, IPS_ENABLE, 0,
4938 42))
Ben Widawsky23d0b132014-04-10 14:32:41 -07004939 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004940 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004941 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004942 POSTING_READ(IPS_CTL);
4943 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004944
4945 /* We need to wait for a vblank before we can disable the plane. */
4946 intel_wait_for_vblank(dev, crtc->pipe);
4947}
4948
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004949static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004950{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004951 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004952 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004953 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004954
4955 mutex_lock(&dev->struct_mutex);
4956 dev_priv->mm.interruptible = false;
4957 (void) intel_overlay_switch_off(intel_crtc->overlay);
4958 dev_priv->mm.interruptible = true;
4959 mutex_unlock(&dev->struct_mutex);
4960 }
4961
4962 /* Let userspace switch the overlay on again. In most cases userspace
4963 * has to recompute where to put it anyway.
4964 */
4965}
4966
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004967/**
4968 * intel_post_enable_primary - Perform operations after enabling primary plane
4969 * @crtc: the CRTC whose primary plane was just enabled
4970 *
4971 * Performs potentially sleeping operations that must be done after the primary
4972 * plane is enabled, such as updating FBC and IPS. Note that this may be
4973 * called due to an explicit primary plane update, or due to an implicit
4974 * re-enable that is caused when a sprite plane is updated to no longer
4975 * completely hide the primary plane.
4976 */
4977static void
4978intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004979{
4980 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004981 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4983 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004984
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004985 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004986 * FIXME IPS should be fine as long as one plane is
4987 * enabled, but in practice it seems to have problems
4988 * when going from primary only to sprite only and vice
4989 * versa.
4990 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004991 hsw_enable_ips(intel_crtc);
4992
Daniel Vetterf99d7062014-06-19 16:01:59 +02004993 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004994 * Gen2 reports pipe underruns whenever all planes are disabled.
4995 * So don't enable underrun reporting before at least some planes
4996 * are enabled.
4997 * FIXME: Need to fix the logic to work when we turn off all planes
4998 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004999 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005000 if (IS_GEN2(dev_priv))
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005001 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5002
Ville Syrjäläaca7b682015-10-30 19:22:21 +02005003 /* Underruns don't always raise interrupts, so check manually. */
5004 intel_check_cpu_fifo_underruns(dev_priv);
5005 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005006}
5007
Ville Syrjälä2622a082016-03-09 19:07:26 +02005008/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005009static void
5010intel_pre_disable_primary(struct drm_crtc *crtc)
5011{
5012 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005013 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005014 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5015 int pipe = intel_crtc->pipe;
5016
5017 /*
5018 * Gen2 reports pipe underruns whenever all planes are disabled.
5019 * So diasble underrun reporting before all the planes get disabled.
5020 * FIXME: Need to fix the logic to work when we turn off all planes
5021 * but leave the pipe running.
5022 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005023 if (IS_GEN2(dev_priv))
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005024 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5025
5026 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02005027 * FIXME IPS should be fine as long as one plane is
5028 * enabled, but in practice it seems to have problems
5029 * when going from primary only to sprite only and vice
5030 * versa.
5031 */
5032 hsw_disable_ips(intel_crtc);
5033}
5034
5035/* FIXME get rid of this and use pre_plane_update */
5036static void
5037intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
5038{
5039 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005040 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +02005041 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5042 int pipe = intel_crtc->pipe;
5043
5044 intel_pre_disable_primary(crtc);
5045
5046 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005047 * Vblank time updates from the shadow to live plane control register
5048 * are blocked if the memory self-refresh mode is active at that
5049 * moment. So to make sure the plane gets truly disabled, disable
5050 * first the self-refresh mode. The self-refresh enable bit in turn
5051 * will be checked/applied by the HW only at the next frame start
5052 * event which is after the vblank start event, so we need to have a
5053 * wait-for-vblank between disabling the plane and the pipe.
5054 */
Tvrtko Ursulin49cff962016-10-13 11:02:54 +01005055 if (HAS_GMCH_DISPLAY(dev_priv)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005056 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03005057 dev_priv->wm.vlv.cxsr = false;
5058 intel_wait_for_vblank(dev, pipe);
5059 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03005060}
5061
Daniel Vetter5a21b662016-05-24 17:13:53 +02005062static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5063{
5064 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
5065 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5066 struct intel_crtc_state *pipe_config =
5067 to_intel_crtc_state(crtc->base.state);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005068 struct drm_plane *primary = crtc->base.primary;
5069 struct drm_plane_state *old_pri_state =
5070 drm_atomic_get_existing_plane_state(old_state, primary);
5071
Chris Wilson5748b6a2016-08-04 16:32:38 +01005072 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
Daniel Vetter5a21b662016-05-24 17:13:53 +02005073
5074 crtc->wm.cxsr_allowed = true;
5075
5076 if (pipe_config->update_wm_post && pipe_config->base.active)
5077 intel_update_watermarks(&crtc->base);
5078
5079 if (old_pri_state) {
5080 struct intel_plane_state *primary_state =
5081 to_intel_plane_state(primary->state);
5082 struct intel_plane_state *old_primary_state =
5083 to_intel_plane_state(old_pri_state);
5084
5085 intel_fbc_post_update(crtc);
5086
Ville Syrjälä936e71e2016-07-26 19:06:59 +03005087 if (primary_state->base.visible &&
Daniel Vetter5a21b662016-05-24 17:13:53 +02005088 (needs_modeset(&pipe_config->base) ||
Ville Syrjälä936e71e2016-07-26 19:06:59 +03005089 !old_primary_state->base.visible))
Daniel Vetter5a21b662016-05-24 17:13:53 +02005090 intel_post_enable_primary(&crtc->base);
5091 }
5092}
5093
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005094static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005095{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005096 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005097 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005098 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01005099 struct intel_crtc_state *pipe_config =
5100 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005101 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5102 struct drm_plane *primary = crtc->base.primary;
5103 struct drm_plane_state *old_pri_state =
5104 drm_atomic_get_existing_plane_state(old_state, primary);
5105 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005106
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005107 if (old_pri_state) {
5108 struct intel_plane_state *primary_state =
5109 to_intel_plane_state(primary->state);
5110 struct intel_plane_state *old_primary_state =
5111 to_intel_plane_state(old_pri_state);
5112
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +02005113 intel_fbc_pre_update(crtc, pipe_config, primary_state);
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01005114
Ville Syrjälä936e71e2016-07-26 19:06:59 +03005115 if (old_primary_state->base.visible &&
5116 (modeset || !primary_state->base.visible))
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01005117 intel_pre_disable_primary(&crtc->base);
5118 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03005119
Tvrtko Ursulin49cff962016-10-13 11:02:54 +01005120 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03005121 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01005122
Ville Syrjälä2622a082016-03-09 19:07:26 +02005123 /*
5124 * Vblank time updates from the shadow to live plane control register
5125 * are blocked if the memory self-refresh mode is active at that
5126 * moment. So to make sure the plane gets truly disabled, disable
5127 * first the self-refresh mode. The self-refresh enable bit in turn
5128 * will be checked/applied by the HW only at the next frame start
5129 * event which is after the vblank start event, so we need to have a
5130 * wait-for-vblank between disabling the plane and the pipe.
5131 */
5132 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01005133 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02005134 dev_priv->wm.vlv.cxsr = false;
5135 intel_wait_for_vblank(dev, crtc->pipe);
5136 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03005137 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01005138
Matt Ropered4a6a72016-02-23 17:20:13 -08005139 /*
5140 * IVB workaround: must disable low power watermarks for at least
5141 * one frame before enabling scaling. LP watermarks can be re-enabled
5142 * when scaling is disabled.
5143 *
5144 * WaCxSRDisabledForSpriteScaling:ivb
5145 */
5146 if (pipe_config->disable_lp_wm) {
5147 ilk_disable_lp_wm(dev);
5148 intel_wait_for_vblank(dev, crtc->pipe);
5149 }
5150
5151 /*
5152 * If we're doing a modeset, we're done. No need to do any pre-vblank
5153 * watermark programming here.
5154 */
5155 if (needs_modeset(&pipe_config->base))
5156 return;
5157
5158 /*
5159 * For platforms that support atomic watermarks, program the
5160 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5161 * will be the intermediate values that are safe for both pre- and
5162 * post- vblank; when vblank happens, the 'active' values will be set
5163 * to the final 'target' values and we'll do this again to get the
5164 * optimal watermarks. For gen9+ platforms, the values we program here
5165 * will be the final target values which will get automatically latched
5166 * at vblank time; no further programming will be necessary.
5167 *
5168 * If a platform hasn't been transitioned to atomic watermarks yet,
5169 * we'll continue to update watermarks the old way, if flags tell
5170 * us to.
5171 */
5172 if (dev_priv->display.initial_watermarks != NULL)
5173 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02005174 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01005175 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02005176}
5177
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005178static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005179{
5180 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005181 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005182 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005183 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005184
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03005185 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03005186
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02005187 drm_for_each_plane_mask(p, dev, plane_mask)
5188 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03005189
Daniel Vetterf99d7062014-06-19 16:01:59 +02005190 /*
5191 * FIXME: Once we grow proper nuclear flip support out of this we need
5192 * to compute the mask of flip planes precisely. For the time being
5193 * consider this a flip to a NULL plane.
5194 */
Chris Wilson5748b6a2016-08-04 16:32:38 +01005195 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005196}
5197
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005198static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005199 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005200 struct drm_atomic_state *old_state)
5201{
5202 struct drm_connector_state *old_conn_state;
5203 struct drm_connector *conn;
5204 int i;
5205
5206 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5207 struct drm_connector_state *conn_state = conn->state;
5208 struct intel_encoder *encoder =
5209 to_intel_encoder(conn_state->best_encoder);
5210
5211 if (conn_state->crtc != crtc)
5212 continue;
5213
5214 if (encoder->pre_pll_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005215 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005216 }
5217}
5218
5219static void intel_encoders_pre_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005220 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005221 struct drm_atomic_state *old_state)
5222{
5223 struct drm_connector_state *old_conn_state;
5224 struct drm_connector *conn;
5225 int i;
5226
5227 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5228 struct drm_connector_state *conn_state = conn->state;
5229 struct intel_encoder *encoder =
5230 to_intel_encoder(conn_state->best_encoder);
5231
5232 if (conn_state->crtc != crtc)
5233 continue;
5234
5235 if (encoder->pre_enable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005236 encoder->pre_enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005237 }
5238}
5239
5240static void intel_encoders_enable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005241 struct intel_crtc_state *crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005242 struct drm_atomic_state *old_state)
5243{
5244 struct drm_connector_state *old_conn_state;
5245 struct drm_connector *conn;
5246 int i;
5247
5248 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5249 struct drm_connector_state *conn_state = conn->state;
5250 struct intel_encoder *encoder =
5251 to_intel_encoder(conn_state->best_encoder);
5252
5253 if (conn_state->crtc != crtc)
5254 continue;
5255
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005256 encoder->enable(encoder, crtc_state, conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005257 intel_opregion_notify_encoder(encoder, true);
5258 }
5259}
5260
5261static void intel_encoders_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005262 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005263 struct drm_atomic_state *old_state)
5264{
5265 struct drm_connector_state *old_conn_state;
5266 struct drm_connector *conn;
5267 int i;
5268
5269 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5270 struct intel_encoder *encoder =
5271 to_intel_encoder(old_conn_state->best_encoder);
5272
5273 if (old_conn_state->crtc != crtc)
5274 continue;
5275
5276 intel_opregion_notify_encoder(encoder, false);
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005277 encoder->disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005278 }
5279}
5280
5281static void intel_encoders_post_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005282 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005283 struct drm_atomic_state *old_state)
5284{
5285 struct drm_connector_state *old_conn_state;
5286 struct drm_connector *conn;
5287 int i;
5288
5289 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5290 struct intel_encoder *encoder =
5291 to_intel_encoder(old_conn_state->best_encoder);
5292
5293 if (old_conn_state->crtc != crtc)
5294 continue;
5295
5296 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005297 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005298 }
5299}
5300
5301static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005302 struct intel_crtc_state *old_crtc_state,
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005303 struct drm_atomic_state *old_state)
5304{
5305 struct drm_connector_state *old_conn_state;
5306 struct drm_connector *conn;
5307 int i;
5308
5309 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5310 struct intel_encoder *encoder =
5311 to_intel_encoder(old_conn_state->best_encoder);
5312
5313 if (old_conn_state->crtc != crtc)
5314 continue;
5315
5316 if (encoder->post_pll_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005317 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
Maarten Lankhorstfb1c98b2016-08-09 17:04:03 +02005318 }
5319}
5320
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005321static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5322 struct drm_atomic_state *old_state)
Jesse Barnesf67a5592011-01-05 10:31:48 -08005323{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005324 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnesf67a5592011-01-05 10:31:48 -08005325 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005326 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005327 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5328 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08005329
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005330 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08005331 return;
5332
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005333 /*
5334 * Sometimes spurious CPU pipe underruns happen during FDI
5335 * training, at least with VGA+HDMI cloning. Suppress them.
5336 *
5337 * On ILK we get an occasional spurious CPU pipe underruns
5338 * between eDP port A enable and vdd enable. Also PCH port
5339 * enable seems to result in the occasional CPU pipe underrun.
5340 *
5341 * Spurious PCH underruns also occur during PCH enabling.
5342 */
5343 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5344 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005345 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005346 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5347
5348 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02005349 intel_prepare_shared_dpll(intel_crtc);
5350
Ville Syrjälä37a56502016-06-22 21:57:04 +03005351 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305352 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005353
5354 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02005355 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005356
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005357 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02005358 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005359 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02005360 }
5361
5362 ironlake_set_pipeconf(crtc);
5363
Jesse Barnesf67a5592011-01-05 10:31:48 -08005364 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005365
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005366 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005367
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005368 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02005369 /* Note: FDI PLL enabling _must_ be done before we enable the
5370 * cpu pipes, hence this is separate from all the other fdi/pch
5371 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02005372 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02005373 } else {
5374 assert_fdi_tx_disabled(dev_priv, pipe);
5375 assert_fdi_rx_disabled(dev_priv, pipe);
5376 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08005377
Jesse Barnesb074cec2013-04-25 12:55:02 -07005378 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005379
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005380 /*
5381 * On ILK+ LUT must be loaded before the pipe is running but with
5382 * clocks enabled
5383 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005384 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02005385
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005386 if (dev_priv->display.initial_watermarks != NULL)
5387 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005388 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08005389
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005390 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08005391 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005392
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005393 assert_vblank_disabled(crtc);
5394 drm_crtc_vblank_on(crtc);
5395
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005396 intel_encoders_enable(crtc, pipe_config, old_state);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02005397
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005398 if (HAS_PCH_CPT(dev_priv))
Daniel Vettera1520312013-05-03 11:49:50 +02005399 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005400
5401 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5402 if (intel_crtc->config->has_pch_encoder)
5403 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005404 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005405 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005406}
5407
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005408/* IPS only exists on ULT machines and is tied to pipe A. */
5409static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5410{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005411 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005412}
5413
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005414static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5415 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005416{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005417 struct drm_crtc *crtc = pipe_config->base.crtc;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005418 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005419 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005420 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005421 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02005422 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005423
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005424 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005425 return;
5426
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005427 if (intel_crtc->config->has_pch_encoder)
5428 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5429 false);
5430
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005431 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Imre Deak95a7a2a2016-06-13 16:44:35 +03005432
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02005433 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005434 intel_enable_shared_dpll(intel_crtc);
5435
Ville Syrjälä37a56502016-06-22 21:57:04 +03005436 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305437 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005438
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005439 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005440 intel_set_pipe_timings(intel_crtc);
5441
Jani Nikulabc58be62016-03-18 17:05:39 +02005442 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02005443
Jani Nikula4d1de972016-03-18 17:05:42 +02005444 if (cpu_transcoder != TRANSCODER_EDP &&
5445 !transcoder_is_dsi(cpu_transcoder)) {
5446 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005447 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005448 }
5449
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005450 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005451 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005452 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005453 }
5454
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005455 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005456 haswell_set_pipeconf(crtc);
5457
Jani Nikula391bf042016-03-18 17:05:40 +02005458 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02005459
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005460 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02005461
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005462 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005463
Daniel Vetter6b698512015-11-28 11:05:39 +01005464 if (intel_crtc->config->has_pch_encoder)
5465 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5466 else
5467 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5468
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005469 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005470
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005471 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03005472 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03005473
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005474 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305475 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005476
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005477 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005478 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005479 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005480 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005481
5482 /*
5483 * On ILK+ LUT must be loaded before the pipe is running but with
5484 * clocks enabled
5485 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02005486 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005487
Paulo Zanoni1f544382012-10-24 11:32:00 -02005488 intel_ddi_set_pipe_settings(crtc);
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005489 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305490 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005491
Imre Deak1d5bf5d2016-02-29 22:10:33 +02005492 if (dev_priv->display.initial_watermarks != NULL)
5493 dev_priv->display.initial_watermarks(pipe_config);
5494 else
5495 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02005496
5497 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005498 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005499 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005500
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005501 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005502 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005503
Jani Nikulaa65347b2015-11-27 12:21:46 +02005504 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005505 intel_ddi_set_vc_payload_alloc(crtc, true);
5506
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005507 assert_vblank_disabled(crtc);
5508 drm_crtc_vblank_on(crtc);
5509
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005510 intel_encoders_enable(crtc, pipe_config, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005511
Daniel Vetter6b698512015-11-28 11:05:39 +01005512 if (intel_crtc->config->has_pch_encoder) {
5513 intel_wait_for_vblank(dev, pipe);
5514 intel_wait_for_vblank(dev, pipe);
5515 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005516 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5517 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01005518 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005519
Paulo Zanonie4916942013-09-20 16:21:19 -03005520 /* If we change the relative order between pipe/planes enabling, we need
5521 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005522 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005523 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02005524 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5525 intel_wait_for_vblank(dev, hsw_workaround_pipe);
5526 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005527}
5528
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005529static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005530{
5531 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005532 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005533 int pipe = crtc->pipe;
5534
5535 /* To avoid upsetting the power well on haswell only disable the pfit if
5536 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005537 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005538 I915_WRITE(PF_CTL(pipe), 0);
5539 I915_WRITE(PF_WIN_POS(pipe), 0);
5540 I915_WRITE(PF_WIN_SZ(pipe), 0);
5541 }
5542}
5543
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005544static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5545 struct drm_atomic_state *old_state)
Jesse Barnes6be4a602010-09-10 10:26:01 -07005546{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005547 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005548 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005549 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005550 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5551 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005552
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005553 /*
5554 * Sometimes spurious CPU pipe underruns happen when the
5555 * pipe is already disabled, but FDI RX/TX is still enabled.
5556 * Happens at least with VGA+HDMI cloning. Suppress them.
5557 */
5558 if (intel_crtc->config->has_pch_encoder) {
5559 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005560 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005561 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02005562
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005563 intel_encoders_disable(crtc, old_crtc_state, old_state);
Daniel Vetterea9d7582012-07-10 10:42:52 +02005564
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005565 drm_crtc_vblank_off(crtc);
5566 assert_vblank_disabled(crtc);
5567
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005568 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005569
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005570 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005571
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005572 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005573 ironlake_fdi_disable(crtc);
5574
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005575 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005576
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005577 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005578 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005579
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005580 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005581 i915_reg_t reg;
5582 u32 temp;
5583
Daniel Vetterd925c592013-06-05 13:34:04 +02005584 /* disable TRANS_DP_CTL */
5585 reg = TRANS_DP_CTL(pipe);
5586 temp = I915_READ(reg);
5587 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5588 TRANS_DP_PORT_SEL_MASK);
5589 temp |= TRANS_DP_PORT_SEL_NONE;
5590 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005591
Daniel Vetterd925c592013-06-05 13:34:04 +02005592 /* disable DPLL_SEL */
5593 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005594 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005595 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005596 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005597
Daniel Vetterd925c592013-06-05 13:34:04 +02005598 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005599 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005600
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005601 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005602 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005603}
5604
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005605static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5606 struct drm_atomic_state *old_state)
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005607{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02005608 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005609 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005610 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005611 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005612 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005613
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005614 if (intel_crtc->config->has_pch_encoder)
5615 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5616 false);
5617
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005618 intel_encoders_disable(crtc, old_crtc_state, old_state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005619
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005620 drm_crtc_vblank_off(crtc);
5621 assert_vblank_disabled(crtc);
5622
Jani Nikula4d1de972016-03-18 17:05:42 +02005623 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005624 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005625 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005626
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005627 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005628 intel_ddi_set_vc_payload_alloc(crtc, false);
5629
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005630 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305631 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005632
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005633 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005634 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005635 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005636 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005637
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005638 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305639 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005640
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02005641 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005642
Maarten Lankhorstb7076542016-08-23 16:18:08 +02005643 if (old_crtc_state->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005644 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5645 true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005646}
5647
Jesse Barnes2dd24552013-04-25 12:55:01 -07005648static void i9xx_pfit_enable(struct intel_crtc *crtc)
5649{
5650 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005651 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005652 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005653
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005654 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005655 return;
5656
Daniel Vetterc0b03412013-05-28 12:05:54 +02005657 /*
5658 * The panel fitter should only be adjusted whilst the pipe is disabled,
5659 * according to register description and PRM.
5660 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005661 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5662 assert_pipe_disabled(dev_priv, crtc->pipe);
5663
Jesse Barnesb074cec2013-04-25 12:55:02 -07005664 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5665 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005666
5667 /* Border color in case we don't scale up to the full screen. Black by
5668 * default, change to something else for debugging. */
5669 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005670}
5671
Dave Airlied05410f2014-06-05 13:22:59 +10005672static enum intel_display_power_domain port_to_power_domain(enum port port)
5673{
5674 switch (port) {
5675 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005676 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005677 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005678 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005679 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005680 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005681 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005682 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005683 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005684 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005685 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005686 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005687 return POWER_DOMAIN_PORT_OTHER;
5688 }
5689}
5690
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005691static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5692{
5693 switch (port) {
5694 case PORT_A:
5695 return POWER_DOMAIN_AUX_A;
5696 case PORT_B:
5697 return POWER_DOMAIN_AUX_B;
5698 case PORT_C:
5699 return POWER_DOMAIN_AUX_C;
5700 case PORT_D:
5701 return POWER_DOMAIN_AUX_D;
5702 case PORT_E:
5703 /* FIXME: Check VBT for actual wiring of PORT E */
5704 return POWER_DOMAIN_AUX_D;
5705 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005706 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005707 return POWER_DOMAIN_AUX_A;
5708 }
5709}
5710
Imre Deak319be8a2014-03-04 19:22:57 +02005711enum intel_display_power_domain
5712intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005713{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01005714 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
Imre Deak319be8a2014-03-04 19:22:57 +02005715 struct intel_digital_port *intel_dig_port;
5716
5717 switch (intel_encoder->type) {
5718 case INTEL_OUTPUT_UNKNOWN:
5719 /* Only DDI platforms should ever use this output type */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01005720 WARN_ON_ONCE(!HAS_DDI(dev_priv));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005721 case INTEL_OUTPUT_DP:
Imre Deak319be8a2014-03-04 19:22:57 +02005722 case INTEL_OUTPUT_HDMI:
5723 case INTEL_OUTPUT_EDP:
5724 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005725 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005726 case INTEL_OUTPUT_DP_MST:
5727 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5728 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005729 case INTEL_OUTPUT_ANALOG:
5730 return POWER_DOMAIN_PORT_CRT;
5731 case INTEL_OUTPUT_DSI:
5732 return POWER_DOMAIN_PORT_DSI;
5733 default:
5734 return POWER_DOMAIN_PORT_OTHER;
5735 }
5736}
5737
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005738enum intel_display_power_domain
5739intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5740{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01005741 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005742 struct intel_digital_port *intel_dig_port;
5743
5744 switch (intel_encoder->type) {
5745 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005746 case INTEL_OUTPUT_HDMI:
5747 /*
5748 * Only DDI platforms should ever use these output types.
5749 * We can get here after the HDMI detect code has already set
5750 * the type of the shared encoder. Since we can't be sure
5751 * what's the status of the given connectors, play safe and
5752 * run the DP detection too.
5753 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01005754 WARN_ON_ONCE(!HAS_DDI(dev_priv));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005755 case INTEL_OUTPUT_DP:
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005756 case INTEL_OUTPUT_EDP:
5757 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5758 return port_to_aux_power_domain(intel_dig_port->port);
5759 case INTEL_OUTPUT_DP_MST:
5760 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5761 return port_to_aux_power_domain(intel_dig_port->port);
5762 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005763 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005764 return POWER_DOMAIN_AUX_A;
5765 }
5766}
5767
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005768static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5769 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005770{
5771 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005772 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005773 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5774 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005775 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005776 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005777
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005778 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005779 return 0;
5780
Imre Deak77d22dc2014-03-05 16:20:52 +02005781 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5782 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005783 if (crtc_state->pch_pfit.enabled ||
5784 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005785 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5786
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005787 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5788 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5789
Imre Deak319be8a2014-03-04 19:22:57 +02005790 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005791 }
Imre Deak319be8a2014-03-04 19:22:57 +02005792
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005793 if (crtc_state->shared_dpll)
5794 mask |= BIT(POWER_DOMAIN_PLLS);
5795
Imre Deak77d22dc2014-03-05 16:20:52 +02005796 return mask;
5797}
5798
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005799static unsigned long
5800modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5801 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005802{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005803 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005804 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5805 enum intel_display_power_domain domain;
Daniel Vetter5a21b662016-05-24 17:13:53 +02005806 unsigned long domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005807
5808 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005809 intel_crtc->enabled_power_domains = new_domains =
5810 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005811
Daniel Vetter5a21b662016-05-24 17:13:53 +02005812 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005813
5814 for_each_power_domain(domain, domains)
5815 intel_display_power_get(dev_priv, domain);
5816
Daniel Vetter5a21b662016-05-24 17:13:53 +02005817 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005818}
5819
5820static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5821 unsigned long domains)
5822{
5823 enum intel_display_power_domain domain;
5824
5825 for_each_power_domain(domain, domains)
5826 intel_display_power_put(dev_priv, domain);
5827}
5828
Mika Kaholaadafdc62015-08-18 14:36:59 +03005829static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5830{
5831 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5832
5833 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5834 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5835 return max_cdclk_freq;
5836 else if (IS_CHERRYVIEW(dev_priv))
5837 return max_cdclk_freq*95/100;
5838 else if (INTEL_INFO(dev_priv)->gen < 4)
5839 return 2*max_cdclk_freq*90/100;
5840 else
5841 return max_cdclk_freq*90/100;
5842}
5843
Ville Syrjäläb2045352016-05-13 23:41:27 +03005844static int skl_calc_cdclk(int max_pixclk, int vco);
5845
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005846static void intel_update_max_cdclk(struct drm_device *dev)
5847{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005848 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005849
Tvrtko Ursulin08537232016-10-13 11:03:02 +01005850 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005851 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005852 int max_cdclk, vco;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005853
Ville Syrjäläb2045352016-05-13 23:41:27 +03005854 vco = dev_priv->skl_preferred_vco_freq;
Ville Syrjälä63911d72016-05-13 23:41:32 +03005855 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005856
5857 /*
5858 * Use the lower (vco 8640) cdclk values as a
5859 * first guess. skl_calc_cdclk() will correct it
5860 * if the preferred vco is 8100 instead.
5861 */
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005862 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005863 max_cdclk = 617143;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005864 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005865 max_cdclk = 540000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005866 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005867 max_cdclk = 432000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005868 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005869 max_cdclk = 308571;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005870
5871 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01005872 } else if (IS_BROXTON(dev_priv)) {
Matt Roper281c1142016-04-05 14:37:19 -07005873 dev_priv->max_cdclk_freq = 624000;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01005874 } else if (IS_BROADWELL(dev_priv)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005875 /*
5876 * FIXME with extra cooling we can allow
5877 * 540 MHz for ULX and 675 Mhz for ULT.
5878 * How can we know if extra cooling is
5879 * available? PCI ID, VTB, something else?
5880 */
5881 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5882 dev_priv->max_cdclk_freq = 450000;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005883 else if (IS_BDW_ULX(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005884 dev_priv->max_cdclk_freq = 450000;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005885 else if (IS_BDW_ULT(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005886 dev_priv->max_cdclk_freq = 540000;
5887 else
5888 dev_priv->max_cdclk_freq = 675000;
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01005889 } else if (IS_CHERRYVIEW(dev_priv)) {
Mika Kahola0904dea2015-06-12 10:11:32 +03005890 dev_priv->max_cdclk_freq = 320000;
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01005891 } else if (IS_VALLEYVIEW(dev_priv)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005892 dev_priv->max_cdclk_freq = 400000;
5893 } else {
5894 /* otherwise assume cdclk is fixed */
5895 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5896 }
5897
Mika Kaholaadafdc62015-08-18 14:36:59 +03005898 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5899
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005900 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5901 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005902
5903 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5904 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005905}
5906
5907static void intel_update_cdclk(struct drm_device *dev)
5908{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005909 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005910
5911 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005912
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005913 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005914 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5915 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5916 dev_priv->cdclk_pll.ref);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005917 else
5918 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5919 dev_priv->cdclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005920
5921 /*
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005922 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5923 * Programmng [sic] note: bit[9:2] should be programmed to the number
5924 * of cdclk that generates 4MHz reference clock freq which is used to
5925 * generate GMBus clock. This will vary with the cdclk freq.
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005926 */
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005927 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005928 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005929}
5930
Ville Syrjälä92891e42016-05-11 22:44:45 +03005931/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5932static int skl_cdclk_decimal(int cdclk)
5933{
5934 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5935}
5936
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005937static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5938{
5939 int ratio;
5940
5941 if (cdclk == dev_priv->cdclk_pll.ref)
5942 return 0;
5943
5944 switch (cdclk) {
5945 default:
5946 MISSING_CASE(cdclk);
5947 case 144000:
5948 case 288000:
5949 case 384000:
5950 case 576000:
5951 ratio = 60;
5952 break;
5953 case 624000:
5954 ratio = 65;
5955 break;
5956 }
5957
5958 return dev_priv->cdclk_pll.ref * ratio;
5959}
5960
Ville Syrjälä2b730012016-05-13 23:41:34 +03005961static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5962{
5963 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5964
5965 /* Timeout 200us */
Chris Wilson95cac282016-06-30 15:33:03 +01005966 if (intel_wait_for_register(dev_priv,
5967 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5968 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005969 DRM_ERROR("timeout waiting for DE PLL unlock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005970
5971 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005972}
5973
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005974static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005975{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005976 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005977 u32 val;
5978
5979 val = I915_READ(BXT_DE_PLL_CTL);
5980 val &= ~BXT_DE_PLL_RATIO_MASK;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005981 val |= BXT_DE_PLL_RATIO(ratio);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005982 I915_WRITE(BXT_DE_PLL_CTL, val);
5983
5984 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5985
5986 /* Timeout 200us */
Chris Wilsone084e1b2016-06-30 15:33:04 +01005987 if (intel_wait_for_register(dev_priv,
5988 BXT_DE_PLL_ENABLE,
5989 BXT_DE_PLL_LOCK,
5990 BXT_DE_PLL_LOCK,
5991 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005992 DRM_ERROR("timeout waiting for DE PLL lock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005993
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005994 dev_priv->cdclk_pll.vco = vco;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005995}
5996
Imre Deak324513c2016-06-13 16:44:36 +03005997static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305998{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005999 u32 val, divider;
6000 int vco, ret;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306001
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006002 vco = bxt_de_pll_vco(dev_priv, cdclk);
6003
6004 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
6005
6006 /* cdclk = vco / 2 / div{1,1.5,2,4} */
6007 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
6008 case 8:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306009 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306010 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006011 case 4:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306012 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306013 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006014 case 3:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306015 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306016 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006017 case 2:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306018 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306019 break;
6020 default:
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006021 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
6022 WARN_ON(vco != 0);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306023
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006024 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
6025 break;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306026 }
6027
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306028 /* Inform power controller of upcoming frequency change */
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006029 mutex_lock(&dev_priv->rps.hw_lock);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306030 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
6031 0x80000000);
6032 mutex_unlock(&dev_priv->rps.hw_lock);
6033
6034 if (ret) {
6035 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006036 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306037 return;
6038 }
6039
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006040 if (dev_priv->cdclk_pll.vco != 0 &&
6041 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03006042 bxt_de_pll_disable(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306043
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006044 if (dev_priv->cdclk_pll.vco != vco)
6045 bxt_de_pll_enable(dev_priv, vco);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306046
Ville Syrjälä5f199df2016-05-13 23:41:38 +03006047 val = divider | skl_cdclk_decimal(cdclk);
6048 /*
6049 * FIXME if only the cd2x divider needs changing, it could be done
6050 * without shutting off the pipe (if only one pipe is active).
6051 */
6052 val |= BXT_CDCLK_CD2X_PIPE_NONE;
6053 /*
6054 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6055 * enable otherwise.
6056 */
6057 if (cdclk >= 500000)
6058 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6059 I915_WRITE(CDCLK_CTL, val);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306060
6061 mutex_lock(&dev_priv->rps.hw_lock);
6062 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006063 DIV_ROUND_UP(cdclk, 25000));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306064 mutex_unlock(&dev_priv->rps.hw_lock);
6065
6066 if (ret) {
6067 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006068 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306069 return;
6070 }
6071
Chris Wilson91c8a322016-07-05 10:40:23 +01006072 intel_update_cdclk(&dev_priv->drm);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306073}
6074
Imre Deakd66a2192016-05-24 15:38:33 +03006075static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306076{
Imre Deakd66a2192016-05-24 15:38:33 +03006077 u32 cdctl, expected;
6078
Chris Wilson91c8a322016-07-05 10:40:23 +01006079 intel_update_cdclk(&dev_priv->drm);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306080
Imre Deakd66a2192016-05-24 15:38:33 +03006081 if (dev_priv->cdclk_pll.vco == 0 ||
6082 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
6083 goto sanitize;
6084
6085 /* DPLL okay; verify the cdclock
6086 *
6087 * Some BIOS versions leave an incorrect decimal frequency value and
6088 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
6089 * so sanitize this register.
6090 */
6091 cdctl = I915_READ(CDCLK_CTL);
6092 /*
6093 * Let's ignore the pipe field, since BIOS could have configured the
6094 * dividers both synching to an active pipe, or asynchronously
6095 * (PIPE_NONE).
6096 */
6097 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
6098
6099 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
6100 skl_cdclk_decimal(dev_priv->cdclk_freq);
6101 /*
6102 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6103 * enable otherwise.
6104 */
6105 if (dev_priv->cdclk_freq >= 500000)
6106 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6107
6108 if (cdctl == expected)
6109 /* All well; nothing to sanitize */
6110 return;
6111
6112sanitize:
6113 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
6114
6115 /* force cdclk programming */
6116 dev_priv->cdclk_freq = 0;
6117
6118 /* force full PLL disable + enable */
6119 dev_priv->cdclk_pll.vco = -1;
6120}
6121
Imre Deak324513c2016-06-13 16:44:36 +03006122void bxt_init_cdclk(struct drm_i915_private *dev_priv)
Imre Deakd66a2192016-05-24 15:38:33 +03006123{
6124 bxt_sanitize_cdclk(dev_priv);
6125
6126 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03006127 return;
Imre Deakc2e001e2016-04-01 16:02:43 +03006128
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306129 /*
6130 * FIXME:
6131 * - The initial CDCLK needs to be read from VBT.
6132 * Need to make this change after VBT has changes for BXT.
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306133 */
Imre Deak324513c2016-06-13 16:44:36 +03006134 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306135}
6136
Imre Deak324513c2016-06-13 16:44:36 +03006137void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306138{
Imre Deak324513c2016-06-13 16:44:36 +03006139 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306140}
6141
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006142static int skl_calc_cdclk(int max_pixclk, int vco)
6143{
Ville Syrjälä63911d72016-05-13 23:41:32 +03006144 if (vco == 8640000) {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006145 if (max_pixclk > 540000)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006146 return 617143;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006147 else if (max_pixclk > 432000)
6148 return 540000;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006149 else if (max_pixclk > 308571)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006150 return 432000;
6151 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006152 return 308571;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006153 } else {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006154 if (max_pixclk > 540000)
6155 return 675000;
6156 else if (max_pixclk > 450000)
6157 return 540000;
6158 else if (max_pixclk > 337500)
6159 return 450000;
6160 else
6161 return 337500;
6162 }
6163}
6164
Ville Syrjäläea617912016-05-13 23:41:24 +03006165static void
6166skl_dpll0_update(struct drm_i915_private *dev_priv)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006167{
Ville Syrjäläea617912016-05-13 23:41:24 +03006168 u32 val;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006169
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006170 dev_priv->cdclk_pll.ref = 24000;
Imre Deak1c3f7702016-05-24 15:38:32 +03006171 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006172
Ville Syrjäläea617912016-05-13 23:41:24 +03006173 val = I915_READ(LCPLL1_CTL);
Imre Deak1c3f7702016-05-24 15:38:32 +03006174 if ((val & LCPLL_PLL_ENABLE) == 0)
Ville Syrjäläea617912016-05-13 23:41:24 +03006175 return;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006176
Imre Deak1c3f7702016-05-24 15:38:32 +03006177 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
6178 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006179
Ville Syrjäläea617912016-05-13 23:41:24 +03006180 val = I915_READ(DPLL_CTRL1);
6181
Imre Deak1c3f7702016-05-24 15:38:32 +03006182 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
6183 DPLL_CTRL1_SSC(SKL_DPLL0) |
6184 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
6185 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
6186 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006187
Ville Syrjäläea617912016-05-13 23:41:24 +03006188 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
6189 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
6190 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
6191 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
6192 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03006193 dev_priv->cdclk_pll.vco = 8100000;
Ville Syrjäläea617912016-05-13 23:41:24 +03006194 break;
6195 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
6196 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03006197 dev_priv->cdclk_pll.vco = 8640000;
Ville Syrjäläea617912016-05-13 23:41:24 +03006198 break;
6199 default:
6200 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
Ville Syrjäläea617912016-05-13 23:41:24 +03006201 break;
6202 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006203}
6204
Ville Syrjäläb2045352016-05-13 23:41:27 +03006205void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
6206{
6207 bool changed = dev_priv->skl_preferred_vco_freq != vco;
6208
6209 dev_priv->skl_preferred_vco_freq = vco;
6210
6211 if (changed)
Chris Wilson91c8a322016-07-05 10:40:23 +01006212 intel_update_max_cdclk(&dev_priv->drm);
Ville Syrjäläb2045352016-05-13 23:41:27 +03006213}
6214
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006215static void
Ville Syrjälä3861fc62016-05-11 22:44:50 +03006216skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006217{
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006218 int min_cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006219 u32 val;
6220
Ville Syrjälä63911d72016-05-13 23:41:32 +03006221 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03006222
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006223 /* select the minimum CDCLK before enabling DPLL 0 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006224 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006225 I915_WRITE(CDCLK_CTL, val);
6226 POSTING_READ(CDCLK_CTL);
6227
6228 /*
6229 * We always enable DPLL0 with the lowest link rate possible, but still
6230 * taking into account the VCO required to operate the eDP panel at the
6231 * desired frequency. The usual DP link rates operate with a VCO of
6232 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
6233 * The modeset code is responsible for the selection of the exact link
6234 * rate later on, with the constraint of choosing a frequency that
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03006235 * works with vco.
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006236 */
6237 val = I915_READ(DPLL_CTRL1);
6238
6239 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
6240 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
6241 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Ville Syrjälä63911d72016-05-13 23:41:32 +03006242 if (vco == 8640000)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006243 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
6244 SKL_DPLL0);
6245 else
6246 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
6247 SKL_DPLL0);
6248
6249 I915_WRITE(DPLL_CTRL1, val);
6250 POSTING_READ(DPLL_CTRL1);
6251
6252 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
6253
Chris Wilsone24ca052016-06-30 15:33:05 +01006254 if (intel_wait_for_register(dev_priv,
6255 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
6256 5))
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006257 DRM_ERROR("DPLL0 not locked\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006258
Ville Syrjälä63911d72016-05-13 23:41:32 +03006259 dev_priv->cdclk_pll.vco = vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +03006260
6261 /* We'll want to keep using the current vco from now on. */
6262 skl_set_preferred_cdclk_vco(dev_priv, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006263}
6264
Ville Syrjälä430e05d2016-05-11 22:44:47 +03006265static void
6266skl_dpll0_disable(struct drm_i915_private *dev_priv)
6267{
6268 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
Chris Wilson8ad32a02016-06-30 15:33:06 +01006269 if (intel_wait_for_register(dev_priv,
6270 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
6271 1))
Ville Syrjälä430e05d2016-05-11 22:44:47 +03006272 DRM_ERROR("Couldn't disable DPLL0\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006273
Ville Syrjälä63911d72016-05-13 23:41:32 +03006274 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä430e05d2016-05-11 22:44:47 +03006275}
6276
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006277static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
6278{
6279 int ret;
6280 u32 val;
6281
6282 /* inform PCU we want to change CDCLK */
6283 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
6284 mutex_lock(&dev_priv->rps.hw_lock);
6285 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
6286 mutex_unlock(&dev_priv->rps.hw_lock);
6287
6288 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
6289}
6290
6291static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
6292{
Ville Syrjälä848496e2016-07-13 16:32:03 +03006293 return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006294}
6295
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006296static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006297{
Chris Wilson91c8a322016-07-05 10:40:23 +01006298 struct drm_device *dev = &dev_priv->drm;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006299 u32 freq_select, pcu_ack;
6300
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006301 WARN_ON((cdclk == 24000) != (vco == 0));
6302
Ville Syrjälä63911d72016-05-13 23:41:32 +03006303 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006304
6305 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
6306 DRM_ERROR("failed to inform PCU about cdclk change\n");
6307 return;
6308 }
6309
6310 /* set CDCLK_CTL */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006311 switch (cdclk) {
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006312 case 450000:
6313 case 432000:
6314 freq_select = CDCLK_FREQ_450_432;
6315 pcu_ack = 1;
6316 break;
6317 case 540000:
6318 freq_select = CDCLK_FREQ_540;
6319 pcu_ack = 2;
6320 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006321 case 308571:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006322 case 337500:
6323 default:
6324 freq_select = CDCLK_FREQ_337_308;
6325 pcu_ack = 0;
6326 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006327 case 617143:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006328 case 675000:
6329 freq_select = CDCLK_FREQ_675_617;
6330 pcu_ack = 3;
6331 break;
6332 }
6333
Ville Syrjälä63911d72016-05-13 23:41:32 +03006334 if (dev_priv->cdclk_pll.vco != 0 &&
6335 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006336 skl_dpll0_disable(dev_priv);
6337
Ville Syrjälä63911d72016-05-13 23:41:32 +03006338 if (dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006339 skl_dpll0_enable(dev_priv, vco);
6340
Ville Syrjälä9ef56152016-05-11 22:44:49 +03006341 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006342 POSTING_READ(CDCLK_CTL);
6343
6344 /* inform PCU of the change */
6345 mutex_lock(&dev_priv->rps.hw_lock);
6346 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
6347 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01006348
6349 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006350}
6351
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006352static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
6353
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006354void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
6355{
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006356 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006357}
6358
6359void skl_init_cdclk(struct drm_i915_private *dev_priv)
6360{
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006361 int cdclk, vco;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006362
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006363 skl_sanitize_cdclk(dev_priv);
6364
Ville Syrjälä63911d72016-05-13 23:41:32 +03006365 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006366 /*
6367 * Use the current vco as our initial
6368 * guess as to what the preferred vco is.
6369 */
6370 if (dev_priv->skl_preferred_vco_freq == 0)
6371 skl_set_preferred_cdclk_vco(dev_priv,
Ville Syrjälä63911d72016-05-13 23:41:32 +03006372 dev_priv->cdclk_pll.vco);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006373 return;
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03006374 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006375
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006376 vco = dev_priv->skl_preferred_vco_freq;
6377 if (vco == 0)
Ville Syrjälä63911d72016-05-13 23:41:32 +03006378 vco = 8100000;
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006379 cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006380
Ville Syrjälä70c2c182016-05-13 23:41:30 +03006381 skl_set_cdclk(dev_priv, cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01006382}
6383
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006384static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306385{
Ville Syrjälä09492492016-05-13 23:41:28 +03006386 uint32_t cdctl, expected;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306387
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05306388 /*
6389 * check if the pre-os intialized the display
6390 * There is SWF18 scratchpad register defined which is set by the
6391 * pre-os which can be used by the OS drivers to check the status
6392 */
6393 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
6394 goto sanitize;
6395
Chris Wilson91c8a322016-07-05 10:40:23 +01006396 intel_update_cdclk(&dev_priv->drm);
Imre Deak1c3f7702016-05-24 15:38:32 +03006397 /* Is PLL enabled and locked ? */
6398 if (dev_priv->cdclk_pll.vco == 0 ||
6399 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
6400 goto sanitize;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006401
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306402 /* DPLL okay; verify the cdclock
6403 *
6404 * Noticed in some instances that the freq selection is correct but
6405 * decimal part is programmed wrong from BIOS where pre-os does not
6406 * enable display. Verify the same as well.
6407 */
Ville Syrjälä09492492016-05-13 23:41:28 +03006408 cdctl = I915_READ(CDCLK_CTL);
6409 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
6410 skl_cdclk_decimal(dev_priv->cdclk_freq);
6411 if (cdctl == expected)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306412 /* All well; nothing to sanitize */
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006413 return;
6414
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306415sanitize:
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006416 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
Clint Taylorc89e39f2016-05-13 23:41:21 +03006417
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03006418 /* force cdclk programming */
6419 dev_priv->cdclk_freq = 0;
6420 /* force full PLL disable + enable */
Ville Syrjälä63911d72016-05-13 23:41:32 +03006421 dev_priv->cdclk_pll.vco = -1;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05306422}
6423
Jesse Barnes30a970c2013-11-04 13:48:12 -08006424/* Adjust CDclk dividers to allow high res or save power if possible */
6425static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
6426{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006427 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006428 u32 val, cmd;
6429
Vandana Kannan164dfd22014-11-24 13:37:41 +05306430 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
6431 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02006432
Ville Syrjälädfcab172014-06-13 13:37:47 +03006433 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08006434 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03006435 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006436 cmd = 1;
6437 else
6438 cmd = 0;
6439
6440 mutex_lock(&dev_priv->rps.hw_lock);
6441 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6442 val &= ~DSPFREQGUAR_MASK;
6443 val |= (cmd << DSPFREQGUAR_SHIFT);
6444 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6445 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6446 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
6447 50)) {
6448 DRM_ERROR("timed out waiting for CDclk change\n");
6449 }
6450 mutex_unlock(&dev_priv->rps.hw_lock);
6451
Ville Syrjälä54433e92015-05-26 20:42:31 +03006452 mutex_lock(&dev_priv->sb_lock);
6453
Ville Syrjälädfcab172014-06-13 13:37:47 +03006454 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006455 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006456
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006457 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006458
Jesse Barnes30a970c2013-11-04 13:48:12 -08006459 /* adjust cdclk divider */
6460 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03006461 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006462 val |= divider;
6463 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03006464
6465 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03006466 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03006467 50))
6468 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08006469 }
6470
Jesse Barnes30a970c2013-11-04 13:48:12 -08006471 /* adjust self-refresh exit latency value */
6472 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
6473 val &= ~0x7f;
6474
6475 /*
6476 * For high bandwidth configs, we set a higher latency in the bunit
6477 * so that the core display fetch happens in time to avoid underruns.
6478 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03006479 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006480 val |= 4500 / 250; /* 4.5 usec */
6481 else
6482 val |= 3000 / 250; /* 3.0 usec */
6483 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03006484
Ville Syrjäläa5805162015-05-26 20:42:30 +03006485 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006486
Ville Syrjäläb6283052015-06-03 15:45:07 +03006487 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006488}
6489
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006490static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
6491{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006492 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006493 u32 val, cmd;
6494
Vandana Kannan164dfd22014-11-24 13:37:41 +05306495 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
6496 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006497
6498 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006499 case 333333:
6500 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006501 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006502 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006503 break;
6504 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01006505 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006506 return;
6507 }
6508
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02006509 /*
6510 * Specs are full of misinformation, but testing on actual
6511 * hardware has shown that we just need to write the desired
6512 * CCK divider into the Punit register.
6513 */
6514 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
6515
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006516 mutex_lock(&dev_priv->rps.hw_lock);
6517 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6518 val &= ~DSPFREQGUAR_MASK_CHV;
6519 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
6520 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6521 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6522 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
6523 50)) {
6524 DRM_ERROR("timed out waiting for CDclk change\n");
6525 }
6526 mutex_unlock(&dev_priv->rps.hw_lock);
6527
Ville Syrjäläb6283052015-06-03 15:45:07 +03006528 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006529}
6530
Jesse Barnes30a970c2013-11-04 13:48:12 -08006531static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6532 int max_pixclk)
6533{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006534 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006535 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006536
Jesse Barnes30a970c2013-11-04 13:48:12 -08006537 /*
6538 * Really only a few cases to deal with, as only 4 CDclks are supported:
6539 * 200MHz
6540 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006541 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006542 * 400MHz (VLV only)
6543 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6544 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006545 *
6546 * We seem to get an unstable or solid color picture at 200MHz.
6547 * Not sure what's wrong. For now use 200MHz only when all pipes
6548 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08006549 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006550 if (!IS_CHERRYVIEW(dev_priv) &&
6551 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006552 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02006553 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03006554 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006555 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03006556 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03006557 else
6558 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006559}
6560
Imre Deak324513c2016-06-13 16:44:36 +03006561static int bxt_calc_cdclk(int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006562{
Ville Syrjälä760e1472016-05-11 22:44:46 +03006563 if (max_pixclk > 576000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306564 return 624000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006565 else if (max_pixclk > 384000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306566 return 576000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006567 else if (max_pixclk > 288000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306568 return 384000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03006569 else if (max_pixclk > 144000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306570 return 288000;
6571 else
6572 return 144000;
6573}
6574
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01006575/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006576static int intel_mode_max_pixclk(struct drm_device *dev,
6577 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006578{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006579 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +01006580 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006581 struct drm_crtc *crtc;
6582 struct drm_crtc_state *crtc_state;
6583 unsigned max_pixclk = 0, i;
6584 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006585
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006586 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6587 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006588
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006589 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6590 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006591
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006592 if (crtc_state->enable)
6593 pixclk = crtc_state->adjusted_mode.crtc_clock;
6594
6595 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006596 }
6597
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006598 for_each_pipe(dev_priv, pipe)
6599 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6600
Jesse Barnes30a970c2013-11-04 13:48:12 -08006601 return max_pixclk;
6602}
6603
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006604static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006605{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006606 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006607 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006608 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006609 struct intel_atomic_state *intel_state =
6610 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006611
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006612 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006613 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306614
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006615 if (!intel_state->active_crtcs)
6616 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6617
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006618 return 0;
6619}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006620
Imre Deak324513c2016-06-13 16:44:36 +03006621static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006622{
Ville Syrjälä4e5ca602016-05-11 22:44:44 +03006623 int max_pixclk = ilk_max_pixel_rate(state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006624 struct intel_atomic_state *intel_state =
6625 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006626
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006627 intel_state->cdclk = intel_state->dev_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +03006628 bxt_calc_cdclk(max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006629
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006630 if (!intel_state->active_crtcs)
Imre Deak324513c2016-06-13 16:44:36 +03006631 intel_state->dev_cdclk = bxt_calc_cdclk(0);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006632
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006633 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006634}
6635
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006636static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6637{
6638 unsigned int credits, default_credits;
6639
6640 if (IS_CHERRYVIEW(dev_priv))
6641 default_credits = PFI_CREDIT(12);
6642 else
6643 default_credits = PFI_CREDIT(8);
6644
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006645 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006646 /* CHV suggested value is 31 or 63 */
6647 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006648 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006649 else
6650 credits = PFI_CREDIT(15);
6651 } else {
6652 credits = default_credits;
6653 }
6654
6655 /*
6656 * WA - write default credits before re-programming
6657 * FIXME: should we also set the resend bit here?
6658 */
6659 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6660 default_credits);
6661
6662 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6663 credits | PFI_CREDIT_RESEND);
6664
6665 /*
6666 * FIXME is this guaranteed to clear
6667 * immediately or should we poll for it?
6668 */
6669 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6670}
6671
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006672static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006673{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006674 struct drm_device *dev = old_state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006675 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006676 struct intel_atomic_state *old_intel_state =
6677 to_intel_atomic_state(old_state);
6678 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006679
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006680 /*
6681 * FIXME: We can end up here with all power domains off, yet
6682 * with a CDCLK frequency other than the minimum. To account
6683 * for this take the PIPE-A power domain, which covers the HW
6684 * blocks needed for the following programming. This can be
6685 * removed once it's guaranteed that we get here either with
6686 * the minimum CDCLK set, or the required power domains
6687 * enabled.
6688 */
6689 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006690
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006691 if (IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006692 cherryview_set_cdclk(dev, req_cdclk);
6693 else
6694 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006695
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006696 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006697
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006698 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006699}
6700
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006701static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
6702 struct drm_atomic_state *old_state)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006703{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006704 struct drm_crtc *crtc = pipe_config->base.crtc;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006705 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006706 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006707 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006708 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006709
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006710 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006711 return;
6712
Ville Syrjälä37a56502016-06-22 21:57:04 +03006713 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306714 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006715
6716 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006717 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006718
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006719 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
Chris Wilsonfac5e232016-07-04 11:34:36 +01006720 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006721
6722 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6723 I915_WRITE(CHV_CANVAS(pipe), 0);
6724 }
6725
Daniel Vetter5b18e572014-04-24 23:55:06 +02006726 i9xx_set_pipeconf(intel_crtc);
6727
Jesse Barnes89b667f2013-04-18 14:51:36 -07006728 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006729
Daniel Vettera72e4c92014-09-30 10:56:47 +02006730 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006731
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006732 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006733
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006734 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006735 chv_prepare_pll(intel_crtc, intel_crtc->config);
6736 chv_enable_pll(intel_crtc, intel_crtc->config);
6737 } else {
6738 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6739 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006740 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006741
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006742 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006743
Jesse Barnes2dd24552013-04-25 12:55:01 -07006744 i9xx_pfit_enable(intel_crtc);
6745
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006746 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006747
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006748 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006749 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006750
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006751 assert_vblank_disabled(crtc);
6752 drm_crtc_vblank_on(crtc);
6753
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006754 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006755}
6756
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006757static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6758{
6759 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006760 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006761
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006762 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6763 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006764}
6765
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006766static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
6767 struct drm_atomic_state *old_state)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006768{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006769 struct drm_crtc *crtc = pipe_config->base.crtc;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006770 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006771 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006773 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006774
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006775 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006776 return;
6777
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006778 i9xx_set_pll_dividers(intel_crtc);
6779
Ville Syrjälä37a56502016-06-22 21:57:04 +03006780 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306781 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006782
6783 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006784 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006785
Daniel Vetter5b18e572014-04-24 23:55:06 +02006786 i9xx_set_pipeconf(intel_crtc);
6787
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006788 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006789
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006790 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006791 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006792
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006793 intel_encoders_pre_enable(crtc, pipe_config, old_state);
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006794
Daniel Vetterf6736a12013-06-05 13:34:30 +02006795 i9xx_enable_pll(intel_crtc);
6796
Jesse Barnes2dd24552013-04-25 12:55:01 -07006797 i9xx_pfit_enable(intel_crtc);
6798
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006799 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006800
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006801 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006802 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006803
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006804 assert_vblank_disabled(crtc);
6805 drm_crtc_vblank_on(crtc);
6806
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006807 intel_encoders_enable(crtc, pipe_config, old_state);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006808}
6809
Daniel Vetter87476d62013-04-11 16:29:06 +02006810static void i9xx_pfit_disable(struct intel_crtc *crtc)
6811{
6812 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006813 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter328d8e82013-05-08 10:36:31 +02006814
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006815 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006816 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006817
6818 assert_pipe_disabled(dev_priv, crtc->pipe);
6819
Daniel Vetter328d8e82013-05-08 10:36:31 +02006820 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6821 I915_READ(PFIT_CONTROL));
6822 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006823}
6824
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006825static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
6826 struct drm_atomic_state *old_state)
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006827{
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006828 struct drm_crtc *crtc = old_crtc_state->base.crtc;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006829 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006830 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006831 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6832 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006833
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006834 /*
6835 * On gen2 planes are double buffered but the pipe isn't, so we must
6836 * wait for planes to fully turn off before disabling the pipe.
6837 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006838 if (IS_GEN2(dev_priv))
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006839 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006840
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006841 intel_encoders_disable(crtc, old_crtc_state, old_state);
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006842
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006843 drm_crtc_vblank_off(crtc);
6844 assert_vblank_disabled(crtc);
6845
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006846 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006847
Daniel Vetter87476d62013-04-11 16:29:06 +02006848 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006849
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006850 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006851
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006852 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01006853 if (IS_CHERRYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006854 chv_disable_pll(dev_priv, pipe);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01006855 else if (IS_VALLEYVIEW(dev_priv))
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006856 vlv_disable_pll(dev_priv, pipe);
6857 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006858 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006859 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006860
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02006861 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006862
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01006863 if (!IS_GEN2(dev_priv))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006864 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006865}
6866
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006867static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006868{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006869 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006870 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006871 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006872 enum intel_display_power_domain domain;
6873 unsigned long domains;
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006874 struct drm_atomic_state *state;
6875 struct intel_crtc_state *crtc_state;
6876 int ret;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006877
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006878 if (!intel_crtc->active)
6879 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006880
Ville Syrjälä936e71e2016-07-26 19:06:59 +03006881 if (to_intel_plane_state(crtc->primary->state)->base.visible) {
Daniel Vetter5a21b662016-05-24 17:13:53 +02006882 WARN_ON(intel_crtc->flip_work);
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006883
Ville Syrjälä2622a082016-03-09 19:07:26 +02006884 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006885
6886 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
Ville Syrjälä936e71e2016-07-26 19:06:59 +03006887 to_intel_plane_state(crtc->primary->state)->base.visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006888 }
6889
Maarten Lankhorst4a806552016-08-09 17:04:01 +02006890 state = drm_atomic_state_alloc(crtc->dev);
6891 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
6892
6893 /* Everything's already locked, -EDEADLK can't happen. */
6894 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
6895 ret = drm_atomic_add_affected_connectors(state, crtc);
6896
6897 WARN_ON(IS_ERR(crtc_state) || ret);
6898
6899 dev_priv->display.crtc_disable(crtc_state, state);
6900
Chris Wilson08536952016-10-14 13:18:18 +01006901 drm_atomic_state_put(state);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006902
Ville Syrjälä78108b72016-05-27 20:59:19 +03006903 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6904 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006905
6906 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6907 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006908 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006909 crtc->enabled = false;
6910 crtc->state->connector_mask = 0;
6911 crtc->state->encoder_mask = 0;
6912
6913 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6914 encoder->base.crtc = NULL;
6915
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006916 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006917 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006918 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006919
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006920 domains = intel_crtc->enabled_power_domains;
6921 for_each_power_domain(domain, domains)
6922 intel_display_power_put(dev_priv, domain);
6923 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006924
6925 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6926 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006927}
6928
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006929/*
6930 * turn all crtc's off, but do not adjust state
6931 * This has to be paired with a call to intel_modeset_setup_hw_state.
6932 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006933int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006934{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006935 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006936 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006937 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006938
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006939 state = drm_atomic_helper_suspend(dev);
6940 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006941 if (ret)
6942 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006943 else
6944 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006945 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006946}
6947
Chris Wilsonea5b2132010-08-04 13:50:23 +01006948void intel_encoder_destroy(struct drm_encoder *encoder)
6949{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006950 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006951
Chris Wilsonea5b2132010-08-04 13:50:23 +01006952 drm_encoder_cleanup(encoder);
6953 kfree(intel_encoder);
6954}
6955
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006956/* Cross check the actual hw state with our own modeset state tracking (and it's
6957 * internal consistency). */
Daniel Vetter5a21b662016-05-24 17:13:53 +02006958static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006959{
Daniel Vetter5a21b662016-05-24 17:13:53 +02006960 struct drm_crtc *crtc = connector->base.state->crtc;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006961
6962 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6963 connector->base.base.id,
6964 connector->base.name);
6965
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006966 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006967 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter5a21b662016-05-24 17:13:53 +02006968 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006969
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006970 I915_STATE_WARN(!crtc,
6971 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006972
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006973 if (!crtc)
6974 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006975
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006976 I915_STATE_WARN(!crtc->state->active,
6977 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006978
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006979 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006980 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006981
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006982 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006983 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006984
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006985 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006986 "attached encoder crtc differs from connector crtc\n");
6987 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006988 I915_STATE_WARN(crtc && crtc->state->active,
6989 "attached crtc is active, but connector isn't\n");
Daniel Vetter5a21b662016-05-24 17:13:53 +02006990 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006991 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006992 }
6993}
6994
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006995int intel_connector_init(struct intel_connector *connector)
6996{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006997 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006998
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006999 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03007000 return -ENOMEM;
7001
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03007002 return 0;
7003}
7004
7005struct intel_connector *intel_connector_alloc(void)
7006{
7007 struct intel_connector *connector;
7008
7009 connector = kzalloc(sizeof *connector, GFP_KERNEL);
7010 if (!connector)
7011 return NULL;
7012
7013 if (intel_connector_init(connector) < 0) {
7014 kfree(connector);
7015 return NULL;
7016 }
7017
7018 return connector;
7019}
7020
Daniel Vetterf0947c32012-07-02 13:10:34 +02007021/* Simple connector->get_hw_state implementation for encoders that support only
7022 * one connector and no cloning and hence the encoder state determines the state
7023 * of the connector. */
7024bool intel_connector_get_hw_state(struct intel_connector *connector)
7025{
Daniel Vetter24929352012-07-02 20:28:59 +02007026 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02007027 struct intel_encoder *encoder = connector->encoder;
7028
7029 return encoder->get_hw_state(encoder, &pipe);
7030}
7031
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007032static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02007033{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007034 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
7035 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02007036
7037 return 0;
7038}
7039
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007040static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007041 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007042{
Tvrtko Ursulin86527442016-10-13 11:03:00 +01007043 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007044 struct drm_atomic_state *state = pipe_config->base.state;
7045 struct intel_crtc *other_crtc;
7046 struct intel_crtc_state *other_crtc_state;
7047
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007048 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
7049 pipe_name(pipe), pipe_config->fdi_lanes);
7050 if (pipe_config->fdi_lanes > 4) {
7051 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
7052 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007053 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007054 }
7055
Tvrtko Ursulin86527442016-10-13 11:03:00 +01007056 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007057 if (pipe_config->fdi_lanes > 2) {
7058 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
7059 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007060 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007061 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007062 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007063 }
7064 }
7065
7066 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007067 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007068
7069 /* Ivybridge 3 pipe is really complicated */
7070 switch (pipe) {
7071 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007072 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007073 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007074 if (pipe_config->fdi_lanes <= 2)
7075 return 0;
7076
7077 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
7078 other_crtc_state =
7079 intel_atomic_get_crtc_state(state, other_crtc);
7080 if (IS_ERR(other_crtc_state))
7081 return PTR_ERR(other_crtc_state);
7082
7083 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007084 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
7085 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007086 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007087 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007088 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007089 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02007090 if (pipe_config->fdi_lanes > 2) {
7091 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
7092 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007093 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02007094 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007095
7096 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
7097 other_crtc_state =
7098 intel_atomic_get_crtc_state(state, other_crtc);
7099 if (IS_ERR(other_crtc_state))
7100 return PTR_ERR(other_crtc_state);
7101
7102 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007103 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007104 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007105 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007106 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007107 default:
7108 BUG();
7109 }
7110}
7111
Daniel Vettere29c22c2013-02-21 00:00:16 +01007112#define RETRY 1
7113static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007114 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02007115{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007116 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007117 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007118 int lane, link_bw, fdi_dotclock, ret;
7119 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02007120
Daniel Vettere29c22c2013-02-21 00:00:16 +01007121retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02007122 /* FDI is a binary signal running at ~2.7GHz, encoding
7123 * each output octet as 10 bits. The actual frequency
7124 * is stored as a divider into a 100MHz clock, and the
7125 * mode pixel clock is stored in units of 1KHz.
7126 * Hence the bw of each lane in terms of the mode signal
7127 * is:
7128 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02007129 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02007130
Damien Lespiau241bfc32013-09-25 16:45:37 +01007131 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02007132
Daniel Vetter2bd89a02013-06-01 17:16:19 +02007133 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02007134 pipe_config->pipe_bpp);
7135
7136 pipe_config->fdi_lanes = lane;
7137
Daniel Vetter2bd89a02013-06-01 17:16:19 +02007138 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02007139 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02007140
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02007141 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007142 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01007143 pipe_config->pipe_bpp -= 2*3;
7144 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
7145 pipe_config->pipe_bpp);
7146 needs_recompute = true;
7147 pipe_config->bw_constrained = true;
7148
7149 goto retry;
7150 }
7151
7152 if (needs_recompute)
7153 return RETRY;
7154
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03007155 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02007156}
7157
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007158static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
7159 struct intel_crtc_state *pipe_config)
7160{
7161 if (pipe_config->pipe_bpp > 24)
7162 return false;
7163
7164 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007165 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007166 return true;
7167
7168 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03007169 * We compare against max which means we must take
7170 * the increased cdclk requirement into account when
7171 * calculating the new cdclk.
7172 *
7173 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007174 */
7175 return ilk_pipe_pixel_rate(pipe_config) <=
7176 dev_priv->max_cdclk_freq * 95 / 100;
7177}
7178
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007179static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007180 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007181{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007182 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007183 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007184
Jani Nikulad330a952014-01-21 11:24:25 +02007185 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03007186 hsw_crtc_supports_ips(crtc) &&
7187 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007188}
7189
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02007190static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
7191{
7192 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7193
7194 /* GDG double wide on either pipe, otherwise pipe A only */
7195 return INTEL_INFO(dev_priv)->gen < 4 &&
7196 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
7197}
7198
Daniel Vettera43f6e02013-06-07 23:10:32 +02007199static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007200 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08007201{
Daniel Vettera43f6e02013-06-07 23:10:32 +02007202 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007203 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007204 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03007205 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01007206
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007207 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03007208 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007209
7210 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02007211 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007212 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007213 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02007214 if (intel_crtc_supports_double_wide(crtc) &&
7215 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03007216 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007217 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03007218 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03007219 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03007220
Ville Syrjäläf3261152016-05-24 21:34:18 +03007221 if (adjusted_mode->crtc_clock > clock_limit) {
7222 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
7223 adjusted_mode->crtc_clock, clock_limit,
7224 yesno(pipe_config->double_wide));
7225 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08007226 }
Chris Wilson89749352010-09-12 18:25:19 +01007227
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03007228 /*
7229 * Pipe horizontal size must be even in:
7230 * - DVO ganged mode
7231 * - LVDS dual channel mode
7232 * - Double wide pipe
7233 */
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007234 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03007235 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
7236 pipe_config->pipe_src_w &= ~1;
7237
Damien Lespiau8693a822013-05-03 18:48:11 +01007238 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
7239 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03007240 */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007241 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03007242 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01007243 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03007244
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007245 if (HAS_IPS(dev_priv))
Daniel Vettera43f6e02013-06-07 23:10:32 +02007246 hsw_compute_ips_config(crtc, pipe_config);
7247
Daniel Vetter877d48d2013-04-19 11:24:43 +02007248 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02007249 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02007250
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02007251 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007252}
7253
Ville Syrjälä1652d192015-03-31 14:12:01 +03007254static int skylake_get_display_clock_speed(struct drm_device *dev)
7255{
7256 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläea617912016-05-13 23:41:24 +03007257 uint32_t cdctl;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007258
Ville Syrjäläea617912016-05-13 23:41:24 +03007259 skl_dpll0_update(dev_priv);
7260
Ville Syrjälä63911d72016-05-13 23:41:32 +03007261 if (dev_priv->cdclk_pll.vco == 0)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03007262 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007263
Ville Syrjäläea617912016-05-13 23:41:24 +03007264 cdctl = I915_READ(CDCLK_CTL);
Ville Syrjälä1652d192015-03-31 14:12:01 +03007265
Ville Syrjälä63911d72016-05-13 23:41:32 +03007266 if (dev_priv->cdclk_pll.vco == 8640000) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03007267 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7268 case CDCLK_FREQ_450_432:
7269 return 432000;
7270 case CDCLK_FREQ_337_308:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03007271 return 308571;
Ville Syrjäläea617912016-05-13 23:41:24 +03007272 case CDCLK_FREQ_540:
7273 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007274 case CDCLK_FREQ_675_617:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03007275 return 617143;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007276 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03007277 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03007278 }
7279 } else {
Ville Syrjälä1652d192015-03-31 14:12:01 +03007280 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7281 case CDCLK_FREQ_450_432:
7282 return 450000;
7283 case CDCLK_FREQ_337_308:
7284 return 337500;
Ville Syrjäläea617912016-05-13 23:41:24 +03007285 case CDCLK_FREQ_540:
7286 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007287 case CDCLK_FREQ_675_617:
7288 return 675000;
7289 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03007290 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03007291 }
7292 }
7293
Ville Syrjälä709e05c2016-05-13 23:41:33 +03007294 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03007295}
7296
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007297static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
7298{
7299 u32 val;
7300
7301 dev_priv->cdclk_pll.ref = 19200;
Imre Deak1c3f7702016-05-24 15:38:32 +03007302 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007303
7304 val = I915_READ(BXT_DE_PLL_ENABLE);
Imre Deak1c3f7702016-05-24 15:38:32 +03007305 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007306 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007307
Imre Deak1c3f7702016-05-24 15:38:32 +03007308 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
7309 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007310
7311 val = I915_READ(BXT_DE_PLL_CTL);
7312 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
7313 dev_priv->cdclk_pll.ref;
7314}
7315
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007316static int broxton_get_display_clock_speed(struct drm_device *dev)
7317{
7318 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf5986242016-05-13 23:41:37 +03007319 u32 divider;
7320 int div, vco;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007321
Ville Syrjälä83d7c812016-05-13 23:41:35 +03007322 bxt_de_pll_update(dev_priv);
7323
Ville Syrjäläf5986242016-05-13 23:41:37 +03007324 vco = dev_priv->cdclk_pll.vco;
7325 if (vco == 0)
7326 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007327
Ville Syrjäläf5986242016-05-13 23:41:37 +03007328 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007329
Ville Syrjäläf5986242016-05-13 23:41:37 +03007330 switch (divider) {
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007331 case BXT_CDCLK_CD2X_DIV_SEL_1:
Ville Syrjäläf5986242016-05-13 23:41:37 +03007332 div = 2;
7333 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007334 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
Ville Syrjäläf5986242016-05-13 23:41:37 +03007335 div = 3;
7336 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007337 case BXT_CDCLK_CD2X_DIV_SEL_2:
Ville Syrjäläf5986242016-05-13 23:41:37 +03007338 div = 4;
7339 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007340 case BXT_CDCLK_CD2X_DIV_SEL_4:
Ville Syrjäläf5986242016-05-13 23:41:37 +03007341 div = 8;
7342 break;
7343 default:
7344 MISSING_CASE(divider);
7345 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007346 }
7347
Ville Syrjäläf5986242016-05-13 23:41:37 +03007348 return DIV_ROUND_CLOSEST(vco, div);
Bob Paauweacd3f3d2015-06-23 14:14:26 -07007349}
7350
Ville Syrjälä1652d192015-03-31 14:12:01 +03007351static int broadwell_get_display_clock_speed(struct drm_device *dev)
7352{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007353 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1652d192015-03-31 14:12:01 +03007354 uint32_t lcpll = I915_READ(LCPLL_CTL);
7355 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7356
7357 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7358 return 800000;
7359 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7360 return 450000;
7361 else if (freq == LCPLL_CLK_FREQ_450)
7362 return 450000;
7363 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
7364 return 540000;
7365 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
7366 return 337500;
7367 else
7368 return 675000;
7369}
7370
7371static int haswell_get_display_clock_speed(struct drm_device *dev)
7372{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007373 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1652d192015-03-31 14:12:01 +03007374 uint32_t lcpll = I915_READ(LCPLL_CTL);
7375 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7376
7377 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7378 return 800000;
7379 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7380 return 450000;
7381 else if (freq == LCPLL_CLK_FREQ_450)
7382 return 450000;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007383 else if (IS_HSW_ULT(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +03007384 return 337500;
7385 else
7386 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007387}
7388
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07007389static int valleyview_get_display_clock_speed(struct drm_device *dev)
7390{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007391 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
7392 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07007393}
7394
Ville Syrjäläb37a6432015-03-31 14:11:54 +03007395static int ilk_get_display_clock_speed(struct drm_device *dev)
7396{
7397 return 450000;
7398}
7399
Jesse Barnese70236a2009-09-21 10:42:27 -07007400static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08007401{
Jesse Barnese70236a2009-09-21 10:42:27 -07007402 return 400000;
7403}
Jesse Barnes79e53942008-11-07 14:24:08 -08007404
Jesse Barnese70236a2009-09-21 10:42:27 -07007405static int i915_get_display_clock_speed(struct drm_device *dev)
7406{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007407 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07007408}
Jesse Barnes79e53942008-11-07 14:24:08 -08007409
Jesse Barnese70236a2009-09-21 10:42:27 -07007410static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
7411{
7412 return 200000;
7413}
Jesse Barnes79e53942008-11-07 14:24:08 -08007414
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007415static int pnv_get_display_clock_speed(struct drm_device *dev)
7416{
David Weinehall52a05c32016-08-22 13:32:44 +03007417 struct pci_dev *pdev = dev->pdev;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007418 u16 gcfgc = 0;
7419
David Weinehall52a05c32016-08-22 13:32:44 +03007420 pci_read_config_word(pdev, GCFGC, &gcfgc);
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007421
7422 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7423 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007424 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007425 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007426 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007427 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007428 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007429 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
7430 return 200000;
7431 default:
7432 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
7433 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007434 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007435 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007436 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02007437 }
7438}
7439
Jesse Barnese70236a2009-09-21 10:42:27 -07007440static int i915gm_get_display_clock_speed(struct drm_device *dev)
7441{
David Weinehall52a05c32016-08-22 13:32:44 +03007442 struct pci_dev *pdev = dev->pdev;
Jesse Barnese70236a2009-09-21 10:42:27 -07007443 u16 gcfgc = 0;
7444
David Weinehall52a05c32016-08-22 13:32:44 +03007445 pci_read_config_word(pdev, GCFGC, &gcfgc);
Jesse Barnese70236a2009-09-21 10:42:27 -07007446
7447 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03007448 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07007449 else {
7450 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7451 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007452 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07007453 default:
7454 case GC_DISPLAY_CLOCK_190_200_MHZ:
7455 return 190000;
7456 }
7457 }
7458}
Jesse Barnes79e53942008-11-07 14:24:08 -08007459
Jesse Barnese70236a2009-09-21 10:42:27 -07007460static int i865_get_display_clock_speed(struct drm_device *dev)
7461{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007462 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07007463}
7464
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007465static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07007466{
David Weinehall52a05c32016-08-22 13:32:44 +03007467 struct pci_dev *pdev = dev->pdev;
Jesse Barnese70236a2009-09-21 10:42:27 -07007468 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007469
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03007470 /*
7471 * 852GM/852GMV only supports 133 MHz and the HPLLCC
7472 * encoding is different :(
7473 * FIXME is this the right way to detect 852GM/852GMV?
7474 */
David Weinehall52a05c32016-08-22 13:32:44 +03007475 if (pdev->revision == 0x1)
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03007476 return 133333;
7477
David Weinehall52a05c32016-08-22 13:32:44 +03007478 pci_bus_read_config_word(pdev->bus,
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007479 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
7480
Jesse Barnese70236a2009-09-21 10:42:27 -07007481 /* Assume that the hardware is in the high speed state. This
7482 * should be the default.
7483 */
7484 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
7485 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007486 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07007487 case GC_CLOCK_100_200:
7488 return 200000;
7489 case GC_CLOCK_166_250:
7490 return 250000;
7491 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03007492 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03007493 case GC_CLOCK_133_266:
7494 case GC_CLOCK_133_266_2:
7495 case GC_CLOCK_166_266:
7496 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07007497 }
7498
7499 /* Shouldn't happen */
7500 return 0;
7501}
7502
7503static int i830_get_display_clock_speed(struct drm_device *dev)
7504{
Ville Syrjäläe907f172015-03-31 14:09:47 +03007505 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08007506}
7507
Ville Syrjälä34edce22015-05-22 11:22:33 +03007508static unsigned int intel_hpll_vco(struct drm_device *dev)
7509{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007510 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007511 static const unsigned int blb_vco[8] = {
7512 [0] = 3200000,
7513 [1] = 4000000,
7514 [2] = 5333333,
7515 [3] = 4800000,
7516 [4] = 6400000,
7517 };
7518 static const unsigned int pnv_vco[8] = {
7519 [0] = 3200000,
7520 [1] = 4000000,
7521 [2] = 5333333,
7522 [3] = 4800000,
7523 [4] = 2666667,
7524 };
7525 static const unsigned int cl_vco[8] = {
7526 [0] = 3200000,
7527 [1] = 4000000,
7528 [2] = 5333333,
7529 [3] = 6400000,
7530 [4] = 3333333,
7531 [5] = 3566667,
7532 [6] = 4266667,
7533 };
7534 static const unsigned int elk_vco[8] = {
7535 [0] = 3200000,
7536 [1] = 4000000,
7537 [2] = 5333333,
7538 [3] = 4800000,
7539 };
7540 static const unsigned int ctg_vco[8] = {
7541 [0] = 3200000,
7542 [1] = 4000000,
7543 [2] = 5333333,
7544 [3] = 6400000,
7545 [4] = 2666667,
7546 [5] = 4266667,
7547 };
7548 const unsigned int *vco_table;
7549 unsigned int vco;
7550 uint8_t tmp = 0;
7551
7552 /* FIXME other chipsets? */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007553 if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +03007554 vco_table = ctg_vco;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007555 else if (IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +03007556 vco_table = elk_vco;
7557 else if (IS_CRESTLINE(dev))
7558 vco_table = cl_vco;
7559 else if (IS_PINEVIEW(dev))
7560 vco_table = pnv_vco;
7561 else if (IS_G33(dev))
7562 vco_table = blb_vco;
7563 else
7564 return 0;
7565
7566 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7567
7568 vco = vco_table[tmp & 0x7];
7569 if (vco == 0)
7570 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7571 else
7572 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7573
7574 return vco;
7575}
7576
7577static int gm45_get_display_clock_speed(struct drm_device *dev)
7578{
David Weinehall52a05c32016-08-22 13:32:44 +03007579 struct pci_dev *pdev = dev->pdev;
Ville Syrjälä34edce22015-05-22 11:22:33 +03007580 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7581 uint16_t tmp = 0;
7582
David Weinehall52a05c32016-08-22 13:32:44 +03007583 pci_read_config_word(pdev, GCFGC, &tmp);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007584
7585 cdclk_sel = (tmp >> 12) & 0x1;
7586
7587 switch (vco) {
7588 case 2666667:
7589 case 4000000:
7590 case 5333333:
7591 return cdclk_sel ? 333333 : 222222;
7592 case 3200000:
7593 return cdclk_sel ? 320000 : 228571;
7594 default:
7595 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7596 return 222222;
7597 }
7598}
7599
7600static int i965gm_get_display_clock_speed(struct drm_device *dev)
7601{
David Weinehall52a05c32016-08-22 13:32:44 +03007602 struct pci_dev *pdev = dev->pdev;
Ville Syrjälä34edce22015-05-22 11:22:33 +03007603 static const uint8_t div_3200[] = { 16, 10, 8 };
7604 static const uint8_t div_4000[] = { 20, 12, 10 };
7605 static const uint8_t div_5333[] = { 24, 16, 14 };
7606 const uint8_t *div_table;
7607 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7608 uint16_t tmp = 0;
7609
David Weinehall52a05c32016-08-22 13:32:44 +03007610 pci_read_config_word(pdev, GCFGC, &tmp);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007611
7612 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7613
7614 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7615 goto fail;
7616
7617 switch (vco) {
7618 case 3200000:
7619 div_table = div_3200;
7620 break;
7621 case 4000000:
7622 div_table = div_4000;
7623 break;
7624 case 5333333:
7625 div_table = div_5333;
7626 break;
7627 default:
7628 goto fail;
7629 }
7630
7631 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7632
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007633fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007634 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7635 return 200000;
7636}
7637
7638static int g33_get_display_clock_speed(struct drm_device *dev)
7639{
David Weinehall52a05c32016-08-22 13:32:44 +03007640 struct pci_dev *pdev = dev->pdev;
Ville Syrjälä34edce22015-05-22 11:22:33 +03007641 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7642 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7643 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7644 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7645 const uint8_t *div_table;
7646 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7647 uint16_t tmp = 0;
7648
David Weinehall52a05c32016-08-22 13:32:44 +03007649 pci_read_config_word(pdev, GCFGC, &tmp);
Ville Syrjälä34edce22015-05-22 11:22:33 +03007650
7651 cdclk_sel = (tmp >> 4) & 0x7;
7652
7653 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7654 goto fail;
7655
7656 switch (vco) {
7657 case 3200000:
7658 div_table = div_3200;
7659 break;
7660 case 4000000:
7661 div_table = div_4000;
7662 break;
7663 case 4800000:
7664 div_table = div_4800;
7665 break;
7666 case 5333333:
7667 div_table = div_5333;
7668 break;
7669 default:
7670 goto fail;
7671 }
7672
7673 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7674
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007675fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007676 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7677 return 190476;
7678}
7679
Zhenyu Wang2c072452009-06-05 15:38:42 +08007680static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007681intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007682{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007683 while (*num > DATA_LINK_M_N_MASK ||
7684 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007685 *num >>= 1;
7686 *den >>= 1;
7687 }
7688}
7689
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007690static void compute_m_n(unsigned int m, unsigned int n,
7691 uint32_t *ret_m, uint32_t *ret_n)
7692{
7693 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7694 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7695 intel_reduce_m_n_ratio(ret_m, ret_n);
7696}
7697
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007698void
7699intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7700 int pixel_clock, int link_clock,
7701 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007702{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007703 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007704
7705 compute_m_n(bits_per_pixel * pixel_clock,
7706 link_clock * nlanes * 8,
7707 &m_n->gmch_m, &m_n->gmch_n);
7708
7709 compute_m_n(pixel_clock, link_clock,
7710 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007711}
7712
Chris Wilsona7615032011-01-12 17:04:08 +00007713static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7714{
Jani Nikulad330a952014-01-21 11:24:25 +02007715 if (i915.panel_use_ssc >= 0)
7716 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007717 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007718 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007719}
7720
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007721static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007722{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007723 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007724}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007725
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007726static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7727{
7728 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007729}
7730
Daniel Vetterf47709a2013-03-28 10:42:02 +01007731static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007732 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007733 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08007734{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007735 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007736 u32 fp, fp2 = 0;
7737
7738 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007739 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007740 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007741 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007742 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007743 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007744 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007745 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007746 }
7747
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007748 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007749
Daniel Vetterf47709a2013-03-28 10:42:02 +01007750 crtc->lowfreq_avail = false;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007751 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007752 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007753 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007754 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007755 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007756 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007757 }
7758}
7759
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007760static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7761 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007762{
7763 u32 reg_val;
7764
7765 /*
7766 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7767 * and set it to a reasonable value instead.
7768 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007769 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007770 reg_val &= 0xffffff00;
7771 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007772 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007773
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007774 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007775 reg_val &= 0x8cffffff;
7776 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007777 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007778
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007779 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007780 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007781 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007782
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007783 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007784 reg_val &= 0x00ffffff;
7785 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007786 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007787}
7788
Daniel Vetterb5518422013-05-03 11:49:48 +02007789static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7790 struct intel_link_m_n *m_n)
7791{
7792 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007793 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007794 int pipe = crtc->pipe;
7795
Daniel Vettere3b95f12013-05-03 11:49:49 +02007796 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7797 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7798 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7799 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007800}
7801
7802static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007803 struct intel_link_m_n *m_n,
7804 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007805{
7806 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007807 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007808 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007809 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007810
7811 if (INTEL_INFO(dev)->gen >= 5) {
7812 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7813 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7814 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7815 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007816 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7817 * for gen < 8) and if DRRS is supported (to make sure the
7818 * registers are not unnecessarily accessed).
7819 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007820 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
7821 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007822 I915_WRITE(PIPE_DATA_M2(transcoder),
7823 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7824 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7825 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7826 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7827 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007828 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007829 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7830 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7831 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7832 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007833 }
7834}
7835
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307836void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007837{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307838 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7839
7840 if (m_n == M1_N1) {
7841 dp_m_n = &crtc->config->dp_m_n;
7842 dp_m2_n2 = &crtc->config->dp_m2_n2;
7843 } else if (m_n == M2_N2) {
7844
7845 /*
7846 * M2_N2 registers are not supported. Hence m2_n2 divider value
7847 * needs to be programmed into M1_N1.
7848 */
7849 dp_m_n = &crtc->config->dp_m2_n2;
7850 } else {
7851 DRM_ERROR("Unsupported divider value\n");
7852 return;
7853 }
7854
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007855 if (crtc->config->has_pch_encoder)
7856 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007857 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307858 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007859}
7860
Daniel Vetter251ac862015-06-18 10:30:24 +02007861static void vlv_compute_dpll(struct intel_crtc *crtc,
7862 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007863{
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007864 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007865 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007866 if (crtc->pipe != PIPE_A)
7867 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007868
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007869 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007870 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007871 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7872 DPLL_EXT_BUFFER_ENABLE_VLV;
7873
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007874 pipe_config->dpll_hw_state.dpll_md =
7875 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7876}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007877
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007878static void chv_compute_dpll(struct intel_crtc *crtc,
7879 struct intel_crtc_state *pipe_config)
7880{
7881 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007882 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007883 if (crtc->pipe != PIPE_A)
7884 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7885
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007886 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007887 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007888 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7889
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007890 pipe_config->dpll_hw_state.dpll_md =
7891 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007892}
7893
Ville Syrjäläd288f652014-10-28 13:20:22 +02007894static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007895 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007896{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007897 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007898 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007899 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007900 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007901 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007902 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007903
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007904 /* Enable Refclk */
7905 I915_WRITE(DPLL(pipe),
7906 pipe_config->dpll_hw_state.dpll &
7907 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7908
7909 /* No need to actually set up the DPLL with DSI */
7910 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7911 return;
7912
Ville Syrjäläa5805162015-05-26 20:42:30 +03007913 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007914
Ville Syrjäläd288f652014-10-28 13:20:22 +02007915 bestn = pipe_config->dpll.n;
7916 bestm1 = pipe_config->dpll.m1;
7917 bestm2 = pipe_config->dpll.m2;
7918 bestp1 = pipe_config->dpll.p1;
7919 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007920
Jesse Barnes89b667f2013-04-18 14:51:36 -07007921 /* See eDP HDMI DPIO driver vbios notes doc */
7922
7923 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007924 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007925 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007926
7927 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007928 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007929
7930 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007931 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007932 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007933 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007934
7935 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007936 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007937
7938 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007939 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7940 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7941 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007942 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007943
7944 /*
7945 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7946 * but we don't support that).
7947 * Note: don't use the DAC post divider as it seems unstable.
7948 */
7949 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007950 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007951
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007952 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007953 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007954
Jesse Barnes89b667f2013-04-18 14:51:36 -07007955 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007956 if (pipe_config->port_clock == 162000 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007957 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7958 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007959 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007960 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007961 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007962 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007963 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007964
Ville Syrjälä37a56502016-06-22 21:57:04 +03007965 if (intel_crtc_has_dp_encoder(pipe_config)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007966 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007967 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007968 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007969 0x0df40000);
7970 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007971 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007972 0x0df70000);
7973 } else { /* HDMI or VGA */
7974 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007975 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007976 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007977 0x0df70000);
7978 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007979 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007980 0x0df40000);
7981 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007982
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007983 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007984 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ville Syrjälä2210ce72016-06-22 21:57:05 +03007985 if (intel_crtc_has_dp_encoder(crtc->config))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007986 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007987 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007988
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007989 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007990 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007991}
7992
Ville Syrjäläd288f652014-10-28 13:20:22 +02007993static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007994 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007995{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007996 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007997 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007998 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007999 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308000 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008001 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05308002 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308003 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008004
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03008005 /* Enable Refclk and SSC */
8006 I915_WRITE(DPLL(pipe),
8007 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
8008
8009 /* No need to actually set up the DPLL with DSI */
8010 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8011 return;
8012
Ville Syrjäläd288f652014-10-28 13:20:22 +02008013 bestn = pipe_config->dpll.n;
8014 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
8015 bestm1 = pipe_config->dpll.m1;
8016 bestm2 = pipe_config->dpll.m2 >> 22;
8017 bestp1 = pipe_config->dpll.p1;
8018 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308019 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05308020 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308021 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008022
Ville Syrjäläa5805162015-05-26 20:42:30 +03008023 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008024
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008025 /* p1 and p2 divider */
8026 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
8027 5 << DPIO_CHV_S1_DIV_SHIFT |
8028 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
8029 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
8030 1 << DPIO_CHV_K_DIV_SHIFT);
8031
8032 /* Feedback post-divider - m2 */
8033 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
8034
8035 /* Feedback refclk divider - n and m1 */
8036 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
8037 DPIO_CHV_M1_DIV_BY_2 |
8038 1 << DPIO_CHV_N_DIV_SHIFT);
8039
8040 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03008041 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008042
8043 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05308044 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8045 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
8046 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
8047 if (bestm2_frac)
8048 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
8049 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008050
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05308051 /* Program digital lock detect threshold */
8052 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
8053 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
8054 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
8055 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
8056 if (!bestm2_frac)
8057 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
8058 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
8059
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008060 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308061 if (vco == 5400000) {
8062 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
8063 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
8064 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
8065 tribuf_calcntr = 0x9;
8066 } else if (vco <= 6200000) {
8067 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
8068 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
8069 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8070 tribuf_calcntr = 0x9;
8071 } else if (vco <= 6480000) {
8072 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8073 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8074 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8075 tribuf_calcntr = 0x8;
8076 } else {
8077 /* Not supported. Apply the same limits as in the max case */
8078 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8079 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8080 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8081 tribuf_calcntr = 0;
8082 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008083 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
8084
Ville Syrjälä968040b2015-03-11 22:52:08 +02008085 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05308086 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
8087 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
8088 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
8089
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008090 /* AFC Recal */
8091 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
8092 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
8093 DPIO_AFC_RECAL);
8094
Ville Syrjäläa5805162015-05-26 20:42:30 +03008095 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008096}
8097
Ville Syrjäläd288f652014-10-28 13:20:22 +02008098/**
8099 * vlv_force_pll_on - forcibly enable just the PLL
8100 * @dev_priv: i915 private structure
8101 * @pipe: pipe PLL to enable
8102 * @dpll: PLL configuration
8103 *
8104 * Enable the PLL for @pipe using the supplied @dpll config. To be used
8105 * in cases where we need the PLL enabled even when @pipe is not going to
8106 * be enabled.
8107 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008108int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
8109 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02008110{
8111 struct intel_crtc *crtc =
8112 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008113 struct intel_crtc_state *pipe_config;
8114
8115 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8116 if (!pipe_config)
8117 return -ENOMEM;
8118
8119 pipe_config->base.crtc = &crtc->base;
8120 pipe_config->pixel_multiplier = 1;
8121 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02008122
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008123 if (IS_CHERRYVIEW(to_i915(dev))) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008124 chv_compute_dpll(crtc, pipe_config);
8125 chv_prepare_pll(crtc, pipe_config);
8126 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02008127 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008128 vlv_compute_dpll(crtc, pipe_config);
8129 vlv_prepare_pll(crtc, pipe_config);
8130 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02008131 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00008132
8133 kfree(pipe_config);
8134
8135 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02008136}
8137
8138/**
8139 * vlv_force_pll_off - forcibly disable just the PLL
8140 * @dev_priv: i915 private structure
8141 * @pipe: pipe PLL to disable
8142 *
8143 * Disable the PLL for @pipe. To be used in cases where we need
8144 * the PLL enabled even when @pipe is not going to be enabled.
8145 */
8146void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
8147{
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008148 if (IS_CHERRYVIEW(to_i915(dev)))
Ville Syrjäläd288f652014-10-28 13:20:22 +02008149 chv_disable_pll(to_i915(dev), pipe);
8150 else
8151 vlv_disable_pll(to_i915(dev), pipe);
8152}
8153
Daniel Vetter251ac862015-06-18 10:30:24 +02008154static void i9xx_compute_dpll(struct intel_crtc *crtc,
8155 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008156 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008157{
Daniel Vetterf47709a2013-03-28 10:42:02 +01008158 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008159 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008160 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008161 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008162
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008163 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05308164
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008165 dpll = DPLL_VGA_MODE_DIS;
8166
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008167 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008168 dpll |= DPLLB_MODE_LVDS;
8169 else
8170 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01008171
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008172 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) || IS_G33(dev_priv)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008173 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02008174 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008175 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02008176
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008177 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8178 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008179 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008180
Ville Syrjälä37a56502016-06-22 21:57:04 +03008181 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008182 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008183
8184 /* compute bitmask from p1 value */
8185 if (IS_PINEVIEW(dev))
8186 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8187 else {
8188 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01008189 if (IS_G4X(dev_priv) && reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008190 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8191 }
8192 switch (clock->p2) {
8193 case 5:
8194 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8195 break;
8196 case 7:
8197 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8198 break;
8199 case 10:
8200 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8201 break;
8202 case 14:
8203 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8204 break;
8205 }
8206 if (INTEL_INFO(dev)->gen >= 4)
8207 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8208
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008209 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008210 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008211 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008212 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008213 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8214 else
8215 dpll |= PLL_REF_INPUT_DREFCLK;
8216
8217 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008218 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008219
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008220 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008221 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008222 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008223 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008224 }
8225}
8226
Daniel Vetter251ac862015-06-18 10:30:24 +02008227static void i8xx_compute_dpll(struct intel_crtc *crtc,
8228 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008229 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008230{
Daniel Vetterf47709a2013-03-28 10:42:02 +01008231 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008232 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008233 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008234 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008235
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008236 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05308237
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008238 dpll = DPLL_VGA_MODE_DIS;
8239
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008240 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008241 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8242 } else {
8243 if (clock->p1 == 2)
8244 dpll |= PLL_P1_DIVIDE_BY_TWO;
8245 else
8246 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8247 if (clock->p2 == 4)
8248 dpll |= PLL_P2_DIVIDE_BY_4;
8249 }
8250
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008251 if (!IS_I830(dev_priv) &&
8252 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008253 dpll |= DPLL_DVO_2X_MODE;
8254
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008255 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008256 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008257 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8258 else
8259 dpll |= PLL_REF_INPUT_DREFCLK;
8260
8261 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008262 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008263}
8264
Daniel Vetter8a654f32013-06-01 17:16:22 +02008265static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008266{
8267 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008268 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008269 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008270 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03008271 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02008272 uint32_t crtc_vtotal, crtc_vblank_end;
8273 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02008274
8275 /* We need to be careful not to changed the adjusted mode, for otherwise
8276 * the hw state checker will get angry at the mismatch. */
8277 crtc_vtotal = adjusted_mode->crtc_vtotal;
8278 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008279
Ville Syrjälä609aeac2014-03-28 23:29:30 +02008280 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008281 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02008282 crtc_vtotal -= 1;
8283 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02008284
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008285 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02008286 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
8287 else
8288 vsyncshift = adjusted_mode->crtc_hsync_start -
8289 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02008290 if (vsyncshift < 0)
8291 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008292 }
8293
8294 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008295 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008296
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008297 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008298 (adjusted_mode->crtc_hdisplay - 1) |
8299 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008300 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008301 (adjusted_mode->crtc_hblank_start - 1) |
8302 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008303 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008304 (adjusted_mode->crtc_hsync_start - 1) |
8305 ((adjusted_mode->crtc_hsync_end - 1) << 16));
8306
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008307 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008308 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02008309 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008310 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008311 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02008312 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008313 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008314 (adjusted_mode->crtc_vsync_start - 1) |
8315 ((adjusted_mode->crtc_vsync_end - 1) << 16));
8316
Paulo Zanonib5e508d2012-10-24 11:34:43 -02008317 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
8318 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
8319 * documented on the DDI_FUNC_CTL register description, EDP Input Select
8320 * bits. */
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01008321 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
Paulo Zanonib5e508d2012-10-24 11:34:43 -02008322 (pipe == PIPE_B || pipe == PIPE_C))
8323 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
8324
Jani Nikulabc58be62016-03-18 17:05:39 +02008325}
8326
8327static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
8328{
8329 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008330 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02008331 enum pipe pipe = intel_crtc->pipe;
8332
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008333 /* pipesrc controls the size that is scaled from, which should
8334 * always be the user's requested size.
8335 */
8336 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008337 ((intel_crtc->config->pipe_src_w - 1) << 16) |
8338 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03008339}
8340
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008341static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008342 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008343{
8344 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008345 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008346 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
8347 uint32_t tmp;
8348
8349 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008350 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
8351 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008352 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008353 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
8354 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008355 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008356 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
8357 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008358
8359 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008360 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
8361 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008362 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008363 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
8364 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008365 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008366 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
8367 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008368
8369 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008370 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
8371 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
8372 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008373 }
Jani Nikulabc58be62016-03-18 17:05:39 +02008374}
8375
8376static void intel_get_pipe_src_size(struct intel_crtc *crtc,
8377 struct intel_crtc_state *pipe_config)
8378{
8379 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008380 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02008381 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008382
8383 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03008384 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
8385 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
8386
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008387 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
8388 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008389}
8390
Daniel Vetterf6a83282014-02-11 15:28:57 -08008391void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008392 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03008393{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008394 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
8395 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
8396 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
8397 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03008398
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008399 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
8400 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
8401 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
8402 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03008403
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008404 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02008405 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03008406
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02008407 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
8408 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02008409
8410 mode->hsync = drm_mode_hsync(mode);
8411 mode->vrefresh = drm_mode_vrefresh(mode);
8412 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03008413}
8414
Daniel Vetter84b046f2013-02-19 18:48:54 +01008415static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
8416{
8417 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008418 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter84b046f2013-02-19 18:48:54 +01008419 uint32_t pipeconf;
8420
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02008421 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01008422
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03008423 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
8424 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8425 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02008426
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008427 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03008428 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01008429
Daniel Vetterff9ce462013-04-24 14:57:17 +02008430 /* only g4x and later have fancy bpc/dither controls */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01008431 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8432 IS_CHERRYVIEW(dev_priv)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02008433 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008434 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02008435 pipeconf |= PIPECONF_DITHER_EN |
8436 PIPECONF_DITHER_TYPE_SP;
8437
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008438 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02008439 case 18:
8440 pipeconf |= PIPECONF_6BPC;
8441 break;
8442 case 24:
8443 pipeconf |= PIPECONF_8BPC;
8444 break;
8445 case 30:
8446 pipeconf |= PIPECONF_10BPC;
8447 break;
8448 default:
8449 /* Case prevented by intel_choose_pipe_bpp_dither. */
8450 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01008451 }
8452 }
8453
8454 if (HAS_PIPE_CXSR(dev)) {
8455 if (intel_crtc->lowfreq_avail) {
8456 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
8457 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
8458 } else {
8459 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01008460 }
8461 }
8462
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008463 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02008464 if (INTEL_INFO(dev)->gen < 4 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008465 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02008466 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
8467 else
8468 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
8469 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01008470 pipeconf |= PIPECONF_PROGRESSIVE;
8471
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008472 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08008473 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02008474 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03008475
Daniel Vetter84b046f2013-02-19 18:48:54 +01008476 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
8477 POSTING_READ(PIPECONF(intel_crtc->pipe));
8478}
8479
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008480static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
8481 struct intel_crtc_state *crtc_state)
8482{
8483 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008484 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008485 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008486 int refclk = 48000;
8487
8488 memset(&crtc_state->dpll_hw_state, 0,
8489 sizeof(crtc_state->dpll_hw_state));
8490
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008491 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008492 if (intel_panel_use_ssc(dev_priv)) {
8493 refclk = dev_priv->vbt.lvds_ssc_freq;
8494 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8495 }
8496
8497 limit = &intel_limits_i8xx_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008498 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008499 limit = &intel_limits_i8xx_dvo;
8500 } else {
8501 limit = &intel_limits_i8xx_dac;
8502 }
8503
8504 if (!crtc_state->clock_set &&
8505 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8506 refclk, NULL, &crtc_state->dpll)) {
8507 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8508 return -EINVAL;
8509 }
8510
8511 i8xx_compute_dpll(crtc, crtc_state, NULL);
8512
8513 return 0;
8514}
8515
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008516static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
8517 struct intel_crtc_state *crtc_state)
8518{
8519 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008520 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008521 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008522 int refclk = 96000;
8523
8524 memset(&crtc_state->dpll_hw_state, 0,
8525 sizeof(crtc_state->dpll_hw_state));
8526
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008527 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008528 if (intel_panel_use_ssc(dev_priv)) {
8529 refclk = dev_priv->vbt.lvds_ssc_freq;
8530 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8531 }
8532
8533 if (intel_is_dual_link_lvds(dev))
8534 limit = &intel_limits_g4x_dual_channel_lvds;
8535 else
8536 limit = &intel_limits_g4x_single_channel_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008537 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
8538 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008539 limit = &intel_limits_g4x_hdmi;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008540 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02008541 limit = &intel_limits_g4x_sdvo;
8542 } else {
8543 /* The option is for other outputs */
8544 limit = &intel_limits_i9xx_sdvo;
8545 }
8546
8547 if (!crtc_state->clock_set &&
8548 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8549 refclk, NULL, &crtc_state->dpll)) {
8550 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8551 return -EINVAL;
8552 }
8553
8554 i9xx_compute_dpll(crtc, crtc_state, NULL);
8555
8556 return 0;
8557}
8558
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008559static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8560 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008561{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008562 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008563 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008564 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008565 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008566
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008567 memset(&crtc_state->dpll_hw_state, 0,
8568 sizeof(crtc_state->dpll_hw_state));
8569
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008570 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008571 if (intel_panel_use_ssc(dev_priv)) {
8572 refclk = dev_priv->vbt.lvds_ssc_freq;
8573 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8574 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008575
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008576 limit = &intel_limits_pineview_lvds;
8577 } else {
8578 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008579 }
Jani Nikulaf2335332013-09-13 11:03:09 +03008580
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008581 if (!crtc_state->clock_set &&
8582 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8583 refclk, NULL, &crtc_state->dpll)) {
8584 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8585 return -EINVAL;
8586 }
8587
8588 i9xx_compute_dpll(crtc, crtc_state, NULL);
8589
8590 return 0;
8591}
8592
8593static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8594 struct intel_crtc_state *crtc_state)
8595{
8596 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008597 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008598 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008599 int refclk = 96000;
8600
8601 memset(&crtc_state->dpll_hw_state, 0,
8602 sizeof(crtc_state->dpll_hw_state));
8603
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008604 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008605 if (intel_panel_use_ssc(dev_priv)) {
8606 refclk = dev_priv->vbt.lvds_ssc_freq;
8607 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008608 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008609
8610 limit = &intel_limits_i9xx_lvds;
8611 } else {
8612 limit = &intel_limits_i9xx_sdvo;
8613 }
8614
8615 if (!crtc_state->clock_set &&
8616 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8617 refclk, NULL, &crtc_state->dpll)) {
8618 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8619 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008620 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008621
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008622 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07008623
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008624 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008625}
8626
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008627static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8628 struct intel_crtc_state *crtc_state)
8629{
8630 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008631 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008632
8633 memset(&crtc_state->dpll_hw_state, 0,
8634 sizeof(crtc_state->dpll_hw_state));
8635
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008636 if (!crtc_state->clock_set &&
8637 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8638 refclk, NULL, &crtc_state->dpll)) {
8639 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8640 return -EINVAL;
8641 }
8642
8643 chv_compute_dpll(crtc, crtc_state);
8644
8645 return 0;
8646}
8647
8648static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8649 struct intel_crtc_state *crtc_state)
8650{
8651 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008652 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008653
8654 memset(&crtc_state->dpll_hw_state, 0,
8655 sizeof(crtc_state->dpll_hw_state));
8656
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008657 if (!crtc_state->clock_set &&
8658 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8659 refclk, NULL, &crtc_state->dpll)) {
8660 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8661 return -EINVAL;
8662 }
8663
8664 vlv_compute_dpll(crtc, crtc_state);
8665
8666 return 0;
8667}
8668
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008669static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008670 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008671{
8672 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008673 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008674 uint32_t tmp;
8675
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008676 if (INTEL_GEN(dev_priv) <= 3 &&
8677 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008678 return;
8679
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008680 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008681 if (!(tmp & PFIT_ENABLE))
8682 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008683
Daniel Vetter06922822013-07-11 13:35:40 +02008684 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008685 if (INTEL_INFO(dev)->gen < 4) {
8686 if (crtc->pipe != PIPE_B)
8687 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008688 } else {
8689 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8690 return;
8691 }
8692
Daniel Vetter06922822013-07-11 13:35:40 +02008693 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008694 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008695}
8696
Jesse Barnesacbec812013-09-20 11:29:32 -07008697static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008698 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008699{
8700 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008701 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesacbec812013-09-20 11:29:32 -07008702 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008703 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07008704 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008705 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008706
Ville Syrjäläb5219732016-03-15 16:40:01 +02008707 /* In case of DSI, DPLL will not be used */
8708 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308709 return;
8710
Ville Syrjäläa5805162015-05-26 20:42:30 +03008711 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008712 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008713 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008714
8715 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8716 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8717 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8718 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8719 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8720
Imre Deakdccbea32015-06-22 23:35:51 +03008721 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008722}
8723
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008724static void
8725i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8726 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008727{
8728 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008729 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008730 u32 val, base, offset;
8731 int pipe = crtc->pipe, plane = crtc->plane;
8732 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008733 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008734 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008735 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008736
Damien Lespiau42a7b082015-02-05 19:35:13 +00008737 val = I915_READ(DSPCNTR(plane));
8738 if (!(val & DISPLAY_PLANE_ENABLE))
8739 return;
8740
Damien Lespiaud9806c92015-01-21 14:07:19 +00008741 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008742 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008743 DRM_DEBUG_KMS("failed to alloc fb\n");
8744 return;
8745 }
8746
Damien Lespiau1b842c82015-01-21 13:50:54 +00008747 fb = &intel_fb->base;
8748
Daniel Vetter18c52472015-02-10 17:16:09 +00008749 if (INTEL_INFO(dev)->gen >= 4) {
8750 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008751 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008752 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8753 }
8754 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008755
8756 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008757 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008758 fb->pixel_format = fourcc;
8759 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008760
8761 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008762 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008763 offset = I915_READ(DSPTILEOFF(plane));
8764 else
8765 offset = I915_READ(DSPLINOFF(plane));
8766 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8767 } else {
8768 base = I915_READ(DSPADDR(plane));
8769 }
8770 plane_config->base = base;
8771
8772 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008773 fb->width = ((val >> 16) & 0xfff) + 1;
8774 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008775
8776 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008777 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008778
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008779 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008780 fb->pixel_format,
8781 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008782
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008783 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008784
Damien Lespiau2844a922015-01-20 12:51:48 +00008785 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8786 pipe_name(pipe), plane, fb->width, fb->height,
8787 fb->bits_per_pixel, base, fb->pitches[0],
8788 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008789
Damien Lespiau2d140302015-02-05 17:22:18 +00008790 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008791}
8792
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008793static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008794 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008795{
8796 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008797 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008798 int pipe = pipe_config->cpu_transcoder;
8799 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008800 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008801 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008802 int refclk = 100000;
8803
Ville Syrjäläb5219732016-03-15 16:40:01 +02008804 /* In case of DSI, DPLL will not be used */
8805 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8806 return;
8807
Ville Syrjäläa5805162015-05-26 20:42:30 +03008808 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008809 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8810 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8811 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8812 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008813 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008814 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008815
8816 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008817 clock.m2 = (pll_dw0 & 0xff) << 22;
8818 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8819 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008820 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8821 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8822 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8823
Imre Deakdccbea32015-06-22 23:35:51 +03008824 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008825}
8826
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008827static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008828 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008829{
8830 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008831 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02008832 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008833 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008834 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008835
Imre Deak17290502016-02-12 18:55:11 +02008836 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8837 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008838 return false;
8839
Daniel Vettere143a212013-07-04 12:01:15 +02008840 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008841 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008842
Imre Deak17290502016-02-12 18:55:11 +02008843 ret = false;
8844
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008845 tmp = I915_READ(PIPECONF(crtc->pipe));
8846 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008847 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008848
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01008849 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8850 IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008851 switch (tmp & PIPECONF_BPC_MASK) {
8852 case PIPECONF_6BPC:
8853 pipe_config->pipe_bpp = 18;
8854 break;
8855 case PIPECONF_8BPC:
8856 pipe_config->pipe_bpp = 24;
8857 break;
8858 case PIPECONF_10BPC:
8859 pipe_config->pipe_bpp = 30;
8860 break;
8861 default:
8862 break;
8863 }
8864 }
8865
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008866 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
Wayne Boyer666a4532015-12-09 12:29:35 -08008867 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008868 pipe_config->limited_color_range = true;
8869
Ville Syrjälä282740f2013-09-04 18:30:03 +03008870 if (INTEL_INFO(dev)->gen < 4)
8871 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8872
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008873 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008874 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008875
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008876 i9xx_get_pfit_config(crtc, pipe_config);
8877
Daniel Vetter6c49f242013-06-06 12:45:25 +02008878 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008879 /* No way to read it out on pipes B and C */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008880 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
Ville Syrjäläc2317752016-03-15 16:39:56 +02008881 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8882 else
8883 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008884 pipe_config->pixel_multiplier =
8885 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8886 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008887 pipe_config->dpll_hw_state.dpll_md = tmp;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008888 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
8889 IS_G33(dev_priv)) {
Daniel Vetter6c49f242013-06-06 12:45:25 +02008890 tmp = I915_READ(DPLL(crtc->pipe));
8891 pipe_config->pixel_multiplier =
8892 ((tmp & SDVO_MULTIPLIER_MASK)
8893 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8894 } else {
8895 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8896 * port and will be fixed up in the encoder->get_config
8897 * function. */
8898 pipe_config->pixel_multiplier = 1;
8899 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008900 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008901 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008902 /*
8903 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8904 * on 830. Filter it out here so that we don't
8905 * report errors due to that.
8906 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008907 if (IS_I830(dev_priv))
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008908 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8909
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008910 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8911 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008912 } else {
8913 /* Mask out read-only status bits. */
8914 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8915 DPLL_PORTC_READY_MASK |
8916 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008917 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008918
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01008919 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008920 chv_crtc_clock_get(crtc, pipe_config);
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01008921 else if (IS_VALLEYVIEW(dev_priv))
Jesse Barnesacbec812013-09-20 11:29:32 -07008922 vlv_crtc_clock_get(crtc, pipe_config);
8923 else
8924 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008925
Ville Syrjälä0f646142015-08-26 19:39:18 +03008926 /*
8927 * Normally the dotclock is filled in by the encoder .get_config()
8928 * but in case the pipe is enabled w/o any ports we need a sane
8929 * default.
8930 */
8931 pipe_config->base.adjusted_mode.crtc_clock =
8932 pipe_config->port_clock / pipe_config->pixel_multiplier;
8933
Imre Deak17290502016-02-12 18:55:11 +02008934 ret = true;
8935
8936out:
8937 intel_display_power_put(dev_priv, power_domain);
8938
8939 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008940}
8941
Paulo Zanonidde86e22012-12-01 12:04:25 -02008942static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008943{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008944 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008945 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04008946 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008947 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008948 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008949 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008950 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008951 bool has_ck505 = false;
8952 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04008953 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008954
8955 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008956 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008957 switch (encoder->type) {
8958 case INTEL_OUTPUT_LVDS:
8959 has_panel = true;
8960 has_lvds = true;
8961 break;
8962 case INTEL_OUTPUT_EDP:
8963 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008964 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008965 has_cpu_edp = true;
8966 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008967 default:
8968 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008969 }
8970 }
8971
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008972 if (HAS_PCH_IBX(dev_priv)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008973 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008974 can_ssc = has_ck505;
8975 } else {
8976 has_ck505 = false;
8977 can_ssc = true;
8978 }
8979
Lyude1c1a24d2016-06-14 11:04:09 -04008980 /* Check if any DPLLs are using the SSC source */
8981 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8982 u32 temp = I915_READ(PCH_DPLL(i));
8983
8984 if (!(temp & DPLL_VCO_ENABLE))
8985 continue;
8986
8987 if ((temp & PLL_REF_INPUT_MASK) ==
8988 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8989 using_ssc_source = true;
8990 break;
8991 }
8992 }
8993
8994 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8995 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008996
8997 /* Ironlake: try to setup display ref clock before DPLL
8998 * enabling. This is only under driver's control after
8999 * PCH B stepping, previous chipset stepping should be
9000 * ignoring this setting.
9001 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009002 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07009003
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009004 /* As we must carefully and slowly disable/enable each source in turn,
9005 * compute the final state we want first and check if we need to
9006 * make any changes at all.
9007 */
9008 final = val;
9009 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07009010 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009011 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07009012 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009013 final |= DREF_NONSPREAD_SOURCE_ENABLE;
9014
Daniel Vetter8c07eb62016-06-09 18:39:07 +02009015 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009016 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02009017 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07009018
Keith Packard199e5d72011-09-22 12:01:57 -07009019 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009020 final |= DREF_SSC_SOURCE_ENABLE;
9021
9022 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9023 final |= DREF_SSC1_ENABLE;
9024
9025 if (has_cpu_edp) {
9026 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9027 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9028 else
9029 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9030 } else
9031 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04009032 } else if (using_ssc_source) {
9033 final |= DREF_SSC_SOURCE_ENABLE;
9034 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009035 }
9036
9037 if (final == val)
9038 return;
9039
9040 /* Always enable nonspread source */
9041 val &= ~DREF_NONSPREAD_SOURCE_MASK;
9042
9043 if (has_ck505)
9044 val |= DREF_NONSPREAD_CK505_ENABLE;
9045 else
9046 val |= DREF_NONSPREAD_SOURCE_ENABLE;
9047
9048 if (has_panel) {
9049 val &= ~DREF_SSC_SOURCE_MASK;
9050 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07009051
Keith Packard199e5d72011-09-22 12:01:57 -07009052 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07009053 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07009054 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009055 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02009056 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009057 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07009058
9059 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009060 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07009061 POSTING_READ(PCH_DREF_CONTROL);
9062 udelay(200);
9063
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009064 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07009065
9066 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07009067 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07009068 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07009069 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009070 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02009071 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009072 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07009073 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009074 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07009075
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009076 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07009077 POSTING_READ(PCH_DREF_CONTROL);
9078 udelay(200);
9079 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04009080 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07009081
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009082 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07009083
9084 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009085 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07009086
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009087 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07009088 POSTING_READ(PCH_DREF_CONTROL);
9089 udelay(200);
9090
Lyude1c1a24d2016-06-14 11:04:09 -04009091 if (!using_ssc_source) {
9092 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07009093
Lyude1c1a24d2016-06-14 11:04:09 -04009094 /* Turn off the SSC source */
9095 val &= ~DREF_SSC_SOURCE_MASK;
9096 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07009097
Lyude1c1a24d2016-06-14 11:04:09 -04009098 /* Turn off SSC1 */
9099 val &= ~DREF_SSC1_ENABLE;
9100
9101 I915_WRITE(PCH_DREF_CONTROL, val);
9102 POSTING_READ(PCH_DREF_CONTROL);
9103 udelay(200);
9104 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07009105 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07009106
9107 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07009108}
9109
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009110static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02009111{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009112 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02009113
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009114 tmp = I915_READ(SOUTH_CHICKEN2);
9115 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
9116 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009117
Imre Deakcf3598c2016-06-28 13:37:31 +03009118 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
9119 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009120 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02009121
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009122 tmp = I915_READ(SOUTH_CHICKEN2);
9123 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
9124 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009125
Imre Deakcf3598c2016-06-28 13:37:31 +03009126 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
9127 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009128 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009129}
9130
9131/* WaMPhyProgramming:hsw */
9132static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9133{
9134 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02009135
9136 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9137 tmp &= ~(0xFF << 24);
9138 tmp |= (0x12 << 24);
9139 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9140
Paulo Zanonidde86e22012-12-01 12:04:25 -02009141 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9142 tmp |= (1 << 11);
9143 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9144
9145 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9146 tmp |= (1 << 11);
9147 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9148
Paulo Zanonidde86e22012-12-01 12:04:25 -02009149 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9150 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9151 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9152
9153 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9154 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9155 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9156
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009157 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9158 tmp &= ~(7 << 13);
9159 tmp |= (5 << 13);
9160 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009161
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009162 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9163 tmp &= ~(7 << 13);
9164 tmp |= (5 << 13);
9165 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009166
9167 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9168 tmp &= ~0xFF;
9169 tmp |= 0x1C;
9170 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9171
9172 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9173 tmp &= ~0xFF;
9174 tmp |= 0x1C;
9175 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
9176
9177 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
9178 tmp &= ~(0xFF << 16);
9179 tmp |= (0x1C << 16);
9180 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
9181
9182 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
9183 tmp &= ~(0xFF << 16);
9184 tmp |= (0x1C << 16);
9185 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
9186
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009187 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
9188 tmp |= (1 << 27);
9189 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009190
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009191 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
9192 tmp |= (1 << 27);
9193 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009194
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009195 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
9196 tmp &= ~(0xF << 28);
9197 tmp |= (4 << 28);
9198 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009199
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03009200 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
9201 tmp &= ~(0xF << 28);
9202 tmp |= (4 << 28);
9203 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009204}
9205
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009206/* Implements 3 different sequences from BSpec chapter "Display iCLK
9207 * Programming" based on the parameters passed:
9208 * - Sequence to enable CLKOUT_DP
9209 * - Sequence to enable CLKOUT_DP without spread
9210 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
9211 */
9212static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
9213 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009214{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009215 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009216 uint32_t reg, tmp;
9217
9218 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
9219 with_spread = true;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009220 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
9221 with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009222 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009223
Ville Syrjäläa5805162015-05-26 20:42:30 +03009224 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009225
9226 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9227 tmp &= ~SBI_SSCCTL_DISABLE;
9228 tmp |= SBI_SSCCTL_PATHALT;
9229 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9230
9231 udelay(24);
9232
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009233 if (with_spread) {
9234 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9235 tmp &= ~SBI_SSCCTL_PATHALT;
9236 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03009237
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009238 if (with_fdi) {
9239 lpt_reset_fdi_mphy(dev_priv);
9240 lpt_program_fdi_mphy(dev_priv);
9241 }
9242 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02009243
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009244 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03009245 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9246 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9247 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01009248
Ville Syrjäläa5805162015-05-26 20:42:30 +03009249 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02009250}
9251
Paulo Zanoni47701c32013-07-23 11:19:25 -03009252/* Sequence to disable CLKOUT_DP */
9253static void lpt_disable_clkout_dp(struct drm_device *dev)
9254{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009255 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni47701c32013-07-23 11:19:25 -03009256 uint32_t reg, tmp;
9257
Ville Syrjäläa5805162015-05-26 20:42:30 +03009258 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03009259
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01009260 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03009261 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9262 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9263 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9264
9265 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9266 if (!(tmp & SBI_SSCCTL_DISABLE)) {
9267 if (!(tmp & SBI_SSCCTL_PATHALT)) {
9268 tmp |= SBI_SSCCTL_PATHALT;
9269 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9270 udelay(32);
9271 }
9272 tmp |= SBI_SSCCTL_DISABLE;
9273 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9274 }
9275
Ville Syrjäläa5805162015-05-26 20:42:30 +03009276 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03009277}
9278
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02009279#define BEND_IDX(steps) ((50 + (steps)) / 5)
9280
9281static const uint16_t sscdivintphase[] = {
9282 [BEND_IDX( 50)] = 0x3B23,
9283 [BEND_IDX( 45)] = 0x3B23,
9284 [BEND_IDX( 40)] = 0x3C23,
9285 [BEND_IDX( 35)] = 0x3C23,
9286 [BEND_IDX( 30)] = 0x3D23,
9287 [BEND_IDX( 25)] = 0x3D23,
9288 [BEND_IDX( 20)] = 0x3E23,
9289 [BEND_IDX( 15)] = 0x3E23,
9290 [BEND_IDX( 10)] = 0x3F23,
9291 [BEND_IDX( 5)] = 0x3F23,
9292 [BEND_IDX( 0)] = 0x0025,
9293 [BEND_IDX( -5)] = 0x0025,
9294 [BEND_IDX(-10)] = 0x0125,
9295 [BEND_IDX(-15)] = 0x0125,
9296 [BEND_IDX(-20)] = 0x0225,
9297 [BEND_IDX(-25)] = 0x0225,
9298 [BEND_IDX(-30)] = 0x0325,
9299 [BEND_IDX(-35)] = 0x0325,
9300 [BEND_IDX(-40)] = 0x0425,
9301 [BEND_IDX(-45)] = 0x0425,
9302 [BEND_IDX(-50)] = 0x0525,
9303};
9304
9305/*
9306 * Bend CLKOUT_DP
9307 * steps -50 to 50 inclusive, in steps of 5
9308 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
9309 * change in clock period = -(steps / 10) * 5.787 ps
9310 */
9311static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
9312{
9313 uint32_t tmp;
9314 int idx = BEND_IDX(steps);
9315
9316 if (WARN_ON(steps % 5 != 0))
9317 return;
9318
9319 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
9320 return;
9321
9322 mutex_lock(&dev_priv->sb_lock);
9323
9324 if (steps % 10 != 0)
9325 tmp = 0xAAAAAAAB;
9326 else
9327 tmp = 0x00000000;
9328 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
9329
9330 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
9331 tmp &= 0xffff0000;
9332 tmp |= sscdivintphase[idx];
9333 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
9334
9335 mutex_unlock(&dev_priv->sb_lock);
9336}
9337
9338#undef BEND_IDX
9339
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009340static void lpt_init_pch_refclk(struct drm_device *dev)
9341{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009342 struct intel_encoder *encoder;
9343 bool has_vga = false;
9344
Damien Lespiaub2784e12014-08-05 11:29:37 +01009345 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009346 switch (encoder->type) {
9347 case INTEL_OUTPUT_ANALOG:
9348 has_vga = true;
9349 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02009350 default:
9351 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009352 }
9353 }
9354
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02009355 if (has_vga) {
9356 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03009357 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02009358 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03009359 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02009360 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03009361}
9362
Paulo Zanonidde86e22012-12-01 12:04:25 -02009363/*
9364 * Initialize reference clocks when the driver loads
9365 */
9366void intel_init_pch_refclk(struct drm_device *dev)
9367{
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009368 struct drm_i915_private *dev_priv = to_i915(dev);
9369
9370 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
Paulo Zanonidde86e22012-12-01 12:04:25 -02009371 ironlake_init_pch_refclk(dev);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009372 else if (HAS_PCH_LPT(dev_priv))
Paulo Zanonidde86e22012-12-01 12:04:25 -02009373 lpt_init_pch_refclk(dev);
9374}
9375
Daniel Vetter6ff93602013-04-19 11:24:36 +02009376static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03009377{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009378 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanonic8203562012-09-12 10:06:29 -03009379 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9380 int pipe = intel_crtc->pipe;
9381 uint32_t val;
9382
Daniel Vetter78114072013-06-13 00:54:57 +02009383 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03009384
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009385 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03009386 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01009387 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009388 break;
9389 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01009390 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009391 break;
9392 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01009393 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009394 break;
9395 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01009396 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03009397 break;
9398 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03009399 /* Case prevented by intel_choose_pipe_bpp_dither. */
9400 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03009401 }
9402
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009403 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03009404 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9405
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009406 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03009407 val |= PIPECONF_INTERLACED_ILK;
9408 else
9409 val |= PIPECONF_PROGRESSIVE;
9410
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009411 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02009412 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02009413
Paulo Zanonic8203562012-09-12 10:06:29 -03009414 I915_WRITE(PIPECONF(pipe), val);
9415 POSTING_READ(PIPECONF(pipe));
9416}
9417
Daniel Vetter6ff93602013-04-19 11:24:36 +02009418static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009419{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009420 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009421 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009422 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02009423 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009424
Jani Nikula391bf042016-03-18 17:05:40 +02009425 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009426 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9427
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009428 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009429 val |= PIPECONF_INTERLACED_ILK;
9430 else
9431 val |= PIPECONF_PROGRESSIVE;
9432
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009433 I915_WRITE(PIPECONF(cpu_transcoder), val);
9434 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02009435}
9436
Jani Nikula391bf042016-03-18 17:05:40 +02009437static void haswell_set_pipemisc(struct drm_crtc *crtc)
9438{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009439 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Jani Nikula391bf042016-03-18 17:05:40 +02009440 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9441
9442 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
9443 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009444
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009445 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009446 case 18:
9447 val |= PIPEMISC_DITHER_6_BPC;
9448 break;
9449 case 24:
9450 val |= PIPEMISC_DITHER_8_BPC;
9451 break;
9452 case 30:
9453 val |= PIPEMISC_DITHER_10_BPC;
9454 break;
9455 case 36:
9456 val |= PIPEMISC_DITHER_12_BPC;
9457 break;
9458 default:
9459 /* Case prevented by pipe_config_set_bpp. */
9460 BUG();
9461 }
9462
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009463 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009464 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
9465
Jani Nikula391bf042016-03-18 17:05:40 +02009466 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07009467 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03009468}
9469
Paulo Zanonid4b19312012-11-29 11:29:32 -02009470int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
9471{
9472 /*
9473 * Account for spread spectrum to avoid
9474 * oversubscribing the link. Max center spread
9475 * is 2.5%; use 5% for safety's sake.
9476 */
9477 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02009478 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02009479}
9480
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009481static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02009482{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009483 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03009484}
9485
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009486static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
9487 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03009488 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009489{
9490 struct drm_crtc *crtc = &intel_crtc->base;
9491 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009492 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009493 u32 dpll, fp, fp2;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009494 int factor;
Jesse Barnes79e53942008-11-07 14:24:08 -08009495
Chris Wilsonc1858122010-12-03 21:35:48 +00009496 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07009497 factor = 21;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009498 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Eric Anholt8febb292011-03-30 13:01:07 -07009499 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009500 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01009501 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07009502 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009503 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07009504 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00009505
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009506 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00009507
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009508 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
9509 fp |= FP_CB_TUNE;
9510
9511 if (reduced_clock) {
9512 fp2 = i9xx_dpll_compute_fp(reduced_clock);
9513
9514 if (reduced_clock->m < factor * reduced_clock->n)
9515 fp2 |= FP_CB_TUNE;
9516 } else {
9517 fp2 = fp;
9518 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02009519
Chris Wilson5eddb702010-09-11 13:48:45 +01009520 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08009521
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009522 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Eric Anholta07d6782011-03-30 13:01:08 -07009523 dpll |= DPLLB_MODE_LVDS;
9524 else
9525 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009526
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009527 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02009528 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02009529
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009530 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
9531 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02009532 dpll |= DPLL_SDVO_HIGH_SPEED;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009533
Ville Syrjälä37a56502016-06-22 21:57:04 +03009534 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02009535 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08009536
Ville Syrjälä7d7f8632016-09-26 11:30:46 +03009537 /*
9538 * The high speed IO clock is only really required for
9539 * SDVO/HDMI/DP, but we also enable it for CRT to make it
9540 * possible to share the DPLL between CRT and HDMI. Enabling
9541 * the clock needlessly does no real harm, except use up a
9542 * bit of power potentially.
9543 *
9544 * We'll limit this to IVB with 3 pipes, since it has only two
9545 * DPLLs and so DPLL sharing is the only way to get three pipes
9546 * driving PCH ports at the same time. On SNB we could do this,
9547 * and potentially avoid enabling the second DPLL, but it's not
9548 * clear if it''s a win or loss power wise. No point in doing
9549 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
9550 */
9551 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
9552 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
9553 dpll |= DPLL_SDVO_HIGH_SPEED;
9554
Eric Anholta07d6782011-03-30 13:01:08 -07009555 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009556 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009557 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009558 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07009559
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009560 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07009561 case 5:
9562 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9563 break;
9564 case 7:
9565 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9566 break;
9567 case 10:
9568 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9569 break;
9570 case 14:
9571 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9572 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009573 }
9574
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03009575 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9576 intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05009577 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08009578 else
9579 dpll |= PLL_REF_INPUT_DREFCLK;
9580
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009581 dpll |= DPLL_VCO_ENABLE;
9582
9583 crtc_state->dpll_hw_state.dpll = dpll;
9584 crtc_state->dpll_hw_state.fp0 = fp;
9585 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009586}
9587
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009588static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9589 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08009590{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009591 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009592 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03009593 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02009594 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009595 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03009596 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009597 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08009598
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03009599 memset(&crtc_state->dpll_hw_state, 0,
9600 sizeof(crtc_state->dpll_hw_state));
9601
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009602 crtc->lowfreq_avail = false;
9603
9604 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9605 if (!crtc_state->has_pch_encoder)
9606 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009607
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03009608 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009609 if (intel_panel_use_ssc(dev_priv)) {
9610 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9611 dev_priv->vbt.lvds_ssc_freq);
9612 refclk = dev_priv->vbt.lvds_ssc_freq;
9613 }
9614
9615 if (intel_is_dual_link_lvds(dev)) {
9616 if (refclk == 100000)
9617 limit = &intel_limits_ironlake_dual_lvds_100m;
9618 else
9619 limit = &intel_limits_ironlake_dual_lvds;
9620 } else {
9621 if (refclk == 100000)
9622 limit = &intel_limits_ironlake_single_lvds_100m;
9623 else
9624 limit = &intel_limits_ironlake_single_lvds;
9625 }
9626 } else {
9627 limit = &intel_limits_ironlake_dac;
9628 }
9629
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009630 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009631 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9632 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009633 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9634 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009635 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009636
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009637 ironlake_compute_dpll(crtc, crtc_state,
9638 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009639
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009640 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9641 if (pll == NULL) {
9642 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9643 pipe_name(crtc->pipe));
9644 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009645 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009646
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03009647 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009648 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009649 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02009650
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009651 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009652}
9653
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009654static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9655 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009656{
9657 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009658 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009659 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009660
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009661 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9662 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9663 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9664 & ~TU_SIZE_MASK;
9665 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9666 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9667 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9668}
9669
9670static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9671 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009672 struct intel_link_m_n *m_n,
9673 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009674{
9675 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009676 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009677 enum pipe pipe = crtc->pipe;
9678
9679 if (INTEL_INFO(dev)->gen >= 5) {
9680 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9681 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9682 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9683 & ~TU_SIZE_MASK;
9684 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9685 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9686 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009687 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9688 * gen < 8) and if DRRS is supported (to make sure the
9689 * registers are not unnecessarily read).
9690 */
9691 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009692 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009693 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9694 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9695 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9696 & ~TU_SIZE_MASK;
9697 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9698 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9699 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9700 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009701 } else {
9702 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9703 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9704 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9705 & ~TU_SIZE_MASK;
9706 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9707 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9708 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9709 }
9710}
9711
9712void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009713 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009714{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009715 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009716 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9717 else
9718 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009719 &pipe_config->dp_m_n,
9720 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009721}
9722
Daniel Vetter72419202013-04-04 13:28:53 +02009723static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009724 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009725{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009726 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009727 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009728}
9729
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009730static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009731 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009732{
9733 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009734 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Kondurua1b22782015-04-07 15:28:45 -07009735 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9736 uint32_t ps_ctrl = 0;
9737 int id = -1;
9738 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009739
Chandra Kondurua1b22782015-04-07 15:28:45 -07009740 /* find scaler attached to this pipe */
9741 for (i = 0; i < crtc->num_scalers; i++) {
9742 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9743 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9744 id = i;
9745 pipe_config->pch_pfit.enabled = true;
9746 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9747 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9748 break;
9749 }
9750 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009751
Chandra Kondurua1b22782015-04-07 15:28:45 -07009752 scaler_state->scaler_id = id;
9753 if (id >= 0) {
9754 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9755 } else {
9756 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009757 }
9758}
9759
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009760static void
9761skylake_get_initial_plane_config(struct intel_crtc *crtc,
9762 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009763{
9764 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009765 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau40f46282015-02-27 11:15:21 +00009766 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009767 int pipe = crtc->pipe;
9768 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009769 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009770 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009771 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009772
Damien Lespiaud9806c92015-01-21 14:07:19 +00009773 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009774 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009775 DRM_DEBUG_KMS("failed to alloc fb\n");
9776 return;
9777 }
9778
Damien Lespiau1b842c82015-01-21 13:50:54 +00009779 fb = &intel_fb->base;
9780
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009781 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009782 if (!(val & PLANE_CTL_ENABLE))
9783 goto error;
9784
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009785 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9786 fourcc = skl_format_to_fourcc(pixel_format,
9787 val & PLANE_CTL_ORDER_RGBX,
9788 val & PLANE_CTL_ALPHA_MASK);
9789 fb->pixel_format = fourcc;
9790 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9791
Damien Lespiau40f46282015-02-27 11:15:21 +00009792 tiling = val & PLANE_CTL_TILED_MASK;
9793 switch (tiling) {
9794 case PLANE_CTL_TILED_LINEAR:
9795 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9796 break;
9797 case PLANE_CTL_TILED_X:
9798 plane_config->tiling = I915_TILING_X;
9799 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9800 break;
9801 case PLANE_CTL_TILED_Y:
9802 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9803 break;
9804 case PLANE_CTL_TILED_YF:
9805 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9806 break;
9807 default:
9808 MISSING_CASE(tiling);
9809 goto error;
9810 }
9811
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009812 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9813 plane_config->base = base;
9814
9815 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9816
9817 val = I915_READ(PLANE_SIZE(pipe, 0));
9818 fb->height = ((val >> 16) & 0xfff) + 1;
9819 fb->width = ((val >> 0) & 0x1fff) + 1;
9820
9821 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009822 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009823 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009824 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9825
9826 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009827 fb->pixel_format,
9828 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009829
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009830 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009831
9832 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9833 pipe_name(pipe), fb->width, fb->height,
9834 fb->bits_per_pixel, base, fb->pitches[0],
9835 plane_config->size);
9836
Damien Lespiau2d140302015-02-05 17:22:18 +00009837 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009838 return;
9839
9840error:
Matthew Auldd1a3a032016-08-23 16:00:44 +01009841 kfree(intel_fb);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009842}
9843
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009844static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009845 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009846{
9847 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009848 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009849 uint32_t tmp;
9850
9851 tmp = I915_READ(PF_CTL(crtc->pipe));
9852
9853 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009854 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009855 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9856 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009857
9858 /* We currently do not free assignements of panel fitters on
9859 * ivb/hsw (since we don't use the higher upscaling modes which
9860 * differentiates them) so just WARN about this case for now. */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01009861 if (IS_GEN7(dev_priv)) {
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009862 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9863 PF_PIPE_SEL_IVB(crtc->pipe));
9864 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009865 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009866}
9867
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009868static void
9869ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9870 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009871{
9872 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009873 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009874 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009875 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009876 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009877 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009878 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009879 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009880
Damien Lespiau42a7b082015-02-05 19:35:13 +00009881 val = I915_READ(DSPCNTR(pipe));
9882 if (!(val & DISPLAY_PLANE_ENABLE))
9883 return;
9884
Damien Lespiaud9806c92015-01-21 14:07:19 +00009885 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009886 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009887 DRM_DEBUG_KMS("failed to alloc fb\n");
9888 return;
9889 }
9890
Damien Lespiau1b842c82015-01-21 13:50:54 +00009891 fb = &intel_fb->base;
9892
Daniel Vetter18c52472015-02-10 17:16:09 +00009893 if (INTEL_INFO(dev)->gen >= 4) {
9894 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009895 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009896 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9897 }
9898 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009899
9900 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009901 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009902 fb->pixel_format = fourcc;
9903 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009904
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009905 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01009906 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009907 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009908 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009909 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009910 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009911 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009912 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009913 }
9914 plane_config->base = base;
9915
9916 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009917 fb->width = ((val >> 16) & 0xfff) + 1;
9918 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009919
9920 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009921 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009922
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009923 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009924 fb->pixel_format,
9925 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009926
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009927 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009928
Damien Lespiau2844a922015-01-20 12:51:48 +00009929 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9930 pipe_name(pipe), fb->width, fb->height,
9931 fb->bits_per_pixel, base, fb->pitches[0],
9932 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009933
Damien Lespiau2d140302015-02-05 17:22:18 +00009934 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009935}
9936
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009937static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009938 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009939{
9940 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009941 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02009942 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009943 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009944 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009945
Imre Deak17290502016-02-12 18:55:11 +02009946 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9947 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009948 return false;
9949
Daniel Vettere143a212013-07-04 12:01:15 +02009950 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009951 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009952
Imre Deak17290502016-02-12 18:55:11 +02009953 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009954 tmp = I915_READ(PIPECONF(crtc->pipe));
9955 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009956 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009957
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009958 switch (tmp & PIPECONF_BPC_MASK) {
9959 case PIPECONF_6BPC:
9960 pipe_config->pipe_bpp = 18;
9961 break;
9962 case PIPECONF_8BPC:
9963 pipe_config->pipe_bpp = 24;
9964 break;
9965 case PIPECONF_10BPC:
9966 pipe_config->pipe_bpp = 30;
9967 break;
9968 case PIPECONF_12BPC:
9969 pipe_config->pipe_bpp = 36;
9970 break;
9971 default:
9972 break;
9973 }
9974
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009975 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9976 pipe_config->limited_color_range = true;
9977
Daniel Vetterab9412b2013-05-03 11:49:46 +02009978 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009979 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009980 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009981
Daniel Vetter88adfff2013-03-28 10:42:01 +01009982 pipe_config->has_pch_encoder = true;
9983
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009984 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9985 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9986 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009987
9988 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009989
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009990 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03009991 /*
9992 * The pipe->pch transcoder and pch transcoder->pll
9993 * mapping is fixed.
9994 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009995 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009996 } else {
9997 tmp = I915_READ(PCH_DPLL_SEL);
9998 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009999 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010000 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010001 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010002 }
Daniel Vetter66e985c2013-06-05 13:34:20 +020010003
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010004 pipe_config->shared_dpll =
10005 intel_get_shared_dpll_by_id(dev_priv, pll_id);
10006 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +020010007
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010008 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10009 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +020010010
10011 tmp = pipe_config->dpll_hw_state.dpll;
10012 pipe_config->pixel_multiplier =
10013 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
10014 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010015
10016 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +020010017 } else {
10018 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010019 }
10020
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010021 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +020010022 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010023
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010024 ironlake_get_pfit_config(crtc, pipe_config);
10025
Imre Deak17290502016-02-12 18:55:11 +020010026 ret = true;
10027
10028out:
10029 intel_display_power_put(dev_priv, power_domain);
10030
10031 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010032}
10033
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010034static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
10035{
Chris Wilson91c8a322016-07-05 10:40:23 +010010036 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010037 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010038
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010039 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -050010040 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010041 pipe_name(crtc->pipe));
10042
Rob Clarke2c719b2014-12-15 13:56:32 -050010043 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
10044 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +030010045 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
10046 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Imre Deak44cb7342016-08-10 14:07:29 +030010047 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010048 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010049 "CPU PWM1 enabled\n");
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010010050 if (IS_HASWELL(dev_priv))
Rob Clarke2c719b2014-12-15 13:56:32 -050010051 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -030010052 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010053 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010054 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010055 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010056 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050010057 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010058
Paulo Zanoni9926ada2014-04-01 19:39:47 -030010059 /*
10060 * In theory we can still leave IRQs enabled, as long as only the HPD
10061 * interrupts remain enabled. We used to check for that, but since it's
10062 * gen-specific and since we only disable LCPLL after we fully disable
10063 * the interrupts, the check below should be enough.
10064 */
Rob Clarke2c719b2014-12-15 13:56:32 -050010065 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010066}
10067
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010068static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
10069{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010010070 if (IS_HASWELL(dev_priv))
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010071 return I915_READ(D_COMP_HSW);
10072 else
10073 return I915_READ(D_COMP_BDW);
10074}
10075
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010076static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
10077{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010010078 if (IS_HASWELL(dev_priv)) {
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010079 mutex_lock(&dev_priv->rps.hw_lock);
10080 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
10081 val))
Chris Wilson79cf2192016-08-24 11:16:07 +010010082 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010083 mutex_unlock(&dev_priv->rps.hw_lock);
10084 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010085 I915_WRITE(D_COMP_BDW, val);
10086 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010087 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010088}
10089
10090/*
10091 * This function implements pieces of two sequences from BSpec:
10092 * - Sequence for display software to disable LCPLL
10093 * - Sequence for display software to allow package C8+
10094 * The steps implemented here are just the steps that actually touch the LCPLL
10095 * register. Callers should take care of disabling all the display engine
10096 * functions, doing the mode unset, fixing interrupts, etc.
10097 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -030010098static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
10099 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010100{
10101 uint32_t val;
10102
10103 assert_can_disable_lcpll(dev_priv);
10104
10105 val = I915_READ(LCPLL_CTL);
10106
10107 if (switch_to_fclk) {
10108 val |= LCPLL_CD_SOURCE_FCLK;
10109 I915_WRITE(LCPLL_CTL, val);
10110
Imre Deakf53dd632016-06-28 13:37:32 +030010111 if (wait_for_us(I915_READ(LCPLL_CTL) &
10112 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010113 DRM_ERROR("Switching to FCLK failed\n");
10114
10115 val = I915_READ(LCPLL_CTL);
10116 }
10117
10118 val |= LCPLL_PLL_DISABLE;
10119 I915_WRITE(LCPLL_CTL, val);
10120 POSTING_READ(LCPLL_CTL);
10121
Chris Wilson24d84412016-06-30 15:33:07 +010010122 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010123 DRM_ERROR("LCPLL still locked\n");
10124
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010125 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010126 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010127 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010128 ndelay(100);
10129
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010130 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
10131 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010132 DRM_ERROR("D_COMP RCOMP still in progress\n");
10133
10134 if (allow_power_down) {
10135 val = I915_READ(LCPLL_CTL);
10136 val |= LCPLL_POWER_DOWN_ALLOW;
10137 I915_WRITE(LCPLL_CTL, val);
10138 POSTING_READ(LCPLL_CTL);
10139 }
10140}
10141
10142/*
10143 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
10144 * source.
10145 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -030010146static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010147{
10148 uint32_t val;
10149
10150 val = I915_READ(LCPLL_CTL);
10151
10152 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
10153 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
10154 return;
10155
Paulo Zanonia8a8bd52014-03-07 20:08:05 -030010156 /*
10157 * Make sure we're not on PC8 state before disabling PC8, otherwise
10158 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -030010159 */
Mika Kuoppala59bad942015-01-16 11:34:40 +020010160 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -030010161
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010162 if (val & LCPLL_POWER_DOWN_ALLOW) {
10163 val &= ~LCPLL_POWER_DOWN_ALLOW;
10164 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +020010165 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010166 }
10167
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -030010168 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010169 val |= D_COMP_COMP_FORCE;
10170 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -030010171 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010172
10173 val = I915_READ(LCPLL_CTL);
10174 val &= ~LCPLL_PLL_DISABLE;
10175 I915_WRITE(LCPLL_CTL, val);
10176
Chris Wilson93220c02016-06-30 15:33:08 +010010177 if (intel_wait_for_register(dev_priv,
10178 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
10179 5))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010180 DRM_ERROR("LCPLL not locked yet\n");
10181
10182 if (val & LCPLL_CD_SOURCE_FCLK) {
10183 val = I915_READ(LCPLL_CTL);
10184 val &= ~LCPLL_CD_SOURCE_FCLK;
10185 I915_WRITE(LCPLL_CTL, val);
10186
Imre Deakf53dd632016-06-28 13:37:32 +030010187 if (wait_for_us((I915_READ(LCPLL_CTL) &
10188 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010189 DRM_ERROR("Switching back to LCPLL failed\n");
10190 }
Paulo Zanoni215733f2013-08-19 13:18:07 -030010191
Mika Kuoppala59bad942015-01-16 11:34:40 +020010192 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Chris Wilson91c8a322016-07-05 10:40:23 +010010193 intel_update_cdclk(&dev_priv->drm);
Paulo Zanonibe256dc2013-07-23 11:19:26 -030010194}
10195
Paulo Zanoni765dab672014-03-07 20:08:18 -030010196/*
10197 * Package states C8 and deeper are really deep PC states that can only be
10198 * reached when all the devices on the system allow it, so even if the graphics
10199 * device allows PC8+, it doesn't mean the system will actually get to these
10200 * states. Our driver only allows PC8+ when going into runtime PM.
10201 *
10202 * The requirements for PC8+ are that all the outputs are disabled, the power
10203 * well is disabled and most interrupts are disabled, and these are also
10204 * requirements for runtime PM. When these conditions are met, we manually do
10205 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
10206 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
10207 * hang the machine.
10208 *
10209 * When we really reach PC8 or deeper states (not just when we allow it) we lose
10210 * the state of some registers, so when we come back from PC8+ we need to
10211 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
10212 * need to take care of the registers kept by RC6. Notice that this happens even
10213 * if we don't put the device in PCI D3 state (which is what currently happens
10214 * because of the runtime PM support).
10215 *
10216 * For more, read "Display Sequences for Package C8" on the hardware
10217 * documentation.
10218 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -030010219void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -030010220{
Chris Wilson91c8a322016-07-05 10:40:23 +010010221 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonic67a4702013-08-19 13:18:09 -030010222 uint32_t val;
10223
Paulo Zanonic67a4702013-08-19 13:18:09 -030010224 DRM_DEBUG_KMS("Enabling package C8+\n");
10225
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010010226 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -030010227 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10228 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
10229 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10230 }
10231
10232 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010233 hsw_disable_lcpll(dev_priv, true, true);
10234}
10235
Paulo Zanonia14cb6f2014-03-07 20:08:17 -030010236void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -030010237{
Chris Wilson91c8a322016-07-05 10:40:23 +010010238 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonic67a4702013-08-19 13:18:09 -030010239 uint32_t val;
10240
Paulo Zanonic67a4702013-08-19 13:18:09 -030010241 DRM_DEBUG_KMS("Disabling package C8+\n");
10242
10243 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010244 lpt_init_pch_refclk(dev);
10245
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010010246 if (HAS_PCH_LPT_LP(dev_priv)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -030010247 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10248 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
10249 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10250 }
Paulo Zanonic67a4702013-08-19 13:18:09 -030010251}
10252
Imre Deak324513c2016-06-13 16:44:36 +030010253static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +053010254{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030010255 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010256 struct intel_atomic_state *old_intel_state =
10257 to_intel_atomic_state(old_state);
10258 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053010259
Imre Deak324513c2016-06-13 16:44:36 +030010260 bxt_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +053010261}
10262
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010263/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010264static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010265{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010266 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010010267 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010268 struct drm_crtc *crtc;
10269 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010270 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010271 unsigned max_pixel_rate = 0, i;
10272 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010273
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010274 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
10275 sizeof(intel_state->min_pixclk));
10276
10277 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010278 int pixel_rate;
10279
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010280 crtc_state = to_intel_crtc_state(cstate);
10281 if (!crtc_state->base.enable) {
10282 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010283 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010284 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010285
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010286 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010287
10288 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010289 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010290 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
10291
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010292 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010293 }
10294
Maarten Lankhorst565602d2015-12-10 12:33:57 +010010295 for_each_pipe(dev_priv, pipe)
10296 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
10297
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010298 return max_pixel_rate;
10299}
10300
10301static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
10302{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010303 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010304 uint32_t val, data;
10305 int ret;
10306
10307 if (WARN((I915_READ(LCPLL_CTL) &
10308 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
10309 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
10310 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
10311 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
10312 "trying to change cdclk frequency with cdclk not enabled\n"))
10313 return;
10314
10315 mutex_lock(&dev_priv->rps.hw_lock);
10316 ret = sandybridge_pcode_write(dev_priv,
10317 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
10318 mutex_unlock(&dev_priv->rps.hw_lock);
10319 if (ret) {
10320 DRM_ERROR("failed to inform pcode about cdclk change\n");
10321 return;
10322 }
10323
10324 val = I915_READ(LCPLL_CTL);
10325 val |= LCPLL_CD_SOURCE_FCLK;
10326 I915_WRITE(LCPLL_CTL, val);
10327
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +000010328 if (wait_for_us(I915_READ(LCPLL_CTL) &
10329 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010330 DRM_ERROR("Switching to FCLK failed\n");
10331
10332 val = I915_READ(LCPLL_CTL);
10333 val &= ~LCPLL_CLK_FREQ_MASK;
10334
10335 switch (cdclk) {
10336 case 450000:
10337 val |= LCPLL_CLK_FREQ_450;
10338 data = 0;
10339 break;
10340 case 540000:
10341 val |= LCPLL_CLK_FREQ_54O_BDW;
10342 data = 1;
10343 break;
10344 case 337500:
10345 val |= LCPLL_CLK_FREQ_337_5_BDW;
10346 data = 2;
10347 break;
10348 case 675000:
10349 val |= LCPLL_CLK_FREQ_675_BDW;
10350 data = 3;
10351 break;
10352 default:
10353 WARN(1, "invalid cdclk frequency\n");
10354 return;
10355 }
10356
10357 I915_WRITE(LCPLL_CTL, val);
10358
10359 val = I915_READ(LCPLL_CTL);
10360 val &= ~LCPLL_CD_SOURCE_FCLK;
10361 I915_WRITE(LCPLL_CTL, val);
10362
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +000010363 if (wait_for_us((I915_READ(LCPLL_CTL) &
10364 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010365 DRM_ERROR("Switching back to LCPLL failed\n");
10366
10367 mutex_lock(&dev_priv->rps.hw_lock);
10368 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
10369 mutex_unlock(&dev_priv->rps.hw_lock);
10370
Ville Syrjälä7f1052a2016-04-26 19:46:32 +030010371 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
10372
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010373 intel_update_cdclk(dev);
10374
10375 WARN(cdclk != dev_priv->cdclk_freq,
10376 "cdclk requested %d kHz but got %d kHz\n",
10377 cdclk, dev_priv->cdclk_freq);
10378}
10379
Ville Syrjälä587c7912016-05-11 22:44:41 +030010380static int broadwell_calc_cdclk(int max_pixclk)
10381{
10382 if (max_pixclk > 540000)
10383 return 675000;
10384 else if (max_pixclk > 450000)
10385 return 540000;
10386 else if (max_pixclk > 337500)
10387 return 450000;
10388 else
10389 return 337500;
10390}
10391
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010392static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010393{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010394 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010395 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010396 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010397 int cdclk;
10398
10399 /*
10400 * FIXME should also account for plane ratio
10401 * once 64bpp pixel formats are supported.
10402 */
Ville Syrjälä587c7912016-05-11 22:44:41 +030010403 cdclk = broadwell_calc_cdclk(max_pixclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010404
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010405 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +010010406 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10407 cdclk, dev_priv->max_cdclk_freq);
10408 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010409 }
10410
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010411 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10412 if (!intel_state->active_crtcs)
Ville Syrjälä587c7912016-05-11 22:44:41 +030010413 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010414
10415 return 0;
10416}
10417
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010418static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010419{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010420 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010010421 struct intel_atomic_state *old_intel_state =
10422 to_intel_atomic_state(old_state);
10423 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010424
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020010425 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030010426}
10427
Clint Taylorc89e39f2016-05-13 23:41:21 +030010428static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
10429{
10430 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
10431 struct drm_i915_private *dev_priv = to_i915(state->dev);
10432 const int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläa8ca4932016-05-13 23:41:23 +030010433 int vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +030010434 int cdclk;
10435
10436 /*
10437 * FIXME should also account for plane ratio
10438 * once 64bpp pixel formats are supported.
10439 */
Ville Syrjäläa8ca4932016-05-13 23:41:23 +030010440 cdclk = skl_calc_cdclk(max_pixclk, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +030010441
10442 /*
10443 * FIXME move the cdclk caclulation to
10444 * compute_config() so we can fail gracegully.
10445 */
10446 if (cdclk > dev_priv->max_cdclk_freq) {
10447 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10448 cdclk, dev_priv->max_cdclk_freq);
10449 cdclk = dev_priv->max_cdclk_freq;
10450 }
10451
10452 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10453 if (!intel_state->active_crtcs)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +030010454 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +030010455
10456 return 0;
10457}
10458
10459static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
10460{
Ville Syrjälä1cd593e2016-05-13 23:41:26 +030010461 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
10462 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
10463 unsigned int req_cdclk = intel_state->dev_cdclk;
10464 unsigned int req_vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +030010465
Ville Syrjälä1cd593e2016-05-13 23:41:26 +030010466 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +030010467}
10468
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +020010469static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
10470 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030010471{
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010472 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
Mika Kaholaaf3997b2016-02-05 13:29:28 +020010473 if (!intel_ddi_pll_select(crtc, crtc_state))
10474 return -EINVAL;
10475 }
Daniel Vetter716c2e52014-06-25 22:02:02 +030010476
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +030010477 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +020010478
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +020010479 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010480}
10481
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010482static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
10483 enum port port,
10484 struct intel_crtc_state *pipe_config)
10485{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010486 enum intel_dpll_id id;
10487
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010488 switch (port) {
10489 case PORT_A:
Imre Deak08250c42016-03-14 19:55:34 +020010490 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010491 break;
10492 case PORT_B:
Imre Deak08250c42016-03-14 19:55:34 +020010493 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010494 break;
10495 case PORT_C:
Imre Deak08250c42016-03-14 19:55:34 +020010496 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010497 break;
10498 default:
10499 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010500 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010501 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010502
10503 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010504}
10505
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010506static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
10507 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010508 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010509{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010510 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +020010511 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010512
10513 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010514 id = temp >> (port * 3 + 1);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010515
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010516 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010517 return;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010518
10519 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010520}
10521
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010522static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
10523 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010524 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010525{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010526 enum intel_dpll_id id;
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010527 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010528
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010529 switch (ddi_pll_sel) {
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010530 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010531 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010532 break;
10533 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010534 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010535 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +010010536 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010537 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +020010538 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +020010539 case PORT_CLK_SEL_LCPLL_810:
10540 id = DPLL_ID_LCPLL_810;
10541 break;
10542 case PORT_CLK_SEL_LCPLL_1350:
10543 id = DPLL_ID_LCPLL_1350;
10544 break;
10545 case PORT_CLK_SEL_LCPLL_2700:
10546 id = DPLL_ID_LCPLL_2700;
10547 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010548 default:
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070010549 MISSING_CASE(ddi_pll_sel);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010550 /* fall through */
10551 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010552 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010553 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010554
10555 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010556}
10557
Jani Nikulacf304292016-03-18 17:05:41 +020010558static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10559 struct intel_crtc_state *pipe_config,
10560 unsigned long *power_domain_mask)
10561{
10562 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010563 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulacf304292016-03-18 17:05:41 +020010564 enum intel_display_power_domain power_domain;
10565 u32 tmp;
10566
Imre Deakd9a7bc62016-05-12 16:18:50 +030010567 /*
10568 * The pipe->transcoder mapping is fixed with the exception of the eDP
10569 * transcoder handled below.
10570 */
Jani Nikulacf304292016-03-18 17:05:41 +020010571 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10572
10573 /*
10574 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10575 * consistency and less surprising code; it's in always on power).
10576 */
10577 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10578 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10579 enum pipe trans_edp_pipe;
10580 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10581 default:
10582 WARN(1, "unknown pipe linked to edp transcoder\n");
10583 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10584 case TRANS_DDI_EDP_INPUT_A_ON:
10585 trans_edp_pipe = PIPE_A;
10586 break;
10587 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10588 trans_edp_pipe = PIPE_B;
10589 break;
10590 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10591 trans_edp_pipe = PIPE_C;
10592 break;
10593 }
10594
10595 if (trans_edp_pipe == crtc->pipe)
10596 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10597 }
10598
10599 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10600 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10601 return false;
10602 *power_domain_mask |= BIT(power_domain);
10603
10604 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10605
10606 return tmp & PIPECONF_ENABLE;
10607}
10608
Jani Nikula4d1de972016-03-18 17:05:42 +020010609static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10610 struct intel_crtc_state *pipe_config,
10611 unsigned long *power_domain_mask)
10612{
10613 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010614 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020010615 enum intel_display_power_domain power_domain;
10616 enum port port;
10617 enum transcoder cpu_transcoder;
10618 u32 tmp;
10619
Jani Nikula4d1de972016-03-18 17:05:42 +020010620 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10621 if (port == PORT_A)
10622 cpu_transcoder = TRANSCODER_DSI_A;
10623 else
10624 cpu_transcoder = TRANSCODER_DSI_C;
10625
10626 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10627 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10628 continue;
10629 *power_domain_mask |= BIT(power_domain);
10630
Imre Deakdb18b6a2016-03-24 12:41:40 +020010631 /*
10632 * The PLL needs to be enabled with a valid divider
10633 * configuration, otherwise accessing DSI registers will hang
10634 * the machine. See BSpec North Display Engine
10635 * registers/MIPI[BXT]. We can break out here early, since we
10636 * need the same DSI PLL to be enabled for both DSI ports.
10637 */
10638 if (!intel_dsi_pll_is_enabled(dev_priv))
10639 break;
10640
Jani Nikula4d1de972016-03-18 17:05:42 +020010641 /* XXX: this works for video mode only */
10642 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10643 if (!(tmp & DPI_ENABLE))
10644 continue;
10645
10646 tmp = I915_READ(MIPI_CTRL(port));
10647 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10648 continue;
10649
10650 pipe_config->cpu_transcoder = cpu_transcoder;
Jani Nikula4d1de972016-03-18 17:05:42 +020010651 break;
10652 }
10653
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010654 return transcoder_is_dsi(pipe_config->cpu_transcoder);
Jani Nikula4d1de972016-03-18 17:05:42 +020010655}
10656
Daniel Vetter26804af2014-06-25 22:01:55 +030010657static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010658 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +030010659{
10660 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010661 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010662 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +030010663 enum port port;
10664 uint32_t tmp;
10665
10666 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10667
10668 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10669
Tvrtko Ursulin08537232016-10-13 11:03:02 +010010670 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010671 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +010010672 else if (IS_BROXTON(dev_priv))
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010673 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010674 else
10675 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010676
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010677 pll = pipe_config->shared_dpll;
10678 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010679 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10680 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010681 }
10682
Daniel Vetter26804af2014-06-25 22:01:55 +030010683 /*
10684 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10685 * DDI E. So just check whether this pipe is wired to DDI E and whether
10686 * the PCH transcoder is on.
10687 */
Damien Lespiauca370452013-12-03 13:56:24 +000010688 if (INTEL_INFO(dev)->gen < 9 &&
10689 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010690 pipe_config->has_pch_encoder = true;
10691
10692 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10693 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10694 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10695
10696 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10697 }
10698}
10699
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010700static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010701 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010702{
10703 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010704 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +020010705 enum intel_display_power_domain power_domain;
10706 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +020010707 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010708
Imre Deak17290502016-02-12 18:55:11 +020010709 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10710 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010711 return false;
Imre Deak17290502016-02-12 18:55:11 +020010712 power_domain_mask = BIT(power_domain);
10713
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010714 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010715
Jani Nikulacf304292016-03-18 17:05:41 +020010716 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +020010717
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010718 if (IS_BROXTON(dev_priv) &&
10719 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10720 WARN_ON(active);
10721 active = true;
Jani Nikula4d1de972016-03-18 17:05:42 +020010722 }
10723
Jani Nikulacf304292016-03-18 17:05:41 +020010724 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010725 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010726
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010727 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +020010728 haswell_get_ddi_port_state(crtc, pipe_config);
10729 intel_get_pipe_timings(crtc, pipe_config);
10730 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010731
Jani Nikulabc58be62016-03-18 17:05:39 +020010732 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010733
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010734 pipe_config->gamma_mode =
10735 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10736
Chandra Kondurua1b22782015-04-07 15:28:45 -070010737 if (INTEL_INFO(dev)->gen >= 9) {
10738 skl_init_scalers(dev, crtc, pipe_config);
10739 }
10740
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010741 if (INTEL_INFO(dev)->gen >= 9) {
10742 pipe_config->scaler_state.scaler_id = -1;
10743 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10744 }
10745
Imre Deak17290502016-02-12 18:55:11 +020010746 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10747 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10748 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010749 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010750 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010751 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010752 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010753 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010754
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010010755 if (IS_HASWELL(dev_priv))
Jesse Barnese59150d2014-01-07 13:30:45 -080010756 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10757 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010758
Jani Nikula4d1de972016-03-18 17:05:42 +020010759 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10760 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010761 pipe_config->pixel_multiplier =
10762 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10763 } else {
10764 pipe_config->pixel_multiplier = 1;
10765 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010766
Imre Deak17290502016-02-12 18:55:11 +020010767out:
10768 for_each_power_domain(power_domain, power_domain_mask)
10769 intel_display_power_put(dev_priv, power_domain);
10770
Jani Nikulacf304292016-03-18 17:05:41 +020010771 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010772}
10773
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010774static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10775 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010776{
10777 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010778 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010779 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010780 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010781
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010782 if (plane_state && plane_state->base.visible) {
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010783 unsigned int width = plane_state->base.crtc_w;
10784 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010785 unsigned int stride = roundup_pow_of_two(width) * 4;
10786
10787 switch (stride) {
10788 default:
10789 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10790 width, stride);
10791 stride = 256;
10792 /* fallthrough */
10793 case 256:
10794 case 512:
10795 case 1024:
10796 case 2048:
10797 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010798 }
10799
Ville Syrjälädc41c152014-08-13 11:57:05 +030010800 cntl |= CURSOR_ENABLE |
10801 CURSOR_GAMMA_ENABLE |
10802 CURSOR_FORMAT_ARGB |
10803 CURSOR_STRIDE(stride);
10804
10805 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010806 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010807
Ville Syrjälädc41c152014-08-13 11:57:05 +030010808 if (intel_crtc->cursor_cntl != 0 &&
10809 (intel_crtc->cursor_base != base ||
10810 intel_crtc->cursor_size != size ||
10811 intel_crtc->cursor_cntl != cntl)) {
10812 /* On these chipsets we can only modify the base/size/stride
10813 * whilst the cursor is disabled.
10814 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010815 I915_WRITE(CURCNTR(PIPE_A), 0);
10816 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010817 intel_crtc->cursor_cntl = 0;
10818 }
10819
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010820 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010821 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010822 intel_crtc->cursor_base = base;
10823 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010824
10825 if (intel_crtc->cursor_size != size) {
10826 I915_WRITE(CURSIZE, size);
10827 intel_crtc->cursor_size = size;
10828 }
10829
Chris Wilson4b0e3332014-05-30 16:35:26 +030010830 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010831 I915_WRITE(CURCNTR(PIPE_A), cntl);
10832 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010833 intel_crtc->cursor_cntl = cntl;
10834 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010835}
10836
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010837static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10838 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010839{
10840 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010841 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010842 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -020010843 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Lyude62e0fb82016-08-22 12:50:08 -040010844 const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -020010845 const struct skl_plane_wm *p_wm =
10846 &cstate->wm.skl.optimal.planes[PLANE_CURSOR];
Chris Wilson560b85b2010-08-07 11:01:38 +010010847 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010848 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010849
Lyude62e0fb82016-08-22 12:50:08 -040010850 if (INTEL_GEN(dev_priv) >= 9 && wm->dirty_pipes & drm_crtc_mask(crtc))
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -020010851 skl_write_cursor_wm(intel_crtc, p_wm, &wm->ddb);
Lyude62e0fb82016-08-22 12:50:08 -040010852
Ville Syrjälä936e71e2016-07-26 19:06:59 +030010853 if (plane_state && plane_state->base.visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010854 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010855 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010856 case 64:
10857 cntl |= CURSOR_MODE_64_ARGB_AX;
10858 break;
10859 case 128:
10860 cntl |= CURSOR_MODE_128_ARGB_AX;
10861 break;
10862 case 256:
10863 cntl |= CURSOR_MODE_256_ARGB_AX;
10864 break;
10865 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010866 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010867 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010868 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010869 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010870
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010010871 if (HAS_DDI(dev_priv))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010872 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010873
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +030010874 if (plane_state->base.rotation == DRM_ROTATE_180)
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010875 cntl |= CURSOR_ROTATE_180;
10876 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010877
Chris Wilson4b0e3332014-05-30 16:35:26 +030010878 if (intel_crtc->cursor_cntl != cntl) {
10879 I915_WRITE(CURCNTR(pipe), cntl);
10880 POSTING_READ(CURCNTR(pipe));
10881 intel_crtc->cursor_cntl = cntl;
10882 }
10883
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010884 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010885 I915_WRITE(CURBASE(pipe), base);
10886 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010887
10888 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010889}
10890
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010891/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010892static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010893 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010894{
10895 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010896 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010897 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10898 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010899 u32 base = intel_crtc->cursor_addr;
10900 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010901
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010902 if (plane_state) {
10903 int x = plane_state->base.crtc_x;
10904 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010905
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010906 if (x < 0) {
10907 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10908 x = -x;
10909 }
10910 pos |= x << CURSOR_X_SHIFT;
10911
10912 if (y < 0) {
10913 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10914 y = -y;
10915 }
10916 pos |= y << CURSOR_Y_SHIFT;
10917
10918 /* ILK+ do this automagically */
Tvrtko Ursulin49cff962016-10-13 11:02:54 +010010919 if (HAS_GMCH_DISPLAY(dev_priv) &&
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +030010920 plane_state->base.rotation == DRM_ROTATE_180) {
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010921 base += (plane_state->base.crtc_h *
10922 plane_state->base.crtc_w - 1) * 4;
10923 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010924 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010925
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010926 I915_WRITE(CURPOS(pipe), pos);
10927
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010928 if (IS_845G(dev_priv) || IS_I865G(dev_priv))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010929 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010930 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010931 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010932}
10933
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010934static bool cursor_size_ok(struct drm_i915_private *dev_priv,
Ville Syrjälädc41c152014-08-13 11:57:05 +030010935 uint32_t width, uint32_t height)
10936{
10937 if (width == 0 || height == 0)
10938 return false;
10939
10940 /*
10941 * 845g/865g are special in that they are only limited by
10942 * the width of their cursors, the height is arbitrary up to
10943 * the precision of the register. Everything else requires
10944 * square cursors, limited to a few power-of-two sizes.
10945 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010946 if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
Ville Syrjälädc41c152014-08-13 11:57:05 +030010947 if ((width & 63) != 0)
10948 return false;
10949
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010950 if (width > (IS_845G(dev_priv) ? 64 : 512))
Ville Syrjälädc41c152014-08-13 11:57:05 +030010951 return false;
10952
10953 if (height > 1023)
10954 return false;
10955 } else {
10956 switch (width | height) {
10957 case 256:
10958 case 128:
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010010959 if (IS_GEN2(dev_priv))
Ville Syrjälädc41c152014-08-13 11:57:05 +030010960 return false;
10961 case 64:
10962 break;
10963 default:
10964 return false;
10965 }
10966 }
10967
10968 return true;
10969}
10970
Jesse Barnes79e53942008-11-07 14:24:08 -080010971/* VESA 640x480x72Hz mode to set on the pipe */
10972static struct drm_display_mode load_detect_mode = {
10973 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10974 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10975};
10976
Daniel Vettera8bb6812014-02-10 18:00:39 +010010977struct drm_framebuffer *
10978__intel_framebuffer_create(struct drm_device *dev,
10979 struct drm_mode_fb_cmd2 *mode_cmd,
10980 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010981{
10982 struct intel_framebuffer *intel_fb;
10983 int ret;
10984
10985 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010986 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010987 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010988
10989 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010990 if (ret)
10991 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010992
10993 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010994
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010995err:
10996 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010997 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010998}
10999
Daniel Vetterb5ea6422014-03-02 21:18:00 +010011000static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010011001intel_framebuffer_create(struct drm_device *dev,
11002 struct drm_mode_fb_cmd2 *mode_cmd,
11003 struct drm_i915_gem_object *obj)
11004{
11005 struct drm_framebuffer *fb;
11006 int ret;
11007
11008 ret = i915_mutex_lock_interruptible(dev);
11009 if (ret)
11010 return ERR_PTR(ret);
11011 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
11012 mutex_unlock(&dev->struct_mutex);
11013
11014 return fb;
11015}
11016
Chris Wilsond2dff872011-04-19 08:36:26 +010011017static u32
11018intel_framebuffer_pitch_for_width(int width, int bpp)
11019{
11020 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
11021 return ALIGN(pitch, 64);
11022}
11023
11024static u32
11025intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
11026{
11027 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020011028 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010011029}
11030
11031static struct drm_framebuffer *
11032intel_framebuffer_create_for_mode(struct drm_device *dev,
11033 struct drm_display_mode *mode,
11034 int depth, int bpp)
11035{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020011036 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010011037 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000011038 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010011039
Dave Gordond37cd8a2016-04-22 19:14:32 +010011040 obj = i915_gem_object_create(dev,
Chris Wilsond2dff872011-04-19 08:36:26 +010011041 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +010011042 if (IS_ERR(obj))
11043 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010011044
11045 mode_cmd.width = mode->hdisplay;
11046 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011047 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
11048 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000011049 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010011050
Lukas Wunnerdcb13942015-07-04 11:50:58 +020011051 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
11052 if (IS_ERR(fb))
Chris Wilsonf0cd5182016-10-28 13:58:43 +010011053 i915_gem_object_put(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020011054
11055 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010011056}
11057
11058static struct drm_framebuffer *
11059mode_fits_in_fbdev(struct drm_device *dev,
11060 struct drm_display_mode *mode)
11061{
Daniel Vetter06957262015-08-10 13:34:08 +020011062#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsonfac5e232016-07-04 11:34:36 +010011063 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsond2dff872011-04-19 08:36:26 +010011064 struct drm_i915_gem_object *obj;
11065 struct drm_framebuffer *fb;
11066
Daniel Vetter4c0e5522014-02-14 16:35:54 +010011067 if (!dev_priv->fbdev)
11068 return NULL;
11069
11070 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010011071 return NULL;
11072
Jesse Barnes8bcd4552014-02-07 12:10:38 -080011073 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010011074 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010011075
Jesse Barnes8bcd4552014-02-07 12:10:38 -080011076 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011077 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
11078 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010011079 return NULL;
11080
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011081 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010011082 return NULL;
11083
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011084 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010011085 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020011086#else
11087 return NULL;
11088#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010011089}
11090
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030011091static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
11092 struct drm_crtc *crtc,
11093 struct drm_display_mode *mode,
11094 struct drm_framebuffer *fb,
11095 int x, int y)
11096{
11097 struct drm_plane_state *plane_state;
11098 int hdisplay, vdisplay;
11099 int ret;
11100
11101 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
11102 if (IS_ERR(plane_state))
11103 return PTR_ERR(plane_state);
11104
11105 if (mode)
11106 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
11107 else
11108 hdisplay = vdisplay = 0;
11109
11110 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
11111 if (ret)
11112 return ret;
11113 drm_atomic_set_fb_for_plane(plane_state, fb);
11114 plane_state->crtc_x = 0;
11115 plane_state->crtc_y = 0;
11116 plane_state->crtc_w = hdisplay;
11117 plane_state->crtc_h = vdisplay;
11118 plane_state->src_x = x << 16;
11119 plane_state->src_y = y << 16;
11120 plane_state->src_w = hdisplay << 16;
11121 plane_state->src_h = vdisplay << 16;
11122
11123 return 0;
11124}
11125
Daniel Vetterd2434ab2012-08-12 21:20:10 +020011126bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010011127 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050011128 struct intel_load_detect_pipe *old,
11129 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080011130{
11131 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020011132 struct intel_encoder *intel_encoder =
11133 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080011134 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010011135 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080011136 struct drm_crtc *crtc = NULL;
11137 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020011138 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050011139 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011140 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020011141 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030011142 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050011143 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080011144
Chris Wilsond2dff872011-04-19 08:36:26 +010011145 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030011146 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030011147 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010011148
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011149 old->restore_state = NULL;
11150
Rob Clark51fd3712013-11-19 12:10:12 -050011151retry:
11152 ret = drm_modeset_lock(&config->connection_mutex, ctx);
11153 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011154 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020011155
Jesse Barnes79e53942008-11-07 14:24:08 -080011156 /*
11157 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010011158 *
Jesse Barnes79e53942008-11-07 14:24:08 -080011159 * - if the connector already has an assigned crtc, use it (but make
11160 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010011161 *
Jesse Barnes79e53942008-11-07 14:24:08 -080011162 * - try to find the first unused crtc that can drive this connector,
11163 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080011164 */
11165
11166 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011167 if (connector->state->crtc) {
11168 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010011169
Rob Clark51fd3712013-11-19 12:10:12 -050011170 ret = drm_modeset_lock(&crtc->mutex, ctx);
11171 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011172 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010011173
11174 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011175 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080011176 }
11177
11178 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010011179 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080011180 i++;
11181 if (!(encoder->possible_crtcs & (1 << i)))
11182 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011183
11184 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
11185 if (ret)
11186 goto fail;
11187
11188 if (possible_crtc->state->enable) {
11189 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030011190 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011191 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030011192
11193 crtc = possible_crtc;
11194 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080011195 }
11196
11197 /*
11198 * If we didn't find an unused CRTC, don't use any.
11199 */
11200 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010011201 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011202 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011203 }
11204
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011205found:
11206 intel_crtc = to_intel_crtc(crtc);
11207
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010011208 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
11209 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011210 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011211
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011212 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011213 restore_state = drm_atomic_state_alloc(dev);
11214 if (!state || !restore_state) {
11215 ret = -ENOMEM;
11216 goto fail;
11217 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011218
11219 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011220 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011221
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020011222 connector_state = drm_atomic_get_connector_state(state, connector);
11223 if (IS_ERR(connector_state)) {
11224 ret = PTR_ERR(connector_state);
11225 goto fail;
11226 }
11227
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011228 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
11229 if (ret)
11230 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020011231
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030011232 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
11233 if (IS_ERR(crtc_state)) {
11234 ret = PTR_ERR(crtc_state);
11235 goto fail;
11236 }
11237
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020011238 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030011239
Chris Wilson64927112011-04-20 07:25:26 +010011240 if (!mode)
11241 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080011242
Chris Wilsond2dff872011-04-19 08:36:26 +010011243 /* We need a framebuffer large enough to accommodate all accesses
11244 * that the plane may generate whilst we perform load detection.
11245 * We can not rely on the fbcon either being present (we get called
11246 * during its initialisation to detect all boot displays, or it may
11247 * not even exist) or that it is large enough to satisfy the
11248 * requested mode.
11249 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020011250 fb = mode_fits_in_fbdev(dev, mode);
11251 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010011252 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020011253 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010011254 } else
11255 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020011256 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010011257 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020011258 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011259 }
Chris Wilsond2dff872011-04-19 08:36:26 +010011260
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030011261 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
11262 if (ret)
11263 goto fail;
11264
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011265 drm_framebuffer_unreference(fb);
11266
11267 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
11268 if (ret)
11269 goto fail;
11270
11271 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
11272 if (!ret)
11273 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
11274 if (!ret)
11275 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
11276 if (ret) {
11277 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
11278 goto fail;
11279 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030011280
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010011281 ret = drm_atomic_commit(state);
11282 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010011283 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020011284 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011285 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011286
11287 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010011288
Jesse Barnes79e53942008-11-07 14:24:08 -080011289 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070011290 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010011291 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020011292
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020011293fail:
Chris Wilson7fb71c82016-10-19 12:37:43 +010011294 if (state) {
11295 drm_atomic_state_put(state);
11296 state = NULL;
11297 }
11298 if (restore_state) {
11299 drm_atomic_state_put(restore_state);
11300 restore_state = NULL;
11301 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011302
Rob Clark51fd3712013-11-19 12:10:12 -050011303 if (ret == -EDEADLK) {
11304 drm_modeset_backoff(ctx);
11305 goto retry;
11306 }
11307
Ville Syrjälä412b61d2014-01-17 15:59:39 +020011308 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080011309}
11310
Daniel Vetterd2434ab2012-08-12 21:20:10 +020011311void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020011312 struct intel_load_detect_pipe *old,
11313 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080011314{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020011315 struct intel_encoder *intel_encoder =
11316 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010011317 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011318 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030011319 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080011320
Chris Wilsond2dff872011-04-19 08:36:26 +010011321 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030011322 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030011323 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010011324
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011325 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010011326 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011327
11328 ret = drm_atomic_commit(state);
Chris Wilson08536952016-10-14 13:18:18 +010011329 if (ret)
Maarten Lankhorstedde3612016-02-17 09:18:35 +010011330 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
Chris Wilson08536952016-10-14 13:18:18 +010011331 drm_atomic_state_put(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080011332}
11333
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011334static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011335 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011336{
Chris Wilsonfac5e232016-07-04 11:34:36 +010011337 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011338 u32 dpll = pipe_config->dpll_hw_state.dpll;
11339
11340 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020011341 return dev_priv->vbt.lvds_ssc_freq;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010011342 else if (HAS_PCH_SPLIT(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011343 return 120000;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011344 else if (!IS_GEN2(dev_priv))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011345 return 96000;
11346 else
11347 return 48000;
11348}
11349
Jesse Barnes79e53942008-11-07 14:24:08 -080011350/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011351static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011352 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080011353{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011354 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011355 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011356 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030011357 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080011358 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030011359 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030011360 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011361 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080011362
11363 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030011364 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080011365 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030011366 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080011367
11368 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050011369 if (IS_PINEVIEW(dev)) {
11370 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
11371 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080011372 } else {
11373 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
11374 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
11375 }
11376
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011377 if (!IS_GEN2(dev_priv)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050011378 if (IS_PINEVIEW(dev))
11379 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
11380 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080011381 else
11382 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080011383 DPLL_FPA01_P1_POST_DIV_SHIFT);
11384
11385 switch (dpll & DPLL_MODE_MASK) {
11386 case DPLLB_MODE_DAC_SERIAL:
11387 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
11388 5 : 10;
11389 break;
11390 case DPLLB_MODE_LVDS:
11391 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
11392 7 : 14;
11393 break;
11394 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080011395 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080011396 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011397 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080011398 }
11399
Daniel Vetterac58c3f2013-06-01 17:16:17 +020011400 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030011401 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020011402 else
Imre Deakdccbea32015-06-22 23:35:51 +030011403 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080011404 } else {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010011405 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020011406 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080011407
11408 if (is_lvds) {
11409 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
11410 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020011411
11412 if (lvds & LVDS_CLKB_POWER_UP)
11413 clock.p2 = 7;
11414 else
11415 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080011416 } else {
11417 if (dpll & PLL_P1_DIVIDE_BY_TWO)
11418 clock.p1 = 2;
11419 else {
11420 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
11421 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
11422 }
11423 if (dpll & PLL_P2_DIVIDE_BY_4)
11424 clock.p2 = 4;
11425 else
11426 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080011427 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030011428
Imre Deakdccbea32015-06-22 23:35:51 +030011429 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080011430 }
11431
Ville Syrjälä18442d02013-09-13 16:00:08 +030011432 /*
11433 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010011434 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030011435 * encoder's get_config() function.
11436 */
Imre Deakdccbea32015-06-22 23:35:51 +030011437 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011438}
11439
Ville Syrjälä6878da02013-09-13 15:59:11 +030011440int intel_dotclock_calculate(int link_freq,
11441 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011442{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011443 /*
11444 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030011445 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011446 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030011447 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011448 *
11449 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030011450 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080011451 */
11452
Ville Syrjälä6878da02013-09-13 15:59:11 +030011453 if (!m_n->link_n)
11454 return 0;
11455
11456 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
11457}
11458
Ville Syrjälä18442d02013-09-13 16:00:08 +030011459static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011460 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030011461{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011462 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030011463
11464 /* read out port_clock from the DPLL */
11465 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030011466
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011467 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020011468 * In case there is an active pipe without active ports,
11469 * we may need some idea for the dotclock anyway.
11470 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011471 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011472 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020011473 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030011474 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080011475}
11476
11477/** Returns the currently programmed mode of the given pipe. */
11478struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
11479 struct drm_crtc *crtc)
11480{
Chris Wilsonfac5e232016-07-04 11:34:36 +010011481 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011482 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011483 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080011484 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011485 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020011486 int htot = I915_READ(HTOTAL(cpu_transcoder));
11487 int hsync = I915_READ(HSYNC(cpu_transcoder));
11488 int vtot = I915_READ(VTOTAL(cpu_transcoder));
11489 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030011490 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080011491
11492 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
11493 if (!mode)
11494 return NULL;
11495
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011496 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
11497 if (!pipe_config) {
11498 kfree(mode);
11499 return NULL;
11500 }
11501
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011502 /*
11503 * Construct a pipe_config sufficient for getting the clock info
11504 * back out of crtc_clock_get.
11505 *
11506 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
11507 * to use a real value here instead.
11508 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011509 pipe_config->cpu_transcoder = (enum transcoder) pipe;
11510 pipe_config->pixel_multiplier = 1;
11511 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
11512 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
11513 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
11514 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011515
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011516 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080011517 mode->hdisplay = (htot & 0xffff) + 1;
11518 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
11519 mode->hsync_start = (hsync & 0xffff) + 1;
11520 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
11521 mode->vdisplay = (vtot & 0xffff) + 1;
11522 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
11523 mode->vsync_start = (vsync & 0xffff) + 1;
11524 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
11525
11526 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080011527
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000011528 kfree(pipe_config);
11529
Jesse Barnes79e53942008-11-07 14:24:08 -080011530 return mode;
11531}
11532
11533static void intel_crtc_destroy(struct drm_crtc *crtc)
11534{
11535 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011536 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011537 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020011538
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011539 spin_lock_irq(&dev->event_lock);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011540 work = intel_crtc->flip_work;
11541 intel_crtc->flip_work = NULL;
11542 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011543
Daniel Vetter5a21b662016-05-24 17:13:53 +020011544 if (work) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011545 cancel_work_sync(&work->mmio_work);
11546 cancel_work_sync(&work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011547 kfree(work);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011548 }
Jesse Barnes79e53942008-11-07 14:24:08 -080011549
11550 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011551
Jesse Barnes79e53942008-11-07 14:24:08 -080011552 kfree(intel_crtc);
11553}
11554
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011555static void intel_unpin_work_fn(struct work_struct *__work)
11556{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011557 struct intel_flip_work *work =
11558 container_of(__work, struct intel_flip_work, unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011559 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11560 struct drm_device *dev = crtc->base.dev;
11561 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011562
Daniel Vetter5a21b662016-05-24 17:13:53 +020011563 if (is_mmio_work(work))
11564 flush_work(&work->mmio_work);
11565
11566 mutex_lock(&dev->struct_mutex);
11567 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilsonf8c417c2016-07-20 13:31:53 +010011568 i915_gem_object_put(work->pending_flip_obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011569 mutex_unlock(&dev->struct_mutex);
11570
Chris Wilsone8a261e2016-07-20 13:31:49 +010011571 i915_gem_request_put(work->flip_queued_req);
11572
Chris Wilson5748b6a2016-08-04 16:32:38 +010011573 intel_frontbuffer_flip_complete(to_i915(dev),
11574 to_intel_plane(primary)->frontbuffer_bit);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011575 intel_fbc_post_update(crtc);
11576 drm_framebuffer_unreference(work->old_fb);
11577
11578 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11579 atomic_dec(&crtc->unpin_work_count);
11580
11581 kfree(work);
11582}
11583
11584/* Is 'a' after or equal to 'b'? */
11585static bool g4x_flip_count_after_eq(u32 a, u32 b)
11586{
11587 return !((a - b) & 0x80000000);
11588}
11589
11590static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11591 struct intel_flip_work *work)
11592{
11593 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011594 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011595
Chris Wilson8af29b02016-09-09 14:11:47 +010011596 if (abort_flip_on_reset(crtc))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011597 return true;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011598
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011599 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011600 * The relevant registers doen't exist on pre-ctg.
11601 * As the flip done interrupt doesn't trigger for mmio
11602 * flips on gmch platforms, a flip count check isn't
11603 * really needed there. But since ctg has the registers,
11604 * include it in the check anyway.
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011605 */
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010011606 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011607 return true;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011608
Daniel Vetter5a21b662016-05-24 17:13:53 +020011609 /*
11610 * BDW signals flip done immediately if the plane
11611 * is disabled, even if the plane enable is already
11612 * armed to occur at the next vblank :(
11613 */
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011614
Daniel Vetter5a21b662016-05-24 17:13:53 +020011615 /*
11616 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11617 * used the same base address. In that case the mmio flip might
11618 * have completed, but the CS hasn't even executed the flip yet.
11619 *
11620 * A flip count check isn't enough as the CS might have updated
11621 * the base address just after start of vblank, but before we
11622 * managed to process the interrupt. This means we'd complete the
11623 * CS flip too soon.
11624 *
11625 * Combining both checks should get us a good enough result. It may
11626 * still happen that the CS flip has been executed, but has not
11627 * yet actually completed. But in case the base address is the same
11628 * anyway, we don't really care.
11629 */
11630 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11631 crtc->flip_work->gtt_offset &&
11632 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11633 crtc->flip_work->flip_count);
11634}
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011635
Daniel Vetter5a21b662016-05-24 17:13:53 +020011636static bool
11637__pageflip_finished_mmio(struct intel_crtc *crtc,
11638 struct intel_flip_work *work)
11639{
11640 /*
11641 * MMIO work completes when vblank is different from
11642 * flip_queued_vblank.
11643 *
11644 * Reset counter value doesn't matter, this is handled by
11645 * i915_wait_request finishing early, so no need to handle
11646 * reset here.
11647 */
11648 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011649}
11650
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011651
11652static bool pageflip_finished(struct intel_crtc *crtc,
11653 struct intel_flip_work *work)
11654{
11655 if (!atomic_read(&work->pending))
11656 return false;
11657
11658 smp_rmb();
11659
Daniel Vetter5a21b662016-05-24 17:13:53 +020011660 if (is_mmio_work(work))
11661 return __pageflip_finished_mmio(crtc, work);
11662 else
11663 return __pageflip_finished_cs(crtc, work);
11664}
11665
11666void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11667{
Chris Wilson91c8a322016-07-05 10:40:23 +010011668 struct drm_device *dev = &dev_priv->drm;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011669 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11670 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11671 struct intel_flip_work *work;
11672 unsigned long flags;
11673
11674 /* Ignore early vblank irqs */
11675 if (!crtc)
11676 return;
11677
Daniel Vetterf3260382014-09-15 14:55:23 +020011678 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011679 * This is called both by irq handlers and the reset code (to complete
11680 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000011681 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011682 spin_lock_irqsave(&dev->event_lock, flags);
11683 work = intel_crtc->flip_work;
11684
11685 if (work != NULL &&
11686 !is_mmio_work(work) &&
11687 pageflip_finished(intel_crtc, work))
11688 page_flip_completed(intel_crtc);
11689
11690 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011691}
11692
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011693void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011694{
Chris Wilson91c8a322016-07-05 10:40:23 +010011695 struct drm_device *dev = &dev_priv->drm;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011696 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11697 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11698 struct intel_flip_work *work;
11699 unsigned long flags;
11700
11701 /* Ignore early vblank irqs */
11702 if (!crtc)
11703 return;
11704
11705 /*
11706 * This is called both by irq handlers and the reset code (to complete
11707 * lost pageflips) so needs the full irqsave spinlocks.
11708 */
11709 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011710 work = intel_crtc->flip_work;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011711
Daniel Vetter5a21b662016-05-24 17:13:53 +020011712 if (work != NULL &&
11713 is_mmio_work(work) &&
11714 pageflip_finished(intel_crtc, work))
11715 page_flip_completed(intel_crtc);
Maarten Lankhorst68858432016-05-17 15:07:52 +020011716
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011717 spin_unlock_irqrestore(&dev->event_lock, flags);
11718}
11719
Daniel Vetter5a21b662016-05-24 17:13:53 +020011720static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11721 struct intel_flip_work *work)
11722{
11723 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
11724
11725 /* Ensure that the work item is consistent when activating it ... */
11726 smp_mb__before_atomic();
11727 atomic_set(&work->pending, 1);
11728}
11729
11730static int intel_gen2_queue_flip(struct drm_device *dev,
11731 struct drm_crtc *crtc,
11732 struct drm_framebuffer *fb,
11733 struct drm_i915_gem_object *obj,
11734 struct drm_i915_gem_request *req,
11735 uint32_t flags)
11736{
Chris Wilson7e37f882016-08-02 22:50:21 +010011737 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011738 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11739 u32 flip_mask;
11740 int ret;
11741
11742 ret = intel_ring_begin(req, 6);
11743 if (ret)
11744 return ret;
11745
11746 /* Can't queue multiple flips, so wait for the previous
11747 * one to finish before executing the next.
11748 */
11749 if (intel_crtc->plane)
11750 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11751 else
11752 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011753 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11754 intel_ring_emit(ring, MI_NOOP);
11755 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011756 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011757 intel_ring_emit(ring, fb->pitches[0]);
11758 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11759 intel_ring_emit(ring, 0); /* aux display base address, unused */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011760
11761 return 0;
11762}
11763
11764static int intel_gen3_queue_flip(struct drm_device *dev,
11765 struct drm_crtc *crtc,
11766 struct drm_framebuffer *fb,
11767 struct drm_i915_gem_object *obj,
11768 struct drm_i915_gem_request *req,
11769 uint32_t flags)
11770{
Chris Wilson7e37f882016-08-02 22:50:21 +010011771 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11773 u32 flip_mask;
11774 int ret;
11775
11776 ret = intel_ring_begin(req, 6);
11777 if (ret)
11778 return ret;
11779
11780 if (intel_crtc->plane)
11781 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11782 else
11783 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011784 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11785 intel_ring_emit(ring, MI_NOOP);
11786 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011787 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011788 intel_ring_emit(ring, fb->pitches[0]);
11789 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11790 intel_ring_emit(ring, MI_NOOP);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011791
11792 return 0;
11793}
11794
11795static int intel_gen4_queue_flip(struct drm_device *dev,
11796 struct drm_crtc *crtc,
11797 struct drm_framebuffer *fb,
11798 struct drm_i915_gem_object *obj,
11799 struct drm_i915_gem_request *req,
11800 uint32_t flags)
11801{
Chris Wilson7e37f882016-08-02 22:50:21 +010011802 struct intel_ring *ring = req->ring;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011803 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011804 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11805 uint32_t pf, pipesrc;
11806 int ret;
11807
11808 ret = intel_ring_begin(req, 4);
11809 if (ret)
11810 return ret;
11811
11812 /* i965+ uses the linear or tiled offsets from the
11813 * Display Registers (which do not change across a page-flip)
11814 * so we need only reprogram the base address.
11815 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010011816 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011817 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011818 intel_ring_emit(ring, fb->pitches[0]);
11819 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
Ville Syrjälä72618eb2016-02-04 20:38:20 +020011820 intel_fb_modifier_to_tiling(fb->modifier[0]));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011821
11822 /* XXX Enabling the panel-fitter across page-flip is so far
11823 * untested on non-native modes, so ignore it for now.
11824 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11825 */
11826 pf = 0;
11827 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011828 intel_ring_emit(ring, pf | pipesrc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011829
11830 return 0;
11831}
11832
11833static int intel_gen6_queue_flip(struct drm_device *dev,
11834 struct drm_crtc *crtc,
11835 struct drm_framebuffer *fb,
11836 struct drm_i915_gem_object *obj,
11837 struct drm_i915_gem_request *req,
11838 uint32_t flags)
11839{
Chris Wilson7e37f882016-08-02 22:50:21 +010011840 struct intel_ring *ring = req->ring;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011841 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011842 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11843 uint32_t pf, pipesrc;
11844 int ret;
11845
11846 ret = intel_ring_begin(req, 4);
11847 if (ret)
11848 return ret;
11849
Chris Wilsonb5321f32016-08-02 22:50:18 +010011850 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011851 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä72618eb2016-02-04 20:38:20 +020011852 intel_ring_emit(ring, fb->pitches[0] |
11853 intel_fb_modifier_to_tiling(fb->modifier[0]));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011854 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011855
11856 /* Contrary to the suggestions in the documentation,
11857 * "Enable Panel Fitter" does not seem to be required when page
11858 * flipping with a non-native mode, and worse causes a normal
11859 * modeset to fail.
11860 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11861 */
11862 pf = 0;
11863 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011864 intel_ring_emit(ring, pf | pipesrc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011865
11866 return 0;
11867}
11868
11869static int intel_gen7_queue_flip(struct drm_device *dev,
11870 struct drm_crtc *crtc,
11871 struct drm_framebuffer *fb,
11872 struct drm_i915_gem_object *obj,
11873 struct drm_i915_gem_request *req,
11874 uint32_t flags)
11875{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011876 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson7e37f882016-08-02 22:50:21 +010011877 struct intel_ring *ring = req->ring;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011878 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11879 uint32_t plane_bit = 0;
11880 int len, ret;
11881
11882 switch (intel_crtc->plane) {
11883 case PLANE_A:
11884 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11885 break;
11886 case PLANE_B:
11887 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11888 break;
11889 case PLANE_C:
11890 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11891 break;
11892 default:
11893 WARN_ONCE(1, "unknown plane in flip command\n");
11894 return -ENODEV;
11895 }
11896
11897 len = 4;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011898 if (req->engine->id == RCS) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020011899 len += 6;
11900 /*
11901 * On Gen 8, SRM is now taking an extra dword to accommodate
11902 * 48bits addresses, and we need a NOOP for the batch size to
11903 * stay even.
11904 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011905 if (IS_GEN8(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011906 len += 2;
11907 }
11908
11909 /*
11910 * BSpec MI_DISPLAY_FLIP for IVB:
11911 * "The full packet must be contained within the same cache line."
11912 *
11913 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11914 * cacheline, if we ever start emitting more commands before
11915 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11916 * then do the cacheline alignment, and finally emit the
11917 * MI_DISPLAY_FLIP.
11918 */
11919 ret = intel_ring_cacheline_align(req);
11920 if (ret)
11921 return ret;
11922
11923 ret = intel_ring_begin(req, len);
11924 if (ret)
11925 return ret;
11926
11927 /* Unmask the flip-done completion message. Note that the bspec says that
11928 * we should do this for both the BCS and RCS, and that we must not unmask
11929 * more than one flip event at any time (or ensure that one flip message
11930 * can be sent by waiting for flip-done prior to queueing new flips).
11931 * Experimentation says that BCS works despite DERRMR masking all
11932 * flip-done completion events and that unmasking all planes at once
11933 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11934 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11935 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010011936 if (req->engine->id == RCS) {
11937 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11938 intel_ring_emit_reg(ring, DERRMR);
11939 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011940 DERRMR_PIPEB_PRI_FLIP_DONE |
11941 DERRMR_PIPEC_PRI_FLIP_DONE));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011942 if (IS_GEN8(dev_priv))
Chris Wilsonb5321f32016-08-02 22:50:18 +010011943 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011944 MI_SRM_LRM_GLOBAL_GTT);
11945 else
Chris Wilsonb5321f32016-08-02 22:50:18 +010011946 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011947 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonb5321f32016-08-02 22:50:18 +010011948 intel_ring_emit_reg(ring, DERRMR);
Chris Wilsonbde13eb2016-08-15 10:49:07 +010011949 intel_ring_emit(ring,
11950 i915_ggtt_offset(req->engine->scratch) + 256);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010011951 if (IS_GEN8(dev_priv)) {
Chris Wilsonb5321f32016-08-02 22:50:18 +010011952 intel_ring_emit(ring, 0);
11953 intel_ring_emit(ring, MI_NOOP);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011954 }
11955 }
11956
Chris Wilsonb5321f32016-08-02 22:50:18 +010011957 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä72618eb2016-02-04 20:38:20 +020011958 intel_ring_emit(ring, fb->pitches[0] |
11959 intel_fb_modifier_to_tiling(fb->modifier[0]));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011960 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11961 intel_ring_emit(ring, (MI_NOOP));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011962
11963 return 0;
11964}
11965
11966static bool use_mmio_flip(struct intel_engine_cs *engine,
11967 struct drm_i915_gem_object *obj)
11968{
11969 /*
11970 * This is not being used for older platforms, because
11971 * non-availability of flip done interrupt forces us to use
11972 * CS flips. Older platforms derive flip done using some clever
11973 * tricks involving the flip_pending status bits and vblank irqs.
11974 * So using MMIO flips there would disrupt this mechanism.
11975 */
11976
11977 if (engine == NULL)
11978 return true;
11979
11980 if (INTEL_GEN(engine->i915) < 5)
11981 return false;
11982
11983 if (i915.use_mmio_flip < 0)
11984 return false;
11985 else if (i915.use_mmio_flip > 0)
11986 return true;
11987 else if (i915.enable_execlists)
11988 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011989
Chris Wilsond07f0e52016-10-28 13:58:44 +010011990 return engine != i915_gem_object_last_write_engine(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011991}
11992
11993static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11994 unsigned int rotation,
11995 struct intel_flip_work *work)
11996{
11997 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011998 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011999 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
12000 const enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd2196772016-01-28 18:33:11 +020012001 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012002
12003 ctl = I915_READ(PLANE_CTL(pipe, 0));
12004 ctl &= ~PLANE_CTL_TILED_MASK;
12005 switch (fb->modifier[0]) {
12006 case DRM_FORMAT_MOD_NONE:
12007 break;
12008 case I915_FORMAT_MOD_X_TILED:
12009 ctl |= PLANE_CTL_TILED_X;
12010 break;
12011 case I915_FORMAT_MOD_Y_TILED:
12012 ctl |= PLANE_CTL_TILED_Y;
12013 break;
12014 case I915_FORMAT_MOD_Yf_TILED:
12015 ctl |= PLANE_CTL_TILED_YF;
12016 break;
12017 default:
12018 MISSING_CASE(fb->modifier[0]);
12019 }
12020
12021 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020012022 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
12023 * PLANE_SURF updates, the update is then guaranteed to be atomic.
12024 */
12025 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
12026 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
12027
12028 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
12029 POSTING_READ(PLANE_SURF(pipe, 0));
12030}
12031
12032static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
12033 struct intel_flip_work *work)
12034{
12035 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012036 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä72618eb2016-02-04 20:38:20 +020012037 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012038 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
12039 u32 dspcntr;
12040
12041 dspcntr = I915_READ(reg);
12042
Ville Syrjälä72618eb2016-02-04 20:38:20 +020012043 if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
Daniel Vetter5a21b662016-05-24 17:13:53 +020012044 dspcntr |= DISPPLANE_TILED;
12045 else
12046 dspcntr &= ~DISPPLANE_TILED;
12047
12048 I915_WRITE(reg, dspcntr);
12049
12050 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
12051 POSTING_READ(DSPSURF(intel_crtc->plane));
12052}
12053
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020012054static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000012055{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020012056 struct intel_flip_work *work =
12057 container_of(w, struct intel_flip_work, mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012058 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
12059 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12060 struct intel_framebuffer *intel_fb =
12061 to_intel_framebuffer(crtc->base.primary->fb);
12062 struct drm_i915_gem_object *obj = intel_fb->obj;
12063
Chris Wilsond07f0e52016-10-28 13:58:44 +010012064 WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012065
12066 intel_pipe_update_start(crtc);
12067
12068 if (INTEL_GEN(dev_priv) >= 9)
12069 skl_do_mmio_flip(crtc, work->rotation, work);
12070 else
12071 /* use_mmio_flip() retricts MMIO flips to ilk+ */
12072 ilk_do_mmio_flip(crtc, work);
12073
12074 intel_pipe_update_end(crtc, work);
12075}
12076
12077static int intel_default_queue_flip(struct drm_device *dev,
12078 struct drm_crtc *crtc,
12079 struct drm_framebuffer *fb,
12080 struct drm_i915_gem_object *obj,
12081 struct drm_i915_gem_request *req,
12082 uint32_t flags)
12083{
12084 return -ENODEV;
12085}
12086
12087static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
12088 struct intel_crtc *intel_crtc,
12089 struct intel_flip_work *work)
12090{
12091 u32 addr, vblank;
12092
12093 if (!atomic_read(&work->pending))
12094 return false;
12095
12096 smp_rmb();
12097
12098 vblank = intel_crtc_get_vblank_counter(intel_crtc);
12099 if (work->flip_ready_vblank == 0) {
12100 if (work->flip_queued_req &&
Chris Wilsonf69a02c2016-07-01 17:23:16 +010012101 !i915_gem_request_completed(work->flip_queued_req))
Daniel Vetter5a21b662016-05-24 17:13:53 +020012102 return false;
12103
12104 work->flip_ready_vblank = vblank;
12105 }
12106
12107 if (vblank - work->flip_ready_vblank < 3)
12108 return false;
12109
12110 /* Potential stall - if we see that the flip has happened,
12111 * assume a missed interrupt. */
12112 if (INTEL_GEN(dev_priv) >= 4)
12113 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
12114 else
12115 addr = I915_READ(DSPADDR(intel_crtc->plane));
12116
12117 /* There is a potential issue here with a false positive after a flip
12118 * to the same address. We could address this by checking for a
12119 * non-incrementing frame counter.
12120 */
12121 return addr == work->gtt_offset;
12122}
12123
12124void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
12125{
Chris Wilson91c8a322016-07-05 10:40:23 +010012126 struct drm_device *dev = &dev_priv->drm;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012127 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020012128 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012129 struct intel_flip_work *work;
12130
12131 WARN_ON(!in_interrupt());
12132
12133 if (crtc == NULL)
12134 return;
12135
12136 spin_lock(&dev->event_lock);
12137 work = intel_crtc->flip_work;
12138
12139 if (work != NULL && !is_mmio_work(work) &&
12140 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
12141 WARN_ONCE(1,
12142 "Kicking stuck page flip: queued at %d, now %d\n",
12143 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
12144 page_flip_completed(intel_crtc);
12145 work = NULL;
12146 }
12147
12148 if (work != NULL && !is_mmio_work(work) &&
12149 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
12150 intel_queue_rps_boost_for_request(work->flip_queued_req);
12151 spin_unlock(&dev->event_lock);
12152}
12153
12154static int intel_crtc_page_flip(struct drm_crtc *crtc,
12155 struct drm_framebuffer *fb,
12156 struct drm_pending_vblank_event *event,
12157 uint32_t page_flip_flags)
12158{
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020012159 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012160 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012161 struct drm_framebuffer *old_fb = crtc->primary->fb;
12162 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12163 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12164 struct drm_plane *primary = crtc->primary;
12165 enum pipe pipe = intel_crtc->pipe;
12166 struct intel_flip_work *work;
12167 struct intel_engine_cs *engine;
12168 bool mmio_flip;
Chris Wilson8e637172016-08-02 22:50:26 +010012169 struct drm_i915_gem_request *request;
Chris Wilson058d88c2016-08-15 10:49:06 +010012170 struct i915_vma *vma;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012171 int ret;
Sourab Gupta84c33a62014-06-02 16:47:17 +053012172
Daniel Vetter5a21b662016-05-24 17:13:53 +020012173 /*
12174 * drm_mode_page_flip_ioctl() should already catch this, but double
12175 * check to be safe. In the future we may enable pageflipping from
12176 * a disabled primary plane.
12177 */
12178 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
12179 return -EBUSY;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020012180
Daniel Vetter5a21b662016-05-24 17:13:53 +020012181 /* Can't change pixel format via MI display flips. */
12182 if (fb->pixel_format != crtc->primary->fb->pixel_format)
12183 return -EINVAL;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020012184
Daniel Vetter5a21b662016-05-24 17:13:53 +020012185 /*
12186 * TILEOFF/LINOFF registers can't be changed via MI display flips.
12187 * Note that pitch changes could also affect these register.
12188 */
12189 if (INTEL_INFO(dev)->gen > 3 &&
12190 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
12191 fb->pitches[0] != crtc->primary->fb->pitches[0]))
12192 return -EINVAL;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020012193
Daniel Vetter5a21b662016-05-24 17:13:53 +020012194 if (i915_terminally_wedged(&dev_priv->gpu_error))
12195 goto out_hang;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020012196
Daniel Vetter5a21b662016-05-24 17:13:53 +020012197 work = kzalloc(sizeof(*work), GFP_KERNEL);
12198 if (work == NULL)
12199 return -ENOMEM;
12200
12201 work->event = event;
12202 work->crtc = crtc;
12203 work->old_fb = old_fb;
12204 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
Sourab Gupta84c33a62014-06-02 16:47:17 +053012205
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012206 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012207 if (ret)
12208 goto free_work;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012209
Daniel Vetter5a21b662016-05-24 17:13:53 +020012210 /* We borrow the event spin lock for protecting flip_work */
12211 spin_lock_irq(&dev->event_lock);
12212 if (intel_crtc->flip_work) {
12213 /* Before declaring the flip queue wedged, check if
12214 * the hardware completed the operation behind our backs.
12215 */
12216 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
12217 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
12218 page_flip_completed(intel_crtc);
12219 } else {
12220 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
12221 spin_unlock_irq(&dev->event_lock);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020012222
Daniel Vetter5a21b662016-05-24 17:13:53 +020012223 drm_crtc_vblank_put(crtc);
12224 kfree(work);
12225 return -EBUSY;
12226 }
12227 }
12228 intel_crtc->flip_work = work;
12229 spin_unlock_irq(&dev->event_lock);
Alex Goinsfd8e0582015-11-25 18:43:38 -080012230
Daniel Vetter5a21b662016-05-24 17:13:53 +020012231 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
12232 flush_workqueue(dev_priv->wq);
12233
12234 /* Reference the objects for the scheduled work. */
12235 drm_framebuffer_reference(work->old_fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012236
12237 crtc->primary->fb = fb;
12238 update_state_fb(crtc->primary);
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020012239
Chris Wilson25dc5562016-07-20 13:31:52 +010012240 work->pending_flip_obj = i915_gem_object_get(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012241
12242 ret = i915_mutex_lock_interruptible(dev);
12243 if (ret)
12244 goto cleanup;
12245
Chris Wilson8af29b02016-09-09 14:11:47 +010012246 intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
12247 if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020012248 ret = -EIO;
12249 goto cleanup;
12250 }
12251
12252 atomic_inc(&intel_crtc->unpin_work_count);
12253
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010012254 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Daniel Vetter5a21b662016-05-24 17:13:53 +020012255 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
12256
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010012257 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Akash Goel3b3f1652016-10-13 22:44:48 +053012258 engine = dev_priv->engine[BCS];
Ville Syrjälä72618eb2016-02-04 20:38:20 +020012259 if (fb->modifier[0] != old_fb->modifier[0])
Daniel Vetter5a21b662016-05-24 17:13:53 +020012260 /* vlv: DISPLAY_FLIP fails to change tiling */
12261 engine = NULL;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +010012262 } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
Akash Goel3b3f1652016-10-13 22:44:48 +053012263 engine = dev_priv->engine[BCS];
Daniel Vetter5a21b662016-05-24 17:13:53 +020012264 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsond07f0e52016-10-28 13:58:44 +010012265 engine = i915_gem_object_last_write_engine(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012266 if (engine == NULL || engine->id != RCS)
Akash Goel3b3f1652016-10-13 22:44:48 +053012267 engine = dev_priv->engine[BCS];
Daniel Vetter5a21b662016-05-24 17:13:53 +020012268 } else {
Akash Goel3b3f1652016-10-13 22:44:48 +053012269 engine = dev_priv->engine[RCS];
Daniel Vetter5a21b662016-05-24 17:13:53 +020012270 }
12271
12272 mmio_flip = use_mmio_flip(engine, obj);
12273
Chris Wilson058d88c2016-08-15 10:49:06 +010012274 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
12275 if (IS_ERR(vma)) {
12276 ret = PTR_ERR(vma);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012277 goto cleanup_pending;
Chris Wilson058d88c2016-08-15 10:49:06 +010012278 }
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020012279
Ville Syrjälä6687c902015-09-15 13:16:41 +030012280 work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012281 work->gtt_offset += intel_crtc->dspaddr_offset;
12282 work->rotation = crtc->primary->state->rotation;
12283
Paulo Zanoni1f0613162016-08-17 16:41:44 -030012284 /*
12285 * There's the potential that the next frame will not be compatible with
12286 * FBC, so we want to call pre_update() before the actual page flip.
12287 * The problem is that pre_update() caches some information about the fb
12288 * object, so we want to do this only after the object is pinned. Let's
12289 * be on the safe side and do this immediately before scheduling the
12290 * flip.
12291 */
12292 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
12293 to_intel_plane_state(primary->state));
12294
Daniel Vetter5a21b662016-05-24 17:13:53 +020012295 if (mmio_flip) {
12296 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
Imre Deak6277c8d2016-09-20 14:58:19 +030012297 queue_work(system_unbound_wq, &work->mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012298 } else {
Chris Wilson8e637172016-08-02 22:50:26 +010012299 request = i915_gem_request_alloc(engine, engine->last_context);
12300 if (IS_ERR(request)) {
12301 ret = PTR_ERR(request);
12302 goto cleanup_unpin;
12303 }
12304
Chris Wilsona2bc4692016-09-09 14:11:56 +010012305 ret = i915_gem_request_await_object(request, obj, false);
Chris Wilson8e637172016-08-02 22:50:26 +010012306 if (ret)
12307 goto cleanup_request;
12308
Daniel Vetter5a21b662016-05-24 17:13:53 +020012309 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
12310 page_flip_flags);
12311 if (ret)
Chris Wilson8e637172016-08-02 22:50:26 +010012312 goto cleanup_request;
Daniel Vetter5a21b662016-05-24 17:13:53 +020012313
12314 intel_mark_page_flip_active(intel_crtc, work);
12315
Chris Wilson8e637172016-08-02 22:50:26 +010012316 work->flip_queued_req = i915_gem_request_get(request);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012317 i915_add_request_no_flush(request);
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020012318 }
12319
Daniel Vetter5a21b662016-05-24 17:13:53 +020012320 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
12321 to_intel_plane(primary)->frontbuffer_bit);
12322 mutex_unlock(&dev->struct_mutex);
12323
Chris Wilson5748b6a2016-08-04 16:32:38 +010012324 intel_frontbuffer_flip_prepare(to_i915(dev),
Daniel Vetter5a21b662016-05-24 17:13:53 +020012325 to_intel_plane(primary)->frontbuffer_bit);
12326
12327 trace_i915_flip_request(intel_crtc->plane, obj);
12328
12329 return 0;
12330
Chris Wilson8e637172016-08-02 22:50:26 +010012331cleanup_request:
12332 i915_add_request_no_flush(request);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012333cleanup_unpin:
12334 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
12335cleanup_pending:
Daniel Vetter5a21b662016-05-24 17:13:53 +020012336 atomic_dec(&intel_crtc->unpin_work_count);
12337 mutex_unlock(&dev->struct_mutex);
12338cleanup:
12339 crtc->primary->fb = old_fb;
12340 update_state_fb(crtc->primary);
12341
Chris Wilsonf0cd5182016-10-28 13:58:43 +010012342 i915_gem_object_put(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012343 drm_framebuffer_unreference(work->old_fb);
12344
12345 spin_lock_irq(&dev->event_lock);
12346 intel_crtc->flip_work = NULL;
12347 spin_unlock_irq(&dev->event_lock);
12348
12349 drm_crtc_vblank_put(crtc);
12350free_work:
12351 kfree(work);
12352
12353 if (ret == -EIO) {
12354 struct drm_atomic_state *state;
12355 struct drm_plane_state *plane_state;
12356
12357out_hang:
12358 state = drm_atomic_state_alloc(dev);
12359 if (!state)
12360 return -ENOMEM;
12361 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
12362
12363retry:
12364 plane_state = drm_atomic_get_plane_state(state, primary);
12365 ret = PTR_ERR_OR_ZERO(plane_state);
12366 if (!ret) {
12367 drm_atomic_set_fb_for_plane(plane_state, fb);
12368
12369 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
12370 if (!ret)
12371 ret = drm_atomic_commit(state);
12372 }
12373
12374 if (ret == -EDEADLK) {
12375 drm_modeset_backoff(state->acquire_ctx);
12376 drm_atomic_state_clear(state);
12377 goto retry;
12378 }
12379
Chris Wilson08536952016-10-14 13:18:18 +010012380 drm_atomic_state_put(state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020012381
12382 if (ret == 0 && event) {
12383 spin_lock_irq(&dev->event_lock);
12384 drm_crtc_send_vblank_event(crtc, event);
12385 spin_unlock_irq(&dev->event_lock);
12386 }
12387 }
12388 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070012389}
12390
Daniel Vetter5a21b662016-05-24 17:13:53 +020012391
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012392/**
12393 * intel_wm_need_update - Check whether watermarks need updating
12394 * @plane: drm plane
12395 * @state: new plane state
12396 *
12397 * Check current plane state versus the new one to determine whether
12398 * watermarks need to be recalculated.
12399 *
12400 * Returns true or false.
12401 */
12402static bool intel_wm_need_update(struct drm_plane *plane,
12403 struct drm_plane_state *state)
12404{
Matt Roperd21fbe82015-09-24 15:53:12 -070012405 struct intel_plane_state *new = to_intel_plane_state(state);
12406 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
12407
12408 /* Update watermarks on tiling or size changes. */
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012409 if (new->base.visible != cur->base.visible)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012410 return true;
12411
12412 if (!cur->base.fb || !new->base.fb)
12413 return false;
12414
12415 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
12416 cur->base.rotation != new->base.rotation ||
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012417 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
12418 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
12419 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
12420 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012421 return true;
12422
12423 return false;
12424}
12425
Matt Roperd21fbe82015-09-24 15:53:12 -070012426static bool needs_scaling(struct intel_plane_state *state)
12427{
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012428 int src_w = drm_rect_width(&state->base.src) >> 16;
12429 int src_h = drm_rect_height(&state->base.src) >> 16;
12430 int dst_w = drm_rect_width(&state->base.dst);
12431 int dst_h = drm_rect_height(&state->base.dst);
Matt Roperd21fbe82015-09-24 15:53:12 -070012432
12433 return (src_w != dst_w || src_h != dst_h);
12434}
12435
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012436int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
12437 struct drm_plane_state *plane_state)
12438{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010012439 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012440 struct drm_crtc *crtc = crtc_state->crtc;
12441 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12442 struct drm_plane *plane = plane_state->plane;
12443 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080012444 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012445 struct intel_plane_state *old_plane_state =
12446 to_intel_plane_state(plane->state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012447 bool mode_changed = needs_modeset(crtc_state);
12448 bool was_crtc_enabled = crtc->state->active;
12449 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012450 bool turn_off, turn_on, visible, was_visible;
12451 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030012452 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012453
Tvrtko Ursulin55b8f2a2016-10-14 09:17:22 +010012454 if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012455 ret = skl_update_scaler_plane(
12456 to_intel_crtc_state(crtc_state),
12457 to_intel_plane_state(plane_state));
12458 if (ret)
12459 return ret;
12460 }
12461
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012462 was_visible = old_plane_state->base.visible;
12463 visible = to_intel_plane_state(plane_state)->base.visible;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012464
12465 if (!was_crtc_enabled && WARN_ON(was_visible))
12466 was_visible = false;
12467
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010012468 /*
12469 * Visibility is calculated as if the crtc was on, but
12470 * after scaler setup everything depends on it being off
12471 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030012472 *
12473 * FIXME this is wrong for watermarks. Watermarks should also
12474 * be computed as if the pipe would be active. Perhaps move
12475 * per-plane wm computation to the .check_plane() hook, and
12476 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010012477 */
12478 if (!is_crtc_enabled)
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012479 to_intel_plane_state(plane_state)->base.visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012480
12481 if (!was_visible && !visible)
12482 return 0;
12483
Maarten Lankhorste8861672016-02-24 11:24:26 +010012484 if (fb != old_plane_state->base.fb)
12485 pipe_config->fb_changed = true;
12486
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012487 turn_off = was_visible && (!visible || mode_changed);
12488 turn_on = visible && (!was_visible || mode_changed);
12489
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012490 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjälä78108b72016-05-27 20:59:19 +030012491 intel_crtc->base.base.id,
12492 intel_crtc->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012493 plane->base.id, plane->name,
12494 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012495
Ville Syrjälä72660ce2016-05-27 20:59:20 +030012496 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12497 plane->base.id, plane->name,
12498 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012499 turn_off, turn_on, mode_changed);
12500
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012501 if (turn_on) {
12502 pipe_config->update_wm_pre = true;
12503
12504 /* must disable cxsr around plane enable/disable */
12505 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12506 pipe_config->disable_cxsr = true;
12507 } else if (turn_off) {
12508 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012509
Ville Syrjälä852eb002015-06-24 22:00:07 +030012510 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010012511 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010012512 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012513 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012514 /* FIXME bollocks */
12515 pipe_config->update_wm_pre = true;
12516 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012517 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012518
Matt Ropered4a6a72016-02-23 17:20:13 -080012519 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012520 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
12521 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080012522 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12523
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070012524 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010012525 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030012526
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010012527 /*
12528 * WaCxSRDisabledForSpriteScaling:ivb
12529 *
12530 * cstate->update_wm was already set above, so this flag will
12531 * take effect when we commit and program watermarks.
12532 */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +010012533 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010012534 needs_scaling(to_intel_plane_state(plane_state)) &&
12535 !needs_scaling(old_plane_state))
12536 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012537
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012538 return 0;
12539}
12540
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012541static bool encoders_cloneable(const struct intel_encoder *a,
12542 const struct intel_encoder *b)
12543{
12544 /* masks could be asymmetric, so check both ways */
12545 return a == b || (a->cloneable & (1 << b->type) &&
12546 b->cloneable & (1 << a->type));
12547}
12548
12549static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12550 struct intel_crtc *crtc,
12551 struct intel_encoder *encoder)
12552{
12553 struct intel_encoder *source_encoder;
12554 struct drm_connector *connector;
12555 struct drm_connector_state *connector_state;
12556 int i;
12557
12558 for_each_connector_in_state(state, connector, connector_state, i) {
12559 if (connector_state->crtc != &crtc->base)
12560 continue;
12561
12562 source_encoder =
12563 to_intel_encoder(connector_state->best_encoder);
12564 if (!encoders_cloneable(encoder, source_encoder))
12565 return false;
12566 }
12567
12568 return true;
12569}
12570
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012571static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12572 struct drm_crtc_state *crtc_state)
12573{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012574 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012575 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012576 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012577 struct intel_crtc_state *pipe_config =
12578 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012579 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012580 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012581 bool mode_changed = needs_modeset(crtc_state);
12582
Ville Syrjälä852eb002015-06-24 22:00:07 +030012583 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012584 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012585
Maarten Lankhorstad421372015-06-15 12:33:42 +020012586 if (mode_changed && crtc_state->enable &&
12587 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012588 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012589 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12590 pipe_config);
12591 if (ret)
12592 return ret;
12593 }
12594
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012595 if (crtc_state->color_mgmt_changed) {
12596 ret = intel_color_check(crtc, crtc_state);
12597 if (ret)
12598 return ret;
Lionel Landwerline7852a42016-05-25 14:30:41 +010012599
12600 /*
12601 * Changing color management on Intel hardware is
12602 * handled as part of planes update.
12603 */
12604 crtc_state->planes_changed = true;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012605 }
12606
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012607 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012608 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010012609 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080012610 if (ret) {
12611 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012612 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080012613 }
12614 }
12615
12616 if (dev_priv->display.compute_intermediate_wm &&
12617 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12618 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12619 return 0;
12620
12621 /*
12622 * Calculate 'intermediate' watermarks that satisfy both the
12623 * old state and the new state. We can program these
12624 * immediately.
12625 */
12626 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12627 intel_crtc,
12628 pipe_config);
12629 if (ret) {
12630 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12631 return ret;
12632 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070012633 } else if (dev_priv->display.compute_intermediate_wm) {
12634 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12635 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012636 }
12637
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012638 if (INTEL_INFO(dev)->gen >= 9) {
12639 if (mode_changed)
12640 ret = skl_update_scaler_crtc(pipe_config);
12641
12642 if (!ret)
12643 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12644 pipe_config);
12645 }
12646
12647 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012648}
12649
Jani Nikula65b38e02015-04-13 11:26:56 +030012650static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012651 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Daniel Vetter5a21b662016-05-24 17:13:53 +020012652 .atomic_begin = intel_begin_crtc_commit,
12653 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012654 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012655};
12656
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012657static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12658{
12659 struct intel_connector *connector;
12660
12661 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020012662 if (connector->base.state->crtc)
12663 drm_connector_unreference(&connector->base);
12664
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012665 if (connector->base.encoder) {
12666 connector->base.state->best_encoder =
12667 connector->base.encoder;
12668 connector->base.state->crtc =
12669 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020012670
12671 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012672 } else {
12673 connector->base.state->best_encoder = NULL;
12674 connector->base.state->crtc = NULL;
12675 }
12676 }
12677}
12678
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012679static void
Robin Schroereba905b2014-05-18 02:24:50 +020012680connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012681 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012682{
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012683 const struct drm_display_info *info = &connector->base.display_info;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012684 int bpp = pipe_config->pipe_bpp;
12685
12686 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012687 connector->base.base.id,
12688 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012689
12690 /* Don't use an invalid EDID bpc value */
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012691 if (info->bpc != 0 && info->bpc * 3 < bpp) {
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012692 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012693 bpp, info->bpc * 3);
12694 pipe_config->pipe_bpp = info->bpc * 3;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012695 }
12696
Mario Kleiner196f9542016-07-06 12:05:45 +020012697 /* Clamp bpp to 8 on screens without EDID 1.4 */
Ville Syrjälä6a2a5c52016-09-28 16:51:42 +030012698 if (info->bpc == 0 && bpp > 24) {
Mario Kleiner196f9542016-07-06 12:05:45 +020012699 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
12700 bpp);
12701 pipe_config->pipe_bpp = 24;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012702 }
12703}
12704
12705static int
12706compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012707 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012708{
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010012709 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012710 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012711 struct drm_connector *connector;
12712 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012713 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012714
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010012715 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
12716 IS_CHERRYVIEW(dev_priv)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012717 bpp = 10*3;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010012718 else if (INTEL_GEN(dev_priv) >= 5)
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012719 bpp = 12*3;
12720 else
12721 bpp = 8*3;
12722
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012723
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012724 pipe_config->pipe_bpp = bpp;
12725
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012726 state = pipe_config->base.state;
12727
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012728 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012729 for_each_connector_in_state(state, connector, connector_state, i) {
12730 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012731 continue;
12732
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012733 connected_sink_compute_bpp(to_intel_connector(connector),
12734 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012735 }
12736
12737 return bpp;
12738}
12739
Daniel Vetter644db712013-09-19 14:53:58 +020012740static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12741{
12742 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12743 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012744 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012745 mode->crtc_hdisplay, mode->crtc_hsync_start,
12746 mode->crtc_hsync_end, mode->crtc_htotal,
12747 mode->crtc_vdisplay, mode->crtc_vsync_start,
12748 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12749}
12750
Daniel Vetterc0b03412013-05-28 12:05:54 +020012751static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012752 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012753 const char *context)
12754{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012755 struct drm_device *dev = crtc->base.dev;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012756 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012757 struct drm_plane *plane;
12758 struct intel_plane *intel_plane;
12759 struct intel_plane_state *state;
12760 struct drm_framebuffer *fb;
12761
Ville Syrjälä78108b72016-05-27 20:59:19 +030012762 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12763 crtc->base.base.id, crtc->base.name,
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012764 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012765
Jani Nikulada205632016-03-15 21:51:10 +020012766 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012767 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12768 pipe_config->pipe_bpp, pipe_config->dither);
12769 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12770 pipe_config->has_pch_encoder,
12771 pipe_config->fdi_lanes,
12772 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12773 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12774 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012775 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 +030012776 intel_crtc_has_dp_encoder(pipe_config),
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012777 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012778 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12779 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12780 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012781
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012782 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 +030012783 intel_crtc_has_dp_encoder(pipe_config),
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012784 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012785 pipe_config->dp_m2_n2.gmch_m,
12786 pipe_config->dp_m2_n2.gmch_n,
12787 pipe_config->dp_m2_n2.link_m,
12788 pipe_config->dp_m2_n2.link_n,
12789 pipe_config->dp_m2_n2.tu);
12790
Daniel Vetter55072d12014-11-20 16:10:28 +010012791 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12792 pipe_config->has_audio,
12793 pipe_config->has_infoframe);
12794
Daniel Vetterc0b03412013-05-28 12:05:54 +020012795 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012796 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012797 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012798 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12799 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012800 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012801 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12802 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012803 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12804 crtc->num_scalers,
12805 pipe_config->scaler_state.scaler_users,
12806 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012807 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12808 pipe_config->gmch_pfit.control,
12809 pipe_config->gmch_pfit.pgm_ratios,
12810 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012811 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012812 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012813 pipe_config->pch_pfit.size,
12814 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012815 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012816 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012817
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +010012818 if (IS_BROXTON(dev_priv)) {
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070012819 DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012820 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012821 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012822 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012823 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012824 pipe_config->dpll_hw_state.pll0,
12825 pipe_config->dpll_hw_state.pll1,
12826 pipe_config->dpll_hw_state.pll2,
12827 pipe_config->dpll_hw_state.pll3,
12828 pipe_config->dpll_hw_state.pll6,
12829 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012830 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012831 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012832 pipe_config->dpll_hw_state.pcsdw12);
Tvrtko Ursulin08537232016-10-13 11:03:02 +010012833 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070012834 DRM_DEBUG_KMS("dpll_hw_state: "
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012835 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012836 pipe_config->dpll_hw_state.ctrl1,
12837 pipe_config->dpll_hw_state.cfgcr1,
12838 pipe_config->dpll_hw_state.cfgcr2);
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012839 } else if (HAS_DDI(dev_priv)) {
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -070012840 DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012841 pipe_config->dpll_hw_state.wrpll,
12842 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012843 } else {
12844 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12845 "fp0: 0x%x, fp1: 0x%x\n",
12846 pipe_config->dpll_hw_state.dpll,
12847 pipe_config->dpll_hw_state.dpll_md,
12848 pipe_config->dpll_hw_state.fp0,
12849 pipe_config->dpll_hw_state.fp1);
12850 }
12851
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012852 DRM_DEBUG_KMS("planes on this crtc\n");
12853 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Eric Engestromd3828142016-08-15 16:29:55 +010012854 char *format_name;
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012855 intel_plane = to_intel_plane(plane);
12856 if (intel_plane->pipe != crtc->pipe)
12857 continue;
12858
12859 state = to_intel_plane_state(plane->state);
12860 fb = state->base.fb;
12861 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012862 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12863 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012864 continue;
12865 }
12866
Eric Engestrom90844f02016-08-15 01:02:38 +010012867 format_name = drm_get_format_name(fb->pixel_format);
12868
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012869 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12870 plane->base.id, plane->name);
12871 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
Eric Engestrom90844f02016-08-15 01:02:38 +010012872 fb->base.id, fb->width, fb->height, format_name);
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012873 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12874 state->scaler_id,
Ville Syrjälä936e71e2016-07-26 19:06:59 +030012875 state->base.src.x1 >> 16,
12876 state->base.src.y1 >> 16,
12877 drm_rect_width(&state->base.src) >> 16,
12878 drm_rect_height(&state->base.src) >> 16,
12879 state->base.dst.x1, state->base.dst.y1,
12880 drm_rect_width(&state->base.dst),
12881 drm_rect_height(&state->base.dst));
Eric Engestrom90844f02016-08-15 01:02:38 +010012882
12883 kfree(format_name);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012884 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012885}
12886
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012887static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012888{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012889 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012890 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012891 unsigned int used_ports = 0;
Ville Syrjälä477321e2016-07-28 17:50:40 +030012892 unsigned int used_mst_ports = 0;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012893
12894 /*
12895 * Walk the connector list instead of the encoder
12896 * list to detect the problem on ddi platforms
12897 * where there's just one encoder per digital port.
12898 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012899 drm_for_each_connector(connector, dev) {
12900 struct drm_connector_state *connector_state;
12901 struct intel_encoder *encoder;
12902
12903 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12904 if (!connector_state)
12905 connector_state = connector->state;
12906
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012907 if (!connector_state->best_encoder)
12908 continue;
12909
12910 encoder = to_intel_encoder(connector_state->best_encoder);
12911
12912 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012913
12914 switch (encoder->type) {
12915 unsigned int port_mask;
12916 case INTEL_OUTPUT_UNKNOWN:
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010012917 if (WARN_ON(!HAS_DDI(to_i915(dev))))
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012918 break;
Ville Syrjäläcca05022016-06-22 21:57:06 +030012919 case INTEL_OUTPUT_DP:
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012920 case INTEL_OUTPUT_HDMI:
12921 case INTEL_OUTPUT_EDP:
12922 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12923
12924 /* the same port mustn't appear more than once */
12925 if (used_ports & port_mask)
12926 return false;
12927
12928 used_ports |= port_mask;
Ville Syrjälä477321e2016-07-28 17:50:40 +030012929 break;
12930 case INTEL_OUTPUT_DP_MST:
12931 used_mst_ports |=
12932 1 << enc_to_mst(&encoder->base)->primary->port;
12933 break;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012934 default:
12935 break;
12936 }
12937 }
12938
Ville Syrjälä477321e2016-07-28 17:50:40 +030012939 /* can't mix MST and SST/HDMI on the same port */
12940 if (used_ports & used_mst_ports)
12941 return false;
12942
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012943 return true;
12944}
12945
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012946static void
12947clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12948{
12949 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012950 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012951 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012952 struct intel_shared_dpll *shared_dpll;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012953 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012954
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012955 /* FIXME: before the switch to atomic started, a new pipe_config was
12956 * kzalloc'd. Code that depends on any field being zero should be
12957 * fixed, so that the crtc_state can be safely duplicated. For now,
12958 * only fields that are know to not cause problems are preserved. */
12959
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012960 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012961 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012962 shared_dpll = crtc_state->shared_dpll;
12963 dpll_hw_state = crtc_state->dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012964 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012965
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012966 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012967
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012968 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012969 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012970 crtc_state->shared_dpll = shared_dpll;
12971 crtc_state->dpll_hw_state = dpll_hw_state;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012972 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012973}
12974
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012975static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012976intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012977 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012978{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012979 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012980 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012981 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012982 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012983 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012984 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012985 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012986
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012987 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012988
Daniel Vettere143a212013-07-04 12:01:15 +020012989 pipe_config->cpu_transcoder =
12990 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012991
Imre Deak2960bc92013-07-30 13:36:32 +030012992 /*
12993 * Sanitize sync polarity flags based on requested ones. If neither
12994 * positive or negative polarity is requested, treat this as meaning
12995 * negative polarity.
12996 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012997 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012998 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012999 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030013000
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013001 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030013002 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013003 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030013004
Daniel Vetterd328c9d2015-04-10 16:22:37 +020013005 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
13006 pipe_config);
13007 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010013008 goto fail;
13009
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030013010 /*
13011 * Determine the real pipe dimensions. Note that stereo modes can
13012 * increase the actual pipe size due to the frame doubling and
13013 * insertion of additional space for blanks between the frame. This
13014 * is stored in the crtc timings. We use the requested mode to do this
13015 * computation to clearly distinguish it from the adjusted mode, which
13016 * can be changed by the connectors in the below retry loop.
13017 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013018 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080013019 &pipe_config->pipe_src_w,
13020 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030013021
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013022 for_each_connector_in_state(state, connector, connector_state, i) {
13023 if (connector_state->crtc != crtc)
13024 continue;
13025
13026 encoder = to_intel_encoder(connector_state->best_encoder);
13027
Ville Syrjäläe25148d2016-06-22 21:57:09 +030013028 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
13029 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
13030 goto fail;
13031 }
13032
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013033 /*
13034 * Determine output_types before calling the .compute_config()
13035 * hooks so that the hooks can use this information safely.
13036 */
13037 pipe_config->output_types |= 1 << encoder->type;
13038 }
13039
Daniel Vettere29c22c2013-02-21 00:00:16 +010013040encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020013041 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020013042 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020013043 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020013044
Daniel Vetter135c81b2013-07-21 21:37:09 +020013045 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013046 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
13047 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020013048
Daniel Vetter7758a112012-07-08 19:40:39 +020013049 /* Pass our mode to the connectors and the CRTC to give them a chance to
13050 * adjust it according to limitations or connector properties, and also
13051 * a chance to reject the mode entirely.
13052 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030013053 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020013054 if (connector_state->crtc != crtc)
13055 continue;
13056
13057 encoder = to_intel_encoder(connector_state->best_encoder);
13058
Maarten Lankhorst0a478c22016-08-09 17:04:05 +020013059 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
Daniel Vetterefea6e82013-07-21 21:36:59 +020013060 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020013061 goto fail;
13062 }
13063 }
13064
Daniel Vetterff9a6752013-06-01 17:16:21 +020013065 /* Set default port clock if not overwritten by the encoder. Needs to be
13066 * done afterwards in case the encoder adjusts the mode. */
13067 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013068 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010013069 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020013070
Daniel Vettera43f6e02013-06-07 23:10:32 +020013071 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010013072 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020013073 DRM_DEBUG_KMS("CRTC fixup failed\n");
13074 goto fail;
13075 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010013076
13077 if (ret == RETRY) {
13078 if (WARN(!retry, "loop in pipe configuration computation\n")) {
13079 ret = -EINVAL;
13080 goto fail;
13081 }
13082
13083 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
13084 retry = false;
13085 goto encoder_retry;
13086 }
13087
Daniel Vettere8fa4272015-08-12 11:43:34 +020013088 /* Dithering seems to not pass-through bits correctly when it should, so
13089 * only enable it on 6bpc panels. */
13090 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020013091 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020013092 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010013093
Daniel Vetter7758a112012-07-08 19:40:39 +020013094fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030013095 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020013096}
13097
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013098static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013099intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013100{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013101 struct drm_crtc *crtc;
13102 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020013103 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020013104
Ville Syrjälä76688512014-01-10 11:28:06 +020013105 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020013106 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020013107 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020013108
13109 /* Update hwmode for vblank functions */
13110 if (crtc->state->active)
13111 crtc->hwmode = crtc->state->adjusted_mode;
13112 else
13113 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020013114
13115 /*
13116 * Update legacy state to satisfy fbc code. This can
13117 * be removed when fbc uses the atomic state.
13118 */
13119 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
13120 struct drm_plane_state *plane_state = crtc->primary->state;
13121
13122 crtc->primary->fb = plane_state->fb;
13123 crtc->x = plane_state->src_x >> 16;
13124 crtc->y = plane_state->src_y >> 16;
13125 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020013126 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020013127}
13128
Ville Syrjälä3bd26262013-09-06 23:29:02 +030013129static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030013130{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030013131 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030013132
13133 if (clock1 == clock2)
13134 return true;
13135
13136 if (!clock1 || !clock2)
13137 return false;
13138
13139 diff = abs(clock1 - clock2);
13140
13141 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
13142 return true;
13143
13144 return false;
13145}
13146
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013147static bool
13148intel_compare_m_n(unsigned int m, unsigned int n,
13149 unsigned int m2, unsigned int n2,
13150 bool exact)
13151{
13152 if (m == m2 && n == n2)
13153 return true;
13154
13155 if (exact || !m || !n || !m2 || !n2)
13156 return false;
13157
13158 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
13159
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010013160 if (n > n2) {
13161 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013162 m2 <<= 1;
13163 n2 <<= 1;
13164 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010013165 } else if (n < n2) {
13166 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013167 m <<= 1;
13168 n <<= 1;
13169 }
13170 }
13171
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010013172 if (n != n2)
13173 return false;
13174
13175 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013176}
13177
13178static bool
13179intel_compare_link_m_n(const struct intel_link_m_n *m_n,
13180 struct intel_link_m_n *m2_n2,
13181 bool adjust)
13182{
13183 if (m_n->tu == m2_n2->tu &&
13184 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
13185 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
13186 intel_compare_m_n(m_n->link_m, m_n->link_n,
13187 m2_n2->link_m, m2_n2->link_n, !adjust)) {
13188 if (adjust)
13189 *m2_n2 = *m_n;
13190
13191 return true;
13192 }
13193
13194 return false;
13195}
13196
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013197static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020013198intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020013199 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013200 struct intel_crtc_state *pipe_config,
13201 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013202{
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010013203 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013204 bool ret = true;
13205
13206#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
13207 do { \
13208 if (!adjust) \
13209 DRM_ERROR(fmt, ##__VA_ARGS__); \
13210 else \
13211 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
13212 } while (0)
13213
Daniel Vetter66e985c2013-06-05 13:34:20 +020013214#define PIPE_CONF_CHECK_X(name) \
13215 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013216 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020013217 "(expected 0x%08x, found 0x%08x)\n", \
13218 current_config->name, \
13219 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013220 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020013221 }
13222
Daniel Vetter08a24032013-04-19 11:25:34 +020013223#define PIPE_CONF_CHECK_I(name) \
13224 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013225 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020013226 "(expected %i, found %i)\n", \
13227 current_config->name, \
13228 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013229 ret = false; \
13230 }
13231
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013232#define PIPE_CONF_CHECK_P(name) \
13233 if (current_config->name != pipe_config->name) { \
13234 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13235 "(expected %p, found %p)\n", \
13236 current_config->name, \
13237 pipe_config->name); \
13238 ret = false; \
13239 }
13240
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013241#define PIPE_CONF_CHECK_M_N(name) \
13242 if (!intel_compare_link_m_n(&current_config->name, \
13243 &pipe_config->name,\
13244 adjust)) { \
13245 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13246 "(expected tu %i gmch %i/%i link %i/%i, " \
13247 "found tu %i, gmch %i/%i link %i/%i)\n", \
13248 current_config->name.tu, \
13249 current_config->name.gmch_m, \
13250 current_config->name.gmch_n, \
13251 current_config->name.link_m, \
13252 current_config->name.link_n, \
13253 pipe_config->name.tu, \
13254 pipe_config->name.gmch_m, \
13255 pipe_config->name.gmch_n, \
13256 pipe_config->name.link_m, \
13257 pipe_config->name.link_n); \
13258 ret = false; \
13259 }
13260
Daniel Vetter55c561a2016-03-30 11:34:36 +020013261/* This is required for BDW+ where there is only one set of registers for
13262 * switching between high and low RR.
13263 * This macro can be used whenever a comparison has to be made between one
13264 * hw state and multiple sw state variables.
13265 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013266#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
13267 if (!intel_compare_link_m_n(&current_config->name, \
13268 &pipe_config->name, adjust) && \
13269 !intel_compare_link_m_n(&current_config->alt_name, \
13270 &pipe_config->name, adjust)) { \
13271 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13272 "(expected tu %i gmch %i/%i link %i/%i, " \
13273 "or tu %i gmch %i/%i link %i/%i, " \
13274 "found tu %i, gmch %i/%i link %i/%i)\n", \
13275 current_config->name.tu, \
13276 current_config->name.gmch_m, \
13277 current_config->name.gmch_n, \
13278 current_config->name.link_m, \
13279 current_config->name.link_n, \
13280 current_config->alt_name.tu, \
13281 current_config->alt_name.gmch_m, \
13282 current_config->alt_name.gmch_n, \
13283 current_config->alt_name.link_m, \
13284 current_config->alt_name.link_n, \
13285 pipe_config->name.tu, \
13286 pipe_config->name.gmch_m, \
13287 pipe_config->name.gmch_n, \
13288 pipe_config->name.link_m, \
13289 pipe_config->name.link_n); \
13290 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010013291 }
13292
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013293#define PIPE_CONF_CHECK_FLAGS(name, mask) \
13294 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013295 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013296 "(expected %i, found %i)\n", \
13297 current_config->name & (mask), \
13298 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013299 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013300 }
13301
Ville Syrjälä5e550652013-09-06 23:29:07 +030013302#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
13303 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013304 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030013305 "(expected %i, found %i)\n", \
13306 current_config->name, \
13307 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013308 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030013309 }
13310
Daniel Vetterbb760062013-06-06 14:55:52 +020013311#define PIPE_CONF_QUIRK(quirk) \
13312 ((current_config->quirks | pipe_config->quirks) & (quirk))
13313
Daniel Vettereccb1402013-05-22 00:50:22 +020013314 PIPE_CONF_CHECK_I(cpu_transcoder);
13315
Daniel Vetter08a24032013-04-19 11:25:34 +020013316 PIPE_CONF_CHECK_I(has_pch_encoder);
13317 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013318 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020013319
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030013320 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030013321 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070013322
13323 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013324 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070013325
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013326 if (current_config->has_drrs)
13327 PIPE_CONF_CHECK_M_N(dp_m2_n2);
13328 } else
13329 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030013330
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013331 PIPE_CONF_CHECK_X(output_types);
Jani Nikulaa65347b2015-11-27 12:21:46 +020013332
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013333 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
13334 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
13335 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
13336 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
13337 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
13338 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013339
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013340 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
13341 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
13342 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
13343 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
13344 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
13345 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013346
Daniel Vetterc93f54c2013-06-27 19:47:19 +020013347 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020013348 PIPE_CONF_CHECK_I(has_hdmi_sink);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010013349 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010013350 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020013351 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080013352 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020013353
Daniel Vetter9ed109a2014-04-24 23:54:52 +020013354 PIPE_CONF_CHECK_I(has_audio);
13355
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013356 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013357 DRM_MODE_FLAG_INTERLACE);
13358
Daniel Vetterbb760062013-06-06 14:55:52 +020013359 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
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_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013362 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013363 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013364 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013365 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013366 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020013367 DRM_MODE_FLAG_NVSYNC);
13368 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070013369
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030013370 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020013371 /* pfit ratios are autocomputed by the hw on gen4+ */
13372 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020013373 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030013374 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020013375
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013376 if (!adjust) {
13377 PIPE_CONF_CHECK_I(pipe_src_w);
13378 PIPE_CONF_CHECK_I(pipe_src_h);
13379
13380 PIPE_CONF_CHECK_I(pch_pfit.enabled);
13381 if (current_config->pch_pfit.enabled) {
13382 PIPE_CONF_CHECK_X(pch_pfit.pos);
13383 PIPE_CONF_CHECK_X(pch_pfit.size);
13384 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020013385
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020013386 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
13387 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070013388
Jesse Barnese59150d2014-01-07 13:30:45 -080013389 /* BDW+ don't expose a synchronous way to read the state */
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010013390 if (IS_HASWELL(dev_priv))
Jesse Barnese59150d2014-01-07 13:30:45 -080013391 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030013392
Ville Syrjälä282740f2013-09-04 18:30:03 +030013393 PIPE_CONF_CHECK_I(double_wide);
13394
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013395 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020013396 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020013397 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020013398 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
13399 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030013400 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010013401 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000013402 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
13403 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
13404 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020013405
Ville Syrjälä47eacba2016-04-12 22:14:35 +030013406 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
13407 PIPE_CONF_CHECK_X(dsi_pll.div);
13408
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010013409 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
Ville Syrjälä42571ae2013-09-06 23:29:00 +030013410 PIPE_CONF_CHECK_I(pipe_bpp);
13411
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013412 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080013413 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030013414
Daniel Vetter66e985c2013-06-05 13:34:20 +020013415#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020013416#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013417#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020013418#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030013419#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020013420#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013421#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020013422
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013423 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013424}
13425
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020013426static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
13427 const struct intel_crtc_state *pipe_config)
13428{
13429 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020013430 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020013431 &pipe_config->fdi_m_n);
13432 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
13433
13434 /*
13435 * FDI already provided one idea for the dotclock.
13436 * Yell if the encoder disagrees.
13437 */
13438 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
13439 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
13440 fdi_dotclock, dotclock);
13441 }
13442}
13443
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013444static void verify_wm_state(struct drm_crtc *crtc,
13445 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000013446{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013447 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010013448 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau08db6652014-11-04 17:06:52 +000013449 struct skl_ddb_allocation hw_ddb, *sw_ddb;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013450 struct skl_pipe_wm hw_wm, *sw_wm;
13451 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
13452 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013453 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13454 const enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013455 int plane, level, max_level = ilk_wm_max_level(dev_priv);
Damien Lespiau08db6652014-11-04 17:06:52 +000013456
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013457 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000013458 return;
13459
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013460 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
13461 sw_wm = &intel_crtc->wm.active.skl;
13462
Damien Lespiau08db6652014-11-04 17:06:52 +000013463 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
13464 sw_ddb = &dev_priv->wm.skl_hw.ddb;
13465
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013466 /* planes */
Matt Roper8b364b42016-10-26 15:51:28 -070013467 for_each_universal_plane(dev_priv, pipe, plane) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013468 hw_plane_wm = &hw_wm.planes[plane];
13469 sw_plane_wm = &sw_wm->planes[plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000013470
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013471 /* Watermarks */
13472 for (level = 0; level <= max_level; level++) {
13473 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
13474 &sw_plane_wm->wm[level]))
13475 continue;
Damien Lespiau08db6652014-11-04 17:06:52 +000013476
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013477 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",
13478 pipe_name(pipe), plane + 1, level,
13479 sw_plane_wm->wm[level].plane_en,
13480 sw_plane_wm->wm[level].plane_res_b,
13481 sw_plane_wm->wm[level].plane_res_l,
13482 hw_plane_wm->wm[level].plane_en,
13483 hw_plane_wm->wm[level].plane_res_b,
13484 hw_plane_wm->wm[level].plane_res_l);
13485 }
13486
13487 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
13488 &sw_plane_wm->trans_wm)) {
13489 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",
13490 pipe_name(pipe), plane + 1,
13491 sw_plane_wm->trans_wm.plane_en,
13492 sw_plane_wm->trans_wm.plane_res_b,
13493 sw_plane_wm->trans_wm.plane_res_l,
13494 hw_plane_wm->trans_wm.plane_en,
13495 hw_plane_wm->trans_wm.plane_res_b,
13496 hw_plane_wm->trans_wm.plane_res_l);
13497 }
13498
13499 /* DDB */
13500 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
13501 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
13502
13503 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040013504 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 -040013505 pipe_name(pipe), plane + 1,
13506 sw_ddb_entry->start, sw_ddb_entry->end,
13507 hw_ddb_entry->start, hw_ddb_entry->end);
13508 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013509 }
13510
Lyude27082492016-08-24 07:48:10 +020013511 /*
13512 * cursor
13513 * If the cursor plane isn't active, we may not have updated it's ddb
13514 * allocation. In that case since the ddb allocation will be updated
13515 * once the plane becomes visible, we can skip this check
13516 */
13517 if (intel_crtc->cursor_addr) {
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013518 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
13519 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013520
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013521 /* Watermarks */
13522 for (level = 0; level <= max_level; level++) {
13523 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
13524 &sw_plane_wm->wm[level]))
13525 continue;
13526
13527 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",
13528 pipe_name(pipe), level,
13529 sw_plane_wm->wm[level].plane_en,
13530 sw_plane_wm->wm[level].plane_res_b,
13531 sw_plane_wm->wm[level].plane_res_l,
13532 hw_plane_wm->wm[level].plane_en,
13533 hw_plane_wm->wm[level].plane_res_b,
13534 hw_plane_wm->wm[level].plane_res_l);
13535 }
13536
13537 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
13538 &sw_plane_wm->trans_wm)) {
13539 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13540 pipe_name(pipe),
13541 sw_plane_wm->trans_wm.plane_en,
13542 sw_plane_wm->trans_wm.plane_res_b,
13543 sw_plane_wm->trans_wm.plane_res_l,
13544 hw_plane_wm->trans_wm.plane_en,
13545 hw_plane_wm->trans_wm.plane_res_b,
13546 hw_plane_wm->trans_wm.plane_res_l);
13547 }
13548
13549 /* DDB */
13550 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
13551 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
13552
13553 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
cpaul@redhat.comfaccd992016-10-14 17:31:58 -040013554 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
Lyude27082492016-08-24 07:48:10 +020013555 pipe_name(pipe),
cpaul@redhat.com3de8a142016-10-14 17:31:57 -040013556 sw_ddb_entry->start, sw_ddb_entry->end,
13557 hw_ddb_entry->start, hw_ddb_entry->end);
Lyude27082492016-08-24 07:48:10 +020013558 }
Damien Lespiau08db6652014-11-04 17:06:52 +000013559 }
13560}
13561
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013562static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013563verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013564{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013565 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013566
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013567 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013568 struct drm_encoder *encoder = connector->encoder;
13569 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013570
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013571 if (state->crtc != crtc)
13572 continue;
13573
Daniel Vetter5a21b662016-05-24 17:13:53 +020013574 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013575
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013576 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013577 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013578 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013579}
13580
13581static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013582verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013583{
13584 struct intel_encoder *encoder;
13585 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013586
Damien Lespiaub2784e12014-08-05 11:29:37 +010013587 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013588 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013589 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013590
13591 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13592 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013593 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013594
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013595 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013596 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013597 continue;
13598 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013599
13600 I915_STATE_WARN(connector->base.state->crtc !=
13601 encoder->base.crtc,
13602 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013603 }
Dave Airlie0e32b392014-05-02 14:02:48 +100013604
Rob Clarke2c719b2014-12-15 13:56:32 -050013605 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013606 "encoder's enabled state mismatch "
13607 "(expected %i, found %i)\n",
13608 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013609
13610 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013611 bool active;
13612
13613 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013614 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013615 "encoder detached but still enabled on pipe %c.\n",
13616 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013617 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013618 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013619}
13620
13621static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013622verify_crtc_state(struct drm_crtc *crtc,
13623 struct drm_crtc_state *old_crtc_state,
13624 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013625{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013626 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010013627 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013628 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013629 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13630 struct intel_crtc_state *pipe_config, *sw_config;
13631 struct drm_atomic_state *old_state;
13632 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013633
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013634 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020013635 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013636 pipe_config = to_intel_crtc_state(old_crtc_state);
13637 memset(pipe_config, 0, sizeof(*pipe_config));
13638 pipe_config->base.crtc = crtc;
13639 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013640
Ville Syrjälä78108b72016-05-27 20:59:19 +030013641 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013642
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013643 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013644
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013645 /* hw state is inconsistent with the pipe quirk */
13646 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13647 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13648 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013649
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013650 I915_STATE_WARN(new_crtc_state->active != active,
13651 "crtc active state doesn't match with hw state "
13652 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013653
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013654 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13655 "transitional active state does not match atomic hw state "
13656 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013657
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013658 for_each_encoder_on_crtc(dev, crtc, encoder) {
13659 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013660
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013661 active = encoder->get_hw_state(encoder, &pipe);
13662 I915_STATE_WARN(active != new_crtc_state->active,
13663 "[ENCODER:%i] active %i with crtc active %i\n",
13664 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013665
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013666 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13667 "Encoder connected to wrong pipe %c\n",
13668 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013669
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013670 if (active) {
13671 pipe_config->output_types |= 1 << encoder->type;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013672 encoder->get_config(encoder, pipe_config);
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013673 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013674 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013675
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013676 if (!new_crtc_state->active)
13677 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013678
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013679 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013680
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013681 sw_config = to_intel_crtc_state(crtc->state);
13682 if (!intel_pipe_config_compare(dev, sw_config,
13683 pipe_config, false)) {
13684 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13685 intel_dump_pipe_config(intel_crtc, pipe_config,
13686 "[hw state]");
13687 intel_dump_pipe_config(intel_crtc, sw_config,
13688 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013689 }
13690}
13691
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013692static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013693verify_single_dpll_state(struct drm_i915_private *dev_priv,
13694 struct intel_shared_dpll *pll,
13695 struct drm_crtc *crtc,
13696 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013697{
13698 struct intel_dpll_hw_state dpll_hw_state;
13699 unsigned crtc_mask;
13700 bool active;
13701
13702 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13703
13704 DRM_DEBUG_KMS("%s\n", pll->name);
13705
13706 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
13707
13708 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13709 I915_STATE_WARN(!pll->on && pll->active_mask,
13710 "pll in active use but not on in sw tracking\n");
13711 I915_STATE_WARN(pll->on && !pll->active_mask,
13712 "pll is on but not used by any active crtc\n");
13713 I915_STATE_WARN(pll->on != active,
13714 "pll on state mismatch (expected %i, found %i)\n",
13715 pll->on, active);
13716 }
13717
13718 if (!crtc) {
13719 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13720 "more active pll users than references: %x vs %x\n",
13721 pll->active_mask, pll->config.crtc_mask);
13722
13723 return;
13724 }
13725
13726 crtc_mask = 1 << drm_crtc_index(crtc);
13727
13728 if (new_state->active)
13729 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13730 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13731 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13732 else
13733 I915_STATE_WARN(pll->active_mask & crtc_mask,
13734 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13735 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13736
13737 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13738 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13739 crtc_mask, pll->config.crtc_mask);
13740
13741 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13742 &dpll_hw_state,
13743 sizeof(dpll_hw_state)),
13744 "pll hw state mismatch\n");
13745}
13746
13747static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013748verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13749 struct drm_crtc_state *old_crtc_state,
13750 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013751{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013752 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013753 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13754 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13755
13756 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013757 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013758
13759 if (old_state->shared_dpll &&
13760 old_state->shared_dpll != new_state->shared_dpll) {
13761 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13762 struct intel_shared_dpll *pll = old_state->shared_dpll;
13763
13764 I915_STATE_WARN(pll->active_mask & crtc_mask,
13765 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13766 pipe_name(drm_crtc_index(crtc)));
13767 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13768 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13769 pipe_name(drm_crtc_index(crtc)));
13770 }
13771}
13772
13773static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013774intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013775 struct drm_crtc_state *old_state,
13776 struct drm_crtc_state *new_state)
13777{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013778 if (!needs_modeset(new_state) &&
13779 !to_intel_crtc_state(new_state)->update_pipe)
13780 return;
13781
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013782 verify_wm_state(crtc, new_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013783 verify_connector_state(crtc->dev, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013784 verify_crtc_state(crtc, old_state, new_state);
13785 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013786}
13787
13788static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013789verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013790{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013791 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013792 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013793
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013794 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013795 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013796}
Daniel Vetter53589012013-06-05 13:34:16 +020013797
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013798static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013799intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013800{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013801 verify_encoder_state(dev);
13802 verify_connector_state(dev, NULL);
13803 verify_disabled_dpll_state(dev);
Daniel Vetter25c5b262012-07-08 22:08:04 +020013804}
13805
Ville Syrjälä80715b22014-05-15 20:23:23 +030013806static void update_scanline_offset(struct intel_crtc *crtc)
13807{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013808 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013809
13810 /*
13811 * The scanline counter increments at the leading edge of hsync.
13812 *
13813 * On most platforms it starts counting from vtotal-1 on the
13814 * first active line. That means the scanline counter value is
13815 * always one less than what we would expect. Ie. just after
13816 * start of vblank, which also occurs at start of hsync (on the
13817 * last active line), the scanline counter will read vblank_start-1.
13818 *
13819 * On gen2 the scanline counter starts counting from 1 instead
13820 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13821 * to keep the value positive), instead of adding one.
13822 *
13823 * On HSW+ the behaviour of the scanline counter depends on the output
13824 * type. For DP ports it behaves like most other platforms, but on HDMI
13825 * there's an extra 1 line difference. So we need to add two instead of
13826 * one to the value.
13827 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013828 if (IS_GEN2(dev_priv)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013829 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013830 int vtotal;
13831
Ville Syrjälä124abe02015-09-08 13:40:45 +030013832 vtotal = adjusted_mode->crtc_vtotal;
13833 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013834 vtotal /= 2;
13835
13836 crtc->scanline_offset = vtotal - 1;
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010013837 } else if (HAS_DDI(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +030013838 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013839 crtc->scanline_offset = 2;
13840 } else
13841 crtc->scanline_offset = 1;
13842}
13843
Maarten Lankhorstad421372015-06-15 12:33:42 +020013844static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013845{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013846 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013847 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013848 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013849 struct drm_crtc *crtc;
13850 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013851 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013852
13853 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013854 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013855
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013856 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013857 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013858 struct intel_shared_dpll *old_dpll =
13859 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013860
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013861 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013862 continue;
13863
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013864 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013865
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013866 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013867 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013868
Maarten Lankhorstad421372015-06-15 12:33:42 +020013869 if (!shared_dpll)
13870 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13871
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013872 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013873 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013874}
13875
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013876/*
13877 * This implements the workaround described in the "notes" section of the mode
13878 * set sequence documentation. When going from no pipes or single pipe to
13879 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13880 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13881 */
13882static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13883{
13884 struct drm_crtc_state *crtc_state;
13885 struct intel_crtc *intel_crtc;
13886 struct drm_crtc *crtc;
13887 struct intel_crtc_state *first_crtc_state = NULL;
13888 struct intel_crtc_state *other_crtc_state = NULL;
13889 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13890 int i;
13891
13892 /* look at all crtc's that are going to be enabled in during modeset */
13893 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13894 intel_crtc = to_intel_crtc(crtc);
13895
13896 if (!crtc_state->active || !needs_modeset(crtc_state))
13897 continue;
13898
13899 if (first_crtc_state) {
13900 other_crtc_state = to_intel_crtc_state(crtc_state);
13901 break;
13902 } else {
13903 first_crtc_state = to_intel_crtc_state(crtc_state);
13904 first_pipe = intel_crtc->pipe;
13905 }
13906 }
13907
13908 /* No workaround needed? */
13909 if (!first_crtc_state)
13910 return 0;
13911
13912 /* w/a possibly needed, check how many crtc's are already enabled. */
13913 for_each_intel_crtc(state->dev, intel_crtc) {
13914 struct intel_crtc_state *pipe_config;
13915
13916 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13917 if (IS_ERR(pipe_config))
13918 return PTR_ERR(pipe_config);
13919
13920 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13921
13922 if (!pipe_config->base.active ||
13923 needs_modeset(&pipe_config->base))
13924 continue;
13925
13926 /* 2 or more enabled crtcs means no need for w/a */
13927 if (enabled_pipe != INVALID_PIPE)
13928 return 0;
13929
13930 enabled_pipe = intel_crtc->pipe;
13931 }
13932
13933 if (enabled_pipe != INVALID_PIPE)
13934 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13935 else if (other_crtc_state)
13936 other_crtc_state->hsw_workaround_pipe = first_pipe;
13937
13938 return 0;
13939}
13940
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013941static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13942{
13943 struct drm_crtc *crtc;
13944 struct drm_crtc_state *crtc_state;
13945 int ret = 0;
13946
13947 /* add all active pipes to the state */
13948 for_each_crtc(state->dev, crtc) {
13949 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13950 if (IS_ERR(crtc_state))
13951 return PTR_ERR(crtc_state);
13952
13953 if (!crtc_state->active || needs_modeset(crtc_state))
13954 continue;
13955
13956 crtc_state->mode_changed = true;
13957
13958 ret = drm_atomic_add_affected_connectors(state, crtc);
13959 if (ret)
13960 break;
13961
13962 ret = drm_atomic_add_affected_planes(state, crtc);
13963 if (ret)
13964 break;
13965 }
13966
13967 return ret;
13968}
13969
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013970static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013971{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013972 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013973 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013974 struct drm_crtc *crtc;
13975 struct drm_crtc_state *crtc_state;
13976 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013977
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013978 if (!check_digital_port_conflicts(state)) {
13979 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13980 return -EINVAL;
13981 }
13982
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013983 intel_state->modeset = true;
13984 intel_state->active_crtcs = dev_priv->active_crtcs;
13985
13986 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13987 if (crtc_state->active)
13988 intel_state->active_crtcs |= 1 << i;
13989 else
13990 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070013991
13992 if (crtc_state->active != crtc->state->active)
13993 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013994 }
13995
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013996 /*
13997 * See if the config requires any additional preparation, e.g.
13998 * to adjust global state with pipes off. We need to do this
13999 * here so we can get the modeset_pipe updated config for the new
14000 * mode set on this crtc. For other crtcs we need to use the
14001 * adjusted_mode bits in the crtc directly.
14002 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014003 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030014004 if (!intel_state->cdclk_pll_vco)
Ville Syrjälä63911d72016-05-13 23:41:32 +030014005 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +030014006 if (!intel_state->cdclk_pll_vco)
14007 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014008
Clint Taylorc89e39f2016-05-13 23:41:21 +030014009 ret = dev_priv->display.modeset_calc_cdclk(state);
14010 if (ret < 0)
14011 return ret;
14012
14013 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030014014 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014015 ret = intel_modeset_all_pipes(state);
14016
14017 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030014018 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010014019
14020 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
14021 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014022 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010014023 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030014024
Maarten Lankhorstad421372015-06-15 12:33:42 +020014025 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030014026
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014027 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020014028 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020014029
Maarten Lankhorstad421372015-06-15 12:33:42 +020014030 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030014031}
14032
Matt Roperaa363132015-09-24 15:53:18 -070014033/*
14034 * Handle calculation of various watermark data at the end of the atomic check
14035 * phase. The code here should be run after the per-crtc and per-plane 'check'
14036 * handlers to ensure that all derived state has been updated.
14037 */
Matt Roper55994c22016-05-12 07:06:08 -070014038static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070014039{
14040 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070014041 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070014042
14043 /* Is there platform-specific watermark information to calculate? */
14044 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070014045 return dev_priv->display.compute_global_watermarks(state);
14046
14047 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070014048}
14049
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014050/**
14051 * intel_atomic_check - validate state object
14052 * @dev: drm device
14053 * @state: state to validate
14054 */
14055static int intel_atomic_check(struct drm_device *dev,
14056 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020014057{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020014058 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070014059 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014060 struct drm_crtc *crtc;
14061 struct drm_crtc_state *crtc_state;
14062 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020014063 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014064
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014065 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020014066 if (ret)
14067 return ret;
14068
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014069 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014070 struct intel_crtc_state *pipe_config =
14071 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020014072
14073 /* Catch I915_MODE_FLAG_INHERITED */
14074 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
14075 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014076
Daniel Vetter26495482015-07-15 14:15:52 +020014077 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014078 continue;
14079
Daniel Vetteraf4a8792016-05-09 09:31:25 +020014080 if (!crtc_state->enable) {
14081 any_ms = true;
14082 continue;
14083 }
14084
Daniel Vetter26495482015-07-15 14:15:52 +020014085 /* FIXME: For only active_changed we shouldn't need to do any
14086 * state recomputation at all. */
14087
Daniel Vetter1ed51de2015-07-15 14:15:51 +020014088 ret = drm_atomic_add_affected_connectors(state, crtc);
14089 if (ret)
14090 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020014091
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014092 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020014093 if (ret) {
14094 intel_dump_pipe_config(to_intel_crtc(crtc),
14095 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014096 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020014097 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014098
Jani Nikula73831232015-11-19 10:26:30 +020014099 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020014100 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014101 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020014102 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020014103 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020014104 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020014105 }
14106
Daniel Vetteraf4a8792016-05-09 09:31:25 +020014107 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020014108 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020014109
Daniel Vetteraf4a8792016-05-09 09:31:25 +020014110 ret = drm_atomic_add_affected_planes(state, crtc);
14111 if (ret)
14112 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020014113
Daniel Vetter26495482015-07-15 14:15:52 +020014114 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
14115 needs_modeset(crtc_state) ?
14116 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014117 }
14118
Maarten Lankhorst61333b62015-06-15 12:33:50 +020014119 if (any_ms) {
14120 ret = intel_modeset_checks(state);
14121
14122 if (ret)
14123 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014124 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020014125 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020014126
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020014127 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070014128 if (ret)
14129 return ret;
14130
Paulo Zanonif51be2e2016-01-19 11:35:50 -020014131 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070014132 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020014133}
14134
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014135static int intel_atomic_prepare_commit(struct drm_device *dev,
Chris Wilsond07f0e52016-10-28 13:58:44 +010014136 struct drm_atomic_state *state)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014137{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014138 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014139 struct drm_crtc_state *crtc_state;
14140 struct drm_crtc *crtc;
14141 int i, ret;
14142
Daniel Vetter5a21b662016-05-24 17:13:53 +020014143 for_each_crtc_in_state(state, crtc, crtc_state, i) {
14144 if (state->legacy_cursor_update)
14145 continue;
14146
14147 ret = intel_crtc_wait_for_pending_flips(crtc);
14148 if (ret)
14149 return ret;
14150
14151 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
14152 flush_workqueue(dev_priv->wq);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020014153 }
14154
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014155 ret = mutex_lock_interruptible(&dev->struct_mutex);
14156 if (ret)
14157 return ret;
14158
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014159 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010014160 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014161
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014162 return ret;
14163}
14164
Maarten Lankhorsta2991412016-05-17 15:07:48 +020014165u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
14166{
14167 struct drm_device *dev = crtc->base.dev;
14168
14169 if (!dev->max_vblank_count)
14170 return drm_accurate_vblank_count(&crtc->base);
14171
14172 return dev->driver->get_vblank_counter(dev, crtc->pipe);
14173}
14174
Daniel Vetter5a21b662016-05-24 17:13:53 +020014175static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
14176 struct drm_i915_private *dev_priv,
14177 unsigned crtc_mask)
Maarten Lankhorste8861672016-02-24 11:24:26 +010014178{
Daniel Vetter5a21b662016-05-24 17:13:53 +020014179 unsigned last_vblank_count[I915_MAX_PIPES];
14180 enum pipe pipe;
14181 int ret;
Maarten Lankhorste8861672016-02-24 11:24:26 +010014182
Daniel Vetter5a21b662016-05-24 17:13:53 +020014183 if (!crtc_mask)
14184 return;
Maarten Lankhorste8861672016-02-24 11:24:26 +010014185
Daniel Vetter5a21b662016-05-24 17:13:53 +020014186 for_each_pipe(dev_priv, pipe) {
14187 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorste8861672016-02-24 11:24:26 +010014188
Daniel Vetter5a21b662016-05-24 17:13:53 +020014189 if (!((1 << pipe) & crtc_mask))
Maarten Lankhorste8861672016-02-24 11:24:26 +010014190 continue;
14191
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020014192 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014193 if (WARN_ON(ret != 0)) {
14194 crtc_mask &= ~(1 << pipe);
14195 continue;
14196 }
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020014197
Daniel Vetter5a21b662016-05-24 17:13:53 +020014198 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
14199 }
14200
14201 for_each_pipe(dev_priv, pipe) {
14202 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
14203 long lret;
14204
14205 if (!((1 << pipe) & crtc_mask))
14206 continue;
14207
14208 lret = wait_event_timeout(dev->vblank[pipe].queue,
14209 last_vblank_count[pipe] !=
14210 drm_crtc_vblank_count(crtc),
14211 msecs_to_jiffies(50));
14212
14213 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
14214
14215 drm_crtc_vblank_put(crtc);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020014216 }
14217}
14218
Daniel Vetter5a21b662016-05-24 17:13:53 +020014219static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020014220{
Daniel Vetter5a21b662016-05-24 17:13:53 +020014221 /* fb updated, need to unpin old fb */
14222 if (crtc_state->fb_changed)
14223 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020014224
Daniel Vetter5a21b662016-05-24 17:13:53 +020014225 /* wm changes, need vblank before final wm's */
14226 if (crtc_state->update_wm_post)
14227 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020014228
Daniel Vetter5a21b662016-05-24 17:13:53 +020014229 /*
14230 * cxsr is re-enabled after vblank.
14231 * This is already handled by crtc_state->update_wm_post,
14232 * but added for clarity.
14233 */
14234 if (crtc_state->disable_cxsr)
14235 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020014236
Daniel Vetter5a21b662016-05-24 17:13:53 +020014237 return false;
Maarten Lankhorste8861672016-02-24 11:24:26 +010014238}
14239
Lyude896e5bb2016-08-24 07:48:09 +020014240static void intel_update_crtc(struct drm_crtc *crtc,
14241 struct drm_atomic_state *state,
14242 struct drm_crtc_state *old_crtc_state,
14243 unsigned int *crtc_vblank_mask)
14244{
14245 struct drm_device *dev = crtc->dev;
14246 struct drm_i915_private *dev_priv = to_i915(dev);
14247 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14248 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
14249 bool modeset = needs_modeset(crtc->state);
14250
14251 if (modeset) {
14252 update_scanline_offset(intel_crtc);
14253 dev_priv->display.crtc_enable(pipe_config, state);
14254 } else {
14255 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
14256 }
14257
14258 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
14259 intel_fbc_enable(
14260 intel_crtc, pipe_config,
14261 to_intel_plane_state(crtc->primary->state));
14262 }
14263
14264 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
14265
14266 if (needs_vblank_wait(pipe_config))
14267 *crtc_vblank_mask |= drm_crtc_mask(crtc);
14268}
14269
14270static void intel_update_crtcs(struct drm_atomic_state *state,
14271 unsigned int *crtc_vblank_mask)
14272{
14273 struct drm_crtc *crtc;
14274 struct drm_crtc_state *old_crtc_state;
14275 int i;
14276
14277 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14278 if (!crtc->state->active)
14279 continue;
14280
14281 intel_update_crtc(crtc, state, old_crtc_state,
14282 crtc_vblank_mask);
14283 }
14284}
14285
Lyude27082492016-08-24 07:48:10 +020014286static void skl_update_crtcs(struct drm_atomic_state *state,
14287 unsigned int *crtc_vblank_mask)
14288{
14289 struct drm_device *dev = state->dev;
Lyude27082492016-08-24 07:48:10 +020014290 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14291 struct drm_crtc *crtc;
Lyudece0ba282016-09-15 10:46:35 -040014292 struct intel_crtc *intel_crtc;
Lyude27082492016-08-24 07:48:10 +020014293 struct drm_crtc_state *old_crtc_state;
Lyudece0ba282016-09-15 10:46:35 -040014294 struct intel_crtc_state *cstate;
Lyude27082492016-08-24 07:48:10 +020014295 unsigned int updated = 0;
14296 bool progress;
14297 enum pipe pipe;
14298
14299 /*
14300 * Whenever the number of active pipes changes, we need to make sure we
14301 * update the pipes in the right order so that their ddb allocations
14302 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
14303 * cause pipe underruns and other bad stuff.
14304 */
14305 do {
14306 int i;
14307 progress = false;
14308
14309 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14310 bool vbl_wait = false;
14311 unsigned int cmask = drm_crtc_mask(crtc);
Lyudece0ba282016-09-15 10:46:35 -040014312
14313 intel_crtc = to_intel_crtc(crtc);
14314 cstate = to_intel_crtc_state(crtc->state);
14315 pipe = intel_crtc->pipe;
Lyude27082492016-08-24 07:48:10 +020014316
14317 if (updated & cmask || !crtc->state->active)
14318 continue;
Lyudece0ba282016-09-15 10:46:35 -040014319 if (skl_ddb_allocation_overlaps(state, intel_crtc))
Lyude27082492016-08-24 07:48:10 +020014320 continue;
14321
14322 updated |= cmask;
14323
14324 /*
14325 * If this is an already active pipe, it's DDB changed,
14326 * and this isn't the last pipe that needs updating
14327 * then we need to wait for a vblank to pass for the
14328 * new ddb allocation to take effect.
14329 */
Lyudece0ba282016-09-15 10:46:35 -040014330 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
14331 &intel_crtc->hw_ddb) &&
Lyude27082492016-08-24 07:48:10 +020014332 !crtc->state->active_changed &&
14333 intel_state->wm_results.dirty_pipes != updated)
14334 vbl_wait = true;
14335
14336 intel_update_crtc(crtc, state, old_crtc_state,
14337 crtc_vblank_mask);
14338
14339 if (vbl_wait)
14340 intel_wait_for_vblank(dev, pipe);
14341
14342 progress = true;
14343 }
14344 } while (progress);
14345}
14346
Daniel Vetter94f05022016-06-14 18:01:00 +020014347static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020014348{
Daniel Vetter94f05022016-06-14 18:01:00 +020014349 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014350 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010014351 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014352 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014353 struct drm_crtc *crtc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020014354 struct intel_crtc_state *intel_cstate;
Daniel Vetter5a21b662016-05-24 17:13:53 +020014355 bool hw_check = intel_state->modeset;
14356 unsigned long put_domains[I915_MAX_PIPES] = {};
14357 unsigned crtc_vblank_mask = 0;
Chris Wilsone95433c2016-10-28 13:58:27 +010014358 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020014359
Daniel Vetterea0000f2016-06-13 16:13:46 +020014360 drm_atomic_helper_wait_for_dependencies(state);
14361
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014362 if (intel_state->modeset) {
14363 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
14364 sizeof(intel_state->min_pixclk));
14365 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010014366 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Daniel Vetter5a21b662016-05-24 17:13:53 +020014367
14368 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014369 }
14370
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014371 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020014372 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14373
Daniel Vetter5a21b662016-05-24 17:13:53 +020014374 if (needs_modeset(crtc->state) ||
14375 to_intel_crtc_state(crtc->state)->update_pipe) {
14376 hw_check = true;
14377
14378 put_domains[to_intel_crtc(crtc)->pipe] =
14379 modeset_get_crtc_power_domains(crtc,
14380 to_intel_crtc_state(crtc->state));
14381 }
14382
Maarten Lankhorst61333b62015-06-15 12:33:50 +020014383 if (!needs_modeset(crtc->state))
14384 continue;
14385
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014386 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010014387
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014388 if (old_crtc_state->active) {
14389 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorst4a806552016-08-09 17:04:01 +020014390 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020014391 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020014392 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020014393 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020014394
14395 /*
14396 * Underruns don't always raise
14397 * interrupts, so check manually.
14398 */
14399 intel_check_cpu_fifo_underruns(dev_priv);
14400 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010014401
14402 if (!crtc->state->active)
14403 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020014404 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010014405 }
Daniel Vetter7758a112012-07-08 19:40:39 +020014406
Daniel Vetterea9d7582012-07-10 10:42:52 +020014407 /* Only after disabling all output pipelines that will be changed can we
14408 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020014409 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020014410
Maarten Lankhorst565602d2015-12-10 12:33:57 +010014411 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020014412 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010014413
14414 if (dev_priv->display.modeset_commit_cdclk &&
Clint Taylorc89e39f2016-05-13 23:41:21 +030014415 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030014416 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010014417 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010014418
Lyude656d1b82016-08-17 15:55:54 -040014419 /*
14420 * SKL workaround: bspec recommends we disable the SAGV when we
14421 * have more then one pipe enabled
14422 */
Paulo Zanoni56feca92016-09-22 18:00:28 -030014423 if (!intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030014424 intel_disable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040014425
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020014426 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020014427 }
Daniel Vetter47fab732012-10-26 10:58:18 +020014428
Lyude896e5bb2016-08-24 07:48:09 +020014429 /* Complete the events for pipes that have now been disabled */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020014430 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020014431 bool modeset = needs_modeset(crtc->state);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020014432
Daniel Vetter1f7528c2016-06-13 16:13:45 +020014433 /* Complete events for now disable pipes here. */
14434 if (modeset && !crtc->state->active && crtc->state->event) {
14435 spin_lock_irq(&dev->event_lock);
14436 drm_crtc_send_vblank_event(crtc, crtc->state->event);
14437 spin_unlock_irq(&dev->event_lock);
14438
14439 crtc->state->event = NULL;
14440 }
Matt Ropered4a6a72016-02-23 17:20:13 -080014441 }
14442
Lyude896e5bb2016-08-24 07:48:09 +020014443 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
14444 dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
14445
Daniel Vetter94f05022016-06-14 18:01:00 +020014446 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
14447 * already, but still need the state for the delayed optimization. To
14448 * fix this:
14449 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
14450 * - schedule that vblank worker _before_ calling hw_done
14451 * - at the start of commit_tail, cancel it _synchrously
14452 * - switch over to the vblank wait helper in the core after that since
14453 * we don't need out special handling any more.
14454 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020014455 if (!state->legacy_cursor_update)
14456 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
14457
14458 /*
14459 * Now that the vblank has passed, we can go ahead and program the
14460 * optimal watermarks on platforms that need two-step watermark
14461 * programming.
14462 *
14463 * TODO: Move this (and other cleanup) to an async worker eventually.
14464 */
14465 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14466 intel_cstate = to_intel_crtc_state(crtc->state);
14467
14468 if (dev_priv->display.optimize_watermarks)
14469 dev_priv->display.optimize_watermarks(intel_cstate);
14470 }
14471
14472 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14473 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
14474
14475 if (put_domains[i])
14476 modeset_put_power_domains(dev_priv, put_domains[i]);
14477
14478 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
14479 }
14480
Paulo Zanoni56feca92016-09-22 18:00:28 -030014481 if (intel_state->modeset && intel_can_enable_sagv(state))
Paulo Zanoni16dcdc42016-09-22 18:00:27 -030014482 intel_enable_sagv(dev_priv);
Lyude656d1b82016-08-17 15:55:54 -040014483
Daniel Vetter94f05022016-06-14 18:01:00 +020014484 drm_atomic_helper_commit_hw_done(state);
14485
Daniel Vetter5a21b662016-05-24 17:13:53 +020014486 if (intel_state->modeset)
14487 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
14488
14489 mutex_lock(&dev->struct_mutex);
14490 drm_atomic_helper_cleanup_planes(dev, state);
14491 mutex_unlock(&dev->struct_mutex);
14492
Daniel Vetterea0000f2016-06-13 16:13:46 +020014493 drm_atomic_helper_commit_cleanup_done(state);
14494
Chris Wilson08536952016-10-14 13:18:18 +010014495 drm_atomic_state_put(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080014496
Mika Kuoppala75714942015-12-16 09:26:48 +020014497 /* As one of the primary mmio accessors, KMS has a high likelihood
14498 * of triggering bugs in unclaimed access. After we finish
14499 * modesetting, see if an error has been flagged, and if so
14500 * enable debugging for the next modeset - and hope we catch
14501 * the culprit.
14502 *
14503 * XXX note that we assume display power is on at this point.
14504 * This might hold true now but we need to add pm helper to check
14505 * unclaimed only when the hardware is on, as atomic commits
14506 * can happen also when the device is completely off.
14507 */
14508 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter94f05022016-06-14 18:01:00 +020014509}
14510
14511static void intel_atomic_commit_work(struct work_struct *work)
14512{
14513 struct drm_atomic_state *state = container_of(work,
14514 struct drm_atomic_state,
14515 commit_work);
14516 intel_atomic_commit_tail(state);
14517}
14518
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014519static void intel_atomic_track_fbs(struct drm_atomic_state *state)
14520{
14521 struct drm_plane_state *old_plane_state;
14522 struct drm_plane *plane;
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014523 int i;
14524
Chris Wilsonfaf5bf02016-08-04 16:32:37 +010014525 for_each_plane_in_state(state, plane, old_plane_state, i)
14526 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
14527 intel_fb_obj(plane->state->fb),
14528 to_intel_plane(plane)->frontbuffer_bit);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014529}
14530
Daniel Vetter94f05022016-06-14 18:01:00 +020014531/**
14532 * intel_atomic_commit - commit validated state object
14533 * @dev: DRM device
14534 * @state: the top-level driver state object
14535 * @nonblock: nonblocking commit
14536 *
14537 * This function commits a top-level state object that has been validated
14538 * with drm_atomic_helper_check().
14539 *
14540 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
14541 * nonblocking commits are only safe for pure plane updates. Everything else
14542 * should work though.
14543 *
14544 * RETURNS
14545 * Zero for success or -errno.
14546 */
14547static int intel_atomic_commit(struct drm_device *dev,
14548 struct drm_atomic_state *state,
14549 bool nonblock)
14550{
14551 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010014552 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94f05022016-06-14 18:01:00 +020014553 int ret = 0;
14554
14555 if (intel_state->modeset && nonblock) {
14556 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
14557 return -EINVAL;
14558 }
14559
14560 ret = drm_atomic_helper_setup_commit(state, nonblock);
14561 if (ret)
14562 return ret;
14563
14564 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
14565
Chris Wilsond07f0e52016-10-28 13:58:44 +010014566 ret = intel_atomic_prepare_commit(dev, state);
Daniel Vetter94f05022016-06-14 18:01:00 +020014567 if (ret) {
14568 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
14569 return ret;
14570 }
14571
14572 drm_atomic_helper_swap_state(state, true);
14573 dev_priv->wm.distrust_bios_wm = false;
14574 dev_priv->wm.skl_results = intel_state->wm_results;
14575 intel_shared_dpll_commit(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014576 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020014577
Chris Wilson08536952016-10-14 13:18:18 +010014578 drm_atomic_state_get(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020014579 if (nonblock)
14580 queue_work(system_unbound_wq, &state->commit_work);
14581 else
14582 intel_atomic_commit_tail(state);
Mika Kuoppala75714942015-12-16 09:26:48 +020014583
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014584 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020014585}
14586
Chris Wilsonc0c36b942012-12-19 16:08:43 +000014587void intel_crtc_restore_mode(struct drm_crtc *crtc)
14588{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014589 struct drm_device *dev = crtc->dev;
14590 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014591 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030014592 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014593
14594 state = drm_atomic_state_alloc(dev);
14595 if (!state) {
Ville Syrjälä78108b72016-05-27 20:59:19 +030014596 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
14597 crtc->base.id, crtc->name);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014598 return;
14599 }
14600
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014601 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014602
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014603retry:
14604 crtc_state = drm_atomic_get_crtc_state(state, crtc);
14605 ret = PTR_ERR_OR_ZERO(crtc_state);
14606 if (!ret) {
14607 if (!crtc_state->active)
14608 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014609
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014610 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014611 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014612 }
14613
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014614 if (ret == -EDEADLK) {
14615 drm_atomic_state_clear(state);
14616 drm_modeset_backoff(state->acquire_ctx);
14617 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030014618 }
14619
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014620out:
Chris Wilson08536952016-10-14 13:18:18 +010014621 drm_atomic_state_put(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000014622}
14623
Bob Paauwea8784872016-07-15 14:59:02 +010014624/*
14625 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
14626 * drm_atomic_helper_legacy_gamma_set() directly.
14627 */
14628static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
14629 u16 *red, u16 *green, u16 *blue,
14630 uint32_t size)
14631{
14632 struct drm_device *dev = crtc->dev;
14633 struct drm_mode_config *config = &dev->mode_config;
14634 struct drm_crtc_state *state;
14635 int ret;
14636
14637 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
14638 if (ret)
14639 return ret;
14640
14641 /*
14642 * Make sure we update the legacy properties so this works when
14643 * atomic is not enabled.
14644 */
14645
14646 state = crtc->state;
14647
14648 drm_object_property_set_value(&crtc->base,
14649 config->degamma_lut_property,
14650 (state->degamma_lut) ?
14651 state->degamma_lut->base.id : 0);
14652
14653 drm_object_property_set_value(&crtc->base,
14654 config->ctm_property,
14655 (state->ctm) ?
14656 state->ctm->base.id : 0);
14657
14658 drm_object_property_set_value(&crtc->base,
14659 config->gamma_lut_property,
14660 (state->gamma_lut) ?
14661 state->gamma_lut->base.id : 0);
14662
14663 return 0;
14664}
14665
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014666static const struct drm_crtc_funcs intel_crtc_funcs = {
Bob Paauwea8784872016-07-15 14:59:02 +010014667 .gamma_set = intel_atomic_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014668 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000014669 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014670 .destroy = intel_crtc_destroy,
Chris Wilson527b6ab2016-06-24 13:44:03 +010014671 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080014672 .atomic_duplicate_state = intel_crtc_duplicate_state,
14673 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014674};
14675
Matt Roper6beb8c232014-12-01 15:40:14 -080014676/**
14677 * intel_prepare_plane_fb - Prepare fb for usage on plane
14678 * @plane: drm plane to prepare for
14679 * @fb: framebuffer to prepare for presentation
14680 *
14681 * Prepares a framebuffer for usage on a display plane. Generally this
14682 * involves pinning the underlying object and updating the frontbuffer tracking
14683 * bits. Some older platforms need special physical address handling for
14684 * cursor planes.
14685 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014686 * Must be called with struct_mutex held.
14687 *
Matt Roper6beb8c232014-12-01 15:40:14 -080014688 * Returns 0 on success, negative error code on failure.
14689 */
14690int
14691intel_prepare_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010014692 struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070014693{
14694 struct drm_device *dev = plane->dev;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010014695 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst844f9112015-09-02 10:42:40 +020014696 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080014697 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014698 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsond07f0e52016-10-28 13:58:44 +010014699 long lret;
Matt Roper6beb8c232014-12-01 15:40:14 -080014700 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070014701
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014702 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070014703 return 0;
14704
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014705 if (old_obj) {
14706 struct drm_crtc_state *crtc_state =
14707 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14708
14709 /* Big Hammer, we also need to ensure that any pending
14710 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14711 * current scanout is retired before unpinning the old
14712 * framebuffer. Note that we rely on userspace rendering
14713 * into the buffer attached to the pipe they are waiting
14714 * on. If not, userspace generates a GPU hang with IPEHR
14715 * point to the MI_WAIT_FOR_EVENT.
14716 *
14717 * This should only fail upon a hung GPU, in which case we
14718 * can safely continue.
14719 */
14720 if (needs_modeset(crtc_state))
Chris Wilsone95433c2016-10-28 13:58:27 +010014721 ret = i915_gem_object_wait(old_obj,
14722 I915_WAIT_INTERRUPTIBLE |
14723 I915_WAIT_LOCKED,
14724 MAX_SCHEDULE_TIMEOUT,
14725 NULL);
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014726 if (ret) {
14727 /* GPU hangs should have been swallowed by the wait */
14728 WARN_ON(ret == -EIO);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014729 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014730 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014731 }
14732
Chris Wilsonc37efb92016-06-17 08:28:47 +010014733 if (!obj)
14734 return 0;
14735
Daniel Vetter5a21b662016-05-24 17:13:53 +020014736 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsond07f0e52016-10-28 13:58:44 +010014737 lret = i915_gem_object_wait(obj,
14738 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
14739 MAX_SCHEDULE_TIMEOUT,
14740 NULL);
14741 if (lret == -ERESTARTSYS)
14742 return lret;
Daniel Vetter5a21b662016-05-24 17:13:53 +020014743
Chris Wilsond07f0e52016-10-28 13:58:44 +010014744 WARN(lret < 0, "waiting returns %li\n", lret);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014745
Chris Wilsonc37efb92016-06-17 08:28:47 +010014746 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080014747 INTEL_INFO(dev)->cursor_needs_physical) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010014748 int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
Matt Roper6beb8c232014-12-01 15:40:14 -080014749 ret = i915_gem_object_attach_phys(obj, align);
Chris Wilsond07f0e52016-10-28 13:58:44 +010014750 if (ret) {
Matt Roper6beb8c232014-12-01 15:40:14 -080014751 DRM_DEBUG_KMS("failed to attach phys object\n");
Chris Wilsond07f0e52016-10-28 13:58:44 +010014752 return ret;
14753 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014754 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +010014755 struct i915_vma *vma;
14756
14757 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Chris Wilsond07f0e52016-10-28 13:58:44 +010014758 if (IS_ERR(vma)) {
14759 DRM_DEBUG_KMS("failed to pin object\n");
14760 return PTR_ERR(vma);
14761 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014762 }
14763
Chris Wilsond07f0e52016-10-28 13:58:44 +010014764 return 0;
Matt Roper6beb8c232014-12-01 15:40:14 -080014765}
14766
Matt Roper38f3ce32014-12-02 07:45:25 -080014767/**
14768 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14769 * @plane: drm plane to clean up for
14770 * @fb: old framebuffer that was on plane
14771 *
14772 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014773 *
14774 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080014775 */
14776void
14777intel_cleanup_plane_fb(struct drm_plane *plane,
Chris Wilson18320402016-08-18 19:00:16 +010014778 struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080014779{
14780 struct drm_device *dev = plane->dev;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014781 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014782 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14783 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080014784
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014785 old_intel_state = to_intel_plane_state(old_state);
14786
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014787 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080014788 return;
14789
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014790 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14791 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020014792 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Matt Roper465c1202014-05-29 08:06:54 -070014793}
14794
Chandra Konduru6156a452015-04-27 13:48:39 -070014795int
14796skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14797{
14798 int max_scale;
Chandra Konduru6156a452015-04-27 13:48:39 -070014799 int crtc_clock, cdclk;
14800
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010014801 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070014802 return DRM_PLANE_HELPER_NO_SCALING;
14803
Chandra Konduru6156a452015-04-27 13:48:39 -070014804 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014805 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070014806
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010014807 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070014808 return DRM_PLANE_HELPER_NO_SCALING;
14809
14810 /*
14811 * skl max scale is lower of:
14812 * close to 3 but not 3, -1 is for that purpose
14813 * or
14814 * cdclk/crtc_clock
14815 */
14816 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14817
14818 return max_scale;
14819}
14820
Matt Roper465c1202014-05-29 08:06:54 -070014821static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014822intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014823 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014824 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014825{
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014826 struct drm_i915_private *dev_priv = to_i915(plane->dev);
Matt Roper2b875c22014-12-01 15:40:13 -080014827 struct drm_crtc *crtc = state->base.crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070014828 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014829 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14830 bool can_position = false;
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014831 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014832
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014833 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjälä693bdc22016-01-15 20:46:53 +020014834 /* use scaler when colorkey is not required */
14835 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14836 min_scale = 1;
14837 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14838 }
Sonika Jindald8106362015-04-10 14:37:28 +053014839 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014840 }
Sonika Jindald8106362015-04-10 14:37:28 +053014841
Daniel Vettercc926382016-08-15 10:41:47 +020014842 ret = drm_plane_helper_check_state(&state->base,
14843 &state->clip,
14844 min_scale, max_scale,
14845 can_position, true);
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014846 if (ret)
14847 return ret;
14848
Daniel Vettercc926382016-08-15 10:41:47 +020014849 if (!state->base.fb)
Ville Syrjäläb63a16f2016-01-28 16:53:54 +020014850 return 0;
14851
14852 if (INTEL_GEN(dev_priv) >= 9) {
14853 ret = skl_check_plane_surface(state);
14854 if (ret)
14855 return ret;
14856 }
14857
14858 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070014859}
14860
Daniel Vetter5a21b662016-05-24 17:13:53 +020014861static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14862 struct drm_crtc_state *old_crtc_state)
14863{
14864 struct drm_device *dev = crtc->dev;
Lyude62e0fb82016-08-22 12:50:08 -040014865 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014866 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Lyudeb707aa52016-09-15 10:56:06 -040014867 struct intel_crtc_state *intel_cstate =
14868 to_intel_crtc_state(crtc->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020014869 struct intel_crtc_state *old_intel_state =
14870 to_intel_crtc_state(old_crtc_state);
14871 bool modeset = needs_modeset(crtc->state);
Lyude62e0fb82016-08-22 12:50:08 -040014872 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter5a21b662016-05-24 17:13:53 +020014873
14874 /* Perform vblank evasion around commit operation */
14875 intel_pipe_update_start(intel_crtc);
14876
14877 if (modeset)
14878 return;
14879
14880 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14881 intel_color_set_csc(crtc->state);
14882 intel_color_load_luts(crtc->state);
14883 }
14884
Lyudeb707aa52016-09-15 10:56:06 -040014885 if (intel_cstate->update_pipe) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020014886 intel_update_pipe_config(intel_crtc, old_intel_state);
Lyudeb707aa52016-09-15 10:56:06 -040014887 } else if (INTEL_GEN(dev_priv) >= 9) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020014888 skl_detach_scalers(intel_crtc);
Lyude62e0fb82016-08-22 12:50:08 -040014889
14890 I915_WRITE(PIPE_WM_LINETIME(pipe),
Lyudeb707aa52016-09-15 10:56:06 -040014891 intel_cstate->wm.skl.optimal.linetime);
Lyude62e0fb82016-08-22 12:50:08 -040014892 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020014893}
14894
14895static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14896 struct drm_crtc_state *old_crtc_state)
14897{
14898 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14899
14900 intel_pipe_update_end(intel_crtc, NULL);
14901}
14902
Matt Ropercf4c7c12014-12-04 10:27:42 -080014903/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014904 * intel_plane_destroy - destroy a plane
14905 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014906 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014907 * Common destruction function for all types of planes (primary, cursor,
14908 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014909 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014910void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014911{
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014912 if (!plane)
14913 return;
14914
Matt Roper465c1202014-05-29 08:06:54 -070014915 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014916 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070014917}
14918
Matt Roper65a3fea2015-01-21 16:35:42 -080014919const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014920 .update_plane = drm_atomic_helper_update_plane,
14921 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014922 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014923 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014924 .atomic_get_property = intel_plane_atomic_get_property,
14925 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014926 .atomic_duplicate_state = intel_plane_duplicate_state,
14927 .atomic_destroy_state = intel_plane_destroy_state,
14928
Matt Roper465c1202014-05-29 08:06:54 -070014929};
14930
14931static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14932 int pipe)
14933{
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010014934 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014935 struct intel_plane *primary = NULL;
14936 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014937 const uint32_t *intel_primary_formats;
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030014938 unsigned int supported_rotations;
Thierry Reding45e37432015-08-12 16:54:28 +020014939 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014940 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014941
14942 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014943 if (!primary)
14944 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070014945
Matt Roper8e7d6882015-01-21 16:35:41 -080014946 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014947 if (!state)
14948 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014949 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014950
Matt Roper465c1202014-05-29 08:06:54 -070014951 primary->can_scale = false;
14952 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014953 if (INTEL_INFO(dev)->gen >= 9) {
14954 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014955 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014956 }
Matt Roper465c1202014-05-29 08:06:54 -070014957 primary->pipe = pipe;
14958 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014959 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014960 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014961 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14962 primary->plane = !pipe;
14963
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014964 if (INTEL_INFO(dev)->gen >= 9) {
14965 intel_primary_formats = skl_primary_formats;
14966 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014967
14968 primary->update_plane = skylake_update_primary_plane;
14969 primary->disable_plane = skylake_disable_primary_plane;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010014970 } else if (HAS_PCH_SPLIT(dev_priv)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014971 intel_primary_formats = i965_primary_formats;
14972 num_formats = ARRAY_SIZE(i965_primary_formats);
14973
14974 primary->update_plane = ironlake_update_primary_plane;
14975 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014976 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014977 intel_primary_formats = i965_primary_formats;
14978 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014979
14980 primary->update_plane = i9xx_update_primary_plane;
14981 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014982 } else {
14983 intel_primary_formats = i8xx_primary_formats;
14984 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014985
14986 primary->update_plane = i9xx_update_primary_plane;
14987 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014988 }
14989
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014990 if (INTEL_INFO(dev)->gen >= 9)
14991 ret = drm_universal_plane_init(dev, &primary->base, 0,
14992 &intel_plane_funcs,
14993 intel_primary_formats, num_formats,
14994 DRM_PLANE_TYPE_PRIMARY,
14995 "plane 1%c", pipe_name(pipe));
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010014996 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014997 ret = drm_universal_plane_init(dev, &primary->base, 0,
14998 &intel_plane_funcs,
14999 intel_primary_formats, num_formats,
15000 DRM_PLANE_TYPE_PRIMARY,
15001 "primary %c", pipe_name(pipe));
15002 else
15003 ret = drm_universal_plane_init(dev, &primary->base, 0,
15004 &intel_plane_funcs,
15005 intel_primary_formats, num_formats,
15006 DRM_PLANE_TYPE_PRIMARY,
15007 "plane %c", plane_name(primary->plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015008 if (ret)
15009 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053015010
Dave Airlie5481e272016-10-25 16:36:13 +100015011 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030015012 supported_rotations =
15013 DRM_ROTATE_0 | DRM_ROTATE_90 |
15014 DRM_ROTATE_180 | DRM_ROTATE_270;
Dave Airlie5481e272016-10-25 16:36:13 +100015015 } else if (INTEL_GEN(dev_priv) >= 4) {
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030015016 supported_rotations =
15017 DRM_ROTATE_0 | DRM_ROTATE_180;
15018 } else {
15019 supported_rotations = DRM_ROTATE_0;
15020 }
15021
Dave Airlie5481e272016-10-25 16:36:13 +100015022 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030015023 drm_plane_create_rotation_property(&primary->base,
15024 DRM_ROTATE_0,
15025 supported_rotations);
Sonika Jindal48404c12014-08-22 14:06:04 +053015026
Matt Roperea2c67b2014-12-23 10:41:52 -080015027 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
15028
Matt Roper465c1202014-05-29 08:06:54 -070015029 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015030
15031fail:
15032 kfree(state);
15033 kfree(primary);
15034
15035 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070015036}
15037
Matt Roper3d7d6512014-06-10 08:28:13 -070015038static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030015039intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020015040 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030015041 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070015042{
Matt Roper2b875c22014-12-01 15:40:13 -080015043 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015044 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020015045 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015046 unsigned stride;
15047 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030015048
Ville Syrjäläf8856a42016-07-26 19:07:00 +030015049 ret = drm_plane_helper_check_state(&state->base,
15050 &state->clip,
15051 DRM_PLANE_HELPER_NO_SCALING,
15052 DRM_PLANE_HELPER_NO_SCALING,
15053 true, true);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015054 if (ret)
15055 return ret;
15056
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015057 /* if we want to turn off the cursor ignore width and height */
15058 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020015059 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015060
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015061 /* Check for which cursor types we support */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010015062 if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
15063 state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080015064 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
15065 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015066 return -EINVAL;
15067 }
15068
Matt Roperea2c67b2014-12-23 10:41:52 -080015069 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
15070 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015071 DRM_DEBUG_KMS("buffer is too small\n");
15072 return -ENOMEM;
15073 }
15074
Ville Syrjälä3a656b52015-03-09 21:08:37 +020015075 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015076 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020015077 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015078 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030015079
Ville Syrjäläb29ec922015-12-18 19:24:39 +020015080 /*
15081 * There's something wrong with the cursor on CHV pipe C.
15082 * If it straddles the left edge of the screen then
15083 * moving it away from the edge or disabling it often
15084 * results in a pipe underrun, and often that can lead to
15085 * dead pipe (constant underrun reported, and it scans
15086 * out just a solid color). To recover from that, the
15087 * display power well must be turned off and on again.
15088 * Refuse the put the cursor into that compromised position.
15089 */
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015090 if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
Ville Syrjälä936e71e2016-07-26 19:06:59 +030015091 state->base.visible && state->base.crtc_x < 0) {
Ville Syrjäläb29ec922015-12-18 19:24:39 +020015092 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
15093 return -EINVAL;
15094 }
15095
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020015096 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030015097}
15098
Matt Roperf4a2cf22014-12-01 15:40:12 -080015099static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030015100intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020015101 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030015102{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010015103 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
15104
15105 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015106 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030015107}
15108
15109static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015110intel_update_cursor_plane(struct drm_plane *plane,
15111 const struct intel_crtc_state *crtc_state,
15112 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030015113{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015114 struct drm_crtc *crtc = crtc_state->base.crtc;
15115 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080015116 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080015117 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080015118 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070015119
Matt Roperf4a2cf22014-12-01 15:40:12 -080015120 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080015121 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080015122 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Chris Wilson058d88c2016-08-15 10:49:06 +010015123 addr = i915_gem_object_ggtt_offset(obj, NULL);
Matt Roperf4a2cf22014-12-01 15:40:12 -080015124 else
Gustavo Padovana912f122014-12-01 15:40:10 -080015125 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080015126
Gustavo Padovana912f122014-12-01 15:40:10 -080015127 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015128 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070015129}
Gustavo Padovan852e7872014-09-05 17:22:31 -030015130
Matt Roper3d7d6512014-06-10 08:28:13 -070015131static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
15132 int pipe)
15133{
Dave Airlie5481e272016-10-25 16:36:13 +100015134 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015135 struct intel_plane *cursor = NULL;
15136 struct intel_plane_state *state = NULL;
15137 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070015138
15139 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015140 if (!cursor)
15141 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070015142
Matt Roper8e7d6882015-01-21 16:35:41 -080015143 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015144 if (!state)
15145 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080015146 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080015147
Matt Roper3d7d6512014-06-10 08:28:13 -070015148 cursor->can_scale = false;
15149 cursor->max_downscale = 1;
15150 cursor->pipe = pipe;
15151 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030015152 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080015153 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010015154 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030015155 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070015156
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015157 ret = drm_universal_plane_init(dev, &cursor->base, 0,
15158 &intel_plane_funcs,
15159 intel_cursor_formats,
15160 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjälä38573dc2016-05-27 20:59:23 +030015161 DRM_PLANE_TYPE_CURSOR,
15162 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015163 if (ret)
15164 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070015165
Dave Airlie5481e272016-10-25 16:36:13 +100015166 if (INTEL_GEN(dev_priv) >= 4)
Ville Syrjälä93ca7e02016-09-26 19:30:56 +030015167 drm_plane_create_rotation_property(&cursor->base,
15168 DRM_ROTATE_0,
15169 DRM_ROTATE_0 |
15170 DRM_ROTATE_180);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070015171
Chandra Konduruaf99ced2015-05-11 14:35:47 -070015172 if (INTEL_INFO(dev)->gen >=9)
15173 state->scaler_id = -1;
15174
Matt Roperea2c67b2014-12-23 10:41:52 -080015175 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
15176
Matt Roper3d7d6512014-06-10 08:28:13 -070015177 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000015178
15179fail:
15180 kfree(state);
15181 kfree(cursor);
15182
15183 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070015184}
15185
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015186static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
15187 struct intel_crtc_state *crtc_state)
15188{
15189 int i;
15190 struct intel_scaler *intel_scaler;
15191 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
15192
15193 for (i = 0; i < intel_crtc->num_scalers; i++) {
15194 intel_scaler = &scaler_state->scalers[i];
15195 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015196 intel_scaler->mode = PS_SCALER_MODE_DYN;
15197 }
15198
15199 scaler_state->scaler_id = -1;
15200}
15201
Hannes Ederb358d0a2008-12-18 21:18:47 +010015202static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080015203{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015204 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015205 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015206 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070015207 struct drm_plane *primary = NULL;
15208 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000015209 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080015210
Daniel Vetter955382f2013-09-19 14:05:45 +020015211 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080015212 if (intel_crtc == NULL)
15213 return;
15214
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015215 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
15216 if (!crtc_state)
15217 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030015218 intel_crtc->config = crtc_state;
15219 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080015220 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015221
Chandra Konduru549e2bf2015-04-07 15:28:38 -070015222 /* initialize shared scalers */
15223 if (INTEL_INFO(dev)->gen >= 9) {
15224 if (pipe == PIPE_C)
15225 intel_crtc->num_scalers = 1;
15226 else
15227 intel_crtc->num_scalers = SKL_NUM_SCALERS;
15228
15229 skl_init_scalers(dev, intel_crtc, crtc_state);
15230 }
15231
Matt Roper465c1202014-05-29 08:06:54 -070015232 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070015233 if (!primary)
15234 goto fail;
15235
15236 cursor = intel_cursor_plane_create(dev, pipe);
15237 if (!cursor)
15238 goto fail;
15239
Matt Roper465c1202014-05-29 08:06:54 -070015240 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030015241 cursor, &intel_crtc_funcs,
15242 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070015243 if (ret)
15244 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080015245
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020015246 /*
15247 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020015248 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020015249 */
Jesse Barnes80824002009-09-10 15:28:06 -070015250 intel_crtc->pipe = pipe;
15251 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010015252 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080015253 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010015254 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070015255 }
15256
Chris Wilson4b0e3332014-05-30 16:35:26 +030015257 intel_crtc->cursor_base = ~0;
15258 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030015259 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030015260
Ville Syrjälä852eb002015-06-24 22:00:07 +030015261 intel_crtc->wm.cxsr_allowed = true;
15262
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080015263 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
15264 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
15265 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
15266 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
15267
Jesse Barnes79e53942008-11-07 14:24:08 -080015268 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020015269
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000015270 intel_color_init(&intel_crtc->base);
15271
Daniel Vetter87b6b102014-05-15 15:33:46 +020015272 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070015273 return;
15274
15275fail:
Ville Syrjälä69ae5612016-05-27 20:59:22 +030015276 intel_plane_destroy(primary);
15277 intel_plane_destroy(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020015278 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070015279 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080015280}
15281
Jesse Barnes752aa882013-10-31 18:55:49 +020015282enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
15283{
15284 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015285 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020015286
Rob Clark51fd3712013-11-19 12:10:12 -050015287 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020015288
Ville Syrjäläd3babd32014-11-07 11:16:01 +020015289 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020015290 return INVALID_PIPE;
15291
15292 return to_intel_crtc(encoder->crtc)->pipe;
15293}
15294
Carl Worth08d7b3d2009-04-29 14:43:54 -070015295int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000015296 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070015297{
Carl Worth08d7b3d2009-04-29 14:43:54 -070015298 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040015299 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020015300 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070015301
Rob Clark7707e652014-07-17 23:30:04 -040015302 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010015303 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030015304 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070015305
Rob Clark7707e652014-07-17 23:30:04 -040015306 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020015307 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070015308
Daniel Vetterc05422d2009-08-11 16:05:30 +020015309 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070015310}
15311
Daniel Vetter66a92782012-07-12 20:08:18 +020015312static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080015313{
Daniel Vetter66a92782012-07-12 20:08:18 +020015314 struct drm_device *dev = encoder->base.dev;
15315 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080015316 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080015317 int entry = 0;
15318
Damien Lespiaub2784e12014-08-05 11:29:37 +010015319 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020015320 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020015321 index_mask |= (1 << entry);
15322
Jesse Barnes79e53942008-11-07 14:24:08 -080015323 entry++;
15324 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010015325
Jesse Barnes79e53942008-11-07 14:24:08 -080015326 return index_mask;
15327}
15328
Chris Wilson4d302442010-12-14 19:21:29 +000015329static bool has_edp_a(struct drm_device *dev)
15330{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015331 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson4d302442010-12-14 19:21:29 +000015332
15333 if (!IS_MOBILE(dev))
15334 return false;
15335
15336 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
15337 return false;
15338
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015339 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000015340 return false;
15341
15342 return true;
15343}
15344
Jesse Barnes84b4e042014-06-25 08:24:29 -070015345static bool intel_crt_present(struct drm_device *dev)
15346{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015347 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes84b4e042014-06-25 08:24:29 -070015348
Damien Lespiau884497e2013-12-03 13:56:23 +000015349 if (INTEL_INFO(dev)->gen >= 9)
15350 return false;
15351
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010015352 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070015353 return false;
15354
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015355 if (IS_CHERRYVIEW(dev_priv))
Jesse Barnes84b4e042014-06-25 08:24:29 -070015356 return false;
15357
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010015358 if (HAS_PCH_LPT_H(dev_priv) &&
15359 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
Ville Syrjälä65e472e2015-12-01 23:28:55 +020015360 return false;
15361
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020015362 /* DDI E can't be used if DDI A requires 4 lanes */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010015363 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 +020015364 return false;
15365
Ville Syrjäläe4abb732015-12-01 23:31:33 +020015366 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070015367 return false;
15368
15369 return true;
15370}
15371
Imre Deak8090ba82016-08-10 14:07:33 +030015372void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
15373{
15374 int pps_num;
15375 int pps_idx;
15376
15377 if (HAS_DDI(dev_priv))
15378 return;
15379 /*
15380 * This w/a is needed at least on CPT/PPT, but to be sure apply it
15381 * everywhere where registers can be write protected.
15382 */
15383 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15384 pps_num = 2;
15385 else
15386 pps_num = 1;
15387
15388 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
15389 u32 val = I915_READ(PP_CONTROL(pps_idx));
15390
15391 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
15392 I915_WRITE(PP_CONTROL(pps_idx), val);
15393 }
15394}
15395
Imre Deak44cb7342016-08-10 14:07:29 +030015396static void intel_pps_init(struct drm_i915_private *dev_priv)
15397{
15398 if (HAS_PCH_SPLIT(dev_priv) || IS_BROXTON(dev_priv))
15399 dev_priv->pps_mmio_base = PCH_PPS_BASE;
15400 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15401 dev_priv->pps_mmio_base = VLV_PPS_BASE;
15402 else
15403 dev_priv->pps_mmio_base = PPS_BASE;
Imre Deak8090ba82016-08-10 14:07:33 +030015404
15405 intel_pps_unlock_regs_wa(dev_priv);
Imre Deak44cb7342016-08-10 14:07:29 +030015406}
15407
Jesse Barnes79e53942008-11-07 14:24:08 -080015408static void intel_setup_outputs(struct drm_device *dev)
15409{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015410 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson4ef69c72010-09-09 15:14:28 +010015411 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040015412 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080015413
Imre Deak44cb7342016-08-10 14:07:29 +030015414 intel_pps_init(dev_priv);
15415
Imre Deak97a824e12016-06-21 11:51:47 +030015416 /*
15417 * intel_edp_init_connector() depends on this completing first, to
15418 * prevent the registeration of both eDP and LVDS and the incorrect
15419 * sharing of the PPS.
15420 */
Daniel Vetterc9093352013-06-06 22:22:47 +020015421 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015422
Jesse Barnes84b4e042014-06-25 08:24:29 -070015423 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020015424 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040015425
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +010015426 if (IS_BROXTON(dev_priv)) {
Vandana Kannanc776eb22014-08-19 12:05:01 +053015427 /*
15428 * FIXME: Broxton doesn't support port detection via the
15429 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
15430 * detect the ports.
15431 */
15432 intel_ddi_init(dev, PORT_A);
15433 intel_ddi_init(dev, PORT_B);
15434 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020015435
15436 intel_dsi_init(dev);
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +010015437 } else if (HAS_DDI(dev_priv)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030015438 int found;
15439
Jesse Barnesde31fac2015-03-06 15:53:32 -080015440 /*
15441 * Haswell uses DDI functions to detect digital outputs.
15442 * On SKL pre-D0 the strap isn't connected, so we assume
15443 * it's there.
15444 */
Ville Syrjälä77179402015-09-18 20:03:35 +030015445 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080015446 /* WaIgnoreDDIAStrap: skl */
Tvrtko Ursulin08537232016-10-13 11:03:02 +010015447 if (found || IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030015448 intel_ddi_init(dev, PORT_A);
15449
15450 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
15451 * register */
15452 found = I915_READ(SFUSE_STRAP);
15453
15454 if (found & SFUSE_STRAP_DDIB_DETECTED)
15455 intel_ddi_init(dev, PORT_B);
15456 if (found & SFUSE_STRAP_DDIC_DETECTED)
15457 intel_ddi_init(dev, PORT_C);
15458 if (found & SFUSE_STRAP_DDID_DETECTED)
15459 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070015460 /*
15461 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
15462 */
Tvrtko Ursulin08537232016-10-13 11:03:02 +010015463 if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070015464 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
15465 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
15466 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
15467 intel_ddi_init(dev, PORT_E);
15468
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010015469 } else if (HAS_PCH_SPLIT(dev_priv)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040015470 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020015471 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020015472
15473 if (has_edp_a(dev))
15474 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040015475
Paulo Zanonidc0fa712013-02-19 16:21:46 -030015476 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080015477 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020015478 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080015479 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030015480 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080015481 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030015482 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080015483 }
15484
Paulo Zanonidc0fa712013-02-19 16:21:46 -030015485 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030015486 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080015487
Paulo Zanonidc0fa712013-02-19 16:21:46 -030015488 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030015489 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080015490
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080015491 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030015492 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080015493
Daniel Vetter270b3042012-10-27 15:52:05 +020015494 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030015495 intel_dp_init(dev, PCH_DP_D, PORT_D);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015496 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030015497 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010015498
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030015499 /*
15500 * The DP_DETECTED bit is the latched state of the DDC
15501 * SDA pin at boot. However since eDP doesn't require DDC
15502 * (no way to plug in a DP->HDMI dongle) the DDC pins for
15503 * eDP ports may have been muxed to an alternate function.
15504 * Thus we can't rely on the DP_DETECTED bit alone to detect
15505 * eDP ports. Consult the VBT as well as DP_DETECTED to
15506 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030015507 *
15508 * Sadly the straps seem to be missing sometimes even for HDMI
15509 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
15510 * and VBT for the presence of the port. Additionally we can't
15511 * trust the port type the VBT declares as we've seen at least
15512 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030015513 */
Chris Wilson457c52d2016-06-01 08:27:50 +010015514 has_edp = intel_dp_is_edp(dev, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015515 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
15516 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010015517 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015518 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030015519 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030015520
Chris Wilson457c52d2016-06-01 08:27:50 +010015521 has_edp = intel_dp_is_edp(dev, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015522 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
15523 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010015524 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015525 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030015526 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053015527
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015528 if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030015529 /*
15530 * eDP not supported on port D,
15531 * so no need to worry about it
15532 */
15533 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
15534 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030015535 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030015536 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
15537 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030015538 }
15539
Jani Nikula3cfca972013-08-27 15:12:26 +030015540 intel_dsi_init(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015541 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
Ma Ling27185ae2009-08-24 13:50:23 +080015542 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080015543
Paulo Zanonie2debe92013-02-18 19:00:27 -030015544 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015545 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020015546 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015547 if (!found && IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015548 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030015549 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015550 }
Ma Ling27185ae2009-08-24 13:50:23 +080015551
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015552 if (!found && IS_G4X(dev_priv))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030015553 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080015554 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040015555
15556 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040015557
Paulo Zanonie2debe92013-02-18 19:00:27 -030015558 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015559 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020015560 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015561 }
Ma Ling27185ae2009-08-24 13:50:23 +080015562
Paulo Zanonie2debe92013-02-18 19:00:27 -030015563 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080015564
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015565 if (IS_G4X(dev_priv)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015566 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030015567 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080015568 }
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015569 if (IS_G4X(dev_priv))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030015570 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080015571 }
Ma Ling27185ae2009-08-24 13:50:23 +080015572
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +010015573 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030015574 intel_dp_init(dev, DP_D, PORT_D);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010015575 } else if (IS_GEN2(dev_priv))
Jesse Barnes79e53942008-11-07 14:24:08 -080015576 intel_dvo_init(dev);
15577
Zhenyu Wang103a1962009-11-27 11:44:36 +080015578 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080015579 intel_tv_init(dev);
15580
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080015581 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070015582
Damien Lespiaub2784e12014-08-05 11:29:37 +010015583 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010015584 encoder->base.possible_crtcs = encoder->crtc_mask;
15585 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020015586 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080015587 }
Chris Wilson47356eb2011-01-11 17:06:04 +000015588
Paulo Zanonidde86e22012-12-01 12:04:25 -020015589 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020015590
15591 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015592}
15593
15594static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
15595{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030015596 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080015597 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080015598
Daniel Vetteref2d6332014-02-10 18:00:38 +010015599 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030015600 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010015601 WARN_ON(!intel_fb->obj->framebuffer_references--);
Chris Wilsonf8c417c2016-07-20 13:31:53 +010015602 i915_gem_object_put(intel_fb->obj);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030015603 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015604 kfree(intel_fb);
15605}
15606
15607static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000015608 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080015609 unsigned int *handle)
15610{
15611 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000015612 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080015613
Chris Wilsoncc917ab2015-10-13 14:22:26 +010015614 if (obj->userptr.mm) {
15615 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
15616 return -EINVAL;
15617 }
15618
Chris Wilson05394f32010-11-08 19:18:58 +000015619 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080015620}
15621
Rodrigo Vivi86c98582015-07-08 16:22:45 -070015622static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
15623 struct drm_file *file,
15624 unsigned flags, unsigned color,
15625 struct drm_clip_rect *clips,
15626 unsigned num_clips)
15627{
15628 struct drm_device *dev = fb->dev;
15629 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
15630 struct drm_i915_gem_object *obj = intel_fb->obj;
15631
15632 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030015633 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070015634 mutex_unlock(&dev->struct_mutex);
15635
15636 return 0;
15637}
15638
Jesse Barnes79e53942008-11-07 14:24:08 -080015639static const struct drm_framebuffer_funcs intel_fb_funcs = {
15640 .destroy = intel_user_framebuffer_destroy,
15641 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070015642 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080015643};
15644
Damien Lespiaub3218032015-02-27 11:15:18 +000015645static
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015646u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
15647 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +000015648{
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015649 u32 gen = INTEL_INFO(dev_priv)->gen;
Damien Lespiaub3218032015-02-27 11:15:18 +000015650
15651 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020015652 int cpp = drm_format_plane_cpp(pixel_format, 0);
15653
Damien Lespiaub3218032015-02-27 11:15:18 +000015654 /* "The stride in bytes must not exceed the of the size of 8K
15655 * pixels and 32K bytes."
15656 */
Ville Syrjäläac484962016-01-20 21:05:26 +020015657 return min(8192 * cpp, 32768);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015658 } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
15659 !IS_CHERRYVIEW(dev_priv)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000015660 return 32*1024;
15661 } else if (gen >= 4) {
15662 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15663 return 16*1024;
15664 else
15665 return 32*1024;
15666 } else if (gen >= 3) {
15667 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15668 return 8*1024;
15669 else
15670 return 16*1024;
15671 } else {
15672 /* XXX DSPC is limited to 4k tiled */
15673 return 8*1024;
15674 }
15675}
15676
Daniel Vetterb5ea6422014-03-02 21:18:00 +010015677static int intel_framebuffer_init(struct drm_device *dev,
15678 struct intel_framebuffer *intel_fb,
15679 struct drm_mode_fb_cmd2 *mode_cmd,
15680 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080015681{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015682 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015683 unsigned int tiling = i915_gem_object_get_tiling(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080015684 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000015685 u32 pitch_limit, stride_alignment;
Eric Engestromd3828142016-08-15 16:29:55 +010015686 char *format_name;
Jesse Barnes79e53942008-11-07 14:24:08 -080015687
Daniel Vetterdd4916c2013-10-09 21:23:51 +020015688 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
15689
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015690 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015691 /*
15692 * If there's a fence, enforce that
15693 * the fb modifier and tiling mode match.
15694 */
15695 if (tiling != I915_TILING_NONE &&
15696 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015697 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
15698 return -EINVAL;
15699 }
15700 } else {
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015701 if (tiling == I915_TILING_X) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015702 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015703 } else if (tiling == I915_TILING_Y) {
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015704 DRM_DEBUG("No Y tiling for legacy addfb\n");
15705 return -EINVAL;
15706 }
15707 }
15708
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000015709 /* Passed in modifier sanity checking. */
15710 switch (mode_cmd->modifier[0]) {
15711 case I915_FORMAT_MOD_Y_TILED:
15712 case I915_FORMAT_MOD_Yf_TILED:
15713 if (INTEL_INFO(dev)->gen < 9) {
15714 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15715 mode_cmd->modifier[0]);
15716 return -EINVAL;
15717 }
15718 case DRM_FORMAT_MOD_NONE:
15719 case I915_FORMAT_MOD_X_TILED:
15720 break;
15721 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070015722 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15723 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010015724 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015725 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015726
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015727 /*
15728 * gen2/3 display engine uses the fence if present,
15729 * so the tiling mode must match the fb modifier exactly.
15730 */
15731 if (INTEL_INFO(dev_priv)->gen < 4 &&
15732 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
15733 DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
15734 return -EINVAL;
15735 }
15736
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015737 stride_alignment = intel_fb_stride_alignment(dev_priv,
15738 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000015739 mode_cmd->pixel_format);
15740 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15741 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15742 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010015743 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015744 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015745
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015746 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000015747 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015748 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000015749 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15750 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015751 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015752 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015753 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015754 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015755
Ville Syrjäläc2ff7372016-02-11 19:16:37 +020015756 /*
15757 * If there's a fence, enforce that
15758 * the fb pitch and fence stride match.
15759 */
15760 if (tiling != I915_TILING_NONE &&
Chris Wilson3e510a82016-08-05 10:14:23 +010015761 mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015762 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
Chris Wilson3e510a82016-08-05 10:14:23 +010015763 mode_cmd->pitches[0],
15764 i915_gem_object_get_stride(obj));
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015765 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015766 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015767
Ville Syrjälä57779d02012-10-31 17:50:14 +020015768 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080015769 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020015770 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015771 case DRM_FORMAT_RGB565:
15772 case DRM_FORMAT_XRGB8888:
15773 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015774 break;
15775 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015776 if (INTEL_INFO(dev)->gen > 3) {
Eric Engestrom90844f02016-08-15 01:02:38 +010015777 format_name = drm_get_format_name(mode_cmd->pixel_format);
15778 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15779 kfree(format_name);
Ville Syrjälä57779d02012-10-31 17:50:14 +020015780 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015781 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020015782 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020015783 case DRM_FORMAT_ABGR8888:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015784 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
Wayne Boyer666a4532015-12-09 12:29:35 -080015785 INTEL_INFO(dev)->gen < 9) {
Eric Engestrom90844f02016-08-15 01:02:38 +010015786 format_name = drm_get_format_name(mode_cmd->pixel_format);
15787 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15788 kfree(format_name);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015789 return -EINVAL;
15790 }
15791 break;
15792 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015793 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015794 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015795 if (INTEL_INFO(dev)->gen < 4) {
Eric Engestrom90844f02016-08-15 01:02:38 +010015796 format_name = drm_get_format_name(mode_cmd->pixel_format);
15797 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15798 kfree(format_name);
Ville Syrjälä57779d02012-10-31 17:50:14 +020015799 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015800 }
Jesse Barnesb5626742011-06-24 12:19:27 -070015801 break;
Damien Lespiau75312082015-05-15 19:06:01 +010015802 case DRM_FORMAT_ABGR2101010:
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010015803 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
Eric Engestrom90844f02016-08-15 01:02:38 +010015804 format_name = drm_get_format_name(mode_cmd->pixel_format);
15805 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15806 kfree(format_name);
Damien Lespiau75312082015-05-15 19:06:01 +010015807 return -EINVAL;
15808 }
15809 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020015810 case DRM_FORMAT_YUYV:
15811 case DRM_FORMAT_UYVY:
15812 case DRM_FORMAT_YVYU:
15813 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015814 if (INTEL_INFO(dev)->gen < 5) {
Eric Engestrom90844f02016-08-15 01:02:38 +010015815 format_name = drm_get_format_name(mode_cmd->pixel_format);
15816 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15817 kfree(format_name);
Ville Syrjälä57779d02012-10-31 17:50:14 +020015818 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015819 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015820 break;
15821 default:
Eric Engestrom90844f02016-08-15 01:02:38 +010015822 format_name = drm_get_format_name(mode_cmd->pixel_format);
15823 DRM_DEBUG("unsupported pixel format: %s\n", format_name);
15824 kfree(format_name);
Chris Wilson57cd6502010-08-08 12:34:44 +010015825 return -EINVAL;
15826 }
15827
Ville Syrjälä90f9a332012-10-31 17:50:19 +020015828 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15829 if (mode_cmd->offsets[0] != 0)
15830 return -EINVAL;
15831
Daniel Vetterc7d73f62012-12-13 23:38:38 +010015832 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15833 intel_fb->obj = obj;
15834
Ville Syrjälä6687c902015-09-15 13:16:41 +030015835 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
15836 if (ret)
15837 return ret;
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020015838
Jesse Barnes79e53942008-11-07 14:24:08 -080015839 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15840 if (ret) {
15841 DRM_ERROR("framebuffer init failed %d\n", ret);
15842 return ret;
15843 }
15844
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020015845 intel_fb->obj->framebuffer_references++;
15846
Jesse Barnes79e53942008-11-07 14:24:08 -080015847 return 0;
15848}
15849
Jesse Barnes79e53942008-11-07 14:24:08 -080015850static struct drm_framebuffer *
15851intel_user_framebuffer_create(struct drm_device *dev,
15852 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020015853 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080015854{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015855 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000015856 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020015857 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080015858
Chris Wilson03ac0642016-07-20 13:31:51 +010015859 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15860 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010015861 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080015862
Daniel Vetter92907cb2015-11-23 09:04:05 +010015863 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015864 if (IS_ERR(fb))
Chris Wilsonf0cd5182016-10-28 13:58:43 +010015865 i915_gem_object_put(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015866
15867 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080015868}
15869
Jesse Barnes79e53942008-11-07 14:24:08 -080015870static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080015871 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020015872 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080015873 .atomic_check = intel_atomic_check,
15874 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020015875 .atomic_state_alloc = intel_atomic_state_alloc,
15876 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080015877};
15878
Imre Deak88212942016-03-16 13:38:53 +020015879/**
15880 * intel_init_display_hooks - initialize the display modesetting hooks
15881 * @dev_priv: device private
15882 */
15883void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070015884{
Imre Deak88212942016-03-16 13:38:53 +020015885 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015886 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015887 dev_priv->display.get_initial_plane_config =
15888 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015889 dev_priv->display.crtc_compute_clock =
15890 haswell_crtc_compute_clock;
15891 dev_priv->display.crtc_enable = haswell_crtc_enable;
15892 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015893 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015894 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015895 dev_priv->display.get_initial_plane_config =
15896 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020015897 dev_priv->display.crtc_compute_clock =
15898 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020015899 dev_priv->display.crtc_enable = haswell_crtc_enable;
15900 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015901 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015902 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015903 dev_priv->display.get_initial_plane_config =
15904 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020015905 dev_priv->display.crtc_compute_clock =
15906 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015907 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15908 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015909 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070015910 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015911 dev_priv->display.get_initial_plane_config =
15912 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015913 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15914 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15915 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15916 } else if (IS_VALLEYVIEW(dev_priv)) {
15917 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15918 dev_priv->display.get_initial_plane_config =
15919 i9xx_get_initial_plane_config;
15920 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070015921 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15922 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020015923 } else if (IS_G4X(dev_priv)) {
15924 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15925 dev_priv->display.get_initial_plane_config =
15926 i9xx_get_initial_plane_config;
15927 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15928 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15929 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020015930 } else if (IS_PINEVIEW(dev_priv)) {
15931 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15932 dev_priv->display.get_initial_plane_config =
15933 i9xx_get_initial_plane_config;
15934 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15935 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15936 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015937 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015938 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015939 dev_priv->display.get_initial_plane_config =
15940 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020015941 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015942 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15943 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015944 } else {
15945 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15946 dev_priv->display.get_initial_plane_config =
15947 i9xx_get_initial_plane_config;
15948 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15949 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15950 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070015951 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015952
Jesse Barnese70236a2009-09-21 10:42:27 -070015953 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020015954 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015955 dev_priv->display.get_display_clock_speed =
15956 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015957 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070015958 dev_priv->display.get_display_clock_speed =
15959 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015960 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015961 dev_priv->display.get_display_clock_speed =
15962 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015963 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015964 dev_priv->display.get_display_clock_speed =
15965 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015966 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070015967 dev_priv->display.get_display_clock_speed =
15968 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015969 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030015970 dev_priv->display.get_display_clock_speed =
15971 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015972 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15973 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015974 dev_priv->display.get_display_clock_speed =
15975 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015976 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015977 dev_priv->display.get_display_clock_speed =
15978 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015979 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015980 dev_priv->display.get_display_clock_speed =
15981 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015982 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015983 dev_priv->display.get_display_clock_speed =
15984 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015985 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015986 dev_priv->display.get_display_clock_speed =
15987 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015988 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015989 dev_priv->display.get_display_clock_speed =
15990 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015991 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015992 dev_priv->display.get_display_clock_speed =
15993 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015994 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015995 dev_priv->display.get_display_clock_speed =
15996 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015997 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015998 dev_priv->display.get_display_clock_speed =
15999 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020016000 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070016001 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030016002 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030016003 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020016004 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070016005 dev_priv->display.get_display_clock_speed =
16006 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030016007 }
Jesse Barnese70236a2009-09-21 10:42:27 -070016008
Imre Deak88212942016-03-16 13:38:53 +020016009 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053016010 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020016011 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053016012 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020016013 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053016014 /* FIXME: detect B0+ stepping and use auto training */
16015 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020016016 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053016017 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030016018 }
16019
16020 if (IS_BROADWELL(dev_priv)) {
16021 dev_priv->display.modeset_commit_cdclk =
16022 broadwell_modeset_commit_cdclk;
16023 dev_priv->display.modeset_calc_cdclk =
16024 broadwell_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020016025 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020016026 dev_priv->display.modeset_commit_cdclk =
16027 valleyview_modeset_commit_cdclk;
16028 dev_priv->display.modeset_calc_cdclk =
16029 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020016030 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020016031 dev_priv->display.modeset_commit_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030016032 bxt_modeset_commit_cdclk;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020016033 dev_priv->display.modeset_calc_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030016034 bxt_modeset_calc_cdclk;
Clint Taylorc89e39f2016-05-13 23:41:21 +030016035 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
16036 dev_priv->display.modeset_commit_cdclk =
16037 skl_modeset_commit_cdclk;
16038 dev_priv->display.modeset_calc_cdclk =
16039 skl_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070016040 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020016041
Lyude27082492016-08-24 07:48:10 +020016042 if (dev_priv->info.gen >= 9)
16043 dev_priv->display.update_crtcs = skl_update_crtcs;
16044 else
16045 dev_priv->display.update_crtcs = intel_update_crtcs;
16046
Daniel Vetter5a21b662016-05-24 17:13:53 +020016047 switch (INTEL_INFO(dev_priv)->gen) {
16048 case 2:
16049 dev_priv->display.queue_flip = intel_gen2_queue_flip;
16050 break;
16051
16052 case 3:
16053 dev_priv->display.queue_flip = intel_gen3_queue_flip;
16054 break;
16055
16056 case 4:
16057 case 5:
16058 dev_priv->display.queue_flip = intel_gen4_queue_flip;
16059 break;
16060
16061 case 6:
16062 dev_priv->display.queue_flip = intel_gen6_queue_flip;
16063 break;
16064 case 7:
16065 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
16066 dev_priv->display.queue_flip = intel_gen7_queue_flip;
16067 break;
16068 case 9:
16069 /* Drop through - unsupported since execlist only. */
16070 default:
16071 /* Default just returns -ENODEV to indicate unsupported */
16072 dev_priv->display.queue_flip = intel_default_queue_flip;
16073 }
Jesse Barnese70236a2009-09-21 10:42:27 -070016074}
16075
Jesse Barnesb690e962010-07-19 13:53:12 -070016076/*
16077 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
16078 * resume, or other times. This quirk makes sure that's the case for
16079 * affected systems.
16080 */
Akshay Joshi0206e352011-08-16 15:34:10 -040016081static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070016082{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016083 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb690e962010-07-19 13:53:12 -070016084
16085 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020016086 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070016087}
16088
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030016089static void quirk_pipeb_force(struct drm_device *dev)
16090{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016091 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030016092
16093 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
16094 DRM_INFO("applying pipe b force quirk\n");
16095}
16096
Keith Packard435793d2011-07-12 14:56:22 -070016097/*
16098 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
16099 */
16100static void quirk_ssc_force_disable(struct drm_device *dev)
16101{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016102 struct drm_i915_private *dev_priv = to_i915(dev);
Keith Packard435793d2011-07-12 14:56:22 -070016103 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020016104 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070016105}
16106
Carsten Emde4dca20e2012-03-15 15:56:26 +010016107/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010016108 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
16109 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010016110 */
16111static void quirk_invert_brightness(struct drm_device *dev)
16112{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016113 struct drm_i915_private *dev_priv = to_i915(dev);
Carsten Emde4dca20e2012-03-15 15:56:26 +010016114 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020016115 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070016116}
16117
Scot Doyle9c72cc62014-07-03 23:27:50 +000016118/* Some VBT's incorrectly indicate no backlight is present */
16119static void quirk_backlight_present(struct drm_device *dev)
16120{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016121 struct drm_i915_private *dev_priv = to_i915(dev);
Scot Doyle9c72cc62014-07-03 23:27:50 +000016122 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
16123 DRM_INFO("applying backlight present quirk\n");
16124}
16125
Jesse Barnesb690e962010-07-19 13:53:12 -070016126struct intel_quirk {
16127 int device;
16128 int subsystem_vendor;
16129 int subsystem_device;
16130 void (*hook)(struct drm_device *dev);
16131};
16132
Egbert Eich5f85f172012-10-14 15:46:38 +020016133/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
16134struct intel_dmi_quirk {
16135 void (*hook)(struct drm_device *dev);
16136 const struct dmi_system_id (*dmi_id_list)[];
16137};
16138
16139static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
16140{
16141 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
16142 return 1;
16143}
16144
16145static const struct intel_dmi_quirk intel_dmi_quirks[] = {
16146 {
16147 .dmi_id_list = &(const struct dmi_system_id[]) {
16148 {
16149 .callback = intel_dmi_reverse_brightness,
16150 .ident = "NCR Corporation",
16151 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
16152 DMI_MATCH(DMI_PRODUCT_NAME, ""),
16153 },
16154 },
16155 { } /* terminating entry */
16156 },
16157 .hook = quirk_invert_brightness,
16158 },
16159};
16160
Ben Widawskyc43b5632012-04-16 14:07:40 -070016161static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070016162 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
16163 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
16164
Jesse Barnesb690e962010-07-19 13:53:12 -070016165 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
16166 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
16167
Ville Syrjälä5f080c02014-08-15 01:22:06 +030016168 /* 830 needs to leave pipe A & dpll A up */
16169 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
16170
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030016171 /* 830 needs to leave pipe B & dpll B up */
16172 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
16173
Keith Packard435793d2011-07-12 14:56:22 -070016174 /* Lenovo U160 cannot use SSC on LVDS */
16175 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020016176
16177 /* Sony Vaio Y cannot use SSC on LVDS */
16178 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010016179
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010016180 /* Acer Aspire 5734Z must invert backlight brightness */
16181 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
16182
16183 /* Acer/eMachines G725 */
16184 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
16185
16186 /* Acer/eMachines e725 */
16187 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
16188
16189 /* Acer/Packard Bell NCL20 */
16190 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
16191
16192 /* Acer Aspire 4736Z */
16193 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020016194
16195 /* Acer Aspire 5336 */
16196 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000016197
16198 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
16199 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000016200
Scot Doyledfb3d47b2014-08-21 16:08:02 +000016201 /* Acer C720 Chromebook (Core i3 4005U) */
16202 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
16203
jens steinb2a96012014-10-28 20:25:53 +010016204 /* Apple Macbook 2,1 (Core 2 T7400) */
16205 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
16206
Jani Nikula1b9448b2015-11-05 11:49:59 +020016207 /* Apple Macbook 4,1 */
16208 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
16209
Scot Doyled4967d82014-07-03 23:27:52 +000016210 /* Toshiba CB35 Chromebook (Celeron 2955U) */
16211 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000016212
16213 /* HP Chromebook 14 (Celeron 2955U) */
16214 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020016215
16216 /* Dell Chromebook 11 */
16217 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020016218
16219 /* Dell Chromebook 11 (2015 version) */
16220 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070016221};
16222
16223static void intel_init_quirks(struct drm_device *dev)
16224{
16225 struct pci_dev *d = dev->pdev;
16226 int i;
16227
16228 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
16229 struct intel_quirk *q = &intel_quirks[i];
16230
16231 if (d->device == q->device &&
16232 (d->subsystem_vendor == q->subsystem_vendor ||
16233 q->subsystem_vendor == PCI_ANY_ID) &&
16234 (d->subsystem_device == q->subsystem_device ||
16235 q->subsystem_device == PCI_ANY_ID))
16236 q->hook(dev);
16237 }
Egbert Eich5f85f172012-10-14 15:46:38 +020016238 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
16239 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
16240 intel_dmi_quirks[i].hook(dev);
16241 }
Jesse Barnesb690e962010-07-19 13:53:12 -070016242}
16243
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016244/* Disable the VGA plane that we never use */
16245static void i915_disable_vga(struct drm_device *dev)
16246{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016247 struct drm_i915_private *dev_priv = to_i915(dev);
David Weinehall52a05c32016-08-22 13:32:44 +030016248 struct pci_dev *pdev = dev_priv->drm.pdev;
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016249 u8 sr1;
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010016250 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016251
Ville Syrjälä2b37c612014-01-22 21:32:38 +020016252 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
David Weinehall52a05c32016-08-22 13:32:44 +030016253 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070016254 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016255 sr1 = inb(VGA_SR_DATA);
16256 outb(sr1 | 1<<5, VGA_SR_DATA);
David Weinehall52a05c32016-08-22 13:32:44 +030016257 vga_put(pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016258 udelay(300);
16259
Ville Syrjälä01f5a622014-12-16 18:38:37 +020016260 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016261 POSTING_READ(vga_reg);
16262}
16263
Daniel Vetterf8175862012-04-10 15:50:11 +020016264void intel_modeset_init_hw(struct drm_device *dev)
16265{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016266 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010016267
Ville Syrjäläb6283052015-06-03 15:45:07 +030016268 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010016269
16270 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
16271
Daniel Vetterf8175862012-04-10 15:50:11 +020016272 intel_init_clock_gating(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020016273}
16274
Matt Roperd93c0372015-12-03 11:37:41 -080016275/*
16276 * Calculate what we think the watermarks should be for the state we've read
16277 * out of the hardware and then immediately program those watermarks so that
16278 * we ensure the hardware settings match our internal state.
16279 *
16280 * We can calculate what we think WM's should be by creating a duplicate of the
16281 * current state (which was constructed during hardware readout) and running it
16282 * through the atomic check code to calculate new watermark values in the
16283 * state object.
16284 */
16285static void sanitize_watermarks(struct drm_device *dev)
16286{
16287 struct drm_i915_private *dev_priv = to_i915(dev);
16288 struct drm_atomic_state *state;
16289 struct drm_crtc *crtc;
16290 struct drm_crtc_state *cstate;
16291 struct drm_modeset_acquire_ctx ctx;
16292 int ret;
16293 int i;
16294
16295 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080016296 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080016297 return;
16298
16299 /*
16300 * We need to hold connection_mutex before calling duplicate_state so
16301 * that the connector loop is protected.
16302 */
16303 drm_modeset_acquire_init(&ctx, 0);
16304retry:
Matt Roper0cd12622016-01-12 07:13:37 -080016305 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080016306 if (ret == -EDEADLK) {
16307 drm_modeset_backoff(&ctx);
16308 goto retry;
16309 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080016310 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080016311 }
16312
16313 state = drm_atomic_helper_duplicate_state(dev, &ctx);
16314 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080016315 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080016316
Matt Ropered4a6a72016-02-23 17:20:13 -080016317 /*
16318 * Hardware readout is the only time we don't want to calculate
16319 * intermediate watermarks (since we don't trust the current
16320 * watermarks).
16321 */
16322 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16323
Matt Roperd93c0372015-12-03 11:37:41 -080016324 ret = intel_atomic_check(dev, state);
16325 if (ret) {
16326 /*
16327 * If we fail here, it means that the hardware appears to be
16328 * programmed in a way that shouldn't be possible, given our
16329 * understanding of watermark requirements. This might mean a
16330 * mistake in the hardware readout code or a mistake in the
16331 * watermark calculations for a given platform. Raise a WARN
16332 * so that this is noticeable.
16333 *
16334 * If this actually happens, we'll have to just leave the
16335 * BIOS-programmed watermarks untouched and hope for the best.
16336 */
16337 WARN(true, "Could not determine valid watermarks for inherited state\n");
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020016338 goto put_state;
Matt Roperd93c0372015-12-03 11:37:41 -080016339 }
16340
16341 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080016342 for_each_crtc_in_state(state, crtc, cstate, i) {
16343 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
16344
Matt Ropered4a6a72016-02-23 17:20:13 -080016345 cs->wm.need_postvbl_update = true;
16346 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080016347 }
16348
Arnd Bergmannb9a1b712016-10-18 17:16:23 +020016349put_state:
Chris Wilson08536952016-10-14 13:18:18 +010016350 drm_atomic_state_put(state);
Matt Roper0cd12622016-01-12 07:13:37 -080016351fail:
Matt Roperd93c0372015-12-03 11:37:41 -080016352 drm_modeset_drop_locks(&ctx);
16353 drm_modeset_acquire_fini(&ctx);
16354}
16355
Jesse Barnes79e53942008-11-07 14:24:08 -080016356void intel_modeset_init(struct drm_device *dev)
16357{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030016358 struct drm_i915_private *dev_priv = to_i915(dev);
16359 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000016360 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000016361 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080016362 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080016363
16364 drm_mode_config_init(dev);
16365
16366 dev->mode_config.min_width = 0;
16367 dev->mode_config.min_height = 0;
16368
Dave Airlie019d96c2011-09-29 16:20:42 +010016369 dev->mode_config.preferred_depth = 24;
16370 dev->mode_config.prefer_shadow = 1;
16371
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000016372 dev->mode_config.allow_fb_modifiers = true;
16373
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020016374 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080016375
Jesse Barnesb690e962010-07-19 13:53:12 -070016376 intel_init_quirks(dev);
16377
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030016378 intel_init_pm(dev);
16379
Ben Widawskye3c74752013-04-05 13:12:39 -070016380 if (INTEL_INFO(dev)->num_pipes == 0)
16381 return;
16382
Lukas Wunner69f92f62015-07-15 13:57:35 +020016383 /*
16384 * There may be no VBT; and if the BIOS enabled SSC we can
16385 * just keep using it to avoid unnecessary flicker. Whereas if the
16386 * BIOS isn't using it, don't assume it will work even if the VBT
16387 * indicates as much.
16388 */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010016389 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
Lukas Wunner69f92f62015-07-15 13:57:35 +020016390 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
16391 DREF_SSC1_ENABLE);
16392
16393 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
16394 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
16395 bios_lvds_use_ssc ? "en" : "dis",
16396 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
16397 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
16398 }
16399 }
16400
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010016401 if (IS_GEN2(dev_priv)) {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010016402 dev->mode_config.max_width = 2048;
16403 dev->mode_config.max_height = 2048;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010016404 } else if (IS_GEN3(dev_priv)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070016405 dev->mode_config.max_width = 4096;
16406 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080016407 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010016408 dev->mode_config.max_width = 8192;
16409 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080016410 }
Damien Lespiau068be562014-03-28 14:17:49 +000016411
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +010016412 if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
16413 dev->mode_config.cursor_width = IS_845G(dev_priv) ? 64 : 512;
Ville Syrjälädc41c152014-08-13 11:57:05 +030016414 dev->mode_config.cursor_height = 1023;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010016415 } else if (IS_GEN2(dev_priv)) {
Damien Lespiau068be562014-03-28 14:17:49 +000016416 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
16417 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
16418 } else {
16419 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
16420 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
16421 }
16422
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030016423 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080016424
Zhao Yakui28c97732009-10-09 11:39:41 +080016425 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070016426 INTEL_INFO(dev)->num_pipes,
16427 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080016428
Damien Lespiau055e3932014-08-18 13:49:10 +010016429 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000016430 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000016431 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000016432 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070016433 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030016434 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000016435 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070016436 }
Jesse Barnes79e53942008-11-07 14:24:08 -080016437 }
16438
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030016439 intel_update_czclk(dev_priv);
16440 intel_update_cdclk(dev);
16441
Daniel Vettere72f9fb2013-06-05 13:34:06 +020016442 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010016443
Ville Syrjäläb2045352016-05-13 23:41:27 +030016444 if (dev_priv->max_cdclk_freq == 0)
16445 intel_update_max_cdclk(dev);
16446
Jesse Barnes9cce37f2010-08-13 15:11:26 -070016447 /* Just disable it once at startup */
16448 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016449 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000016450
Daniel Vetter6e9f7982014-05-29 23:54:47 +020016451 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016452 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020016453 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080016454
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016455 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020016456 struct intel_initial_plane_config plane_config = {};
16457
Jesse Barnes46f297f2014-03-07 08:57:48 -080016458 if (!crtc->active)
16459 continue;
16460
Jesse Barnes46f297f2014-03-07 08:57:48 -080016461 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080016462 * Note that reserving the BIOS fb up front prevents us
16463 * from stuffing other stolen allocations like the ring
16464 * on top. This prevents some ugliness at boot time, and
16465 * can even allow for smooth boot transitions if the BIOS
16466 * fb is large enough for the active pipe configuration.
16467 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020016468 dev_priv->display.get_initial_plane_config(crtc,
16469 &plane_config);
16470
16471 /*
16472 * If the fb is shared between multiple heads, we'll
16473 * just get the first one.
16474 */
16475 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080016476 }
Matt Roperd93c0372015-12-03 11:37:41 -080016477
16478 /*
16479 * Make sure hardware watermarks really match the state we read out.
16480 * Note that we need to do this after reconstructing the BIOS fb's
16481 * since the watermark calculation done here will use pstate->fb.
16482 */
16483 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010016484}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080016485
Daniel Vetter7fad7982012-07-04 17:51:47 +020016486static void intel_enable_pipe_a(struct drm_device *dev)
16487{
16488 struct intel_connector *connector;
16489 struct drm_connector *crt = NULL;
16490 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030016491 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020016492
16493 /* We can't just switch on the pipe A, we need to set things up with a
16494 * proper mode and output configuration. As a gross hack, enable pipe A
16495 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016496 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020016497 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
16498 crt = &connector->base;
16499 break;
16500 }
16501 }
16502
16503 if (!crt)
16504 return;
16505
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030016506 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020016507 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020016508}
16509
Daniel Vetterfa555832012-10-10 23:14:00 +020016510static bool
16511intel_check_plane_mapping(struct intel_crtc *crtc)
16512{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070016513 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010016514 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä649636e2015-09-22 19:50:01 +030016515 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020016516
Ben Widawsky7eb552a2013-03-13 14:05:41 -070016517 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020016518 return true;
16519
Ville Syrjälä649636e2015-09-22 19:50:01 +030016520 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020016521
16522 if ((val & DISPLAY_PLANE_ENABLE) &&
16523 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
16524 return false;
16525
16526 return true;
16527}
16528
Ville Syrjälä02e93c32015-08-26 19:39:19 +030016529static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
16530{
16531 struct drm_device *dev = crtc->base.dev;
16532 struct intel_encoder *encoder;
16533
16534 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
16535 return true;
16536
16537 return false;
16538}
16539
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020016540static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
16541{
16542 struct drm_device *dev = encoder->base.dev;
16543 struct intel_connector *connector;
16544
16545 for_each_connector_on_encoder(dev, &encoder->base, connector)
16546 return connector;
16547
16548 return NULL;
16549}
16550
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030016551static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
16552 enum transcoder pch_transcoder)
16553{
16554 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
16555 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
16556}
16557
Daniel Vetter24929352012-07-02 20:28:59 +020016558static void intel_sanitize_crtc(struct intel_crtc *crtc)
16559{
16560 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010016561 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020016562 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020016563
Daniel Vetter24929352012-07-02 20:28:59 +020016564 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020016565 if (!transcoder_is_dsi(cpu_transcoder)) {
16566 i915_reg_t reg = PIPECONF(cpu_transcoder);
16567
16568 I915_WRITE(reg,
16569 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
16570 }
Daniel Vetter24929352012-07-02 20:28:59 +020016571
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030016572 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010016573 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030016574 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016575 struct intel_plane *plane;
16576
Daniel Vetter96256042015-02-13 21:03:42 +010016577 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016578
16579 /* Disable everything but the primary plane */
16580 for_each_intel_plane_on_crtc(dev, crtc, plane) {
16581 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
16582 continue;
16583
16584 plane->disable_plane(&plane->base, &crtc->base);
16585 }
Daniel Vetter96256042015-02-13 21:03:42 +010016586 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030016587
Daniel Vetter24929352012-07-02 20:28:59 +020016588 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020016589 * disable the crtc (and hence change the state) if it is wrong. Note
16590 * that gen4+ has a fixed plane -> pipe mapping. */
16591 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020016592 bool plane;
16593
Ville Syrjälä78108b72016-05-27 20:59:19 +030016594 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
16595 crtc->base.base.id, crtc->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020016596
16597 /* Pipe has the wrong plane attached and the plane is active.
16598 * Temporarily change the plane mapping and disable everything
16599 * ... */
16600 plane = crtc->plane;
Ville Syrjälä936e71e2016-07-26 19:06:59 +030016601 to_intel_plane_state(crtc->base.primary->state)->base.visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020016602 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020016603 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020016604 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020016605 }
Daniel Vetter24929352012-07-02 20:28:59 +020016606
Daniel Vetter7fad7982012-07-04 17:51:47 +020016607 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
16608 crtc->pipe == PIPE_A && !crtc->active) {
16609 /* BIOS forgot to enable pipe A, this mostly happens after
16610 * resume. Force-enable the pipe to fix this, the update_dpms
16611 * call below we restore the pipe to the right state, but leave
16612 * the required bits on. */
16613 intel_enable_pipe_a(dev);
16614 }
16615
Daniel Vetter24929352012-07-02 20:28:59 +020016616 /* Adjust the state of the output pipe according to whether we
16617 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010016618 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020016619 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020016620
Tvrtko Ursulin49cff962016-10-13 11:02:54 +010016621 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010016622 /*
16623 * We start out with underrun reporting disabled to avoid races.
16624 * For correct bookkeeping mark this on active crtcs.
16625 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020016626 * Also on gmch platforms we dont have any hardware bits to
16627 * disable the underrun reporting. Which means we need to start
16628 * out with underrun reporting disabled also on inactive pipes,
16629 * since otherwise we'll complain about the garbage we read when
16630 * e.g. coming up after runtime pm.
16631 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010016632 * No protection against concurrent access is required - at
16633 * worst a fifo underrun happens which also sets this to false.
16634 */
16635 crtc->cpu_fifo_underrun_disabled = true;
Ville Syrjäläa168f5b2016-08-05 20:00:17 +030016636 /*
16637 * We track the PCH trancoder underrun reporting state
16638 * within the crtc. With crtc for pipe A housing the underrun
16639 * reporting state for PCH transcoder A, crtc for pipe B housing
16640 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
16641 * and marking underrun reporting as disabled for the non-existing
16642 * PCH transcoders B and C would prevent enabling the south
16643 * error interrupt (see cpt_can_enable_serr_int()).
16644 */
16645 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
16646 crtc->pch_fifo_underrun_disabled = true;
Daniel Vetter4cc31482014-03-24 00:01:41 +010016647 }
Daniel Vetter24929352012-07-02 20:28:59 +020016648}
16649
16650static void intel_sanitize_encoder(struct intel_encoder *encoder)
16651{
16652 struct intel_connector *connector;
Daniel Vetter24929352012-07-02 20:28:59 +020016653
16654 /* We need to check both for a crtc link (meaning that the
16655 * encoder is active and trying to read from a pipe) and the
16656 * pipe itself being active. */
16657 bool has_active_crtc = encoder->base.crtc &&
16658 to_intel_crtc(encoder->base.crtc)->active;
16659
Maarten Lankhorst496b0fc2016-08-23 16:18:07 +020016660 connector = intel_encoder_find_connector(encoder);
16661 if (connector && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020016662 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
16663 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016664 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020016665
16666 /* Connector is active, but has no active pipe. This is
16667 * fallout from our resume register restoring. Disable
16668 * the encoder manually again. */
16669 if (encoder->base.crtc) {
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020016670 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
16671
Daniel Vetter24929352012-07-02 20:28:59 +020016672 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
16673 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016674 encoder->base.name);
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020016675 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030016676 if (encoder->post_disable)
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020016677 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
Daniel Vetter24929352012-07-02 20:28:59 +020016678 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020016679 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020016680
16681 /* Inconsistent output/port/pipe state happens presumably due to
16682 * a bug in one of the get_hw_state functions. Or someplace else
16683 * in our code, like the register restore mess on resume. Clamp
16684 * things to off as a safer default. */
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020016685
16686 connector->base.dpms = DRM_MODE_DPMS_OFF;
16687 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020016688 }
16689 /* Enabled encoders without active connectors will be fixed in
16690 * the crtc fixup. */
16691}
16692
Imre Deak04098752014-02-18 00:02:16 +020016693void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016694{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016695 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010016696 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016697
Imre Deak04098752014-02-18 00:02:16 +020016698 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
16699 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
16700 i915_disable_vga(dev);
16701 }
16702}
16703
16704void i915_redisable_vga(struct drm_device *dev)
16705{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016706 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak04098752014-02-18 00:02:16 +020016707
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030016708 /* This function can be called both from intel_modeset_setup_hw_state or
16709 * at a very early point in our resume sequence, where the power well
16710 * structures are not yet restored. Since this function is at a very
16711 * paranoid "someone might have enabled VGA while we were not looking"
16712 * level, just check if the power well is enabled instead of trying to
16713 * follow the "don't touch the power well if we don't need it" policy
16714 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020016715 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030016716 return;
16717
Imre Deak04098752014-02-18 00:02:16 +020016718 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020016719
16720 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010016721}
16722
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016723static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016724{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016725 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016726
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016727 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016728}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016729
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016730/* FIXME read out full plane state for all planes */
16731static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016732{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016733 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016734 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016735 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020016736
Ville Syrjälä936e71e2016-07-26 19:06:59 +030016737 plane_state->base.visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016738 primary_get_hw_state(to_intel_plane(primary));
16739
Ville Syrjälä936e71e2016-07-26 19:06:59 +030016740 if (plane_state->base.visible)
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020016741 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016742}
16743
Daniel Vetter30e984d2013-06-05 13:34:17 +020016744static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020016745{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016746 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016747 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020016748 struct intel_crtc *crtc;
16749 struct intel_encoder *encoder;
16750 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020016751 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020016752
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016753 dev_priv->active_crtcs = 0;
16754
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016755 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016756 struct intel_crtc_state *crtc_state = crtc->config;
16757 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020016758
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020016759 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016760 memset(crtc_state, 0, sizeof(*crtc_state));
16761 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020016762
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016763 crtc_state->base.active = crtc_state->base.enable =
16764 dev_priv->display.get_pipe_config(crtc, crtc_state);
16765
16766 crtc->base.enabled = crtc_state->base.enable;
16767 crtc->active = crtc_state->base.active;
16768
16769 if (crtc_state->base.active) {
16770 dev_priv->active_crtcs |= 1 << crtc->pipe;
16771
Clint Taylorc89e39f2016-05-13 23:41:21 +030016772 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016773 pixclk = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016774 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016775 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16776 else
16777 WARN_ON(dev_priv->display.modeset_calc_cdclk);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016778
16779 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16780 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16781 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016782 }
16783
16784 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030016785
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016786 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020016787
Ville Syrjälä78108b72016-05-27 20:59:19 +030016788 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16789 crtc->base.base.id, crtc->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016790 crtc->active ? "enabled" : "disabled");
16791 }
16792
Daniel Vetter53589012013-06-05 13:34:16 +020016793 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16794 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16795
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016796 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16797 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016798 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016799 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016800 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016801 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020016802 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016803 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020016804
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020016805 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016806 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020016807 }
16808
Damien Lespiaub2784e12014-08-05 11:29:37 +010016809 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016810 pipe = 0;
16811
16812 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070016813 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16814 encoder->base.crtc = &crtc->base;
Ville Syrjälä253c84c2016-06-22 21:57:01 +030016815 crtc->config->output_types |= 1 << encoder->type;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016816 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020016817 } else {
16818 encoder->base.crtc = NULL;
16819 }
16820
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016821 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020016822 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016823 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016824 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016825 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020016826 }
16827
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016828 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020016829 if (connector->get_hw_state(connector)) {
16830 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016831
16832 encoder = connector->encoder;
16833 connector->base.encoder = &encoder->base;
16834
16835 if (encoder->base.crtc &&
16836 encoder->base.crtc->state->active) {
16837 /*
16838 * This has to be done during hardware readout
16839 * because anything calling .crtc_disable may
16840 * rely on the connector_mask being accurate.
16841 */
16842 encoder->base.crtc->state->connector_mask |=
16843 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010016844 encoder->base.crtc->state->encoder_mask |=
16845 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016846 }
16847
Daniel Vetter24929352012-07-02 20:28:59 +020016848 } else {
16849 connector->base.dpms = DRM_MODE_DPMS_OFF;
16850 connector->base.encoder = NULL;
16851 }
16852 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16853 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030016854 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016855 connector->base.encoder ? "enabled" : "disabled");
16856 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016857
16858 for_each_intel_crtc(dev, crtc) {
16859 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16860
16861 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16862 if (crtc->base.state->active) {
16863 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16864 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16865 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16866
16867 /*
16868 * The initial mode needs to be set in order to keep
16869 * the atomic core happy. It wants a valid mode if the
16870 * crtc's enabled, so we do the above call.
16871 *
16872 * At this point some state updated by the connectors
16873 * in their ->detect() callback has not run yet, so
16874 * no recalculation can be done yet.
16875 *
16876 * Even if we could do a recalculation and modeset
16877 * right now it would cause a double modeset if
16878 * fbdev or userspace chooses a different initial mode.
16879 *
16880 * If that happens, someone indicated they wanted a
16881 * mode change, which means it's safe to do a full
16882 * recalculation.
16883 */
16884 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030016885
16886 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16887 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016888 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020016889
16890 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016891 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020016892}
16893
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016894/* Scan out the current hw modeset state,
16895 * and sanitizes it to the current state
16896 */
16897static void
16898intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020016899{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016900 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter30e984d2013-06-05 13:34:17 +020016901 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016902 struct intel_crtc *crtc;
16903 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020016904 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016905
16906 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016907
16908 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010016909 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016910 intel_sanitize_encoder(encoder);
16911 }
16912
Damien Lespiau055e3932014-08-18 13:49:10 +010016913 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020016914 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16915 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016916 intel_dump_pipe_config(crtc, crtc->config,
16917 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020016918 }
Daniel Vetter9a935852012-07-05 22:34:27 +020016919
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020016920 intel_modeset_update_connector_atomic_state(dev);
16921
Daniel Vetter35c95372013-07-17 06:55:04 +020016922 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16923 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16924
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016925 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020016926 continue;
16927
16928 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16929
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016930 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020016931 pll->on = false;
16932 }
16933
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +010016934 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030016935 vlv_wm_get_hw_state(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +010016936 else if (IS_GEN9(dev_priv))
Pradeep Bhat30789992014-11-04 17:06:45 +000016937 skl_wm_get_hw_state(dev);
Tvrtko Ursulin6e266952016-10-13 11:02:53 +010016938 else if (HAS_PCH_SPLIT(dev_priv))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030016939 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016940
16941 for_each_intel_crtc(dev, crtc) {
16942 unsigned long put_domains;
16943
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010016944 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016945 if (WARN_ON(put_domains))
16946 modeset_put_power_domains(dev_priv, put_domains);
16947 }
16948 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020016949
16950 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016951}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030016952
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016953void intel_display_resume(struct drm_device *dev)
16954{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016955 struct drm_i915_private *dev_priv = to_i915(dev);
16956 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16957 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016958 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020016959
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016960 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst73974892016-08-05 23:28:27 +030016961 if (state)
16962 state->acquire_ctx = &ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016963
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016964 /*
16965 * This is a cludge because with real atomic modeset mode_config.mutex
16966 * won't be taken. Unfortunately some probed state like
16967 * audio_codec_enable is still protected by mode_config.mutex, so lock
16968 * it here for now.
16969 */
16970 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016971 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016972
Maarten Lankhorst73974892016-08-05 23:28:27 +030016973 while (1) {
16974 ret = drm_modeset_lock_all_ctx(dev, &ctx);
16975 if (ret != -EDEADLK)
16976 break;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016977
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016978 drm_modeset_backoff(&ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016979 }
16980
Maarten Lankhorst73974892016-08-05 23:28:27 +030016981 if (!ret)
16982 ret = __intel_display_resume(dev, state);
16983
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016984 drm_modeset_drop_locks(&ctx);
16985 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016986 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016987
Chris Wilson08536952016-10-14 13:18:18 +010016988 if (ret)
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016989 DRM_ERROR("Restoring old state failed with %i\n", ret);
Chris Wilson08536952016-10-14 13:18:18 +010016990 drm_atomic_state_put(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010016991}
16992
16993void intel_modeset_gem_init(struct drm_device *dev)
16994{
Chris Wilsondc979972016-05-10 14:10:04 +010016995 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016996 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070016997 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -080016998
Chris Wilsondc979972016-05-10 14:10:04 +010016999 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030017000
Chris Wilson1833b132012-05-09 11:56:28 +010017001 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020017002
Chris Wilson1ee8da62016-05-12 12:43:23 +010017003 intel_setup_overlay(dev_priv);
Jesse Barnes484b41d2014-03-07 08:57:55 -080017004
17005 /*
17006 * Make sure any fbs we allocated at startup are properly
17007 * pinned & fenced. When we do the allocation it's too early
17008 * for this.
17009 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010017010 for_each_crtc(dev, c) {
Chris Wilson058d88c2016-08-15 10:49:06 +010017011 struct i915_vma *vma;
17012
Matt Roper2ff8fde2014-07-08 07:50:07 -070017013 obj = intel_fb_obj(c->primary->fb);
17014 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080017015 continue;
17016
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010017017 mutex_lock(&dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +010017018 vma = intel_pin_and_fence_fb_obj(c->primary->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +020017019 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010017020 mutex_unlock(&dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +010017021 if (IS_ERR(vma)) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080017022 DRM_ERROR("failed to pin boot fb on pipe %d\n",
17023 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100017024 drm_framebuffer_unreference(c->primary->fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020017025 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020017026 c->primary->crtc = c->primary->state->crtc = NULL;
Daniel Vetter5a21b662016-05-24 17:13:53 +020017027 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020017028 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080017029 }
17030 }
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010017031}
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020017032
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010017033int intel_connector_register(struct drm_connector *connector)
17034{
17035 struct intel_connector *intel_connector = to_intel_connector(connector);
17036 int ret;
17037
17038 ret = intel_backlight_device_register(intel_connector);
17039 if (ret)
17040 goto err;
17041
17042 return 0;
17043
17044err:
17045 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080017046}
17047
Chris Wilsonc191eca2016-06-17 11:40:33 +010017048void intel_connector_unregister(struct drm_connector *connector)
Imre Deak4932e2c2014-02-11 17:12:48 +020017049{
Chris Wilsone63d87c2016-06-17 11:40:34 +010017050 struct intel_connector *intel_connector = to_intel_connector(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020017051
Chris Wilsone63d87c2016-06-17 11:40:34 +010017052 intel_backlight_device_unregister(intel_connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020017053 intel_panel_destroy_backlight(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020017054}
17055
Jesse Barnes79e53942008-11-07 14:24:08 -080017056void intel_modeset_cleanup(struct drm_device *dev)
17057{
Chris Wilsonfac5e232016-07-04 11:34:36 +010017058 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -070017059
Chris Wilsondc979972016-05-10 14:10:04 +010017060 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020017061
Daniel Vetterfd0c0642013-04-24 11:13:35 +020017062 /*
17063 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020017064 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020017065 * experience fancy races otherwise.
17066 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020017067 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070017068
Daniel Vetterfd0c0642013-04-24 11:13:35 +020017069 /*
17070 * Due to the hpd irq storm handling the hotplug work can re-arm the
17071 * poll handlers. Hence disable polling after hpd handling is shut down.
17072 */
Keith Packardf87ea762010-10-03 19:36:26 -070017073 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020017074
Jesse Barnes723bfd72010-10-07 16:01:13 -070017075 intel_unregister_dsm_handler();
17076
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020017077 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050017078
Chris Wilson1630fe72011-07-08 12:22:42 +010017079 /* flush any delayed tasks or pending work */
17080 flush_scheduled_work();
17081
Jesse Barnes79e53942008-11-07 14:24:08 -080017082 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010017083
Chris Wilson1ee8da62016-05-12 12:43:23 +010017084 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030017085
Chris Wilsondc979972016-05-10 14:10:04 +010017086 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010017087
17088 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080017089}
17090
Chris Wilsondf0e9242010-09-09 16:20:55 +010017091void intel_connector_attach_encoder(struct intel_connector *connector,
17092 struct intel_encoder *encoder)
17093{
17094 connector->encoder = encoder;
17095 drm_mode_connector_attach_encoder(&connector->base,
17096 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080017097}
Dave Airlie28d52042009-09-21 14:33:58 +100017098
17099/*
17100 * set vga decode state - true == enable VGA decode
17101 */
17102int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
17103{
Chris Wilsonfac5e232016-07-04 11:34:36 +010017104 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsona885b3c2013-12-17 14:34:50 +000017105 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100017106 u16 gmch_ctrl;
17107
Chris Wilson75fa0412014-02-07 18:37:02 -020017108 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
17109 DRM_ERROR("failed to read control word\n");
17110 return -EIO;
17111 }
17112
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020017113 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
17114 return 0;
17115
Dave Airlie28d52042009-09-21 14:33:58 +100017116 if (state)
17117 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
17118 else
17119 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020017120
17121 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
17122 DRM_ERROR("failed to write control word\n");
17123 return -EIO;
17124 }
17125
Dave Airlie28d52042009-09-21 14:33:58 +100017126 return 0;
17127}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017128
Chris Wilson98a2f412016-10-12 10:05:18 +010017129#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
17130
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017131struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030017132
17133 u32 power_well_driver;
17134
Chris Wilson63b66e52013-08-08 15:12:06 +020017135 int num_transcoders;
17136
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017137 struct intel_cursor_error_state {
17138 u32 control;
17139 u32 position;
17140 u32 base;
17141 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010017142 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017143
17144 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020017145 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017146 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030017147 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010017148 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017149
17150 struct intel_plane_error_state {
17151 u32 control;
17152 u32 stride;
17153 u32 size;
17154 u32 pos;
17155 u32 addr;
17156 u32 surface;
17157 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010017158 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020017159
17160 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020017161 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020017162 enum transcoder cpu_transcoder;
17163
17164 u32 conf;
17165
17166 u32 htotal;
17167 u32 hblank;
17168 u32 hsync;
17169 u32 vtotal;
17170 u32 vblank;
17171 u32 vsync;
17172 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017173};
17174
17175struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010017176intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017177{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017178 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020017179 int transcoders[] = {
17180 TRANSCODER_A,
17181 TRANSCODER_B,
17182 TRANSCODER_C,
17183 TRANSCODER_EDP,
17184 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017185 int i;
17186
Chris Wilsonc0336662016-05-06 15:40:21 +010017187 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020017188 return NULL;
17189
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020017190 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017191 if (error == NULL)
17192 return NULL;
17193
Chris Wilsonc0336662016-05-06 15:40:21 +010017194 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030017195 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
17196
Damien Lespiau055e3932014-08-18 13:49:10 +010017197 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020017198 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020017199 __intel_display_power_is_enabled(dev_priv,
17200 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020017201 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020017202 continue;
17203
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030017204 error->cursor[i].control = I915_READ(CURCNTR(i));
17205 error->cursor[i].position = I915_READ(CURPOS(i));
17206 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017207
17208 error->plane[i].control = I915_READ(DSPCNTR(i));
17209 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010017210 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030017211 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030017212 error->plane[i].pos = I915_READ(DSPPOS(i));
17213 }
Chris Wilsonc0336662016-05-06 15:40:21 +010017214 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030017215 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010017216 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017217 error->plane[i].surface = I915_READ(DSPSURF(i));
17218 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
17219 }
17220
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017221 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030017222
Chris Wilsonc0336662016-05-06 15:40:21 +010017223 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e2014-04-18 15:55:04 +030017224 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020017225 }
17226
Jani Nikula4d1de972016-03-18 17:05:42 +020017227 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010017228 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030017229 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020017230 error->num_transcoders++; /* Account for eDP. */
17231
17232 for (i = 0; i < error->num_transcoders; i++) {
17233 enum transcoder cpu_transcoder = transcoders[i];
17234
Imre Deakddf9c532013-11-27 22:02:02 +020017235 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020017236 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020017237 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020017238 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020017239 continue;
17240
Chris Wilson63b66e52013-08-08 15:12:06 +020017241 error->transcoder[i].cpu_transcoder = cpu_transcoder;
17242
17243 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
17244 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
17245 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
17246 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
17247 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
17248 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
17249 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017250 }
17251
17252 return error;
17253}
17254
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017255#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
17256
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017257void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017258intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017259 struct drm_device *dev,
17260 struct intel_display_error_state *error)
17261{
Chris Wilsonfac5e232016-07-04 11:34:36 +010017262 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017263 int i;
17264
Chris Wilson63b66e52013-08-08 15:12:06 +020017265 if (!error)
17266 return;
17267
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017268 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Tvrtko Ursulin86527442016-10-13 11:03:00 +010017269 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017270 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030017271 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010017272 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017273 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020017274 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020017275 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017276 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030017277 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017278
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017279 err_printf(m, "Plane [%d]:\n", i);
17280 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
17281 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030017282 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017283 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
17284 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030017285 }
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +010017286 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017287 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017288 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017289 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
17290 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017291 }
17292
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030017293 err_printf(m, "Cursor [%d]:\n", i);
17294 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
17295 err_printf(m, " POS: %08x\n", error->cursor[i].position);
17296 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017297 }
Chris Wilson63b66e52013-08-08 15:12:06 +020017298
17299 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020017300 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020017301 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020017302 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020017303 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020017304 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
17305 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
17306 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
17307 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
17308 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
17309 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
17310 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
17311 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000017312}
Chris Wilson98a2f412016-10-12 10:05:18 +010017313
17314#endif