blob: d0e4023eaf1ac9c54601c24535e45612b799b335 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Imre Deakdb18b6a2016-03-24 12:41:40 +020039#include "intel_dsi.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070040#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080041#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080042#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010043#include <drm/drm_dp_helper.h>
44#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070045#include <drm/drm_plane_helper.h>
46#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080047#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080048#include <linux/reservation.h>
49#include <linux/dma-buf.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080050
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);
Ville Syrjäläd1b32c32016-05-13 23:41:40 +0300126static int broxton_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
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300533/**
534 * Returns whether any output on the specified pipe is of the specified type
535 */
Damien Lespiau40935612014-10-29 11:16:59 +0000536bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300537{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300538 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300539 struct intel_encoder *encoder;
540
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300541 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300542 if (encoder->type == type)
543 return true;
544
545 return false;
546}
547
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200548/**
549 * Returns whether any output on the specified pipe will have the specified
550 * type after a staged modeset is complete, i.e., the same as
551 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
552 * encoder->crtc.
553 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200554static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
555 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200556{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200557 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300558 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200559 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200560 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200561 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200562
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300563 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200564 if (connector_state->crtc != crtc_state->base.crtc)
565 continue;
566
567 num_connectors++;
568
569 encoder = to_intel_encoder(connector_state->best_encoder);
570 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200571 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200572 }
573
574 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200575
576 return false;
577}
578
Imre Deakdccbea32015-06-22 23:35:51 +0300579/*
580 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
581 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
582 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
583 * The helpers' return value is the rate of the clock that is fed to the
584 * display engine's pipe which can be the above fast dot clock rate or a
585 * divided-down version of it.
586 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500587/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300588static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800589{
Shaohua Li21778322009-02-23 15:19:16 +0800590 clock->m = clock->m2 + 2;
591 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200592 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300593 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300594 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
595 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300596
597 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800598}
599
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200600static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
601{
602 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
603}
604
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300605static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800606{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200607 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800608 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200609 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300610 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300611 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
612 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300613
614 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800615}
616
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300617static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300618{
619 clock->m = clock->m1 * clock->m2;
620 clock->p = clock->p1 * clock->p2;
621 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300622 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300623 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
624 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300625
626 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300627}
628
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300629int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300630{
631 clock->m = clock->m1 * clock->m2;
632 clock->p = clock->p1 * clock->p2;
633 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300634 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300635 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
636 clock->n << 22);
637 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300638
639 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300640}
641
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800642#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800643/**
644 * Returns whether the given set of divisors are valid for a given refclk with
645 * the given connectors.
646 */
647
Chris Wilson1b894b52010-12-14 20:04:54 +0000648static bool intel_PLL_is_valid(struct drm_device *dev,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300649 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300650 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800651{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300652 if (clock->n < limit->n.min || limit->n.max < clock->n)
653 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800654 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400655 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800656 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400657 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800658 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400659 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300660
Wayne Boyer666a4532015-12-09 12:29:35 -0800661 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
662 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300663 if (clock->m1 <= clock->m2)
664 INTELPllInvalid("m1 <= m2\n");
665
Wayne Boyer666a4532015-12-09 12:29:35 -0800666 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300667 if (clock->p < limit->p.min || limit->p.max < clock->p)
668 INTELPllInvalid("p out of range\n");
669 if (clock->m < limit->m.min || limit->m.max < clock->m)
670 INTELPllInvalid("m out of range\n");
671 }
672
Jesse Barnes79e53942008-11-07 14:24:08 -0800673 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400674 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800675 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
676 * connector, etc., rather than just a single range.
677 */
678 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400679 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800680
681 return true;
682}
683
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300684static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300685i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300686 const struct intel_crtc_state *crtc_state,
687 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800688{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300689 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800690
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200691 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800692 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100693 * For LVDS just rely on its current settings for dual-channel.
694 * We haven't figured out how to reliably set up different
695 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800696 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100697 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300698 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800699 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300700 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800701 } else {
702 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300703 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800704 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300705 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800706 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300707}
708
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200709/*
710 * Returns a set of divisors for the desired target clock with the given
711 * refclk, or FALSE. The returned values represent the clock equation:
712 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
713 *
714 * Target and reference clocks are specified in kHz.
715 *
716 * If match_clock is provided, then best_clock P divider must match the P
717 * divider from @match_clock used for LVDS downclocking.
718 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300719static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300720i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300721 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300722 int target, int refclk, struct dpll *match_clock,
723 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300724{
725 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300726 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300727 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800728
Akshay Joshi0206e352011-08-16 15:34:10 -0400729 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800730
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300731 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
732
Zhao Yakui42158662009-11-20 11:24:18 +0800733 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
734 clock.m1++) {
735 for (clock.m2 = limit->m2.min;
736 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200737 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800738 break;
739 for (clock.n = limit->n.min;
740 clock.n <= limit->n.max; clock.n++) {
741 for (clock.p1 = limit->p1.min;
742 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800743 int this_err;
744
Imre Deakdccbea32015-06-22 23:35:51 +0300745 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000746 if (!intel_PLL_is_valid(dev, limit,
747 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800748 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800749 if (match_clock &&
750 clock.p != match_clock->p)
751 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800752
753 this_err = abs(clock.dot - target);
754 if (this_err < err) {
755 *best_clock = clock;
756 err = this_err;
757 }
758 }
759 }
760 }
761 }
762
763 return (err != target);
764}
765
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200766/*
767 * Returns a set of divisors for the desired target clock with the given
768 * refclk, or FALSE. The returned values represent the clock equation:
769 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
770 *
771 * Target and reference clocks are specified in kHz.
772 *
773 * If match_clock is provided, then best_clock P divider must match the P
774 * divider from @match_clock used for LVDS downclocking.
775 */
Ma Lingd4906092009-03-18 20:13:27 +0800776static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300777pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200778 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300779 int target, int refclk, struct dpll *match_clock,
780 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200781{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300782 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300783 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200784 int err = target;
785
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200786 memset(best_clock, 0, sizeof(*best_clock));
787
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300788 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
789
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200790 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
791 clock.m1++) {
792 for (clock.m2 = limit->m2.min;
793 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200794 for (clock.n = limit->n.min;
795 clock.n <= limit->n.max; clock.n++) {
796 for (clock.p1 = limit->p1.min;
797 clock.p1 <= limit->p1.max; clock.p1++) {
798 int this_err;
799
Imre Deakdccbea32015-06-22 23:35:51 +0300800 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800801 if (!intel_PLL_is_valid(dev, limit,
802 &clock))
803 continue;
804 if (match_clock &&
805 clock.p != match_clock->p)
806 continue;
807
808 this_err = abs(clock.dot - target);
809 if (this_err < err) {
810 *best_clock = clock;
811 err = this_err;
812 }
813 }
814 }
815 }
816 }
817
818 return (err != target);
819}
820
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200821/*
822 * Returns a set of divisors for the desired target clock with the given
823 * refclk, or FALSE. The returned values represent the clock equation:
824 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200825 *
826 * Target and reference clocks are specified in kHz.
827 *
828 * If match_clock is provided, then best_clock P divider must match the P
829 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200830 */
Ma Lingd4906092009-03-18 20:13:27 +0800831static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300832g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200833 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300834 int target, int refclk, struct dpll *match_clock,
835 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800836{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300837 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300838 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800839 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300840 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400841 /* approximately equals target * 0.00585 */
842 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800843
844 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300845
846 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
847
Ma Lingd4906092009-03-18 20:13:27 +0800848 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200849 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800850 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200851 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800852 for (clock.m1 = limit->m1.max;
853 clock.m1 >= limit->m1.min; clock.m1--) {
854 for (clock.m2 = limit->m2.max;
855 clock.m2 >= limit->m2.min; clock.m2--) {
856 for (clock.p1 = limit->p1.max;
857 clock.p1 >= limit->p1.min; clock.p1--) {
858 int this_err;
859
Imre Deakdccbea32015-06-22 23:35:51 +0300860 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000861 if (!intel_PLL_is_valid(dev, limit,
862 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800863 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000864
865 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800866 if (this_err < err_most) {
867 *best_clock = clock;
868 err_most = this_err;
869 max_n = clock.n;
870 found = true;
871 }
872 }
873 }
874 }
875 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800876 return found;
877}
Ma Lingd4906092009-03-18 20:13:27 +0800878
Imre Deakd5dd62b2015-03-17 11:40:03 +0200879/*
880 * Check if the calculated PLL configuration is more optimal compared to the
881 * best configuration and error found so far. Return the calculated error.
882 */
883static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300884 const struct dpll *calculated_clock,
885 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200886 unsigned int best_error_ppm,
887 unsigned int *error_ppm)
888{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200889 /*
890 * For CHV ignore the error and consider only the P value.
891 * Prefer a bigger P value based on HW requirements.
892 */
893 if (IS_CHERRYVIEW(dev)) {
894 *error_ppm = 0;
895
896 return calculated_clock->p > best_clock->p;
897 }
898
Imre Deak24be4e42015-03-17 11:40:04 +0200899 if (WARN_ON_ONCE(!target_freq))
900 return false;
901
Imre Deakd5dd62b2015-03-17 11:40:03 +0200902 *error_ppm = div_u64(1000000ULL *
903 abs(target_freq - calculated_clock->dot),
904 target_freq);
905 /*
906 * Prefer a better P value over a better (smaller) error if the error
907 * is small. Ensure this preference for future configurations too by
908 * setting the error to 0.
909 */
910 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
911 *error_ppm = 0;
912
913 return true;
914 }
915
916 return *error_ppm + 10 < best_error_ppm;
917}
918
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200919/*
920 * Returns a set of divisors for the desired target clock with the given
921 * refclk, or FALSE. The returned values represent the clock equation:
922 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
923 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800924static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300925vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200926 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300927 int target, int refclk, struct dpll *match_clock,
928 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700929{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200930 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300931 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300932 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300933 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300934 /* min update 19.2 MHz */
935 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300936 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700937
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300938 target *= 5; /* fast clock */
939
940 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700941
942 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300943 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300944 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300945 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300946 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300947 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700948 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300949 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200950 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300951
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300952 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
953 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300954
Imre Deakdccbea32015-06-22 23:35:51 +0300955 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300956
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300957 if (!intel_PLL_is_valid(dev, limit,
958 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300959 continue;
960
Imre Deakd5dd62b2015-03-17 11:40:03 +0200961 if (!vlv_PLL_is_optimal(dev, target,
962 &clock,
963 best_clock,
964 bestppm, &ppm))
965 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300966
Imre Deakd5dd62b2015-03-17 11:40:03 +0200967 *best_clock = clock;
968 bestppm = ppm;
969 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700970 }
971 }
972 }
973 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700974
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300975 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700976}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700977
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200978/*
979 * Returns a set of divisors for the desired target clock with the given
980 * refclk, or FALSE. The returned values represent the clock equation:
981 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
982 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300983static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300984chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200985 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300986 int target, int refclk, struct dpll *match_clock,
987 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300988{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200989 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300990 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200991 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300992 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300993 uint64_t m2;
994 int found = false;
995
996 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200997 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300998
999 /*
1000 * Based on hardware doc, the n always set to 1, and m1 always
1001 * set to 2. If requires to support 200Mhz refclk, we need to
1002 * revisit this because n may not 1 anymore.
1003 */
1004 clock.n = 1, clock.m1 = 2;
1005 target *= 5; /* fast clock */
1006
1007 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1008 for (clock.p2 = limit->p2.p2_fast;
1009 clock.p2 >= limit->p2.p2_slow;
1010 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001011 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001012
1013 clock.p = clock.p1 * clock.p2;
1014
1015 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1016 clock.n) << 22, refclk * clock.m1);
1017
1018 if (m2 > INT_MAX/clock.m1)
1019 continue;
1020
1021 clock.m2 = m2;
1022
Imre Deakdccbea32015-06-22 23:35:51 +03001023 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001024
1025 if (!intel_PLL_is_valid(dev, limit, &clock))
1026 continue;
1027
Imre Deak9ca3ba02015-03-17 11:40:05 +02001028 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1029 best_error_ppm, &error_ppm))
1030 continue;
1031
1032 *best_clock = clock;
1033 best_error_ppm = error_ppm;
1034 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001035 }
1036 }
1037
1038 return found;
1039}
1040
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001041bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03001042 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001043{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001044 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03001045 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001046
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001047 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001048 target_clock, refclk, NULL, best_clock);
1049}
1050
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001051bool intel_crtc_active(struct drm_crtc *crtc)
1052{
1053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1054
1055 /* Be paranoid as we can arrive here with only partial
1056 * state retrieved from the hardware during setup.
1057 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001058 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001059 * as Haswell has gained clock readout/fastboot support.
1060 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001061 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001062 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001063 *
1064 * FIXME: The intel_crtc->active here should be switched to
1065 * crtc->state->active once we have proper CRTC states wired up
1066 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001067 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001068 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001069 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001070}
1071
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001072enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1073 enum pipe pipe)
1074{
1075 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1076 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1077
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001078 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001079}
1080
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001081static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1082{
1083 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001084 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001085 u32 line1, line2;
1086 u32 line_mask;
1087
1088 if (IS_GEN2(dev))
1089 line_mask = DSL_LINEMASK_GEN2;
1090 else
1091 line_mask = DSL_LINEMASK_GEN3;
1092
1093 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001094 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001095 line2 = I915_READ(reg) & line_mask;
1096
1097 return line1 == line2;
1098}
1099
Keith Packardab7ad7f2010-10-03 00:33:06 -07001100/*
1101 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001102 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001103 *
1104 * After disabling a pipe, we can't wait for vblank in the usual way,
1105 * spinning on the vblank interrupt status bit, since we won't actually
1106 * see an interrupt when the pipe is disabled.
1107 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001108 * On Gen4 and above:
1109 * wait for the pipe register state bit to turn off
1110 *
1111 * Otherwise:
1112 * wait for the display line value to settle (it usually
1113 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001114 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001115 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001116static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001117{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001118 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001119 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001120 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001121 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001122
Keith Packardab7ad7f2010-10-03 00:33:06 -07001123 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001124 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001125
Keith Packardab7ad7f2010-10-03 00:33:06 -07001126 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001127 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1128 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001129 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001130 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001131 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001132 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001133 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001134 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001135}
1136
Jesse Barnesb24e7172011-01-04 15:09:30 -08001137/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001138void assert_pll(struct drm_i915_private *dev_priv,
1139 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001140{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001141 u32 val;
1142 bool cur_state;
1143
Ville Syrjälä649636e2015-09-22 19:50:01 +03001144 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001145 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001146 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001147 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001148 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001149}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001150
Jani Nikula23538ef2013-08-27 15:12:22 +03001151/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001152void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001153{
1154 u32 val;
1155 bool cur_state;
1156
Ville Syrjäläa5805162015-05-26 20:42:30 +03001157 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001158 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001159 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001160
1161 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001162 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001163 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001164 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001165}
Jani Nikula23538ef2013-08-27 15:12:22 +03001166
Jesse Barnes040484a2011-01-03 12:14:26 -08001167static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1168 enum pipe pipe, bool state)
1169{
Jesse Barnes040484a2011-01-03 12:14:26 -08001170 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001171 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1172 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001173
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001174 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001175 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001176 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001177 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001178 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001179 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001180 cur_state = !!(val & FDI_TX_ENABLE);
1181 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001182 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001183 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001184 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001185}
1186#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1187#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1188
1189static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1190 enum pipe pipe, bool state)
1191{
Jesse Barnes040484a2011-01-03 12:14:26 -08001192 u32 val;
1193 bool cur_state;
1194
Ville Syrjälä649636e2015-09-22 19:50:01 +03001195 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001196 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001197 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001198 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001199 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001200}
1201#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1202#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1203
1204static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1205 enum pipe pipe)
1206{
Jesse Barnes040484a2011-01-03 12:14:26 -08001207 u32 val;
1208
1209 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001210 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001211 return;
1212
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001213 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001214 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001215 return;
1216
Ville Syrjälä649636e2015-09-22 19:50:01 +03001217 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001218 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 -08001219}
1220
Daniel Vetter55607e82013-06-16 21:42:39 +02001221void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1222 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001223{
Jesse Barnes040484a2011-01-03 12:14:26 -08001224 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001225 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001226
Ville Syrjälä649636e2015-09-22 19:50:01 +03001227 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001228 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001229 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001230 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001231 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001232}
1233
Daniel Vetterb680c372014-09-19 18:27:27 +02001234void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1235 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001236{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001237 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001238 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001239 u32 val;
1240 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001241 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001242
Jani Nikulabedd4db2014-08-22 15:04:13 +03001243 if (WARN_ON(HAS_DDI(dev)))
1244 return;
1245
1246 if (HAS_PCH_SPLIT(dev)) {
1247 u32 port_sel;
1248
Jesse Barnesea0760c2011-01-04 15:09:32 -08001249 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001250 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1251
1252 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1253 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1254 panel_pipe = PIPE_B;
1255 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001256 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001257 /* presumably write lock depends on pipe, not port select */
1258 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1259 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001260 } else {
1261 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001262 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1263 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001264 }
1265
1266 val = I915_READ(pp_reg);
1267 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001268 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001269 locked = false;
1270
Rob Clarke2c719b2014-12-15 13:56:32 -05001271 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001272 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001273 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001274}
1275
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001276static void assert_cursor(struct drm_i915_private *dev_priv,
1277 enum pipe pipe, bool state)
1278{
1279 struct drm_device *dev = dev_priv->dev;
1280 bool cur_state;
1281
Paulo Zanonid9d82082014-02-27 16:30:56 -03001282 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001283 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001284 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001285 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001286
Rob Clarke2c719b2014-12-15 13:56:32 -05001287 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001288 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001289 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001290}
1291#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1292#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1293
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001294void assert_pipe(struct drm_i915_private *dev_priv,
1295 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001296{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001297 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001298 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1299 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001300 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001301
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001302 /* if we need the pipe quirk it must be always on */
1303 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1304 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001305 state = true;
1306
Imre Deak4feed0e2016-02-12 18:55:14 +02001307 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1308 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001309 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001310 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001311
1312 intel_display_power_put(dev_priv, power_domain);
1313 } else {
1314 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001315 }
1316
Rob Clarke2c719b2014-12-15 13:56:32 -05001317 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001318 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001319 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001320}
1321
Chris Wilson931872f2012-01-16 23:01:13 +00001322static void assert_plane(struct drm_i915_private *dev_priv,
1323 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001324{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001325 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001326 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001327
Ville Syrjälä649636e2015-09-22 19:50:01 +03001328 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001329 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001330 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001331 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001332 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001333}
1334
Chris Wilson931872f2012-01-16 23:01:13 +00001335#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1336#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1337
Jesse Barnesb24e7172011-01-04 15:09:30 -08001338static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1339 enum pipe pipe)
1340{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001341 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001342 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001343
Ville Syrjälä653e1022013-06-04 13:49:05 +03001344 /* Primary planes are fixed to pipes on gen4+ */
1345 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001346 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001347 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001348 "plane %c assertion failure, should be disabled but not\n",
1349 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001350 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001351 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001352
Jesse Barnesb24e7172011-01-04 15:09:30 -08001353 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001354 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001355 u32 val = I915_READ(DSPCNTR(i));
1356 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001357 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001358 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001359 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1360 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001361 }
1362}
1363
Jesse Barnes19332d72013-03-28 09:55:38 -07001364static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1365 enum pipe pipe)
1366{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001367 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001368 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001369
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001370 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001371 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001372 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001373 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001374 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1375 sprite, pipe_name(pipe));
1376 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001377 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001378 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001379 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001380 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001381 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001382 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001383 }
1384 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001385 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001386 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001387 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001388 plane_name(pipe), pipe_name(pipe));
1389 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001390 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001391 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001392 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1393 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001394 }
1395}
1396
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001397static void assert_vblank_disabled(struct drm_crtc *crtc)
1398{
Rob Clarke2c719b2014-12-15 13:56:32 -05001399 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001400 drm_crtc_vblank_put(crtc);
1401}
1402
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001403void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1404 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001405{
Jesse Barnes92f25842011-01-04 15:09:34 -08001406 u32 val;
1407 bool enabled;
1408
Ville Syrjälä649636e2015-09-22 19:50:01 +03001409 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001410 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001411 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001412 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1413 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001414}
1415
Keith Packard4e634382011-08-06 10:39:45 -07001416static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1417 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001418{
1419 if ((val & DP_PORT_EN) == 0)
1420 return false;
1421
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001422 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001423 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001424 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1425 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001426 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001427 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1428 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001429 } else {
1430 if ((val & DP_PIPE_MASK) != (pipe << 30))
1431 return false;
1432 }
1433 return true;
1434}
1435
Keith Packard1519b992011-08-06 10:35:34 -07001436static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1437 enum pipe pipe, u32 val)
1438{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001439 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001440 return false;
1441
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001442 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001443 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001444 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001445 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001446 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1447 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001448 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001449 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001450 return false;
1451 }
1452 return true;
1453}
1454
1455static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1456 enum pipe pipe, u32 val)
1457{
1458 if ((val & LVDS_PORT_EN) == 0)
1459 return false;
1460
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001461 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001462 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1463 return false;
1464 } else {
1465 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1466 return false;
1467 }
1468 return true;
1469}
1470
1471static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1472 enum pipe pipe, u32 val)
1473{
1474 if ((val & ADPA_DAC_ENABLE) == 0)
1475 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001476 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001477 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1478 return false;
1479 } else {
1480 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1481 return false;
1482 }
1483 return true;
1484}
1485
Jesse Barnes291906f2011-02-02 12:28:03 -08001486static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001487 enum pipe pipe, i915_reg_t reg,
1488 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001489{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001490 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001491 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001492 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001493 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001494
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001495 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001496 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001497 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001498}
1499
1500static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001501 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001502{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001503 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001504 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001505 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001506 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001507
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001508 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001509 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001510 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001511}
1512
1513static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1514 enum pipe pipe)
1515{
Jesse Barnes291906f2011-02-02 12:28:03 -08001516 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001517
Keith Packardf0575e92011-07-25 22:12:43 -07001518 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1519 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1520 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001521
Ville Syrjälä649636e2015-09-22 19:50:01 +03001522 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001523 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001524 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001525 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001526
Ville Syrjälä649636e2015-09-22 19:50:01 +03001527 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001528 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001529 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001530 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001531
Paulo Zanonie2debe92013-02-18 19:00:27 -03001532 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1533 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1534 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001535}
1536
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001537static void _vlv_enable_pll(struct intel_crtc *crtc,
1538 const struct intel_crtc_state *pipe_config)
1539{
1540 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1541 enum pipe pipe = crtc->pipe;
1542
1543 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1544 POSTING_READ(DPLL(pipe));
1545 udelay(150);
1546
1547 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1548 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1549}
1550
Ville Syrjäläd288f652014-10-28 13:20:22 +02001551static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001552 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001553{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001554 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001555 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001556
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001557 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001558
Daniel Vetter87442f72013-06-06 00:52:17 +02001559 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001560 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001561
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001562 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1563 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001564
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001565 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1566 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001567}
1568
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001569
1570static void _chv_enable_pll(struct intel_crtc *crtc,
1571 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001572{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001573 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001574 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001575 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001576 u32 tmp;
1577
Ville Syrjäläa5805162015-05-26 20:42:30 +03001578 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001579
1580 /* Enable back the 10bit clock to display controller */
1581 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1582 tmp |= DPIO_DCLKP_EN;
1583 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1584
Ville Syrjälä54433e92015-05-26 20:42:31 +03001585 mutex_unlock(&dev_priv->sb_lock);
1586
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001587 /*
1588 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1589 */
1590 udelay(1);
1591
1592 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001593 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001594
1595 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001596 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001597 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001598}
1599
1600static void chv_enable_pll(struct intel_crtc *crtc,
1601 const struct intel_crtc_state *pipe_config)
1602{
1603 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1604 enum pipe pipe = crtc->pipe;
1605
1606 assert_pipe_disabled(dev_priv, pipe);
1607
1608 /* PLL is protected by panel, make sure we can write it */
1609 assert_panel_unlocked(dev_priv, pipe);
1610
1611 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1612 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001613
Ville Syrjäläc2317752016-03-15 16:39:56 +02001614 if (pipe != PIPE_A) {
1615 /*
1616 * WaPixelRepeatModeFixForC0:chv
1617 *
1618 * DPLLCMD is AWOL. Use chicken bits to propagate
1619 * the value from DPLLBMD to either pipe B or C.
1620 */
1621 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1622 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1623 I915_WRITE(CBR4_VLV, 0);
1624 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1625
1626 /*
1627 * DPLLB VGA mode also seems to cause problems.
1628 * We should always have it disabled.
1629 */
1630 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1631 } else {
1632 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1633 POSTING_READ(DPLL_MD(pipe));
1634 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001635}
1636
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001637static int intel_num_dvo_pipes(struct drm_device *dev)
1638{
1639 struct intel_crtc *crtc;
1640 int count = 0;
1641
1642 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001643 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001644 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001645
1646 return count;
1647}
1648
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001649static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001650{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001651 struct drm_device *dev = crtc->base.dev;
1652 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001653 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001654 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001655
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001656 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001657
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001658 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001659 if (IS_MOBILE(dev) && !IS_I830(dev))
1660 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001661
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001662 /* Enable DVO 2x clock on both PLLs if necessary */
1663 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1664 /*
1665 * It appears to be important that we don't enable this
1666 * for the current pipe before otherwise configuring the
1667 * PLL. No idea how this should be handled if multiple
1668 * DVO outputs are enabled simultaneosly.
1669 */
1670 dpll |= DPLL_DVO_2X_MODE;
1671 I915_WRITE(DPLL(!crtc->pipe),
1672 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1673 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001674
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001675 /*
1676 * Apparently we need to have VGA mode enabled prior to changing
1677 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1678 * dividers, even though the register value does change.
1679 */
1680 I915_WRITE(reg, 0);
1681
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001682 I915_WRITE(reg, dpll);
1683
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001684 /* Wait for the clocks to stabilize. */
1685 POSTING_READ(reg);
1686 udelay(150);
1687
1688 if (INTEL_INFO(dev)->gen >= 4) {
1689 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001690 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001691 } else {
1692 /* The pixel multiplier can only be updated once the
1693 * DPLL is enabled and the clocks are stable.
1694 *
1695 * So write it again.
1696 */
1697 I915_WRITE(reg, dpll);
1698 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001699
1700 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001701 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001702 POSTING_READ(reg);
1703 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001704 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001705 POSTING_READ(reg);
1706 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001707 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001708 POSTING_READ(reg);
1709 udelay(150); /* wait for warmup */
1710}
1711
1712/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001713 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001714 * @dev_priv: i915 private structure
1715 * @pipe: pipe PLL to disable
1716 *
1717 * Disable the PLL for @pipe, making sure the pipe is off first.
1718 *
1719 * Note! This is for pre-ILK only.
1720 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001721static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001722{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001723 struct drm_device *dev = crtc->base.dev;
1724 struct drm_i915_private *dev_priv = dev->dev_private;
1725 enum pipe pipe = crtc->pipe;
1726
1727 /* Disable DVO 2x clock on both PLLs if necessary */
1728 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001729 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001730 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001731 I915_WRITE(DPLL(PIPE_B),
1732 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1733 I915_WRITE(DPLL(PIPE_A),
1734 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1735 }
1736
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001737 /* Don't disable pipe or pipe PLLs if needed */
1738 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1739 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001740 return;
1741
1742 /* Make sure the pipe isn't still relying on us */
1743 assert_pipe_disabled(dev_priv, pipe);
1744
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001745 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001746 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001747}
1748
Jesse Barnesf6071162013-10-01 10:41:38 -07001749static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1750{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001751 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001752
1753 /* Make sure the pipe isn't still relying on us */
1754 assert_pipe_disabled(dev_priv, pipe);
1755
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02001756 val = DPLL_INTEGRATED_REF_CLK_VLV |
1757 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1758 if (pipe != PIPE_A)
1759 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1760
Jesse Barnesf6071162013-10-01 10:41:38 -07001761 I915_WRITE(DPLL(pipe), val);
1762 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001763}
1764
1765static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1766{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001767 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001768 u32 val;
1769
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001770 /* Make sure the pipe isn't still relying on us */
1771 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001772
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001773 val = DPLL_SSC_REF_CLK_CHV |
1774 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001775 if (pipe != PIPE_A)
1776 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02001777
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001778 I915_WRITE(DPLL(pipe), val);
1779 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001780
Ville Syrjäläa5805162015-05-26 20:42:30 +03001781 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001782
1783 /* Disable 10bit clock to display controller */
1784 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1785 val &= ~DPIO_DCLKP_EN;
1786 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1787
Ville Syrjäläa5805162015-05-26 20:42:30 +03001788 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001789}
1790
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001791void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001792 struct intel_digital_port *dport,
1793 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001794{
1795 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001796 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001797
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001798 switch (dport->port) {
1799 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001800 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001801 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001802 break;
1803 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001804 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001805 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001806 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001807 break;
1808 case PORT_D:
1809 port_mask = DPLL_PORTD_READY_MASK;
1810 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001811 break;
1812 default:
1813 BUG();
1814 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001815
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001816 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1817 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1818 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001819}
1820
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001821static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1822 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001823{
Daniel Vetter23670b322012-11-01 09:15:30 +01001824 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001825 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001826 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001827 i915_reg_t reg;
1828 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001829
Jesse Barnes040484a2011-01-03 12:14:26 -08001830 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001831 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001832
1833 /* FDI must be feeding us bits for PCH ports */
1834 assert_fdi_tx_enabled(dev_priv, pipe);
1835 assert_fdi_rx_enabled(dev_priv, pipe);
1836
Daniel Vetter23670b322012-11-01 09:15:30 +01001837 if (HAS_PCH_CPT(dev)) {
1838 /* Workaround: Set the timing override bit before enabling the
1839 * pch transcoder. */
1840 reg = TRANS_CHICKEN2(pipe);
1841 val = I915_READ(reg);
1842 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1843 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001844 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001845
Daniel Vetterab9412b2013-05-03 11:49:46 +02001846 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001847 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001848 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001849
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001850 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001851 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001852 * Make the BPC in transcoder be consistent with
1853 * that in pipeconf reg. For HDMI we must use 8bpc
1854 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001855 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001856 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001857 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1858 val |= PIPECONF_8BPC;
1859 else
1860 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001861 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001862
1863 val &= ~TRANS_INTERLACE_MASK;
1864 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001865 if (HAS_PCH_IBX(dev_priv) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001866 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001867 val |= TRANS_LEGACY_INTERLACED_ILK;
1868 else
1869 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001870 else
1871 val |= TRANS_PROGRESSIVE;
1872
Jesse Barnes040484a2011-01-03 12:14:26 -08001873 I915_WRITE(reg, val | TRANS_ENABLE);
1874 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001875 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001876}
1877
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001878static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001879 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001880{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001881 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001882
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001883 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001884 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001885 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001886
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001887 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001888 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001889 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001890 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001891
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001892 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001893 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001894
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001895 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1896 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001897 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001898 else
1899 val |= TRANS_PROGRESSIVE;
1900
Daniel Vetterab9412b2013-05-03 11:49:46 +02001901 I915_WRITE(LPT_TRANSCONF, val);
1902 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001903 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001904}
1905
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001906static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1907 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001908{
Daniel Vetter23670b322012-11-01 09:15:30 +01001909 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001910 i915_reg_t reg;
1911 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001912
1913 /* FDI relies on the transcoder */
1914 assert_fdi_tx_disabled(dev_priv, pipe);
1915 assert_fdi_rx_disabled(dev_priv, pipe);
1916
Jesse Barnes291906f2011-02-02 12:28:03 -08001917 /* Ports must be off as well */
1918 assert_pch_ports_disabled(dev_priv, pipe);
1919
Daniel Vetterab9412b2013-05-03 11:49:46 +02001920 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001921 val = I915_READ(reg);
1922 val &= ~TRANS_ENABLE;
1923 I915_WRITE(reg, val);
1924 /* wait for PCH transcoder off, transcoder state */
1925 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001926 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001927
Ville Syrjäläc4656132015-10-29 21:25:56 +02001928 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001929 /* Workaround: Clear the timing override chicken bit again. */
1930 reg = TRANS_CHICKEN2(pipe);
1931 val = I915_READ(reg);
1932 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1933 I915_WRITE(reg, val);
1934 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001935}
1936
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001937static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001938{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001939 u32 val;
1940
Daniel Vetterab9412b2013-05-03 11:49:46 +02001941 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001942 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001943 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001944 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001945 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001946 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001947
1948 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001949 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001950 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001951 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001952}
1953
1954/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001955 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001956 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001957 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001958 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001959 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001960 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001961static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001962{
Paulo Zanoni03722642014-01-17 13:51:09 -02001963 struct drm_device *dev = crtc->base.dev;
1964 struct drm_i915_private *dev_priv = dev->dev_private;
1965 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001966 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001967 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001968 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001969 u32 val;
1970
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001971 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1972
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001973 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001974 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001975 assert_sprites_disabled(dev_priv, pipe);
1976
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001977 if (HAS_PCH_LPT(dev_priv))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001978 pch_transcoder = TRANSCODER_A;
1979 else
1980 pch_transcoder = pipe;
1981
Jesse Barnesb24e7172011-01-04 15:09:30 -08001982 /*
1983 * A pipe without a PLL won't actually be able to drive bits from
1984 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1985 * need the check.
1986 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001987 if (HAS_GMCH_DISPLAY(dev_priv))
Jani Nikulaa65347b2015-11-27 12:21:46 +02001988 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03001989 assert_dsi_pll_enabled(dev_priv);
1990 else
1991 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001992 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001993 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001994 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001995 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001996 assert_fdi_tx_pll_enabled(dev_priv,
1997 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001998 }
1999 /* FIXME: assert CPU port conditions for SNB+ */
2000 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002001
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002002 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002003 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002004 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002005 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2006 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002007 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002008 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002009
2010 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002011 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02002012
2013 /*
2014 * Until the pipe starts DSL will read as 0, which would cause
2015 * an apparent vblank timestamp jump, which messes up also the
2016 * frame count when it's derived from the timestamps. So let's
2017 * wait for the pipe to start properly before we call
2018 * drm_crtc_vblank_on()
2019 */
2020 if (dev->max_vblank_count == 0 &&
2021 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2022 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002023}
2024
2025/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002026 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002027 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002028 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002029 * Disable the pipe of @crtc, making sure that various hardware
2030 * specific requirements are met, if applicable, e.g. plane
2031 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002032 *
2033 * Will wait until the pipe has shut down before returning.
2034 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002035static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002036{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002037 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002038 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002039 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002040 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002041 u32 val;
2042
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002043 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2044
Jesse Barnesb24e7172011-01-04 15:09:30 -08002045 /*
2046 * Make sure planes won't keep trying to pump pixels to us,
2047 * or we might hang the display.
2048 */
2049 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002050 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002051 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002052
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002053 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002054 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002055 if ((val & PIPECONF_ENABLE) == 0)
2056 return;
2057
Ville Syrjälä67adc642014-08-15 01:21:57 +03002058 /*
2059 * Double wide has implications for planes
2060 * so best keep it disabled when not needed.
2061 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002062 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002063 val &= ~PIPECONF_DOUBLE_WIDE;
2064
2065 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002066 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2067 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002068 val &= ~PIPECONF_ENABLE;
2069
2070 I915_WRITE(reg, val);
2071 if ((val & PIPECONF_ENABLE) == 0)
2072 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002073}
2074
Chris Wilson693db182013-03-05 14:52:39 +00002075static bool need_vtd_wa(struct drm_device *dev)
2076{
2077#ifdef CONFIG_INTEL_IOMMU
2078 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2079 return true;
2080#endif
2081 return false;
2082}
2083
Ville Syrjälä832be822016-01-12 21:08:33 +02002084static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2085{
2086 return IS_GEN2(dev_priv) ? 2048 : 4096;
2087}
2088
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002089static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2090 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002091{
2092 switch (fb_modifier) {
2093 case DRM_FORMAT_MOD_NONE:
2094 return cpp;
2095 case I915_FORMAT_MOD_X_TILED:
2096 if (IS_GEN2(dev_priv))
2097 return 128;
2098 else
2099 return 512;
2100 case I915_FORMAT_MOD_Y_TILED:
2101 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2102 return 128;
2103 else
2104 return 512;
2105 case I915_FORMAT_MOD_Yf_TILED:
2106 switch (cpp) {
2107 case 1:
2108 return 64;
2109 case 2:
2110 case 4:
2111 return 128;
2112 case 8:
2113 case 16:
2114 return 256;
2115 default:
2116 MISSING_CASE(cpp);
2117 return cpp;
2118 }
2119 break;
2120 default:
2121 MISSING_CASE(fb_modifier);
2122 return cpp;
2123 }
2124}
2125
Ville Syrjälä832be822016-01-12 21:08:33 +02002126unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2127 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002128{
Ville Syrjälä832be822016-01-12 21:08:33 +02002129 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2130 return 1;
2131 else
2132 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002133 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002134}
2135
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002136/* Return the tile dimensions in pixel units */
2137static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2138 unsigned int *tile_width,
2139 unsigned int *tile_height,
2140 uint64_t fb_modifier,
2141 unsigned int cpp)
2142{
2143 unsigned int tile_width_bytes =
2144 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2145
2146 *tile_width = tile_width_bytes / cpp;
2147 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2148}
2149
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002150unsigned int
2151intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002152 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002153{
Ville Syrjälä832be822016-01-12 21:08:33 +02002154 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2155 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2156
2157 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002158}
2159
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002160unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2161{
2162 unsigned int size = 0;
2163 int i;
2164
2165 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2166 size += rot_info->plane[i].width * rot_info->plane[i].height;
2167
2168 return size;
2169}
2170
Daniel Vetter75c82a52015-10-14 16:51:04 +02002171static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002172intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2173 const struct drm_framebuffer *fb,
2174 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002175{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002176 if (intel_rotation_90_or_270(rotation)) {
2177 *view = i915_ggtt_view_rotated;
2178 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2179 } else {
2180 *view = i915_ggtt_view_normal;
2181 }
2182}
2183
2184static void
2185intel_fill_fb_info(struct drm_i915_private *dev_priv,
2186 struct drm_framebuffer *fb)
2187{
2188 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002189 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002190
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002191 tile_size = intel_tile_size(dev_priv);
2192
2193 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002194 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2195 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002196
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002197 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2198 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002199
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002200 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002201 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002202 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2203 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002204
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002205 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002206 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2207 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002208 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002209}
2210
Ville Syrjälä603525d2016-01-12 21:08:37 +02002211static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002212{
2213 if (INTEL_INFO(dev_priv)->gen >= 9)
2214 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002215 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002216 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002217 return 128 * 1024;
2218 else if (INTEL_INFO(dev_priv)->gen >= 4)
2219 return 4 * 1024;
2220 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002221 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002222}
2223
Ville Syrjälä603525d2016-01-12 21:08:37 +02002224static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2225 uint64_t fb_modifier)
2226{
2227 switch (fb_modifier) {
2228 case DRM_FORMAT_MOD_NONE:
2229 return intel_linear_alignment(dev_priv);
2230 case I915_FORMAT_MOD_X_TILED:
2231 if (INTEL_INFO(dev_priv)->gen >= 9)
2232 return 256 * 1024;
2233 return 0;
2234 case I915_FORMAT_MOD_Y_TILED:
2235 case I915_FORMAT_MOD_Yf_TILED:
2236 return 1 * 1024 * 1024;
2237 default:
2238 MISSING_CASE(fb_modifier);
2239 return 0;
2240 }
2241}
2242
Chris Wilson127bd2a2010-07-23 23:32:05 +01002243int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002244intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2245 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002246{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002247 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002248 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002249 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002250 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002251 u32 alignment;
2252 int ret;
2253
Matt Roperebcdd392014-07-09 16:22:11 -07002254 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2255
Ville Syrjälä603525d2016-01-12 21:08:37 +02002256 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002257
Ville Syrjälä3465c582016-02-15 22:54:43 +02002258 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002259
Chris Wilson693db182013-03-05 14:52:39 +00002260 /* Note that the w/a also requires 64 PTE of padding following the
2261 * bo. We currently fill all unused PTE with the shadow page and so
2262 * we should always have valid PTE following the scanout preventing
2263 * the VT-d warning.
2264 */
2265 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2266 alignment = 256 * 1024;
2267
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002268 /*
2269 * Global gtt pte registers are special registers which actually forward
2270 * writes to a chunk of system memory. Which means that there is no risk
2271 * that the register values disappear as soon as we call
2272 * intel_runtime_pm_put(), so it is correct to wrap only the
2273 * pin/unpin/fence and not more.
2274 */
2275 intel_runtime_pm_get(dev_priv);
2276
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002277 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2278 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002279 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002280 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002281
2282 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2283 * fence, whereas 965+ only requires a fence if using
2284 * framebuffer compression. For simplicity, we always install
2285 * a fence as the cost is not that onerous.
2286 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002287 if (view.type == I915_GGTT_VIEW_NORMAL) {
2288 ret = i915_gem_object_get_fence(obj);
2289 if (ret == -EDEADLK) {
2290 /*
2291 * -EDEADLK means there are no free fences
2292 * no pending flips.
2293 *
2294 * This is propagated to atomic, but it uses
2295 * -EDEADLK to force a locking recovery, so
2296 * change the returned error to -EBUSY.
2297 */
2298 ret = -EBUSY;
2299 goto err_unpin;
2300 } else if (ret)
2301 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002302
Vivek Kasireddy98072162015-10-29 18:54:38 -07002303 i915_gem_object_pin_fence(obj);
2304 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002305
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002306 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002307 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002308
2309err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002310 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002311err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002312 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002313 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002314}
2315
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +01002316void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002317{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002318 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002319 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002320
Matt Roperebcdd392014-07-09 16:22:11 -07002321 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2322
Ville Syrjälä3465c582016-02-15 22:54:43 +02002323 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002324
Vivek Kasireddy98072162015-10-29 18:54:38 -07002325 if (view.type == I915_GGTT_VIEW_NORMAL)
2326 i915_gem_object_unpin_fence(obj);
2327
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002328 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002329}
2330
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002331/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002332 * Adjust the tile offset by moving the difference into
2333 * the x/y offsets.
2334 *
2335 * Input tile dimensions and pitch must already be
2336 * rotated to match x and y, and in pixel units.
2337 */
2338static u32 intel_adjust_tile_offset(int *x, int *y,
2339 unsigned int tile_width,
2340 unsigned int tile_height,
2341 unsigned int tile_size,
2342 unsigned int pitch_tiles,
2343 u32 old_offset,
2344 u32 new_offset)
2345{
2346 unsigned int tiles;
2347
2348 WARN_ON(old_offset & (tile_size - 1));
2349 WARN_ON(new_offset & (tile_size - 1));
2350 WARN_ON(new_offset > old_offset);
2351
2352 tiles = (old_offset - new_offset) / tile_size;
2353
2354 *y += tiles / pitch_tiles * tile_height;
2355 *x += tiles % pitch_tiles * tile_width;
2356
2357 return new_offset;
2358}
2359
2360/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002361 * Computes the linear offset to the base tile and adjusts
2362 * x, y. bytes per pixel is assumed to be a power-of-two.
2363 *
2364 * In the 90/270 rotated case, x and y are assumed
2365 * to be already rotated to match the rotated GTT view, and
2366 * pitch is the tile_height aligned framebuffer height.
2367 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002368u32 intel_compute_tile_offset(int *x, int *y,
2369 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002370 unsigned int pitch,
2371 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002372{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002373 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2374 uint64_t fb_modifier = fb->modifier[plane];
2375 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002376 u32 offset, offset_aligned, alignment;
2377
2378 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2379 if (alignment)
2380 alignment--;
2381
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002382 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002383 unsigned int tile_size, tile_width, tile_height;
2384 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002385
Ville Syrjäläd8433102016-01-12 21:08:35 +02002386 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002387 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2388 fb_modifier, cpp);
2389
2390 if (intel_rotation_90_or_270(rotation)) {
2391 pitch_tiles = pitch / tile_height;
2392 swap(tile_width, tile_height);
2393 } else {
2394 pitch_tiles = pitch / (tile_width * cpp);
2395 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002396
Ville Syrjäläd8433102016-01-12 21:08:35 +02002397 tile_rows = *y / tile_height;
2398 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002399
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002400 tiles = *x / tile_width;
2401 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002402
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002403 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2404 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002405
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002406 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2407 tile_size, pitch_tiles,
2408 offset, offset_aligned);
2409 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002410 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002411 offset_aligned = offset & ~alignment;
2412
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002413 *y = (offset & alignment) / pitch;
2414 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002415 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002416
2417 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002418}
2419
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002420static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002421{
2422 switch (format) {
2423 case DISPPLANE_8BPP:
2424 return DRM_FORMAT_C8;
2425 case DISPPLANE_BGRX555:
2426 return DRM_FORMAT_XRGB1555;
2427 case DISPPLANE_BGRX565:
2428 return DRM_FORMAT_RGB565;
2429 default:
2430 case DISPPLANE_BGRX888:
2431 return DRM_FORMAT_XRGB8888;
2432 case DISPPLANE_RGBX888:
2433 return DRM_FORMAT_XBGR8888;
2434 case DISPPLANE_BGRX101010:
2435 return DRM_FORMAT_XRGB2101010;
2436 case DISPPLANE_RGBX101010:
2437 return DRM_FORMAT_XBGR2101010;
2438 }
2439}
2440
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002441static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2442{
2443 switch (format) {
2444 case PLANE_CTL_FORMAT_RGB_565:
2445 return DRM_FORMAT_RGB565;
2446 default:
2447 case PLANE_CTL_FORMAT_XRGB_8888:
2448 if (rgb_order) {
2449 if (alpha)
2450 return DRM_FORMAT_ABGR8888;
2451 else
2452 return DRM_FORMAT_XBGR8888;
2453 } else {
2454 if (alpha)
2455 return DRM_FORMAT_ARGB8888;
2456 else
2457 return DRM_FORMAT_XRGB8888;
2458 }
2459 case PLANE_CTL_FORMAT_XRGB_2101010:
2460 if (rgb_order)
2461 return DRM_FORMAT_XBGR2101010;
2462 else
2463 return DRM_FORMAT_XRGB2101010;
2464 }
2465}
2466
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002467static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002468intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2469 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002470{
2471 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002472 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002473 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002474 struct drm_i915_gem_object *obj = NULL;
2475 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002476 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002477 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2478 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2479 PAGE_SIZE);
2480
2481 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002482
Chris Wilsonff2652e2014-03-10 08:07:02 +00002483 if (plane_config->size == 0)
2484 return false;
2485
Paulo Zanoni3badb492015-09-23 12:52:23 -03002486 /* If the FB is too big, just don't use it since fbdev is not very
2487 * important and we should probably use that space with FBC or other
2488 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002489 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002490 return false;
2491
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002492 mutex_lock(&dev->struct_mutex);
2493
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002494 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2495 base_aligned,
2496 base_aligned,
2497 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002498 if (!obj) {
2499 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002500 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002501 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002502
Damien Lespiau49af4492015-01-20 12:51:44 +00002503 obj->tiling_mode = plane_config->tiling;
2504 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002505 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002506
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002507 mode_cmd.pixel_format = fb->pixel_format;
2508 mode_cmd.width = fb->width;
2509 mode_cmd.height = fb->height;
2510 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002511 mode_cmd.modifier[0] = fb->modifier[0];
2512 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002513
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002514 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002515 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002516 DRM_DEBUG_KMS("intel fb init failed\n");
2517 goto out_unref_obj;
2518 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002519
Jesse Barnes46f297f2014-03-07 08:57:48 -08002520 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002521
Daniel Vetterf6936e22015-03-26 12:17:05 +01002522 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002523 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002524
2525out_unref_obj:
2526 drm_gem_object_unreference(&obj->base);
2527 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002528 return false;
2529}
2530
Daniel Vetter5a21b662016-05-24 17:13:53 +02002531/* Update plane->state->fb to match plane->fb after driver-internal updates */
2532static void
2533update_state_fb(struct drm_plane *plane)
2534{
2535 if (plane->fb == plane->state->fb)
2536 return;
2537
2538 if (plane->state->fb)
2539 drm_framebuffer_unreference(plane->state->fb);
2540 plane->state->fb = plane->fb;
2541 if (plane->state->fb)
2542 drm_framebuffer_reference(plane->state->fb);
2543}
2544
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002545static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002546intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2547 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002548{
2549 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002550 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002551 struct drm_crtc *c;
2552 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002553 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002554 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002555 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002556 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2557 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002558 struct intel_plane_state *intel_state =
2559 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002560 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002561
Damien Lespiau2d140302015-02-05 17:22:18 +00002562 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002563 return;
2564
Daniel Vetterf6936e22015-03-26 12:17:05 +01002565 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002566 fb = &plane_config->fb->base;
2567 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002568 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002569
Damien Lespiau2d140302015-02-05 17:22:18 +00002570 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002571
2572 /*
2573 * Failed to alloc the obj, check to see if we should share
2574 * an fb with another CRTC instead
2575 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002576 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002577 i = to_intel_crtc(c);
2578
2579 if (c == &intel_crtc->base)
2580 continue;
2581
Matt Roper2ff8fde2014-07-08 07:50:07 -07002582 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002583 continue;
2584
Daniel Vetter88595ac2015-03-26 12:42:24 +01002585 fb = c->primary->fb;
2586 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002587 continue;
2588
Daniel Vetter88595ac2015-03-26 12:42:24 +01002589 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002590 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002591 drm_framebuffer_reference(fb);
2592 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002593 }
2594 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002595
Matt Roper200757f2015-12-03 11:37:36 -08002596 /*
2597 * We've failed to reconstruct the BIOS FB. Current display state
2598 * indicates that the primary plane is visible, but has a NULL FB,
2599 * which will lead to problems later if we don't fix it up. The
2600 * simplest solution is to just disable the primary plane now and
2601 * pretend the BIOS never had it enabled.
2602 */
2603 to_intel_plane_state(plane_state)->visible = false;
2604 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002605 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002606 intel_plane->disable_plane(primary, &intel_crtc->base);
2607
Daniel Vetter88595ac2015-03-26 12:42:24 +01002608 return;
2609
2610valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002611 plane_state->src_x = 0;
2612 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002613 plane_state->src_w = fb->width << 16;
2614 plane_state->src_h = fb->height << 16;
2615
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002616 plane_state->crtc_x = 0;
2617 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002618 plane_state->crtc_w = fb->width;
2619 plane_state->crtc_h = fb->height;
2620
Matt Roper0a8d8a82015-12-03 11:37:38 -08002621 intel_state->src.x1 = plane_state->src_x;
2622 intel_state->src.y1 = plane_state->src_y;
2623 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2624 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2625 intel_state->dst.x1 = plane_state->crtc_x;
2626 intel_state->dst.y1 = plane_state->crtc_y;
2627 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2628 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2629
Daniel Vetter88595ac2015-03-26 12:42:24 +01002630 obj = intel_fb_obj(fb);
2631 if (obj->tiling_mode != I915_TILING_NONE)
2632 dev_priv->preserve_bios_swizzle = true;
2633
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002634 drm_framebuffer_reference(fb);
2635 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002636 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002637 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002638 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002639}
2640
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002641static void i9xx_update_primary_plane(struct drm_plane *primary,
2642 const struct intel_crtc_state *crtc_state,
2643 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002644{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002645 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002646 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002647 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2648 struct drm_framebuffer *fb = plane_state->base.fb;
2649 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002650 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002651 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002652 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002653 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002654 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002655 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002656 int x = plane_state->src.x1 >> 16;
2657 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002658
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002659 dspcntr = DISPPLANE_GAMMA_ENABLE;
2660
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002661 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002662
2663 if (INTEL_INFO(dev)->gen < 4) {
2664 if (intel_crtc->pipe == PIPE_B)
2665 dspcntr |= DISPPLANE_SEL_PIPE_B;
2666
2667 /* pipesrc and dspsize control the size that is scaled from,
2668 * which should always be the user's requested size.
2669 */
2670 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002671 ((crtc_state->pipe_src_h - 1) << 16) |
2672 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002673 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002674 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2675 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002676 ((crtc_state->pipe_src_h - 1) << 16) |
2677 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002678 I915_WRITE(PRIMPOS(plane), 0);
2679 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002680 }
2681
Ville Syrjälä57779d02012-10-31 17:50:14 +02002682 switch (fb->pixel_format) {
2683 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002684 dspcntr |= DISPPLANE_8BPP;
2685 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002686 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002687 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002688 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002689 case DRM_FORMAT_RGB565:
2690 dspcntr |= DISPPLANE_BGRX565;
2691 break;
2692 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002693 dspcntr |= DISPPLANE_BGRX888;
2694 break;
2695 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002696 dspcntr |= DISPPLANE_RGBX888;
2697 break;
2698 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002699 dspcntr |= DISPPLANE_BGRX101010;
2700 break;
2701 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002702 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002703 break;
2704 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002705 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002706 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002707
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002708 if (INTEL_INFO(dev)->gen >= 4 &&
2709 obj->tiling_mode != I915_TILING_NONE)
2710 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002711
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002712 if (IS_G4X(dev))
2713 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2714
Ville Syrjäläac484962016-01-20 21:05:26 +02002715 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002716
Daniel Vetterc2c75132012-07-05 12:17:30 +02002717 if (INTEL_INFO(dev)->gen >= 4) {
2718 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002719 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002720 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002721 linear_offset -= intel_crtc->dspaddr_offset;
2722 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002723 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002724 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002725
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002726 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302727 dspcntr |= DISPPLANE_ROTATE_180;
2728
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002729 x += (crtc_state->pipe_src_w - 1);
2730 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302731
2732 /* Finding the last pixel of the last line of the display
2733 data and adding to linear_offset*/
2734 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002735 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002736 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302737 }
2738
Paulo Zanoni2db33662015-09-14 15:20:03 -03002739 intel_crtc->adjusted_x = x;
2740 intel_crtc->adjusted_y = y;
2741
Sonika Jindal48404c12014-08-22 14:06:04 +05302742 I915_WRITE(reg, dspcntr);
2743
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002744 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002745 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002746 I915_WRITE(DSPSURF(plane),
2747 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002748 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002749 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002750 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002751 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002752 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002753}
2754
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002755static void i9xx_disable_primary_plane(struct drm_plane *primary,
2756 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002757{
2758 struct drm_device *dev = crtc->dev;
2759 struct drm_i915_private *dev_priv = dev->dev_private;
2760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002761 int plane = intel_crtc->plane;
2762
2763 I915_WRITE(DSPCNTR(plane), 0);
2764 if (INTEL_INFO(dev_priv)->gen >= 4)
2765 I915_WRITE(DSPSURF(plane), 0);
2766 else
2767 I915_WRITE(DSPADDR(plane), 0);
2768 POSTING_READ(DSPCNTR(plane));
2769}
2770
2771static void ironlake_update_primary_plane(struct drm_plane *primary,
2772 const struct intel_crtc_state *crtc_state,
2773 const struct intel_plane_state *plane_state)
2774{
2775 struct drm_device *dev = primary->dev;
2776 struct drm_i915_private *dev_priv = dev->dev_private;
2777 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2778 struct drm_framebuffer *fb = plane_state->base.fb;
2779 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002780 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002781 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002782 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002783 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002784 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002785 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002786 int x = plane_state->src.x1 >> 16;
2787 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002788
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002789 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002790 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002791
2792 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2793 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2794
Ville Syrjälä57779d02012-10-31 17:50:14 +02002795 switch (fb->pixel_format) {
2796 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002797 dspcntr |= DISPPLANE_8BPP;
2798 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002799 case DRM_FORMAT_RGB565:
2800 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002801 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002802 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002803 dspcntr |= DISPPLANE_BGRX888;
2804 break;
2805 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002806 dspcntr |= DISPPLANE_RGBX888;
2807 break;
2808 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002809 dspcntr |= DISPPLANE_BGRX101010;
2810 break;
2811 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002812 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002813 break;
2814 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002815 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002816 }
2817
2818 if (obj->tiling_mode != I915_TILING_NONE)
2819 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002820
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002821 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002822 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002823
Ville Syrjäläac484962016-01-20 21:05:26 +02002824 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002825 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002826 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002827 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002828 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002829 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302830 dspcntr |= DISPPLANE_ROTATE_180;
2831
2832 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002833 x += (crtc_state->pipe_src_w - 1);
2834 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302835
2836 /* Finding the last pixel of the last line of the display
2837 data and adding to linear_offset*/
2838 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002839 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002840 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302841 }
2842 }
2843
Paulo Zanoni2db33662015-09-14 15:20:03 -03002844 intel_crtc->adjusted_x = x;
2845 intel_crtc->adjusted_y = y;
2846
Sonika Jindal48404c12014-08-22 14:06:04 +05302847 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002848
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002849 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002850 I915_WRITE(DSPSURF(plane),
2851 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002852 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002853 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2854 } else {
2855 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2856 I915_WRITE(DSPLINOFF(plane), linear_offset);
2857 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002858 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002859}
2860
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002861u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2862 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002863{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002864 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2865 return 64;
2866 } else {
2867 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002868
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002869 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002870 }
2871}
2872
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002873u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2874 struct drm_i915_gem_object *obj,
2875 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002876{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002877 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002878 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002879 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002880
Ville Syrjäläe7941292016-01-19 18:23:17 +02002881 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002882 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002883
Daniel Vetterce7f1722015-10-14 16:51:06 +02002884 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002885 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002886 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002887 return -1;
2888
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002889 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002890
2891 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002892 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002893 PAGE_SIZE;
2894 }
2895
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002896 WARN_ON(upper_32_bits(offset));
2897
2898 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002899}
2900
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002901static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2902{
2903 struct drm_device *dev = intel_crtc->base.dev;
2904 struct drm_i915_private *dev_priv = dev->dev_private;
2905
2906 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2907 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2908 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002909}
2910
Chandra Kondurua1b22782015-04-07 15:28:45 -07002911/*
2912 * This function detaches (aka. unbinds) unused scalers in hardware
2913 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002914static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002915{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002916 struct intel_crtc_scaler_state *scaler_state;
2917 int i;
2918
Chandra Kondurua1b22782015-04-07 15:28:45 -07002919 scaler_state = &intel_crtc->config->scaler_state;
2920
2921 /* loop through and disable scalers that aren't in use */
2922 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002923 if (!scaler_state->scalers[i].in_use)
2924 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002925 }
2926}
2927
Chandra Konduru6156a452015-04-27 13:48:39 -07002928u32 skl_plane_ctl_format(uint32_t pixel_format)
2929{
Chandra Konduru6156a452015-04-27 13:48:39 -07002930 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002931 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002932 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002933 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002934 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002935 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002936 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002937 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002938 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002939 /*
2940 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2941 * to be already pre-multiplied. We need to add a knob (or a different
2942 * DRM_FORMAT) for user-space to configure that.
2943 */
2944 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002945 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002946 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002947 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002948 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002949 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002951 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002952 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002953 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002955 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002956 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002957 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002959 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002960 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002961 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002962 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002963 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002964 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002965
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002966 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002967}
2968
2969u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2970{
Chandra Konduru6156a452015-04-27 13:48:39 -07002971 switch (fb_modifier) {
2972 case DRM_FORMAT_MOD_NONE:
2973 break;
2974 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002975 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002976 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002977 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002978 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002979 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002980 default:
2981 MISSING_CASE(fb_modifier);
2982 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002983
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002984 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002985}
2986
2987u32 skl_plane_ctl_rotation(unsigned int rotation)
2988{
Chandra Konduru6156a452015-04-27 13:48:39 -07002989 switch (rotation) {
2990 case BIT(DRM_ROTATE_0):
2991 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302992 /*
2993 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2994 * while i915 HW rotation is clockwise, thats why this swapping.
2995 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002996 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302997 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002998 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002999 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003000 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303001 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003002 default:
3003 MISSING_CASE(rotation);
3004 }
3005
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003006 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003007}
3008
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003009static void skylake_update_primary_plane(struct drm_plane *plane,
3010 const struct intel_crtc_state *crtc_state,
3011 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003012{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003013 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003014 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003015 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3016 struct drm_framebuffer *fb = plane_state->base.fb;
3017 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003018 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303019 u32 plane_ctl, stride_div, stride;
3020 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003021 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303022 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003023 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003024 int scaler_id = plane_state->scaler_id;
3025 int src_x = plane_state->src.x1 >> 16;
3026 int src_y = plane_state->src.y1 >> 16;
3027 int src_w = drm_rect_width(&plane_state->src) >> 16;
3028 int src_h = drm_rect_height(&plane_state->src) >> 16;
3029 int dst_x = plane_state->dst.x1;
3030 int dst_y = plane_state->dst.y1;
3031 int dst_w = drm_rect_width(&plane_state->dst);
3032 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003033
3034 plane_ctl = PLANE_CTL_ENABLE |
3035 PLANE_CTL_PIPE_GAMMA_ENABLE |
3036 PLANE_CTL_PIPE_CSC_ENABLE;
3037
Chandra Konduru6156a452015-04-27 13:48:39 -07003038 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3039 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003040 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003041 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003042
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003043 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003044 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003045 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303046
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003047 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003048
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303049 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003050 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3051
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303052 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003053 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303054 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003055 x_offset = stride * tile_height - src_y - src_h;
3056 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003057 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303058 } else {
3059 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003060 x_offset = src_x;
3061 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003062 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303063 }
3064 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003065
Paulo Zanoni2db33662015-09-14 15:20:03 -03003066 intel_crtc->adjusted_x = x_offset;
3067 intel_crtc->adjusted_y = y_offset;
3068
Damien Lespiau70d21f02013-07-03 21:06:04 +01003069 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303070 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3071 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3072 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003073
3074 if (scaler_id >= 0) {
3075 uint32_t ps_ctrl = 0;
3076
3077 WARN_ON(!dst_w || !dst_h);
3078 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3079 crtc_state->scaler_state.scalers[scaler_id].mode;
3080 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3081 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3082 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3083 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3084 I915_WRITE(PLANE_POS(pipe, 0), 0);
3085 } else {
3086 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3087 }
3088
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003089 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003090
3091 POSTING_READ(PLANE_SURF(pipe, 0));
3092}
3093
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003094static void skylake_disable_primary_plane(struct drm_plane *primary,
3095 struct drm_crtc *crtc)
3096{
3097 struct drm_device *dev = crtc->dev;
3098 struct drm_i915_private *dev_priv = dev->dev_private;
3099 int pipe = to_intel_crtc(crtc)->pipe;
3100
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003101 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3102 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3103 POSTING_READ(PLANE_SURF(pipe, 0));
3104}
3105
Jesse Barnes17638cd2011-06-24 12:19:23 -07003106/* Assume fb object is pinned & idle & fenced and just update base pointers */
3107static int
3108intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3109 int x, int y, enum mode_set_atomic state)
3110{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003111 /* Support for kgdboc is disabled, this needs a major rework. */
3112 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003113
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003114 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003115}
3116
Daniel Vetter5a21b662016-05-24 17:13:53 +02003117static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3118{
3119 struct intel_crtc *crtc;
3120
3121 for_each_intel_crtc(dev_priv->dev, crtc)
3122 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3123}
3124
Ville Syrjälä75147472014-11-24 18:28:11 +02003125static void intel_update_primary_planes(struct drm_device *dev)
3126{
Ville Syrjälä75147472014-11-24 18:28:11 +02003127 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003128
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003129 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003130 struct intel_plane *plane = to_intel_plane(crtc->primary);
3131 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003132
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003133 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003134 plane_state = to_intel_plane_state(plane->base.state);
3135
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003136 if (plane_state->visible)
3137 plane->update_plane(&plane->base,
3138 to_intel_crtc_state(crtc->state),
3139 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003140
3141 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003142 }
3143}
3144
Chris Wilsonc0336662016-05-06 15:40:21 +01003145void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003146{
3147 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003148 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003149 return;
3150
3151 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003152 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003153 return;
3154
Chris Wilsonc0336662016-05-06 15:40:21 +01003155 drm_modeset_lock_all(dev_priv->dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003156 /*
3157 * Disabling the crtcs gracefully seems nicer. Also the
3158 * g33 docs say we should at least disable all the planes.
3159 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003160 intel_display_suspend(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003161}
3162
Chris Wilsonc0336662016-05-06 15:40:21 +01003163void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003164{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003165 /*
3166 * Flips in the rings will be nuked by the reset,
3167 * so complete all pending flips so that user space
3168 * will get its events and not get stuck.
3169 */
3170 intel_complete_page_flips(dev_priv);
3171
Ville Syrjälä75147472014-11-24 18:28:11 +02003172 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003173 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003174 return;
3175
3176 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003177 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
Ville Syrjälä75147472014-11-24 18:28:11 +02003178 /*
3179 * Flips in the rings have been nuked by the reset,
3180 * so update the base address of all primary
3181 * planes to the the last fb to make sure we're
3182 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003183 *
3184 * FIXME: Atomic will make this obsolete since we won't schedule
3185 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003186 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003187 intel_update_primary_planes(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003188 return;
3189 }
3190
3191 /*
3192 * The display has been reset as well,
3193 * so need a full re-initialization.
3194 */
3195 intel_runtime_pm_disable_interrupts(dev_priv);
3196 intel_runtime_pm_enable_interrupts(dev_priv);
3197
Chris Wilsonc0336662016-05-06 15:40:21 +01003198 intel_modeset_init_hw(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003199
3200 spin_lock_irq(&dev_priv->irq_lock);
3201 if (dev_priv->display.hpd_irq_setup)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01003202 dev_priv->display.hpd_irq_setup(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003203 spin_unlock_irq(&dev_priv->irq_lock);
3204
Chris Wilsonc0336662016-05-06 15:40:21 +01003205 intel_display_resume(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003206
3207 intel_hpd_init(dev_priv);
3208
Chris Wilsonc0336662016-05-06 15:40:21 +01003209 drm_modeset_unlock_all(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003210}
3211
Chris Wilson7d5e3792014-03-04 13:15:08 +00003212static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3213{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003214 struct drm_device *dev = crtc->dev;
3215 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3216 unsigned reset_counter;
3217 bool pending;
3218
3219 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3220 if (intel_crtc->reset_counter != reset_counter)
3221 return false;
3222
3223 spin_lock_irq(&dev->event_lock);
3224 pending = to_intel_crtc(crtc)->flip_work != NULL;
3225 spin_unlock_irq(&dev->event_lock);
3226
3227 return pending;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003228}
3229
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003230static void intel_update_pipe_config(struct intel_crtc *crtc,
3231 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003232{
3233 struct drm_device *dev = crtc->base.dev;
3234 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003235 struct intel_crtc_state *pipe_config =
3236 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003237
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003238 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3239 crtc->base.mode = crtc->base.state->mode;
3240
3241 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3242 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3243 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003244
3245 /*
3246 * Update pipe size and adjust fitter if needed: the reason for this is
3247 * that in compute_mode_changes we check the native mode (not the pfit
3248 * mode) to see if we can flip rather than do a full mode set. In the
3249 * fastboot case, we'll flip, but if we don't update the pipesrc and
3250 * pfit state, we'll end up with a big fb scanned out into the wrong
3251 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003252 */
3253
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003254 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003255 ((pipe_config->pipe_src_w - 1) << 16) |
3256 (pipe_config->pipe_src_h - 1));
3257
3258 /* on skylake this is done by detaching scalers */
3259 if (INTEL_INFO(dev)->gen >= 9) {
3260 skl_detach_scalers(crtc);
3261
3262 if (pipe_config->pch_pfit.enabled)
3263 skylake_pfit_enable(crtc);
3264 } else if (HAS_PCH_SPLIT(dev)) {
3265 if (pipe_config->pch_pfit.enabled)
3266 ironlake_pfit_enable(crtc);
3267 else if (old_crtc_state->pch_pfit.enabled)
3268 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003269 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003270}
3271
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003272static void intel_fdi_normal_train(struct drm_crtc *crtc)
3273{
3274 struct drm_device *dev = crtc->dev;
3275 struct drm_i915_private *dev_priv = dev->dev_private;
3276 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3277 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003278 i915_reg_t reg;
3279 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003280
3281 /* enable normal train */
3282 reg = FDI_TX_CTL(pipe);
3283 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003284 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003285 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3286 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003287 } else {
3288 temp &= ~FDI_LINK_TRAIN_NONE;
3289 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003290 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003291 I915_WRITE(reg, temp);
3292
3293 reg = FDI_RX_CTL(pipe);
3294 temp = I915_READ(reg);
3295 if (HAS_PCH_CPT(dev)) {
3296 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3297 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3298 } else {
3299 temp &= ~FDI_LINK_TRAIN_NONE;
3300 temp |= FDI_LINK_TRAIN_NONE;
3301 }
3302 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3303
3304 /* wait one idle pattern time */
3305 POSTING_READ(reg);
3306 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003307
3308 /* IVB wants error correction enabled */
3309 if (IS_IVYBRIDGE(dev))
3310 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3311 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003312}
3313
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003314/* The FDI link training functions for ILK/Ibexpeak. */
3315static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3316{
3317 struct drm_device *dev = crtc->dev;
3318 struct drm_i915_private *dev_priv = dev->dev_private;
3319 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3320 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003321 i915_reg_t reg;
3322 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003323
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003324 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003325 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003326
Adam Jacksone1a44742010-06-25 15:32:14 -04003327 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3328 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003329 reg = FDI_RX_IMR(pipe);
3330 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003331 temp &= ~FDI_RX_SYMBOL_LOCK;
3332 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003333 I915_WRITE(reg, temp);
3334 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003335 udelay(150);
3336
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003337 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003338 reg = FDI_TX_CTL(pipe);
3339 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003340 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003341 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003342 temp &= ~FDI_LINK_TRAIN_NONE;
3343 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003344 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003345
Chris Wilson5eddb702010-09-11 13:48:45 +01003346 reg = FDI_RX_CTL(pipe);
3347 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003348 temp &= ~FDI_LINK_TRAIN_NONE;
3349 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003350 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3351
3352 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003353 udelay(150);
3354
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003355 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003356 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3357 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3358 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003359
Chris Wilson5eddb702010-09-11 13:48:45 +01003360 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003361 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003362 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003363 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3364
3365 if ((temp & FDI_RX_BIT_LOCK)) {
3366 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003367 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003368 break;
3369 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003370 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003371 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003372 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003373
3374 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003375 reg = FDI_TX_CTL(pipe);
3376 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003377 temp &= ~FDI_LINK_TRAIN_NONE;
3378 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003379 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003380
Chris Wilson5eddb702010-09-11 13:48:45 +01003381 reg = FDI_RX_CTL(pipe);
3382 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003383 temp &= ~FDI_LINK_TRAIN_NONE;
3384 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003385 I915_WRITE(reg, temp);
3386
3387 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003388 udelay(150);
3389
Chris Wilson5eddb702010-09-11 13:48:45 +01003390 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003391 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003392 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003393 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3394
3395 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003396 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003397 DRM_DEBUG_KMS("FDI train 2 done.\n");
3398 break;
3399 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003400 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003401 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003402 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003403
3404 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003405
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003406}
3407
Akshay Joshi0206e352011-08-16 15:34:10 -04003408static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003409 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3410 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3411 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3412 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3413};
3414
3415/* The FDI link training functions for SNB/Cougarpoint. */
3416static void gen6_fdi_link_train(struct drm_crtc *crtc)
3417{
3418 struct drm_device *dev = crtc->dev;
3419 struct drm_i915_private *dev_priv = dev->dev_private;
3420 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3421 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003422 i915_reg_t reg;
3423 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003424
Adam Jacksone1a44742010-06-25 15:32:14 -04003425 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3426 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003427 reg = FDI_RX_IMR(pipe);
3428 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003429 temp &= ~FDI_RX_SYMBOL_LOCK;
3430 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003431 I915_WRITE(reg, temp);
3432
3433 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003434 udelay(150);
3435
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003436 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003437 reg = FDI_TX_CTL(pipe);
3438 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003439 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003440 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003441 temp &= ~FDI_LINK_TRAIN_NONE;
3442 temp |= FDI_LINK_TRAIN_PATTERN_1;
3443 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3444 /* SNB-B */
3445 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003446 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003447
Daniel Vetterd74cf322012-10-26 10:58:13 +02003448 I915_WRITE(FDI_RX_MISC(pipe),
3449 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3450
Chris Wilson5eddb702010-09-11 13:48:45 +01003451 reg = FDI_RX_CTL(pipe);
3452 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003453 if (HAS_PCH_CPT(dev)) {
3454 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3455 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3456 } else {
3457 temp &= ~FDI_LINK_TRAIN_NONE;
3458 temp |= FDI_LINK_TRAIN_PATTERN_1;
3459 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003460 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3461
3462 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003463 udelay(150);
3464
Akshay Joshi0206e352011-08-16 15:34:10 -04003465 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003466 reg = FDI_TX_CTL(pipe);
3467 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003468 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3469 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003470 I915_WRITE(reg, temp);
3471
3472 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003473 udelay(500);
3474
Sean Paulfa37d392012-03-02 12:53:39 -05003475 for (retry = 0; retry < 5; retry++) {
3476 reg = FDI_RX_IIR(pipe);
3477 temp = I915_READ(reg);
3478 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3479 if (temp & FDI_RX_BIT_LOCK) {
3480 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3481 DRM_DEBUG_KMS("FDI train 1 done.\n");
3482 break;
3483 }
3484 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003485 }
Sean Paulfa37d392012-03-02 12:53:39 -05003486 if (retry < 5)
3487 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003488 }
3489 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003490 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003491
3492 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003493 reg = FDI_TX_CTL(pipe);
3494 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003495 temp &= ~FDI_LINK_TRAIN_NONE;
3496 temp |= FDI_LINK_TRAIN_PATTERN_2;
3497 if (IS_GEN6(dev)) {
3498 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3499 /* SNB-B */
3500 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3501 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003502 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003503
Chris Wilson5eddb702010-09-11 13:48:45 +01003504 reg = FDI_RX_CTL(pipe);
3505 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003506 if (HAS_PCH_CPT(dev)) {
3507 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3508 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3509 } else {
3510 temp &= ~FDI_LINK_TRAIN_NONE;
3511 temp |= FDI_LINK_TRAIN_PATTERN_2;
3512 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003513 I915_WRITE(reg, temp);
3514
3515 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003516 udelay(150);
3517
Akshay Joshi0206e352011-08-16 15:34:10 -04003518 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003519 reg = FDI_TX_CTL(pipe);
3520 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003521 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3522 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003523 I915_WRITE(reg, temp);
3524
3525 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003526 udelay(500);
3527
Sean Paulfa37d392012-03-02 12:53:39 -05003528 for (retry = 0; retry < 5; retry++) {
3529 reg = FDI_RX_IIR(pipe);
3530 temp = I915_READ(reg);
3531 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3532 if (temp & FDI_RX_SYMBOL_LOCK) {
3533 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3534 DRM_DEBUG_KMS("FDI train 2 done.\n");
3535 break;
3536 }
3537 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003538 }
Sean Paulfa37d392012-03-02 12:53:39 -05003539 if (retry < 5)
3540 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003541 }
3542 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003543 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003544
3545 DRM_DEBUG_KMS("FDI train done.\n");
3546}
3547
Jesse Barnes357555c2011-04-28 15:09:55 -07003548/* Manual link training for Ivy Bridge A0 parts */
3549static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3550{
3551 struct drm_device *dev = crtc->dev;
3552 struct drm_i915_private *dev_priv = dev->dev_private;
3553 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3554 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003555 i915_reg_t reg;
3556 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003557
3558 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3559 for train result */
3560 reg = FDI_RX_IMR(pipe);
3561 temp = I915_READ(reg);
3562 temp &= ~FDI_RX_SYMBOL_LOCK;
3563 temp &= ~FDI_RX_BIT_LOCK;
3564 I915_WRITE(reg, temp);
3565
3566 POSTING_READ(reg);
3567 udelay(150);
3568
Daniel Vetter01a415f2012-10-27 15:58:40 +02003569 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3570 I915_READ(FDI_RX_IIR(pipe)));
3571
Jesse Barnes139ccd32013-08-19 11:04:55 -07003572 /* Try each vswing and preemphasis setting twice before moving on */
3573 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3574 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003575 reg = FDI_TX_CTL(pipe);
3576 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003577 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3578 temp &= ~FDI_TX_ENABLE;
3579 I915_WRITE(reg, temp);
3580
3581 reg = FDI_RX_CTL(pipe);
3582 temp = I915_READ(reg);
3583 temp &= ~FDI_LINK_TRAIN_AUTO;
3584 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3585 temp &= ~FDI_RX_ENABLE;
3586 I915_WRITE(reg, temp);
3587
3588 /* enable CPU FDI TX and PCH FDI RX */
3589 reg = FDI_TX_CTL(pipe);
3590 temp = I915_READ(reg);
3591 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003592 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003593 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003594 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003595 temp |= snb_b_fdi_train_param[j/2];
3596 temp |= FDI_COMPOSITE_SYNC;
3597 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3598
3599 I915_WRITE(FDI_RX_MISC(pipe),
3600 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3601
3602 reg = FDI_RX_CTL(pipe);
3603 temp = I915_READ(reg);
3604 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3605 temp |= FDI_COMPOSITE_SYNC;
3606 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3607
3608 POSTING_READ(reg);
3609 udelay(1); /* should be 0.5us */
3610
3611 for (i = 0; i < 4; i++) {
3612 reg = FDI_RX_IIR(pipe);
3613 temp = I915_READ(reg);
3614 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3615
3616 if (temp & FDI_RX_BIT_LOCK ||
3617 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3618 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3619 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3620 i);
3621 break;
3622 }
3623 udelay(1); /* should be 0.5us */
3624 }
3625 if (i == 4) {
3626 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3627 continue;
3628 }
3629
3630 /* Train 2 */
3631 reg = FDI_TX_CTL(pipe);
3632 temp = I915_READ(reg);
3633 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3634 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3635 I915_WRITE(reg, temp);
3636
3637 reg = FDI_RX_CTL(pipe);
3638 temp = I915_READ(reg);
3639 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3640 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003641 I915_WRITE(reg, temp);
3642
3643 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003644 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003645
Jesse Barnes139ccd32013-08-19 11:04:55 -07003646 for (i = 0; i < 4; i++) {
3647 reg = FDI_RX_IIR(pipe);
3648 temp = I915_READ(reg);
3649 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003650
Jesse Barnes139ccd32013-08-19 11:04:55 -07003651 if (temp & FDI_RX_SYMBOL_LOCK ||
3652 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3653 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3654 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3655 i);
3656 goto train_done;
3657 }
3658 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003659 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003660 if (i == 4)
3661 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003662 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003663
Jesse Barnes139ccd32013-08-19 11:04:55 -07003664train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003665 DRM_DEBUG_KMS("FDI train done.\n");
3666}
3667
Daniel Vetter88cefb62012-08-12 19:27:14 +02003668static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003669{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003670 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003671 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003672 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003673 i915_reg_t reg;
3674 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003675
Jesse Barnes0e23b992010-09-10 11:10:00 -07003676 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003677 reg = FDI_RX_CTL(pipe);
3678 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003679 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003680 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003681 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003682 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3683
3684 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003685 udelay(200);
3686
3687 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003688 temp = I915_READ(reg);
3689 I915_WRITE(reg, temp | FDI_PCDCLK);
3690
3691 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003692 udelay(200);
3693
Paulo Zanoni20749732012-11-23 15:30:38 -02003694 /* Enable CPU FDI TX PLL, always on for Ironlake */
3695 reg = FDI_TX_CTL(pipe);
3696 temp = I915_READ(reg);
3697 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3698 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003699
Paulo Zanoni20749732012-11-23 15:30:38 -02003700 POSTING_READ(reg);
3701 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003702 }
3703}
3704
Daniel Vetter88cefb62012-08-12 19:27:14 +02003705static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3706{
3707 struct drm_device *dev = intel_crtc->base.dev;
3708 struct drm_i915_private *dev_priv = dev->dev_private;
3709 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003710 i915_reg_t reg;
3711 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003712
3713 /* Switch from PCDclk to Rawclk */
3714 reg = FDI_RX_CTL(pipe);
3715 temp = I915_READ(reg);
3716 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3717
3718 /* Disable CPU FDI TX PLL */
3719 reg = FDI_TX_CTL(pipe);
3720 temp = I915_READ(reg);
3721 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3722
3723 POSTING_READ(reg);
3724 udelay(100);
3725
3726 reg = FDI_RX_CTL(pipe);
3727 temp = I915_READ(reg);
3728 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3729
3730 /* Wait for the clocks to turn off. */
3731 POSTING_READ(reg);
3732 udelay(100);
3733}
3734
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003735static void ironlake_fdi_disable(struct drm_crtc *crtc)
3736{
3737 struct drm_device *dev = crtc->dev;
3738 struct drm_i915_private *dev_priv = dev->dev_private;
3739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3740 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003741 i915_reg_t reg;
3742 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003743
3744 /* disable CPU FDI tx and PCH FDI rx */
3745 reg = FDI_TX_CTL(pipe);
3746 temp = I915_READ(reg);
3747 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3748 POSTING_READ(reg);
3749
3750 reg = FDI_RX_CTL(pipe);
3751 temp = I915_READ(reg);
3752 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003753 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003754 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3755
3756 POSTING_READ(reg);
3757 udelay(100);
3758
3759 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003760 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003761 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003762
3763 /* still set train pattern 1 */
3764 reg = FDI_TX_CTL(pipe);
3765 temp = I915_READ(reg);
3766 temp &= ~FDI_LINK_TRAIN_NONE;
3767 temp |= FDI_LINK_TRAIN_PATTERN_1;
3768 I915_WRITE(reg, temp);
3769
3770 reg = FDI_RX_CTL(pipe);
3771 temp = I915_READ(reg);
3772 if (HAS_PCH_CPT(dev)) {
3773 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3774 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3775 } else {
3776 temp &= ~FDI_LINK_TRAIN_NONE;
3777 temp |= FDI_LINK_TRAIN_PATTERN_1;
3778 }
3779 /* BPC in FDI rx is consistent with that in PIPECONF */
3780 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003781 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003782 I915_WRITE(reg, temp);
3783
3784 POSTING_READ(reg);
3785 udelay(100);
3786}
3787
Chris Wilson5dce5b932014-01-20 10:17:36 +00003788bool intel_has_pending_fb_unpin(struct drm_device *dev)
3789{
3790 struct intel_crtc *crtc;
3791
3792 /* Note that we don't need to be called with mode_config.lock here
3793 * as our list of CRTC objects is static for the lifetime of the
3794 * device and so cannot disappear as we iterate. Similarly, we can
3795 * happily treat the predicates as racy, atomic checks as userspace
3796 * cannot claim and pin a new fb without at least acquring the
3797 * struct_mutex and so serialising with us.
3798 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003799 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003800 if (atomic_read(&crtc->unpin_work_count) == 0)
3801 continue;
3802
Daniel Vetter5a21b662016-05-24 17:13:53 +02003803 if (crtc->flip_work)
Chris Wilson5dce5b932014-01-20 10:17:36 +00003804 intel_wait_for_vblank(dev, crtc->pipe);
3805
3806 return true;
3807 }
3808
3809 return false;
3810}
3811
Daniel Vetter5a21b662016-05-24 17:13:53 +02003812static void page_flip_completed(struct intel_crtc *intel_crtc)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003813{
3814 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003815 struct intel_flip_work *work = intel_crtc->flip_work;
3816
3817 intel_crtc->flip_work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003818
3819 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07003820 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003821
3822 drm_crtc_vblank_put(&intel_crtc->base);
3823
Daniel Vetter5a21b662016-05-24 17:13:53 +02003824 wake_up_all(&dev_priv->pending_flip_queue);
Maarten Lankhorst143f73b2016-05-17 15:07:54 +02003825 queue_work(dev_priv->wq, &work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003826
3827 trace_i915_flip_complete(intel_crtc->plane,
3828 work->pending_flip_obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003829}
3830
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003831static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003832{
Chris Wilson0f911282012-04-17 10:05:38 +01003833 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003834 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003835 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003836
Daniel Vetter2c10d572012-12-20 21:24:07 +01003837 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003838
3839 ret = wait_event_interruptible_timeout(
3840 dev_priv->pending_flip_queue,
3841 !intel_crtc_has_pending_flip(crtc),
3842 60*HZ);
3843
3844 if (ret < 0)
3845 return ret;
3846
Daniel Vetter5a21b662016-05-24 17:13:53 +02003847 if (ret == 0) {
3848 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3849 struct intel_flip_work *work;
3850
3851 spin_lock_irq(&dev->event_lock);
3852 work = intel_crtc->flip_work;
3853 if (work && !is_mmio_work(work)) {
3854 WARN_ONCE(1, "Removing stuck page flip\n");
3855 page_flip_completed(intel_crtc);
3856 }
3857 spin_unlock_irq(&dev->event_lock);
3858 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003859
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003860 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003861}
3862
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003863static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3864{
3865 u32 temp;
3866
3867 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3868
3869 mutex_lock(&dev_priv->sb_lock);
3870
3871 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3872 temp |= SBI_SSCCTL_DISABLE;
3873 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3874
3875 mutex_unlock(&dev_priv->sb_lock);
3876}
3877
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003878/* Program iCLKIP clock to the desired frequency */
3879static void lpt_program_iclkip(struct drm_crtc *crtc)
3880{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003881 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003882 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003883 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3884 u32 temp;
3885
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003886 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003887
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003888 /* The iCLK virtual clock root frequency is in MHz,
3889 * but the adjusted_mode->crtc_clock in in KHz. To get the
3890 * divisors, it is necessary to divide one by another, so we
3891 * convert the virtual clock precision to KHz here for higher
3892 * precision.
3893 */
3894 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003895 u32 iclk_virtual_root_freq = 172800 * 1000;
3896 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003897 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003898
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003899 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3900 clock << auxdiv);
3901 divsel = (desired_divisor / iclk_pi_range) - 2;
3902 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003903
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003904 /*
3905 * Near 20MHz is a corner case which is
3906 * out of range for the 7-bit divisor
3907 */
3908 if (divsel <= 0x7f)
3909 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003910 }
3911
3912 /* This should not happen with any sane values */
3913 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3914 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3915 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3916 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3917
3918 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 +03003919 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003920 auxdiv,
3921 divsel,
3922 phasedir,
3923 phaseinc);
3924
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003925 mutex_lock(&dev_priv->sb_lock);
3926
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003927 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003928 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003929 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3930 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3931 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3932 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3933 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3934 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003935 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003936
3937 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003938 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003939 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3940 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003941 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003942
3943 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003944 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003945 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003946 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003947
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003948 mutex_unlock(&dev_priv->sb_lock);
3949
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003950 /* Wait for initialization time */
3951 udelay(24);
3952
3953 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3954}
3955
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003956int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3957{
3958 u32 divsel, phaseinc, auxdiv;
3959 u32 iclk_virtual_root_freq = 172800 * 1000;
3960 u32 iclk_pi_range = 64;
3961 u32 desired_divisor;
3962 u32 temp;
3963
3964 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3965 return 0;
3966
3967 mutex_lock(&dev_priv->sb_lock);
3968
3969 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3970 if (temp & SBI_SSCCTL_DISABLE) {
3971 mutex_unlock(&dev_priv->sb_lock);
3972 return 0;
3973 }
3974
3975 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3976 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3977 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3978 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3979 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3980
3981 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3982 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3983 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3984
3985 mutex_unlock(&dev_priv->sb_lock);
3986
3987 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3988
3989 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3990 desired_divisor << auxdiv);
3991}
3992
Daniel Vetter275f01b22013-05-03 11:49:47 +02003993static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3994 enum pipe pch_transcoder)
3995{
3996 struct drm_device *dev = crtc->base.dev;
3997 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003998 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003999
4000 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4001 I915_READ(HTOTAL(cpu_transcoder)));
4002 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4003 I915_READ(HBLANK(cpu_transcoder)));
4004 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4005 I915_READ(HSYNC(cpu_transcoder)));
4006
4007 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4008 I915_READ(VTOTAL(cpu_transcoder)));
4009 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4010 I915_READ(VBLANK(cpu_transcoder)));
4011 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4012 I915_READ(VSYNC(cpu_transcoder)));
4013 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4014 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4015}
4016
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004017static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004018{
4019 struct drm_i915_private *dev_priv = dev->dev_private;
4020 uint32_t temp;
4021
4022 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004023 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004024 return;
4025
4026 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4027 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4028
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004029 temp &= ~FDI_BC_BIFURCATION_SELECT;
4030 if (enable)
4031 temp |= FDI_BC_BIFURCATION_SELECT;
4032
4033 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004034 I915_WRITE(SOUTH_CHICKEN1, temp);
4035 POSTING_READ(SOUTH_CHICKEN1);
4036}
4037
4038static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4039{
4040 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004041
4042 switch (intel_crtc->pipe) {
4043 case PIPE_A:
4044 break;
4045 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004046 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004047 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004048 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004049 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004050
4051 break;
4052 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004053 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004054
4055 break;
4056 default:
4057 BUG();
4058 }
4059}
4060
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004061/* Return which DP Port should be selected for Transcoder DP control */
4062static enum port
4063intel_trans_dp_port_sel(struct drm_crtc *crtc)
4064{
4065 struct drm_device *dev = crtc->dev;
4066 struct intel_encoder *encoder;
4067
4068 for_each_encoder_on_crtc(dev, crtc, encoder) {
4069 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4070 encoder->type == INTEL_OUTPUT_EDP)
4071 return enc_to_dig_port(&encoder->base)->port;
4072 }
4073
4074 return -1;
4075}
4076
Jesse Barnesf67a5592011-01-05 10:31:48 -08004077/*
4078 * Enable PCH resources required for PCH ports:
4079 * - PCH PLLs
4080 * - FDI training & RX/TX
4081 * - update transcoder timings
4082 * - DP transcoding bits
4083 * - transcoder
4084 */
4085static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004086{
4087 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004088 struct drm_i915_private *dev_priv = dev->dev_private;
4089 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4090 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004091 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004092
Daniel Vetterab9412b2013-05-03 11:49:46 +02004093 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004094
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004095 if (IS_IVYBRIDGE(dev))
4096 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4097
Daniel Vettercd986ab2012-10-26 10:58:12 +02004098 /* Write the TU size bits before fdi link training, so that error
4099 * detection works. */
4100 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4101 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4102
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004103 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004104 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004105
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004106 /* We need to program the right clock selection before writing the pixel
4107 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004108 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004109 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004110
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004111 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004112 temp |= TRANS_DPLL_ENABLE(pipe);
4113 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004114 if (intel_crtc->config->shared_dpll ==
4115 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004116 temp |= sel;
4117 else
4118 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004119 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004120 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004121
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004122 /* XXX: pch pll's can be enabled any time before we enable the PCH
4123 * transcoder, and we actually should do this to not upset any PCH
4124 * transcoder that already use the clock when we share it.
4125 *
4126 * Note that enable_shared_dpll tries to do the right thing, but
4127 * get_shared_dpll unconditionally resets the pll - we need that to have
4128 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004129 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004130
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004131 /* set transcoder timing, panel must allow it */
4132 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004133 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004134
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004135 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004136
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004137 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004138 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004139 const struct drm_display_mode *adjusted_mode =
4140 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004141 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004142 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004143 temp = I915_READ(reg);
4144 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004145 TRANS_DP_SYNC_MASK |
4146 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004147 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004148 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004149
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004150 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004151 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004152 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004153 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004154
4155 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004156 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004157 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004158 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004159 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004160 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004161 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004162 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004163 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004164 break;
4165 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004166 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004167 }
4168
Chris Wilson5eddb702010-09-11 13:48:45 +01004169 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004170 }
4171
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004172 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004173}
4174
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004175static void lpt_pch_enable(struct drm_crtc *crtc)
4176{
4177 struct drm_device *dev = crtc->dev;
4178 struct drm_i915_private *dev_priv = dev->dev_private;
4179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004180 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004181
Daniel Vetterab9412b2013-05-03 11:49:46 +02004182 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004183
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004184 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004185
Paulo Zanoni0540e482012-10-31 18:12:40 -02004186 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004187 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004188
Paulo Zanoni937bb612012-10-31 18:12:47 -02004189 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004190}
4191
Daniel Vettera1520312013-05-03 11:49:50 +02004192static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004193{
4194 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004195 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004196 u32 temp;
4197
4198 temp = I915_READ(dslreg);
4199 udelay(500);
4200 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004201 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004202 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004203 }
4204}
4205
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004206static int
4207skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4208 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4209 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004210{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004211 struct intel_crtc_scaler_state *scaler_state =
4212 &crtc_state->scaler_state;
4213 struct intel_crtc *intel_crtc =
4214 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004215 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004216
4217 need_scaling = intel_rotation_90_or_270(rotation) ?
4218 (src_h != dst_w || src_w != dst_h):
4219 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004220
4221 /*
4222 * if plane is being disabled or scaler is no more required or force detach
4223 * - free scaler binded to this plane/crtc
4224 * - in order to do this, update crtc->scaler_usage
4225 *
4226 * Here scaler state in crtc_state is set free so that
4227 * scaler can be assigned to other user. Actual register
4228 * update to free the scaler is done in plane/panel-fit programming.
4229 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4230 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004231 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004232 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004233 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004234 scaler_state->scalers[*scaler_id].in_use = 0;
4235
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004236 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4237 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4238 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004239 scaler_state->scaler_users);
4240 *scaler_id = -1;
4241 }
4242 return 0;
4243 }
4244
4245 /* range checks */
4246 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4247 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4248
4249 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4250 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004251 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004252 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004253 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004254 return -EINVAL;
4255 }
4256
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004257 /* mark this plane as a scaler user in crtc_state */
4258 scaler_state->scaler_users |= (1 << scaler_user);
4259 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4260 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4261 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4262 scaler_state->scaler_users);
4263
4264 return 0;
4265}
4266
4267/**
4268 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4269 *
4270 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004271 *
4272 * Return
4273 * 0 - scaler_usage updated successfully
4274 * error - requested scaling cannot be supported or other error condition
4275 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004276int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004277{
4278 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004279 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004280
4281 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4282 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4283
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004284 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004285 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004286 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004287 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004288}
4289
4290/**
4291 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4292 *
4293 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004294 * @plane_state: atomic plane state to update
4295 *
4296 * Return
4297 * 0 - scaler_usage updated successfully
4298 * error - requested scaling cannot be supported or other error condition
4299 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004300static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4301 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004302{
4303
4304 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004305 struct intel_plane *intel_plane =
4306 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004307 struct drm_framebuffer *fb = plane_state->base.fb;
4308 int ret;
4309
4310 bool force_detach = !fb || !plane_state->visible;
4311
4312 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4313 intel_plane->base.base.id, intel_crtc->pipe,
4314 drm_plane_index(&intel_plane->base));
4315
4316 ret = skl_update_scaler(crtc_state, force_detach,
4317 drm_plane_index(&intel_plane->base),
4318 &plane_state->scaler_id,
4319 plane_state->base.rotation,
4320 drm_rect_width(&plane_state->src) >> 16,
4321 drm_rect_height(&plane_state->src) >> 16,
4322 drm_rect_width(&plane_state->dst),
4323 drm_rect_height(&plane_state->dst));
4324
4325 if (ret || plane_state->scaler_id < 0)
4326 return ret;
4327
Chandra Kondurua1b22782015-04-07 15:28:45 -07004328 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004329 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004330 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004331 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004332 return -EINVAL;
4333 }
4334
4335 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004336 switch (fb->pixel_format) {
4337 case DRM_FORMAT_RGB565:
4338 case DRM_FORMAT_XBGR8888:
4339 case DRM_FORMAT_XRGB8888:
4340 case DRM_FORMAT_ABGR8888:
4341 case DRM_FORMAT_ARGB8888:
4342 case DRM_FORMAT_XRGB2101010:
4343 case DRM_FORMAT_XBGR2101010:
4344 case DRM_FORMAT_YUYV:
4345 case DRM_FORMAT_YVYU:
4346 case DRM_FORMAT_UYVY:
4347 case DRM_FORMAT_VYUY:
4348 break;
4349 default:
4350 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4351 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4352 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004353 }
4354
Chandra Kondurua1b22782015-04-07 15:28:45 -07004355 return 0;
4356}
4357
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004358static void skylake_scaler_disable(struct intel_crtc *crtc)
4359{
4360 int i;
4361
4362 for (i = 0; i < crtc->num_scalers; i++)
4363 skl_detach_scaler(crtc, i);
4364}
4365
4366static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004367{
4368 struct drm_device *dev = crtc->base.dev;
4369 struct drm_i915_private *dev_priv = dev->dev_private;
4370 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004371 struct intel_crtc_scaler_state *scaler_state =
4372 &crtc->config->scaler_state;
4373
4374 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4375
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004376 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004377 int id;
4378
4379 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4380 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4381 return;
4382 }
4383
4384 id = scaler_state->scaler_id;
4385 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4386 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4387 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4388 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4389
4390 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004391 }
4392}
4393
Jesse Barnesb074cec2013-04-25 12:55:02 -07004394static void ironlake_pfit_enable(struct intel_crtc *crtc)
4395{
4396 struct drm_device *dev = crtc->base.dev;
4397 struct drm_i915_private *dev_priv = dev->dev_private;
4398 int pipe = crtc->pipe;
4399
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004400 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004401 /* Force use of hard-coded filter coefficients
4402 * as some pre-programmed values are broken,
4403 * e.g. x201.
4404 */
4405 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4406 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4407 PF_PIPE_SEL_IVB(pipe));
4408 else
4409 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004410 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4411 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004412 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004413}
4414
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004415void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004416{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004417 struct drm_device *dev = crtc->base.dev;
4418 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004419
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004420 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004421 return;
4422
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004423 /*
4424 * We can only enable IPS after we enable a plane and wait for a vblank
4425 * This function is called from post_plane_update, which is run after
4426 * a vblank wait.
4427 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004428
Paulo Zanonid77e4532013-09-24 13:52:55 -03004429 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004430 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004431 mutex_lock(&dev_priv->rps.hw_lock);
4432 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4433 mutex_unlock(&dev_priv->rps.hw_lock);
4434 /* Quoting Art Runyan: "its not safe to expect any particular
4435 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004436 * mailbox." Moreover, the mailbox may return a bogus state,
4437 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004438 */
4439 } else {
4440 I915_WRITE(IPS_CTL, IPS_ENABLE);
4441 /* The bit only becomes 1 in the next vblank, so this wait here
4442 * is essentially intel_wait_for_vblank. If we don't have this
4443 * and don't wait for vblanks until the end of crtc_enable, then
4444 * the HW state readout code will complain that the expected
4445 * IPS_CTL value is not the one we read. */
4446 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4447 DRM_ERROR("Timed out waiting for IPS enable\n");
4448 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004449}
4450
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004451void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004452{
4453 struct drm_device *dev = crtc->base.dev;
4454 struct drm_i915_private *dev_priv = dev->dev_private;
4455
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004456 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004457 return;
4458
4459 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004460 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004461 mutex_lock(&dev_priv->rps.hw_lock);
4462 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4463 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004464 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4465 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4466 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004467 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004468 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004469 POSTING_READ(IPS_CTL);
4470 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004471
4472 /* We need to wait for a vblank before we can disable the plane. */
4473 intel_wait_for_vblank(dev, crtc->pipe);
4474}
4475
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004476static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004477{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004478 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004479 struct drm_device *dev = intel_crtc->base.dev;
4480 struct drm_i915_private *dev_priv = dev->dev_private;
4481
4482 mutex_lock(&dev->struct_mutex);
4483 dev_priv->mm.interruptible = false;
4484 (void) intel_overlay_switch_off(intel_crtc->overlay);
4485 dev_priv->mm.interruptible = true;
4486 mutex_unlock(&dev->struct_mutex);
4487 }
4488
4489 /* Let userspace switch the overlay on again. In most cases userspace
4490 * has to recompute where to put it anyway.
4491 */
4492}
4493
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004494/**
4495 * intel_post_enable_primary - Perform operations after enabling primary plane
4496 * @crtc: the CRTC whose primary plane was just enabled
4497 *
4498 * Performs potentially sleeping operations that must be done after the primary
4499 * plane is enabled, such as updating FBC and IPS. Note that this may be
4500 * called due to an explicit primary plane update, or due to an implicit
4501 * re-enable that is caused when a sprite plane is updated to no longer
4502 * completely hide the primary plane.
4503 */
4504static void
4505intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004506{
4507 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004508 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004509 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4510 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004511
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004512 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004513 * FIXME IPS should be fine as long as one plane is
4514 * enabled, but in practice it seems to have problems
4515 * when going from primary only to sprite only and vice
4516 * versa.
4517 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004518 hsw_enable_ips(intel_crtc);
4519
Daniel Vetterf99d7062014-06-19 16:01:59 +02004520 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004521 * Gen2 reports pipe underruns whenever all planes are disabled.
4522 * So don't enable underrun reporting before at least some planes
4523 * are enabled.
4524 * FIXME: Need to fix the logic to work when we turn off all planes
4525 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004526 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004527 if (IS_GEN2(dev))
4528 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4529
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004530 /* Underruns don't always raise interrupts, so check manually. */
4531 intel_check_cpu_fifo_underruns(dev_priv);
4532 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004533}
4534
Ville Syrjälä2622a082016-03-09 19:07:26 +02004535/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004536static void
4537intel_pre_disable_primary(struct drm_crtc *crtc)
4538{
4539 struct drm_device *dev = crtc->dev;
4540 struct drm_i915_private *dev_priv = dev->dev_private;
4541 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4542 int pipe = intel_crtc->pipe;
4543
4544 /*
4545 * Gen2 reports pipe underruns whenever all planes are disabled.
4546 * So diasble underrun reporting before all the planes get disabled.
4547 * FIXME: Need to fix the logic to work when we turn off all planes
4548 * but leave the pipe running.
4549 */
4550 if (IS_GEN2(dev))
4551 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4552
4553 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004554 * FIXME IPS should be fine as long as one plane is
4555 * enabled, but in practice it seems to have problems
4556 * when going from primary only to sprite only and vice
4557 * versa.
4558 */
4559 hsw_disable_ips(intel_crtc);
4560}
4561
4562/* FIXME get rid of this and use pre_plane_update */
4563static void
4564intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4565{
4566 struct drm_device *dev = crtc->dev;
4567 struct drm_i915_private *dev_priv = dev->dev_private;
4568 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4569 int pipe = intel_crtc->pipe;
4570
4571 intel_pre_disable_primary(crtc);
4572
4573 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004574 * Vblank time updates from the shadow to live plane control register
4575 * are blocked if the memory self-refresh mode is active at that
4576 * moment. So to make sure the plane gets truly disabled, disable
4577 * first the self-refresh mode. The self-refresh enable bit in turn
4578 * will be checked/applied by the HW only at the next frame start
4579 * event which is after the vblank start event, so we need to have a
4580 * wait-for-vblank between disabling the plane and the pipe.
4581 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004582 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004583 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004584 dev_priv->wm.vlv.cxsr = false;
4585 intel_wait_for_vblank(dev, pipe);
4586 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004587}
4588
Daniel Vetter5a21b662016-05-24 17:13:53 +02004589static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4590{
4591 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4592 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4593 struct intel_crtc_state *pipe_config =
4594 to_intel_crtc_state(crtc->base.state);
4595 struct drm_device *dev = crtc->base.dev;
4596 struct drm_plane *primary = crtc->base.primary;
4597 struct drm_plane_state *old_pri_state =
4598 drm_atomic_get_existing_plane_state(old_state, primary);
4599
4600 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
4601
4602 crtc->wm.cxsr_allowed = true;
4603
4604 if (pipe_config->update_wm_post && pipe_config->base.active)
4605 intel_update_watermarks(&crtc->base);
4606
4607 if (old_pri_state) {
4608 struct intel_plane_state *primary_state =
4609 to_intel_plane_state(primary->state);
4610 struct intel_plane_state *old_primary_state =
4611 to_intel_plane_state(old_pri_state);
4612
4613 intel_fbc_post_update(crtc);
4614
4615 if (primary_state->visible &&
4616 (needs_modeset(&pipe_config->base) ||
4617 !old_primary_state->visible))
4618 intel_post_enable_primary(&crtc->base);
4619 }
4620}
4621
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004622static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004623{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004624 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004625 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004626 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004627 struct intel_crtc_state *pipe_config =
4628 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004629 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4630 struct drm_plane *primary = crtc->base.primary;
4631 struct drm_plane_state *old_pri_state =
4632 drm_atomic_get_existing_plane_state(old_state, primary);
4633 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004634
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004635 if (old_pri_state) {
4636 struct intel_plane_state *primary_state =
4637 to_intel_plane_state(primary->state);
4638 struct intel_plane_state *old_primary_state =
4639 to_intel_plane_state(old_pri_state);
4640
Daniel Vetter5a21b662016-05-24 17:13:53 +02004641 intel_fbc_pre_update(crtc);
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004642
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004643 if (old_primary_state->visible &&
4644 (modeset || !primary_state->visible))
4645 intel_pre_disable_primary(&crtc->base);
4646 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004647
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004648 if (pipe_config->disable_cxsr) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004649 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004650
Ville Syrjälä2622a082016-03-09 19:07:26 +02004651 /*
4652 * Vblank time updates from the shadow to live plane control register
4653 * are blocked if the memory self-refresh mode is active at that
4654 * moment. So to make sure the plane gets truly disabled, disable
4655 * first the self-refresh mode. The self-refresh enable bit in turn
4656 * will be checked/applied by the HW only at the next frame start
4657 * event which is after the vblank start event, so we need to have a
4658 * wait-for-vblank between disabling the plane and the pipe.
4659 */
4660 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004661 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004662 dev_priv->wm.vlv.cxsr = false;
4663 intel_wait_for_vblank(dev, crtc->pipe);
4664 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004665 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004666
Matt Ropered4a6a72016-02-23 17:20:13 -08004667 /*
4668 * IVB workaround: must disable low power watermarks for at least
4669 * one frame before enabling scaling. LP watermarks can be re-enabled
4670 * when scaling is disabled.
4671 *
4672 * WaCxSRDisabledForSpriteScaling:ivb
4673 */
4674 if (pipe_config->disable_lp_wm) {
4675 ilk_disable_lp_wm(dev);
4676 intel_wait_for_vblank(dev, crtc->pipe);
4677 }
4678
4679 /*
4680 * If we're doing a modeset, we're done. No need to do any pre-vblank
4681 * watermark programming here.
4682 */
4683 if (needs_modeset(&pipe_config->base))
4684 return;
4685
4686 /*
4687 * For platforms that support atomic watermarks, program the
4688 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4689 * will be the intermediate values that are safe for both pre- and
4690 * post- vblank; when vblank happens, the 'active' values will be set
4691 * to the final 'target' values and we'll do this again to get the
4692 * optimal watermarks. For gen9+ platforms, the values we program here
4693 * will be the final target values which will get automatically latched
4694 * at vblank time; no further programming will be necessary.
4695 *
4696 * If a platform hasn't been transitioned to atomic watermarks yet,
4697 * we'll continue to update watermarks the old way, if flags tell
4698 * us to.
4699 */
4700 if (dev_priv->display.initial_watermarks != NULL)
4701 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004702 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004703 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004704}
4705
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004706static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004707{
4708 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004709 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004710 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004711 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004712
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004713 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004714
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004715 drm_for_each_plane_mask(p, dev, plane_mask)
4716 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004717
Daniel Vetterf99d7062014-06-19 16:01:59 +02004718 /*
4719 * FIXME: Once we grow proper nuclear flip support out of this we need
4720 * to compute the mask of flip planes precisely. For the time being
4721 * consider this a flip to a NULL plane.
4722 */
4723 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004724}
4725
Jesse Barnesf67a5592011-01-05 10:31:48 -08004726static void ironlake_crtc_enable(struct drm_crtc *crtc)
4727{
4728 struct drm_device *dev = crtc->dev;
4729 struct drm_i915_private *dev_priv = dev->dev_private;
4730 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004731 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004732 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004733 struct intel_crtc_state *pipe_config =
4734 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004735
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004736 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004737 return;
4738
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004739 /*
4740 * Sometimes spurious CPU pipe underruns happen during FDI
4741 * training, at least with VGA+HDMI cloning. Suppress them.
4742 *
4743 * On ILK we get an occasional spurious CPU pipe underruns
4744 * between eDP port A enable and vdd enable. Also PCH port
4745 * enable seems to result in the occasional CPU pipe underrun.
4746 *
4747 * Spurious PCH underruns also occur during PCH enabling.
4748 */
4749 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4750 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004751 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004752 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4753
4754 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004755 intel_prepare_shared_dpll(intel_crtc);
4756
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004757 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304758 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004759
4760 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004761 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004762
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004763 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004764 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004765 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004766 }
4767
4768 ironlake_set_pipeconf(crtc);
4769
Jesse Barnesf67a5592011-01-05 10:31:48 -08004770 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004771
Daniel Vetterf6736a12013-06-05 13:34:30 +02004772 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004773 if (encoder->pre_enable)
4774 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004775
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004776 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004777 /* Note: FDI PLL enabling _must_ be done before we enable the
4778 * cpu pipes, hence this is separate from all the other fdi/pch
4779 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004780 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004781 } else {
4782 assert_fdi_tx_disabled(dev_priv, pipe);
4783 assert_fdi_rx_disabled(dev_priv, pipe);
4784 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004785
Jesse Barnesb074cec2013-04-25 12:55:02 -07004786 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004787
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004788 /*
4789 * On ILK+ LUT must be loaded before the pipe is running but with
4790 * clocks enabled
4791 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004792 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004793
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004794 if (dev_priv->display.initial_watermarks != NULL)
4795 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004796 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004797
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004798 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004799 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004800
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004801 assert_vblank_disabled(crtc);
4802 drm_crtc_vblank_on(crtc);
4803
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004804 for_each_encoder_on_crtc(dev, crtc, encoder)
4805 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004806
4807 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004808 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004809
4810 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4811 if (intel_crtc->config->has_pch_encoder)
4812 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004813 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004814 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004815}
4816
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004817/* IPS only exists on ULT machines and is tied to pipe A. */
4818static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4819{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004820 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004821}
4822
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004823static void haswell_crtc_enable(struct drm_crtc *crtc)
4824{
4825 struct drm_device *dev = crtc->dev;
4826 struct drm_i915_private *dev_priv = dev->dev_private;
4827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4828 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004829 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004830 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004831 struct intel_crtc_state *pipe_config =
4832 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004833
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004834 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004835 return;
4836
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004837 if (intel_crtc->config->has_pch_encoder)
4838 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4839 false);
4840
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004841 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004842 intel_enable_shared_dpll(intel_crtc);
4843
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004844 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304845 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004846
Jani Nikula4d1de972016-03-18 17:05:42 +02004847 if (!intel_crtc->config->has_dsi_encoder)
4848 intel_set_pipe_timings(intel_crtc);
4849
Jani Nikulabc58be62016-03-18 17:05:39 +02004850 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004851
Jani Nikula4d1de972016-03-18 17:05:42 +02004852 if (cpu_transcoder != TRANSCODER_EDP &&
4853 !transcoder_is_dsi(cpu_transcoder)) {
4854 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004855 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004856 }
4857
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004858 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004859 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004860 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004861 }
4862
Jani Nikula4d1de972016-03-18 17:05:42 +02004863 if (!intel_crtc->config->has_dsi_encoder)
4864 haswell_set_pipeconf(crtc);
4865
Jani Nikula391bf042016-03-18 17:05:40 +02004866 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004867
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004868 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02004869
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004870 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004871
Daniel Vetter6b698512015-11-28 11:05:39 +01004872 if (intel_crtc->config->has_pch_encoder)
4873 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4874 else
4875 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4876
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304877 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004878 if (encoder->pre_enable)
4879 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304880 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004881
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004882 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004883 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004884
Jani Nikulaa65347b2015-11-27 12:21:46 +02004885 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304886 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004887
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004888 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004889 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004890 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004891 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004892
4893 /*
4894 * On ILK+ LUT must be loaded before the pipe is running but with
4895 * clocks enabled
4896 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004897 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004898
Paulo Zanoni1f544382012-10-24 11:32:00 -02004899 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004900 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304901 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004902
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004903 if (dev_priv->display.initial_watermarks != NULL)
4904 dev_priv->display.initial_watermarks(pipe_config);
4905 else
4906 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004907
4908 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4909 if (!intel_crtc->config->has_dsi_encoder)
4910 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004911
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004912 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004913 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004914
Jani Nikulaa65347b2015-11-27 12:21:46 +02004915 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004916 intel_ddi_set_vc_payload_alloc(crtc, true);
4917
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004918 assert_vblank_disabled(crtc);
4919 drm_crtc_vblank_on(crtc);
4920
Jani Nikula8807e552013-08-30 19:40:32 +03004921 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004922 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004923 intel_opregion_notify_encoder(encoder, true);
4924 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004925
Daniel Vetter6b698512015-11-28 11:05:39 +01004926 if (intel_crtc->config->has_pch_encoder) {
4927 intel_wait_for_vblank(dev, pipe);
4928 intel_wait_for_vblank(dev, pipe);
4929 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004930 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4931 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004932 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004933
Paulo Zanonie4916942013-09-20 16:21:19 -03004934 /* If we change the relative order between pipe/planes enabling, we need
4935 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004936 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4937 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4938 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4939 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4940 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004941}
4942
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004943static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004944{
4945 struct drm_device *dev = crtc->base.dev;
4946 struct drm_i915_private *dev_priv = dev->dev_private;
4947 int pipe = crtc->pipe;
4948
4949 /* To avoid upsetting the power well on haswell only disable the pfit if
4950 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004951 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004952 I915_WRITE(PF_CTL(pipe), 0);
4953 I915_WRITE(PF_WIN_POS(pipe), 0);
4954 I915_WRITE(PF_WIN_SZ(pipe), 0);
4955 }
4956}
4957
Jesse Barnes6be4a602010-09-10 10:26:01 -07004958static void ironlake_crtc_disable(struct drm_crtc *crtc)
4959{
4960 struct drm_device *dev = crtc->dev;
4961 struct drm_i915_private *dev_priv = dev->dev_private;
4962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004963 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004964 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004965
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004966 /*
4967 * Sometimes spurious CPU pipe underruns happen when the
4968 * pipe is already disabled, but FDI RX/TX is still enabled.
4969 * Happens at least with VGA+HDMI cloning. Suppress them.
4970 */
4971 if (intel_crtc->config->has_pch_encoder) {
4972 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004973 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004974 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004975
Daniel Vetterea9d7582012-07-10 10:42:52 +02004976 for_each_encoder_on_crtc(dev, crtc, encoder)
4977 encoder->disable(encoder);
4978
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004979 drm_crtc_vblank_off(crtc);
4980 assert_vblank_disabled(crtc);
4981
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004982 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004983
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004984 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004985
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004986 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004987 ironlake_fdi_disable(crtc);
4988
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004989 for_each_encoder_on_crtc(dev, crtc, encoder)
4990 if (encoder->post_disable)
4991 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004992
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004993 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004994 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004995
Daniel Vetterd925c592013-06-05 13:34:04 +02004996 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004997 i915_reg_t reg;
4998 u32 temp;
4999
Daniel Vetterd925c592013-06-05 13:34:04 +02005000 /* disable TRANS_DP_CTL */
5001 reg = TRANS_DP_CTL(pipe);
5002 temp = I915_READ(reg);
5003 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5004 TRANS_DP_PORT_SEL_MASK);
5005 temp |= TRANS_DP_PORT_SEL_NONE;
5006 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005007
Daniel Vetterd925c592013-06-05 13:34:04 +02005008 /* disable DPLL_SEL */
5009 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005010 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005011 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005012 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005013
Daniel Vetterd925c592013-06-05 13:34:04 +02005014 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005015 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005016
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005017 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005018 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005019}
5020
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005021static void haswell_crtc_disable(struct drm_crtc *crtc)
5022{
5023 struct drm_device *dev = crtc->dev;
5024 struct drm_i915_private *dev_priv = dev->dev_private;
5025 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5026 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005027 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005028
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005029 if (intel_crtc->config->has_pch_encoder)
5030 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5031 false);
5032
Jani Nikula8807e552013-08-30 19:40:32 +03005033 for_each_encoder_on_crtc(dev, crtc, encoder) {
5034 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005035 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005036 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005037
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005038 drm_crtc_vblank_off(crtc);
5039 assert_vblank_disabled(crtc);
5040
Jani Nikula4d1de972016-03-18 17:05:42 +02005041 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5042 if (!intel_crtc->config->has_dsi_encoder)
5043 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005044
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005045 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005046 intel_ddi_set_vc_payload_alloc(crtc, false);
5047
Jani Nikulaa65347b2015-11-27 12:21:46 +02005048 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305049 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005050
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005051 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005052 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005053 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005054 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005055
Jani Nikulaa65347b2015-11-27 12:21:46 +02005056 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305057 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005058
Imre Deak97b040a2014-06-25 22:01:50 +03005059 for_each_encoder_on_crtc(dev, crtc, encoder)
5060 if (encoder->post_disable)
5061 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005062
Ville Syrjälä92966a32015-12-08 16:05:48 +02005063 if (intel_crtc->config->has_pch_encoder) {
5064 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005065 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005066 intel_ddi_fdi_disable(crtc);
5067
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005068 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5069 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005070 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005071}
5072
Jesse Barnes2dd24552013-04-25 12:55:01 -07005073static void i9xx_pfit_enable(struct intel_crtc *crtc)
5074{
5075 struct drm_device *dev = crtc->base.dev;
5076 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005077 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005078
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005079 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005080 return;
5081
Daniel Vetterc0b03412013-05-28 12:05:54 +02005082 /*
5083 * The panel fitter should only be adjusted whilst the pipe is disabled,
5084 * according to register description and PRM.
5085 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005086 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5087 assert_pipe_disabled(dev_priv, crtc->pipe);
5088
Jesse Barnesb074cec2013-04-25 12:55:02 -07005089 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5090 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005091
5092 /* Border color in case we don't scale up to the full screen. Black by
5093 * default, change to something else for debugging. */
5094 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005095}
5096
Dave Airlied05410f2014-06-05 13:22:59 +10005097static enum intel_display_power_domain port_to_power_domain(enum port port)
5098{
5099 switch (port) {
5100 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005101 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005102 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005103 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005104 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005105 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005106 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005107 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005108 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005109 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005110 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005111 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005112 return POWER_DOMAIN_PORT_OTHER;
5113 }
5114}
5115
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005116static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5117{
5118 switch (port) {
5119 case PORT_A:
5120 return POWER_DOMAIN_AUX_A;
5121 case PORT_B:
5122 return POWER_DOMAIN_AUX_B;
5123 case PORT_C:
5124 return POWER_DOMAIN_AUX_C;
5125 case PORT_D:
5126 return POWER_DOMAIN_AUX_D;
5127 case PORT_E:
5128 /* FIXME: Check VBT for actual wiring of PORT E */
5129 return POWER_DOMAIN_AUX_D;
5130 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005131 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005132 return POWER_DOMAIN_AUX_A;
5133 }
5134}
5135
Imre Deak319be8a2014-03-04 19:22:57 +02005136enum intel_display_power_domain
5137intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005138{
Imre Deak319be8a2014-03-04 19:22:57 +02005139 struct drm_device *dev = intel_encoder->base.dev;
5140 struct intel_digital_port *intel_dig_port;
5141
5142 switch (intel_encoder->type) {
5143 case INTEL_OUTPUT_UNKNOWN:
5144 /* Only DDI platforms should ever use this output type */
5145 WARN_ON_ONCE(!HAS_DDI(dev));
5146 case INTEL_OUTPUT_DISPLAYPORT:
5147 case INTEL_OUTPUT_HDMI:
5148 case INTEL_OUTPUT_EDP:
5149 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005150 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005151 case INTEL_OUTPUT_DP_MST:
5152 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5153 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005154 case INTEL_OUTPUT_ANALOG:
5155 return POWER_DOMAIN_PORT_CRT;
5156 case INTEL_OUTPUT_DSI:
5157 return POWER_DOMAIN_PORT_DSI;
5158 default:
5159 return POWER_DOMAIN_PORT_OTHER;
5160 }
5161}
5162
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005163enum intel_display_power_domain
5164intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5165{
5166 struct drm_device *dev = intel_encoder->base.dev;
5167 struct intel_digital_port *intel_dig_port;
5168
5169 switch (intel_encoder->type) {
5170 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005171 case INTEL_OUTPUT_HDMI:
5172 /*
5173 * Only DDI platforms should ever use these output types.
5174 * We can get here after the HDMI detect code has already set
5175 * the type of the shared encoder. Since we can't be sure
5176 * what's the status of the given connectors, play safe and
5177 * run the DP detection too.
5178 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005179 WARN_ON_ONCE(!HAS_DDI(dev));
5180 case INTEL_OUTPUT_DISPLAYPORT:
5181 case INTEL_OUTPUT_EDP:
5182 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5183 return port_to_aux_power_domain(intel_dig_port->port);
5184 case INTEL_OUTPUT_DP_MST:
5185 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5186 return port_to_aux_power_domain(intel_dig_port->port);
5187 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005188 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005189 return POWER_DOMAIN_AUX_A;
5190 }
5191}
5192
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005193static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5194 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005195{
5196 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005197 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005198 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5199 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005200 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005201 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005202
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005203 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005204 return 0;
5205
Imre Deak77d22dc2014-03-05 16:20:52 +02005206 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5207 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005208 if (crtc_state->pch_pfit.enabled ||
5209 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005210 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5211
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005212 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5213 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5214
Imre Deak319be8a2014-03-04 19:22:57 +02005215 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005216 }
Imre Deak319be8a2014-03-04 19:22:57 +02005217
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005218 if (crtc_state->shared_dpll)
5219 mask |= BIT(POWER_DOMAIN_PLLS);
5220
Imre Deak77d22dc2014-03-05 16:20:52 +02005221 return mask;
5222}
5223
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005224static unsigned long
5225modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5226 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005227{
5228 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5229 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5230 enum intel_display_power_domain domain;
Daniel Vetter5a21b662016-05-24 17:13:53 +02005231 unsigned long domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005232
5233 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005234 intel_crtc->enabled_power_domains = new_domains =
5235 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005236
Daniel Vetter5a21b662016-05-24 17:13:53 +02005237 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005238
5239 for_each_power_domain(domain, domains)
5240 intel_display_power_get(dev_priv, domain);
5241
Daniel Vetter5a21b662016-05-24 17:13:53 +02005242 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005243}
5244
5245static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5246 unsigned long domains)
5247{
5248 enum intel_display_power_domain domain;
5249
5250 for_each_power_domain(domain, domains)
5251 intel_display_power_put(dev_priv, domain);
5252}
5253
Mika Kaholaadafdc62015-08-18 14:36:59 +03005254static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5255{
5256 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5257
5258 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5259 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5260 return max_cdclk_freq;
5261 else if (IS_CHERRYVIEW(dev_priv))
5262 return max_cdclk_freq*95/100;
5263 else if (INTEL_INFO(dev_priv)->gen < 4)
5264 return 2*max_cdclk_freq*90/100;
5265 else
5266 return max_cdclk_freq*90/100;
5267}
5268
Ville Syrjäläb2045352016-05-13 23:41:27 +03005269static int skl_calc_cdclk(int max_pixclk, int vco);
5270
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005271static void intel_update_max_cdclk(struct drm_device *dev)
5272{
5273 struct drm_i915_private *dev_priv = dev->dev_private;
5274
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005275 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005276 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005277 int max_cdclk, vco;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005278
Ville Syrjäläb2045352016-05-13 23:41:27 +03005279 vco = dev_priv->skl_preferred_vco_freq;
Ville Syrjälä63911d72016-05-13 23:41:32 +03005280 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005281
5282 /*
5283 * Use the lower (vco 8640) cdclk values as a
5284 * first guess. skl_calc_cdclk() will correct it
5285 * if the preferred vco is 8100 instead.
5286 */
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005287 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005288 max_cdclk = 617143;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005289 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005290 max_cdclk = 540000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005291 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005292 max_cdclk = 432000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005293 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005294 max_cdclk = 308571;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005295
5296 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
Matt Roper281c1142016-04-05 14:37:19 -07005297 } else if (IS_BROXTON(dev)) {
5298 dev_priv->max_cdclk_freq = 624000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005299 } else if (IS_BROADWELL(dev)) {
5300 /*
5301 * FIXME with extra cooling we can allow
5302 * 540 MHz for ULX and 675 Mhz for ULT.
5303 * How can we know if extra cooling is
5304 * available? PCI ID, VTB, something else?
5305 */
5306 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5307 dev_priv->max_cdclk_freq = 450000;
5308 else if (IS_BDW_ULX(dev))
5309 dev_priv->max_cdclk_freq = 450000;
5310 else if (IS_BDW_ULT(dev))
5311 dev_priv->max_cdclk_freq = 540000;
5312 else
5313 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005314 } else if (IS_CHERRYVIEW(dev)) {
5315 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005316 } else if (IS_VALLEYVIEW(dev)) {
5317 dev_priv->max_cdclk_freq = 400000;
5318 } else {
5319 /* otherwise assume cdclk is fixed */
5320 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5321 }
5322
Mika Kaholaadafdc62015-08-18 14:36:59 +03005323 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5324
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005325 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5326 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005327
5328 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5329 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005330}
5331
5332static void intel_update_cdclk(struct drm_device *dev)
5333{
5334 struct drm_i915_private *dev_priv = dev->dev_private;
5335
5336 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005337
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005338 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005339 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5340 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5341 dev_priv->cdclk_pll.ref);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005342 else
5343 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5344 dev_priv->cdclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005345
5346 /*
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005347 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5348 * Programmng [sic] note: bit[9:2] should be programmed to the number
5349 * of cdclk that generates 4MHz reference clock freq which is used to
5350 * generate GMBus clock. This will vary with the cdclk freq.
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005351 */
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005352 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005353 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005354}
5355
Ville Syrjälä92891e42016-05-11 22:44:45 +03005356/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5357static int skl_cdclk_decimal(int cdclk)
5358{
5359 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5360}
5361
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005362static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5363{
5364 int ratio;
5365
5366 if (cdclk == dev_priv->cdclk_pll.ref)
5367 return 0;
5368
5369 switch (cdclk) {
5370 default:
5371 MISSING_CASE(cdclk);
5372 case 144000:
5373 case 288000:
5374 case 384000:
5375 case 576000:
5376 ratio = 60;
5377 break;
5378 case 624000:
5379 ratio = 65;
5380 break;
5381 }
5382
5383 return dev_priv->cdclk_pll.ref * ratio;
5384}
5385
Ville Syrjälä2b730012016-05-13 23:41:34 +03005386static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5387{
5388 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5389
5390 /* Timeout 200us */
5391 if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) == 0, 1))
5392 DRM_ERROR("timeout waiting for DE PLL unlock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005393
5394 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005395}
5396
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005397static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005398{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005399 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005400 u32 val;
5401
5402 val = I915_READ(BXT_DE_PLL_CTL);
5403 val &= ~BXT_DE_PLL_RATIO_MASK;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005404 val |= BXT_DE_PLL_RATIO(ratio);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005405 I915_WRITE(BXT_DE_PLL_CTL, val);
5406
5407 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5408
5409 /* Timeout 200us */
5410 if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) != 0, 1))
5411 DRM_ERROR("timeout waiting for DE PLL lock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005412
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005413 dev_priv->cdclk_pll.vco = vco;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005414}
5415
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005416static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305417{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005418 u32 val, divider;
5419 int vco, ret;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305420
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005421 vco = bxt_de_pll_vco(dev_priv, cdclk);
5422
5423 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5424
5425 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5426 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5427 case 8:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305428 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305429 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005430 case 4:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305431 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305432 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005433 case 3:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305434 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305435 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005436 case 2:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305437 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305438 break;
5439 default:
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005440 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5441 WARN_ON(vco != 0);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305442
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005443 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5444 break;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305445 }
5446
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305447 /* Inform power controller of upcoming frequency change */
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005448 mutex_lock(&dev_priv->rps.hw_lock);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305449 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5450 0x80000000);
5451 mutex_unlock(&dev_priv->rps.hw_lock);
5452
5453 if (ret) {
5454 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005455 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305456 return;
5457 }
5458
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005459 if (dev_priv->cdclk_pll.vco != 0 &&
5460 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005461 bxt_de_pll_disable(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305462
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005463 if (dev_priv->cdclk_pll.vco != vco)
5464 bxt_de_pll_enable(dev_priv, vco);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305465
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005466 val = divider | skl_cdclk_decimal(cdclk);
5467 /*
5468 * FIXME if only the cd2x divider needs changing, it could be done
5469 * without shutting off the pipe (if only one pipe is active).
5470 */
5471 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5472 /*
5473 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5474 * enable otherwise.
5475 */
5476 if (cdclk >= 500000)
5477 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5478 I915_WRITE(CDCLK_CTL, val);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305479
5480 mutex_lock(&dev_priv->rps.hw_lock);
5481 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005482 DIV_ROUND_UP(cdclk, 25000));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305483 mutex_unlock(&dev_priv->rps.hw_lock);
5484
5485 if (ret) {
5486 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005487 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305488 return;
5489 }
5490
Imre Deakc6c46962016-04-01 16:02:40 +03005491 intel_update_cdclk(dev_priv->dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305492}
5493
Imre Deakc6c46962016-04-01 16:02:40 +03005494void broxton_init_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305495{
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03005496 intel_update_cdclk(dev_priv->dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305497
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03005498 if (dev_priv->cdclk_pll.vco != 0)
5499 return;
Imre Deakc2e001e2016-04-01 16:02:43 +03005500
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305501 /*
5502 * FIXME:
5503 * - The initial CDCLK needs to be read from VBT.
5504 * Need to make this change after VBT has changes for BXT.
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305505 */
Ville Syrjäläd1b32c32016-05-13 23:41:40 +03005506 broxton_set_cdclk(dev_priv, broxton_calc_cdclk(0));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305507}
5508
Imre Deakc6c46962016-04-01 16:02:40 +03005509void broxton_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305510{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005511 broxton_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305512}
5513
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005514static int skl_calc_cdclk(int max_pixclk, int vco)
5515{
Ville Syrjälä63911d72016-05-13 23:41:32 +03005516 if (vco == 8640000) {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005517 if (max_pixclk > 540000)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005518 return 617143;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005519 else if (max_pixclk > 432000)
5520 return 540000;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005521 else if (max_pixclk > 308571)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005522 return 432000;
5523 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005524 return 308571;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005525 } else {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005526 if (max_pixclk > 540000)
5527 return 675000;
5528 else if (max_pixclk > 450000)
5529 return 540000;
5530 else if (max_pixclk > 337500)
5531 return 450000;
5532 else
5533 return 337500;
5534 }
5535}
5536
Ville Syrjäläea617912016-05-13 23:41:24 +03005537static void
5538skl_dpll0_update(struct drm_i915_private *dev_priv)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005539{
Ville Syrjäläea617912016-05-13 23:41:24 +03005540 u32 val;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005541
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005542 dev_priv->cdclk_pll.ref = 24000;
Imre Deak1c3f7702016-05-24 15:38:32 +03005543 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005544
Ville Syrjäläea617912016-05-13 23:41:24 +03005545 val = I915_READ(LCPLL1_CTL);
Imre Deak1c3f7702016-05-24 15:38:32 +03005546 if ((val & LCPLL_PLL_ENABLE) == 0)
Ville Syrjäläea617912016-05-13 23:41:24 +03005547 return;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005548
Imre Deak1c3f7702016-05-24 15:38:32 +03005549 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5550 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005551
Ville Syrjäläea617912016-05-13 23:41:24 +03005552 val = I915_READ(DPLL_CTRL1);
5553
Imre Deak1c3f7702016-05-24 15:38:32 +03005554 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5555 DPLL_CTRL1_SSC(SKL_DPLL0) |
5556 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5557 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5558 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005559
Ville Syrjäläea617912016-05-13 23:41:24 +03005560 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5561 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5562 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5563 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5564 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005565 dev_priv->cdclk_pll.vco = 8100000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005566 break;
5567 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5568 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005569 dev_priv->cdclk_pll.vco = 8640000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005570 break;
5571 default:
5572 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
Ville Syrjäläea617912016-05-13 23:41:24 +03005573 break;
5574 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005575}
5576
Ville Syrjäläb2045352016-05-13 23:41:27 +03005577void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5578{
5579 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5580
5581 dev_priv->skl_preferred_vco_freq = vco;
5582
5583 if (changed)
5584 intel_update_max_cdclk(dev_priv->dev);
5585}
5586
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005587static void
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005588skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005589{
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005590 int min_cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005591 u32 val;
5592
Ville Syrjälä63911d72016-05-13 23:41:32 +03005593 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005594
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005595 /* select the minimum CDCLK before enabling DPLL 0 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005596 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005597 I915_WRITE(CDCLK_CTL, val);
5598 POSTING_READ(CDCLK_CTL);
5599
5600 /*
5601 * We always enable DPLL0 with the lowest link rate possible, but still
5602 * taking into account the VCO required to operate the eDP panel at the
5603 * desired frequency. The usual DP link rates operate with a VCO of
5604 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5605 * The modeset code is responsible for the selection of the exact link
5606 * rate later on, with the constraint of choosing a frequency that
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005607 * works with vco.
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005608 */
5609 val = I915_READ(DPLL_CTRL1);
5610
5611 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5612 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5613 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Ville Syrjälä63911d72016-05-13 23:41:32 +03005614 if (vco == 8640000)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005615 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5616 SKL_DPLL0);
5617 else
5618 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5619 SKL_DPLL0);
5620
5621 I915_WRITE(DPLL_CTRL1, val);
5622 POSTING_READ(DPLL_CTRL1);
5623
5624 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5625
5626 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5627 DRM_ERROR("DPLL0 not locked\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005628
Ville Syrjälä63911d72016-05-13 23:41:32 +03005629 dev_priv->cdclk_pll.vco = vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005630
5631 /* We'll want to keep using the current vco from now on. */
5632 skl_set_preferred_cdclk_vco(dev_priv, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005633}
5634
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005635static void
5636skl_dpll0_disable(struct drm_i915_private *dev_priv)
5637{
5638 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5639 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5640 DRM_ERROR("Couldn't disable DPLL0\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005641
Ville Syrjälä63911d72016-05-13 23:41:32 +03005642 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005643}
5644
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005645static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5646{
5647 int ret;
5648 u32 val;
5649
5650 /* inform PCU we want to change CDCLK */
5651 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5652 mutex_lock(&dev_priv->rps.hw_lock);
5653 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5654 mutex_unlock(&dev_priv->rps.hw_lock);
5655
5656 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5657}
5658
5659static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5660{
5661 unsigned int i;
5662
5663 for (i = 0; i < 15; i++) {
5664 if (skl_cdclk_pcu_ready(dev_priv))
5665 return true;
5666 udelay(10);
5667 }
5668
5669 return false;
5670}
5671
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005672static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005673{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005674 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005675 u32 freq_select, pcu_ack;
5676
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005677 WARN_ON((cdclk == 24000) != (vco == 0));
5678
Ville Syrjälä63911d72016-05-13 23:41:32 +03005679 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005680
5681 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5682 DRM_ERROR("failed to inform PCU about cdclk change\n");
5683 return;
5684 }
5685
5686 /* set CDCLK_CTL */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005687 switch (cdclk) {
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005688 case 450000:
5689 case 432000:
5690 freq_select = CDCLK_FREQ_450_432;
5691 pcu_ack = 1;
5692 break;
5693 case 540000:
5694 freq_select = CDCLK_FREQ_540;
5695 pcu_ack = 2;
5696 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005697 case 308571:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005698 case 337500:
5699 default:
5700 freq_select = CDCLK_FREQ_337_308;
5701 pcu_ack = 0;
5702 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005703 case 617143:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005704 case 675000:
5705 freq_select = CDCLK_FREQ_675_617;
5706 pcu_ack = 3;
5707 break;
5708 }
5709
Ville Syrjälä63911d72016-05-13 23:41:32 +03005710 if (dev_priv->cdclk_pll.vco != 0 &&
5711 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005712 skl_dpll0_disable(dev_priv);
5713
Ville Syrjälä63911d72016-05-13 23:41:32 +03005714 if (dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005715 skl_dpll0_enable(dev_priv, vco);
5716
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005717 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005718 POSTING_READ(CDCLK_CTL);
5719
5720 /* inform PCU of the change */
5721 mutex_lock(&dev_priv->rps.hw_lock);
5722 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5723 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005724
5725 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005726}
5727
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005728static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5729
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005730void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5731{
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005732 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005733}
5734
5735void skl_init_cdclk(struct drm_i915_private *dev_priv)
5736{
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005737 int cdclk, vco;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005738
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005739 skl_sanitize_cdclk(dev_priv);
5740
Ville Syrjälä63911d72016-05-13 23:41:32 +03005741 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005742 /*
5743 * Use the current vco as our initial
5744 * guess as to what the preferred vco is.
5745 */
5746 if (dev_priv->skl_preferred_vco_freq == 0)
5747 skl_set_preferred_cdclk_vco(dev_priv,
Ville Syrjälä63911d72016-05-13 23:41:32 +03005748 dev_priv->cdclk_pll.vco);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005749 return;
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005750 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005751
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005752 vco = dev_priv->skl_preferred_vco_freq;
5753 if (vco == 0)
Ville Syrjälä63911d72016-05-13 23:41:32 +03005754 vco = 8100000;
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005755 cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005756
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005757 skl_set_cdclk(dev_priv, cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005758}
5759
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005760static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305761{
Ville Syrjälä09492492016-05-13 23:41:28 +03005762 uint32_t cdctl, expected;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305763
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305764 /*
5765 * check if the pre-os intialized the display
5766 * There is SWF18 scratchpad register defined which is set by the
5767 * pre-os which can be used by the OS drivers to check the status
5768 */
5769 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5770 goto sanitize;
5771
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005772 intel_update_cdclk(dev_priv->dev);
Imre Deak1c3f7702016-05-24 15:38:32 +03005773 /* Is PLL enabled and locked ? */
5774 if (dev_priv->cdclk_pll.vco == 0 ||
5775 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5776 goto sanitize;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005777
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305778 /* DPLL okay; verify the cdclock
5779 *
5780 * Noticed in some instances that the freq selection is correct but
5781 * decimal part is programmed wrong from BIOS where pre-os does not
5782 * enable display. Verify the same as well.
5783 */
Ville Syrjälä09492492016-05-13 23:41:28 +03005784 cdctl = I915_READ(CDCLK_CTL);
5785 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5786 skl_cdclk_decimal(dev_priv->cdclk_freq);
5787 if (cdctl == expected)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305788 /* All well; nothing to sanitize */
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005789 return;
5790
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305791sanitize:
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005792 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
Clint Taylorc89e39f2016-05-13 23:41:21 +03005793
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005794 /* force cdclk programming */
5795 dev_priv->cdclk_freq = 0;
5796 /* force full PLL disable + enable */
Ville Syrjälä63911d72016-05-13 23:41:32 +03005797 dev_priv->cdclk_pll.vco = -1;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305798}
5799
Jesse Barnes30a970c2013-11-04 13:48:12 -08005800/* Adjust CDclk dividers to allow high res or save power if possible */
5801static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5802{
5803 struct drm_i915_private *dev_priv = dev->dev_private;
5804 u32 val, cmd;
5805
Vandana Kannan164dfd22014-11-24 13:37:41 +05305806 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5807 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005808
Ville Syrjälädfcab172014-06-13 13:37:47 +03005809 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005810 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005811 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005812 cmd = 1;
5813 else
5814 cmd = 0;
5815
5816 mutex_lock(&dev_priv->rps.hw_lock);
5817 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5818 val &= ~DSPFREQGUAR_MASK;
5819 val |= (cmd << DSPFREQGUAR_SHIFT);
5820 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5821 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5822 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5823 50)) {
5824 DRM_ERROR("timed out waiting for CDclk change\n");
5825 }
5826 mutex_unlock(&dev_priv->rps.hw_lock);
5827
Ville Syrjälä54433e92015-05-26 20:42:31 +03005828 mutex_lock(&dev_priv->sb_lock);
5829
Ville Syrjälädfcab172014-06-13 13:37:47 +03005830 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005831 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005832
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005833 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005834
Jesse Barnes30a970c2013-11-04 13:48:12 -08005835 /* adjust cdclk divider */
5836 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005837 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005838 val |= divider;
5839 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005840
5841 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005842 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005843 50))
5844 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005845 }
5846
Jesse Barnes30a970c2013-11-04 13:48:12 -08005847 /* adjust self-refresh exit latency value */
5848 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5849 val &= ~0x7f;
5850
5851 /*
5852 * For high bandwidth configs, we set a higher latency in the bunit
5853 * so that the core display fetch happens in time to avoid underruns.
5854 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005855 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005856 val |= 4500 / 250; /* 4.5 usec */
5857 else
5858 val |= 3000 / 250; /* 3.0 usec */
5859 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005860
Ville Syrjäläa5805162015-05-26 20:42:30 +03005861 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005862
Ville Syrjäläb6283052015-06-03 15:45:07 +03005863 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005864}
5865
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005866static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5867{
5868 struct drm_i915_private *dev_priv = dev->dev_private;
5869 u32 val, cmd;
5870
Vandana Kannan164dfd22014-11-24 13:37:41 +05305871 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5872 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005873
5874 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005875 case 333333:
5876 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005877 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005878 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005879 break;
5880 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005881 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005882 return;
5883 }
5884
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005885 /*
5886 * Specs are full of misinformation, but testing on actual
5887 * hardware has shown that we just need to write the desired
5888 * CCK divider into the Punit register.
5889 */
5890 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5891
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005892 mutex_lock(&dev_priv->rps.hw_lock);
5893 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5894 val &= ~DSPFREQGUAR_MASK_CHV;
5895 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5896 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5897 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5898 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5899 50)) {
5900 DRM_ERROR("timed out waiting for CDclk change\n");
5901 }
5902 mutex_unlock(&dev_priv->rps.hw_lock);
5903
Ville Syrjäläb6283052015-06-03 15:45:07 +03005904 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005905}
5906
Jesse Barnes30a970c2013-11-04 13:48:12 -08005907static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5908 int max_pixclk)
5909{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005910 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005911 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005912
Jesse Barnes30a970c2013-11-04 13:48:12 -08005913 /*
5914 * Really only a few cases to deal with, as only 4 CDclks are supported:
5915 * 200MHz
5916 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005917 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005918 * 400MHz (VLV only)
5919 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5920 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005921 *
5922 * We seem to get an unstable or solid color picture at 200MHz.
5923 * Not sure what's wrong. For now use 200MHz only when all pipes
5924 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005925 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005926 if (!IS_CHERRYVIEW(dev_priv) &&
5927 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005928 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005929 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005930 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005931 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005932 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005933 else
5934 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005935}
5936
Ville Syrjäläc44deb62016-05-11 22:44:43 +03005937static int broxton_calc_cdclk(int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005938{
Ville Syrjälä760e1472016-05-11 22:44:46 +03005939 if (max_pixclk > 576000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305940 return 624000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005941 else if (max_pixclk > 384000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305942 return 576000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005943 else if (max_pixclk > 288000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305944 return 384000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005945 else if (max_pixclk > 144000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305946 return 288000;
5947 else
5948 return 144000;
5949}
5950
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005951/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005952static int intel_mode_max_pixclk(struct drm_device *dev,
5953 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005954{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005955 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
5956 struct drm_i915_private *dev_priv = dev->dev_private;
5957 struct drm_crtc *crtc;
5958 struct drm_crtc_state *crtc_state;
5959 unsigned max_pixclk = 0, i;
5960 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005961
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005962 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5963 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005964
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005965 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5966 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005967
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005968 if (crtc_state->enable)
5969 pixclk = crtc_state->adjusted_mode.crtc_clock;
5970
5971 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005972 }
5973
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005974 for_each_pipe(dev_priv, pipe)
5975 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
5976
Jesse Barnes30a970c2013-11-04 13:48:12 -08005977 return max_pixclk;
5978}
5979
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005980static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005981{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005982 struct drm_device *dev = state->dev;
5983 struct drm_i915_private *dev_priv = dev->dev_private;
5984 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005985 struct intel_atomic_state *intel_state =
5986 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005987
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005988 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005989 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305990
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01005991 if (!intel_state->active_crtcs)
5992 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
5993
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005994 return 0;
5995}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005996
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005997static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5998{
Ville Syrjälä4e5ca602016-05-11 22:44:44 +03005999 int max_pixclk = ilk_max_pixel_rate(state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006000 struct intel_atomic_state *intel_state =
6001 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006002
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006003 intel_state->cdclk = intel_state->dev_cdclk =
Ville Syrjäläc44deb62016-05-11 22:44:43 +03006004 broxton_calc_cdclk(max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006005
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006006 if (!intel_state->active_crtcs)
Ville Syrjäläc44deb62016-05-11 22:44:43 +03006007 intel_state->dev_cdclk = broxton_calc_cdclk(0);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006008
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006009 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006010}
6011
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006012static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6013{
6014 unsigned int credits, default_credits;
6015
6016 if (IS_CHERRYVIEW(dev_priv))
6017 default_credits = PFI_CREDIT(12);
6018 else
6019 default_credits = PFI_CREDIT(8);
6020
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006021 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006022 /* CHV suggested value is 31 or 63 */
6023 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006024 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006025 else
6026 credits = PFI_CREDIT(15);
6027 } else {
6028 credits = default_credits;
6029 }
6030
6031 /*
6032 * WA - write default credits before re-programming
6033 * FIXME: should we also set the resend bit here?
6034 */
6035 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6036 default_credits);
6037
6038 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6039 credits | PFI_CREDIT_RESEND);
6040
6041 /*
6042 * FIXME is this guaranteed to clear
6043 * immediately or should we poll for it?
6044 */
6045 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6046}
6047
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006048static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006049{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006050 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006051 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006052 struct intel_atomic_state *old_intel_state =
6053 to_intel_atomic_state(old_state);
6054 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006055
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006056 /*
6057 * FIXME: We can end up here with all power domains off, yet
6058 * with a CDCLK frequency other than the minimum. To account
6059 * for this take the PIPE-A power domain, which covers the HW
6060 * blocks needed for the following programming. This can be
6061 * removed once it's guaranteed that we get here either with
6062 * the minimum CDCLK set, or the required power domains
6063 * enabled.
6064 */
6065 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006066
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006067 if (IS_CHERRYVIEW(dev))
6068 cherryview_set_cdclk(dev, req_cdclk);
6069 else
6070 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006071
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006072 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006073
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006074 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006075}
6076
Jesse Barnes89b667f2013-04-18 14:51:36 -07006077static void valleyview_crtc_enable(struct drm_crtc *crtc)
6078{
6079 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006080 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006081 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6082 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006083 struct intel_crtc_state *pipe_config =
6084 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006085 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006086
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006087 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006088 return;
6089
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006090 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306091 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006092
6093 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006094 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006095
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006096 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6097 struct drm_i915_private *dev_priv = dev->dev_private;
6098
6099 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6100 I915_WRITE(CHV_CANVAS(pipe), 0);
6101 }
6102
Daniel Vetter5b18e572014-04-24 23:55:06 +02006103 i9xx_set_pipeconf(intel_crtc);
6104
Jesse Barnes89b667f2013-04-18 14:51:36 -07006105 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006106
Daniel Vettera72e4c92014-09-30 10:56:47 +02006107 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006108
Jesse Barnes89b667f2013-04-18 14:51:36 -07006109 for_each_encoder_on_crtc(dev, crtc, encoder)
6110 if (encoder->pre_pll_enable)
6111 encoder->pre_pll_enable(encoder);
6112
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006113 if (IS_CHERRYVIEW(dev)) {
6114 chv_prepare_pll(intel_crtc, intel_crtc->config);
6115 chv_enable_pll(intel_crtc, intel_crtc->config);
6116 } else {
6117 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6118 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006119 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006120
6121 for_each_encoder_on_crtc(dev, crtc, encoder)
6122 if (encoder->pre_enable)
6123 encoder->pre_enable(encoder);
6124
Jesse Barnes2dd24552013-04-25 12:55:01 -07006125 i9xx_pfit_enable(intel_crtc);
6126
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006127 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006128
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006129 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006130 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006131
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006132 assert_vblank_disabled(crtc);
6133 drm_crtc_vblank_on(crtc);
6134
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006135 for_each_encoder_on_crtc(dev, crtc, encoder)
6136 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006137}
6138
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006139static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6140{
6141 struct drm_device *dev = crtc->base.dev;
6142 struct drm_i915_private *dev_priv = dev->dev_private;
6143
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006144 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6145 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006146}
6147
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006148static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006149{
6150 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006151 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006152 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006153 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006154 struct intel_crtc_state *pipe_config =
6155 to_intel_crtc_state(crtc->state);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006156 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006157
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006158 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006159 return;
6160
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006161 i9xx_set_pll_dividers(intel_crtc);
6162
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006163 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306164 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006165
6166 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006167 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006168
Daniel Vetter5b18e572014-04-24 23:55:06 +02006169 i9xx_set_pipeconf(intel_crtc);
6170
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006171 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006172
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006173 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006174 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006175
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006176 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006177 if (encoder->pre_enable)
6178 encoder->pre_enable(encoder);
6179
Daniel Vetterf6736a12013-06-05 13:34:30 +02006180 i9xx_enable_pll(intel_crtc);
6181
Jesse Barnes2dd24552013-04-25 12:55:01 -07006182 i9xx_pfit_enable(intel_crtc);
6183
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006184 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006185
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006186 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006187 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006188
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006189 assert_vblank_disabled(crtc);
6190 drm_crtc_vblank_on(crtc);
6191
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006192 for_each_encoder_on_crtc(dev, crtc, encoder)
6193 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006194}
6195
Daniel Vetter87476d62013-04-11 16:29:06 +02006196static void i9xx_pfit_disable(struct intel_crtc *crtc)
6197{
6198 struct drm_device *dev = crtc->base.dev;
6199 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006200
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006201 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006202 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006203
6204 assert_pipe_disabled(dev_priv, crtc->pipe);
6205
Daniel Vetter328d8e82013-05-08 10:36:31 +02006206 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6207 I915_READ(PFIT_CONTROL));
6208 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006209}
6210
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006211static void i9xx_crtc_disable(struct drm_crtc *crtc)
6212{
6213 struct drm_device *dev = crtc->dev;
6214 struct drm_i915_private *dev_priv = dev->dev_private;
6215 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006216 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006217 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006218
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006219 /*
6220 * On gen2 planes are double buffered but the pipe isn't, so we must
6221 * wait for planes to fully turn off before disabling the pipe.
6222 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006223 if (IS_GEN2(dev))
6224 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006225
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006226 for_each_encoder_on_crtc(dev, crtc, encoder)
6227 encoder->disable(encoder);
6228
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006229 drm_crtc_vblank_off(crtc);
6230 assert_vblank_disabled(crtc);
6231
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006232 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006233
Daniel Vetter87476d62013-04-11 16:29:06 +02006234 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006235
Jesse Barnes89b667f2013-04-18 14:51:36 -07006236 for_each_encoder_on_crtc(dev, crtc, encoder)
6237 if (encoder->post_disable)
6238 encoder->post_disable(encoder);
6239
Jani Nikulaa65347b2015-11-27 12:21:46 +02006240 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006241 if (IS_CHERRYVIEW(dev))
6242 chv_disable_pll(dev_priv, pipe);
6243 else if (IS_VALLEYVIEW(dev))
6244 vlv_disable_pll(dev_priv, pipe);
6245 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006246 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006247 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006248
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006249 for_each_encoder_on_crtc(dev, crtc, encoder)
6250 if (encoder->post_pll_disable)
6251 encoder->post_pll_disable(encoder);
6252
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006253 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006254 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006255}
6256
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006257static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006258{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006259 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006260 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006261 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006262 enum intel_display_power_domain domain;
6263 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006264
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006265 if (!intel_crtc->active)
6266 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006267
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006268 if (to_intel_plane_state(crtc->primary->state)->visible) {
Daniel Vetter5a21b662016-05-24 17:13:53 +02006269 WARN_ON(intel_crtc->flip_work);
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006270
Ville Syrjälä2622a082016-03-09 19:07:26 +02006271 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006272
6273 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6274 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006275 }
6276
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006277 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006278
6279 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
6280 crtc->base.id);
6281
6282 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6283 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006284 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006285 crtc->enabled = false;
6286 crtc->state->connector_mask = 0;
6287 crtc->state->encoder_mask = 0;
6288
6289 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6290 encoder->base.crtc = NULL;
6291
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006292 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006293 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006294 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006295
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006296 domains = intel_crtc->enabled_power_domains;
6297 for_each_power_domain(domain, domains)
6298 intel_display_power_put(dev_priv, domain);
6299 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006300
6301 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6302 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006303}
6304
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006305/*
6306 * turn all crtc's off, but do not adjust state
6307 * This has to be paired with a call to intel_modeset_setup_hw_state.
6308 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006309int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006310{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006311 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006312 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006313 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006314
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006315 state = drm_atomic_helper_suspend(dev);
6316 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006317 if (ret)
6318 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006319 else
6320 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006321 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006322}
6323
Chris Wilsonea5b2132010-08-04 13:50:23 +01006324void intel_encoder_destroy(struct drm_encoder *encoder)
6325{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006326 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006327
Chris Wilsonea5b2132010-08-04 13:50:23 +01006328 drm_encoder_cleanup(encoder);
6329 kfree(intel_encoder);
6330}
6331
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006332/* Cross check the actual hw state with our own modeset state tracking (and it's
6333 * internal consistency). */
Daniel Vetter5a21b662016-05-24 17:13:53 +02006334static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006335{
Daniel Vetter5a21b662016-05-24 17:13:53 +02006336 struct drm_crtc *crtc = connector->base.state->crtc;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006337
6338 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6339 connector->base.base.id,
6340 connector->base.name);
6341
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006342 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006343 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter5a21b662016-05-24 17:13:53 +02006344 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006345
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006346 I915_STATE_WARN(!crtc,
6347 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006348
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006349 if (!crtc)
6350 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006351
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006352 I915_STATE_WARN(!crtc->state->active,
6353 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006354
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006355 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006356 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006357
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006358 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006359 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006360
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006361 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006362 "attached encoder crtc differs from connector crtc\n");
6363 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006364 I915_STATE_WARN(crtc && crtc->state->active,
6365 "attached crtc is active, but connector isn't\n");
Daniel Vetter5a21b662016-05-24 17:13:53 +02006366 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006367 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006368 }
6369}
6370
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006371int intel_connector_init(struct intel_connector *connector)
6372{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006373 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006374
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006375 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006376 return -ENOMEM;
6377
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006378 return 0;
6379}
6380
6381struct intel_connector *intel_connector_alloc(void)
6382{
6383 struct intel_connector *connector;
6384
6385 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6386 if (!connector)
6387 return NULL;
6388
6389 if (intel_connector_init(connector) < 0) {
6390 kfree(connector);
6391 return NULL;
6392 }
6393
6394 return connector;
6395}
6396
Daniel Vetterf0947c32012-07-02 13:10:34 +02006397/* Simple connector->get_hw_state implementation for encoders that support only
6398 * one connector and no cloning and hence the encoder state determines the state
6399 * of the connector. */
6400bool intel_connector_get_hw_state(struct intel_connector *connector)
6401{
Daniel Vetter24929352012-07-02 20:28:59 +02006402 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006403 struct intel_encoder *encoder = connector->encoder;
6404
6405 return encoder->get_hw_state(encoder, &pipe);
6406}
6407
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006408static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006409{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006410 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6411 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006412
6413 return 0;
6414}
6415
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006416static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006417 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006418{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006419 struct drm_atomic_state *state = pipe_config->base.state;
6420 struct intel_crtc *other_crtc;
6421 struct intel_crtc_state *other_crtc_state;
6422
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006423 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6424 pipe_name(pipe), pipe_config->fdi_lanes);
6425 if (pipe_config->fdi_lanes > 4) {
6426 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6427 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006428 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006429 }
6430
Paulo Zanonibafb6552013-11-02 21:07:44 -07006431 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006432 if (pipe_config->fdi_lanes > 2) {
6433 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6434 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006435 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006436 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006437 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006438 }
6439 }
6440
6441 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006442 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006443
6444 /* Ivybridge 3 pipe is really complicated */
6445 switch (pipe) {
6446 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006447 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006448 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006449 if (pipe_config->fdi_lanes <= 2)
6450 return 0;
6451
6452 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6453 other_crtc_state =
6454 intel_atomic_get_crtc_state(state, other_crtc);
6455 if (IS_ERR(other_crtc_state))
6456 return PTR_ERR(other_crtc_state);
6457
6458 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006459 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6460 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006461 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006462 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006463 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006464 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006465 if (pipe_config->fdi_lanes > 2) {
6466 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6467 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006468 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006469 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006470
6471 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6472 other_crtc_state =
6473 intel_atomic_get_crtc_state(state, other_crtc);
6474 if (IS_ERR(other_crtc_state))
6475 return PTR_ERR(other_crtc_state);
6476
6477 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006478 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006479 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006480 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006481 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006482 default:
6483 BUG();
6484 }
6485}
6486
Daniel Vettere29c22c2013-02-21 00:00:16 +01006487#define RETRY 1
6488static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006489 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006490{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006491 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006492 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006493 int lane, link_bw, fdi_dotclock, ret;
6494 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006495
Daniel Vettere29c22c2013-02-21 00:00:16 +01006496retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006497 /* FDI is a binary signal running at ~2.7GHz, encoding
6498 * each output octet as 10 bits. The actual frequency
6499 * is stored as a divider into a 100MHz clock, and the
6500 * mode pixel clock is stored in units of 1KHz.
6501 * Hence the bw of each lane in terms of the mode signal
6502 * is:
6503 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006504 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006505
Damien Lespiau241bfc32013-09-25 16:45:37 +01006506 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006507
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006508 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006509 pipe_config->pipe_bpp);
6510
6511 pipe_config->fdi_lanes = lane;
6512
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006513 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006514 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006515
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006516 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006517 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006518 pipe_config->pipe_bpp -= 2*3;
6519 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6520 pipe_config->pipe_bpp);
6521 needs_recompute = true;
6522 pipe_config->bw_constrained = true;
6523
6524 goto retry;
6525 }
6526
6527 if (needs_recompute)
6528 return RETRY;
6529
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006530 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006531}
6532
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006533static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6534 struct intel_crtc_state *pipe_config)
6535{
6536 if (pipe_config->pipe_bpp > 24)
6537 return false;
6538
6539 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006540 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006541 return true;
6542
6543 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006544 * We compare against max which means we must take
6545 * the increased cdclk requirement into account when
6546 * calculating the new cdclk.
6547 *
6548 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006549 */
6550 return ilk_pipe_pixel_rate(pipe_config) <=
6551 dev_priv->max_cdclk_freq * 95 / 100;
6552}
6553
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006554static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006555 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006556{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006557 struct drm_device *dev = crtc->base.dev;
6558 struct drm_i915_private *dev_priv = dev->dev_private;
6559
Jani Nikulad330a952014-01-21 11:24:25 +02006560 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006561 hsw_crtc_supports_ips(crtc) &&
6562 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006563}
6564
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006565static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6566{
6567 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6568
6569 /* GDG double wide on either pipe, otherwise pipe A only */
6570 return INTEL_INFO(dev_priv)->gen < 4 &&
6571 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6572}
6573
Daniel Vettera43f6e02013-06-07 23:10:32 +02006574static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006575 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006576{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006577 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006578 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006579 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006580
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006581 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006582 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006583 int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006584
6585 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006586 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006587 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006588 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006589 if (intel_crtc_supports_double_wide(crtc) &&
6590 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006591 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006592 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006593 }
6594
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006595 if (adjusted_mode->crtc_clock > clock_limit) {
6596 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6597 adjusted_mode->crtc_clock, clock_limit,
6598 yesno(pipe_config->double_wide));
Daniel Vettere29c22c2013-02-21 00:00:16 +01006599 return -EINVAL;
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006600 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006601 }
Chris Wilson89749352010-09-12 18:25:19 +01006602
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006603 /*
6604 * Pipe horizontal size must be even in:
6605 * - DVO ganged mode
6606 * - LVDS dual channel mode
6607 * - Double wide pipe
6608 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006609 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006610 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6611 pipe_config->pipe_src_w &= ~1;
6612
Damien Lespiau8693a822013-05-03 18:48:11 +01006613 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6614 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006615 */
6616 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006617 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006618 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006619
Damien Lespiauf5adf942013-06-24 18:29:34 +01006620 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006621 hsw_compute_ips_config(crtc, pipe_config);
6622
Daniel Vetter877d48d2013-04-19 11:24:43 +02006623 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006624 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006625
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006626 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006627}
6628
Ville Syrjälä1652d192015-03-31 14:12:01 +03006629static int skylake_get_display_clock_speed(struct drm_device *dev)
6630{
6631 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläea617912016-05-13 23:41:24 +03006632 uint32_t cdctl;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006633
Ville Syrjäläea617912016-05-13 23:41:24 +03006634 skl_dpll0_update(dev_priv);
6635
Ville Syrjälä63911d72016-05-13 23:41:32 +03006636 if (dev_priv->cdclk_pll.vco == 0)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006637 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006638
Ville Syrjäläea617912016-05-13 23:41:24 +03006639 cdctl = I915_READ(CDCLK_CTL);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006640
Ville Syrjälä63911d72016-05-13 23:41:32 +03006641 if (dev_priv->cdclk_pll.vco == 8640000) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006642 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6643 case CDCLK_FREQ_450_432:
6644 return 432000;
6645 case CDCLK_FREQ_337_308:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006646 return 308571;
Ville Syrjäläea617912016-05-13 23:41:24 +03006647 case CDCLK_FREQ_540:
6648 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006649 case CDCLK_FREQ_675_617:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006650 return 617143;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006651 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006652 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006653 }
6654 } else {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006655 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6656 case CDCLK_FREQ_450_432:
6657 return 450000;
6658 case CDCLK_FREQ_337_308:
6659 return 337500;
Ville Syrjäläea617912016-05-13 23:41:24 +03006660 case CDCLK_FREQ_540:
6661 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006662 case CDCLK_FREQ_675_617:
6663 return 675000;
6664 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006665 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006666 }
6667 }
6668
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006669 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006670}
6671
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006672static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6673{
6674 u32 val;
6675
6676 dev_priv->cdclk_pll.ref = 19200;
Imre Deak1c3f7702016-05-24 15:38:32 +03006677 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006678
6679 val = I915_READ(BXT_DE_PLL_ENABLE);
Imre Deak1c3f7702016-05-24 15:38:32 +03006680 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006681 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006682
Imre Deak1c3f7702016-05-24 15:38:32 +03006683 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6684 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006685
6686 val = I915_READ(BXT_DE_PLL_CTL);
6687 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6688 dev_priv->cdclk_pll.ref;
6689}
6690
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006691static int broxton_get_display_clock_speed(struct drm_device *dev)
6692{
6693 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf5986242016-05-13 23:41:37 +03006694 u32 divider;
6695 int div, vco;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006696
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006697 bxt_de_pll_update(dev_priv);
6698
Ville Syrjäläf5986242016-05-13 23:41:37 +03006699 vco = dev_priv->cdclk_pll.vco;
6700 if (vco == 0)
6701 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006702
Ville Syrjäläf5986242016-05-13 23:41:37 +03006703 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006704
Ville Syrjäläf5986242016-05-13 23:41:37 +03006705 switch (divider) {
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006706 case BXT_CDCLK_CD2X_DIV_SEL_1:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006707 div = 2;
6708 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006709 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006710 div = 3;
6711 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006712 case BXT_CDCLK_CD2X_DIV_SEL_2:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006713 div = 4;
6714 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006715 case BXT_CDCLK_CD2X_DIV_SEL_4:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006716 div = 8;
6717 break;
6718 default:
6719 MISSING_CASE(divider);
6720 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006721 }
6722
Ville Syrjäläf5986242016-05-13 23:41:37 +03006723 return DIV_ROUND_CLOSEST(vco, div);
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006724}
6725
Ville Syrjälä1652d192015-03-31 14:12:01 +03006726static int broadwell_get_display_clock_speed(struct drm_device *dev)
6727{
6728 struct drm_i915_private *dev_priv = dev->dev_private;
6729 uint32_t lcpll = I915_READ(LCPLL_CTL);
6730 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6731
6732 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6733 return 800000;
6734 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6735 return 450000;
6736 else if (freq == LCPLL_CLK_FREQ_450)
6737 return 450000;
6738 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6739 return 540000;
6740 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6741 return 337500;
6742 else
6743 return 675000;
6744}
6745
6746static int haswell_get_display_clock_speed(struct drm_device *dev)
6747{
6748 struct drm_i915_private *dev_priv = dev->dev_private;
6749 uint32_t lcpll = I915_READ(LCPLL_CTL);
6750 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6751
6752 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6753 return 800000;
6754 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6755 return 450000;
6756 else if (freq == LCPLL_CLK_FREQ_450)
6757 return 450000;
6758 else if (IS_HSW_ULT(dev))
6759 return 337500;
6760 else
6761 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006762}
6763
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006764static int valleyview_get_display_clock_speed(struct drm_device *dev)
6765{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006766 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6767 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006768}
6769
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006770static int ilk_get_display_clock_speed(struct drm_device *dev)
6771{
6772 return 450000;
6773}
6774
Jesse Barnese70236a2009-09-21 10:42:27 -07006775static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006776{
Jesse Barnese70236a2009-09-21 10:42:27 -07006777 return 400000;
6778}
Jesse Barnes79e53942008-11-07 14:24:08 -08006779
Jesse Barnese70236a2009-09-21 10:42:27 -07006780static int i915_get_display_clock_speed(struct drm_device *dev)
6781{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006782 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006783}
Jesse Barnes79e53942008-11-07 14:24:08 -08006784
Jesse Barnese70236a2009-09-21 10:42:27 -07006785static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6786{
6787 return 200000;
6788}
Jesse Barnes79e53942008-11-07 14:24:08 -08006789
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006790static int pnv_get_display_clock_speed(struct drm_device *dev)
6791{
6792 u16 gcfgc = 0;
6793
6794 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6795
6796 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6797 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006798 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006799 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006800 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006801 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006802 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006803 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6804 return 200000;
6805 default:
6806 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6807 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006808 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006809 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006810 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006811 }
6812}
6813
Jesse Barnese70236a2009-09-21 10:42:27 -07006814static int i915gm_get_display_clock_speed(struct drm_device *dev)
6815{
6816 u16 gcfgc = 0;
6817
6818 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6819
6820 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006821 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006822 else {
6823 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6824 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006825 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006826 default:
6827 case GC_DISPLAY_CLOCK_190_200_MHZ:
6828 return 190000;
6829 }
6830 }
6831}
Jesse Barnes79e53942008-11-07 14:24:08 -08006832
Jesse Barnese70236a2009-09-21 10:42:27 -07006833static int i865_get_display_clock_speed(struct drm_device *dev)
6834{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006835 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006836}
6837
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006838static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006839{
6840 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006841
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006842 /*
6843 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6844 * encoding is different :(
6845 * FIXME is this the right way to detect 852GM/852GMV?
6846 */
6847 if (dev->pdev->revision == 0x1)
6848 return 133333;
6849
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006850 pci_bus_read_config_word(dev->pdev->bus,
6851 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6852
Jesse Barnese70236a2009-09-21 10:42:27 -07006853 /* Assume that the hardware is in the high speed state. This
6854 * should be the default.
6855 */
6856 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6857 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006858 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006859 case GC_CLOCK_100_200:
6860 return 200000;
6861 case GC_CLOCK_166_250:
6862 return 250000;
6863 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006864 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006865 case GC_CLOCK_133_266:
6866 case GC_CLOCK_133_266_2:
6867 case GC_CLOCK_166_266:
6868 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006869 }
6870
6871 /* Shouldn't happen */
6872 return 0;
6873}
6874
6875static int i830_get_display_clock_speed(struct drm_device *dev)
6876{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006877 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006878}
6879
Ville Syrjälä34edce22015-05-22 11:22:33 +03006880static unsigned int intel_hpll_vco(struct drm_device *dev)
6881{
6882 struct drm_i915_private *dev_priv = dev->dev_private;
6883 static const unsigned int blb_vco[8] = {
6884 [0] = 3200000,
6885 [1] = 4000000,
6886 [2] = 5333333,
6887 [3] = 4800000,
6888 [4] = 6400000,
6889 };
6890 static const unsigned int pnv_vco[8] = {
6891 [0] = 3200000,
6892 [1] = 4000000,
6893 [2] = 5333333,
6894 [3] = 4800000,
6895 [4] = 2666667,
6896 };
6897 static const unsigned int cl_vco[8] = {
6898 [0] = 3200000,
6899 [1] = 4000000,
6900 [2] = 5333333,
6901 [3] = 6400000,
6902 [4] = 3333333,
6903 [5] = 3566667,
6904 [6] = 4266667,
6905 };
6906 static const unsigned int elk_vco[8] = {
6907 [0] = 3200000,
6908 [1] = 4000000,
6909 [2] = 5333333,
6910 [3] = 4800000,
6911 };
6912 static const unsigned int ctg_vco[8] = {
6913 [0] = 3200000,
6914 [1] = 4000000,
6915 [2] = 5333333,
6916 [3] = 6400000,
6917 [4] = 2666667,
6918 [5] = 4266667,
6919 };
6920 const unsigned int *vco_table;
6921 unsigned int vco;
6922 uint8_t tmp = 0;
6923
6924 /* FIXME other chipsets? */
6925 if (IS_GM45(dev))
6926 vco_table = ctg_vco;
6927 else if (IS_G4X(dev))
6928 vco_table = elk_vco;
6929 else if (IS_CRESTLINE(dev))
6930 vco_table = cl_vco;
6931 else if (IS_PINEVIEW(dev))
6932 vco_table = pnv_vco;
6933 else if (IS_G33(dev))
6934 vco_table = blb_vco;
6935 else
6936 return 0;
6937
6938 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6939
6940 vco = vco_table[tmp & 0x7];
6941 if (vco == 0)
6942 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6943 else
6944 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6945
6946 return vco;
6947}
6948
6949static int gm45_get_display_clock_speed(struct drm_device *dev)
6950{
6951 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6952 uint16_t tmp = 0;
6953
6954 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6955
6956 cdclk_sel = (tmp >> 12) & 0x1;
6957
6958 switch (vco) {
6959 case 2666667:
6960 case 4000000:
6961 case 5333333:
6962 return cdclk_sel ? 333333 : 222222;
6963 case 3200000:
6964 return cdclk_sel ? 320000 : 228571;
6965 default:
6966 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6967 return 222222;
6968 }
6969}
6970
6971static int i965gm_get_display_clock_speed(struct drm_device *dev)
6972{
6973 static const uint8_t div_3200[] = { 16, 10, 8 };
6974 static const uint8_t div_4000[] = { 20, 12, 10 };
6975 static const uint8_t div_5333[] = { 24, 16, 14 };
6976 const uint8_t *div_table;
6977 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6978 uint16_t tmp = 0;
6979
6980 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6981
6982 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6983
6984 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6985 goto fail;
6986
6987 switch (vco) {
6988 case 3200000:
6989 div_table = div_3200;
6990 break;
6991 case 4000000:
6992 div_table = div_4000;
6993 break;
6994 case 5333333:
6995 div_table = div_5333;
6996 break;
6997 default:
6998 goto fail;
6999 }
7000
7001 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7002
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007003fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007004 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7005 return 200000;
7006}
7007
7008static int g33_get_display_clock_speed(struct drm_device *dev)
7009{
7010 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7011 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7012 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7013 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7014 const uint8_t *div_table;
7015 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7016 uint16_t tmp = 0;
7017
7018 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7019
7020 cdclk_sel = (tmp >> 4) & 0x7;
7021
7022 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7023 goto fail;
7024
7025 switch (vco) {
7026 case 3200000:
7027 div_table = div_3200;
7028 break;
7029 case 4000000:
7030 div_table = div_4000;
7031 break;
7032 case 4800000:
7033 div_table = div_4800;
7034 break;
7035 case 5333333:
7036 div_table = div_5333;
7037 break;
7038 default:
7039 goto fail;
7040 }
7041
7042 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7043
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007044fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007045 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7046 return 190476;
7047}
7048
Zhenyu Wang2c072452009-06-05 15:38:42 +08007049static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007050intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007051{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007052 while (*num > DATA_LINK_M_N_MASK ||
7053 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007054 *num >>= 1;
7055 *den >>= 1;
7056 }
7057}
7058
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007059static void compute_m_n(unsigned int m, unsigned int n,
7060 uint32_t *ret_m, uint32_t *ret_n)
7061{
7062 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7063 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7064 intel_reduce_m_n_ratio(ret_m, ret_n);
7065}
7066
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007067void
7068intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7069 int pixel_clock, int link_clock,
7070 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007071{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007072 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007073
7074 compute_m_n(bits_per_pixel * pixel_clock,
7075 link_clock * nlanes * 8,
7076 &m_n->gmch_m, &m_n->gmch_n);
7077
7078 compute_m_n(pixel_clock, link_clock,
7079 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007080}
7081
Chris Wilsona7615032011-01-12 17:04:08 +00007082static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7083{
Jani Nikulad330a952014-01-21 11:24:25 +02007084 if (i915.panel_use_ssc >= 0)
7085 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007086 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007087 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007088}
7089
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007090static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007091{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007092 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007093}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007094
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007095static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7096{
7097 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007098}
7099
Daniel Vetterf47709a2013-03-28 10:42:02 +01007100static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007101 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007102 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08007103{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007104 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007105 u32 fp, fp2 = 0;
7106
7107 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007108 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007109 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007110 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007111 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007112 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007113 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007114 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007115 }
7116
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007117 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007118
Daniel Vetterf47709a2013-03-28 10:42:02 +01007119 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007120 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007121 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007122 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007123 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007124 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007125 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007126 }
7127}
7128
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007129static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7130 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007131{
7132 u32 reg_val;
7133
7134 /*
7135 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7136 * and set it to a reasonable value instead.
7137 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007138 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007139 reg_val &= 0xffffff00;
7140 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007141 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007142
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007143 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007144 reg_val &= 0x8cffffff;
7145 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007146 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007147
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007148 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007149 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007150 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007151
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007152 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007153 reg_val &= 0x00ffffff;
7154 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007155 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007156}
7157
Daniel Vetterb5518422013-05-03 11:49:48 +02007158static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7159 struct intel_link_m_n *m_n)
7160{
7161 struct drm_device *dev = crtc->base.dev;
7162 struct drm_i915_private *dev_priv = dev->dev_private;
7163 int pipe = crtc->pipe;
7164
Daniel Vettere3b95f12013-05-03 11:49:49 +02007165 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7166 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7167 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7168 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007169}
7170
7171static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007172 struct intel_link_m_n *m_n,
7173 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007174{
7175 struct drm_device *dev = crtc->base.dev;
7176 struct drm_i915_private *dev_priv = dev->dev_private;
7177 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007178 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007179
7180 if (INTEL_INFO(dev)->gen >= 5) {
7181 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7182 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7183 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7184 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007185 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7186 * for gen < 8) and if DRRS is supported (to make sure the
7187 * registers are not unnecessarily accessed).
7188 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307189 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007190 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007191 I915_WRITE(PIPE_DATA_M2(transcoder),
7192 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7193 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7194 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7195 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7196 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007197 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007198 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7199 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7200 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7201 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007202 }
7203}
7204
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307205void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007206{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307207 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7208
7209 if (m_n == M1_N1) {
7210 dp_m_n = &crtc->config->dp_m_n;
7211 dp_m2_n2 = &crtc->config->dp_m2_n2;
7212 } else if (m_n == M2_N2) {
7213
7214 /*
7215 * M2_N2 registers are not supported. Hence m2_n2 divider value
7216 * needs to be programmed into M1_N1.
7217 */
7218 dp_m_n = &crtc->config->dp_m2_n2;
7219 } else {
7220 DRM_ERROR("Unsupported divider value\n");
7221 return;
7222 }
7223
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007224 if (crtc->config->has_pch_encoder)
7225 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007226 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307227 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007228}
7229
Daniel Vetter251ac862015-06-18 10:30:24 +02007230static void vlv_compute_dpll(struct intel_crtc *crtc,
7231 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007232{
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007233 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007234 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007235 if (crtc->pipe != PIPE_A)
7236 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007237
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007238 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjälä187a1c02016-04-18 20:34:04 +03007239 if (!pipe_config->has_dsi_encoder)
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007240 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7241 DPLL_EXT_BUFFER_ENABLE_VLV;
7242
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007243 pipe_config->dpll_hw_state.dpll_md =
7244 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7245}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007246
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007247static void chv_compute_dpll(struct intel_crtc *crtc,
7248 struct intel_crtc_state *pipe_config)
7249{
7250 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007251 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007252 if (crtc->pipe != PIPE_A)
7253 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7254
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007255 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjälä187a1c02016-04-18 20:34:04 +03007256 if (!pipe_config->has_dsi_encoder)
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007257 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7258
Ville Syrjälä03ed5cb2016-03-15 16:39:55 +02007259 pipe_config->dpll_hw_state.dpll_md =
7260 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007261}
7262
Ville Syrjäläd288f652014-10-28 13:20:22 +02007263static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007264 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007265{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007266 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007267 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007268 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007269 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007270 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007271 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007272
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007273 /* Enable Refclk */
7274 I915_WRITE(DPLL(pipe),
7275 pipe_config->dpll_hw_state.dpll &
7276 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7277
7278 /* No need to actually set up the DPLL with DSI */
7279 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7280 return;
7281
Ville Syrjäläa5805162015-05-26 20:42:30 +03007282 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007283
Ville Syrjäläd288f652014-10-28 13:20:22 +02007284 bestn = pipe_config->dpll.n;
7285 bestm1 = pipe_config->dpll.m1;
7286 bestm2 = pipe_config->dpll.m2;
7287 bestp1 = pipe_config->dpll.p1;
7288 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007289
Jesse Barnes89b667f2013-04-18 14:51:36 -07007290 /* See eDP HDMI DPIO driver vbios notes doc */
7291
7292 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007293 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007294 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007295
7296 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007297 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007298
7299 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007300 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007301 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007302 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007303
7304 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007305 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007306
7307 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007308 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7309 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7310 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007311 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007312
7313 /*
7314 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7315 * but we don't support that).
7316 * Note: don't use the DAC post divider as it seems unstable.
7317 */
7318 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007319 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007320
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007321 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007322 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007323
Jesse Barnes89b667f2013-04-18 14:51:36 -07007324 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007325 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007326 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7327 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007328 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007329 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007330 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007331 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007332 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007333
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007334 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007335 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007336 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007337 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007338 0x0df40000);
7339 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007340 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007341 0x0df70000);
7342 } else { /* HDMI or VGA */
7343 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007344 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007345 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007346 0x0df70000);
7347 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007348 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007349 0x0df40000);
7350 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007351
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007352 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007353 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007354 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7355 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007356 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007357 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007358
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007359 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007360 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007361}
7362
Ville Syrjäläd288f652014-10-28 13:20:22 +02007363static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007364 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007365{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007366 struct drm_device *dev = crtc->base.dev;
7367 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007368 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007369 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307370 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007371 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307372 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307373 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007374
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007375 /* Enable Refclk and SSC */
7376 I915_WRITE(DPLL(pipe),
7377 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7378
7379 /* No need to actually set up the DPLL with DSI */
7380 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7381 return;
7382
Ville Syrjäläd288f652014-10-28 13:20:22 +02007383 bestn = pipe_config->dpll.n;
7384 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7385 bestm1 = pipe_config->dpll.m1;
7386 bestm2 = pipe_config->dpll.m2 >> 22;
7387 bestp1 = pipe_config->dpll.p1;
7388 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307389 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307390 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307391 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007392
Ville Syrjäläa5805162015-05-26 20:42:30 +03007393 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007394
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007395 /* p1 and p2 divider */
7396 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7397 5 << DPIO_CHV_S1_DIV_SHIFT |
7398 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7399 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7400 1 << DPIO_CHV_K_DIV_SHIFT);
7401
7402 /* Feedback post-divider - m2 */
7403 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7404
7405 /* Feedback refclk divider - n and m1 */
7406 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7407 DPIO_CHV_M1_DIV_BY_2 |
7408 1 << DPIO_CHV_N_DIV_SHIFT);
7409
7410 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007411 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007412
7413 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307414 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7415 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7416 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7417 if (bestm2_frac)
7418 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7419 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007420
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307421 /* Program digital lock detect threshold */
7422 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7423 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7424 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7425 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7426 if (!bestm2_frac)
7427 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7428 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7429
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007430 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307431 if (vco == 5400000) {
7432 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7433 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7434 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7435 tribuf_calcntr = 0x9;
7436 } else if (vco <= 6200000) {
7437 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7438 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7439 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7440 tribuf_calcntr = 0x9;
7441 } else if (vco <= 6480000) {
7442 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7443 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7444 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7445 tribuf_calcntr = 0x8;
7446 } else {
7447 /* Not supported. Apply the same limits as in the max case */
7448 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7449 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7450 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7451 tribuf_calcntr = 0;
7452 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007453 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7454
Ville Syrjälä968040b2015-03-11 22:52:08 +02007455 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307456 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7457 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7458 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7459
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007460 /* AFC Recal */
7461 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7462 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7463 DPIO_AFC_RECAL);
7464
Ville Syrjäläa5805162015-05-26 20:42:30 +03007465 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007466}
7467
Ville Syrjäläd288f652014-10-28 13:20:22 +02007468/**
7469 * vlv_force_pll_on - forcibly enable just the PLL
7470 * @dev_priv: i915 private structure
7471 * @pipe: pipe PLL to enable
7472 * @dpll: PLL configuration
7473 *
7474 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7475 * in cases where we need the PLL enabled even when @pipe is not going to
7476 * be enabled.
7477 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007478int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7479 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007480{
7481 struct intel_crtc *crtc =
7482 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007483 struct intel_crtc_state *pipe_config;
7484
7485 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7486 if (!pipe_config)
7487 return -ENOMEM;
7488
7489 pipe_config->base.crtc = &crtc->base;
7490 pipe_config->pixel_multiplier = 1;
7491 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007492
7493 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007494 chv_compute_dpll(crtc, pipe_config);
7495 chv_prepare_pll(crtc, pipe_config);
7496 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007497 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007498 vlv_compute_dpll(crtc, pipe_config);
7499 vlv_prepare_pll(crtc, pipe_config);
7500 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007501 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007502
7503 kfree(pipe_config);
7504
7505 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007506}
7507
7508/**
7509 * vlv_force_pll_off - forcibly disable just the PLL
7510 * @dev_priv: i915 private structure
7511 * @pipe: pipe PLL to disable
7512 *
7513 * Disable the PLL for @pipe. To be used in cases where we need
7514 * the PLL enabled even when @pipe is not going to be enabled.
7515 */
7516void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7517{
7518 if (IS_CHERRYVIEW(dev))
7519 chv_disable_pll(to_i915(dev), pipe);
7520 else
7521 vlv_disable_pll(to_i915(dev), pipe);
7522}
7523
Daniel Vetter251ac862015-06-18 10:30:24 +02007524static void i9xx_compute_dpll(struct intel_crtc *crtc,
7525 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007526 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007527{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007528 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007529 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007530 u32 dpll;
7531 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007532 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007533
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007534 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307535
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007536 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7537 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007538
7539 dpll = DPLL_VGA_MODE_DIS;
7540
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007541 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007542 dpll |= DPLLB_MODE_LVDS;
7543 else
7544 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007545
Daniel Vetteref1b4602013-06-01 17:17:04 +02007546 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007547 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007548 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007549 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007550
7551 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007552 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007553
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007554 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007555 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007556
7557 /* compute bitmask from p1 value */
7558 if (IS_PINEVIEW(dev))
7559 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7560 else {
7561 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7562 if (IS_G4X(dev) && reduced_clock)
7563 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7564 }
7565 switch (clock->p2) {
7566 case 5:
7567 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7568 break;
7569 case 7:
7570 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7571 break;
7572 case 10:
7573 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7574 break;
7575 case 14:
7576 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7577 break;
7578 }
7579 if (INTEL_INFO(dev)->gen >= 4)
7580 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7581
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007582 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007583 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007584 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007585 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007586 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7587 else
7588 dpll |= PLL_REF_INPUT_DREFCLK;
7589
7590 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007591 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007592
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007593 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007594 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007595 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007596 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007597 }
7598}
7599
Daniel Vetter251ac862015-06-18 10:30:24 +02007600static void i8xx_compute_dpll(struct intel_crtc *crtc,
7601 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007602 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007603{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007604 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007605 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007606 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007607 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007608
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007609 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307610
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007611 dpll = DPLL_VGA_MODE_DIS;
7612
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007613 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007614 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7615 } else {
7616 if (clock->p1 == 2)
7617 dpll |= PLL_P1_DIVIDE_BY_TWO;
7618 else
7619 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7620 if (clock->p2 == 4)
7621 dpll |= PLL_P2_DIVIDE_BY_4;
7622 }
7623
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007624 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007625 dpll |= DPLL_DVO_2X_MODE;
7626
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007627 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007628 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007629 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7630 else
7631 dpll |= PLL_REF_INPUT_DREFCLK;
7632
7633 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007634 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007635}
7636
Daniel Vetter8a654f32013-06-01 17:16:22 +02007637static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007638{
7639 struct drm_device *dev = intel_crtc->base.dev;
7640 struct drm_i915_private *dev_priv = dev->dev_private;
7641 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007642 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007643 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007644 uint32_t crtc_vtotal, crtc_vblank_end;
7645 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007646
7647 /* We need to be careful not to changed the adjusted mode, for otherwise
7648 * the hw state checker will get angry at the mismatch. */
7649 crtc_vtotal = adjusted_mode->crtc_vtotal;
7650 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007651
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007652 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007653 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007654 crtc_vtotal -= 1;
7655 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007656
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007657 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007658 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7659 else
7660 vsyncshift = adjusted_mode->crtc_hsync_start -
7661 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007662 if (vsyncshift < 0)
7663 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007664 }
7665
7666 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007667 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007668
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007669 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007670 (adjusted_mode->crtc_hdisplay - 1) |
7671 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007672 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007673 (adjusted_mode->crtc_hblank_start - 1) |
7674 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007675 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007676 (adjusted_mode->crtc_hsync_start - 1) |
7677 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7678
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007679 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007680 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007681 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007682 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007683 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007684 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007685 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007686 (adjusted_mode->crtc_vsync_start - 1) |
7687 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7688
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007689 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7690 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7691 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7692 * bits. */
7693 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7694 (pipe == PIPE_B || pipe == PIPE_C))
7695 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7696
Jani Nikulabc58be62016-03-18 17:05:39 +02007697}
7698
7699static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7700{
7701 struct drm_device *dev = intel_crtc->base.dev;
7702 struct drm_i915_private *dev_priv = dev->dev_private;
7703 enum pipe pipe = intel_crtc->pipe;
7704
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007705 /* pipesrc controls the size that is scaled from, which should
7706 * always be the user's requested size.
7707 */
7708 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007709 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7710 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007711}
7712
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007713static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007714 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007715{
7716 struct drm_device *dev = crtc->base.dev;
7717 struct drm_i915_private *dev_priv = dev->dev_private;
7718 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7719 uint32_t tmp;
7720
7721 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007722 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7723 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007724 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007725 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7726 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007727 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007728 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7729 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007730
7731 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007732 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7733 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007734 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007735 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7736 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007737 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007738 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7739 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007740
7741 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007742 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7743 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7744 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007745 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007746}
7747
7748static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7749 struct intel_crtc_state *pipe_config)
7750{
7751 struct drm_device *dev = crtc->base.dev;
7752 struct drm_i915_private *dev_priv = dev->dev_private;
7753 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007754
7755 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007756 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7757 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7758
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007759 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7760 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007761}
7762
Daniel Vetterf6a83282014-02-11 15:28:57 -08007763void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007764 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007765{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007766 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7767 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7768 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7769 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007770
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007771 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7772 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7773 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7774 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007775
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007776 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007777 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007778
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007779 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7780 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007781
7782 mode->hsync = drm_mode_hsync(mode);
7783 mode->vrefresh = drm_mode_vrefresh(mode);
7784 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007785}
7786
Daniel Vetter84b046f2013-02-19 18:48:54 +01007787static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7788{
7789 struct drm_device *dev = intel_crtc->base.dev;
7790 struct drm_i915_private *dev_priv = dev->dev_private;
7791 uint32_t pipeconf;
7792
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007793 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007794
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007795 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7796 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7797 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007798
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007799 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007800 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007801
Daniel Vetterff9ce462013-04-24 14:57:17 +02007802 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007803 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007804 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007805 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007806 pipeconf |= PIPECONF_DITHER_EN |
7807 PIPECONF_DITHER_TYPE_SP;
7808
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007809 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007810 case 18:
7811 pipeconf |= PIPECONF_6BPC;
7812 break;
7813 case 24:
7814 pipeconf |= PIPECONF_8BPC;
7815 break;
7816 case 30:
7817 pipeconf |= PIPECONF_10BPC;
7818 break;
7819 default:
7820 /* Case prevented by intel_choose_pipe_bpp_dither. */
7821 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007822 }
7823 }
7824
7825 if (HAS_PIPE_CXSR(dev)) {
7826 if (intel_crtc->lowfreq_avail) {
7827 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7828 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7829 } else {
7830 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007831 }
7832 }
7833
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007834 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007835 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007836 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007837 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7838 else
7839 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7840 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007841 pipeconf |= PIPECONF_PROGRESSIVE;
7842
Wayne Boyer666a4532015-12-09 12:29:35 -08007843 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7844 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007845 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007846
Daniel Vetter84b046f2013-02-19 18:48:54 +01007847 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7848 POSTING_READ(PIPECONF(intel_crtc->pipe));
7849}
7850
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007851static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7852 struct intel_crtc_state *crtc_state)
7853{
7854 struct drm_device *dev = crtc->base.dev;
7855 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007856 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007857 int refclk = 48000;
7858
7859 memset(&crtc_state->dpll_hw_state, 0,
7860 sizeof(crtc_state->dpll_hw_state));
7861
7862 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7863 if (intel_panel_use_ssc(dev_priv)) {
7864 refclk = dev_priv->vbt.lvds_ssc_freq;
7865 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7866 }
7867
7868 limit = &intel_limits_i8xx_lvds;
7869 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7870 limit = &intel_limits_i8xx_dvo;
7871 } else {
7872 limit = &intel_limits_i8xx_dac;
7873 }
7874
7875 if (!crtc_state->clock_set &&
7876 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7877 refclk, NULL, &crtc_state->dpll)) {
7878 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7879 return -EINVAL;
7880 }
7881
7882 i8xx_compute_dpll(crtc, crtc_state, NULL);
7883
7884 return 0;
7885}
7886
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007887static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7888 struct intel_crtc_state *crtc_state)
7889{
7890 struct drm_device *dev = crtc->base.dev;
7891 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007892 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007893 int refclk = 96000;
7894
7895 memset(&crtc_state->dpll_hw_state, 0,
7896 sizeof(crtc_state->dpll_hw_state));
7897
7898 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7899 if (intel_panel_use_ssc(dev_priv)) {
7900 refclk = dev_priv->vbt.lvds_ssc_freq;
7901 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7902 }
7903
7904 if (intel_is_dual_link_lvds(dev))
7905 limit = &intel_limits_g4x_dual_channel_lvds;
7906 else
7907 limit = &intel_limits_g4x_single_channel_lvds;
7908 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7909 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7910 limit = &intel_limits_g4x_hdmi;
7911 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7912 limit = &intel_limits_g4x_sdvo;
7913 } else {
7914 /* The option is for other outputs */
7915 limit = &intel_limits_i9xx_sdvo;
7916 }
7917
7918 if (!crtc_state->clock_set &&
7919 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7920 refclk, NULL, &crtc_state->dpll)) {
7921 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7922 return -EINVAL;
7923 }
7924
7925 i9xx_compute_dpll(crtc, crtc_state, NULL);
7926
7927 return 0;
7928}
7929
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007930static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7931 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007932{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007933 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007934 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007935 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007936 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007937
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007938 memset(&crtc_state->dpll_hw_state, 0,
7939 sizeof(crtc_state->dpll_hw_state));
7940
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007941 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7942 if (intel_panel_use_ssc(dev_priv)) {
7943 refclk = dev_priv->vbt.lvds_ssc_freq;
7944 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7945 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007946
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007947 limit = &intel_limits_pineview_lvds;
7948 } else {
7949 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007950 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007951
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007952 if (!crtc_state->clock_set &&
7953 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7954 refclk, NULL, &crtc_state->dpll)) {
7955 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7956 return -EINVAL;
7957 }
7958
7959 i9xx_compute_dpll(crtc, crtc_state, NULL);
7960
7961 return 0;
7962}
7963
7964static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7965 struct intel_crtc_state *crtc_state)
7966{
7967 struct drm_device *dev = crtc->base.dev;
7968 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007969 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007970 int refclk = 96000;
7971
7972 memset(&crtc_state->dpll_hw_state, 0,
7973 sizeof(crtc_state->dpll_hw_state));
7974
7975 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7976 if (intel_panel_use_ssc(dev_priv)) {
7977 refclk = dev_priv->vbt.lvds_ssc_freq;
7978 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007979 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007980
7981 limit = &intel_limits_i9xx_lvds;
7982 } else {
7983 limit = &intel_limits_i9xx_sdvo;
7984 }
7985
7986 if (!crtc_state->clock_set &&
7987 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7988 refclk, NULL, &crtc_state->dpll)) {
7989 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7990 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007991 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007992
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007993 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07007994
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007995 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007996}
7997
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02007998static int chv_crtc_compute_clock(struct intel_crtc *crtc,
7999 struct intel_crtc_state *crtc_state)
8000{
8001 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008002 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008003
8004 memset(&crtc_state->dpll_hw_state, 0,
8005 sizeof(crtc_state->dpll_hw_state));
8006
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008007 if (!crtc_state->clock_set &&
8008 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8009 refclk, NULL, &crtc_state->dpll)) {
8010 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8011 return -EINVAL;
8012 }
8013
8014 chv_compute_dpll(crtc, crtc_state);
8015
8016 return 0;
8017}
8018
8019static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8020 struct intel_crtc_state *crtc_state)
8021{
8022 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008023 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008024
8025 memset(&crtc_state->dpll_hw_state, 0,
8026 sizeof(crtc_state->dpll_hw_state));
8027
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008028 if (!crtc_state->clock_set &&
8029 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8030 refclk, NULL, &crtc_state->dpll)) {
8031 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8032 return -EINVAL;
8033 }
8034
8035 vlv_compute_dpll(crtc, crtc_state);
8036
8037 return 0;
8038}
8039
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008040static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008041 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008042{
8043 struct drm_device *dev = crtc->base.dev;
8044 struct drm_i915_private *dev_priv = dev->dev_private;
8045 uint32_t tmp;
8046
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008047 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8048 return;
8049
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008050 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008051 if (!(tmp & PFIT_ENABLE))
8052 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008053
Daniel Vetter06922822013-07-11 13:35:40 +02008054 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008055 if (INTEL_INFO(dev)->gen < 4) {
8056 if (crtc->pipe != PIPE_B)
8057 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008058 } else {
8059 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8060 return;
8061 }
8062
Daniel Vetter06922822013-07-11 13:35:40 +02008063 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008064 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008065}
8066
Jesse Barnesacbec812013-09-20 11:29:32 -07008067static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008068 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008069{
8070 struct drm_device *dev = crtc->base.dev;
8071 struct drm_i915_private *dev_priv = dev->dev_private;
8072 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008073 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07008074 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008075 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008076
Ville Syrjäläb5219732016-03-15 16:40:01 +02008077 /* In case of DSI, DPLL will not be used */
8078 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308079 return;
8080
Ville Syrjäläa5805162015-05-26 20:42:30 +03008081 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008082 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008083 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008084
8085 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8086 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8087 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8088 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8089 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8090
Imre Deakdccbea32015-06-22 23:35:51 +03008091 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008092}
8093
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008094static void
8095i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8096 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008097{
8098 struct drm_device *dev = crtc->base.dev;
8099 struct drm_i915_private *dev_priv = dev->dev_private;
8100 u32 val, base, offset;
8101 int pipe = crtc->pipe, plane = crtc->plane;
8102 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008103 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008104 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008105 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008106
Damien Lespiau42a7b082015-02-05 19:35:13 +00008107 val = I915_READ(DSPCNTR(plane));
8108 if (!(val & DISPLAY_PLANE_ENABLE))
8109 return;
8110
Damien Lespiaud9806c92015-01-21 14:07:19 +00008111 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008112 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008113 DRM_DEBUG_KMS("failed to alloc fb\n");
8114 return;
8115 }
8116
Damien Lespiau1b842c82015-01-21 13:50:54 +00008117 fb = &intel_fb->base;
8118
Daniel Vetter18c52472015-02-10 17:16:09 +00008119 if (INTEL_INFO(dev)->gen >= 4) {
8120 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008121 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008122 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8123 }
8124 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008125
8126 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008127 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008128 fb->pixel_format = fourcc;
8129 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008130
8131 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008132 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008133 offset = I915_READ(DSPTILEOFF(plane));
8134 else
8135 offset = I915_READ(DSPLINOFF(plane));
8136 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8137 } else {
8138 base = I915_READ(DSPADDR(plane));
8139 }
8140 plane_config->base = base;
8141
8142 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008143 fb->width = ((val >> 16) & 0xfff) + 1;
8144 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008145
8146 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008147 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008148
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008149 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008150 fb->pixel_format,
8151 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008152
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008153 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008154
Damien Lespiau2844a922015-01-20 12:51:48 +00008155 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8156 pipe_name(pipe), plane, fb->width, fb->height,
8157 fb->bits_per_pixel, base, fb->pitches[0],
8158 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008159
Damien Lespiau2d140302015-02-05 17:22:18 +00008160 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008161}
8162
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008163static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008164 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008165{
8166 struct drm_device *dev = crtc->base.dev;
8167 struct drm_i915_private *dev_priv = dev->dev_private;
8168 int pipe = pipe_config->cpu_transcoder;
8169 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008170 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008171 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008172 int refclk = 100000;
8173
Ville Syrjäläb5219732016-03-15 16:40:01 +02008174 /* In case of DSI, DPLL will not be used */
8175 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8176 return;
8177
Ville Syrjäläa5805162015-05-26 20:42:30 +03008178 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008179 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8180 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8181 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8182 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008183 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008184 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008185
8186 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008187 clock.m2 = (pll_dw0 & 0xff) << 22;
8188 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8189 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008190 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8191 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8192 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8193
Imre Deakdccbea32015-06-22 23:35:51 +03008194 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008195}
8196
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008197static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008198 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008199{
8200 struct drm_device *dev = crtc->base.dev;
8201 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008202 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008203 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008204 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008205
Imre Deak17290502016-02-12 18:55:11 +02008206 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8207 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008208 return false;
8209
Daniel Vettere143a212013-07-04 12:01:15 +02008210 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008211 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008212
Imre Deak17290502016-02-12 18:55:11 +02008213 ret = false;
8214
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008215 tmp = I915_READ(PIPECONF(crtc->pipe));
8216 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008217 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008218
Wayne Boyer666a4532015-12-09 12:29:35 -08008219 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008220 switch (tmp & PIPECONF_BPC_MASK) {
8221 case PIPECONF_6BPC:
8222 pipe_config->pipe_bpp = 18;
8223 break;
8224 case PIPECONF_8BPC:
8225 pipe_config->pipe_bpp = 24;
8226 break;
8227 case PIPECONF_10BPC:
8228 pipe_config->pipe_bpp = 30;
8229 break;
8230 default:
8231 break;
8232 }
8233 }
8234
Wayne Boyer666a4532015-12-09 12:29:35 -08008235 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8236 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008237 pipe_config->limited_color_range = true;
8238
Ville Syrjälä282740f2013-09-04 18:30:03 +03008239 if (INTEL_INFO(dev)->gen < 4)
8240 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8241
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008242 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008243 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008244
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008245 i9xx_get_pfit_config(crtc, pipe_config);
8246
Daniel Vetter6c49f242013-06-06 12:45:25 +02008247 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008248 /* No way to read it out on pipes B and C */
8249 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8250 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8251 else
8252 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008253 pipe_config->pixel_multiplier =
8254 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8255 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008256 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008257 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8258 tmp = I915_READ(DPLL(crtc->pipe));
8259 pipe_config->pixel_multiplier =
8260 ((tmp & SDVO_MULTIPLIER_MASK)
8261 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8262 } else {
8263 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8264 * port and will be fixed up in the encoder->get_config
8265 * function. */
8266 pipe_config->pixel_multiplier = 1;
8267 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008268 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008269 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008270 /*
8271 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8272 * on 830. Filter it out here so that we don't
8273 * report errors due to that.
8274 */
8275 if (IS_I830(dev))
8276 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8277
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008278 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8279 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008280 } else {
8281 /* Mask out read-only status bits. */
8282 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8283 DPLL_PORTC_READY_MASK |
8284 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008285 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008286
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008287 if (IS_CHERRYVIEW(dev))
8288 chv_crtc_clock_get(crtc, pipe_config);
8289 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008290 vlv_crtc_clock_get(crtc, pipe_config);
8291 else
8292 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008293
Ville Syrjälä0f646142015-08-26 19:39:18 +03008294 /*
8295 * Normally the dotclock is filled in by the encoder .get_config()
8296 * but in case the pipe is enabled w/o any ports we need a sane
8297 * default.
8298 */
8299 pipe_config->base.adjusted_mode.crtc_clock =
8300 pipe_config->port_clock / pipe_config->pixel_multiplier;
8301
Imre Deak17290502016-02-12 18:55:11 +02008302 ret = true;
8303
8304out:
8305 intel_display_power_put(dev_priv, power_domain);
8306
8307 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008308}
8309
Paulo Zanonidde86e22012-12-01 12:04:25 -02008310static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008311{
8312 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008313 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008314 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008315 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008316 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008317 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008318 bool has_ck505 = false;
8319 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008320
8321 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008322 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008323 switch (encoder->type) {
8324 case INTEL_OUTPUT_LVDS:
8325 has_panel = true;
8326 has_lvds = true;
8327 break;
8328 case INTEL_OUTPUT_EDP:
8329 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008330 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008331 has_cpu_edp = true;
8332 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008333 default:
8334 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008335 }
8336 }
8337
Keith Packard99eb6a02011-09-26 14:29:12 -07008338 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008339 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008340 can_ssc = has_ck505;
8341 } else {
8342 has_ck505 = false;
8343 can_ssc = true;
8344 }
8345
Imre Deak2de69052013-05-08 13:14:04 +03008346 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8347 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008348
8349 /* Ironlake: try to setup display ref clock before DPLL
8350 * enabling. This is only under driver's control after
8351 * PCH B stepping, previous chipset stepping should be
8352 * ignoring this setting.
8353 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008354 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008355
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008356 /* As we must carefully and slowly disable/enable each source in turn,
8357 * compute the final state we want first and check if we need to
8358 * make any changes at all.
8359 */
8360 final = val;
8361 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008362 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008363 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008364 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008365 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8366
8367 final &= ~DREF_SSC_SOURCE_MASK;
8368 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8369 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008370
Keith Packard199e5d72011-09-22 12:01:57 -07008371 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008372 final |= DREF_SSC_SOURCE_ENABLE;
8373
8374 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8375 final |= DREF_SSC1_ENABLE;
8376
8377 if (has_cpu_edp) {
8378 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8379 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8380 else
8381 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8382 } else
8383 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8384 } else {
8385 final |= DREF_SSC_SOURCE_DISABLE;
8386 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8387 }
8388
8389 if (final == val)
8390 return;
8391
8392 /* Always enable nonspread source */
8393 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8394
8395 if (has_ck505)
8396 val |= DREF_NONSPREAD_CK505_ENABLE;
8397 else
8398 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8399
8400 if (has_panel) {
8401 val &= ~DREF_SSC_SOURCE_MASK;
8402 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008403
Keith Packard199e5d72011-09-22 12:01:57 -07008404 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008405 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008406 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008407 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008408 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008409 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008410
8411 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008412 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008413 POSTING_READ(PCH_DREF_CONTROL);
8414 udelay(200);
8415
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008416 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008417
8418 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008419 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008420 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008421 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008422 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008423 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008424 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008425 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008426 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008427
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008428 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008429 POSTING_READ(PCH_DREF_CONTROL);
8430 udelay(200);
8431 } else {
8432 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8433
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008434 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008435
8436 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008437 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008438
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008439 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008440 POSTING_READ(PCH_DREF_CONTROL);
8441 udelay(200);
8442
8443 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008444 val &= ~DREF_SSC_SOURCE_MASK;
8445 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008446
8447 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008448 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008449
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008450 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008451 POSTING_READ(PCH_DREF_CONTROL);
8452 udelay(200);
8453 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008454
8455 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008456}
8457
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008458static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008459{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008460 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008461
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008462 tmp = I915_READ(SOUTH_CHICKEN2);
8463 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8464 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008465
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008466 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8467 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8468 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008469
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008470 tmp = I915_READ(SOUTH_CHICKEN2);
8471 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8472 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008473
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008474 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8475 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8476 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008477}
8478
8479/* WaMPhyProgramming:hsw */
8480static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8481{
8482 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008483
8484 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8485 tmp &= ~(0xFF << 24);
8486 tmp |= (0x12 << 24);
8487 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8488
Paulo Zanonidde86e22012-12-01 12:04:25 -02008489 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8490 tmp |= (1 << 11);
8491 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8492
8493 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8494 tmp |= (1 << 11);
8495 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8496
Paulo Zanonidde86e22012-12-01 12:04:25 -02008497 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8498 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8499 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8500
8501 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8502 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8503 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8504
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008505 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8506 tmp &= ~(7 << 13);
8507 tmp |= (5 << 13);
8508 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008509
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008510 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8511 tmp &= ~(7 << 13);
8512 tmp |= (5 << 13);
8513 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008514
8515 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8516 tmp &= ~0xFF;
8517 tmp |= 0x1C;
8518 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8519
8520 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8521 tmp &= ~0xFF;
8522 tmp |= 0x1C;
8523 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8524
8525 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8526 tmp &= ~(0xFF << 16);
8527 tmp |= (0x1C << 16);
8528 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8529
8530 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8531 tmp &= ~(0xFF << 16);
8532 tmp |= (0x1C << 16);
8533 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8534
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008535 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8536 tmp |= (1 << 27);
8537 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008538
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008539 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8540 tmp |= (1 << 27);
8541 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008542
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008543 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8544 tmp &= ~(0xF << 28);
8545 tmp |= (4 << 28);
8546 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008547
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008548 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8549 tmp &= ~(0xF << 28);
8550 tmp |= (4 << 28);
8551 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008552}
8553
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008554/* Implements 3 different sequences from BSpec chapter "Display iCLK
8555 * Programming" based on the parameters passed:
8556 * - Sequence to enable CLKOUT_DP
8557 * - Sequence to enable CLKOUT_DP without spread
8558 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8559 */
8560static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8561 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008562{
8563 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008564 uint32_t reg, tmp;
8565
8566 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8567 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008568 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008569 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008570
Ville Syrjäläa5805162015-05-26 20:42:30 +03008571 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008572
8573 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8574 tmp &= ~SBI_SSCCTL_DISABLE;
8575 tmp |= SBI_SSCCTL_PATHALT;
8576 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8577
8578 udelay(24);
8579
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008580 if (with_spread) {
8581 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8582 tmp &= ~SBI_SSCCTL_PATHALT;
8583 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008584
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008585 if (with_fdi) {
8586 lpt_reset_fdi_mphy(dev_priv);
8587 lpt_program_fdi_mphy(dev_priv);
8588 }
8589 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008590
Ville Syrjäläc2699522015-08-27 23:55:59 +03008591 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008592 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8593 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8594 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008595
Ville Syrjäläa5805162015-05-26 20:42:30 +03008596 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008597}
8598
Paulo Zanoni47701c32013-07-23 11:19:25 -03008599/* Sequence to disable CLKOUT_DP */
8600static void lpt_disable_clkout_dp(struct drm_device *dev)
8601{
8602 struct drm_i915_private *dev_priv = dev->dev_private;
8603 uint32_t reg, tmp;
8604
Ville Syrjäläa5805162015-05-26 20:42:30 +03008605 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008606
Ville Syrjäläc2699522015-08-27 23:55:59 +03008607 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008608 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8609 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8610 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8611
8612 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8613 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8614 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8615 tmp |= SBI_SSCCTL_PATHALT;
8616 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8617 udelay(32);
8618 }
8619 tmp |= SBI_SSCCTL_DISABLE;
8620 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8621 }
8622
Ville Syrjäläa5805162015-05-26 20:42:30 +03008623 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008624}
8625
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008626#define BEND_IDX(steps) ((50 + (steps)) / 5)
8627
8628static const uint16_t sscdivintphase[] = {
8629 [BEND_IDX( 50)] = 0x3B23,
8630 [BEND_IDX( 45)] = 0x3B23,
8631 [BEND_IDX( 40)] = 0x3C23,
8632 [BEND_IDX( 35)] = 0x3C23,
8633 [BEND_IDX( 30)] = 0x3D23,
8634 [BEND_IDX( 25)] = 0x3D23,
8635 [BEND_IDX( 20)] = 0x3E23,
8636 [BEND_IDX( 15)] = 0x3E23,
8637 [BEND_IDX( 10)] = 0x3F23,
8638 [BEND_IDX( 5)] = 0x3F23,
8639 [BEND_IDX( 0)] = 0x0025,
8640 [BEND_IDX( -5)] = 0x0025,
8641 [BEND_IDX(-10)] = 0x0125,
8642 [BEND_IDX(-15)] = 0x0125,
8643 [BEND_IDX(-20)] = 0x0225,
8644 [BEND_IDX(-25)] = 0x0225,
8645 [BEND_IDX(-30)] = 0x0325,
8646 [BEND_IDX(-35)] = 0x0325,
8647 [BEND_IDX(-40)] = 0x0425,
8648 [BEND_IDX(-45)] = 0x0425,
8649 [BEND_IDX(-50)] = 0x0525,
8650};
8651
8652/*
8653 * Bend CLKOUT_DP
8654 * steps -50 to 50 inclusive, in steps of 5
8655 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8656 * change in clock period = -(steps / 10) * 5.787 ps
8657 */
8658static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8659{
8660 uint32_t tmp;
8661 int idx = BEND_IDX(steps);
8662
8663 if (WARN_ON(steps % 5 != 0))
8664 return;
8665
8666 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8667 return;
8668
8669 mutex_lock(&dev_priv->sb_lock);
8670
8671 if (steps % 10 != 0)
8672 tmp = 0xAAAAAAAB;
8673 else
8674 tmp = 0x00000000;
8675 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8676
8677 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8678 tmp &= 0xffff0000;
8679 tmp |= sscdivintphase[idx];
8680 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8681
8682 mutex_unlock(&dev_priv->sb_lock);
8683}
8684
8685#undef BEND_IDX
8686
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008687static void lpt_init_pch_refclk(struct drm_device *dev)
8688{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008689 struct intel_encoder *encoder;
8690 bool has_vga = false;
8691
Damien Lespiaub2784e12014-08-05 11:29:37 +01008692 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008693 switch (encoder->type) {
8694 case INTEL_OUTPUT_ANALOG:
8695 has_vga = true;
8696 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008697 default:
8698 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008699 }
8700 }
8701
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008702 if (has_vga) {
8703 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008704 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008705 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008706 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008707 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008708}
8709
Paulo Zanonidde86e22012-12-01 12:04:25 -02008710/*
8711 * Initialize reference clocks when the driver loads
8712 */
8713void intel_init_pch_refclk(struct drm_device *dev)
8714{
8715 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8716 ironlake_init_pch_refclk(dev);
8717 else if (HAS_PCH_LPT(dev))
8718 lpt_init_pch_refclk(dev);
8719}
8720
Daniel Vetter6ff93602013-04-19 11:24:36 +02008721static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008722{
8723 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8724 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8725 int pipe = intel_crtc->pipe;
8726 uint32_t val;
8727
Daniel Vetter78114072013-06-13 00:54:57 +02008728 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008729
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008730 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008731 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008732 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008733 break;
8734 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008735 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008736 break;
8737 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008738 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008739 break;
8740 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008741 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008742 break;
8743 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008744 /* Case prevented by intel_choose_pipe_bpp_dither. */
8745 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008746 }
8747
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008748 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008749 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8750
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008751 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008752 val |= PIPECONF_INTERLACED_ILK;
8753 else
8754 val |= PIPECONF_PROGRESSIVE;
8755
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008756 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008757 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008758
Paulo Zanonic8203562012-09-12 10:06:29 -03008759 I915_WRITE(PIPECONF(pipe), val);
8760 POSTING_READ(PIPECONF(pipe));
8761}
8762
Daniel Vetter6ff93602013-04-19 11:24:36 +02008763static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008764{
Jani Nikula391bf042016-03-18 17:05:40 +02008765 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008766 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008767 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008768 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008769
Jani Nikula391bf042016-03-18 17:05:40 +02008770 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008771 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8772
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008773 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008774 val |= PIPECONF_INTERLACED_ILK;
8775 else
8776 val |= PIPECONF_PROGRESSIVE;
8777
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008778 I915_WRITE(PIPECONF(cpu_transcoder), val);
8779 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008780}
8781
Jani Nikula391bf042016-03-18 17:05:40 +02008782static void haswell_set_pipemisc(struct drm_crtc *crtc)
8783{
8784 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8785 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8786
8787 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8788 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008789
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008790 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008791 case 18:
8792 val |= PIPEMISC_DITHER_6_BPC;
8793 break;
8794 case 24:
8795 val |= PIPEMISC_DITHER_8_BPC;
8796 break;
8797 case 30:
8798 val |= PIPEMISC_DITHER_10_BPC;
8799 break;
8800 case 36:
8801 val |= PIPEMISC_DITHER_12_BPC;
8802 break;
8803 default:
8804 /* Case prevented by pipe_config_set_bpp. */
8805 BUG();
8806 }
8807
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008808 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008809 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8810
Jani Nikula391bf042016-03-18 17:05:40 +02008811 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008812 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008813}
8814
Paulo Zanonid4b19312012-11-29 11:29:32 -02008815int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8816{
8817 /*
8818 * Account for spread spectrum to avoid
8819 * oversubscribing the link. Max center spread
8820 * is 2.5%; use 5% for safety's sake.
8821 */
8822 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008823 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008824}
8825
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008826static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008827{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008828 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008829}
8830
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008831static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8832 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008833 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008834{
8835 struct drm_crtc *crtc = &intel_crtc->base;
8836 struct drm_device *dev = crtc->dev;
8837 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008838 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008839 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008840 struct drm_connector_state *connector_state;
8841 struct intel_encoder *encoder;
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008842 u32 dpll, fp, fp2;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008843 int factor, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008844 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008845
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008846 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008847 if (connector_state->crtc != crtc_state->base.crtc)
8848 continue;
8849
8850 encoder = to_intel_encoder(connector_state->best_encoder);
8851
8852 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008853 case INTEL_OUTPUT_LVDS:
8854 is_lvds = true;
8855 break;
8856 case INTEL_OUTPUT_SDVO:
8857 case INTEL_OUTPUT_HDMI:
8858 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008859 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008860 default:
8861 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008862 }
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008863 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008864
Chris Wilsonc1858122010-12-03 21:35:48 +00008865 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008866 factor = 21;
8867 if (is_lvds) {
8868 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008869 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008870 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008871 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008872 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008873 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008874
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008875 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008876
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008877 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8878 fp |= FP_CB_TUNE;
8879
8880 if (reduced_clock) {
8881 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8882
8883 if (reduced_clock->m < factor * reduced_clock->n)
8884 fp2 |= FP_CB_TUNE;
8885 } else {
8886 fp2 = fp;
8887 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008888
Chris Wilson5eddb702010-09-11 13:48:45 +01008889 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008890
Eric Anholta07d6782011-03-30 13:01:08 -07008891 if (is_lvds)
8892 dpll |= DPLLB_MODE_LVDS;
8893 else
8894 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008895
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008896 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008897 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008898
8899 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008900 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008901 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008902 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008903
Eric Anholta07d6782011-03-30 13:01:08 -07008904 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008905 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008906 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008907 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008908
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008909 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008910 case 5:
8911 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8912 break;
8913 case 7:
8914 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8915 break;
8916 case 10:
8917 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8918 break;
8919 case 14:
8920 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8921 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008922 }
8923
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008924 if (is_lvds && intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008925 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008926 else
8927 dpll |= PLL_REF_INPUT_DREFCLK;
8928
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008929 dpll |= DPLL_VCO_ENABLE;
8930
8931 crtc_state->dpll_hw_state.dpll = dpll;
8932 crtc_state->dpll_hw_state.fp0 = fp;
8933 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008934}
8935
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008936static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8937 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008938{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008939 struct drm_device *dev = crtc->base.dev;
8940 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008941 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008942 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008943 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008944 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008945 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008946
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008947 memset(&crtc_state->dpll_hw_state, 0,
8948 sizeof(crtc_state->dpll_hw_state));
8949
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008950 crtc->lowfreq_avail = false;
8951
8952 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8953 if (!crtc_state->has_pch_encoder)
8954 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008955
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008956 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8957 if (intel_panel_use_ssc(dev_priv)) {
8958 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8959 dev_priv->vbt.lvds_ssc_freq);
8960 refclk = dev_priv->vbt.lvds_ssc_freq;
8961 }
8962
8963 if (intel_is_dual_link_lvds(dev)) {
8964 if (refclk == 100000)
8965 limit = &intel_limits_ironlake_dual_lvds_100m;
8966 else
8967 limit = &intel_limits_ironlake_dual_lvds;
8968 } else {
8969 if (refclk == 100000)
8970 limit = &intel_limits_ironlake_single_lvds_100m;
8971 else
8972 limit = &intel_limits_ironlake_single_lvds;
8973 }
8974 } else {
8975 limit = &intel_limits_ironlake_dac;
8976 }
8977
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008978 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008979 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8980 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008981 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8982 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008983 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008984
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008985 ironlake_compute_dpll(crtc, crtc_state,
8986 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02008987
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008988 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
8989 if (pll == NULL) {
8990 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8991 pipe_name(crtc->pipe));
8992 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008993 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008994
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008995 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8996 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008997 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02008998
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008999 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009000}
9001
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009002static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9003 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009004{
9005 struct drm_device *dev = crtc->base.dev;
9006 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009007 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009008
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009009 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9010 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9011 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9012 & ~TU_SIZE_MASK;
9013 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9014 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9015 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9016}
9017
9018static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9019 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009020 struct intel_link_m_n *m_n,
9021 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009022{
9023 struct drm_device *dev = crtc->base.dev;
9024 struct drm_i915_private *dev_priv = dev->dev_private;
9025 enum pipe pipe = crtc->pipe;
9026
9027 if (INTEL_INFO(dev)->gen >= 5) {
9028 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9029 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9030 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9031 & ~TU_SIZE_MASK;
9032 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9033 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9034 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009035 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9036 * gen < 8) and if DRRS is supported (to make sure the
9037 * registers are not unnecessarily read).
9038 */
9039 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009040 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009041 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9042 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9043 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9044 & ~TU_SIZE_MASK;
9045 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9046 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9047 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9048 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009049 } else {
9050 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9051 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9052 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9053 & ~TU_SIZE_MASK;
9054 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9055 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9056 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9057 }
9058}
9059
9060void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009061 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009062{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009063 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009064 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9065 else
9066 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009067 &pipe_config->dp_m_n,
9068 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009069}
9070
Daniel Vetter72419202013-04-04 13:28:53 +02009071static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009072 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009073{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009074 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009075 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009076}
9077
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009078static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009079 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009080{
9081 struct drm_device *dev = crtc->base.dev;
9082 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009083 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9084 uint32_t ps_ctrl = 0;
9085 int id = -1;
9086 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009087
Chandra Kondurua1b22782015-04-07 15:28:45 -07009088 /* find scaler attached to this pipe */
9089 for (i = 0; i < crtc->num_scalers; i++) {
9090 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9091 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9092 id = i;
9093 pipe_config->pch_pfit.enabled = true;
9094 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9095 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9096 break;
9097 }
9098 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009099
Chandra Kondurua1b22782015-04-07 15:28:45 -07009100 scaler_state->scaler_id = id;
9101 if (id >= 0) {
9102 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9103 } else {
9104 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009105 }
9106}
9107
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009108static void
9109skylake_get_initial_plane_config(struct intel_crtc *crtc,
9110 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009111{
9112 struct drm_device *dev = crtc->base.dev;
9113 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009114 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009115 int pipe = crtc->pipe;
9116 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009117 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009118 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009119 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009120
Damien Lespiaud9806c92015-01-21 14:07:19 +00009121 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009122 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009123 DRM_DEBUG_KMS("failed to alloc fb\n");
9124 return;
9125 }
9126
Damien Lespiau1b842c82015-01-21 13:50:54 +00009127 fb = &intel_fb->base;
9128
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009129 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009130 if (!(val & PLANE_CTL_ENABLE))
9131 goto error;
9132
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009133 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9134 fourcc = skl_format_to_fourcc(pixel_format,
9135 val & PLANE_CTL_ORDER_RGBX,
9136 val & PLANE_CTL_ALPHA_MASK);
9137 fb->pixel_format = fourcc;
9138 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9139
Damien Lespiau40f46282015-02-27 11:15:21 +00009140 tiling = val & PLANE_CTL_TILED_MASK;
9141 switch (tiling) {
9142 case PLANE_CTL_TILED_LINEAR:
9143 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9144 break;
9145 case PLANE_CTL_TILED_X:
9146 plane_config->tiling = I915_TILING_X;
9147 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9148 break;
9149 case PLANE_CTL_TILED_Y:
9150 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9151 break;
9152 case PLANE_CTL_TILED_YF:
9153 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9154 break;
9155 default:
9156 MISSING_CASE(tiling);
9157 goto error;
9158 }
9159
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009160 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9161 plane_config->base = base;
9162
9163 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9164
9165 val = I915_READ(PLANE_SIZE(pipe, 0));
9166 fb->height = ((val >> 16) & 0xfff) + 1;
9167 fb->width = ((val >> 0) & 0x1fff) + 1;
9168
9169 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009170 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009171 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009172 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9173
9174 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009175 fb->pixel_format,
9176 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009177
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009178 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009179
9180 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9181 pipe_name(pipe), fb->width, fb->height,
9182 fb->bits_per_pixel, base, fb->pitches[0],
9183 plane_config->size);
9184
Damien Lespiau2d140302015-02-05 17:22:18 +00009185 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009186 return;
9187
9188error:
9189 kfree(fb);
9190}
9191
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009192static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009193 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009194{
9195 struct drm_device *dev = crtc->base.dev;
9196 struct drm_i915_private *dev_priv = dev->dev_private;
9197 uint32_t tmp;
9198
9199 tmp = I915_READ(PF_CTL(crtc->pipe));
9200
9201 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009202 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009203 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9204 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009205
9206 /* We currently do not free assignements of panel fitters on
9207 * ivb/hsw (since we don't use the higher upscaling modes which
9208 * differentiates them) so just WARN about this case for now. */
9209 if (IS_GEN7(dev)) {
9210 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9211 PF_PIPE_SEL_IVB(crtc->pipe));
9212 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009213 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009214}
9215
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009216static void
9217ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9218 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009219{
9220 struct drm_device *dev = crtc->base.dev;
9221 struct drm_i915_private *dev_priv = dev->dev_private;
9222 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009223 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009224 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009225 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009226 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009227 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009228
Damien Lespiau42a7b082015-02-05 19:35:13 +00009229 val = I915_READ(DSPCNTR(pipe));
9230 if (!(val & DISPLAY_PLANE_ENABLE))
9231 return;
9232
Damien Lespiaud9806c92015-01-21 14:07:19 +00009233 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009234 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009235 DRM_DEBUG_KMS("failed to alloc fb\n");
9236 return;
9237 }
9238
Damien Lespiau1b842c82015-01-21 13:50:54 +00009239 fb = &intel_fb->base;
9240
Daniel Vetter18c52472015-02-10 17:16:09 +00009241 if (INTEL_INFO(dev)->gen >= 4) {
9242 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009243 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009244 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9245 }
9246 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009247
9248 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009249 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009250 fb->pixel_format = fourcc;
9251 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009252
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009253 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009254 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009255 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009256 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009257 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009258 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009259 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009260 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009261 }
9262 plane_config->base = base;
9263
9264 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009265 fb->width = ((val >> 16) & 0xfff) + 1;
9266 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009267
9268 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009269 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009270
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009271 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009272 fb->pixel_format,
9273 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009274
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009275 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009276
Damien Lespiau2844a922015-01-20 12:51:48 +00009277 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9278 pipe_name(pipe), fb->width, fb->height,
9279 fb->bits_per_pixel, base, fb->pitches[0],
9280 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009281
Damien Lespiau2d140302015-02-05 17:22:18 +00009282 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009283}
9284
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009285static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009286 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009287{
9288 struct drm_device *dev = crtc->base.dev;
9289 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009290 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009291 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009292 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009293
Imre Deak17290502016-02-12 18:55:11 +02009294 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9295 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009296 return false;
9297
Daniel Vettere143a212013-07-04 12:01:15 +02009298 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009299 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009300
Imre Deak17290502016-02-12 18:55:11 +02009301 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009302 tmp = I915_READ(PIPECONF(crtc->pipe));
9303 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009304 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009305
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009306 switch (tmp & PIPECONF_BPC_MASK) {
9307 case PIPECONF_6BPC:
9308 pipe_config->pipe_bpp = 18;
9309 break;
9310 case PIPECONF_8BPC:
9311 pipe_config->pipe_bpp = 24;
9312 break;
9313 case PIPECONF_10BPC:
9314 pipe_config->pipe_bpp = 30;
9315 break;
9316 case PIPECONF_12BPC:
9317 pipe_config->pipe_bpp = 36;
9318 break;
9319 default:
9320 break;
9321 }
9322
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009323 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9324 pipe_config->limited_color_range = true;
9325
Daniel Vetterab9412b2013-05-03 11:49:46 +02009326 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009327 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009328 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009329
Daniel Vetter88adfff2013-03-28 10:42:01 +01009330 pipe_config->has_pch_encoder = true;
9331
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009332 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9333 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9334 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009335
9336 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009337
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009338 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03009339 /*
9340 * The pipe->pch transcoder and pch transcoder->pll
9341 * mapping is fixed.
9342 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009343 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009344 } else {
9345 tmp = I915_READ(PCH_DPLL_SEL);
9346 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009347 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009348 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009349 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009350 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009351
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009352 pipe_config->shared_dpll =
9353 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9354 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009355
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009356 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9357 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009358
9359 tmp = pipe_config->dpll_hw_state.dpll;
9360 pipe_config->pixel_multiplier =
9361 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9362 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009363
9364 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009365 } else {
9366 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009367 }
9368
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009369 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009370 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009371
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009372 ironlake_get_pfit_config(crtc, pipe_config);
9373
Imre Deak17290502016-02-12 18:55:11 +02009374 ret = true;
9375
9376out:
9377 intel_display_power_put(dev_priv, power_domain);
9378
9379 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009380}
9381
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009382static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9383{
9384 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009385 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009386
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009387 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009388 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009389 pipe_name(crtc->pipe));
9390
Rob Clarke2c719b2014-12-15 13:56:32 -05009391 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9392 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009393 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9394 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009395 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9396 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009397 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009398 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009399 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009400 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009401 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009402 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009403 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009404 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009405 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009406
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009407 /*
9408 * In theory we can still leave IRQs enabled, as long as only the HPD
9409 * interrupts remain enabled. We used to check for that, but since it's
9410 * gen-specific and since we only disable LCPLL after we fully disable
9411 * the interrupts, the check below should be enough.
9412 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009413 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009414}
9415
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009416static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9417{
9418 struct drm_device *dev = dev_priv->dev;
9419
9420 if (IS_HASWELL(dev))
9421 return I915_READ(D_COMP_HSW);
9422 else
9423 return I915_READ(D_COMP_BDW);
9424}
9425
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009426static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9427{
9428 struct drm_device *dev = dev_priv->dev;
9429
9430 if (IS_HASWELL(dev)) {
9431 mutex_lock(&dev_priv->rps.hw_lock);
9432 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9433 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009434 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009435 mutex_unlock(&dev_priv->rps.hw_lock);
9436 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009437 I915_WRITE(D_COMP_BDW, val);
9438 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009439 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009440}
9441
9442/*
9443 * This function implements pieces of two sequences from BSpec:
9444 * - Sequence for display software to disable LCPLL
9445 * - Sequence for display software to allow package C8+
9446 * The steps implemented here are just the steps that actually touch the LCPLL
9447 * register. Callers should take care of disabling all the display engine
9448 * functions, doing the mode unset, fixing interrupts, etc.
9449 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009450static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9451 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009452{
9453 uint32_t val;
9454
9455 assert_can_disable_lcpll(dev_priv);
9456
9457 val = I915_READ(LCPLL_CTL);
9458
9459 if (switch_to_fclk) {
9460 val |= LCPLL_CD_SOURCE_FCLK;
9461 I915_WRITE(LCPLL_CTL, val);
9462
9463 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9464 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9465 DRM_ERROR("Switching to FCLK failed\n");
9466
9467 val = I915_READ(LCPLL_CTL);
9468 }
9469
9470 val |= LCPLL_PLL_DISABLE;
9471 I915_WRITE(LCPLL_CTL, val);
9472 POSTING_READ(LCPLL_CTL);
9473
9474 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9475 DRM_ERROR("LCPLL still locked\n");
9476
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009477 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009478 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009479 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009480 ndelay(100);
9481
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009482 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9483 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009484 DRM_ERROR("D_COMP RCOMP still in progress\n");
9485
9486 if (allow_power_down) {
9487 val = I915_READ(LCPLL_CTL);
9488 val |= LCPLL_POWER_DOWN_ALLOW;
9489 I915_WRITE(LCPLL_CTL, val);
9490 POSTING_READ(LCPLL_CTL);
9491 }
9492}
9493
9494/*
9495 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9496 * source.
9497 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009498static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009499{
9500 uint32_t val;
9501
9502 val = I915_READ(LCPLL_CTL);
9503
9504 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9505 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9506 return;
9507
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009508 /*
9509 * Make sure we're not on PC8 state before disabling PC8, otherwise
9510 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009511 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009512 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009513
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009514 if (val & LCPLL_POWER_DOWN_ALLOW) {
9515 val &= ~LCPLL_POWER_DOWN_ALLOW;
9516 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009517 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009518 }
9519
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009520 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009521 val |= D_COMP_COMP_FORCE;
9522 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009523 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009524
9525 val = I915_READ(LCPLL_CTL);
9526 val &= ~LCPLL_PLL_DISABLE;
9527 I915_WRITE(LCPLL_CTL, val);
9528
9529 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9530 DRM_ERROR("LCPLL not locked yet\n");
9531
9532 if (val & LCPLL_CD_SOURCE_FCLK) {
9533 val = I915_READ(LCPLL_CTL);
9534 val &= ~LCPLL_CD_SOURCE_FCLK;
9535 I915_WRITE(LCPLL_CTL, val);
9536
9537 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9538 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9539 DRM_ERROR("Switching back to LCPLL failed\n");
9540 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009541
Mika Kuoppala59bad942015-01-16 11:34:40 +02009542 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009543 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009544}
9545
Paulo Zanoni765dab672014-03-07 20:08:18 -03009546/*
9547 * Package states C8 and deeper are really deep PC states that can only be
9548 * reached when all the devices on the system allow it, so even if the graphics
9549 * device allows PC8+, it doesn't mean the system will actually get to these
9550 * states. Our driver only allows PC8+ when going into runtime PM.
9551 *
9552 * The requirements for PC8+ are that all the outputs are disabled, the power
9553 * well is disabled and most interrupts are disabled, and these are also
9554 * requirements for runtime PM. When these conditions are met, we manually do
9555 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9556 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9557 * hang the machine.
9558 *
9559 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9560 * the state of some registers, so when we come back from PC8+ we need to
9561 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9562 * need to take care of the registers kept by RC6. Notice that this happens even
9563 * if we don't put the device in PCI D3 state (which is what currently happens
9564 * because of the runtime PM support).
9565 *
9566 * For more, read "Display Sequences for Package C8" on the hardware
9567 * documentation.
9568 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009569void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009570{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009571 struct drm_device *dev = dev_priv->dev;
9572 uint32_t val;
9573
Paulo Zanonic67a4702013-08-19 13:18:09 -03009574 DRM_DEBUG_KMS("Enabling package C8+\n");
9575
Ville Syrjäläc2699522015-08-27 23:55:59 +03009576 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009577 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9578 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9579 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9580 }
9581
9582 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009583 hsw_disable_lcpll(dev_priv, true, true);
9584}
9585
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009586void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009587{
9588 struct drm_device *dev = dev_priv->dev;
9589 uint32_t val;
9590
Paulo Zanonic67a4702013-08-19 13:18:09 -03009591 DRM_DEBUG_KMS("Disabling package C8+\n");
9592
9593 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009594 lpt_init_pch_refclk(dev);
9595
Ville Syrjäläc2699522015-08-27 23:55:59 +03009596 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009597 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9598 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9599 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9600 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009601}
9602
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009603static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309604{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009605 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009606 struct intel_atomic_state *old_intel_state =
9607 to_intel_atomic_state(old_state);
9608 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309609
Imre Deakc6c46962016-04-01 16:02:40 +03009610 broxton_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309611}
9612
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009613/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009614static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009615{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009616 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9617 struct drm_i915_private *dev_priv = state->dev->dev_private;
9618 struct drm_crtc *crtc;
9619 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009620 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009621 unsigned max_pixel_rate = 0, i;
9622 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009623
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009624 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9625 sizeof(intel_state->min_pixclk));
9626
9627 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009628 int pixel_rate;
9629
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009630 crtc_state = to_intel_crtc_state(cstate);
9631 if (!crtc_state->base.enable) {
9632 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009633 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009634 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009635
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009636 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009637
9638 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009639 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009640 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9641
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009642 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009643 }
9644
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009645 for_each_pipe(dev_priv, pipe)
9646 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9647
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009648 return max_pixel_rate;
9649}
9650
9651static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9652{
9653 struct drm_i915_private *dev_priv = dev->dev_private;
9654 uint32_t val, data;
9655 int ret;
9656
9657 if (WARN((I915_READ(LCPLL_CTL) &
9658 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9659 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9660 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9661 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9662 "trying to change cdclk frequency with cdclk not enabled\n"))
9663 return;
9664
9665 mutex_lock(&dev_priv->rps.hw_lock);
9666 ret = sandybridge_pcode_write(dev_priv,
9667 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9668 mutex_unlock(&dev_priv->rps.hw_lock);
9669 if (ret) {
9670 DRM_ERROR("failed to inform pcode about cdclk change\n");
9671 return;
9672 }
9673
9674 val = I915_READ(LCPLL_CTL);
9675 val |= LCPLL_CD_SOURCE_FCLK;
9676 I915_WRITE(LCPLL_CTL, val);
9677
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009678 if (wait_for_us(I915_READ(LCPLL_CTL) &
9679 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009680 DRM_ERROR("Switching to FCLK failed\n");
9681
9682 val = I915_READ(LCPLL_CTL);
9683 val &= ~LCPLL_CLK_FREQ_MASK;
9684
9685 switch (cdclk) {
9686 case 450000:
9687 val |= LCPLL_CLK_FREQ_450;
9688 data = 0;
9689 break;
9690 case 540000:
9691 val |= LCPLL_CLK_FREQ_54O_BDW;
9692 data = 1;
9693 break;
9694 case 337500:
9695 val |= LCPLL_CLK_FREQ_337_5_BDW;
9696 data = 2;
9697 break;
9698 case 675000:
9699 val |= LCPLL_CLK_FREQ_675_BDW;
9700 data = 3;
9701 break;
9702 default:
9703 WARN(1, "invalid cdclk frequency\n");
9704 return;
9705 }
9706
9707 I915_WRITE(LCPLL_CTL, val);
9708
9709 val = I915_READ(LCPLL_CTL);
9710 val &= ~LCPLL_CD_SOURCE_FCLK;
9711 I915_WRITE(LCPLL_CTL, val);
9712
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009713 if (wait_for_us((I915_READ(LCPLL_CTL) &
9714 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009715 DRM_ERROR("Switching back to LCPLL failed\n");
9716
9717 mutex_lock(&dev_priv->rps.hw_lock);
9718 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9719 mutex_unlock(&dev_priv->rps.hw_lock);
9720
Ville Syrjälä7f1052a2016-04-26 19:46:32 +03009721 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9722
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009723 intel_update_cdclk(dev);
9724
9725 WARN(cdclk != dev_priv->cdclk_freq,
9726 "cdclk requested %d kHz but got %d kHz\n",
9727 cdclk, dev_priv->cdclk_freq);
9728}
9729
Ville Syrjälä587c7912016-05-11 22:44:41 +03009730static int broadwell_calc_cdclk(int max_pixclk)
9731{
9732 if (max_pixclk > 540000)
9733 return 675000;
9734 else if (max_pixclk > 450000)
9735 return 540000;
9736 else if (max_pixclk > 337500)
9737 return 450000;
9738 else
9739 return 337500;
9740}
9741
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009742static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009743{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009744 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009745 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009746 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009747 int cdclk;
9748
9749 /*
9750 * FIXME should also account for plane ratio
9751 * once 64bpp pixel formats are supported.
9752 */
Ville Syrjälä587c7912016-05-11 22:44:41 +03009753 cdclk = broadwell_calc_cdclk(max_pixclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009754
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009755 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009756 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9757 cdclk, dev_priv->max_cdclk_freq);
9758 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009759 }
9760
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009761 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9762 if (!intel_state->active_crtcs)
Ville Syrjälä587c7912016-05-11 22:44:41 +03009763 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009764
9765 return 0;
9766}
9767
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009768static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009769{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009770 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009771 struct intel_atomic_state *old_intel_state =
9772 to_intel_atomic_state(old_state);
9773 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009774
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009775 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009776}
9777
Clint Taylorc89e39f2016-05-13 23:41:21 +03009778static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9779{
9780 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9781 struct drm_i915_private *dev_priv = to_i915(state->dev);
9782 const int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009783 int vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009784 int cdclk;
9785
9786 /*
9787 * FIXME should also account for plane ratio
9788 * once 64bpp pixel formats are supported.
9789 */
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009790 cdclk = skl_calc_cdclk(max_pixclk, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009791
9792 /*
9793 * FIXME move the cdclk caclulation to
9794 * compute_config() so we can fail gracegully.
9795 */
9796 if (cdclk > dev_priv->max_cdclk_freq) {
9797 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9798 cdclk, dev_priv->max_cdclk_freq);
9799 cdclk = dev_priv->max_cdclk_freq;
9800 }
9801
9802 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9803 if (!intel_state->active_crtcs)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009804 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009805
9806 return 0;
9807}
9808
9809static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9810{
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009811 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9812 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9813 unsigned int req_cdclk = intel_state->dev_cdclk;
9814 unsigned int req_vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009815
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009816 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009817}
9818
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009819static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9820 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009821{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009822 struct intel_encoder *intel_encoder =
9823 intel_ddi_get_crtc_new_encoder(crtc_state);
9824
9825 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9826 if (!intel_ddi_pll_select(crtc, crtc_state))
9827 return -EINVAL;
9828 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009829
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009830 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009831
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009832 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009833}
9834
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309835static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9836 enum port port,
9837 struct intel_crtc_state *pipe_config)
9838{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009839 enum intel_dpll_id id;
9840
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309841 switch (port) {
9842 case PORT_A:
9843 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009844 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309845 break;
9846 case PORT_B:
9847 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009848 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309849 break;
9850 case PORT_C:
9851 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009852 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309853 break;
9854 default:
9855 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009856 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309857 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009858
9859 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309860}
9861
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009862static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9863 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009864 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009865{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009866 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009867 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009868
9869 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9870 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9871
9872 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009873 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009874 id = DPLL_ID_SKL_DPLL0;
9875 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009876 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009877 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009878 break;
9879 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009880 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009881 break;
9882 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009883 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009884 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009885 default:
9886 MISSING_CASE(pipe_config->ddi_pll_sel);
9887 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009888 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009889
9890 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009891}
9892
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009893static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9894 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009895 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009896{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009897 enum intel_dpll_id id;
9898
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009899 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9900
9901 switch (pipe_config->ddi_pll_sel) {
9902 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009903 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009904 break;
9905 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009906 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009907 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009908 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009909 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009910 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009911 case PORT_CLK_SEL_LCPLL_810:
9912 id = DPLL_ID_LCPLL_810;
9913 break;
9914 case PORT_CLK_SEL_LCPLL_1350:
9915 id = DPLL_ID_LCPLL_1350;
9916 break;
9917 case PORT_CLK_SEL_LCPLL_2700:
9918 id = DPLL_ID_LCPLL_2700;
9919 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009920 default:
9921 MISSING_CASE(pipe_config->ddi_pll_sel);
9922 /* fall through */
9923 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009924 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009925 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009926
9927 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009928}
9929
Jani Nikulacf304292016-03-18 17:05:41 +02009930static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9931 struct intel_crtc_state *pipe_config,
9932 unsigned long *power_domain_mask)
9933{
9934 struct drm_device *dev = crtc->base.dev;
9935 struct drm_i915_private *dev_priv = dev->dev_private;
9936 enum intel_display_power_domain power_domain;
9937 u32 tmp;
9938
Imre Deakd9a7bc62016-05-12 16:18:50 +03009939 /*
9940 * The pipe->transcoder mapping is fixed with the exception of the eDP
9941 * transcoder handled below.
9942 */
Jani Nikulacf304292016-03-18 17:05:41 +02009943 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9944
9945 /*
9946 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9947 * consistency and less surprising code; it's in always on power).
9948 */
9949 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9950 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9951 enum pipe trans_edp_pipe;
9952 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9953 default:
9954 WARN(1, "unknown pipe linked to edp transcoder\n");
9955 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9956 case TRANS_DDI_EDP_INPUT_A_ON:
9957 trans_edp_pipe = PIPE_A;
9958 break;
9959 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9960 trans_edp_pipe = PIPE_B;
9961 break;
9962 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9963 trans_edp_pipe = PIPE_C;
9964 break;
9965 }
9966
9967 if (trans_edp_pipe == crtc->pipe)
9968 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9969 }
9970
9971 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9972 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9973 return false;
9974 *power_domain_mask |= BIT(power_domain);
9975
9976 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9977
9978 return tmp & PIPECONF_ENABLE;
9979}
9980
Jani Nikula4d1de972016-03-18 17:05:42 +02009981static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
9982 struct intel_crtc_state *pipe_config,
9983 unsigned long *power_domain_mask)
9984{
9985 struct drm_device *dev = crtc->base.dev;
9986 struct drm_i915_private *dev_priv = dev->dev_private;
9987 enum intel_display_power_domain power_domain;
9988 enum port port;
9989 enum transcoder cpu_transcoder;
9990 u32 tmp;
9991
9992 pipe_config->has_dsi_encoder = false;
9993
9994 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
9995 if (port == PORT_A)
9996 cpu_transcoder = TRANSCODER_DSI_A;
9997 else
9998 cpu_transcoder = TRANSCODER_DSI_C;
9999
10000 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10001 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10002 continue;
10003 *power_domain_mask |= BIT(power_domain);
10004
Imre Deakdb18b6a2016-03-24 12:41:40 +020010005 /*
10006 * The PLL needs to be enabled with a valid divider
10007 * configuration, otherwise accessing DSI registers will hang
10008 * the machine. See BSpec North Display Engine
10009 * registers/MIPI[BXT]. We can break out here early, since we
10010 * need the same DSI PLL to be enabled for both DSI ports.
10011 */
10012 if (!intel_dsi_pll_is_enabled(dev_priv))
10013 break;
10014
Jani Nikula4d1de972016-03-18 17:05:42 +020010015 /* XXX: this works for video mode only */
10016 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10017 if (!(tmp & DPI_ENABLE))
10018 continue;
10019
10020 tmp = I915_READ(MIPI_CTRL(port));
10021 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10022 continue;
10023
10024 pipe_config->cpu_transcoder = cpu_transcoder;
10025 pipe_config->has_dsi_encoder = true;
10026 break;
10027 }
10028
10029 return pipe_config->has_dsi_encoder;
10030}
10031
Daniel Vetter26804af2014-06-25 22:01:55 +030010032static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010033 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +030010034{
10035 struct drm_device *dev = crtc->base.dev;
10036 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010037 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +030010038 enum port port;
10039 uint32_t tmp;
10040
10041 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10042
10043 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10044
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070010045 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010046 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010047 else if (IS_BROXTON(dev))
10048 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010049 else
10050 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010051
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010052 pll = pipe_config->shared_dpll;
10053 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010054 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10055 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010056 }
10057
Daniel Vetter26804af2014-06-25 22:01:55 +030010058 /*
10059 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10060 * DDI E. So just check whether this pipe is wired to DDI E and whether
10061 * the PCH transcoder is on.
10062 */
Damien Lespiauca370452013-12-03 13:56:24 +000010063 if (INTEL_INFO(dev)->gen < 9 &&
10064 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010065 pipe_config->has_pch_encoder = true;
10066
10067 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10068 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10069 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10070
10071 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10072 }
10073}
10074
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010075static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010076 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010077{
10078 struct drm_device *dev = crtc->base.dev;
10079 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +020010080 enum intel_display_power_domain power_domain;
10081 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +020010082 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010083
Imre Deak17290502016-02-12 18:55:11 +020010084 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10085 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010086 return false;
Imre Deak17290502016-02-12 18:55:11 +020010087 power_domain_mask = BIT(power_domain);
10088
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010089 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010090
Jani Nikulacf304292016-03-18 17:05:41 +020010091 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +020010092
Jani Nikula4d1de972016-03-18 17:05:42 +020010093 if (IS_BROXTON(dev_priv)) {
10094 bxt_get_dsi_transcoder_state(crtc, pipe_config,
10095 &power_domain_mask);
10096 WARN_ON(active && pipe_config->has_dsi_encoder);
10097 if (pipe_config->has_dsi_encoder)
10098 active = true;
10099 }
10100
Jani Nikulacf304292016-03-18 17:05:41 +020010101 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010102 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010103
Jani Nikula4d1de972016-03-18 17:05:42 +020010104 if (!pipe_config->has_dsi_encoder) {
10105 haswell_get_ddi_port_state(crtc, pipe_config);
10106 intel_get_pipe_timings(crtc, pipe_config);
10107 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010108
Jani Nikulabc58be62016-03-18 17:05:39 +020010109 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010110
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010111 pipe_config->gamma_mode =
10112 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10113
Chandra Kondurua1b22782015-04-07 15:28:45 -070010114 if (INTEL_INFO(dev)->gen >= 9) {
10115 skl_init_scalers(dev, crtc, pipe_config);
10116 }
10117
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010118 if (INTEL_INFO(dev)->gen >= 9) {
10119 pipe_config->scaler_state.scaler_id = -1;
10120 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10121 }
10122
Imre Deak17290502016-02-12 18:55:11 +020010123 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10124 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10125 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010126 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010127 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010128 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010129 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010130 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010131
Jesse Barnese59150d2014-01-07 13:30:45 -080010132 if (IS_HASWELL(dev))
10133 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10134 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010135
Jani Nikula4d1de972016-03-18 17:05:42 +020010136 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10137 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010138 pipe_config->pixel_multiplier =
10139 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10140 } else {
10141 pipe_config->pixel_multiplier = 1;
10142 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010143
Imre Deak17290502016-02-12 18:55:11 +020010144out:
10145 for_each_power_domain(power_domain, power_domain_mask)
10146 intel_display_power_put(dev_priv, power_domain);
10147
Jani Nikulacf304292016-03-18 17:05:41 +020010148 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010149}
10150
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010151static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10152 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010153{
10154 struct drm_device *dev = crtc->dev;
10155 struct drm_i915_private *dev_priv = dev->dev_private;
10156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010157 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010158
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010159 if (plane_state && plane_state->visible) {
10160 unsigned int width = plane_state->base.crtc_w;
10161 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010162 unsigned int stride = roundup_pow_of_two(width) * 4;
10163
10164 switch (stride) {
10165 default:
10166 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10167 width, stride);
10168 stride = 256;
10169 /* fallthrough */
10170 case 256:
10171 case 512:
10172 case 1024:
10173 case 2048:
10174 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010175 }
10176
Ville Syrjälädc41c152014-08-13 11:57:05 +030010177 cntl |= CURSOR_ENABLE |
10178 CURSOR_GAMMA_ENABLE |
10179 CURSOR_FORMAT_ARGB |
10180 CURSOR_STRIDE(stride);
10181
10182 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010183 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010184
Ville Syrjälädc41c152014-08-13 11:57:05 +030010185 if (intel_crtc->cursor_cntl != 0 &&
10186 (intel_crtc->cursor_base != base ||
10187 intel_crtc->cursor_size != size ||
10188 intel_crtc->cursor_cntl != cntl)) {
10189 /* On these chipsets we can only modify the base/size/stride
10190 * whilst the cursor is disabled.
10191 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010192 I915_WRITE(CURCNTR(PIPE_A), 0);
10193 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010194 intel_crtc->cursor_cntl = 0;
10195 }
10196
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010197 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010198 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010199 intel_crtc->cursor_base = base;
10200 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010201
10202 if (intel_crtc->cursor_size != size) {
10203 I915_WRITE(CURSIZE, size);
10204 intel_crtc->cursor_size = size;
10205 }
10206
Chris Wilson4b0e3332014-05-30 16:35:26 +030010207 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010208 I915_WRITE(CURCNTR(PIPE_A), cntl);
10209 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010210 intel_crtc->cursor_cntl = cntl;
10211 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010212}
10213
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010214static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10215 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010216{
10217 struct drm_device *dev = crtc->dev;
10218 struct drm_i915_private *dev_priv = dev->dev_private;
10219 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10220 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010221 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010222
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010223 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010224 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010225 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010226 case 64:
10227 cntl |= CURSOR_MODE_64_ARGB_AX;
10228 break;
10229 case 128:
10230 cntl |= CURSOR_MODE_128_ARGB_AX;
10231 break;
10232 case 256:
10233 cntl |= CURSOR_MODE_256_ARGB_AX;
10234 break;
10235 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010236 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010237 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010238 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010239 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010240
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010241 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010242 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010243
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010244 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10245 cntl |= CURSOR_ROTATE_180;
10246 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010247
Chris Wilson4b0e3332014-05-30 16:35:26 +030010248 if (intel_crtc->cursor_cntl != cntl) {
10249 I915_WRITE(CURCNTR(pipe), cntl);
10250 POSTING_READ(CURCNTR(pipe));
10251 intel_crtc->cursor_cntl = cntl;
10252 }
10253
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010254 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010255 I915_WRITE(CURBASE(pipe), base);
10256 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010257
10258 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010259}
10260
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010261/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010262static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010263 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010264{
10265 struct drm_device *dev = crtc->dev;
10266 struct drm_i915_private *dev_priv = dev->dev_private;
10267 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10268 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010269 u32 base = intel_crtc->cursor_addr;
10270 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010271
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010272 if (plane_state) {
10273 int x = plane_state->base.crtc_x;
10274 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010275
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010276 if (x < 0) {
10277 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10278 x = -x;
10279 }
10280 pos |= x << CURSOR_X_SHIFT;
10281
10282 if (y < 0) {
10283 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10284 y = -y;
10285 }
10286 pos |= y << CURSOR_Y_SHIFT;
10287
10288 /* ILK+ do this automagically */
10289 if (HAS_GMCH_DISPLAY(dev) &&
10290 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10291 base += (plane_state->base.crtc_h *
10292 plane_state->base.crtc_w - 1) * 4;
10293 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010294 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010295
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010296 I915_WRITE(CURPOS(pipe), pos);
10297
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010298 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010299 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010300 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010301 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010302}
10303
Ville Syrjälädc41c152014-08-13 11:57:05 +030010304static bool cursor_size_ok(struct drm_device *dev,
10305 uint32_t width, uint32_t height)
10306{
10307 if (width == 0 || height == 0)
10308 return false;
10309
10310 /*
10311 * 845g/865g are special in that they are only limited by
10312 * the width of their cursors, the height is arbitrary up to
10313 * the precision of the register. Everything else requires
10314 * square cursors, limited to a few power-of-two sizes.
10315 */
10316 if (IS_845G(dev) || IS_I865G(dev)) {
10317 if ((width & 63) != 0)
10318 return false;
10319
10320 if (width > (IS_845G(dev) ? 64 : 512))
10321 return false;
10322
10323 if (height > 1023)
10324 return false;
10325 } else {
10326 switch (width | height) {
10327 case 256:
10328 case 128:
10329 if (IS_GEN2(dev))
10330 return false;
10331 case 64:
10332 break;
10333 default:
10334 return false;
10335 }
10336 }
10337
10338 return true;
10339}
10340
Jesse Barnes79e53942008-11-07 14:24:08 -080010341/* VESA 640x480x72Hz mode to set on the pipe */
10342static struct drm_display_mode load_detect_mode = {
10343 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10344 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10345};
10346
Daniel Vettera8bb6812014-02-10 18:00:39 +010010347struct drm_framebuffer *
10348__intel_framebuffer_create(struct drm_device *dev,
10349 struct drm_mode_fb_cmd2 *mode_cmd,
10350 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010351{
10352 struct intel_framebuffer *intel_fb;
10353 int ret;
10354
10355 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010356 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010357 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010358
10359 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010360 if (ret)
10361 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010362
10363 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010364
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010365err:
10366 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010367 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010368}
10369
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010370static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010371intel_framebuffer_create(struct drm_device *dev,
10372 struct drm_mode_fb_cmd2 *mode_cmd,
10373 struct drm_i915_gem_object *obj)
10374{
10375 struct drm_framebuffer *fb;
10376 int ret;
10377
10378 ret = i915_mutex_lock_interruptible(dev);
10379 if (ret)
10380 return ERR_PTR(ret);
10381 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10382 mutex_unlock(&dev->struct_mutex);
10383
10384 return fb;
10385}
10386
Chris Wilsond2dff872011-04-19 08:36:26 +010010387static u32
10388intel_framebuffer_pitch_for_width(int width, int bpp)
10389{
10390 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10391 return ALIGN(pitch, 64);
10392}
10393
10394static u32
10395intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10396{
10397 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010398 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010399}
10400
10401static struct drm_framebuffer *
10402intel_framebuffer_create_for_mode(struct drm_device *dev,
10403 struct drm_display_mode *mode,
10404 int depth, int bpp)
10405{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010406 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010407 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010408 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010409
Dave Gordond37cd8a2016-04-22 19:14:32 +010010410 obj = i915_gem_object_create(dev,
Chris Wilsond2dff872011-04-19 08:36:26 +010010411 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +010010412 if (IS_ERR(obj))
10413 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010414
10415 mode_cmd.width = mode->hdisplay;
10416 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010417 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10418 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010419 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010420
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010421 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10422 if (IS_ERR(fb))
10423 drm_gem_object_unreference_unlocked(&obj->base);
10424
10425 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010426}
10427
10428static struct drm_framebuffer *
10429mode_fits_in_fbdev(struct drm_device *dev,
10430 struct drm_display_mode *mode)
10431{
Daniel Vetter06957262015-08-10 13:34:08 +020010432#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010433 struct drm_i915_private *dev_priv = dev->dev_private;
10434 struct drm_i915_gem_object *obj;
10435 struct drm_framebuffer *fb;
10436
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010437 if (!dev_priv->fbdev)
10438 return NULL;
10439
10440 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010441 return NULL;
10442
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010443 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010444 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010445
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010446 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010447 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10448 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010449 return NULL;
10450
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010451 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010452 return NULL;
10453
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010454 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010455 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010456#else
10457 return NULL;
10458#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010459}
10460
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010461static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10462 struct drm_crtc *crtc,
10463 struct drm_display_mode *mode,
10464 struct drm_framebuffer *fb,
10465 int x, int y)
10466{
10467 struct drm_plane_state *plane_state;
10468 int hdisplay, vdisplay;
10469 int ret;
10470
10471 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10472 if (IS_ERR(plane_state))
10473 return PTR_ERR(plane_state);
10474
10475 if (mode)
10476 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10477 else
10478 hdisplay = vdisplay = 0;
10479
10480 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10481 if (ret)
10482 return ret;
10483 drm_atomic_set_fb_for_plane(plane_state, fb);
10484 plane_state->crtc_x = 0;
10485 plane_state->crtc_y = 0;
10486 plane_state->crtc_w = hdisplay;
10487 plane_state->crtc_h = vdisplay;
10488 plane_state->src_x = x << 16;
10489 plane_state->src_y = y << 16;
10490 plane_state->src_w = hdisplay << 16;
10491 plane_state->src_h = vdisplay << 16;
10492
10493 return 0;
10494}
10495
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010496bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010497 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010498 struct intel_load_detect_pipe *old,
10499 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010500{
10501 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010502 struct intel_encoder *intel_encoder =
10503 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010504 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010505 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010506 struct drm_crtc *crtc = NULL;
10507 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010508 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010509 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010510 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010511 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010512 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010513 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010514
Chris Wilsond2dff872011-04-19 08:36:26 +010010515 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010516 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010517 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010518
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010519 old->restore_state = NULL;
10520
Rob Clark51fd3712013-11-19 12:10:12 -050010521retry:
10522 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10523 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010524 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010525
Jesse Barnes79e53942008-11-07 14:24:08 -080010526 /*
10527 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010528 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010529 * - if the connector already has an assigned crtc, use it (but make
10530 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010531 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010532 * - try to find the first unused crtc that can drive this connector,
10533 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010534 */
10535
10536 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010537 if (connector->state->crtc) {
10538 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010539
Rob Clark51fd3712013-11-19 12:10:12 -050010540 ret = drm_modeset_lock(&crtc->mutex, ctx);
10541 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010542 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010543
10544 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010545 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010546 }
10547
10548 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010549 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010550 i++;
10551 if (!(encoder->possible_crtcs & (1 << i)))
10552 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010553
10554 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10555 if (ret)
10556 goto fail;
10557
10558 if (possible_crtc->state->enable) {
10559 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010560 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010561 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010562
10563 crtc = possible_crtc;
10564 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010565 }
10566
10567 /*
10568 * If we didn't find an unused CRTC, don't use any.
10569 */
10570 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010571 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010572 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010573 }
10574
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010575found:
10576 intel_crtc = to_intel_crtc(crtc);
10577
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010578 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10579 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010580 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010581
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010582 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010583 restore_state = drm_atomic_state_alloc(dev);
10584 if (!state || !restore_state) {
10585 ret = -ENOMEM;
10586 goto fail;
10587 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010588
10589 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010590 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010591
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010592 connector_state = drm_atomic_get_connector_state(state, connector);
10593 if (IS_ERR(connector_state)) {
10594 ret = PTR_ERR(connector_state);
10595 goto fail;
10596 }
10597
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010598 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10599 if (ret)
10600 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010601
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010602 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10603 if (IS_ERR(crtc_state)) {
10604 ret = PTR_ERR(crtc_state);
10605 goto fail;
10606 }
10607
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010608 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010609
Chris Wilson64927112011-04-20 07:25:26 +010010610 if (!mode)
10611 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010612
Chris Wilsond2dff872011-04-19 08:36:26 +010010613 /* We need a framebuffer large enough to accommodate all accesses
10614 * that the plane may generate whilst we perform load detection.
10615 * We can not rely on the fbcon either being present (we get called
10616 * during its initialisation to detect all boot displays, or it may
10617 * not even exist) or that it is large enough to satisfy the
10618 * requested mode.
10619 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010620 fb = mode_fits_in_fbdev(dev, mode);
10621 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010622 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010623 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010624 } else
10625 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010626 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010627 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010628 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010629 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010630
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010631 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10632 if (ret)
10633 goto fail;
10634
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010635 drm_framebuffer_unreference(fb);
10636
10637 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10638 if (ret)
10639 goto fail;
10640
10641 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10642 if (!ret)
10643 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10644 if (!ret)
10645 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10646 if (ret) {
10647 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10648 goto fail;
10649 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010650
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010651 ret = drm_atomic_commit(state);
10652 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010653 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010654 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010655 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010656
10657 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010658
Jesse Barnes79e53942008-11-07 14:24:08 -080010659 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010660 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010661 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010662
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010663fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010664 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010665 drm_atomic_state_free(restore_state);
10666 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010667
Rob Clark51fd3712013-11-19 12:10:12 -050010668 if (ret == -EDEADLK) {
10669 drm_modeset_backoff(ctx);
10670 goto retry;
10671 }
10672
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010673 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010674}
10675
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010676void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010677 struct intel_load_detect_pipe *old,
10678 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010679{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010680 struct intel_encoder *intel_encoder =
10681 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010682 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010683 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010684 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010685
Chris Wilsond2dff872011-04-19 08:36:26 +010010686 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010687 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010688 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010689
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010690 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010691 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010692
10693 ret = drm_atomic_commit(state);
10694 if (ret) {
10695 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10696 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010697 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010698}
10699
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010700static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010701 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010702{
10703 struct drm_i915_private *dev_priv = dev->dev_private;
10704 u32 dpll = pipe_config->dpll_hw_state.dpll;
10705
10706 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010707 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010708 else if (HAS_PCH_SPLIT(dev))
10709 return 120000;
10710 else if (!IS_GEN2(dev))
10711 return 96000;
10712 else
10713 return 48000;
10714}
10715
Jesse Barnes79e53942008-11-07 14:24:08 -080010716/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010717static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010718 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010719{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010720 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010721 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010722 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010723 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010724 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030010725 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010726 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010727 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010728
10729 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010730 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010731 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010732 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010733
10734 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010735 if (IS_PINEVIEW(dev)) {
10736 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10737 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010738 } else {
10739 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10740 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10741 }
10742
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010743 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010744 if (IS_PINEVIEW(dev))
10745 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10746 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010747 else
10748 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010749 DPLL_FPA01_P1_POST_DIV_SHIFT);
10750
10751 switch (dpll & DPLL_MODE_MASK) {
10752 case DPLLB_MODE_DAC_SERIAL:
10753 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10754 5 : 10;
10755 break;
10756 case DPLLB_MODE_LVDS:
10757 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10758 7 : 14;
10759 break;
10760 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010761 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010762 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010763 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010764 }
10765
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010766 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010767 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010768 else
Imre Deakdccbea32015-06-22 23:35:51 +030010769 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010770 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010771 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010772 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010773
10774 if (is_lvds) {
10775 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10776 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010777
10778 if (lvds & LVDS_CLKB_POWER_UP)
10779 clock.p2 = 7;
10780 else
10781 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010782 } else {
10783 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10784 clock.p1 = 2;
10785 else {
10786 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10787 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10788 }
10789 if (dpll & PLL_P2_DIVIDE_BY_4)
10790 clock.p2 = 4;
10791 else
10792 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010793 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010794
Imre Deakdccbea32015-06-22 23:35:51 +030010795 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010796 }
10797
Ville Syrjälä18442d02013-09-13 16:00:08 +030010798 /*
10799 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010800 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010801 * encoder's get_config() function.
10802 */
Imre Deakdccbea32015-06-22 23:35:51 +030010803 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010804}
10805
Ville Syrjälä6878da02013-09-13 15:59:11 +030010806int intel_dotclock_calculate(int link_freq,
10807 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010808{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010809 /*
10810 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010811 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010812 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010813 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010814 *
10815 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010816 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010817 */
10818
Ville Syrjälä6878da02013-09-13 15:59:11 +030010819 if (!m_n->link_n)
10820 return 0;
10821
10822 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10823}
10824
Ville Syrjälä18442d02013-09-13 16:00:08 +030010825static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010826 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010827{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010828 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010829
10830 /* read out port_clock from the DPLL */
10831 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010832
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010833 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010834 * In case there is an active pipe without active ports,
10835 * we may need some idea for the dotclock anyway.
10836 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010837 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010838 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010839 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010840 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010841}
10842
10843/** Returns the currently programmed mode of the given pipe. */
10844struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10845 struct drm_crtc *crtc)
10846{
Jesse Barnes548f2452011-02-17 10:40:53 -080010847 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010848 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010849 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010850 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010851 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010852 int htot = I915_READ(HTOTAL(cpu_transcoder));
10853 int hsync = I915_READ(HSYNC(cpu_transcoder));
10854 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10855 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010856 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010857
10858 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10859 if (!mode)
10860 return NULL;
10861
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010862 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10863 if (!pipe_config) {
10864 kfree(mode);
10865 return NULL;
10866 }
10867
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010868 /*
10869 * Construct a pipe_config sufficient for getting the clock info
10870 * back out of crtc_clock_get.
10871 *
10872 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10873 * to use a real value here instead.
10874 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010875 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10876 pipe_config->pixel_multiplier = 1;
10877 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10878 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10879 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10880 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010881
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010882 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010883 mode->hdisplay = (htot & 0xffff) + 1;
10884 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10885 mode->hsync_start = (hsync & 0xffff) + 1;
10886 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10887 mode->vdisplay = (vtot & 0xffff) + 1;
10888 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10889 mode->vsync_start = (vsync & 0xffff) + 1;
10890 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10891
10892 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010893
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010894 kfree(pipe_config);
10895
Jesse Barnes79e53942008-11-07 14:24:08 -080010896 return mode;
10897}
10898
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010899void intel_mark_busy(struct drm_i915_private *dev_priv)
Jesse Barnes652c3932009-08-17 13:31:43 -070010900{
Chris Wilsonf62a0072014-02-21 17:55:39 +000010901 if (dev_priv->mm.busy)
10902 return;
10903
Paulo Zanoni43694d62014-03-07 20:08:08 -030010904 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010905 i915_update_gfx_val(dev_priv);
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010906 if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010907 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010908 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010909}
10910
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010911void intel_mark_idle(struct drm_i915_private *dev_priv)
Chris Wilsonf047e392012-07-21 12:31:41 +010010912{
Chris Wilsonf62a0072014-02-21 17:55:39 +000010913 if (!dev_priv->mm.busy)
10914 return;
10915
10916 dev_priv->mm.busy = false;
10917
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010918 if (INTEL_GEN(dev_priv) >= 6)
10919 gen6_rps_idle(dev_priv);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010920
Paulo Zanoni43694d62014-03-07 20:08:08 -030010921 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010922}
10923
Jesse Barnes79e53942008-11-07 14:24:08 -080010924static void intel_crtc_destroy(struct drm_crtc *crtc)
10925{
10926 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010927 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010928 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010929
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010930 spin_lock_irq(&dev->event_lock);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010931 work = intel_crtc->flip_work;
10932 intel_crtc->flip_work = NULL;
10933 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010934
Daniel Vetter5a21b662016-05-24 17:13:53 +020010935 if (work) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010936 cancel_work_sync(&work->mmio_work);
10937 cancel_work_sync(&work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010938 kfree(work);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010939 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010940
10941 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010942
Jesse Barnes79e53942008-11-07 14:24:08 -080010943 kfree(intel_crtc);
10944}
10945
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010946static void intel_unpin_work_fn(struct work_struct *__work)
10947{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010948 struct intel_flip_work *work =
10949 container_of(__work, struct intel_flip_work, unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010950 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10951 struct drm_device *dev = crtc->base.dev;
10952 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010953
Daniel Vetter5a21b662016-05-24 17:13:53 +020010954 if (is_mmio_work(work))
10955 flush_work(&work->mmio_work);
10956
10957 mutex_lock(&dev->struct_mutex);
10958 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
10959 drm_gem_object_unreference(&work->pending_flip_obj->base);
10960
10961 if (work->flip_queued_req)
10962 i915_gem_request_assign(&work->flip_queued_req, NULL);
10963 mutex_unlock(&dev->struct_mutex);
10964
10965 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
10966 intel_fbc_post_update(crtc);
10967 drm_framebuffer_unreference(work->old_fb);
10968
10969 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10970 atomic_dec(&crtc->unpin_work_count);
10971
10972 kfree(work);
10973}
10974
10975/* Is 'a' after or equal to 'b'? */
10976static bool g4x_flip_count_after_eq(u32 a, u32 b)
10977{
10978 return !((a - b) & 0x80000000);
10979}
10980
10981static bool __pageflip_finished_cs(struct intel_crtc *crtc,
10982 struct intel_flip_work *work)
10983{
10984 struct drm_device *dev = crtc->base.dev;
10985 struct drm_i915_private *dev_priv = dev->dev_private;
10986 unsigned reset_counter;
10987
10988 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
10989 if (crtc->reset_counter != reset_counter)
10990 return true;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010991
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020010992 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020010993 * The relevant registers doen't exist on pre-ctg.
10994 * As the flip done interrupt doesn't trigger for mmio
10995 * flips on gmch platforms, a flip count check isn't
10996 * really needed there. But since ctg has the registers,
10997 * include it in the check anyway.
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020010998 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020010999 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11000 return true;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011001
Daniel Vetter5a21b662016-05-24 17:13:53 +020011002 /*
11003 * BDW signals flip done immediately if the plane
11004 * is disabled, even if the plane enable is already
11005 * armed to occur at the next vblank :(
11006 */
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011007
Daniel Vetter5a21b662016-05-24 17:13:53 +020011008 /*
11009 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11010 * used the same base address. In that case the mmio flip might
11011 * have completed, but the CS hasn't even executed the flip yet.
11012 *
11013 * A flip count check isn't enough as the CS might have updated
11014 * the base address just after start of vblank, but before we
11015 * managed to process the interrupt. This means we'd complete the
11016 * CS flip too soon.
11017 *
11018 * Combining both checks should get us a good enough result. It may
11019 * still happen that the CS flip has been executed, but has not
11020 * yet actually completed. But in case the base address is the same
11021 * anyway, we don't really care.
11022 */
11023 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11024 crtc->flip_work->gtt_offset &&
11025 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11026 crtc->flip_work->flip_count);
11027}
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011028
Daniel Vetter5a21b662016-05-24 17:13:53 +020011029static bool
11030__pageflip_finished_mmio(struct intel_crtc *crtc,
11031 struct intel_flip_work *work)
11032{
11033 /*
11034 * MMIO work completes when vblank is different from
11035 * flip_queued_vblank.
11036 *
11037 * Reset counter value doesn't matter, this is handled by
11038 * i915_wait_request finishing early, so no need to handle
11039 * reset here.
11040 */
11041 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011042}
11043
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011044
11045static bool pageflip_finished(struct intel_crtc *crtc,
11046 struct intel_flip_work *work)
11047{
11048 if (!atomic_read(&work->pending))
11049 return false;
11050
11051 smp_rmb();
11052
Daniel Vetter5a21b662016-05-24 17:13:53 +020011053 if (is_mmio_work(work))
11054 return __pageflip_finished_mmio(crtc, work);
11055 else
11056 return __pageflip_finished_cs(crtc, work);
11057}
11058
11059void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11060{
11061 struct drm_device *dev = dev_priv->dev;
11062 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11063 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11064 struct intel_flip_work *work;
11065 unsigned long flags;
11066
11067 /* Ignore early vblank irqs */
11068 if (!crtc)
11069 return;
11070
Daniel Vetterf3260382014-09-15 14:55:23 +020011071 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011072 * This is called both by irq handlers and the reset code (to complete
11073 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000011074 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011075 spin_lock_irqsave(&dev->event_lock, flags);
11076 work = intel_crtc->flip_work;
11077
11078 if (work != NULL &&
11079 !is_mmio_work(work) &&
11080 pageflip_finished(intel_crtc, work))
11081 page_flip_completed(intel_crtc);
11082
11083 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011084}
11085
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011086void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011087{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011088 struct drm_device *dev = dev_priv->dev;
11089 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11090 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11091 struct intel_flip_work *work;
11092 unsigned long flags;
11093
11094 /* Ignore early vblank irqs */
11095 if (!crtc)
11096 return;
11097
11098 /*
11099 * This is called both by irq handlers and the reset code (to complete
11100 * lost pageflips) so needs the full irqsave spinlocks.
11101 */
11102 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011103 work = intel_crtc->flip_work;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011104
Daniel Vetter5a21b662016-05-24 17:13:53 +020011105 if (work != NULL &&
11106 is_mmio_work(work) &&
11107 pageflip_finished(intel_crtc, work))
11108 page_flip_completed(intel_crtc);
Maarten Lankhorst68858432016-05-17 15:07:52 +020011109
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011110 spin_unlock_irqrestore(&dev->event_lock, flags);
11111}
11112
Daniel Vetter5a21b662016-05-24 17:13:53 +020011113static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11114 struct intel_flip_work *work)
11115{
11116 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
11117
11118 /* Ensure that the work item is consistent when activating it ... */
11119 smp_mb__before_atomic();
11120 atomic_set(&work->pending, 1);
11121}
11122
11123static int intel_gen2_queue_flip(struct drm_device *dev,
11124 struct drm_crtc *crtc,
11125 struct drm_framebuffer *fb,
11126 struct drm_i915_gem_object *obj,
11127 struct drm_i915_gem_request *req,
11128 uint32_t flags)
11129{
11130 struct intel_engine_cs *engine = req->engine;
11131 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11132 u32 flip_mask;
11133 int ret;
11134
11135 ret = intel_ring_begin(req, 6);
11136 if (ret)
11137 return ret;
11138
11139 /* Can't queue multiple flips, so wait for the previous
11140 * one to finish before executing the next.
11141 */
11142 if (intel_crtc->plane)
11143 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11144 else
11145 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11146 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11147 intel_ring_emit(engine, MI_NOOP);
11148 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11149 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11150 intel_ring_emit(engine, fb->pitches[0]);
11151 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11152 intel_ring_emit(engine, 0); /* aux display base address, unused */
11153
11154 return 0;
11155}
11156
11157static int intel_gen3_queue_flip(struct drm_device *dev,
11158 struct drm_crtc *crtc,
11159 struct drm_framebuffer *fb,
11160 struct drm_i915_gem_object *obj,
11161 struct drm_i915_gem_request *req,
11162 uint32_t flags)
11163{
11164 struct intel_engine_cs *engine = req->engine;
11165 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11166 u32 flip_mask;
11167 int ret;
11168
11169 ret = intel_ring_begin(req, 6);
11170 if (ret)
11171 return ret;
11172
11173 if (intel_crtc->plane)
11174 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11175 else
11176 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11177 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11178 intel_ring_emit(engine, MI_NOOP);
11179 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
11180 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11181 intel_ring_emit(engine, fb->pitches[0]);
11182 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11183 intel_ring_emit(engine, MI_NOOP);
11184
11185 return 0;
11186}
11187
11188static int intel_gen4_queue_flip(struct drm_device *dev,
11189 struct drm_crtc *crtc,
11190 struct drm_framebuffer *fb,
11191 struct drm_i915_gem_object *obj,
11192 struct drm_i915_gem_request *req,
11193 uint32_t flags)
11194{
11195 struct intel_engine_cs *engine = req->engine;
11196 struct drm_i915_private *dev_priv = dev->dev_private;
11197 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11198 uint32_t pf, pipesrc;
11199 int ret;
11200
11201 ret = intel_ring_begin(req, 4);
11202 if (ret)
11203 return ret;
11204
11205 /* i965+ uses the linear or tiled offsets from the
11206 * Display Registers (which do not change across a page-flip)
11207 * so we need only reprogram the base address.
11208 */
11209 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11210 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11211 intel_ring_emit(engine, fb->pitches[0]);
11212 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
11213 obj->tiling_mode);
11214
11215 /* XXX Enabling the panel-fitter across page-flip is so far
11216 * untested on non-native modes, so ignore it for now.
11217 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11218 */
11219 pf = 0;
11220 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11221 intel_ring_emit(engine, pf | pipesrc);
11222
11223 return 0;
11224}
11225
11226static int intel_gen6_queue_flip(struct drm_device *dev,
11227 struct drm_crtc *crtc,
11228 struct drm_framebuffer *fb,
11229 struct drm_i915_gem_object *obj,
11230 struct drm_i915_gem_request *req,
11231 uint32_t flags)
11232{
11233 struct intel_engine_cs *engine = req->engine;
11234 struct drm_i915_private *dev_priv = dev->dev_private;
11235 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11236 uint32_t pf, pipesrc;
11237 int ret;
11238
11239 ret = intel_ring_begin(req, 4);
11240 if (ret)
11241 return ret;
11242
11243 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11244 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11245 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11246 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11247
11248 /* Contrary to the suggestions in the documentation,
11249 * "Enable Panel Fitter" does not seem to be required when page
11250 * flipping with a non-native mode, and worse causes a normal
11251 * modeset to fail.
11252 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11253 */
11254 pf = 0;
11255 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11256 intel_ring_emit(engine, pf | pipesrc);
11257
11258 return 0;
11259}
11260
11261static int intel_gen7_queue_flip(struct drm_device *dev,
11262 struct drm_crtc *crtc,
11263 struct drm_framebuffer *fb,
11264 struct drm_i915_gem_object *obj,
11265 struct drm_i915_gem_request *req,
11266 uint32_t flags)
11267{
11268 struct intel_engine_cs *engine = req->engine;
11269 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11270 uint32_t plane_bit = 0;
11271 int len, ret;
11272
11273 switch (intel_crtc->plane) {
11274 case PLANE_A:
11275 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11276 break;
11277 case PLANE_B:
11278 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11279 break;
11280 case PLANE_C:
11281 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11282 break;
11283 default:
11284 WARN_ONCE(1, "unknown plane in flip command\n");
11285 return -ENODEV;
11286 }
11287
11288 len = 4;
11289 if (engine->id == RCS) {
11290 len += 6;
11291 /*
11292 * On Gen 8, SRM is now taking an extra dword to accommodate
11293 * 48bits addresses, and we need a NOOP for the batch size to
11294 * stay even.
11295 */
11296 if (IS_GEN8(dev))
11297 len += 2;
11298 }
11299
11300 /*
11301 * BSpec MI_DISPLAY_FLIP for IVB:
11302 * "The full packet must be contained within the same cache line."
11303 *
11304 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11305 * cacheline, if we ever start emitting more commands before
11306 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11307 * then do the cacheline alignment, and finally emit the
11308 * MI_DISPLAY_FLIP.
11309 */
11310 ret = intel_ring_cacheline_align(req);
11311 if (ret)
11312 return ret;
11313
11314 ret = intel_ring_begin(req, len);
11315 if (ret)
11316 return ret;
11317
11318 /* Unmask the flip-done completion message. Note that the bspec says that
11319 * we should do this for both the BCS and RCS, and that we must not unmask
11320 * more than one flip event at any time (or ensure that one flip message
11321 * can be sent by waiting for flip-done prior to queueing new flips).
11322 * Experimentation says that BCS works despite DERRMR masking all
11323 * flip-done completion events and that unmasking all planes at once
11324 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11325 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11326 */
11327 if (engine->id == RCS) {
11328 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11329 intel_ring_emit_reg(engine, DERRMR);
11330 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11331 DERRMR_PIPEB_PRI_FLIP_DONE |
11332 DERRMR_PIPEC_PRI_FLIP_DONE));
11333 if (IS_GEN8(dev))
11334 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
11335 MI_SRM_LRM_GLOBAL_GTT);
11336 else
11337 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
11338 MI_SRM_LRM_GLOBAL_GTT);
11339 intel_ring_emit_reg(engine, DERRMR);
11340 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
11341 if (IS_GEN8(dev)) {
11342 intel_ring_emit(engine, 0);
11343 intel_ring_emit(engine, MI_NOOP);
11344 }
11345 }
11346
11347 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11348 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11349 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11350 intel_ring_emit(engine, (MI_NOOP));
11351
11352 return 0;
11353}
11354
11355static bool use_mmio_flip(struct intel_engine_cs *engine,
11356 struct drm_i915_gem_object *obj)
11357{
11358 /*
11359 * This is not being used for older platforms, because
11360 * non-availability of flip done interrupt forces us to use
11361 * CS flips. Older platforms derive flip done using some clever
11362 * tricks involving the flip_pending status bits and vblank irqs.
11363 * So using MMIO flips there would disrupt this mechanism.
11364 */
11365
11366 if (engine == NULL)
11367 return true;
11368
11369 if (INTEL_GEN(engine->i915) < 5)
11370 return false;
11371
11372 if (i915.use_mmio_flip < 0)
11373 return false;
11374 else if (i915.use_mmio_flip > 0)
11375 return true;
11376 else if (i915.enable_execlists)
11377 return true;
11378 else if (obj->base.dma_buf &&
11379 !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
11380 false))
11381 return true;
11382 else
11383 return engine != i915_gem_request_get_engine(obj->last_write_req);
11384}
11385
11386static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11387 unsigned int rotation,
11388 struct intel_flip_work *work)
11389{
11390 struct drm_device *dev = intel_crtc->base.dev;
11391 struct drm_i915_private *dev_priv = dev->dev_private;
11392 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11393 const enum pipe pipe = intel_crtc->pipe;
11394 u32 ctl, stride, tile_height;
11395
11396 ctl = I915_READ(PLANE_CTL(pipe, 0));
11397 ctl &= ~PLANE_CTL_TILED_MASK;
11398 switch (fb->modifier[0]) {
11399 case DRM_FORMAT_MOD_NONE:
11400 break;
11401 case I915_FORMAT_MOD_X_TILED:
11402 ctl |= PLANE_CTL_TILED_X;
11403 break;
11404 case I915_FORMAT_MOD_Y_TILED:
11405 ctl |= PLANE_CTL_TILED_Y;
11406 break;
11407 case I915_FORMAT_MOD_Yf_TILED:
11408 ctl |= PLANE_CTL_TILED_YF;
11409 break;
11410 default:
11411 MISSING_CASE(fb->modifier[0]);
11412 }
11413
11414 /*
11415 * The stride is either expressed as a multiple of 64 bytes chunks for
11416 * linear buffers or in number of tiles for tiled buffers.
11417 */
11418 if (intel_rotation_90_or_270(rotation)) {
11419 /* stride = Surface height in tiles */
11420 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
11421 stride = DIV_ROUND_UP(fb->height, tile_height);
11422 } else {
11423 stride = fb->pitches[0] /
11424 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11425 fb->pixel_format);
11426 }
11427
11428 /*
11429 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11430 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11431 */
11432 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11433 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11434
11435 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11436 POSTING_READ(PLANE_SURF(pipe, 0));
11437}
11438
11439static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11440 struct intel_flip_work *work)
11441{
11442 struct drm_device *dev = intel_crtc->base.dev;
11443 struct drm_i915_private *dev_priv = dev->dev_private;
11444 struct intel_framebuffer *intel_fb =
11445 to_intel_framebuffer(intel_crtc->base.primary->fb);
11446 struct drm_i915_gem_object *obj = intel_fb->obj;
11447 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11448 u32 dspcntr;
11449
11450 dspcntr = I915_READ(reg);
11451
11452 if (obj->tiling_mode != I915_TILING_NONE)
11453 dspcntr |= DISPPLANE_TILED;
11454 else
11455 dspcntr &= ~DISPPLANE_TILED;
11456
11457 I915_WRITE(reg, dspcntr);
11458
11459 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11460 POSTING_READ(DSPSURF(intel_crtc->plane));
11461}
11462
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011463static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000011464{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011465 struct intel_flip_work *work =
11466 container_of(w, struct intel_flip_work, mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011467 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11468 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11469 struct intel_framebuffer *intel_fb =
11470 to_intel_framebuffer(crtc->base.primary->fb);
11471 struct drm_i915_gem_object *obj = intel_fb->obj;
11472
11473 if (work->flip_queued_req)
11474 WARN_ON(__i915_wait_request(work->flip_queued_req,
11475 false, NULL,
11476 &dev_priv->rps.mmioflips));
11477
11478 /* For framebuffer backed by dmabuf, wait for fence */
11479 if (obj->base.dma_buf)
11480 WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
11481 false, false,
11482 MAX_SCHEDULE_TIMEOUT) < 0);
11483
11484 intel_pipe_update_start(crtc);
11485
11486 if (INTEL_GEN(dev_priv) >= 9)
11487 skl_do_mmio_flip(crtc, work->rotation, work);
11488 else
11489 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11490 ilk_do_mmio_flip(crtc, work);
11491
11492 intel_pipe_update_end(crtc, work);
11493}
11494
11495static int intel_default_queue_flip(struct drm_device *dev,
11496 struct drm_crtc *crtc,
11497 struct drm_framebuffer *fb,
11498 struct drm_i915_gem_object *obj,
11499 struct drm_i915_gem_request *req,
11500 uint32_t flags)
11501{
11502 return -ENODEV;
11503}
11504
11505static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11506 struct intel_crtc *intel_crtc,
11507 struct intel_flip_work *work)
11508{
11509 u32 addr, vblank;
11510
11511 if (!atomic_read(&work->pending))
11512 return false;
11513
11514 smp_rmb();
11515
11516 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11517 if (work->flip_ready_vblank == 0) {
11518 if (work->flip_queued_req &&
11519 !i915_gem_request_completed(work->flip_queued_req, true))
11520 return false;
11521
11522 work->flip_ready_vblank = vblank;
11523 }
11524
11525 if (vblank - work->flip_ready_vblank < 3)
11526 return false;
11527
11528 /* Potential stall - if we see that the flip has happened,
11529 * assume a missed interrupt. */
11530 if (INTEL_GEN(dev_priv) >= 4)
11531 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11532 else
11533 addr = I915_READ(DSPADDR(intel_crtc->plane));
11534
11535 /* There is a potential issue here with a false positive after a flip
11536 * to the same address. We could address this by checking for a
11537 * non-incrementing frame counter.
11538 */
11539 return addr == work->gtt_offset;
11540}
11541
11542void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11543{
11544 struct drm_device *dev = dev_priv->dev;
11545 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011546 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011547 struct intel_flip_work *work;
11548
11549 WARN_ON(!in_interrupt());
11550
11551 if (crtc == NULL)
11552 return;
11553
11554 spin_lock(&dev->event_lock);
11555 work = intel_crtc->flip_work;
11556
11557 if (work != NULL && !is_mmio_work(work) &&
11558 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11559 WARN_ONCE(1,
11560 "Kicking stuck page flip: queued at %d, now %d\n",
11561 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11562 page_flip_completed(intel_crtc);
11563 work = NULL;
11564 }
11565
11566 if (work != NULL && !is_mmio_work(work) &&
11567 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11568 intel_queue_rps_boost_for_request(work->flip_queued_req);
11569 spin_unlock(&dev->event_lock);
11570}
11571
11572static int intel_crtc_page_flip(struct drm_crtc *crtc,
11573 struct drm_framebuffer *fb,
11574 struct drm_pending_vblank_event *event,
11575 uint32_t page_flip_flags)
11576{
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011577 struct drm_device *dev = crtc->dev;
Maarten Lankhorstaa420dd2016-05-17 15:07:51 +020011578 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011579 struct drm_framebuffer *old_fb = crtc->primary->fb;
11580 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11581 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11582 struct drm_plane *primary = crtc->primary;
11583 enum pipe pipe = intel_crtc->pipe;
11584 struct intel_flip_work *work;
11585 struct intel_engine_cs *engine;
11586 bool mmio_flip;
11587 struct drm_i915_gem_request *request = NULL;
11588 int ret;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011589
Daniel Vetter5a21b662016-05-24 17:13:53 +020011590 /*
11591 * drm_mode_page_flip_ioctl() should already catch this, but double
11592 * check to be safe. In the future we may enable pageflipping from
11593 * a disabled primary plane.
11594 */
11595 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11596 return -EBUSY;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011597
Daniel Vetter5a21b662016-05-24 17:13:53 +020011598 /* Can't change pixel format via MI display flips. */
11599 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11600 return -EINVAL;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011601
Daniel Vetter5a21b662016-05-24 17:13:53 +020011602 /*
11603 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11604 * Note that pitch changes could also affect these register.
11605 */
11606 if (INTEL_INFO(dev)->gen > 3 &&
11607 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11608 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11609 return -EINVAL;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011610
Daniel Vetter5a21b662016-05-24 17:13:53 +020011611 if (i915_terminally_wedged(&dev_priv->gpu_error))
11612 goto out_hang;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011613
Daniel Vetter5a21b662016-05-24 17:13:53 +020011614 work = kzalloc(sizeof(*work), GFP_KERNEL);
11615 if (work == NULL)
11616 return -ENOMEM;
11617
11618 work->event = event;
11619 work->crtc = crtc;
11620 work->old_fb = old_fb;
11621 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011622
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011623 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011624 if (ret)
11625 goto free_work;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011626
Daniel Vetter5a21b662016-05-24 17:13:53 +020011627 /* We borrow the event spin lock for protecting flip_work */
11628 spin_lock_irq(&dev->event_lock);
11629 if (intel_crtc->flip_work) {
11630 /* Before declaring the flip queue wedged, check if
11631 * the hardware completed the operation behind our backs.
11632 */
11633 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11634 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11635 page_flip_completed(intel_crtc);
11636 } else {
11637 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11638 spin_unlock_irq(&dev->event_lock);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011639
Daniel Vetter5a21b662016-05-24 17:13:53 +020011640 drm_crtc_vblank_put(crtc);
11641 kfree(work);
11642 return -EBUSY;
11643 }
11644 }
11645 intel_crtc->flip_work = work;
11646 spin_unlock_irq(&dev->event_lock);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011647
Daniel Vetter5a21b662016-05-24 17:13:53 +020011648 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11649 flush_workqueue(dev_priv->wq);
11650
11651 /* Reference the objects for the scheduled work. */
11652 drm_framebuffer_reference(work->old_fb);
11653 drm_gem_object_reference(&obj->base);
11654
11655 crtc->primary->fb = fb;
11656 update_state_fb(crtc->primary);
11657 intel_fbc_pre_update(intel_crtc);
11658
11659 work->pending_flip_obj = obj;
11660
11661 ret = i915_mutex_lock_interruptible(dev);
11662 if (ret)
11663 goto cleanup;
11664
11665 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11666 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11667 ret = -EIO;
11668 goto cleanup;
11669 }
11670
11671 atomic_inc(&intel_crtc->unpin_work_count);
11672
11673 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11674 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11675
11676 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11677 engine = &dev_priv->engine[BCS];
11678 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
11679 /* vlv: DISPLAY_FLIP fails to change tiling */
11680 engine = NULL;
11681 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11682 engine = &dev_priv->engine[BCS];
11683 } else if (INTEL_INFO(dev)->gen >= 7) {
11684 engine = i915_gem_request_get_engine(obj->last_write_req);
11685 if (engine == NULL || engine->id != RCS)
11686 engine = &dev_priv->engine[BCS];
11687 } else {
11688 engine = &dev_priv->engine[RCS];
11689 }
11690
11691 mmio_flip = use_mmio_flip(engine, obj);
11692
11693 /* When using CS flips, we want to emit semaphores between rings.
11694 * However, when using mmio flips we will create a task to do the
11695 * synchronisation, so all we want here is to pin the framebuffer
11696 * into the display plane and skip any waits.
11697 */
11698 if (!mmio_flip) {
11699 ret = i915_gem_object_sync(obj, engine, &request);
11700 if (!ret && !request) {
11701 request = i915_gem_request_alloc(engine, NULL);
11702 ret = PTR_ERR_OR_ZERO(request);
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011703 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011704
Daniel Vetter5a21b662016-05-24 17:13:53 +020011705 if (ret)
11706 goto cleanup_pending;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011707 }
11708
Daniel Vetter5a21b662016-05-24 17:13:53 +020011709 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11710 if (ret)
11711 goto cleanup_pending;
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011712
Daniel Vetter5a21b662016-05-24 17:13:53 +020011713 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11714 obj, 0);
11715 work->gtt_offset += intel_crtc->dspaddr_offset;
11716 work->rotation = crtc->primary->state->rotation;
11717
11718 if (mmio_flip) {
11719 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11720
11721 i915_gem_request_assign(&work->flip_queued_req,
11722 obj->last_write_req);
11723
11724 schedule_work(&work->mmio_work);
11725 } else {
11726 i915_gem_request_assign(&work->flip_queued_req, request);
11727 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11728 page_flip_flags);
11729 if (ret)
11730 goto cleanup_unpin;
11731
11732 intel_mark_page_flip_active(intel_crtc, work);
11733
11734 i915_add_request_no_flush(request);
Maarten Lankhorst143f73b2016-05-17 15:07:54 +020011735 }
11736
Daniel Vetter5a21b662016-05-24 17:13:53 +020011737 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11738 to_intel_plane(primary)->frontbuffer_bit);
11739 mutex_unlock(&dev->struct_mutex);
11740
11741 intel_frontbuffer_flip_prepare(dev,
11742 to_intel_plane(primary)->frontbuffer_bit);
11743
11744 trace_i915_flip_request(intel_crtc->plane, obj);
11745
11746 return 0;
11747
11748cleanup_unpin:
11749 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11750cleanup_pending:
11751 if (!IS_ERR_OR_NULL(request))
11752 i915_add_request_no_flush(request);
11753 atomic_dec(&intel_crtc->unpin_work_count);
11754 mutex_unlock(&dev->struct_mutex);
11755cleanup:
11756 crtc->primary->fb = old_fb;
11757 update_state_fb(crtc->primary);
11758
11759 drm_gem_object_unreference_unlocked(&obj->base);
11760 drm_framebuffer_unreference(work->old_fb);
11761
11762 spin_lock_irq(&dev->event_lock);
11763 intel_crtc->flip_work = NULL;
11764 spin_unlock_irq(&dev->event_lock);
11765
11766 drm_crtc_vblank_put(crtc);
11767free_work:
11768 kfree(work);
11769
11770 if (ret == -EIO) {
11771 struct drm_atomic_state *state;
11772 struct drm_plane_state *plane_state;
11773
11774out_hang:
11775 state = drm_atomic_state_alloc(dev);
11776 if (!state)
11777 return -ENOMEM;
11778 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11779
11780retry:
11781 plane_state = drm_atomic_get_plane_state(state, primary);
11782 ret = PTR_ERR_OR_ZERO(plane_state);
11783 if (!ret) {
11784 drm_atomic_set_fb_for_plane(plane_state, fb);
11785
11786 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11787 if (!ret)
11788 ret = drm_atomic_commit(state);
11789 }
11790
11791 if (ret == -EDEADLK) {
11792 drm_modeset_backoff(state->acquire_ctx);
11793 drm_atomic_state_clear(state);
11794 goto retry;
11795 }
11796
11797 if (ret)
11798 drm_atomic_state_free(state);
11799
11800 if (ret == 0 && event) {
11801 spin_lock_irq(&dev->event_lock);
11802 drm_crtc_send_vblank_event(crtc, event);
11803 spin_unlock_irq(&dev->event_lock);
11804 }
11805 }
11806 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011807}
11808
Daniel Vetter5a21b662016-05-24 17:13:53 +020011809
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011810/**
11811 * intel_wm_need_update - Check whether watermarks need updating
11812 * @plane: drm plane
11813 * @state: new plane state
11814 *
11815 * Check current plane state versus the new one to determine whether
11816 * watermarks need to be recalculated.
11817 *
11818 * Returns true or false.
11819 */
11820static bool intel_wm_need_update(struct drm_plane *plane,
11821 struct drm_plane_state *state)
11822{
Matt Roperd21fbe82015-09-24 15:53:12 -070011823 struct intel_plane_state *new = to_intel_plane_state(state);
11824 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11825
11826 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011827 if (new->visible != cur->visible)
11828 return true;
11829
11830 if (!cur->base.fb || !new->base.fb)
11831 return false;
11832
11833 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11834 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011835 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11836 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11837 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11838 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011839 return true;
11840
11841 return false;
11842}
11843
Matt Roperd21fbe82015-09-24 15:53:12 -070011844static bool needs_scaling(struct intel_plane_state *state)
11845{
11846 int src_w = drm_rect_width(&state->src) >> 16;
11847 int src_h = drm_rect_height(&state->src) >> 16;
11848 int dst_w = drm_rect_width(&state->dst);
11849 int dst_h = drm_rect_height(&state->dst);
11850
11851 return (src_w != dst_w || src_h != dst_h);
11852}
11853
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011854int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11855 struct drm_plane_state *plane_state)
11856{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011857 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011858 struct drm_crtc *crtc = crtc_state->crtc;
11859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11860 struct drm_plane *plane = plane_state->plane;
11861 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011862 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011863 struct intel_plane_state *old_plane_state =
11864 to_intel_plane_state(plane->state);
11865 int idx = intel_crtc->base.base.id, ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011866 bool mode_changed = needs_modeset(crtc_state);
11867 bool was_crtc_enabled = crtc->state->active;
11868 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011869 bool turn_off, turn_on, visible, was_visible;
11870 struct drm_framebuffer *fb = plane_state->fb;
11871
11872 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11873 plane->type != DRM_PLANE_TYPE_CURSOR) {
11874 ret = skl_update_scaler_plane(
11875 to_intel_crtc_state(crtc_state),
11876 to_intel_plane_state(plane_state));
11877 if (ret)
11878 return ret;
11879 }
11880
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011881 was_visible = old_plane_state->visible;
11882 visible = to_intel_plane_state(plane_state)->visible;
11883
11884 if (!was_crtc_enabled && WARN_ON(was_visible))
11885 was_visible = false;
11886
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011887 /*
11888 * Visibility is calculated as if the crtc was on, but
11889 * after scaler setup everything depends on it being off
11890 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030011891 *
11892 * FIXME this is wrong for watermarks. Watermarks should also
11893 * be computed as if the pipe would be active. Perhaps move
11894 * per-plane wm computation to the .check_plane() hook, and
11895 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011896 */
11897 if (!is_crtc_enabled)
11898 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011899
11900 if (!was_visible && !visible)
11901 return 0;
11902
Maarten Lankhorste8861672016-02-24 11:24:26 +010011903 if (fb != old_plane_state->base.fb)
11904 pipe_config->fb_changed = true;
11905
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011906 turn_off = was_visible && (!visible || mode_changed);
11907 turn_on = visible && (!was_visible || mode_changed);
11908
11909 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11910 plane->base.id, fb ? fb->base.id : -1);
11911
11912 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11913 plane->base.id, was_visible, visible,
11914 turn_off, turn_on, mode_changed);
11915
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011916 if (turn_on) {
11917 pipe_config->update_wm_pre = true;
11918
11919 /* must disable cxsr around plane enable/disable */
11920 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11921 pipe_config->disable_cxsr = true;
11922 } else if (turn_off) {
11923 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011924
Ville Syrjälä852eb002015-06-24 22:00:07 +030011925 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011926 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011927 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011928 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011929 /* FIXME bollocks */
11930 pipe_config->update_wm_pre = true;
11931 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011932 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011933
Matt Ropered4a6a72016-02-23 17:20:13 -080011934 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011935 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11936 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011937 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11938
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011939 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011940 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011941
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011942 /*
11943 * WaCxSRDisabledForSpriteScaling:ivb
11944 *
11945 * cstate->update_wm was already set above, so this flag will
11946 * take effect when we commit and program watermarks.
11947 */
11948 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11949 needs_scaling(to_intel_plane_state(plane_state)) &&
11950 !needs_scaling(old_plane_state))
11951 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011952
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011953 return 0;
11954}
11955
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011956static bool encoders_cloneable(const struct intel_encoder *a,
11957 const struct intel_encoder *b)
11958{
11959 /* masks could be asymmetric, so check both ways */
11960 return a == b || (a->cloneable & (1 << b->type) &&
11961 b->cloneable & (1 << a->type));
11962}
11963
11964static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11965 struct intel_crtc *crtc,
11966 struct intel_encoder *encoder)
11967{
11968 struct intel_encoder *source_encoder;
11969 struct drm_connector *connector;
11970 struct drm_connector_state *connector_state;
11971 int i;
11972
11973 for_each_connector_in_state(state, connector, connector_state, i) {
11974 if (connector_state->crtc != &crtc->base)
11975 continue;
11976
11977 source_encoder =
11978 to_intel_encoder(connector_state->best_encoder);
11979 if (!encoders_cloneable(encoder, source_encoder))
11980 return false;
11981 }
11982
11983 return true;
11984}
11985
11986static bool check_encoder_cloning(struct drm_atomic_state *state,
11987 struct intel_crtc *crtc)
11988{
11989 struct intel_encoder *encoder;
11990 struct drm_connector *connector;
11991 struct drm_connector_state *connector_state;
11992 int i;
11993
11994 for_each_connector_in_state(state, connector, connector_state, i) {
11995 if (connector_state->crtc != &crtc->base)
11996 continue;
11997
11998 encoder = to_intel_encoder(connector_state->best_encoder);
11999 if (!check_single_encoder_cloning(state, crtc, encoder))
12000 return false;
12001 }
12002
12003 return true;
12004}
12005
12006static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12007 struct drm_crtc_state *crtc_state)
12008{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012009 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020012010 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012011 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012012 struct intel_crtc_state *pipe_config =
12013 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012014 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012015 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012016 bool mode_changed = needs_modeset(crtc_state);
12017
12018 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
12019 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12020 return -EINVAL;
12021 }
12022
Ville Syrjälä852eb002015-06-24 22:00:07 +030012023 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012024 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012025
Maarten Lankhorstad421372015-06-15 12:33:42 +020012026 if (mode_changed && crtc_state->enable &&
12027 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012028 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012029 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12030 pipe_config);
12031 if (ret)
12032 return ret;
12033 }
12034
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012035 if (crtc_state->color_mgmt_changed) {
12036 ret = intel_color_check(crtc, crtc_state);
12037 if (ret)
12038 return ret;
12039 }
12040
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012041 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012042 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010012043 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080012044 if (ret) {
12045 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012046 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080012047 }
12048 }
12049
12050 if (dev_priv->display.compute_intermediate_wm &&
12051 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12052 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12053 return 0;
12054
12055 /*
12056 * Calculate 'intermediate' watermarks that satisfy both the
12057 * old state and the new state. We can program these
12058 * immediately.
12059 */
12060 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12061 intel_crtc,
12062 pipe_config);
12063 if (ret) {
12064 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12065 return ret;
12066 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070012067 } else if (dev_priv->display.compute_intermediate_wm) {
12068 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12069 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012070 }
12071
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012072 if (INTEL_INFO(dev)->gen >= 9) {
12073 if (mode_changed)
12074 ret = skl_update_scaler_crtc(pipe_config);
12075
12076 if (!ret)
12077 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12078 pipe_config);
12079 }
12080
12081 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012082}
12083
Jani Nikula65b38e02015-04-13 11:26:56 +030012084static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012085 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Daniel Vetter5a21b662016-05-24 17:13:53 +020012086 .atomic_begin = intel_begin_crtc_commit,
12087 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012088 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012089};
12090
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012091static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12092{
12093 struct intel_connector *connector;
12094
12095 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020012096 if (connector->base.state->crtc)
12097 drm_connector_unreference(&connector->base);
12098
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012099 if (connector->base.encoder) {
12100 connector->base.state->best_encoder =
12101 connector->base.encoder;
12102 connector->base.state->crtc =
12103 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020012104
12105 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012106 } else {
12107 connector->base.state->best_encoder = NULL;
12108 connector->base.state->crtc = NULL;
12109 }
12110 }
12111}
12112
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012113static void
Robin Schroereba905b2014-05-18 02:24:50 +020012114connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012115 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012116{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012117 int bpp = pipe_config->pipe_bpp;
12118
12119 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12120 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012121 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012122
12123 /* Don't use an invalid EDID bpc value */
12124 if (connector->base.display_info.bpc &&
12125 connector->base.display_info.bpc * 3 < bpp) {
12126 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12127 bpp, connector->base.display_info.bpc*3);
12128 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12129 }
12130
Jani Nikula013dd9e2016-01-13 16:35:20 +020012131 /* Clamp bpp to default limit on screens without EDID 1.4 */
12132 if (connector->base.display_info.bpc == 0) {
12133 int type = connector->base.connector_type;
12134 int clamp_bpp = 24;
12135
12136 /* Fall back to 18 bpp when DP sink capability is unknown. */
12137 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12138 type == DRM_MODE_CONNECTOR_eDP)
12139 clamp_bpp = 18;
12140
12141 if (bpp > clamp_bpp) {
12142 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12143 bpp, clamp_bpp);
12144 pipe_config->pipe_bpp = clamp_bpp;
12145 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012146 }
12147}
12148
12149static int
12150compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012151 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012152{
12153 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012154 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012155 struct drm_connector *connector;
12156 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012157 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012158
Wayne Boyer666a4532015-12-09 12:29:35 -080012159 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012160 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012161 else if (INTEL_INFO(dev)->gen >= 5)
12162 bpp = 12*3;
12163 else
12164 bpp = 8*3;
12165
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012166
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012167 pipe_config->pipe_bpp = bpp;
12168
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012169 state = pipe_config->base.state;
12170
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012171 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012172 for_each_connector_in_state(state, connector, connector_state, i) {
12173 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012174 continue;
12175
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012176 connected_sink_compute_bpp(to_intel_connector(connector),
12177 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012178 }
12179
12180 return bpp;
12181}
12182
Daniel Vetter644db712013-09-19 14:53:58 +020012183static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12184{
12185 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12186 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012187 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012188 mode->crtc_hdisplay, mode->crtc_hsync_start,
12189 mode->crtc_hsync_end, mode->crtc_htotal,
12190 mode->crtc_vdisplay, mode->crtc_vsync_start,
12191 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12192}
12193
Daniel Vetterc0b03412013-05-28 12:05:54 +020012194static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012195 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012196 const char *context)
12197{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012198 struct drm_device *dev = crtc->base.dev;
12199 struct drm_plane *plane;
12200 struct intel_plane *intel_plane;
12201 struct intel_plane_state *state;
12202 struct drm_framebuffer *fb;
12203
12204 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12205 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012206
Jani Nikulada205632016-03-15 21:51:10 +020012207 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012208 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12209 pipe_config->pipe_bpp, pipe_config->dither);
12210 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12211 pipe_config->has_pch_encoder,
12212 pipe_config->fdi_lanes,
12213 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12214 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12215 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012216 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012217 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012218 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012219 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12220 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12221 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012222
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012223 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012224 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012225 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012226 pipe_config->dp_m2_n2.gmch_m,
12227 pipe_config->dp_m2_n2.gmch_n,
12228 pipe_config->dp_m2_n2.link_m,
12229 pipe_config->dp_m2_n2.link_n,
12230 pipe_config->dp_m2_n2.tu);
12231
Daniel Vetter55072d12014-11-20 16:10:28 +010012232 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12233 pipe_config->has_audio,
12234 pipe_config->has_infoframe);
12235
Daniel Vetterc0b03412013-05-28 12:05:54 +020012236 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012237 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012238 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012239 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12240 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012241 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012242 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12243 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012244 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12245 crtc->num_scalers,
12246 pipe_config->scaler_state.scaler_users,
12247 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012248 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12249 pipe_config->gmch_pfit.control,
12250 pipe_config->gmch_pfit.pgm_ratios,
12251 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012252 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012253 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012254 pipe_config->pch_pfit.size,
12255 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012256 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012257 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012258
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012259 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012260 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012261 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012262 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012263 pipe_config->ddi_pll_sel,
12264 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012265 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012266 pipe_config->dpll_hw_state.pll0,
12267 pipe_config->dpll_hw_state.pll1,
12268 pipe_config->dpll_hw_state.pll2,
12269 pipe_config->dpll_hw_state.pll3,
12270 pipe_config->dpll_hw_state.pll6,
12271 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012272 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012273 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012274 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012275 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012276 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12277 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12278 pipe_config->ddi_pll_sel,
12279 pipe_config->dpll_hw_state.ctrl1,
12280 pipe_config->dpll_hw_state.cfgcr1,
12281 pipe_config->dpll_hw_state.cfgcr2);
12282 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012283 DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012284 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012285 pipe_config->dpll_hw_state.wrpll,
12286 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012287 } else {
12288 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12289 "fp0: 0x%x, fp1: 0x%x\n",
12290 pipe_config->dpll_hw_state.dpll,
12291 pipe_config->dpll_hw_state.dpll_md,
12292 pipe_config->dpll_hw_state.fp0,
12293 pipe_config->dpll_hw_state.fp1);
12294 }
12295
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012296 DRM_DEBUG_KMS("planes on this crtc\n");
12297 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12298 intel_plane = to_intel_plane(plane);
12299 if (intel_plane->pipe != crtc->pipe)
12300 continue;
12301
12302 state = to_intel_plane_state(plane->state);
12303 fb = state->base.fb;
12304 if (!fb) {
12305 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12306 "disabled, scaler_id = %d\n",
12307 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12308 plane->base.id, intel_plane->pipe,
12309 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12310 drm_plane_index(plane), state->scaler_id);
12311 continue;
12312 }
12313
12314 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12315 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12316 plane->base.id, intel_plane->pipe,
12317 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12318 drm_plane_index(plane));
12319 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12320 fb->base.id, fb->width, fb->height, fb->pixel_format);
12321 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12322 state->scaler_id,
12323 state->src.x1 >> 16, state->src.y1 >> 16,
12324 drm_rect_width(&state->src) >> 16,
12325 drm_rect_height(&state->src) >> 16,
12326 state->dst.x1, state->dst.y1,
12327 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12328 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012329}
12330
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012331static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012332{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012333 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012334 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012335 unsigned int used_ports = 0;
12336
12337 /*
12338 * Walk the connector list instead of the encoder
12339 * list to detect the problem on ddi platforms
12340 * where there's just one encoder per digital port.
12341 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012342 drm_for_each_connector(connector, dev) {
12343 struct drm_connector_state *connector_state;
12344 struct intel_encoder *encoder;
12345
12346 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12347 if (!connector_state)
12348 connector_state = connector->state;
12349
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012350 if (!connector_state->best_encoder)
12351 continue;
12352
12353 encoder = to_intel_encoder(connector_state->best_encoder);
12354
12355 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012356
12357 switch (encoder->type) {
12358 unsigned int port_mask;
12359 case INTEL_OUTPUT_UNKNOWN:
12360 if (WARN_ON(!HAS_DDI(dev)))
12361 break;
12362 case INTEL_OUTPUT_DISPLAYPORT:
12363 case INTEL_OUTPUT_HDMI:
12364 case INTEL_OUTPUT_EDP:
12365 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12366
12367 /* the same port mustn't appear more than once */
12368 if (used_ports & port_mask)
12369 return false;
12370
12371 used_ports |= port_mask;
12372 default:
12373 break;
12374 }
12375 }
12376
12377 return true;
12378}
12379
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012380static void
12381clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12382{
12383 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012384 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012385 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012386 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012387 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012388 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012389
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012390 /* FIXME: before the switch to atomic started, a new pipe_config was
12391 * kzalloc'd. Code that depends on any field being zero should be
12392 * fixed, so that the crtc_state can be safely duplicated. For now,
12393 * only fields that are know to not cause problems are preserved. */
12394
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012395 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012396 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012397 shared_dpll = crtc_state->shared_dpll;
12398 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012399 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012400 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012401
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012402 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012403
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012404 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012405 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012406 crtc_state->shared_dpll = shared_dpll;
12407 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012408 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012409 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012410}
12411
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012412static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012413intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012414 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012415{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012416 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012417 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012418 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012419 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012420 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012421 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012422 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012423
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012424 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012425
Daniel Vettere143a212013-07-04 12:01:15 +020012426 pipe_config->cpu_transcoder =
12427 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012428
Imre Deak2960bc92013-07-30 13:36:32 +030012429 /*
12430 * Sanitize sync polarity flags based on requested ones. If neither
12431 * positive or negative polarity is requested, treat this as meaning
12432 * negative polarity.
12433 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012434 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012435 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012436 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012437
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012438 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012439 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012440 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012441
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012442 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12443 pipe_config);
12444 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012445 goto fail;
12446
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012447 /*
12448 * Determine the real pipe dimensions. Note that stereo modes can
12449 * increase the actual pipe size due to the frame doubling and
12450 * insertion of additional space for blanks between the frame. This
12451 * is stored in the crtc timings. We use the requested mode to do this
12452 * computation to clearly distinguish it from the adjusted mode, which
12453 * can be changed by the connectors in the below retry loop.
12454 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012455 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012456 &pipe_config->pipe_src_w,
12457 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012458
Daniel Vettere29c22c2013-02-21 00:00:16 +010012459encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012460 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012461 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012462 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012463
Daniel Vetter135c81b2013-07-21 21:37:09 +020012464 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012465 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12466 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012467
Daniel Vetter7758a112012-07-08 19:40:39 +020012468 /* Pass our mode to the connectors and the CRTC to give them a chance to
12469 * adjust it according to limitations or connector properties, and also
12470 * a chance to reject the mode entirely.
12471 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012472 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012473 if (connector_state->crtc != crtc)
12474 continue;
12475
12476 encoder = to_intel_encoder(connector_state->best_encoder);
12477
Daniel Vetterefea6e82013-07-21 21:36:59 +020012478 if (!(encoder->compute_config(encoder, pipe_config))) {
12479 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012480 goto fail;
12481 }
12482 }
12483
Daniel Vetterff9a6752013-06-01 17:16:21 +020012484 /* Set default port clock if not overwritten by the encoder. Needs to be
12485 * done afterwards in case the encoder adjusts the mode. */
12486 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012487 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012488 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012489
Daniel Vettera43f6e02013-06-07 23:10:32 +020012490 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012491 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012492 DRM_DEBUG_KMS("CRTC fixup failed\n");
12493 goto fail;
12494 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012495
12496 if (ret == RETRY) {
12497 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12498 ret = -EINVAL;
12499 goto fail;
12500 }
12501
12502 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12503 retry = false;
12504 goto encoder_retry;
12505 }
12506
Daniel Vettere8fa4272015-08-12 11:43:34 +020012507 /* Dithering seems to not pass-through bits correctly when it should, so
12508 * only enable it on 6bpc panels. */
12509 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012510 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012511 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012512
Daniel Vetter7758a112012-07-08 19:40:39 +020012513fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012514 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012515}
12516
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012517static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012518intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012519{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012520 struct drm_crtc *crtc;
12521 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012522 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012523
Ville Syrjälä76688512014-01-10 11:28:06 +020012524 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012525 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012526 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012527
12528 /* Update hwmode for vblank functions */
12529 if (crtc->state->active)
12530 crtc->hwmode = crtc->state->adjusted_mode;
12531 else
12532 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012533
12534 /*
12535 * Update legacy state to satisfy fbc code. This can
12536 * be removed when fbc uses the atomic state.
12537 */
12538 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12539 struct drm_plane_state *plane_state = crtc->primary->state;
12540
12541 crtc->primary->fb = plane_state->fb;
12542 crtc->x = plane_state->src_x >> 16;
12543 crtc->y = plane_state->src_y >> 16;
12544 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012545 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012546}
12547
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012548static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012549{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012550 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012551
12552 if (clock1 == clock2)
12553 return true;
12554
12555 if (!clock1 || !clock2)
12556 return false;
12557
12558 diff = abs(clock1 - clock2);
12559
12560 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12561 return true;
12562
12563 return false;
12564}
12565
Daniel Vetter25c5b262012-07-08 22:08:04 +020012566#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12567 list_for_each_entry((intel_crtc), \
12568 &(dev)->mode_config.crtc_list, \
12569 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012570 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012571
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012572static bool
12573intel_compare_m_n(unsigned int m, unsigned int n,
12574 unsigned int m2, unsigned int n2,
12575 bool exact)
12576{
12577 if (m == m2 && n == n2)
12578 return true;
12579
12580 if (exact || !m || !n || !m2 || !n2)
12581 return false;
12582
12583 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12584
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012585 if (n > n2) {
12586 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012587 m2 <<= 1;
12588 n2 <<= 1;
12589 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012590 } else if (n < n2) {
12591 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012592 m <<= 1;
12593 n <<= 1;
12594 }
12595 }
12596
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012597 if (n != n2)
12598 return false;
12599
12600 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012601}
12602
12603static bool
12604intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12605 struct intel_link_m_n *m2_n2,
12606 bool adjust)
12607{
12608 if (m_n->tu == m2_n2->tu &&
12609 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12610 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12611 intel_compare_m_n(m_n->link_m, m_n->link_n,
12612 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12613 if (adjust)
12614 *m2_n2 = *m_n;
12615
12616 return true;
12617 }
12618
12619 return false;
12620}
12621
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012622static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012623intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012624 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012625 struct intel_crtc_state *pipe_config,
12626 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012627{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012628 bool ret = true;
12629
12630#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12631 do { \
12632 if (!adjust) \
12633 DRM_ERROR(fmt, ##__VA_ARGS__); \
12634 else \
12635 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12636 } while (0)
12637
Daniel Vetter66e985c2013-06-05 13:34:20 +020012638#define PIPE_CONF_CHECK_X(name) \
12639 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012640 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012641 "(expected 0x%08x, found 0x%08x)\n", \
12642 current_config->name, \
12643 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012644 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012645 }
12646
Daniel Vetter08a24032013-04-19 11:25:34 +020012647#define PIPE_CONF_CHECK_I(name) \
12648 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012649 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012650 "(expected %i, found %i)\n", \
12651 current_config->name, \
12652 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012653 ret = false; \
12654 }
12655
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012656#define PIPE_CONF_CHECK_P(name) \
12657 if (current_config->name != pipe_config->name) { \
12658 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12659 "(expected %p, found %p)\n", \
12660 current_config->name, \
12661 pipe_config->name); \
12662 ret = false; \
12663 }
12664
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012665#define PIPE_CONF_CHECK_M_N(name) \
12666 if (!intel_compare_link_m_n(&current_config->name, \
12667 &pipe_config->name,\
12668 adjust)) { \
12669 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12670 "(expected tu %i gmch %i/%i link %i/%i, " \
12671 "found tu %i, gmch %i/%i link %i/%i)\n", \
12672 current_config->name.tu, \
12673 current_config->name.gmch_m, \
12674 current_config->name.gmch_n, \
12675 current_config->name.link_m, \
12676 current_config->name.link_n, \
12677 pipe_config->name.tu, \
12678 pipe_config->name.gmch_m, \
12679 pipe_config->name.gmch_n, \
12680 pipe_config->name.link_m, \
12681 pipe_config->name.link_n); \
12682 ret = false; \
12683 }
12684
Daniel Vetter55c561a2016-03-30 11:34:36 +020012685/* This is required for BDW+ where there is only one set of registers for
12686 * switching between high and low RR.
12687 * This macro can be used whenever a comparison has to be made between one
12688 * hw state and multiple sw state variables.
12689 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012690#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12691 if (!intel_compare_link_m_n(&current_config->name, \
12692 &pipe_config->name, adjust) && \
12693 !intel_compare_link_m_n(&current_config->alt_name, \
12694 &pipe_config->name, adjust)) { \
12695 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12696 "(expected tu %i gmch %i/%i link %i/%i, " \
12697 "or tu %i gmch %i/%i link %i/%i, " \
12698 "found tu %i, gmch %i/%i link %i/%i)\n", \
12699 current_config->name.tu, \
12700 current_config->name.gmch_m, \
12701 current_config->name.gmch_n, \
12702 current_config->name.link_m, \
12703 current_config->name.link_n, \
12704 current_config->alt_name.tu, \
12705 current_config->alt_name.gmch_m, \
12706 current_config->alt_name.gmch_n, \
12707 current_config->alt_name.link_m, \
12708 current_config->alt_name.link_n, \
12709 pipe_config->name.tu, \
12710 pipe_config->name.gmch_m, \
12711 pipe_config->name.gmch_n, \
12712 pipe_config->name.link_m, \
12713 pipe_config->name.link_n); \
12714 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012715 }
12716
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012717#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12718 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012719 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012720 "(expected %i, found %i)\n", \
12721 current_config->name & (mask), \
12722 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012723 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012724 }
12725
Ville Syrjälä5e550652013-09-06 23:29:07 +030012726#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12727 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012728 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012729 "(expected %i, found %i)\n", \
12730 current_config->name, \
12731 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012732 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012733 }
12734
Daniel Vetterbb760062013-06-06 14:55:52 +020012735#define PIPE_CONF_QUIRK(quirk) \
12736 ((current_config->quirks | pipe_config->quirks) & (quirk))
12737
Daniel Vettereccb1402013-05-22 00:50:22 +020012738 PIPE_CONF_CHECK_I(cpu_transcoder);
12739
Daniel Vetter08a24032013-04-19 11:25:34 +020012740 PIPE_CONF_CHECK_I(has_pch_encoder);
12741 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012742 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012743
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012744 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012745 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012746
12747 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012748 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012749
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012750 if (current_config->has_drrs)
12751 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12752 } else
12753 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012754
Jani Nikulaa65347b2015-11-27 12:21:46 +020012755 PIPE_CONF_CHECK_I(has_dsi_encoder);
12756
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012757 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12758 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12759 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12760 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12761 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12762 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012763
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012764 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12765 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12766 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12767 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12768 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12769 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012770
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012771 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020012772 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012773 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012774 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012775 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012776 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012777
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012778 PIPE_CONF_CHECK_I(has_audio);
12779
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012780 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012781 DRM_MODE_FLAG_INTERLACE);
12782
Daniel Vetterbb760062013-06-06 14:55:52 +020012783 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012784 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012785 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012786 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012787 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012788 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012789 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012790 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012791 DRM_MODE_FLAG_NVSYNC);
12792 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012793
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012794 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012795 /* pfit ratios are autocomputed by the hw on gen4+ */
12796 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020012797 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012798 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012799
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012800 if (!adjust) {
12801 PIPE_CONF_CHECK_I(pipe_src_w);
12802 PIPE_CONF_CHECK_I(pipe_src_h);
12803
12804 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12805 if (current_config->pch_pfit.enabled) {
12806 PIPE_CONF_CHECK_X(pch_pfit.pos);
12807 PIPE_CONF_CHECK_X(pch_pfit.size);
12808 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012809
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012810 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12811 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012812
Jesse Barnese59150d2014-01-07 13:30:45 -080012813 /* BDW+ don't expose a synchronous way to read the state */
12814 if (IS_HASWELL(dev))
12815 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012816
Ville Syrjälä282740f2013-09-04 18:30:03 +030012817 PIPE_CONF_CHECK_I(double_wide);
12818
Daniel Vetter26804af2014-06-25 22:01:55 +030012819 PIPE_CONF_CHECK_X(ddi_pll_sel);
12820
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012821 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012822 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012823 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012824 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12825 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012826 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012827 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012828 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12829 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12830 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012831
Ville Syrjälä47eacba2016-04-12 22:14:35 +030012832 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12833 PIPE_CONF_CHECK_X(dsi_pll.div);
12834
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012835 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12836 PIPE_CONF_CHECK_I(pipe_bpp);
12837
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012838 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012839 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012840
Daniel Vetter66e985c2013-06-05 13:34:20 +020012841#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012842#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012843#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012844#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012845#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012846#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012847#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012848
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012849 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012850}
12851
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012852static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12853 const struct intel_crtc_state *pipe_config)
12854{
12855 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012856 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012857 &pipe_config->fdi_m_n);
12858 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12859
12860 /*
12861 * FDI already provided one idea for the dotclock.
12862 * Yell if the encoder disagrees.
12863 */
12864 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12865 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12866 fdi_dotclock, dotclock);
12867 }
12868}
12869
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012870static void verify_wm_state(struct drm_crtc *crtc,
12871 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000012872{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012873 struct drm_device *dev = crtc->dev;
Damien Lespiau08db6652014-11-04 17:06:52 +000012874 struct drm_i915_private *dev_priv = dev->dev_private;
12875 struct skl_ddb_allocation hw_ddb, *sw_ddb;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012876 struct skl_ddb_entry *hw_entry, *sw_entry;
12877 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12878 const enum pipe pipe = intel_crtc->pipe;
Damien Lespiau08db6652014-11-04 17:06:52 +000012879 int plane;
12880
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012881 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000012882 return;
12883
12884 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12885 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12886
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012887 /* planes */
12888 for_each_plane(dev_priv, pipe, plane) {
12889 hw_entry = &hw_ddb.plane[pipe][plane];
12890 sw_entry = &sw_ddb->plane[pipe][plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000012891
12892 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12893 continue;
12894
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012895 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12896 "(expected (%u,%u), found (%u,%u))\n",
12897 pipe_name(pipe), plane + 1,
12898 sw_entry->start, sw_entry->end,
12899 hw_entry->start, hw_entry->end);
12900 }
12901
12902 /* cursor */
12903 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12904 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
12905
12906 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012907 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12908 "(expected (%u,%u), found (%u,%u))\n",
12909 pipe_name(pipe),
12910 sw_entry->start, sw_entry->end,
12911 hw_entry->start, hw_entry->end);
12912 }
12913}
12914
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012915static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012916verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012917{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012918 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012919
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012920 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012921 struct drm_encoder *encoder = connector->encoder;
12922 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012923
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012924 if (state->crtc != crtc)
12925 continue;
12926
Daniel Vetter5a21b662016-05-24 17:13:53 +020012927 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012928
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012929 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012930 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012931 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012932}
12933
12934static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012935verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012936{
12937 struct intel_encoder *encoder;
12938 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012939
Damien Lespiaub2784e12014-08-05 11:29:37 +010012940 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012941 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012942 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012943
12944 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12945 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012946 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012947
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012948 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012949 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012950 continue;
12951 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012952
12953 I915_STATE_WARN(connector->base.state->crtc !=
12954 encoder->base.crtc,
12955 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012956 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012957
Rob Clarke2c719b2014-12-15 13:56:32 -050012958 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012959 "encoder's enabled state mismatch "
12960 "(expected %i, found %i)\n",
12961 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012962
12963 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012964 bool active;
12965
12966 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012967 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012968 "encoder detached but still enabled on pipe %c.\n",
12969 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012970 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012971 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012972}
12973
12974static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012975verify_crtc_state(struct drm_crtc *crtc,
12976 struct drm_crtc_state *old_crtc_state,
12977 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012978{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012979 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012980 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012981 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12983 struct intel_crtc_state *pipe_config, *sw_config;
12984 struct drm_atomic_state *old_state;
12985 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012986
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012987 old_state = old_crtc_state->state;
12988 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12989 pipe_config = to_intel_crtc_state(old_crtc_state);
12990 memset(pipe_config, 0, sizeof(*pipe_config));
12991 pipe_config->base.crtc = crtc;
12992 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012993
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012994 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012995
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012996 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012997
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012998 /* hw state is inconsistent with the pipe quirk */
12999 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13000 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13001 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013002
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013003 I915_STATE_WARN(new_crtc_state->active != active,
13004 "crtc active state doesn't match with hw state "
13005 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013006
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013007 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13008 "transitional active state does not match atomic hw state "
13009 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013010
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013011 for_each_encoder_on_crtc(dev, crtc, encoder) {
13012 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013013
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013014 active = encoder->get_hw_state(encoder, &pipe);
13015 I915_STATE_WARN(active != new_crtc_state->active,
13016 "[ENCODER:%i] active %i with crtc active %i\n",
13017 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013018
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013019 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13020 "Encoder connected to wrong pipe %c\n",
13021 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013022
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013023 if (active)
13024 encoder->get_config(encoder, pipe_config);
13025 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013026
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013027 if (!new_crtc_state->active)
13028 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013029
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013030 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013031
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013032 sw_config = to_intel_crtc_state(crtc->state);
13033 if (!intel_pipe_config_compare(dev, sw_config,
13034 pipe_config, false)) {
13035 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13036 intel_dump_pipe_config(intel_crtc, pipe_config,
13037 "[hw state]");
13038 intel_dump_pipe_config(intel_crtc, sw_config,
13039 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013040 }
13041}
13042
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013043static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013044verify_single_dpll_state(struct drm_i915_private *dev_priv,
13045 struct intel_shared_dpll *pll,
13046 struct drm_crtc *crtc,
13047 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013048{
13049 struct intel_dpll_hw_state dpll_hw_state;
13050 unsigned crtc_mask;
13051 bool active;
13052
13053 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13054
13055 DRM_DEBUG_KMS("%s\n", pll->name);
13056
13057 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
13058
13059 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13060 I915_STATE_WARN(!pll->on && pll->active_mask,
13061 "pll in active use but not on in sw tracking\n");
13062 I915_STATE_WARN(pll->on && !pll->active_mask,
13063 "pll is on but not used by any active crtc\n");
13064 I915_STATE_WARN(pll->on != active,
13065 "pll on state mismatch (expected %i, found %i)\n",
13066 pll->on, active);
13067 }
13068
13069 if (!crtc) {
13070 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13071 "more active pll users than references: %x vs %x\n",
13072 pll->active_mask, pll->config.crtc_mask);
13073
13074 return;
13075 }
13076
13077 crtc_mask = 1 << drm_crtc_index(crtc);
13078
13079 if (new_state->active)
13080 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13081 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13082 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13083 else
13084 I915_STATE_WARN(pll->active_mask & crtc_mask,
13085 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13086 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13087
13088 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13089 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13090 crtc_mask, pll->config.crtc_mask);
13091
13092 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13093 &dpll_hw_state,
13094 sizeof(dpll_hw_state)),
13095 "pll hw state mismatch\n");
13096}
13097
13098static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013099verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13100 struct drm_crtc_state *old_crtc_state,
13101 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013102{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013103 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013104 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13105 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13106
13107 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013108 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013109
13110 if (old_state->shared_dpll &&
13111 old_state->shared_dpll != new_state->shared_dpll) {
13112 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13113 struct intel_shared_dpll *pll = old_state->shared_dpll;
13114
13115 I915_STATE_WARN(pll->active_mask & crtc_mask,
13116 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13117 pipe_name(drm_crtc_index(crtc)));
13118 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13119 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13120 pipe_name(drm_crtc_index(crtc)));
13121 }
13122}
13123
13124static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013125intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013126 struct drm_crtc_state *old_state,
13127 struct drm_crtc_state *new_state)
13128{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013129 if (!needs_modeset(new_state) &&
13130 !to_intel_crtc_state(new_state)->update_pipe)
13131 return;
13132
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013133 verify_wm_state(crtc, new_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013134 verify_connector_state(crtc->dev, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013135 verify_crtc_state(crtc, old_state, new_state);
13136 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013137}
13138
13139static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013140verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013141{
13142 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013143 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013144
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013145 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013146 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013147}
Daniel Vetter53589012013-06-05 13:34:16 +020013148
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013149static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013150intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013151{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013152 verify_encoder_state(dev);
13153 verify_connector_state(dev, NULL);
13154 verify_disabled_dpll_state(dev);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013155}
13156
Ville Syrjälä80715b22014-05-15 20:23:23 +030013157static void update_scanline_offset(struct intel_crtc *crtc)
13158{
13159 struct drm_device *dev = crtc->base.dev;
13160
13161 /*
13162 * The scanline counter increments at the leading edge of hsync.
13163 *
13164 * On most platforms it starts counting from vtotal-1 on the
13165 * first active line. That means the scanline counter value is
13166 * always one less than what we would expect. Ie. just after
13167 * start of vblank, which also occurs at start of hsync (on the
13168 * last active line), the scanline counter will read vblank_start-1.
13169 *
13170 * On gen2 the scanline counter starts counting from 1 instead
13171 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13172 * to keep the value positive), instead of adding one.
13173 *
13174 * On HSW+ the behaviour of the scanline counter depends on the output
13175 * type. For DP ports it behaves like most other platforms, but on HDMI
13176 * there's an extra 1 line difference. So we need to add two instead of
13177 * one to the value.
13178 */
13179 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013180 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013181 int vtotal;
13182
Ville Syrjälä124abe02015-09-08 13:40:45 +030013183 vtotal = adjusted_mode->crtc_vtotal;
13184 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013185 vtotal /= 2;
13186
13187 crtc->scanline_offset = vtotal - 1;
13188 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013189 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013190 crtc->scanline_offset = 2;
13191 } else
13192 crtc->scanline_offset = 1;
13193}
13194
Maarten Lankhorstad421372015-06-15 12:33:42 +020013195static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013196{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013197 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013198 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013199 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013200 struct drm_crtc *crtc;
13201 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013202 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013203
13204 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013205 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013206
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013207 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013208 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013209 struct intel_shared_dpll *old_dpll =
13210 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013211
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013212 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013213 continue;
13214
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013215 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013216
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013217 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013218 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013219
Maarten Lankhorstad421372015-06-15 12:33:42 +020013220 if (!shared_dpll)
13221 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13222
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013223 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013224 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013225}
13226
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013227/*
13228 * This implements the workaround described in the "notes" section of the mode
13229 * set sequence documentation. When going from no pipes or single pipe to
13230 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13231 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13232 */
13233static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13234{
13235 struct drm_crtc_state *crtc_state;
13236 struct intel_crtc *intel_crtc;
13237 struct drm_crtc *crtc;
13238 struct intel_crtc_state *first_crtc_state = NULL;
13239 struct intel_crtc_state *other_crtc_state = NULL;
13240 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13241 int i;
13242
13243 /* look at all crtc's that are going to be enabled in during modeset */
13244 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13245 intel_crtc = to_intel_crtc(crtc);
13246
13247 if (!crtc_state->active || !needs_modeset(crtc_state))
13248 continue;
13249
13250 if (first_crtc_state) {
13251 other_crtc_state = to_intel_crtc_state(crtc_state);
13252 break;
13253 } else {
13254 first_crtc_state = to_intel_crtc_state(crtc_state);
13255 first_pipe = intel_crtc->pipe;
13256 }
13257 }
13258
13259 /* No workaround needed? */
13260 if (!first_crtc_state)
13261 return 0;
13262
13263 /* w/a possibly needed, check how many crtc's are already enabled. */
13264 for_each_intel_crtc(state->dev, intel_crtc) {
13265 struct intel_crtc_state *pipe_config;
13266
13267 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13268 if (IS_ERR(pipe_config))
13269 return PTR_ERR(pipe_config);
13270
13271 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13272
13273 if (!pipe_config->base.active ||
13274 needs_modeset(&pipe_config->base))
13275 continue;
13276
13277 /* 2 or more enabled crtcs means no need for w/a */
13278 if (enabled_pipe != INVALID_PIPE)
13279 return 0;
13280
13281 enabled_pipe = intel_crtc->pipe;
13282 }
13283
13284 if (enabled_pipe != INVALID_PIPE)
13285 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13286 else if (other_crtc_state)
13287 other_crtc_state->hsw_workaround_pipe = first_pipe;
13288
13289 return 0;
13290}
13291
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013292static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13293{
13294 struct drm_crtc *crtc;
13295 struct drm_crtc_state *crtc_state;
13296 int ret = 0;
13297
13298 /* add all active pipes to the state */
13299 for_each_crtc(state->dev, crtc) {
13300 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13301 if (IS_ERR(crtc_state))
13302 return PTR_ERR(crtc_state);
13303
13304 if (!crtc_state->active || needs_modeset(crtc_state))
13305 continue;
13306
13307 crtc_state->mode_changed = true;
13308
13309 ret = drm_atomic_add_affected_connectors(state, crtc);
13310 if (ret)
13311 break;
13312
13313 ret = drm_atomic_add_affected_planes(state, crtc);
13314 if (ret)
13315 break;
13316 }
13317
13318 return ret;
13319}
13320
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013321static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013322{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013323 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13324 struct drm_i915_private *dev_priv = state->dev->dev_private;
13325 struct drm_crtc *crtc;
13326 struct drm_crtc_state *crtc_state;
13327 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013328
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013329 if (!check_digital_port_conflicts(state)) {
13330 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13331 return -EINVAL;
13332 }
13333
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013334 intel_state->modeset = true;
13335 intel_state->active_crtcs = dev_priv->active_crtcs;
13336
13337 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13338 if (crtc_state->active)
13339 intel_state->active_crtcs |= 1 << i;
13340 else
13341 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070013342
13343 if (crtc_state->active != crtc->state->active)
13344 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013345 }
13346
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013347 /*
13348 * See if the config requires any additional preparation, e.g.
13349 * to adjust global state with pipes off. We need to do this
13350 * here so we can get the modeset_pipe updated config for the new
13351 * mode set on this crtc. For other crtcs we need to use the
13352 * adjusted_mode bits in the crtc directly.
13353 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013354 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030013355 if (!intel_state->cdclk_pll_vco)
Ville Syrjälä63911d72016-05-13 23:41:32 +030013356 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +030013357 if (!intel_state->cdclk_pll_vco)
13358 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013359
Clint Taylorc89e39f2016-05-13 23:41:21 +030013360 ret = dev_priv->display.modeset_calc_cdclk(state);
13361 if (ret < 0)
13362 return ret;
13363
13364 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013365 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013366 ret = intel_modeset_all_pipes(state);
13367
13368 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013369 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013370
13371 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13372 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013373 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013374 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013375
Maarten Lankhorstad421372015-06-15 12:33:42 +020013376 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013377
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013378 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013379 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013380
Maarten Lankhorstad421372015-06-15 12:33:42 +020013381 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013382}
13383
Matt Roperaa363132015-09-24 15:53:18 -070013384/*
13385 * Handle calculation of various watermark data at the end of the atomic check
13386 * phase. The code here should be run after the per-crtc and per-plane 'check'
13387 * handlers to ensure that all derived state has been updated.
13388 */
Matt Roper55994c22016-05-12 07:06:08 -070013389static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070013390{
13391 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070013392 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070013393
13394 /* Is there platform-specific watermark information to calculate? */
13395 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070013396 return dev_priv->display.compute_global_watermarks(state);
13397
13398 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070013399}
13400
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013401/**
13402 * intel_atomic_check - validate state object
13403 * @dev: drm device
13404 * @state: state to validate
13405 */
13406static int intel_atomic_check(struct drm_device *dev,
13407 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013408{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013409 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013410 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013411 struct drm_crtc *crtc;
13412 struct drm_crtc_state *crtc_state;
13413 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013414 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013415
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013416 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013417 if (ret)
13418 return ret;
13419
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013420 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013421 struct intel_crtc_state *pipe_config =
13422 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013423
13424 /* Catch I915_MODE_FLAG_INHERITED */
13425 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13426 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013427
Daniel Vetter26495482015-07-15 14:15:52 +020013428 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013429 continue;
13430
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013431 if (!crtc_state->enable) {
13432 any_ms = true;
13433 continue;
13434 }
13435
Daniel Vetter26495482015-07-15 14:15:52 +020013436 /* FIXME: For only active_changed we shouldn't need to do any
13437 * state recomputation at all. */
13438
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013439 ret = drm_atomic_add_affected_connectors(state, crtc);
13440 if (ret)
13441 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013442
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013443 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013444 if (ret) {
13445 intel_dump_pipe_config(to_intel_crtc(crtc),
13446 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013447 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013448 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013449
Jani Nikula73831232015-11-19 10:26:30 +020013450 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013451 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013452 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013453 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013454 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013455 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013456 }
13457
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013458 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020013459 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013460
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013461 ret = drm_atomic_add_affected_planes(state, crtc);
13462 if (ret)
13463 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013464
Daniel Vetter26495482015-07-15 14:15:52 +020013465 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13466 needs_modeset(crtc_state) ?
13467 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013468 }
13469
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013470 if (any_ms) {
13471 ret = intel_modeset_checks(state);
13472
13473 if (ret)
13474 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013475 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013476 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013477
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013478 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013479 if (ret)
13480 return ret;
13481
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013482 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070013483 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013484}
13485
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013486static int intel_atomic_prepare_commit(struct drm_device *dev,
13487 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013488 bool nonblock)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013489{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013490 struct drm_i915_private *dev_priv = dev->dev_private;
13491 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013492 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013493 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013494 struct drm_crtc *crtc;
13495 int i, ret;
13496
Daniel Vetter5a21b662016-05-24 17:13:53 +020013497 if (nonblock) {
13498 DRM_DEBUG_KMS("i915 does not yet support nonblocking commit\n");
13499 return -EINVAL;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013500 }
13501
Daniel Vetter5a21b662016-05-24 17:13:53 +020013502 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13503 if (state->legacy_cursor_update)
13504 continue;
13505
13506 ret = intel_crtc_wait_for_pending_flips(crtc);
13507 if (ret)
13508 return ret;
13509
13510 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13511 flush_workqueue(dev_priv->wq);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013512 }
13513
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013514 ret = mutex_lock_interruptible(&dev->struct_mutex);
13515 if (ret)
13516 return ret;
13517
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013518 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013519 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013520
Dave Airlie21daaee2016-05-05 09:56:30 +100013521 if (!ret && !nonblock) {
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013522 for_each_plane_in_state(state, plane, plane_state, i) {
13523 struct intel_plane_state *intel_plane_state =
13524 to_intel_plane_state(plane_state);
13525
13526 if (!intel_plane_state->wait_req)
13527 continue;
13528
13529 ret = __i915_wait_request(intel_plane_state->wait_req,
Chris Wilson299259a2016-04-13 17:35:06 +010013530 true, NULL, NULL);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013531 if (ret) {
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013532 /* Any hang should be swallowed by the wait */
13533 WARN_ON(ret == -EIO);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013534 mutex_lock(&dev->struct_mutex);
13535 drm_atomic_helper_cleanup_planes(dev, state);
13536 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013537 break;
Chris Wilsonf7e58382016-04-13 17:35:07 +010013538 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013539 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013540 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013541
13542 return ret;
13543}
13544
Maarten Lankhorsta2991412016-05-17 15:07:48 +020013545u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13546{
13547 struct drm_device *dev = crtc->base.dev;
13548
13549 if (!dev->max_vblank_count)
13550 return drm_accurate_vblank_count(&crtc->base);
13551
13552 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13553}
13554
Daniel Vetter5a21b662016-05-24 17:13:53 +020013555static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13556 struct drm_i915_private *dev_priv,
13557 unsigned crtc_mask)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013558{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013559 unsigned last_vblank_count[I915_MAX_PIPES];
13560 enum pipe pipe;
13561 int ret;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013562
Daniel Vetter5a21b662016-05-24 17:13:53 +020013563 if (!crtc_mask)
13564 return;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013565
Daniel Vetter5a21b662016-05-24 17:13:53 +020013566 for_each_pipe(dev_priv, pipe) {
13567 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorste8861672016-02-24 11:24:26 +010013568
Daniel Vetter5a21b662016-05-24 17:13:53 +020013569 if (!((1 << pipe) & crtc_mask))
Maarten Lankhorste8861672016-02-24 11:24:26 +010013570 continue;
13571
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013572 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013573 if (WARN_ON(ret != 0)) {
13574 crtc_mask &= ~(1 << pipe);
13575 continue;
13576 }
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013577
Daniel Vetter5a21b662016-05-24 17:13:53 +020013578 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13579 }
13580
13581 for_each_pipe(dev_priv, pipe) {
13582 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13583 long lret;
13584
13585 if (!((1 << pipe) & crtc_mask))
13586 continue;
13587
13588 lret = wait_event_timeout(dev->vblank[pipe].queue,
13589 last_vblank_count[pipe] !=
13590 drm_crtc_vblank_count(crtc),
13591 msecs_to_jiffies(50));
13592
13593 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
13594
13595 drm_crtc_vblank_put(crtc);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013596 }
13597}
13598
Daniel Vetter5a21b662016-05-24 17:13:53 +020013599static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013600{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013601 /* fb updated, need to unpin old fb */
13602 if (crtc_state->fb_changed)
13603 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013604
Daniel Vetter5a21b662016-05-24 17:13:53 +020013605 /* wm changes, need vblank before final wm's */
13606 if (crtc_state->update_wm_post)
13607 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013608
Daniel Vetter5a21b662016-05-24 17:13:53 +020013609 /*
13610 * cxsr is re-enabled after vblank.
13611 * This is already handled by crtc_state->update_wm_post,
13612 * but added for clarity.
13613 */
13614 if (crtc_state->disable_cxsr)
13615 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013616
Daniel Vetter5a21b662016-05-24 17:13:53 +020013617 return false;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013618}
13619
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013620/**
13621 * intel_atomic_commit - commit validated state object
13622 * @dev: DRM device
13623 * @state: the top-level driver state object
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013624 * @nonblock: nonblocking commit
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013625 *
13626 * This function commits a top-level state object that has been validated
13627 * with drm_atomic_helper_check().
13628 *
13629 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13630 * we can only handle plane-related operations and do not yet support
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013631 * nonblocking commit.
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013632 *
13633 * RETURNS
13634 * Zero for success or -errno.
13635 */
13636static int intel_atomic_commit(struct drm_device *dev,
13637 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013638 bool nonblock)
Daniel Vettera6778b32012-07-02 09:56:42 +020013639{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013640 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013641 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013642 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013643 struct drm_crtc *crtc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013644 struct intel_crtc_state *intel_cstate;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013645 int ret = 0, i;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013646 bool hw_check = intel_state->modeset;
13647 unsigned long put_domains[I915_MAX_PIPES] = {};
13648 unsigned crtc_vblank_mask = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013649
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013650 ret = intel_atomic_prepare_commit(dev, state, nonblock);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013651 if (ret) {
13652 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013653 return ret;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013654 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013655
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013656 drm_atomic_helper_swap_state(dev, state);
Matt Roper279e99d2016-05-12 07:06:02 -070013657 dev_priv->wm.distrust_bios_wm = false;
Matt Roper734fa012016-05-12 15:11:40 -070013658 dev_priv->wm.skl_results = intel_state->wm_results;
Maarten Lankhorsta1475e72016-03-14 09:27:53 +010013659 intel_shared_dpll_commit(state);
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013660
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013661 if (intel_state->modeset) {
13662 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13663 sizeof(intel_state->min_pixclk));
13664 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013665 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013666
13667 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013668 }
13669
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013670 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013671 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13672
Daniel Vetter5a21b662016-05-24 17:13:53 +020013673 if (needs_modeset(crtc->state) ||
13674 to_intel_crtc_state(crtc->state)->update_pipe) {
13675 hw_check = true;
13676
13677 put_domains[to_intel_crtc(crtc)->pipe] =
13678 modeset_get_crtc_power_domains(crtc,
13679 to_intel_crtc_state(crtc->state));
13680 }
13681
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013682 if (!needs_modeset(crtc->state))
13683 continue;
13684
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013685 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013686
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013687 if (old_crtc_state->active) {
13688 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013689 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013690 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013691 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013692 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013693
13694 /*
13695 * Underruns don't always raise
13696 * interrupts, so check manually.
13697 */
13698 intel_check_cpu_fifo_underruns(dev_priv);
13699 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013700
13701 if (!crtc->state->active)
13702 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013703 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013704 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013705
Daniel Vetterea9d7582012-07-10 10:42:52 +020013706 /* Only after disabling all output pipelines that will be changed can we
13707 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013708 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013709
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013710 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013711 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013712
13713 if (dev_priv->display.modeset_commit_cdclk &&
Clint Taylorc89e39f2016-05-13 23:41:21 +030013714 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013715 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013716 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013717
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013718 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013719 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013720
Daniel Vettera6778b32012-07-02 09:56:42 +020013721 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013722 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013723 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13724 bool modeset = needs_modeset(crtc->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013725 struct intel_crtc_state *pipe_config =
13726 to_intel_crtc_state(crtc->state);
13727 bool update_pipe = !modeset && pipe_config->update_pipe;
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013728
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013729 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013730 update_scanline_offset(to_intel_crtc(crtc));
13731 dev_priv->display.crtc_enable(crtc);
13732 }
13733
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013734 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013735 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013736
Daniel Vetter5a21b662016-05-24 17:13:53 +020013737 if (crtc->state->active &&
13738 drm_atomic_get_existing_plane_state(state, crtc->primary))
13739 intel_fbc_enable(intel_crtc);
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013740
Daniel Vetter5a21b662016-05-24 17:13:53 +020013741 if (crtc->state->active &&
13742 (crtc->state->planes_changed || update_pipe))
13743 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013744
Daniel Vetter5a21b662016-05-24 17:13:53 +020013745 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13746 crtc_vblank_mask |= 1 << i;
Matt Ropered4a6a72016-02-23 17:20:13 -080013747 }
13748
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013749 /* FIXME: add subpixel order */
13750
Daniel Vetter5a21b662016-05-24 17:13:53 +020013751 if (!state->legacy_cursor_update)
13752 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13753
13754 /*
13755 * Now that the vblank has passed, we can go ahead and program the
13756 * optimal watermarks on platforms that need two-step watermark
13757 * programming.
13758 *
13759 * TODO: Move this (and other cleanup) to an async worker eventually.
13760 */
13761 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13762 intel_cstate = to_intel_crtc_state(crtc->state);
13763
13764 if (dev_priv->display.optimize_watermarks)
13765 dev_priv->display.optimize_watermarks(intel_cstate);
13766 }
13767
13768 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13769 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13770
13771 if (put_domains[i])
13772 modeset_put_power_domains(dev_priv, put_domains[i]);
13773
13774 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13775 }
13776
13777 if (intel_state->modeset)
13778 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13779
13780 mutex_lock(&dev->struct_mutex);
13781 drm_atomic_helper_cleanup_planes(dev, state);
13782 mutex_unlock(&dev->struct_mutex);
13783
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013784 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013785
Mika Kuoppala75714942015-12-16 09:26:48 +020013786 /* As one of the primary mmio accessors, KMS has a high likelihood
13787 * of triggering bugs in unclaimed access. After we finish
13788 * modesetting, see if an error has been flagged, and if so
13789 * enable debugging for the next modeset - and hope we catch
13790 * the culprit.
13791 *
13792 * XXX note that we assume display power is on at this point.
13793 * This might hold true now but we need to add pm helper to check
13794 * unclaimed only when the hardware is on, as atomic commits
13795 * can happen also when the device is completely off.
13796 */
13797 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
13798
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013799 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013800}
13801
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013802void intel_crtc_restore_mode(struct drm_crtc *crtc)
13803{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013804 struct drm_device *dev = crtc->dev;
13805 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013806 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013807 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013808
13809 state = drm_atomic_state_alloc(dev);
13810 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013811 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013812 crtc->base.id);
13813 return;
13814 }
13815
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013816 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013817
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013818retry:
13819 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13820 ret = PTR_ERR_OR_ZERO(crtc_state);
13821 if (!ret) {
13822 if (!crtc_state->active)
13823 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013824
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013825 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013826 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013827 }
13828
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013829 if (ret == -EDEADLK) {
13830 drm_atomic_state_clear(state);
13831 drm_modeset_backoff(state->acquire_ctx);
13832 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013833 }
13834
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013835 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013836out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013837 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013838}
13839
Daniel Vetter25c5b262012-07-08 22:08:04 +020013840#undef for_each_intel_crtc_masked
13841
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013842static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013843 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013844 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013845 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013846 .destroy = intel_crtc_destroy,
Daniel Vetter5a21b662016-05-24 17:13:53 +020013847 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013848 .atomic_duplicate_state = intel_crtc_duplicate_state,
13849 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013850};
13851
Matt Roper6beb8c232014-12-01 15:40:14 -080013852/**
13853 * intel_prepare_plane_fb - Prepare fb for usage on plane
13854 * @plane: drm plane to prepare for
13855 * @fb: framebuffer to prepare for presentation
13856 *
13857 * Prepares a framebuffer for usage on a display plane. Generally this
13858 * involves pinning the underlying object and updating the frontbuffer tracking
13859 * bits. Some older platforms need special physical address handling for
13860 * cursor planes.
13861 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013862 * Must be called with struct_mutex held.
13863 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013864 * Returns 0 on success, negative error code on failure.
13865 */
13866int
13867intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013868 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013869{
13870 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013871 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013872 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013873 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013874 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013875 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013876
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013877 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013878 return 0;
13879
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013880 if (old_obj) {
13881 struct drm_crtc_state *crtc_state =
13882 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13883
13884 /* Big Hammer, we also need to ensure that any pending
13885 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13886 * current scanout is retired before unpinning the old
13887 * framebuffer. Note that we rely on userspace rendering
13888 * into the buffer attached to the pipe they are waiting
13889 * on. If not, userspace generates a GPU hang with IPEHR
13890 * point to the MI_WAIT_FOR_EVENT.
13891 *
13892 * This should only fail upon a hung GPU, in which case we
13893 * can safely continue.
13894 */
13895 if (needs_modeset(crtc_state))
13896 ret = i915_gem_object_wait_rendering(old_obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013897 if (ret) {
13898 /* GPU hangs should have been swallowed by the wait */
13899 WARN_ON(ret == -EIO);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013900 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013901 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013902 }
13903
Daniel Vetter5a21b662016-05-24 17:13:53 +020013904 /* For framebuffer backed by dmabuf, wait for fence */
13905 if (obj && obj->base.dma_buf) {
13906 long lret;
13907
13908 lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
13909 false, true,
13910 MAX_SCHEDULE_TIMEOUT);
13911 if (lret == -ERESTARTSYS)
13912 return lret;
13913
13914 WARN(lret < 0, "waiting returns %li\n", lret);
13915 }
13916
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013917 if (!obj) {
13918 ret = 0;
13919 } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080013920 INTEL_INFO(dev)->cursor_needs_physical) {
13921 int align = IS_I830(dev) ? 16 * 1024 : 256;
13922 ret = i915_gem_object_attach_phys(obj, align);
13923 if (ret)
13924 DRM_DEBUG_KMS("failed to attach phys object\n");
13925 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020013926 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080013927 }
13928
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013929 if (ret == 0) {
13930 if (obj) {
13931 struct intel_plane_state *plane_state =
13932 to_intel_plane_state(new_state);
13933
13934 i915_gem_request_assign(&plane_state->wait_req,
13935 obj->last_write_req);
13936 }
13937
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013938 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013939 }
Matt Roper6beb8c232014-12-01 15:40:14 -080013940
Matt Roper6beb8c232014-12-01 15:40:14 -080013941 return ret;
13942}
13943
Matt Roper38f3ce32014-12-02 07:45:25 -080013944/**
13945 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13946 * @plane: drm plane to clean up for
13947 * @fb: old framebuffer that was on plane
13948 *
13949 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013950 *
13951 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080013952 */
13953void
13954intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013955 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013956{
13957 struct drm_device *dev = plane->dev;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013958 struct intel_plane *intel_plane = to_intel_plane(plane);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013959 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013960 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
13961 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080013962
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013963 old_intel_state = to_intel_plane_state(old_state);
13964
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013965 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080013966 return;
13967
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013968 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
13969 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020013970 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013971
13972 /* prepare_fb aborted? */
13973 if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
13974 (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
13975 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013976
13977 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070013978}
13979
Chandra Konduru6156a452015-04-27 13:48:39 -070013980int
13981skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13982{
13983 int max_scale;
13984 struct drm_device *dev;
13985 struct drm_i915_private *dev_priv;
13986 int crtc_clock, cdclk;
13987
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010013988 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070013989 return DRM_PLANE_HELPER_NO_SCALING;
13990
13991 dev = intel_crtc->base.dev;
13992 dev_priv = dev->dev_private;
13993 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013994 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013995
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010013996 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070013997 return DRM_PLANE_HELPER_NO_SCALING;
13998
13999 /*
14000 * skl max scale is lower of:
14001 * close to 3 but not 3, -1 is for that purpose
14002 * or
14003 * cdclk/crtc_clock
14004 */
14005 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14006
14007 return max_scale;
14008}
14009
Matt Roper465c1202014-05-29 08:06:54 -070014010static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014011intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014012 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014013 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014014{
Matt Roper2b875c22014-12-01 15:40:13 -080014015 struct drm_crtc *crtc = state->base.crtc;
14016 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070014017 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014018 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14019 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014020
Ville Syrjälä693bdc22016-01-15 20:46:53 +020014021 if (INTEL_INFO(plane->dev)->gen >= 9) {
14022 /* use scaler when colorkey is not required */
14023 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14024 min_scale = 1;
14025 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14026 }
Sonika Jindald8106362015-04-10 14:37:28 +053014027 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014028 }
Sonika Jindald8106362015-04-10 14:37:28 +053014029
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014030 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14031 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014032 min_scale, max_scale,
14033 can_position, true,
14034 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070014035}
14036
Daniel Vetter5a21b662016-05-24 17:13:53 +020014037static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14038 struct drm_crtc_state *old_crtc_state)
14039{
14040 struct drm_device *dev = crtc->dev;
14041 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14042 struct intel_crtc_state *old_intel_state =
14043 to_intel_crtc_state(old_crtc_state);
14044 bool modeset = needs_modeset(crtc->state);
14045
14046 /* Perform vblank evasion around commit operation */
14047 intel_pipe_update_start(intel_crtc);
14048
14049 if (modeset)
14050 return;
14051
14052 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14053 intel_color_set_csc(crtc->state);
14054 intel_color_load_luts(crtc->state);
14055 }
14056
14057 if (to_intel_crtc_state(crtc->state)->update_pipe)
14058 intel_update_pipe_config(intel_crtc, old_intel_state);
14059 else if (INTEL_INFO(dev)->gen >= 9)
14060 skl_detach_scalers(intel_crtc);
14061}
14062
14063static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14064 struct drm_crtc_state *old_crtc_state)
14065{
14066 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14067
14068 intel_pipe_update_end(intel_crtc, NULL);
14069}
14070
Matt Ropercf4c7c12014-12-04 10:27:42 -080014071/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014072 * intel_plane_destroy - destroy a plane
14073 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014074 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014075 * Common destruction function for all types of planes (primary, cursor,
14076 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014077 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014078void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014079{
14080 struct intel_plane *intel_plane = to_intel_plane(plane);
14081 drm_plane_cleanup(plane);
14082 kfree(intel_plane);
14083}
14084
Matt Roper65a3fea2015-01-21 16:35:42 -080014085const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014086 .update_plane = drm_atomic_helper_update_plane,
14087 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014088 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014089 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014090 .atomic_get_property = intel_plane_atomic_get_property,
14091 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014092 .atomic_duplicate_state = intel_plane_duplicate_state,
14093 .atomic_destroy_state = intel_plane_destroy_state,
14094
Matt Roper465c1202014-05-29 08:06:54 -070014095};
14096
14097static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14098 int pipe)
14099{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014100 struct intel_plane *primary = NULL;
14101 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014102 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020014103 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014104 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014105
14106 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014107 if (!primary)
14108 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070014109
Matt Roper8e7d6882015-01-21 16:35:41 -080014110 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014111 if (!state)
14112 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014113 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014114
Matt Roper465c1202014-05-29 08:06:54 -070014115 primary->can_scale = false;
14116 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014117 if (INTEL_INFO(dev)->gen >= 9) {
14118 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014119 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014120 }
Matt Roper465c1202014-05-29 08:06:54 -070014121 primary->pipe = pipe;
14122 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014123 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014124 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014125 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14126 primary->plane = !pipe;
14127
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014128 if (INTEL_INFO(dev)->gen >= 9) {
14129 intel_primary_formats = skl_primary_formats;
14130 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014131
14132 primary->update_plane = skylake_update_primary_plane;
14133 primary->disable_plane = skylake_disable_primary_plane;
14134 } else if (HAS_PCH_SPLIT(dev)) {
14135 intel_primary_formats = i965_primary_formats;
14136 num_formats = ARRAY_SIZE(i965_primary_formats);
14137
14138 primary->update_plane = ironlake_update_primary_plane;
14139 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014140 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014141 intel_primary_formats = i965_primary_formats;
14142 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014143
14144 primary->update_plane = i9xx_update_primary_plane;
14145 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014146 } else {
14147 intel_primary_formats = i8xx_primary_formats;
14148 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014149
14150 primary->update_plane = i9xx_update_primary_plane;
14151 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014152 }
14153
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014154 ret = drm_universal_plane_init(dev, &primary->base, 0,
14155 &intel_plane_funcs,
14156 intel_primary_formats, num_formats,
14157 DRM_PLANE_TYPE_PRIMARY, NULL);
14158 if (ret)
14159 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053014160
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014161 if (INTEL_INFO(dev)->gen >= 4)
14162 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014163
Matt Roperea2c67b2014-12-23 10:41:52 -080014164 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14165
Matt Roper465c1202014-05-29 08:06:54 -070014166 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014167
14168fail:
14169 kfree(state);
14170 kfree(primary);
14171
14172 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014173}
14174
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014175void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14176{
14177 if (!dev->mode_config.rotation_property) {
14178 unsigned long flags = BIT(DRM_ROTATE_0) |
14179 BIT(DRM_ROTATE_180);
14180
14181 if (INTEL_INFO(dev)->gen >= 9)
14182 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14183
14184 dev->mode_config.rotation_property =
14185 drm_mode_create_rotation_property(dev, flags);
14186 }
14187 if (dev->mode_config.rotation_property)
14188 drm_object_attach_property(&plane->base.base,
14189 dev->mode_config.rotation_property,
14190 plane->base.state->rotation);
14191}
14192
Matt Roper3d7d6512014-06-10 08:28:13 -070014193static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014194intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014195 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014196 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014197{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014198 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014199 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014200 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014201 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014202 unsigned stride;
14203 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014204
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014205 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14206 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014207 DRM_PLANE_HELPER_NO_SCALING,
14208 DRM_PLANE_HELPER_NO_SCALING,
14209 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014210 if (ret)
14211 return ret;
14212
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014213 /* if we want to turn off the cursor ignore width and height */
14214 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014215 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014216
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014217 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014218 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014219 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14220 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014221 return -EINVAL;
14222 }
14223
Matt Roperea2c67b2014-12-23 10:41:52 -080014224 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14225 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014226 DRM_DEBUG_KMS("buffer is too small\n");
14227 return -ENOMEM;
14228 }
14229
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014230 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014231 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014232 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014233 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014234
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014235 /*
14236 * There's something wrong with the cursor on CHV pipe C.
14237 * If it straddles the left edge of the screen then
14238 * moving it away from the edge or disabling it often
14239 * results in a pipe underrun, and often that can lead to
14240 * dead pipe (constant underrun reported, and it scans
14241 * out just a solid color). To recover from that, the
14242 * display power well must be turned off and on again.
14243 * Refuse the put the cursor into that compromised position.
14244 */
14245 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14246 state->visible && state->base.crtc_x < 0) {
14247 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14248 return -EINVAL;
14249 }
14250
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014251 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014252}
14253
Matt Roperf4a2cf22014-12-01 15:40:12 -080014254static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014255intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014256 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014257{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014258 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14259
14260 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014261 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014262}
14263
14264static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014265intel_update_cursor_plane(struct drm_plane *plane,
14266 const struct intel_crtc_state *crtc_state,
14267 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014268{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014269 struct drm_crtc *crtc = crtc_state->base.crtc;
14270 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014271 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014272 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014273 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014274
Matt Roperf4a2cf22014-12-01 15:40:12 -080014275 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014276 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014277 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014278 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014279 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014280 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014281
Gustavo Padovana912f122014-12-01 15:40:10 -080014282 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014283 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014284}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014285
Matt Roper3d7d6512014-06-10 08:28:13 -070014286static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14287 int pipe)
14288{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014289 struct intel_plane *cursor = NULL;
14290 struct intel_plane_state *state = NULL;
14291 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070014292
14293 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014294 if (!cursor)
14295 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070014296
Matt Roper8e7d6882015-01-21 16:35:41 -080014297 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014298 if (!state)
14299 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014300 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014301
Matt Roper3d7d6512014-06-10 08:28:13 -070014302 cursor->can_scale = false;
14303 cursor->max_downscale = 1;
14304 cursor->pipe = pipe;
14305 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014306 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014307 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014308 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014309 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014310
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014311 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14312 &intel_plane_funcs,
14313 intel_cursor_formats,
14314 ARRAY_SIZE(intel_cursor_formats),
14315 DRM_PLANE_TYPE_CURSOR, NULL);
14316 if (ret)
14317 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014318
14319 if (INTEL_INFO(dev)->gen >= 4) {
14320 if (!dev->mode_config.rotation_property)
14321 dev->mode_config.rotation_property =
14322 drm_mode_create_rotation_property(dev,
14323 BIT(DRM_ROTATE_0) |
14324 BIT(DRM_ROTATE_180));
14325 if (dev->mode_config.rotation_property)
14326 drm_object_attach_property(&cursor->base.base,
14327 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014328 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014329 }
14330
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014331 if (INTEL_INFO(dev)->gen >=9)
14332 state->scaler_id = -1;
14333
Matt Roperea2c67b2014-12-23 10:41:52 -080014334 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14335
Matt Roper3d7d6512014-06-10 08:28:13 -070014336 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014337
14338fail:
14339 kfree(state);
14340 kfree(cursor);
14341
14342 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014343}
14344
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014345static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14346 struct intel_crtc_state *crtc_state)
14347{
14348 int i;
14349 struct intel_scaler *intel_scaler;
14350 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14351
14352 for (i = 0; i < intel_crtc->num_scalers; i++) {
14353 intel_scaler = &scaler_state->scalers[i];
14354 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014355 intel_scaler->mode = PS_SCALER_MODE_DYN;
14356 }
14357
14358 scaler_state->scaler_id = -1;
14359}
14360
Hannes Ederb358d0a2008-12-18 21:18:47 +010014361static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014362{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014363 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014364 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014365 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014366 struct drm_plane *primary = NULL;
14367 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014368 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014369
Daniel Vetter955382f2013-09-19 14:05:45 +020014370 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014371 if (intel_crtc == NULL)
14372 return;
14373
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014374 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14375 if (!crtc_state)
14376 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014377 intel_crtc->config = crtc_state;
14378 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014379 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014380
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014381 /* initialize shared scalers */
14382 if (INTEL_INFO(dev)->gen >= 9) {
14383 if (pipe == PIPE_C)
14384 intel_crtc->num_scalers = 1;
14385 else
14386 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14387
14388 skl_init_scalers(dev, intel_crtc, crtc_state);
14389 }
14390
Matt Roper465c1202014-05-29 08:06:54 -070014391 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014392 if (!primary)
14393 goto fail;
14394
14395 cursor = intel_cursor_plane_create(dev, pipe);
14396 if (!cursor)
14397 goto fail;
14398
Matt Roper465c1202014-05-29 08:06:54 -070014399 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjäläf9882872015-12-09 16:19:31 +020014400 cursor, &intel_crtc_funcs, NULL);
Matt Roper3d7d6512014-06-10 08:28:13 -070014401 if (ret)
14402 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014403
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014404 /*
14405 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014406 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014407 */
Jesse Barnes80824002009-09-10 15:28:06 -070014408 intel_crtc->pipe = pipe;
14409 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014410 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014411 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014412 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014413 }
14414
Chris Wilson4b0e3332014-05-30 16:35:26 +030014415 intel_crtc->cursor_base = ~0;
14416 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014417 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014418
Ville Syrjälä852eb002015-06-24 22:00:07 +030014419 intel_crtc->wm.cxsr_allowed = true;
14420
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014421 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14422 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14423 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14424 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14425
Jesse Barnes79e53942008-11-07 14:24:08 -080014426 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014427
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014428 intel_color_init(&intel_crtc->base);
14429
Daniel Vetter87b6b102014-05-15 15:33:46 +020014430 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014431 return;
14432
14433fail:
14434 if (primary)
14435 drm_plane_cleanup(primary);
14436 if (cursor)
14437 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014438 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014439 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014440}
14441
Jesse Barnes752aa882013-10-31 18:55:49 +020014442enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14443{
14444 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014445 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014446
Rob Clark51fd3712013-11-19 12:10:12 -050014447 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014448
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014449 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014450 return INVALID_PIPE;
14451
14452 return to_intel_crtc(encoder->crtc)->pipe;
14453}
14454
Carl Worth08d7b3d2009-04-29 14:43:54 -070014455int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014456 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014457{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014458 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014459 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014460 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014461
Rob Clark7707e652014-07-17 23:30:04 -040014462 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014463
Rob Clark7707e652014-07-17 23:30:04 -040014464 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014465 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014466 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014467 }
14468
Rob Clark7707e652014-07-17 23:30:04 -040014469 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014470 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014471
Daniel Vetterc05422d2009-08-11 16:05:30 +020014472 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014473}
14474
Daniel Vetter66a92782012-07-12 20:08:18 +020014475static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014476{
Daniel Vetter66a92782012-07-12 20:08:18 +020014477 struct drm_device *dev = encoder->base.dev;
14478 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014479 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014480 int entry = 0;
14481
Damien Lespiaub2784e12014-08-05 11:29:37 +010014482 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014483 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014484 index_mask |= (1 << entry);
14485
Jesse Barnes79e53942008-11-07 14:24:08 -080014486 entry++;
14487 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014488
Jesse Barnes79e53942008-11-07 14:24:08 -080014489 return index_mask;
14490}
14491
Chris Wilson4d302442010-12-14 19:21:29 +000014492static bool has_edp_a(struct drm_device *dev)
14493{
14494 struct drm_i915_private *dev_priv = dev->dev_private;
14495
14496 if (!IS_MOBILE(dev))
14497 return false;
14498
14499 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14500 return false;
14501
Damien Lespiaue3589902014-02-07 19:12:50 +000014502 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014503 return false;
14504
14505 return true;
14506}
14507
Jesse Barnes84b4e042014-06-25 08:24:29 -070014508static bool intel_crt_present(struct drm_device *dev)
14509{
14510 struct drm_i915_private *dev_priv = dev->dev_private;
14511
Damien Lespiau884497e2013-12-03 13:56:23 +000014512 if (INTEL_INFO(dev)->gen >= 9)
14513 return false;
14514
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014515 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014516 return false;
14517
14518 if (IS_CHERRYVIEW(dev))
14519 return false;
14520
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014521 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14522 return false;
14523
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014524 /* DDI E can't be used if DDI A requires 4 lanes */
14525 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14526 return false;
14527
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014528 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014529 return false;
14530
14531 return true;
14532}
14533
Jesse Barnes79e53942008-11-07 14:24:08 -080014534static void intel_setup_outputs(struct drm_device *dev)
14535{
Eric Anholt725e30a2009-01-22 13:01:02 -080014536 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014537 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014538 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014539
Daniel Vetterc9093352013-06-06 22:22:47 +020014540 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014541
Jesse Barnes84b4e042014-06-25 08:24:29 -070014542 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014543 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014544
Vandana Kannanc776eb22014-08-19 12:05:01 +053014545 if (IS_BROXTON(dev)) {
14546 /*
14547 * FIXME: Broxton doesn't support port detection via the
14548 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14549 * detect the ports.
14550 */
14551 intel_ddi_init(dev, PORT_A);
14552 intel_ddi_init(dev, PORT_B);
14553 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014554
14555 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014556 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014557 int found;
14558
Jesse Barnesde31fac2015-03-06 15:53:32 -080014559 /*
14560 * Haswell uses DDI functions to detect digital outputs.
14561 * On SKL pre-D0 the strap isn't connected, so we assume
14562 * it's there.
14563 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014564 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014565 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014566 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014567 intel_ddi_init(dev, PORT_A);
14568
14569 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14570 * register */
14571 found = I915_READ(SFUSE_STRAP);
14572
14573 if (found & SFUSE_STRAP_DDIB_DETECTED)
14574 intel_ddi_init(dev, PORT_B);
14575 if (found & SFUSE_STRAP_DDIC_DETECTED)
14576 intel_ddi_init(dev, PORT_C);
14577 if (found & SFUSE_STRAP_DDID_DETECTED)
14578 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014579 /*
14580 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14581 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014582 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014583 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14584 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14585 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14586 intel_ddi_init(dev, PORT_E);
14587
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014588 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014589 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014590 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014591
14592 if (has_edp_a(dev))
14593 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014594
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014595 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014596 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014597 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014598 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014599 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014600 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014601 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014602 }
14603
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014604 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014605 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014606
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014607 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014608 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014609
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014610 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014611 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014612
Daniel Vetter270b3042012-10-27 15:52:05 +020014613 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014614 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014615 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014616 /*
14617 * The DP_DETECTED bit is the latched state of the DDC
14618 * SDA pin at boot. However since eDP doesn't require DDC
14619 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14620 * eDP ports may have been muxed to an alternate function.
14621 * Thus we can't rely on the DP_DETECTED bit alone to detect
14622 * eDP ports. Consult the VBT as well as DP_DETECTED to
14623 * detect eDP ports.
14624 */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014625 if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014626 !intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014627 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14628 if (I915_READ(VLV_DP_B) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014629 intel_dp_is_edp(dev, PORT_B))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014630 intel_dp_init(dev, VLV_DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014631
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014632 if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014633 !intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014634 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14635 if (I915_READ(VLV_DP_C) & DP_DETECTED ||
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014636 intel_dp_is_edp(dev, PORT_C))
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014637 intel_dp_init(dev, VLV_DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014638
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014639 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014640 /* eDP not supported on port D, so don't check VBT */
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014641 if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14642 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14643 if (I915_READ(CHV_DP_D) & DP_DETECTED)
14644 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014645 }
14646
Jani Nikula3cfca972013-08-27 15:12:26 +030014647 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014648 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014649 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014650
Paulo Zanonie2debe92013-02-18 19:00:27 -030014651 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014652 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014653 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014654 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014655 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014656 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014657 }
Ma Ling27185ae2009-08-24 13:50:23 +080014658
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014659 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014660 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014661 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014662
14663 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014664
Paulo Zanonie2debe92013-02-18 19:00:27 -030014665 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014666 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014667 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014668 }
Ma Ling27185ae2009-08-24 13:50:23 +080014669
Paulo Zanonie2debe92013-02-18 19:00:27 -030014670 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014671
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014672 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014673 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014674 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014675 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014676 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014677 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014678 }
Ma Ling27185ae2009-08-24 13:50:23 +080014679
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014680 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014681 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014682 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014683 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014684 intel_dvo_init(dev);
14685
Zhenyu Wang103a1962009-11-27 11:44:36 +080014686 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014687 intel_tv_init(dev);
14688
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014689 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014690
Damien Lespiaub2784e12014-08-05 11:29:37 +010014691 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014692 encoder->base.possible_crtcs = encoder->crtc_mask;
14693 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014694 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014695 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014696
Paulo Zanonidde86e22012-12-01 12:04:25 -020014697 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014698
14699 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014700}
14701
14702static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14703{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014704 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014705 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014706
Daniel Vetteref2d6332014-02-10 18:00:38 +010014707 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014708 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014709 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014710 drm_gem_object_unreference(&intel_fb->obj->base);
14711 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014712 kfree(intel_fb);
14713}
14714
14715static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014716 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014717 unsigned int *handle)
14718{
14719 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014720 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014721
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014722 if (obj->userptr.mm) {
14723 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14724 return -EINVAL;
14725 }
14726
Chris Wilson05394f32010-11-08 19:18:58 +000014727 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014728}
14729
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014730static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14731 struct drm_file *file,
14732 unsigned flags, unsigned color,
14733 struct drm_clip_rect *clips,
14734 unsigned num_clips)
14735{
14736 struct drm_device *dev = fb->dev;
14737 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14738 struct drm_i915_gem_object *obj = intel_fb->obj;
14739
14740 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014741 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014742 mutex_unlock(&dev->struct_mutex);
14743
14744 return 0;
14745}
14746
Jesse Barnes79e53942008-11-07 14:24:08 -080014747static const struct drm_framebuffer_funcs intel_fb_funcs = {
14748 .destroy = intel_user_framebuffer_destroy,
14749 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014750 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014751};
14752
Damien Lespiaub3218032015-02-27 11:15:18 +000014753static
14754u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14755 uint32_t pixel_format)
14756{
14757 u32 gen = INTEL_INFO(dev)->gen;
14758
14759 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014760 int cpp = drm_format_plane_cpp(pixel_format, 0);
14761
Damien Lespiaub3218032015-02-27 11:15:18 +000014762 /* "The stride in bytes must not exceed the of the size of 8K
14763 * pixels and 32K bytes."
14764 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014765 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014766 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014767 return 32*1024;
14768 } else if (gen >= 4) {
14769 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14770 return 16*1024;
14771 else
14772 return 32*1024;
14773 } else if (gen >= 3) {
14774 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14775 return 8*1024;
14776 else
14777 return 16*1024;
14778 } else {
14779 /* XXX DSPC is limited to 4k tiled */
14780 return 8*1024;
14781 }
14782}
14783
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014784static int intel_framebuffer_init(struct drm_device *dev,
14785 struct intel_framebuffer *intel_fb,
14786 struct drm_mode_fb_cmd2 *mode_cmd,
14787 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014788{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014789 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014790 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014791 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014792 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014793
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014794 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14795
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014796 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14797 /* Enforce that fb modifier and tiling mode match, but only for
14798 * X-tiled. This is needed for FBC. */
14799 if (!!(obj->tiling_mode == I915_TILING_X) !=
14800 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14801 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14802 return -EINVAL;
14803 }
14804 } else {
14805 if (obj->tiling_mode == I915_TILING_X)
14806 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14807 else if (obj->tiling_mode == I915_TILING_Y) {
14808 DRM_DEBUG("No Y tiling for legacy addfb\n");
14809 return -EINVAL;
14810 }
14811 }
14812
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014813 /* Passed in modifier sanity checking. */
14814 switch (mode_cmd->modifier[0]) {
14815 case I915_FORMAT_MOD_Y_TILED:
14816 case I915_FORMAT_MOD_Yf_TILED:
14817 if (INTEL_INFO(dev)->gen < 9) {
14818 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14819 mode_cmd->modifier[0]);
14820 return -EINVAL;
14821 }
14822 case DRM_FORMAT_MOD_NONE:
14823 case I915_FORMAT_MOD_X_TILED:
14824 break;
14825 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014826 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14827 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014828 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014829 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014830
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014831 stride_alignment = intel_fb_stride_alignment(dev_priv,
14832 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014833 mode_cmd->pixel_format);
14834 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14835 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14836 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014837 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014838 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014839
Damien Lespiaub3218032015-02-27 11:15:18 +000014840 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14841 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014842 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014843 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14844 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014845 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014846 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014847 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014848 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014849
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014850 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014851 mode_cmd->pitches[0] != obj->stride) {
14852 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14853 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014854 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014855 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014856
Ville Syrjälä57779d02012-10-31 17:50:14 +020014857 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014858 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014859 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014860 case DRM_FORMAT_RGB565:
14861 case DRM_FORMAT_XRGB8888:
14862 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014863 break;
14864 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014865 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014866 DRM_DEBUG("unsupported pixel format: %s\n",
14867 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014868 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014869 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014870 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014871 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014872 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14873 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014874 DRM_DEBUG("unsupported pixel format: %s\n",
14875 drm_get_format_name(mode_cmd->pixel_format));
14876 return -EINVAL;
14877 }
14878 break;
14879 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014880 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014881 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014882 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014883 DRM_DEBUG("unsupported pixel format: %s\n",
14884 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014885 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014886 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014887 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014888 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080014889 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010014890 DRM_DEBUG("unsupported pixel format: %s\n",
14891 drm_get_format_name(mode_cmd->pixel_format));
14892 return -EINVAL;
14893 }
14894 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014895 case DRM_FORMAT_YUYV:
14896 case DRM_FORMAT_UYVY:
14897 case DRM_FORMAT_YVYU:
14898 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014899 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014900 DRM_DEBUG("unsupported pixel format: %s\n",
14901 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014902 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014903 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014904 break;
14905 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014906 DRM_DEBUG("unsupported pixel format: %s\n",
14907 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014908 return -EINVAL;
14909 }
14910
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014911 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14912 if (mode_cmd->offsets[0] != 0)
14913 return -EINVAL;
14914
Damien Lespiauec2c9812015-01-20 12:51:45 +000014915 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014916 mode_cmd->pixel_format,
14917 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014918 /* FIXME drm helper for size checks (especially planar formats)? */
14919 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14920 return -EINVAL;
14921
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014922 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14923 intel_fb->obj = obj;
14924
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020014925 intel_fill_fb_info(dev_priv, &intel_fb->base);
14926
Jesse Barnes79e53942008-11-07 14:24:08 -080014927 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14928 if (ret) {
14929 DRM_ERROR("framebuffer init failed %d\n", ret);
14930 return ret;
14931 }
14932
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020014933 intel_fb->obj->framebuffer_references++;
14934
Jesse Barnes79e53942008-11-07 14:24:08 -080014935 return 0;
14936}
14937
Jesse Barnes79e53942008-11-07 14:24:08 -080014938static struct drm_framebuffer *
14939intel_user_framebuffer_create(struct drm_device *dev,
14940 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020014941 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014942{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014943 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000014944 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014945 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080014946
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014947 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
Ville Syrjälä76dc3762015-11-11 19:11:28 +020014948 mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014949 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014950 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014951
Daniel Vetter92907cb2015-11-23 09:04:05 +010014952 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020014953 if (IS_ERR(fb))
14954 drm_gem_object_unreference_unlocked(&obj->base);
14955
14956 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080014957}
14958
Daniel Vetter06957262015-08-10 13:34:08 +020014959#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014960static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014961{
14962}
14963#endif
14964
Jesse Barnes79e53942008-11-07 14:24:08 -080014965static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014966 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014967 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014968 .atomic_check = intel_atomic_check,
14969 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014970 .atomic_state_alloc = intel_atomic_state_alloc,
14971 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014972};
14973
Imre Deak88212942016-03-16 13:38:53 +020014974/**
14975 * intel_init_display_hooks - initialize the display modesetting hooks
14976 * @dev_priv: device private
14977 */
14978void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070014979{
Imre Deak88212942016-03-16 13:38:53 +020014980 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014981 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014982 dev_priv->display.get_initial_plane_config =
14983 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014984 dev_priv->display.crtc_compute_clock =
14985 haswell_crtc_compute_clock;
14986 dev_priv->display.crtc_enable = haswell_crtc_enable;
14987 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014988 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014989 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014990 dev_priv->display.get_initial_plane_config =
14991 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014992 dev_priv->display.crtc_compute_clock =
14993 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014994 dev_priv->display.crtc_enable = haswell_crtc_enable;
14995 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020014996 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014997 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014998 dev_priv->display.get_initial_plane_config =
14999 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020015000 dev_priv->display.crtc_compute_clock =
15001 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015002 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15003 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015004 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070015005 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015006 dev_priv->display.get_initial_plane_config =
15007 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015008 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15009 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15010 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15011 } else if (IS_VALLEYVIEW(dev_priv)) {
15012 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15013 dev_priv->display.get_initial_plane_config =
15014 i9xx_get_initial_plane_config;
15015 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070015016 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15017 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020015018 } else if (IS_G4X(dev_priv)) {
15019 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15020 dev_priv->display.get_initial_plane_config =
15021 i9xx_get_initial_plane_config;
15022 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15023 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15024 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020015025 } else if (IS_PINEVIEW(dev_priv)) {
15026 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15027 dev_priv->display.get_initial_plane_config =
15028 i9xx_get_initial_plane_config;
15029 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15030 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15031 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015032 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015033 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015034 dev_priv->display.get_initial_plane_config =
15035 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020015036 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015037 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15038 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015039 } else {
15040 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15041 dev_priv->display.get_initial_plane_config =
15042 i9xx_get_initial_plane_config;
15043 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15044 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15045 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070015046 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015047
Jesse Barnese70236a2009-09-21 10:42:27 -070015048 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020015049 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015050 dev_priv->display.get_display_clock_speed =
15051 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015052 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070015053 dev_priv->display.get_display_clock_speed =
15054 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015055 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015056 dev_priv->display.get_display_clock_speed =
15057 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015058 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015059 dev_priv->display.get_display_clock_speed =
15060 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015061 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070015062 dev_priv->display.get_display_clock_speed =
15063 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015064 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030015065 dev_priv->display.get_display_clock_speed =
15066 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015067 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15068 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015069 dev_priv->display.get_display_clock_speed =
15070 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015071 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015072 dev_priv->display.get_display_clock_speed =
15073 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015074 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015075 dev_priv->display.get_display_clock_speed =
15076 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015077 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015078 dev_priv->display.get_display_clock_speed =
15079 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015080 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015081 dev_priv->display.get_display_clock_speed =
15082 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015083 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015084 dev_priv->display.get_display_clock_speed =
15085 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015086 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015087 dev_priv->display.get_display_clock_speed =
15088 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015089 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015090 dev_priv->display.get_display_clock_speed =
15091 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015092 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015093 dev_priv->display.get_display_clock_speed =
15094 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015095 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015096 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030015097 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015098 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020015099 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070015100 dev_priv->display.get_display_clock_speed =
15101 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015102 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015103
Imre Deak88212942016-03-16 13:38:53 +020015104 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015105 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015106 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015107 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015108 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015109 /* FIXME: detect B0+ stepping and use auto training */
15110 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015111 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015112 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030015113 }
15114
15115 if (IS_BROADWELL(dev_priv)) {
15116 dev_priv->display.modeset_commit_cdclk =
15117 broadwell_modeset_commit_cdclk;
15118 dev_priv->display.modeset_calc_cdclk =
15119 broadwell_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015120 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015121 dev_priv->display.modeset_commit_cdclk =
15122 valleyview_modeset_commit_cdclk;
15123 dev_priv->display.modeset_calc_cdclk =
15124 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015125 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015126 dev_priv->display.modeset_commit_cdclk =
15127 broxton_modeset_commit_cdclk;
15128 dev_priv->display.modeset_calc_cdclk =
15129 broxton_modeset_calc_cdclk;
Clint Taylorc89e39f2016-05-13 23:41:21 +030015130 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15131 dev_priv->display.modeset_commit_cdclk =
15132 skl_modeset_commit_cdclk;
15133 dev_priv->display.modeset_calc_cdclk =
15134 skl_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015135 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020015136
15137 switch (INTEL_INFO(dev_priv)->gen) {
15138 case 2:
15139 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15140 break;
15141
15142 case 3:
15143 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15144 break;
15145
15146 case 4:
15147 case 5:
15148 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15149 break;
15150
15151 case 6:
15152 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15153 break;
15154 case 7:
15155 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15156 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15157 break;
15158 case 9:
15159 /* Drop through - unsupported since execlist only. */
15160 default:
15161 /* Default just returns -ENODEV to indicate unsupported */
15162 dev_priv->display.queue_flip = intel_default_queue_flip;
15163 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015164}
15165
Jesse Barnesb690e962010-07-19 13:53:12 -070015166/*
15167 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15168 * resume, or other times. This quirk makes sure that's the case for
15169 * affected systems.
15170 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015171static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015172{
15173 struct drm_i915_private *dev_priv = dev->dev_private;
15174
15175 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015176 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015177}
15178
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015179static void quirk_pipeb_force(struct drm_device *dev)
15180{
15181 struct drm_i915_private *dev_priv = dev->dev_private;
15182
15183 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15184 DRM_INFO("applying pipe b force quirk\n");
15185}
15186
Keith Packard435793d2011-07-12 14:56:22 -070015187/*
15188 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15189 */
15190static void quirk_ssc_force_disable(struct drm_device *dev)
15191{
15192 struct drm_i915_private *dev_priv = dev->dev_private;
15193 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015194 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015195}
15196
Carsten Emde4dca20e2012-03-15 15:56:26 +010015197/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015198 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15199 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015200 */
15201static void quirk_invert_brightness(struct drm_device *dev)
15202{
15203 struct drm_i915_private *dev_priv = dev->dev_private;
15204 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015205 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015206}
15207
Scot Doyle9c72cc62014-07-03 23:27:50 +000015208/* Some VBT's incorrectly indicate no backlight is present */
15209static void quirk_backlight_present(struct drm_device *dev)
15210{
15211 struct drm_i915_private *dev_priv = dev->dev_private;
15212 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15213 DRM_INFO("applying backlight present quirk\n");
15214}
15215
Jesse Barnesb690e962010-07-19 13:53:12 -070015216struct intel_quirk {
15217 int device;
15218 int subsystem_vendor;
15219 int subsystem_device;
15220 void (*hook)(struct drm_device *dev);
15221};
15222
Egbert Eich5f85f172012-10-14 15:46:38 +020015223/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15224struct intel_dmi_quirk {
15225 void (*hook)(struct drm_device *dev);
15226 const struct dmi_system_id (*dmi_id_list)[];
15227};
15228
15229static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15230{
15231 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15232 return 1;
15233}
15234
15235static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15236 {
15237 .dmi_id_list = &(const struct dmi_system_id[]) {
15238 {
15239 .callback = intel_dmi_reverse_brightness,
15240 .ident = "NCR Corporation",
15241 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15242 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15243 },
15244 },
15245 { } /* terminating entry */
15246 },
15247 .hook = quirk_invert_brightness,
15248 },
15249};
15250
Ben Widawskyc43b5632012-04-16 14:07:40 -070015251static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015252 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15253 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15254
Jesse Barnesb690e962010-07-19 13:53:12 -070015255 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15256 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15257
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015258 /* 830 needs to leave pipe A & dpll A up */
15259 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15260
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015261 /* 830 needs to leave pipe B & dpll B up */
15262 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15263
Keith Packard435793d2011-07-12 14:56:22 -070015264 /* Lenovo U160 cannot use SSC on LVDS */
15265 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015266
15267 /* Sony Vaio Y cannot use SSC on LVDS */
15268 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015269
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015270 /* Acer Aspire 5734Z must invert backlight brightness */
15271 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15272
15273 /* Acer/eMachines G725 */
15274 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15275
15276 /* Acer/eMachines e725 */
15277 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15278
15279 /* Acer/Packard Bell NCL20 */
15280 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15281
15282 /* Acer Aspire 4736Z */
15283 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015284
15285 /* Acer Aspire 5336 */
15286 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015287
15288 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15289 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015290
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015291 /* Acer C720 Chromebook (Core i3 4005U) */
15292 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15293
jens steinb2a96012014-10-28 20:25:53 +010015294 /* Apple Macbook 2,1 (Core 2 T7400) */
15295 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15296
Jani Nikula1b9448b2015-11-05 11:49:59 +020015297 /* Apple Macbook 4,1 */
15298 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15299
Scot Doyled4967d82014-07-03 23:27:52 +000015300 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15301 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015302
15303 /* HP Chromebook 14 (Celeron 2955U) */
15304 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015305
15306 /* Dell Chromebook 11 */
15307 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015308
15309 /* Dell Chromebook 11 (2015 version) */
15310 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015311};
15312
15313static void intel_init_quirks(struct drm_device *dev)
15314{
15315 struct pci_dev *d = dev->pdev;
15316 int i;
15317
15318 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15319 struct intel_quirk *q = &intel_quirks[i];
15320
15321 if (d->device == q->device &&
15322 (d->subsystem_vendor == q->subsystem_vendor ||
15323 q->subsystem_vendor == PCI_ANY_ID) &&
15324 (d->subsystem_device == q->subsystem_device ||
15325 q->subsystem_device == PCI_ANY_ID))
15326 q->hook(dev);
15327 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015328 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15329 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15330 intel_dmi_quirks[i].hook(dev);
15331 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015332}
15333
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015334/* Disable the VGA plane that we never use */
15335static void i915_disable_vga(struct drm_device *dev)
15336{
15337 struct drm_i915_private *dev_priv = dev->dev_private;
15338 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015339 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015340
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015341 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015342 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015343 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015344 sr1 = inb(VGA_SR_DATA);
15345 outb(sr1 | 1<<5, VGA_SR_DATA);
15346 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15347 udelay(300);
15348
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015349 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015350 POSTING_READ(vga_reg);
15351}
15352
Daniel Vetterf8175862012-04-10 15:50:11 +020015353void intel_modeset_init_hw(struct drm_device *dev)
15354{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015355 struct drm_i915_private *dev_priv = dev->dev_private;
15356
Ville Syrjäläb6283052015-06-03 15:45:07 +030015357 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015358
15359 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15360
Daniel Vetterf8175862012-04-10 15:50:11 +020015361 intel_init_clock_gating(dev);
Chris Wilsondc979972016-05-10 14:10:04 +010015362 intel_enable_gt_powersave(dev_priv);
Daniel Vetterf8175862012-04-10 15:50:11 +020015363}
15364
Matt Roperd93c0372015-12-03 11:37:41 -080015365/*
15366 * Calculate what we think the watermarks should be for the state we've read
15367 * out of the hardware and then immediately program those watermarks so that
15368 * we ensure the hardware settings match our internal state.
15369 *
15370 * We can calculate what we think WM's should be by creating a duplicate of the
15371 * current state (which was constructed during hardware readout) and running it
15372 * through the atomic check code to calculate new watermark values in the
15373 * state object.
15374 */
15375static void sanitize_watermarks(struct drm_device *dev)
15376{
15377 struct drm_i915_private *dev_priv = to_i915(dev);
15378 struct drm_atomic_state *state;
15379 struct drm_crtc *crtc;
15380 struct drm_crtc_state *cstate;
15381 struct drm_modeset_acquire_ctx ctx;
15382 int ret;
15383 int i;
15384
15385 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015386 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015387 return;
15388
15389 /*
15390 * We need to hold connection_mutex before calling duplicate_state so
15391 * that the connector loop is protected.
15392 */
15393 drm_modeset_acquire_init(&ctx, 0);
15394retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015395 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015396 if (ret == -EDEADLK) {
15397 drm_modeset_backoff(&ctx);
15398 goto retry;
15399 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015400 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015401 }
15402
15403 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15404 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015405 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015406
Matt Ropered4a6a72016-02-23 17:20:13 -080015407 /*
15408 * Hardware readout is the only time we don't want to calculate
15409 * intermediate watermarks (since we don't trust the current
15410 * watermarks).
15411 */
15412 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15413
Matt Roperd93c0372015-12-03 11:37:41 -080015414 ret = intel_atomic_check(dev, state);
15415 if (ret) {
15416 /*
15417 * If we fail here, it means that the hardware appears to be
15418 * programmed in a way that shouldn't be possible, given our
15419 * understanding of watermark requirements. This might mean a
15420 * mistake in the hardware readout code or a mistake in the
15421 * watermark calculations for a given platform. Raise a WARN
15422 * so that this is noticeable.
15423 *
15424 * If this actually happens, we'll have to just leave the
15425 * BIOS-programmed watermarks untouched and hope for the best.
15426 */
15427 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015428 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015429 }
15430
15431 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080015432 for_each_crtc_in_state(state, crtc, cstate, i) {
15433 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15434
Matt Ropered4a6a72016-02-23 17:20:13 -080015435 cs->wm.need_postvbl_update = true;
15436 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015437 }
15438
15439 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015440fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015441 drm_modeset_drop_locks(&ctx);
15442 drm_modeset_acquire_fini(&ctx);
15443}
15444
Jesse Barnes79e53942008-11-07 14:24:08 -080015445void intel_modeset_init(struct drm_device *dev)
15446{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015447 struct drm_i915_private *dev_priv = to_i915(dev);
15448 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015449 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015450 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015451 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015452
15453 drm_mode_config_init(dev);
15454
15455 dev->mode_config.min_width = 0;
15456 dev->mode_config.min_height = 0;
15457
Dave Airlie019d96c2011-09-29 16:20:42 +010015458 dev->mode_config.preferred_depth = 24;
15459 dev->mode_config.prefer_shadow = 1;
15460
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015461 dev->mode_config.allow_fb_modifiers = true;
15462
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015463 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015464
Jesse Barnesb690e962010-07-19 13:53:12 -070015465 intel_init_quirks(dev);
15466
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015467 intel_init_pm(dev);
15468
Ben Widawskye3c74752013-04-05 13:12:39 -070015469 if (INTEL_INFO(dev)->num_pipes == 0)
15470 return;
15471
Lukas Wunner69f92f62015-07-15 13:57:35 +020015472 /*
15473 * There may be no VBT; and if the BIOS enabled SSC we can
15474 * just keep using it to avoid unnecessary flicker. Whereas if the
15475 * BIOS isn't using it, don't assume it will work even if the VBT
15476 * indicates as much.
15477 */
15478 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15479 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15480 DREF_SSC1_ENABLE);
15481
15482 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15483 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15484 bios_lvds_use_ssc ? "en" : "dis",
15485 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15486 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15487 }
15488 }
15489
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015490 if (IS_GEN2(dev)) {
15491 dev->mode_config.max_width = 2048;
15492 dev->mode_config.max_height = 2048;
15493 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015494 dev->mode_config.max_width = 4096;
15495 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015496 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015497 dev->mode_config.max_width = 8192;
15498 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015499 }
Damien Lespiau068be562014-03-28 14:17:49 +000015500
Ville Syrjälädc41c152014-08-13 11:57:05 +030015501 if (IS_845G(dev) || IS_I865G(dev)) {
15502 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15503 dev->mode_config.cursor_height = 1023;
15504 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015505 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15506 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15507 } else {
15508 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15509 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15510 }
15511
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015512 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015513
Zhao Yakui28c97732009-10-09 11:39:41 +080015514 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015515 INTEL_INFO(dev)->num_pipes,
15516 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015517
Damien Lespiau055e3932014-08-18 13:49:10 +010015518 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015519 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015520 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015521 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015522 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015523 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015524 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015525 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015526 }
15527
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015528 intel_update_czclk(dev_priv);
15529 intel_update_cdclk(dev);
15530
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015531 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015532
Ville Syrjäläb2045352016-05-13 23:41:27 +030015533 if (dev_priv->max_cdclk_freq == 0)
15534 intel_update_max_cdclk(dev);
15535
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015536 /* Just disable it once at startup */
15537 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015538 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015539
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015540 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015541 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015542 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015543
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015544 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015545 struct intel_initial_plane_config plane_config = {};
15546
Jesse Barnes46f297f2014-03-07 08:57:48 -080015547 if (!crtc->active)
15548 continue;
15549
Jesse Barnes46f297f2014-03-07 08:57:48 -080015550 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015551 * Note that reserving the BIOS fb up front prevents us
15552 * from stuffing other stolen allocations like the ring
15553 * on top. This prevents some ugliness at boot time, and
15554 * can even allow for smooth boot transitions if the BIOS
15555 * fb is large enough for the active pipe configuration.
15556 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015557 dev_priv->display.get_initial_plane_config(crtc,
15558 &plane_config);
15559
15560 /*
15561 * If the fb is shared between multiple heads, we'll
15562 * just get the first one.
15563 */
15564 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015565 }
Matt Roperd93c0372015-12-03 11:37:41 -080015566
15567 /*
15568 * Make sure hardware watermarks really match the state we read out.
15569 * Note that we need to do this after reconstructing the BIOS fb's
15570 * since the watermark calculation done here will use pstate->fb.
15571 */
15572 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015573}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015574
Daniel Vetter7fad7982012-07-04 17:51:47 +020015575static void intel_enable_pipe_a(struct drm_device *dev)
15576{
15577 struct intel_connector *connector;
15578 struct drm_connector *crt = NULL;
15579 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015580 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015581
15582 /* We can't just switch on the pipe A, we need to set things up with a
15583 * proper mode and output configuration. As a gross hack, enable pipe A
15584 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015585 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015586 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15587 crt = &connector->base;
15588 break;
15589 }
15590 }
15591
15592 if (!crt)
15593 return;
15594
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015595 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015596 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015597}
15598
Daniel Vetterfa555832012-10-10 23:14:00 +020015599static bool
15600intel_check_plane_mapping(struct intel_crtc *crtc)
15601{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015602 struct drm_device *dev = crtc->base.dev;
15603 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015604 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015605
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015606 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015607 return true;
15608
Ville Syrjälä649636e2015-09-22 19:50:01 +030015609 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015610
15611 if ((val & DISPLAY_PLANE_ENABLE) &&
15612 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15613 return false;
15614
15615 return true;
15616}
15617
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015618static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15619{
15620 struct drm_device *dev = crtc->base.dev;
15621 struct intel_encoder *encoder;
15622
15623 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15624 return true;
15625
15626 return false;
15627}
15628
Ville Syrjälädd756192016-02-17 21:28:45 +020015629static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15630{
15631 struct drm_device *dev = encoder->base.dev;
15632 struct intel_connector *connector;
15633
15634 for_each_connector_on_encoder(dev, &encoder->base, connector)
15635 return true;
15636
15637 return false;
15638}
15639
Daniel Vetter24929352012-07-02 20:28:59 +020015640static void intel_sanitize_crtc(struct intel_crtc *crtc)
15641{
15642 struct drm_device *dev = crtc->base.dev;
15643 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015644 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015645
Daniel Vetter24929352012-07-02 20:28:59 +020015646 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015647 if (!transcoder_is_dsi(cpu_transcoder)) {
15648 i915_reg_t reg = PIPECONF(cpu_transcoder);
15649
15650 I915_WRITE(reg,
15651 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15652 }
Daniel Vetter24929352012-07-02 20:28:59 +020015653
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015654 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015655 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015656 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015657 struct intel_plane *plane;
15658
Daniel Vetter96256042015-02-13 21:03:42 +010015659 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015660
15661 /* Disable everything but the primary plane */
15662 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15663 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15664 continue;
15665
15666 plane->disable_plane(&plane->base, &crtc->base);
15667 }
Daniel Vetter96256042015-02-13 21:03:42 +010015668 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015669
Daniel Vetter24929352012-07-02 20:28:59 +020015670 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015671 * disable the crtc (and hence change the state) if it is wrong. Note
15672 * that gen4+ has a fixed plane -> pipe mapping. */
15673 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015674 bool plane;
15675
Daniel Vetter24929352012-07-02 20:28:59 +020015676 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15677 crtc->base.base.id);
15678
15679 /* Pipe has the wrong plane attached and the plane is active.
15680 * Temporarily change the plane mapping and disable everything
15681 * ... */
15682 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015683 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015684 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015685 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015686 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015687 }
Daniel Vetter24929352012-07-02 20:28:59 +020015688
Daniel Vetter7fad7982012-07-04 17:51:47 +020015689 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15690 crtc->pipe == PIPE_A && !crtc->active) {
15691 /* BIOS forgot to enable pipe A, this mostly happens after
15692 * resume. Force-enable the pipe to fix this, the update_dpms
15693 * call below we restore the pipe to the right state, but leave
15694 * the required bits on. */
15695 intel_enable_pipe_a(dev);
15696 }
15697
Daniel Vetter24929352012-07-02 20:28:59 +020015698 /* Adjust the state of the output pipe according to whether we
15699 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015700 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015701 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015702
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015703 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015704 /*
15705 * We start out with underrun reporting disabled to avoid races.
15706 * For correct bookkeeping mark this on active crtcs.
15707 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015708 * Also on gmch platforms we dont have any hardware bits to
15709 * disable the underrun reporting. Which means we need to start
15710 * out with underrun reporting disabled also on inactive pipes,
15711 * since otherwise we'll complain about the garbage we read when
15712 * e.g. coming up after runtime pm.
15713 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015714 * No protection against concurrent access is required - at
15715 * worst a fifo underrun happens which also sets this to false.
15716 */
15717 crtc->cpu_fifo_underrun_disabled = true;
15718 crtc->pch_fifo_underrun_disabled = true;
15719 }
Daniel Vetter24929352012-07-02 20:28:59 +020015720}
15721
15722static void intel_sanitize_encoder(struct intel_encoder *encoder)
15723{
15724 struct intel_connector *connector;
15725 struct drm_device *dev = encoder->base.dev;
15726
15727 /* We need to check both for a crtc link (meaning that the
15728 * encoder is active and trying to read from a pipe) and the
15729 * pipe itself being active. */
15730 bool has_active_crtc = encoder->base.crtc &&
15731 to_intel_crtc(encoder->base.crtc)->active;
15732
Ville Syrjälädd756192016-02-17 21:28:45 +020015733 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015734 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15735 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015736 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015737
15738 /* Connector is active, but has no active pipe. This is
15739 * fallout from our resume register restoring. Disable
15740 * the encoder manually again. */
15741 if (encoder->base.crtc) {
15742 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15743 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015744 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015745 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015746 if (encoder->post_disable)
15747 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015748 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015749 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015750
15751 /* Inconsistent output/port/pipe state happens presumably due to
15752 * a bug in one of the get_hw_state functions. Or someplace else
15753 * in our code, like the register restore mess on resume. Clamp
15754 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015755 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015756 if (connector->encoder != encoder)
15757 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015758 connector->base.dpms = DRM_MODE_DPMS_OFF;
15759 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015760 }
15761 }
15762 /* Enabled encoders without active connectors will be fixed in
15763 * the crtc fixup. */
15764}
15765
Imre Deak04098752014-02-18 00:02:16 +020015766void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015767{
15768 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015769 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015770
Imre Deak04098752014-02-18 00:02:16 +020015771 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15772 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15773 i915_disable_vga(dev);
15774 }
15775}
15776
15777void i915_redisable_vga(struct drm_device *dev)
15778{
15779 struct drm_i915_private *dev_priv = dev->dev_private;
15780
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015781 /* This function can be called both from intel_modeset_setup_hw_state or
15782 * at a very early point in our resume sequence, where the power well
15783 * structures are not yet restored. Since this function is at a very
15784 * paranoid "someone might have enabled VGA while we were not looking"
15785 * level, just check if the power well is enabled instead of trying to
15786 * follow the "don't touch the power well if we don't need it" policy
15787 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015788 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015789 return;
15790
Imre Deak04098752014-02-18 00:02:16 +020015791 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015792
15793 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015794}
15795
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015796static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015797{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015798 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015799
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015800 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015801}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015802
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015803/* FIXME read out full plane state for all planes */
15804static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015805{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015806 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015807 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015808 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015809
Matt Roper19b8d382015-09-24 15:53:17 -070015810 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015811 primary_get_hw_state(to_intel_plane(primary));
15812
15813 if (plane_state->visible)
15814 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015815}
15816
Daniel Vetter30e984d2013-06-05 13:34:17 +020015817static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015818{
15819 struct drm_i915_private *dev_priv = dev->dev_private;
15820 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015821 struct intel_crtc *crtc;
15822 struct intel_encoder *encoder;
15823 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015824 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015825
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015826 dev_priv->active_crtcs = 0;
15827
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015828 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015829 struct intel_crtc_state *crtc_state = crtc->config;
15830 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015831
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015832 __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
15833 memset(crtc_state, 0, sizeof(*crtc_state));
15834 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015835
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015836 crtc_state->base.active = crtc_state->base.enable =
15837 dev_priv->display.get_pipe_config(crtc, crtc_state);
15838
15839 crtc->base.enabled = crtc_state->base.enable;
15840 crtc->active = crtc_state->base.active;
15841
15842 if (crtc_state->base.active) {
15843 dev_priv->active_crtcs |= 1 << crtc->pipe;
15844
Clint Taylorc89e39f2016-05-13 23:41:21 +030015845 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015846 pixclk = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjälä9558d152016-05-13 23:41:20 +030015847 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015848 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15849 else
15850 WARN_ON(dev_priv->display.modeset_calc_cdclk);
Ville Syrjälä9558d152016-05-13 23:41:20 +030015851
15852 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15853 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
15854 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015855 }
15856
15857 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015858
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015859 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015860
15861 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15862 crtc->base.base.id,
15863 crtc->active ? "enabled" : "disabled");
15864 }
15865
Daniel Vetter53589012013-06-05 13:34:16 +020015866 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15867 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15868
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015869 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15870 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015871 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015872 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015873 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015874 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020015875 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015876 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020015877
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015878 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015879 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020015880 }
15881
Damien Lespiaub2784e12014-08-05 11:29:37 +010015882 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015883 pipe = 0;
15884
15885 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015886 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15887 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015888 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015889 } else {
15890 encoder->base.crtc = NULL;
15891 }
15892
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015893 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015894 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015895 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015896 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015897 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015898 }
15899
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015900 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015901 if (connector->get_hw_state(connector)) {
15902 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015903
15904 encoder = connector->encoder;
15905 connector->base.encoder = &encoder->base;
15906
15907 if (encoder->base.crtc &&
15908 encoder->base.crtc->state->active) {
15909 /*
15910 * This has to be done during hardware readout
15911 * because anything calling .crtc_disable may
15912 * rely on the connector_mask being accurate.
15913 */
15914 encoder->base.crtc->state->connector_mask |=
15915 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010015916 encoder->base.crtc->state->encoder_mask |=
15917 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010015918 }
15919
Daniel Vetter24929352012-07-02 20:28:59 +020015920 } else {
15921 connector->base.dpms = DRM_MODE_DPMS_OFF;
15922 connector->base.encoder = NULL;
15923 }
15924 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15925 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015926 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015927 connector->base.encoder ? "enabled" : "disabled");
15928 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015929
15930 for_each_intel_crtc(dev, crtc) {
15931 crtc->base.hwmode = crtc->config->base.adjusted_mode;
15932
15933 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15934 if (crtc->base.state->active) {
15935 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15936 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15937 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15938
15939 /*
15940 * The initial mode needs to be set in order to keep
15941 * the atomic core happy. It wants a valid mode if the
15942 * crtc's enabled, so we do the above call.
15943 *
15944 * At this point some state updated by the connectors
15945 * in their ->detect() callback has not run yet, so
15946 * no recalculation can be done yet.
15947 *
15948 * Even if we could do a recalculation and modeset
15949 * right now it would cause a double modeset if
15950 * fbdev or userspace chooses a different initial mode.
15951 *
15952 * If that happens, someone indicated they wanted a
15953 * mode change, which means it's safe to do a full
15954 * recalculation.
15955 */
15956 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030015957
15958 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15959 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015960 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020015961
15962 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030015963 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015964}
15965
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015966/* Scan out the current hw modeset state,
15967 * and sanitizes it to the current state
15968 */
15969static void
15970intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015971{
15972 struct drm_i915_private *dev_priv = dev->dev_private;
15973 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015974 struct intel_crtc *crtc;
15975 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015976 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015977
15978 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015979
15980 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015981 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015982 intel_sanitize_encoder(encoder);
15983 }
15984
Damien Lespiau055e3932014-08-18 13:49:10 +010015985 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015986 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15987 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015988 intel_dump_pipe_config(crtc, crtc->config,
15989 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015990 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015991
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015992 intel_modeset_update_connector_atomic_state(dev);
15993
Daniel Vetter35c95372013-07-17 06:55:04 +020015994 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15995 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15996
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015997 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020015998 continue;
15999
16000 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16001
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016002 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020016003 pll->on = false;
16004 }
16005
Wayne Boyer666a4532015-12-09 12:29:35 -080016006 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030016007 vlv_wm_get_hw_state(dev);
16008 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000016009 skl_wm_get_hw_state(dev);
16010 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030016011 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016012
16013 for_each_intel_crtc(dev, crtc) {
16014 unsigned long put_domains;
16015
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010016016 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016017 if (WARN_ON(put_domains))
16018 modeset_put_power_domains(dev_priv, put_domains);
16019 }
16020 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020016021
16022 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016023}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030016024
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016025void intel_display_resume(struct drm_device *dev)
16026{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016027 struct drm_i915_private *dev_priv = to_i915(dev);
16028 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16029 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016030 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016031 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020016032
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016033 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016034
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016035 /*
16036 * This is a cludge because with real atomic modeset mode_config.mutex
16037 * won't be taken. Unfortunately some probed state like
16038 * audio_codec_enable is still protected by mode_config.mutex, so lock
16039 * it here for now.
16040 */
16041 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016042 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016043
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016044retry:
16045 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016046
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016047 if (ret == 0 && !setup) {
16048 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016049
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016050 intel_modeset_setup_hw_state(dev);
16051 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010016052 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020016053
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016054 if (ret == 0 && state) {
16055 struct drm_crtc_state *crtc_state;
16056 struct drm_crtc *crtc;
16057 int i;
16058
16059 state->acquire_ctx = &ctx;
16060
Ville Syrjäläe3d54572016-05-13 10:10:42 -070016061 /* ignore any reset values/BIOS leftovers in the WM registers */
16062 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16063
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016064 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16065 /*
16066 * Force recalculation even if we restore
16067 * current state. With fast modeset this may not result
16068 * in a modeset when the state is compatible.
16069 */
16070 crtc_state->mode_changed = true;
16071 }
16072
16073 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016074 }
16075
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016076 if (ret == -EDEADLK) {
16077 drm_modeset_backoff(&ctx);
16078 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016079 }
16080
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016081 drm_modeset_drop_locks(&ctx);
16082 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016083 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016084
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016085 if (ret) {
16086 DRM_ERROR("Restoring old state failed with %i\n", ret);
16087 drm_atomic_state_free(state);
16088 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010016089}
16090
16091void intel_modeset_gem_init(struct drm_device *dev)
16092{
Chris Wilsondc979972016-05-10 14:10:04 +010016093 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016094 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070016095 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016096 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080016097
Chris Wilsondc979972016-05-10 14:10:04 +010016098 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016099
Chris Wilson1833b132012-05-09 11:56:28 +010016100 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020016101
Chris Wilson1ee8da62016-05-12 12:43:23 +010016102 intel_setup_overlay(dev_priv);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016103
16104 /*
16105 * Make sure any fbs we allocated at startup are properly
16106 * pinned & fenced. When we do the allocation it's too early
16107 * for this.
16108 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010016109 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070016110 obj = intel_fb_obj(c->primary->fb);
16111 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080016112 continue;
16113
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016114 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020016115 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16116 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016117 mutex_unlock(&dev->struct_mutex);
16118 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016119 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16120 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016121 drm_framebuffer_unreference(c->primary->fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020016122 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016123 c->primary->crtc = c->primary->state->crtc = NULL;
Daniel Vetter5a21b662016-05-24 17:13:53 +020016124 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016125 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016126 }
16127 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016128
16129 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016130}
16131
Imre Deak4932e2c2014-02-11 17:12:48 +020016132void intel_connector_unregister(struct intel_connector *intel_connector)
16133{
16134 struct drm_connector *connector = &intel_connector->base;
16135
16136 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010016137 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016138}
16139
Jesse Barnes79e53942008-11-07 14:24:08 -080016140void intel_modeset_cleanup(struct drm_device *dev)
16141{
Jesse Barnes652c3932009-08-17 13:31:43 -070016142 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula19c80542015-12-16 12:48:16 +020016143 struct intel_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070016144
Chris Wilsondc979972016-05-10 14:10:04 +010016145 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020016146
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016147 intel_backlight_unregister(dev);
16148
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016149 /*
16150 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016151 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016152 * experience fancy races otherwise.
16153 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016154 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016155
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016156 /*
16157 * Due to the hpd irq storm handling the hotplug work can re-arm the
16158 * poll handlers. Hence disable polling after hpd handling is shut down.
16159 */
Keith Packardf87ea762010-10-03 19:36:26 -070016160 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016161
Jesse Barnes723bfd72010-10-07 16:01:13 -070016162 intel_unregister_dsm_handler();
16163
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016164 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016165
Chris Wilson1630fe72011-07-08 12:22:42 +010016166 /* flush any delayed tasks or pending work */
16167 flush_scheduled_work();
16168
Jani Nikuladb31af1d2013-11-08 16:48:53 +020016169 /* destroy the backlight and sysfs files before encoders/connectors */
Jani Nikula19c80542015-12-16 12:48:16 +020016170 for_each_intel_connector(dev, connector)
16171 connector->unregister(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -030016172
Jesse Barnes79e53942008-11-07 14:24:08 -080016173 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016174
Chris Wilson1ee8da62016-05-12 12:43:23 +010016175 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016176
Chris Wilsondc979972016-05-10 14:10:04 +010016177 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010016178
16179 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016180}
16181
Dave Airlie28d52042009-09-21 14:33:58 +100016182/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080016183 * Return which encoder is currently attached for connector.
16184 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010016185struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080016186{
Chris Wilsondf0e9242010-09-09 16:20:55 +010016187 return &intel_attached_encoder(connector)->base;
16188}
Jesse Barnes79e53942008-11-07 14:24:08 -080016189
Chris Wilsondf0e9242010-09-09 16:20:55 +010016190void intel_connector_attach_encoder(struct intel_connector *connector,
16191 struct intel_encoder *encoder)
16192{
16193 connector->encoder = encoder;
16194 drm_mode_connector_attach_encoder(&connector->base,
16195 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016196}
Dave Airlie28d52042009-09-21 14:33:58 +100016197
16198/*
16199 * set vga decode state - true == enable VGA decode
16200 */
16201int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16202{
16203 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016204 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016205 u16 gmch_ctrl;
16206
Chris Wilson75fa0412014-02-07 18:37:02 -020016207 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16208 DRM_ERROR("failed to read control word\n");
16209 return -EIO;
16210 }
16211
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016212 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16213 return 0;
16214
Dave Airlie28d52042009-09-21 14:33:58 +100016215 if (state)
16216 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16217 else
16218 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016219
16220 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16221 DRM_ERROR("failed to write control word\n");
16222 return -EIO;
16223 }
16224
Dave Airlie28d52042009-09-21 14:33:58 +100016225 return 0;
16226}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016227
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016228struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016229
16230 u32 power_well_driver;
16231
Chris Wilson63b66e52013-08-08 15:12:06 +020016232 int num_transcoders;
16233
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016234 struct intel_cursor_error_state {
16235 u32 control;
16236 u32 position;
16237 u32 base;
16238 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016239 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016240
16241 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016242 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016243 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030016244 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016245 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016246
16247 struct intel_plane_error_state {
16248 u32 control;
16249 u32 stride;
16250 u32 size;
16251 u32 pos;
16252 u32 addr;
16253 u32 surface;
16254 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016255 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016256
16257 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016258 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016259 enum transcoder cpu_transcoder;
16260
16261 u32 conf;
16262
16263 u32 htotal;
16264 u32 hblank;
16265 u32 hsync;
16266 u32 vtotal;
16267 u32 vblank;
16268 u32 vsync;
16269 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016270};
16271
16272struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010016273intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016274{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016275 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016276 int transcoders[] = {
16277 TRANSCODER_A,
16278 TRANSCODER_B,
16279 TRANSCODER_C,
16280 TRANSCODER_EDP,
16281 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016282 int i;
16283
Chris Wilsonc0336662016-05-06 15:40:21 +010016284 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020016285 return NULL;
16286
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016287 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016288 if (error == NULL)
16289 return NULL;
16290
Chris Wilsonc0336662016-05-06 15:40:21 +010016291 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016292 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16293
Damien Lespiau055e3932014-08-18 13:49:10 +010016294 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016295 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016296 __intel_display_power_is_enabled(dev_priv,
16297 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016298 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016299 continue;
16300
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016301 error->cursor[i].control = I915_READ(CURCNTR(i));
16302 error->cursor[i].position = I915_READ(CURPOS(i));
16303 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016304
16305 error->plane[i].control = I915_READ(DSPCNTR(i));
16306 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016307 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016308 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016309 error->plane[i].pos = I915_READ(DSPPOS(i));
16310 }
Chris Wilsonc0336662016-05-06 15:40:21 +010016311 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030016312 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016313 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016314 error->plane[i].surface = I915_READ(DSPSURF(i));
16315 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16316 }
16317
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016318 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030016319
Chris Wilsonc0336662016-05-06 15:40:21 +010016320 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e2014-04-18 15:55:04 +030016321 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016322 }
16323
Jani Nikula4d1de972016-03-18 17:05:42 +020016324 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010016325 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030016326 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020016327 error->num_transcoders++; /* Account for eDP. */
16328
16329 for (i = 0; i < error->num_transcoders; i++) {
16330 enum transcoder cpu_transcoder = transcoders[i];
16331
Imre Deakddf9c532013-11-27 22:02:02 +020016332 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016333 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016334 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016335 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016336 continue;
16337
Chris Wilson63b66e52013-08-08 15:12:06 +020016338 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16339
16340 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16341 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16342 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16343 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16344 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16345 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16346 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016347 }
16348
16349 return error;
16350}
16351
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016352#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16353
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016354void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016355intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016356 struct drm_device *dev,
16357 struct intel_display_error_state *error)
16358{
Damien Lespiau055e3932014-08-18 13:49:10 +010016359 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016360 int i;
16361
Chris Wilson63b66e52013-08-08 15:12:06 +020016362 if (!error)
16363 return;
16364
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016365 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016366 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016367 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016368 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016369 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016370 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016371 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016372 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016373 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016374 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016375
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016376 err_printf(m, "Plane [%d]:\n", i);
16377 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16378 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016379 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016380 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16381 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016382 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016383 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016384 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016385 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016386 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16387 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016388 }
16389
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016390 err_printf(m, "Cursor [%d]:\n", i);
16391 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16392 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16393 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016394 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016395
16396 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016397 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016398 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016399 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016400 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016401 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16402 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16403 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16404 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16405 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16406 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16407 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16408 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016409}