blob: 8704355e471a519ab6318e0180765edbbb7420ac [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"
Chris Wilsonc37efb92016-06-17 08:28:47 +010039#include "i915_gem_dmabuf.h"
Imre Deakdb18b6a2016-03-24 12:41:40 +020040#include "intel_dsi.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070041#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080042#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080043#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010044#include <drm/drm_dp_helper.h>
45#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070046#include <drm/drm_plane_helper.h>
47#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080048#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080049#include <linux/reservation.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080050
Daniel Vetter5a21b662016-05-24 17:13:53 +020051static bool is_mmio_work(struct intel_flip_work *work)
52{
53 return work->mmio_work.func;
54}
55
Matt Roper465c1202014-05-29 08:06:54 -070056/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010057static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010058 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070060 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010061 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070062};
63
64/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010065static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010066 DRM_FORMAT_C8,
67 DRM_FORMAT_RGB565,
68 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070069 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010070 DRM_FORMAT_XRGB2101010,
71 DRM_FORMAT_XBGR2101010,
72};
73
74static const uint32_t skl_primary_formats[] = {
75 DRM_FORMAT_C8,
76 DRM_FORMAT_RGB565,
77 DRM_FORMAT_XRGB8888,
78 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010079 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070080 DRM_FORMAT_ABGR8888,
81 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070082 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053083 DRM_FORMAT_YUYV,
84 DRM_FORMAT_YVYU,
85 DRM_FORMAT_UYVY,
86 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070087};
88
Matt Roper3d7d6512014-06-10 08:28:13 -070089/* Cursor formats */
90static const uint32_t intel_cursor_formats[] = {
91 DRM_FORMAT_ARGB8888,
92};
93
Jesse Barnesf1f644d2013-06-27 00:39:25 +030094static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020095 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030096static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020097 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030098
Jesse Barneseb1bfe82014-02-12 12:26:25 -080099static int intel_framebuffer_init(struct drm_device *dev,
100 struct intel_framebuffer *ifb,
101 struct drm_mode_fb_cmd2 *mode_cmd,
102 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +0200103static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
104static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +0200105static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200106static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700107 struct intel_link_m_n *m_n,
108 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200109static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200110static void haswell_set_pipeconf(struct drm_crtc *crtc);
Jani Nikula391bf042016-03-18 17:05:40 +0200111static void haswell_set_pipemisc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200112static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200113 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200114static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200115 const struct intel_crtc_state *pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +0200116static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
117static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700118static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
119 struct intel_crtc_state *crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200120static void skylake_pfit_enable(struct intel_crtc *crtc);
121static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
122static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200123static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200124static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Ville Syrjälä4e5ca602016-05-11 22:44:44 +0300125static int ilk_max_pixel_rate(struct drm_atomic_state *state);
Imre Deak324513c2016-06-13 16:44:36 +0300126static int bxt_calc_cdclk(int max_pixclk);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100127
Ma Lingd4906092009-03-18 20:13:27 +0800128struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300129 struct {
130 int min, max;
131 } dot, vco, n, m, m1, m2, p, p1;
132
133 struct {
134 int dot_limit;
135 int p2_slow, p2_fast;
136 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800137};
Jesse Barnes79e53942008-11-07 14:24:08 -0800138
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300139/* returns HPLL frequency in kHz */
140static int valleyview_get_vco(struct drm_i915_private *dev_priv)
141{
142 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
143
144 /* Obtain SKU information */
145 mutex_lock(&dev_priv->sb_lock);
146 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
147 CCK_FUSE_HPLL_FREQ_MASK;
148 mutex_unlock(&dev_priv->sb_lock);
149
150 return vco_freq[hpll_freq] * 1000;
151}
152
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200153int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
154 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300155{
156 u32 val;
157 int divider;
158
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300159 mutex_lock(&dev_priv->sb_lock);
160 val = vlv_cck_read(dev_priv, reg);
161 mutex_unlock(&dev_priv->sb_lock);
162
163 divider = val & CCK_FREQUENCY_VALUES;
164
165 WARN((val & CCK_FREQUENCY_STATUS) !=
166 (divider << CCK_FREQUENCY_STATUS_SHIFT),
167 "%s change in progress\n", name);
168
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200169 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
170}
171
172static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
173 const char *name, u32 reg)
174{
175 if (dev_priv->hpll_freq == 0)
176 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
177
178 return vlv_get_cck_clock(dev_priv, name, reg,
179 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300180}
181
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200182static int
183intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200184{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200185 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200186}
187
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200188static int
189intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300190{
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300191 /* RAWCLK_FREQ_VLV register updated from power well code */
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200192 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
193 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200194}
195
196static int
197intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
198{
Jani Nikula79e50a42015-08-26 10:58:20 +0300199 uint32_t clkcfg;
200
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200201 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300202 clkcfg = I915_READ(CLKCFG);
203 switch (clkcfg & CLKCFG_FSB_MASK) {
204 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200205 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300206 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200207 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300208 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200209 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300210 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200211 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300212 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200213 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300214 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200215 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300216 /* these two are just a guess; one of them might be right */
217 case CLKCFG_FSB_1600:
218 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200219 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300220 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200221 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300222 }
223}
224
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300225void intel_update_rawclk(struct drm_i915_private *dev_priv)
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200226{
227 if (HAS_PCH_SPLIT(dev_priv))
228 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
229 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
230 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
231 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
232 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
233 else
234 return; /* no rawclk on other platforms, or no need to know it */
235
236 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
237}
238
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300239static void intel_update_czclk(struct drm_i915_private *dev_priv)
240{
Wayne Boyer666a4532015-12-09 12:29:35 -0800241 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300242 return;
243
244 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
245 CCK_CZ_CLOCK_CONTROL);
246
247 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
248}
249
Chris Wilson021357a2010-09-07 20:54:59 +0100250static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200251intel_fdi_link_freq(struct drm_i915_private *dev_priv,
252 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100253{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200254 if (HAS_DDI(dev_priv))
255 return pipe_config->port_clock; /* SPLL */
256 else if (IS_GEN5(dev_priv))
257 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200258 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200259 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100260}
261
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300262static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400263 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200264 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200265 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400266 .m = { .min = 96, .max = 140 },
267 .m1 = { .min = 18, .max = 26 },
268 .m2 = { .min = 6, .max = 16 },
269 .p = { .min = 4, .max = 128 },
270 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700271 .p2 = { .dot_limit = 165000,
272 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700273};
274
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300275static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200276 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200277 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200278 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200279 .m = { .min = 96, .max = 140 },
280 .m1 = { .min = 18, .max = 26 },
281 .m2 = { .min = 6, .max = 16 },
282 .p = { .min = 4, .max = 128 },
283 .p1 = { .min = 2, .max = 33 },
284 .p2 = { .dot_limit = 165000,
285 .p2_slow = 4, .p2_fast = 4 },
286};
287
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300288static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400289 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200290 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200291 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400292 .m = { .min = 96, .max = 140 },
293 .m1 = { .min = 18, .max = 26 },
294 .m2 = { .min = 6, .max = 16 },
295 .p = { .min = 4, .max = 128 },
296 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700297 .p2 = { .dot_limit = 165000,
298 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700299};
Eric Anholt273e27c2011-03-30 13:01:10 -0700300
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300301static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400302 .dot = { .min = 20000, .max = 400000 },
303 .vco = { .min = 1400000, .max = 2800000 },
304 .n = { .min = 1, .max = 6 },
305 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100306 .m1 = { .min = 8, .max = 18 },
307 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400308 .p = { .min = 5, .max = 80 },
309 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700310 .p2 = { .dot_limit = 200000,
311 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700312};
313
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300314static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400315 .dot = { .min = 20000, .max = 400000 },
316 .vco = { .min = 1400000, .max = 2800000 },
317 .n = { .min = 1, .max = 6 },
318 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100319 .m1 = { .min = 8, .max = 18 },
320 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400321 .p = { .min = 7, .max = 98 },
322 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700323 .p2 = { .dot_limit = 112000,
324 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700325};
326
Eric Anholt273e27c2011-03-30 13:01:10 -0700327
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300328static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700329 .dot = { .min = 25000, .max = 270000 },
330 .vco = { .min = 1750000, .max = 3500000},
331 .n = { .min = 1, .max = 4 },
332 .m = { .min = 104, .max = 138 },
333 .m1 = { .min = 17, .max = 23 },
334 .m2 = { .min = 5, .max = 11 },
335 .p = { .min = 10, .max = 30 },
336 .p1 = { .min = 1, .max = 3},
337 .p2 = { .dot_limit = 270000,
338 .p2_slow = 10,
339 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800340 },
Keith Packarde4b36692009-06-05 19:22:17 -0700341};
342
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300343static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700344 .dot = { .min = 22000, .max = 400000 },
345 .vco = { .min = 1750000, .max = 3500000},
346 .n = { .min = 1, .max = 4 },
347 .m = { .min = 104, .max = 138 },
348 .m1 = { .min = 16, .max = 23 },
349 .m2 = { .min = 5, .max = 11 },
350 .p = { .min = 5, .max = 80 },
351 .p1 = { .min = 1, .max = 8},
352 .p2 = { .dot_limit = 165000,
353 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700354};
355
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300356static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700357 .dot = { .min = 20000, .max = 115000 },
358 .vco = { .min = 1750000, .max = 3500000 },
359 .n = { .min = 1, .max = 3 },
360 .m = { .min = 104, .max = 138 },
361 .m1 = { .min = 17, .max = 23 },
362 .m2 = { .min = 5, .max = 11 },
363 .p = { .min = 28, .max = 112 },
364 .p1 = { .min = 2, .max = 8 },
365 .p2 = { .dot_limit = 0,
366 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800367 },
Keith Packarde4b36692009-06-05 19:22:17 -0700368};
369
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300370static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700371 .dot = { .min = 80000, .max = 224000 },
372 .vco = { .min = 1750000, .max = 3500000 },
373 .n = { .min = 1, .max = 3 },
374 .m = { .min = 104, .max = 138 },
375 .m1 = { .min = 17, .max = 23 },
376 .m2 = { .min = 5, .max = 11 },
377 .p = { .min = 14, .max = 42 },
378 .p1 = { .min = 2, .max = 6 },
379 .p2 = { .dot_limit = 0,
380 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800381 },
Keith Packarde4b36692009-06-05 19:22:17 -0700382};
383
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300384static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400385 .dot = { .min = 20000, .max = 400000},
386 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700387 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400388 .n = { .min = 3, .max = 6 },
389 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700390 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400391 .m1 = { .min = 0, .max = 0 },
392 .m2 = { .min = 0, .max = 254 },
393 .p = { .min = 5, .max = 80 },
394 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700395 .p2 = { .dot_limit = 200000,
396 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700397};
398
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300399static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400400 .dot = { .min = 20000, .max = 400000 },
401 .vco = { .min = 1700000, .max = 3500000 },
402 .n = { .min = 3, .max = 6 },
403 .m = { .min = 2, .max = 256 },
404 .m1 = { .min = 0, .max = 0 },
405 .m2 = { .min = 0, .max = 254 },
406 .p = { .min = 7, .max = 112 },
407 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700408 .p2 = { .dot_limit = 112000,
409 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700410};
411
Eric Anholt273e27c2011-03-30 13:01:10 -0700412/* Ironlake / Sandybridge
413 *
414 * We calculate clock using (register_value + 2) for N/M1/M2, so here
415 * the range value for them is (actual_value - 2).
416 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300417static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700418 .dot = { .min = 25000, .max = 350000 },
419 .vco = { .min = 1760000, .max = 3510000 },
420 .n = { .min = 1, .max = 5 },
421 .m = { .min = 79, .max = 127 },
422 .m1 = { .min = 12, .max = 22 },
423 .m2 = { .min = 5, .max = 9 },
424 .p = { .min = 5, .max = 80 },
425 .p1 = { .min = 1, .max = 8 },
426 .p2 = { .dot_limit = 225000,
427 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700428};
429
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300430static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700431 .dot = { .min = 25000, .max = 350000 },
432 .vco = { .min = 1760000, .max = 3510000 },
433 .n = { .min = 1, .max = 3 },
434 .m = { .min = 79, .max = 118 },
435 .m1 = { .min = 12, .max = 22 },
436 .m2 = { .min = 5, .max = 9 },
437 .p = { .min = 28, .max = 112 },
438 .p1 = { .min = 2, .max = 8 },
439 .p2 = { .dot_limit = 225000,
440 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800441};
442
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300443static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700444 .dot = { .min = 25000, .max = 350000 },
445 .vco = { .min = 1760000, .max = 3510000 },
446 .n = { .min = 1, .max = 3 },
447 .m = { .min = 79, .max = 127 },
448 .m1 = { .min = 12, .max = 22 },
449 .m2 = { .min = 5, .max = 9 },
450 .p = { .min = 14, .max = 56 },
451 .p1 = { .min = 2, .max = 8 },
452 .p2 = { .dot_limit = 225000,
453 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800454};
455
Eric Anholt273e27c2011-03-30 13:01:10 -0700456/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300457static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700458 .dot = { .min = 25000, .max = 350000 },
459 .vco = { .min = 1760000, .max = 3510000 },
460 .n = { .min = 1, .max = 2 },
461 .m = { .min = 79, .max = 126 },
462 .m1 = { .min = 12, .max = 22 },
463 .m2 = { .min = 5, .max = 9 },
464 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400465 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700466 .p2 = { .dot_limit = 225000,
467 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800468};
469
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300470static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700471 .dot = { .min = 25000, .max = 350000 },
472 .vco = { .min = 1760000, .max = 3510000 },
473 .n = { .min = 1, .max = 3 },
474 .m = { .min = 79, .max = 126 },
475 .m1 = { .min = 12, .max = 22 },
476 .m2 = { .min = 5, .max = 9 },
477 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400478 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700479 .p2 = { .dot_limit = 225000,
480 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800481};
482
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300483static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300484 /*
485 * These are the data rate limits (measured in fast clocks)
486 * since those are the strictest limits we have. The fast
487 * clock and actual rate limits are more relaxed, so checking
488 * them would make no difference.
489 */
490 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200491 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700492 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700493 .m1 = { .min = 2, .max = 3 },
494 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300495 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300496 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700497};
498
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300499static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300500 /*
501 * These are the data rate limits (measured in fast clocks)
502 * since those are the strictest limits we have. The fast
503 * clock and actual rate limits are more relaxed, so checking
504 * them would make no difference.
505 */
506 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200507 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300508 .n = { .min = 1, .max = 1 },
509 .m1 = { .min = 2, .max = 2 },
510 .m2 = { .min = 24 << 22, .max = 175 << 22 },
511 .p1 = { .min = 2, .max = 4 },
512 .p2 = { .p2_slow = 1, .p2_fast = 14 },
513};
514
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300515static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200516 /* FIXME: find real dot limits */
517 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530518 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200519 .n = { .min = 1, .max = 1 },
520 .m1 = { .min = 2, .max = 2 },
521 /* FIXME: find real m2 limits */
522 .m2 = { .min = 2 << 22, .max = 255 << 22 },
523 .p1 = { .min = 2, .max = 4 },
524 .p2 = { .p2_slow = 1, .p2_fast = 20 },
525};
526
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200527static bool
528needs_modeset(struct drm_crtc_state *state)
529{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200530 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200531}
532
Imre Deakdccbea32015-06-22 23:35:51 +0300533/*
534 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
535 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
536 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
537 * The helpers' return value is the rate of the clock that is fed to the
538 * display engine's pipe which can be the above fast dot clock rate or a
539 * divided-down version of it.
540 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500541/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300542static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800543{
Shaohua Li21778322009-02-23 15:19:16 +0800544 clock->m = clock->m2 + 2;
545 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200546 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300547 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300548 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
549 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300550
551 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800552}
553
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200554static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
555{
556 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
557}
558
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300559static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800560{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200561 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800562 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200563 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300564 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300565 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
566 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300567
568 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800569}
570
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300571static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300572{
573 clock->m = clock->m1 * clock->m2;
574 clock->p = clock->p1 * clock->p2;
575 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300576 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300577 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
578 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300579
580 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300581}
582
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300583int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300584{
585 clock->m = clock->m1 * clock->m2;
586 clock->p = clock->p1 * clock->p2;
587 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300588 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300589 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
590 clock->n << 22);
591 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300592
593 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300594}
595
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800596#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800597/**
598 * Returns whether the given set of divisors are valid for a given refclk with
599 * the given connectors.
600 */
601
Chris Wilson1b894b52010-12-14 20:04:54 +0000602static bool intel_PLL_is_valid(struct drm_device *dev,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300603 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300604 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800605{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300606 if (clock->n < limit->n.min || limit->n.max < clock->n)
607 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800608 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400609 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800610 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400611 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400613 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300614
Wayne Boyer666a4532015-12-09 12:29:35 -0800615 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
616 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300617 if (clock->m1 <= clock->m2)
618 INTELPllInvalid("m1 <= m2\n");
619
Wayne Boyer666a4532015-12-09 12:29:35 -0800620 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300621 if (clock->p < limit->p.min || limit->p.max < clock->p)
622 INTELPllInvalid("p out of range\n");
623 if (clock->m < limit->m.min || limit->m.max < clock->m)
624 INTELPllInvalid("m out of range\n");
625 }
626
Jesse Barnes79e53942008-11-07 14:24:08 -0800627 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400628 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800629 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
630 * connector, etc., rather than just a single range.
631 */
632 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400633 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800634
635 return true;
636}
637
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300638static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300639i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300640 const struct intel_crtc_state *crtc_state,
641 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800642{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300643 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800644
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +0300645 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800646 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100647 * For LVDS just rely on its current settings for dual-channel.
648 * We haven't figured out how to reliably set up different
649 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800650 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100651 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300652 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800653 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300654 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800655 } else {
656 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300657 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800658 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300659 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800660 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300661}
662
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200663/*
664 * Returns a set of divisors for the desired target clock with the given
665 * refclk, or FALSE. The returned values represent the clock equation:
666 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
667 *
668 * Target and reference clocks are specified in kHz.
669 *
670 * If match_clock is provided, then best_clock P divider must match the P
671 * divider from @match_clock used for LVDS downclocking.
672 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300673static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300674i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300675 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300676 int target, int refclk, struct dpll *match_clock,
677 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300678{
679 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300680 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300681 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800682
Akshay Joshi0206e352011-08-16 15:34:10 -0400683 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800684
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300685 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
686
Zhao Yakui42158662009-11-20 11:24:18 +0800687 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
688 clock.m1++) {
689 for (clock.m2 = limit->m2.min;
690 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200691 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800692 break;
693 for (clock.n = limit->n.min;
694 clock.n <= limit->n.max; clock.n++) {
695 for (clock.p1 = limit->p1.min;
696 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800697 int this_err;
698
Imre Deakdccbea32015-06-22 23:35:51 +0300699 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000700 if (!intel_PLL_is_valid(dev, limit,
701 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800702 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800703 if (match_clock &&
704 clock.p != match_clock->p)
705 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800706
707 this_err = abs(clock.dot - target);
708 if (this_err < err) {
709 *best_clock = clock;
710 err = this_err;
711 }
712 }
713 }
714 }
715 }
716
717 return (err != target);
718}
719
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200720/*
721 * Returns a set of divisors for the desired target clock with the given
722 * refclk, or FALSE. The returned values represent the clock equation:
723 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
724 *
725 * Target and reference clocks are specified in kHz.
726 *
727 * If match_clock is provided, then best_clock P divider must match the P
728 * divider from @match_clock used for LVDS downclocking.
729 */
Ma Lingd4906092009-03-18 20:13:27 +0800730static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300731pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200732 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300733 int target, int refclk, struct dpll *match_clock,
734 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200735{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300736 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300737 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200738 int err = target;
739
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200740 memset(best_clock, 0, sizeof(*best_clock));
741
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300742 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
743
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200744 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
745 clock.m1++) {
746 for (clock.m2 = limit->m2.min;
747 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200748 for (clock.n = limit->n.min;
749 clock.n <= limit->n.max; clock.n++) {
750 for (clock.p1 = limit->p1.min;
751 clock.p1 <= limit->p1.max; clock.p1++) {
752 int this_err;
753
Imre Deakdccbea32015-06-22 23:35:51 +0300754 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800755 if (!intel_PLL_is_valid(dev, limit,
756 &clock))
757 continue;
758 if (match_clock &&
759 clock.p != match_clock->p)
760 continue;
761
762 this_err = abs(clock.dot - target);
763 if (this_err < err) {
764 *best_clock = clock;
765 err = this_err;
766 }
767 }
768 }
769 }
770 }
771
772 return (err != target);
773}
774
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200775/*
776 * Returns a set of divisors for the desired target clock with the given
777 * refclk, or FALSE. The returned values represent the clock equation:
778 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200779 *
780 * Target and reference clocks are specified in kHz.
781 *
782 * If match_clock is provided, then best_clock P divider must match the P
783 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200784 */
Ma Lingd4906092009-03-18 20:13:27 +0800785static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300786g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200787 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300788 int target, int refclk, struct dpll *match_clock,
789 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800790{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300791 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300792 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800793 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300794 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400795 /* approximately equals target * 0.00585 */
796 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800797
798 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300799
800 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
801
Ma Lingd4906092009-03-18 20:13:27 +0800802 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200803 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800804 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200805 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800806 for (clock.m1 = limit->m1.max;
807 clock.m1 >= limit->m1.min; clock.m1--) {
808 for (clock.m2 = limit->m2.max;
809 clock.m2 >= limit->m2.min; clock.m2--) {
810 for (clock.p1 = limit->p1.max;
811 clock.p1 >= limit->p1.min; clock.p1--) {
812 int this_err;
813
Imre Deakdccbea32015-06-22 23:35:51 +0300814 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000815 if (!intel_PLL_is_valid(dev, limit,
816 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800817 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000818
819 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800820 if (this_err < err_most) {
821 *best_clock = clock;
822 err_most = this_err;
823 max_n = clock.n;
824 found = true;
825 }
826 }
827 }
828 }
829 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800830 return found;
831}
Ma Lingd4906092009-03-18 20:13:27 +0800832
Imre Deakd5dd62b2015-03-17 11:40:03 +0200833/*
834 * Check if the calculated PLL configuration is more optimal compared to the
835 * best configuration and error found so far. Return the calculated error.
836 */
837static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300838 const struct dpll *calculated_clock,
839 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200840 unsigned int best_error_ppm,
841 unsigned int *error_ppm)
842{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200843 /*
844 * For CHV ignore the error and consider only the P value.
845 * Prefer a bigger P value based on HW requirements.
846 */
847 if (IS_CHERRYVIEW(dev)) {
848 *error_ppm = 0;
849
850 return calculated_clock->p > best_clock->p;
851 }
852
Imre Deak24be4e42015-03-17 11:40:04 +0200853 if (WARN_ON_ONCE(!target_freq))
854 return false;
855
Imre Deakd5dd62b2015-03-17 11:40:03 +0200856 *error_ppm = div_u64(1000000ULL *
857 abs(target_freq - calculated_clock->dot),
858 target_freq);
859 /*
860 * Prefer a better P value over a better (smaller) error if the error
861 * is small. Ensure this preference for future configurations too by
862 * setting the error to 0.
863 */
864 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
865 *error_ppm = 0;
866
867 return true;
868 }
869
870 return *error_ppm + 10 < best_error_ppm;
871}
872
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200873/*
874 * Returns a set of divisors for the desired target clock with the given
875 * refclk, or FALSE. The returned values represent the clock equation:
876 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
877 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800878static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300879vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200880 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300881 int target, int refclk, struct dpll *match_clock,
882 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700883{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200884 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300885 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300886 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300887 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300888 /* min update 19.2 MHz */
889 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300890 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700891
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300892 target *= 5; /* fast clock */
893
894 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700895
896 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300897 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300898 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300899 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300900 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300901 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700902 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300903 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200904 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300905
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300906 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
907 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300908
Imre Deakdccbea32015-06-22 23:35:51 +0300909 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300910
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300911 if (!intel_PLL_is_valid(dev, limit,
912 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300913 continue;
914
Imre Deakd5dd62b2015-03-17 11:40:03 +0200915 if (!vlv_PLL_is_optimal(dev, target,
916 &clock,
917 best_clock,
918 bestppm, &ppm))
919 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300920
Imre Deakd5dd62b2015-03-17 11:40:03 +0200921 *best_clock = clock;
922 bestppm = ppm;
923 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700924 }
925 }
926 }
927 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700928
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300929 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700930}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700931
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200932/*
933 * Returns a set of divisors for the desired target clock with the given
934 * refclk, or FALSE. The returned values represent the clock equation:
935 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
936 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300937static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300938chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200939 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300940 int target, int refclk, struct dpll *match_clock,
941 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300942{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200943 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300944 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200945 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300946 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300947 uint64_t m2;
948 int found = false;
949
950 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200951 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300952
953 /*
954 * Based on hardware doc, the n always set to 1, and m1 always
955 * set to 2. If requires to support 200Mhz refclk, we need to
956 * revisit this because n may not 1 anymore.
957 */
958 clock.n = 1, clock.m1 = 2;
959 target *= 5; /* fast clock */
960
961 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
962 for (clock.p2 = limit->p2.p2_fast;
963 clock.p2 >= limit->p2.p2_slow;
964 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200965 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300966
967 clock.p = clock.p1 * clock.p2;
968
969 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
970 clock.n) << 22, refclk * clock.m1);
971
972 if (m2 > INT_MAX/clock.m1)
973 continue;
974
975 clock.m2 = m2;
976
Imre Deakdccbea32015-06-22 23:35:51 +0300977 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300978
979 if (!intel_PLL_is_valid(dev, limit, &clock))
980 continue;
981
Imre Deak9ca3ba02015-03-17 11:40:05 +0200982 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
983 best_error_ppm, &error_ppm))
984 continue;
985
986 *best_clock = clock;
987 best_error_ppm = error_ppm;
988 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300989 }
990 }
991
992 return found;
993}
994
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200995bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300996 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200997{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200998 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300999 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001000
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001001 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001002 target_clock, refclk, NULL, best_clock);
1003}
1004
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001005bool intel_crtc_active(struct drm_crtc *crtc)
1006{
1007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1008
1009 /* Be paranoid as we can arrive here with only partial
1010 * state retrieved from the hardware during setup.
1011 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001012 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001013 * as Haswell has gained clock readout/fastboot support.
1014 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001015 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001016 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001017 *
1018 * FIXME: The intel_crtc->active here should be switched to
1019 * crtc->state->active once we have proper CRTC states wired up
1020 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001021 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001022 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001023 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001024}
1025
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001026enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1027 enum pipe pipe)
1028{
1029 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1031
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001032 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001033}
1034
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001035static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1036{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001037 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001038 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001039 u32 line1, line2;
1040 u32 line_mask;
1041
1042 if (IS_GEN2(dev))
1043 line_mask = DSL_LINEMASK_GEN2;
1044 else
1045 line_mask = DSL_LINEMASK_GEN3;
1046
1047 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001048 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001049 line2 = I915_READ(reg) & line_mask;
1050
1051 return line1 == line2;
1052}
1053
Keith Packardab7ad7f2010-10-03 00:33:06 -07001054/*
1055 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001056 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001057 *
1058 * After disabling a pipe, we can't wait for vblank in the usual way,
1059 * spinning on the vblank interrupt status bit, since we won't actually
1060 * see an interrupt when the pipe is disabled.
1061 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001062 * On Gen4 and above:
1063 * wait for the pipe register state bit to turn off
1064 *
1065 * Otherwise:
1066 * wait for the display line value to settle (it usually
1067 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001068 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001069 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001070static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001071{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001072 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001073 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001074 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001075 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001076
Keith Packardab7ad7f2010-10-03 00:33:06 -07001077 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001078 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001079
Keith Packardab7ad7f2010-10-03 00:33:06 -07001080 /* Wait for the Pipe State to go off */
Chris Wilsonb8511f52016-06-30 15:32:53 +01001081 if (intel_wait_for_register(dev_priv,
1082 reg, I965_PIPECONF_ACTIVE, 0,
1083 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001084 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001085 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001086 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001087 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001088 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001089 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001090}
1091
Jesse Barnesb24e7172011-01-04 15:09:30 -08001092/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001093void assert_pll(struct drm_i915_private *dev_priv,
1094 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001095{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001096 u32 val;
1097 bool cur_state;
1098
Ville Syrjälä649636e2015-09-22 19:50:01 +03001099 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001100 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001101 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001102 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001103 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001104}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001105
Jani Nikula23538ef2013-08-27 15:12:22 +03001106/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001107void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001108{
1109 u32 val;
1110 bool cur_state;
1111
Ville Syrjäläa5805162015-05-26 20:42:30 +03001112 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001113 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001114 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001115
1116 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001117 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001118 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001119 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001120}
Jani Nikula23538ef2013-08-27 15:12:22 +03001121
Jesse Barnes040484a2011-01-03 12:14:26 -08001122static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1123 enum pipe pipe, bool state)
1124{
Jesse Barnes040484a2011-01-03 12:14:26 -08001125 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001126 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1127 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001128
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001129 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001130 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001131 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001132 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001133 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001134 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001135 cur_state = !!(val & FDI_TX_ENABLE);
1136 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001137 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001138 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001139 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001140}
1141#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1142#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1143
1144static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1145 enum pipe pipe, bool state)
1146{
Jesse Barnes040484a2011-01-03 12:14:26 -08001147 u32 val;
1148 bool cur_state;
1149
Ville Syrjälä649636e2015-09-22 19:50:01 +03001150 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001151 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001152 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001153 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001154 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001155}
1156#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1157#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1158
1159static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1160 enum pipe pipe)
1161{
Jesse Barnes040484a2011-01-03 12:14:26 -08001162 u32 val;
1163
1164 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001165 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001166 return;
1167
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001168 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001169 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001170 return;
1171
Ville Syrjälä649636e2015-09-22 19:50:01 +03001172 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001173 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 -08001174}
1175
Daniel Vetter55607e82013-06-16 21:42:39 +02001176void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1177 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001178{
Jesse Barnes040484a2011-01-03 12:14:26 -08001179 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001180 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001181
Ville Syrjälä649636e2015-09-22 19:50:01 +03001182 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001183 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001184 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001185 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001186 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001187}
1188
Daniel Vetterb680c372014-09-19 18:27:27 +02001189void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1190 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001191{
Chris Wilson91c8a322016-07-05 10:40:23 +01001192 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001193 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001194 u32 val;
1195 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001196 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001197
Jani Nikulabedd4db2014-08-22 15:04:13 +03001198 if (WARN_ON(HAS_DDI(dev)))
1199 return;
1200
1201 if (HAS_PCH_SPLIT(dev)) {
1202 u32 port_sel;
1203
Jesse Barnesea0760c2011-01-04 15:09:32 -08001204 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001205 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1206
1207 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1208 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1209 panel_pipe = PIPE_B;
1210 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001211 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001212 /* presumably write lock depends on pipe, not port select */
1213 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1214 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001215 } else {
1216 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001217 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1218 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001219 }
1220
1221 val = I915_READ(pp_reg);
1222 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001223 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001224 locked = false;
1225
Rob Clarke2c719b2014-12-15 13:56:32 -05001226 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001227 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001228 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001229}
1230
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001231static void assert_cursor(struct drm_i915_private *dev_priv,
1232 enum pipe pipe, bool state)
1233{
Chris Wilson91c8a322016-07-05 10:40:23 +01001234 struct drm_device *dev = &dev_priv->drm;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001235 bool cur_state;
1236
Paulo Zanonid9d82082014-02-27 16:30:56 -03001237 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001238 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001239 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001240 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001241
Rob Clarke2c719b2014-12-15 13:56:32 -05001242 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001243 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001244 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001245}
1246#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1247#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1248
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001249void assert_pipe(struct drm_i915_private *dev_priv,
1250 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001251{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001252 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001253 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1254 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001255 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001256
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001257 /* if we need the pipe quirk it must be always on */
1258 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1259 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001260 state = true;
1261
Imre Deak4feed0e2016-02-12 18:55:14 +02001262 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1263 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001264 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001265 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001266
1267 intel_display_power_put(dev_priv, power_domain);
1268 } else {
1269 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001270 }
1271
Rob Clarke2c719b2014-12-15 13:56:32 -05001272 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001273 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001274 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001275}
1276
Chris Wilson931872f2012-01-16 23:01:13 +00001277static void assert_plane(struct drm_i915_private *dev_priv,
1278 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001279{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001280 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001281 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001282
Ville Syrjälä649636e2015-09-22 19:50:01 +03001283 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001284 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001285 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001286 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001287 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001288}
1289
Chris Wilson931872f2012-01-16 23:01:13 +00001290#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1291#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1292
Jesse Barnesb24e7172011-01-04 15:09:30 -08001293static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1294 enum pipe pipe)
1295{
Chris Wilson91c8a322016-07-05 10:40:23 +01001296 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001297 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001298
Ville Syrjälä653e1022013-06-04 13:49:05 +03001299 /* Primary planes are fixed to pipes on gen4+ */
1300 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001301 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001302 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001303 "plane %c assertion failure, should be disabled but not\n",
1304 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001305 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001306 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001307
Jesse Barnesb24e7172011-01-04 15:09:30 -08001308 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001309 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001310 u32 val = I915_READ(DSPCNTR(i));
1311 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001312 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001313 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001314 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1315 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001316 }
1317}
1318
Jesse Barnes19332d72013-03-28 09:55:38 -07001319static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1320 enum pipe pipe)
1321{
Chris Wilson91c8a322016-07-05 10:40:23 +01001322 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001323 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001324
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001325 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001326 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001327 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001328 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001329 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1330 sprite, pipe_name(pipe));
1331 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001332 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001333 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001334 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001335 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001336 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001337 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001338 }
1339 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001340 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001341 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001342 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001343 plane_name(pipe), pipe_name(pipe));
1344 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001345 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001346 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001347 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1348 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001349 }
1350}
1351
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001352static void assert_vblank_disabled(struct drm_crtc *crtc)
1353{
Rob Clarke2c719b2014-12-15 13:56:32 -05001354 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001355 drm_crtc_vblank_put(crtc);
1356}
1357
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001358void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1359 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001360{
Jesse Barnes92f25842011-01-04 15:09:34 -08001361 u32 val;
1362 bool enabled;
1363
Ville Syrjälä649636e2015-09-22 19:50:01 +03001364 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001365 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001366 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001367 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1368 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001369}
1370
Keith Packard4e634382011-08-06 10:39:45 -07001371static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1372 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001373{
1374 if ((val & DP_PORT_EN) == 0)
1375 return false;
1376
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001377 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001378 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001379 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1380 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001381 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001382 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1383 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001384 } else {
1385 if ((val & DP_PIPE_MASK) != (pipe << 30))
1386 return false;
1387 }
1388 return true;
1389}
1390
Keith Packard1519b992011-08-06 10:35:34 -07001391static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1392 enum pipe pipe, u32 val)
1393{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001394 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001395 return false;
1396
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001397 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001398 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001399 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001400 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001401 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1402 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001403 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001404 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001405 return false;
1406 }
1407 return true;
1408}
1409
1410static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1411 enum pipe pipe, u32 val)
1412{
1413 if ((val & LVDS_PORT_EN) == 0)
1414 return false;
1415
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001416 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001417 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1418 return false;
1419 } else {
1420 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1421 return false;
1422 }
1423 return true;
1424}
1425
1426static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1427 enum pipe pipe, u32 val)
1428{
1429 if ((val & ADPA_DAC_ENABLE) == 0)
1430 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001431 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001432 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1433 return false;
1434 } else {
1435 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1436 return false;
1437 }
1438 return true;
1439}
1440
Jesse Barnes291906f2011-02-02 12:28:03 -08001441static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001442 enum pipe pipe, i915_reg_t reg,
1443 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001444{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001445 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001446 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001447 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001448 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001449
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001450 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001451 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001452 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001453}
1454
1455static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001456 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001457{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001458 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001459 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001460 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001461 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001462
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001463 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001464 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001465 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001466}
1467
1468static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1469 enum pipe pipe)
1470{
Jesse Barnes291906f2011-02-02 12:28:03 -08001471 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001472
Keith Packardf0575e92011-07-25 22:12:43 -07001473 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1474 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1475 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001476
Ville Syrjälä649636e2015-09-22 19:50:01 +03001477 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001478 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001479 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001480 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001481
Ville Syrjälä649636e2015-09-22 19:50:01 +03001482 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001483 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001484 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001485 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001486
Paulo Zanonie2debe92013-02-18 19:00:27 -03001487 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1488 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1489 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001490}
1491
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001492static void _vlv_enable_pll(struct intel_crtc *crtc,
1493 const struct intel_crtc_state *pipe_config)
1494{
1495 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1496 enum pipe pipe = crtc->pipe;
1497
1498 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1499 POSTING_READ(DPLL(pipe));
1500 udelay(150);
1501
Chris Wilson2c30b432016-06-30 15:32:54 +01001502 if (intel_wait_for_register(dev_priv,
1503 DPLL(pipe),
1504 DPLL_LOCK_VLV,
1505 DPLL_LOCK_VLV,
1506 1))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001507 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1508}
1509
Ville Syrjäläd288f652014-10-28 13:20:22 +02001510static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001511 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001512{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001513 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001514 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001515
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001516 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001517
Daniel Vetter87442f72013-06-06 00:52:17 +02001518 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001519 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001520
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001521 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1522 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001523
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001524 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1525 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001526}
1527
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001528
1529static void _chv_enable_pll(struct intel_crtc *crtc,
1530 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001531{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001532 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001533 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001534 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001535 u32 tmp;
1536
Ville Syrjäläa5805162015-05-26 20:42:30 +03001537 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001538
1539 /* Enable back the 10bit clock to display controller */
1540 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1541 tmp |= DPIO_DCLKP_EN;
1542 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1543
Ville Syrjälä54433e92015-05-26 20:42:31 +03001544 mutex_unlock(&dev_priv->sb_lock);
1545
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001546 /*
1547 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1548 */
1549 udelay(1);
1550
1551 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001552 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001553
1554 /* Check PLL is locked */
Chris Wilson6b188262016-06-30 15:32:55 +01001555 if (intel_wait_for_register(dev_priv,
1556 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1557 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001558 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001559}
1560
1561static void chv_enable_pll(struct intel_crtc *crtc,
1562 const struct intel_crtc_state *pipe_config)
1563{
1564 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1565 enum pipe pipe = crtc->pipe;
1566
1567 assert_pipe_disabled(dev_priv, pipe);
1568
1569 /* PLL is protected by panel, make sure we can write it */
1570 assert_panel_unlocked(dev_priv, pipe);
1571
1572 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1573 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001574
Ville Syrjäläc2317752016-03-15 16:39:56 +02001575 if (pipe != PIPE_A) {
1576 /*
1577 * WaPixelRepeatModeFixForC0:chv
1578 *
1579 * DPLLCMD is AWOL. Use chicken bits to propagate
1580 * the value from DPLLBMD to either pipe B or C.
1581 */
1582 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1583 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1584 I915_WRITE(CBR4_VLV, 0);
1585 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1586
1587 /*
1588 * DPLLB VGA mode also seems to cause problems.
1589 * We should always have it disabled.
1590 */
1591 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1592 } else {
1593 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1594 POSTING_READ(DPLL_MD(pipe));
1595 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001596}
1597
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001598static int intel_num_dvo_pipes(struct drm_device *dev)
1599{
1600 struct intel_crtc *crtc;
1601 int count = 0;
1602
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001603 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001604 count += crtc->base.state->active &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001605 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1606 }
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001607
1608 return count;
1609}
1610
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001611static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001612{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001613 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001614 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001615 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001616 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001617
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001618 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001619
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001620 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001621 if (IS_MOBILE(dev) && !IS_I830(dev))
1622 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001623
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001624 /* Enable DVO 2x clock on both PLLs if necessary */
1625 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1626 /*
1627 * It appears to be important that we don't enable this
1628 * for the current pipe before otherwise configuring the
1629 * PLL. No idea how this should be handled if multiple
1630 * DVO outputs are enabled simultaneosly.
1631 */
1632 dpll |= DPLL_DVO_2X_MODE;
1633 I915_WRITE(DPLL(!crtc->pipe),
1634 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1635 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001636
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001637 /*
1638 * Apparently we need to have VGA mode enabled prior to changing
1639 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1640 * dividers, even though the register value does change.
1641 */
1642 I915_WRITE(reg, 0);
1643
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001644 I915_WRITE(reg, dpll);
1645
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001646 /* Wait for the clocks to stabilize. */
1647 POSTING_READ(reg);
1648 udelay(150);
1649
1650 if (INTEL_INFO(dev)->gen >= 4) {
1651 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001652 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001653 } else {
1654 /* The pixel multiplier can only be updated once the
1655 * DPLL is enabled and the clocks are stable.
1656 *
1657 * So write it again.
1658 */
1659 I915_WRITE(reg, dpll);
1660 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001661
1662 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001663 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001664 POSTING_READ(reg);
1665 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001666 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001667 POSTING_READ(reg);
1668 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001669 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001670 POSTING_READ(reg);
1671 udelay(150); /* wait for warmup */
1672}
1673
1674/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001675 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001676 * @dev_priv: i915 private structure
1677 * @pipe: pipe PLL to disable
1678 *
1679 * Disable the PLL for @pipe, making sure the pipe is off first.
1680 *
1681 * Note! This is for pre-ILK only.
1682 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001683static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001684{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001685 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001686 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001687 enum pipe pipe = crtc->pipe;
1688
1689 /* Disable DVO 2x clock on both PLLs if necessary */
1690 if (IS_I830(dev) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001691 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001692 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001693 I915_WRITE(DPLL(PIPE_B),
1694 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1695 I915_WRITE(DPLL(PIPE_A),
1696 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1697 }
1698
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001699 /* Don't disable pipe or pipe PLLs if needed */
1700 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1701 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001702 return;
1703
1704 /* Make sure the pipe isn't still relying on us */
1705 assert_pipe_disabled(dev_priv, pipe);
1706
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001707 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001708 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001709}
1710
Jesse Barnesf6071162013-10-01 10:41:38 -07001711static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1712{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001713 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001714
1715 /* Make sure the pipe isn't still relying on us */
1716 assert_pipe_disabled(dev_priv, pipe);
1717
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001718 val = DPLL_INTEGRATED_REF_CLK_VLV |
1719 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1720 if (pipe != PIPE_A)
1721 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1722
Jesse Barnesf6071162013-10-01 10:41:38 -07001723 I915_WRITE(DPLL(pipe), val);
1724 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001725}
1726
1727static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1728{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001729 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001730 u32 val;
1731
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001732 /* Make sure the pipe isn't still relying on us */
1733 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001734
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001735 val = DPLL_SSC_REF_CLK_CHV |
1736 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001737 if (pipe != PIPE_A)
1738 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001739
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001740 I915_WRITE(DPLL(pipe), val);
1741 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001742
Ville Syrjäläa5805162015-05-26 20:42:30 +03001743 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001744
1745 /* Disable 10bit clock to display controller */
1746 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1747 val &= ~DPIO_DCLKP_EN;
1748 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1749
Ville Syrjäläa5805162015-05-26 20:42:30 +03001750 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001751}
1752
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001753void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001754 struct intel_digital_port *dport,
1755 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001756{
1757 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001758 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001759
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001760 switch (dport->port) {
1761 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001762 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001763 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001764 break;
1765 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001766 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001767 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001768 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001769 break;
1770 case PORT_D:
1771 port_mask = DPLL_PORTD_READY_MASK;
1772 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001773 break;
1774 default:
1775 BUG();
1776 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001777
Chris Wilson370004d2016-06-30 15:32:56 +01001778 if (intel_wait_for_register(dev_priv,
1779 dpll_reg, port_mask, expected_mask,
1780 1000))
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001781 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1782 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001783}
1784
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001785static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1786 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001787{
Chris Wilson91c8a322016-07-05 10:40:23 +01001788 struct drm_device *dev = &dev_priv->drm;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001789 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001790 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001791 i915_reg_t reg;
1792 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001793
Jesse Barnes040484a2011-01-03 12:14:26 -08001794 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001795 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001796
1797 /* FDI must be feeding us bits for PCH ports */
1798 assert_fdi_tx_enabled(dev_priv, pipe);
1799 assert_fdi_rx_enabled(dev_priv, pipe);
1800
Daniel Vetter23670b322012-11-01 09:15:30 +01001801 if (HAS_PCH_CPT(dev)) {
1802 /* Workaround: Set the timing override bit before enabling the
1803 * pch transcoder. */
1804 reg = TRANS_CHICKEN2(pipe);
1805 val = I915_READ(reg);
1806 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1807 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001808 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001809
Daniel Vetterab9412b2013-05-03 11:49:46 +02001810 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001811 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001812 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001813
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001814 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001815 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001816 * Make the BPC in transcoder be consistent with
1817 * that in pipeconf reg. For HDMI we must use 8bpc
1818 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001819 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001820 val &= ~PIPECONF_BPC_MASK;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001821 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001822 val |= PIPECONF_8BPC;
1823 else
1824 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001825 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001826
1827 val &= ~TRANS_INTERLACE_MASK;
1828 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001829 if (HAS_PCH_IBX(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001830 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001831 val |= TRANS_LEGACY_INTERLACED_ILK;
1832 else
1833 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001834 else
1835 val |= TRANS_PROGRESSIVE;
1836
Jesse Barnes040484a2011-01-03 12:14:26 -08001837 I915_WRITE(reg, val | TRANS_ENABLE);
Chris Wilson650fbd82016-06-30 15:32:57 +01001838 if (intel_wait_for_register(dev_priv,
1839 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1840 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001841 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001842}
1843
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001844static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001845 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001846{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001847 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001848
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001849 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001850 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001851 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001852
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001853 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001854 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001855 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001856 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001857
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001858 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001859 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001860
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001861 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1862 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001863 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001864 else
1865 val |= TRANS_PROGRESSIVE;
1866
Daniel Vetterab9412b2013-05-03 11:49:46 +02001867 I915_WRITE(LPT_TRANSCONF, val);
Chris Wilsond9f96242016-06-30 15:32:58 +01001868 if (intel_wait_for_register(dev_priv,
1869 LPT_TRANSCONF,
1870 TRANS_STATE_ENABLE,
1871 TRANS_STATE_ENABLE,
1872 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001873 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001874}
1875
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001876static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1877 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001878{
Chris Wilson91c8a322016-07-05 10:40:23 +01001879 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001880 i915_reg_t reg;
1881 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001882
1883 /* FDI relies on the transcoder */
1884 assert_fdi_tx_disabled(dev_priv, pipe);
1885 assert_fdi_rx_disabled(dev_priv, pipe);
1886
Jesse Barnes291906f2011-02-02 12:28:03 -08001887 /* Ports must be off as well */
1888 assert_pch_ports_disabled(dev_priv, pipe);
1889
Daniel Vetterab9412b2013-05-03 11:49:46 +02001890 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001891 val = I915_READ(reg);
1892 val &= ~TRANS_ENABLE;
1893 I915_WRITE(reg, val);
1894 /* wait for PCH transcoder off, transcoder state */
Chris Wilsona7d04662016-06-30 15:32:59 +01001895 if (intel_wait_for_register(dev_priv,
1896 reg, TRANS_STATE_ENABLE, 0,
1897 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001898 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001899
Ville Syrjäläc4656132015-10-29 21:25:56 +02001900 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001901 /* Workaround: Clear the timing override chicken bit again. */
1902 reg = TRANS_CHICKEN2(pipe);
1903 val = I915_READ(reg);
1904 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1905 I915_WRITE(reg, val);
1906 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001907}
1908
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001909static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001910{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001911 u32 val;
1912
Daniel Vetterab9412b2013-05-03 11:49:46 +02001913 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001914 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001915 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001916 /* wait for PCH transcoder off, transcoder state */
Chris Wilsondfdb4742016-06-30 15:33:00 +01001917 if (intel_wait_for_register(dev_priv,
1918 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1919 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001920 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001921
1922 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001923 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001924 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001925 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001926}
1927
1928/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001929 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001930 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001931 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001932 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001933 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001934 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001935static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001936{
Paulo Zanoni03722642014-01-17 13:51:09 -02001937 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001938 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni03722642014-01-17 13:51:09 -02001939 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001940 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001941 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001942 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001943 u32 val;
1944
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001945 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1946
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001947 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001948 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001949 assert_sprites_disabled(dev_priv, pipe);
1950
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001951 if (HAS_PCH_LPT(dev_priv))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001952 pch_transcoder = TRANSCODER_A;
1953 else
1954 pch_transcoder = pipe;
1955
Jesse Barnesb24e7172011-01-04 15:09:30 -08001956 /*
1957 * A pipe without a PLL won't actually be able to drive bits from
1958 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1959 * need the check.
1960 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001961 if (HAS_GMCH_DISPLAY(dev_priv))
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03001962 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03001963 assert_dsi_pll_enabled(dev_priv);
1964 else
1965 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001966 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001967 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001968 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001969 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001970 assert_fdi_tx_pll_enabled(dev_priv,
1971 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001972 }
1973 /* FIXME: assert CPU port conditions for SNB+ */
1974 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001975
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001976 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001977 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001978 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001979 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1980 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00001981 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001982 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001983
1984 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001985 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001986
1987 /*
1988 * Until the pipe starts DSL will read as 0, which would cause
1989 * an apparent vblank timestamp jump, which messes up also the
1990 * frame count when it's derived from the timestamps. So let's
1991 * wait for the pipe to start properly before we call
1992 * drm_crtc_vblank_on()
1993 */
1994 if (dev->max_vblank_count == 0 &&
1995 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1996 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001997}
1998
1999/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002000 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002001 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002002 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002003 * Disable the pipe of @crtc, making sure that various hardware
2004 * specific requirements are met, if applicable, e.g. plane
2005 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002006 *
2007 * Will wait until the pipe has shut down before returning.
2008 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002009static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002010{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002011 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002012 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002013 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002014 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002015 u32 val;
2016
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002017 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2018
Jesse Barnesb24e7172011-01-04 15:09:30 -08002019 /*
2020 * Make sure planes won't keep trying to pump pixels to us,
2021 * or we might hang the display.
2022 */
2023 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002024 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002025 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002026
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002027 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002028 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002029 if ((val & PIPECONF_ENABLE) == 0)
2030 return;
2031
Ville Syrjälä67adc642014-08-15 01:21:57 +03002032 /*
2033 * Double wide has implications for planes
2034 * so best keep it disabled when not needed.
2035 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002036 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002037 val &= ~PIPECONF_DOUBLE_WIDE;
2038
2039 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002040 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2041 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002042 val &= ~PIPECONF_ENABLE;
2043
2044 I915_WRITE(reg, val);
2045 if ((val & PIPECONF_ENABLE) == 0)
2046 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002047}
2048
Chris Wilson693db182013-03-05 14:52:39 +00002049static bool need_vtd_wa(struct drm_device *dev)
2050{
2051#ifdef CONFIG_INTEL_IOMMU
2052 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2053 return true;
2054#endif
2055 return false;
2056}
2057
Ville Syrjälä832be822016-01-12 21:08:33 +02002058static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2059{
2060 return IS_GEN2(dev_priv) ? 2048 : 4096;
2061}
2062
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002063static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2064 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002065{
2066 switch (fb_modifier) {
2067 case DRM_FORMAT_MOD_NONE:
2068 return cpp;
2069 case I915_FORMAT_MOD_X_TILED:
2070 if (IS_GEN2(dev_priv))
2071 return 128;
2072 else
2073 return 512;
2074 case I915_FORMAT_MOD_Y_TILED:
2075 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2076 return 128;
2077 else
2078 return 512;
2079 case I915_FORMAT_MOD_Yf_TILED:
2080 switch (cpp) {
2081 case 1:
2082 return 64;
2083 case 2:
2084 case 4:
2085 return 128;
2086 case 8:
2087 case 16:
2088 return 256;
2089 default:
2090 MISSING_CASE(cpp);
2091 return cpp;
2092 }
2093 break;
2094 default:
2095 MISSING_CASE(fb_modifier);
2096 return cpp;
2097 }
2098}
2099
Ville Syrjälä832be822016-01-12 21:08:33 +02002100unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2101 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002102{
Ville Syrjälä832be822016-01-12 21:08:33 +02002103 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2104 return 1;
2105 else
2106 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002107 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002108}
2109
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002110/* Return the tile dimensions in pixel units */
2111static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2112 unsigned int *tile_width,
2113 unsigned int *tile_height,
2114 uint64_t fb_modifier,
2115 unsigned int cpp)
2116{
2117 unsigned int tile_width_bytes =
2118 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2119
2120 *tile_width = tile_width_bytes / cpp;
2121 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2122}
2123
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002124unsigned int
2125intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002126 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002127{
Ville Syrjälä832be822016-01-12 21:08:33 +02002128 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2129 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2130
2131 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002132}
2133
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002134unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2135{
2136 unsigned int size = 0;
2137 int i;
2138
2139 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2140 size += rot_info->plane[i].width * rot_info->plane[i].height;
2141
2142 return size;
2143}
2144
Daniel Vetter75c82a52015-10-14 16:51:04 +02002145static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002146intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2147 const struct drm_framebuffer *fb,
2148 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002149{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002150 if (intel_rotation_90_or_270(rotation)) {
2151 *view = i915_ggtt_view_rotated;
2152 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2153 } else {
2154 *view = i915_ggtt_view_normal;
2155 }
2156}
2157
2158static void
2159intel_fill_fb_info(struct drm_i915_private *dev_priv,
2160 struct drm_framebuffer *fb)
2161{
2162 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002163 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002164
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002165 tile_size = intel_tile_size(dev_priv);
2166
2167 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002168 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2169 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002170
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002171 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2172 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002173
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002174 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002175 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002176 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2177 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002178
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002179 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002180 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2181 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002182 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002183}
2184
Ville Syrjälä603525d2016-01-12 21:08:37 +02002185static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002186{
2187 if (INTEL_INFO(dev_priv)->gen >= 9)
2188 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002189 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002190 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002191 return 128 * 1024;
2192 else if (INTEL_INFO(dev_priv)->gen >= 4)
2193 return 4 * 1024;
2194 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002195 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002196}
2197
Ville Syrjälä603525d2016-01-12 21:08:37 +02002198static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2199 uint64_t fb_modifier)
2200{
2201 switch (fb_modifier) {
2202 case DRM_FORMAT_MOD_NONE:
2203 return intel_linear_alignment(dev_priv);
2204 case I915_FORMAT_MOD_X_TILED:
2205 if (INTEL_INFO(dev_priv)->gen >= 9)
2206 return 256 * 1024;
2207 return 0;
2208 case I915_FORMAT_MOD_Y_TILED:
2209 case I915_FORMAT_MOD_Yf_TILED:
2210 return 1 * 1024 * 1024;
2211 default:
2212 MISSING_CASE(fb_modifier);
2213 return 0;
2214 }
2215}
2216
Chris Wilson127bd2a2010-07-23 23:32:05 +01002217int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002218intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2219 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002220{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002221 struct drm_device *dev = fb->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002222 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002223 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002224 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002225 u32 alignment;
2226 int ret;
2227
Matt Roperebcdd392014-07-09 16:22:11 -07002228 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2229
Ville Syrjälä603525d2016-01-12 21:08:37 +02002230 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002231
Ville Syrjälä3465c582016-02-15 22:54:43 +02002232 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002233
Chris Wilson693db182013-03-05 14:52:39 +00002234 /* Note that the w/a also requires 64 PTE of padding following the
2235 * bo. We currently fill all unused PTE with the shadow page and so
2236 * we should always have valid PTE following the scanout preventing
2237 * the VT-d warning.
2238 */
2239 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2240 alignment = 256 * 1024;
2241
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002242 /*
2243 * Global gtt pte registers are special registers which actually forward
2244 * writes to a chunk of system memory. Which means that there is no risk
2245 * that the register values disappear as soon as we call
2246 * intel_runtime_pm_put(), so it is correct to wrap only the
2247 * pin/unpin/fence and not more.
2248 */
2249 intel_runtime_pm_get(dev_priv);
2250
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002251 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2252 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002253 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002254 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002255
2256 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2257 * fence, whereas 965+ only requires a fence if using
2258 * framebuffer compression. For simplicity, we always install
2259 * a fence as the cost is not that onerous.
2260 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002261 if (view.type == I915_GGTT_VIEW_NORMAL) {
2262 ret = i915_gem_object_get_fence(obj);
2263 if (ret == -EDEADLK) {
2264 /*
2265 * -EDEADLK means there are no free fences
2266 * no pending flips.
2267 *
2268 * This is propagated to atomic, but it uses
2269 * -EDEADLK to force a locking recovery, so
2270 * change the returned error to -EBUSY.
2271 */
2272 ret = -EBUSY;
2273 goto err_unpin;
2274 } else if (ret)
2275 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002276
Vivek Kasireddy98072162015-10-29 18:54:38 -07002277 i915_gem_object_pin_fence(obj);
2278 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002279
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002280 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002281 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002282
2283err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002284 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002285err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002286 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002287 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002288}
2289
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +01002290void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002291{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002292 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002293 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002294
Matt Roperebcdd392014-07-09 16:22:11 -07002295 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2296
Ville Syrjälä3465c582016-02-15 22:54:43 +02002297 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002298
Vivek Kasireddy98072162015-10-29 18:54:38 -07002299 if (view.type == I915_GGTT_VIEW_NORMAL)
2300 i915_gem_object_unpin_fence(obj);
2301
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002302 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002303}
2304
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002305/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002306 * Adjust the tile offset by moving the difference into
2307 * the x/y offsets.
2308 *
2309 * Input tile dimensions and pitch must already be
2310 * rotated to match x and y, and in pixel units.
2311 */
2312static u32 intel_adjust_tile_offset(int *x, int *y,
2313 unsigned int tile_width,
2314 unsigned int tile_height,
2315 unsigned int tile_size,
2316 unsigned int pitch_tiles,
2317 u32 old_offset,
2318 u32 new_offset)
2319{
2320 unsigned int tiles;
2321
2322 WARN_ON(old_offset & (tile_size - 1));
2323 WARN_ON(new_offset & (tile_size - 1));
2324 WARN_ON(new_offset > old_offset);
2325
2326 tiles = (old_offset - new_offset) / tile_size;
2327
2328 *y += tiles / pitch_tiles * tile_height;
2329 *x += tiles % pitch_tiles * tile_width;
2330
2331 return new_offset;
2332}
2333
2334/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002335 * Computes the linear offset to the base tile and adjusts
2336 * x, y. bytes per pixel is assumed to be a power-of-two.
2337 *
2338 * In the 90/270 rotated case, x and y are assumed
2339 * to be already rotated to match the rotated GTT view, and
2340 * pitch is the tile_height aligned framebuffer height.
2341 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002342u32 intel_compute_tile_offset(int *x, int *y,
2343 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002344 unsigned int pitch,
2345 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002346{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002347 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2348 uint64_t fb_modifier = fb->modifier[plane];
2349 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002350 u32 offset, offset_aligned, alignment;
2351
2352 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2353 if (alignment)
2354 alignment--;
2355
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002356 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002357 unsigned int tile_size, tile_width, tile_height;
2358 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002359
Ville Syrjäläd8433102016-01-12 21:08:35 +02002360 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002361 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2362 fb_modifier, cpp);
2363
2364 if (intel_rotation_90_or_270(rotation)) {
2365 pitch_tiles = pitch / tile_height;
2366 swap(tile_width, tile_height);
2367 } else {
2368 pitch_tiles = pitch / (tile_width * cpp);
2369 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002370
Ville Syrjäläd8433102016-01-12 21:08:35 +02002371 tile_rows = *y / tile_height;
2372 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002373
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002374 tiles = *x / tile_width;
2375 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002376
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002377 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2378 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002379
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002380 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2381 tile_size, pitch_tiles,
2382 offset, offset_aligned);
2383 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002384 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002385 offset_aligned = offset & ~alignment;
2386
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002387 *y = (offset & alignment) / pitch;
2388 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002389 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002390
2391 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002392}
2393
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002394static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002395{
2396 switch (format) {
2397 case DISPPLANE_8BPP:
2398 return DRM_FORMAT_C8;
2399 case DISPPLANE_BGRX555:
2400 return DRM_FORMAT_XRGB1555;
2401 case DISPPLANE_BGRX565:
2402 return DRM_FORMAT_RGB565;
2403 default:
2404 case DISPPLANE_BGRX888:
2405 return DRM_FORMAT_XRGB8888;
2406 case DISPPLANE_RGBX888:
2407 return DRM_FORMAT_XBGR8888;
2408 case DISPPLANE_BGRX101010:
2409 return DRM_FORMAT_XRGB2101010;
2410 case DISPPLANE_RGBX101010:
2411 return DRM_FORMAT_XBGR2101010;
2412 }
2413}
2414
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002415static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2416{
2417 switch (format) {
2418 case PLANE_CTL_FORMAT_RGB_565:
2419 return DRM_FORMAT_RGB565;
2420 default:
2421 case PLANE_CTL_FORMAT_XRGB_8888:
2422 if (rgb_order) {
2423 if (alpha)
2424 return DRM_FORMAT_ABGR8888;
2425 else
2426 return DRM_FORMAT_XBGR8888;
2427 } else {
2428 if (alpha)
2429 return DRM_FORMAT_ARGB8888;
2430 else
2431 return DRM_FORMAT_XRGB8888;
2432 }
2433 case PLANE_CTL_FORMAT_XRGB_2101010:
2434 if (rgb_order)
2435 return DRM_FORMAT_XBGR2101010;
2436 else
2437 return DRM_FORMAT_XRGB2101010;
2438 }
2439}
2440
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002441static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002442intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2443 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002444{
2445 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002446 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002447 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002448 struct drm_i915_gem_object *obj = NULL;
2449 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002450 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002451 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2452 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2453 PAGE_SIZE);
2454
2455 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002456
Chris Wilsonff2652e2014-03-10 08:07:02 +00002457 if (plane_config->size == 0)
2458 return false;
2459
Paulo Zanoni3badb492015-09-23 12:52:23 -03002460 /* If the FB is too big, just don't use it since fbdev is not very
2461 * important and we should probably use that space with FBC or other
2462 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002463 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002464 return false;
2465
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002466 mutex_lock(&dev->struct_mutex);
2467
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002468 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2469 base_aligned,
2470 base_aligned,
2471 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002472 if (!obj) {
2473 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002474 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002475 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002476
Damien Lespiau49af4492015-01-20 12:51:44 +00002477 obj->tiling_mode = plane_config->tiling;
2478 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002479 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002480
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002481 mode_cmd.pixel_format = fb->pixel_format;
2482 mode_cmd.width = fb->width;
2483 mode_cmd.height = fb->height;
2484 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002485 mode_cmd.modifier[0] = fb->modifier[0];
2486 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002487
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002488 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002489 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002490 DRM_DEBUG_KMS("intel fb init failed\n");
2491 goto out_unref_obj;
2492 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002493
Jesse Barnes46f297f2014-03-07 08:57:48 -08002494 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002495
Daniel Vetterf6936e22015-03-26 12:17:05 +01002496 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002497 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002498
2499out_unref_obj:
2500 drm_gem_object_unreference(&obj->base);
2501 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002502 return false;
2503}
2504
Daniel Vetter5a21b662016-05-24 17:13:53 +02002505/* Update plane->state->fb to match plane->fb after driver-internal updates */
2506static void
2507update_state_fb(struct drm_plane *plane)
2508{
2509 if (plane->fb == plane->state->fb)
2510 return;
2511
2512 if (plane->state->fb)
2513 drm_framebuffer_unreference(plane->state->fb);
2514 plane->state->fb = plane->fb;
2515 if (plane->state->fb)
2516 drm_framebuffer_reference(plane->state->fb);
2517}
2518
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002519static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002520intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2521 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002522{
2523 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002524 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002525 struct drm_crtc *c;
2526 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002527 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002528 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002529 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002530 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2531 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002532 struct intel_plane_state *intel_state =
2533 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002534 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002535
Damien Lespiau2d140302015-02-05 17:22:18 +00002536 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002537 return;
2538
Daniel Vetterf6936e22015-03-26 12:17:05 +01002539 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002540 fb = &plane_config->fb->base;
2541 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002542 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002543
Damien Lespiau2d140302015-02-05 17:22:18 +00002544 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002545
2546 /*
2547 * Failed to alloc the obj, check to see if we should share
2548 * an fb with another CRTC instead
2549 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002550 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002551 i = to_intel_crtc(c);
2552
2553 if (c == &intel_crtc->base)
2554 continue;
2555
Matt Roper2ff8fde2014-07-08 07:50:07 -07002556 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002557 continue;
2558
Daniel Vetter88595ac2015-03-26 12:42:24 +01002559 fb = c->primary->fb;
2560 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002561 continue;
2562
Daniel Vetter88595ac2015-03-26 12:42:24 +01002563 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002564 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002565 drm_framebuffer_reference(fb);
2566 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002567 }
2568 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002569
Matt Roper200757f2015-12-03 11:37:36 -08002570 /*
2571 * We've failed to reconstruct the BIOS FB. Current display state
2572 * indicates that the primary plane is visible, but has a NULL FB,
2573 * which will lead to problems later if we don't fix it up. The
2574 * simplest solution is to just disable the primary plane now and
2575 * pretend the BIOS never had it enabled.
2576 */
2577 to_intel_plane_state(plane_state)->visible = false;
2578 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002579 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002580 intel_plane->disable_plane(primary, &intel_crtc->base);
2581
Daniel Vetter88595ac2015-03-26 12:42:24 +01002582 return;
2583
2584valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002585 plane_state->src_x = 0;
2586 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002587 plane_state->src_w = fb->width << 16;
2588 plane_state->src_h = fb->height << 16;
2589
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002590 plane_state->crtc_x = 0;
2591 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002592 plane_state->crtc_w = fb->width;
2593 plane_state->crtc_h = fb->height;
2594
Matt Roper0a8d8a82015-12-03 11:37:38 -08002595 intel_state->src.x1 = plane_state->src_x;
2596 intel_state->src.y1 = plane_state->src_y;
2597 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2598 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2599 intel_state->dst.x1 = plane_state->crtc_x;
2600 intel_state->dst.y1 = plane_state->crtc_y;
2601 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2602 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2603
Daniel Vetter88595ac2015-03-26 12:42:24 +01002604 obj = intel_fb_obj(fb);
2605 if (obj->tiling_mode != I915_TILING_NONE)
2606 dev_priv->preserve_bios_swizzle = true;
2607
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002608 drm_framebuffer_reference(fb);
2609 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002610 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002611 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002612 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002613}
2614
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002615static void i9xx_update_primary_plane(struct drm_plane *primary,
2616 const struct intel_crtc_state *crtc_state,
2617 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002618{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002619 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002620 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002621 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2622 struct drm_framebuffer *fb = plane_state->base.fb;
2623 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002624 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002625 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002626 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002627 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002628 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002629 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002630 int x = plane_state->src.x1 >> 16;
2631 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002632
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002633 dspcntr = DISPPLANE_GAMMA_ENABLE;
2634
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002635 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002636
2637 if (INTEL_INFO(dev)->gen < 4) {
2638 if (intel_crtc->pipe == PIPE_B)
2639 dspcntr |= DISPPLANE_SEL_PIPE_B;
2640
2641 /* pipesrc and dspsize control the size that is scaled from,
2642 * which should always be the user's requested size.
2643 */
2644 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002645 ((crtc_state->pipe_src_h - 1) << 16) |
2646 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002647 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002648 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2649 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002650 ((crtc_state->pipe_src_h - 1) << 16) |
2651 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002652 I915_WRITE(PRIMPOS(plane), 0);
2653 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002654 }
2655
Ville Syrjälä57779d02012-10-31 17:50:14 +02002656 switch (fb->pixel_format) {
2657 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002658 dspcntr |= DISPPLANE_8BPP;
2659 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002660 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002661 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002662 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002663 case DRM_FORMAT_RGB565:
2664 dspcntr |= DISPPLANE_BGRX565;
2665 break;
2666 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002667 dspcntr |= DISPPLANE_BGRX888;
2668 break;
2669 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002670 dspcntr |= DISPPLANE_RGBX888;
2671 break;
2672 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002673 dspcntr |= DISPPLANE_BGRX101010;
2674 break;
2675 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002676 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002677 break;
2678 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002679 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002680 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002681
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002682 if (INTEL_INFO(dev)->gen >= 4 &&
2683 obj->tiling_mode != I915_TILING_NONE)
2684 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002685
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002686 if (IS_G4X(dev))
2687 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2688
Ville Syrjäläac484962016-01-20 21:05:26 +02002689 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002690
Daniel Vetterc2c75132012-07-05 12:17:30 +02002691 if (INTEL_INFO(dev)->gen >= 4) {
2692 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002693 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002694 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002695 linear_offset -= intel_crtc->dspaddr_offset;
2696 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002697 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002698 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002699
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002700 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302701 dspcntr |= DISPPLANE_ROTATE_180;
2702
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002703 x += (crtc_state->pipe_src_w - 1);
2704 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302705
2706 /* Finding the last pixel of the last line of the display
2707 data and adding to linear_offset*/
2708 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002709 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002710 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302711 }
2712
Paulo Zanoni2db33662015-09-14 15:20:03 -03002713 intel_crtc->adjusted_x = x;
2714 intel_crtc->adjusted_y = y;
2715
Sonika Jindal48404c12014-08-22 14:06:04 +05302716 I915_WRITE(reg, dspcntr);
2717
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002718 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002719 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002720 I915_WRITE(DSPSURF(plane),
2721 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002722 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002723 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002724 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002725 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002726 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002727}
2728
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002729static void i9xx_disable_primary_plane(struct drm_plane *primary,
2730 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002731{
2732 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002733 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002734 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002735 int plane = intel_crtc->plane;
2736
2737 I915_WRITE(DSPCNTR(plane), 0);
2738 if (INTEL_INFO(dev_priv)->gen >= 4)
2739 I915_WRITE(DSPSURF(plane), 0);
2740 else
2741 I915_WRITE(DSPADDR(plane), 0);
2742 POSTING_READ(DSPCNTR(plane));
2743}
2744
2745static void ironlake_update_primary_plane(struct drm_plane *primary,
2746 const struct intel_crtc_state *crtc_state,
2747 const struct intel_plane_state *plane_state)
2748{
2749 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002750 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002751 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2752 struct drm_framebuffer *fb = plane_state->base.fb;
2753 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002754 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002755 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002756 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002757 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002758 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002759 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002760 int x = plane_state->src.x1 >> 16;
2761 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002762
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002763 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002764 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002765
2766 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2767 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2768
Ville Syrjälä57779d02012-10-31 17:50:14 +02002769 switch (fb->pixel_format) {
2770 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002771 dspcntr |= DISPPLANE_8BPP;
2772 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002773 case DRM_FORMAT_RGB565:
2774 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002775 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002776 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002777 dspcntr |= DISPPLANE_BGRX888;
2778 break;
2779 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002780 dspcntr |= DISPPLANE_RGBX888;
2781 break;
2782 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002783 dspcntr |= DISPPLANE_BGRX101010;
2784 break;
2785 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002786 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002787 break;
2788 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002789 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002790 }
2791
2792 if (obj->tiling_mode != I915_TILING_NONE)
2793 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002794
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002795 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002796 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002797
Ville Syrjäläac484962016-01-20 21:05:26 +02002798 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002799 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002800 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002801 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002802 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002803 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302804 dspcntr |= DISPPLANE_ROTATE_180;
2805
2806 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002807 x += (crtc_state->pipe_src_w - 1);
2808 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302809
2810 /* Finding the last pixel of the last line of the display
2811 data and adding to linear_offset*/
2812 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002813 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002814 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302815 }
2816 }
2817
Paulo Zanoni2db33662015-09-14 15:20:03 -03002818 intel_crtc->adjusted_x = x;
2819 intel_crtc->adjusted_y = y;
2820
Sonika Jindal48404c12014-08-22 14:06:04 +05302821 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002822
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002823 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002824 I915_WRITE(DSPSURF(plane),
2825 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002826 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002827 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2828 } else {
2829 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2830 I915_WRITE(DSPLINOFF(plane), linear_offset);
2831 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002832 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002833}
2834
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002835u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2836 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002837{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002838 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2839 return 64;
2840 } else {
2841 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002842
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002843 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002844 }
2845}
2846
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002847u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2848 struct drm_i915_gem_object *obj,
2849 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002850{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002851 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002852 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002853 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002854
Ville Syrjäläe7941292016-01-19 18:23:17 +02002855 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002856 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002857
Daniel Vetterce7f1722015-10-14 16:51:06 +02002858 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002859 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002860 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002861 return -1;
2862
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002863 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002864
2865 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002866 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002867 PAGE_SIZE;
2868 }
2869
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002870 WARN_ON(upper_32_bits(offset));
2871
2872 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002873}
2874
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002875static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2876{
2877 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002878 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002879
2880 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2881 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2882 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002883}
2884
Chandra Kondurua1b22782015-04-07 15:28:45 -07002885/*
2886 * This function detaches (aka. unbinds) unused scalers in hardware
2887 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002888static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002889{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002890 struct intel_crtc_scaler_state *scaler_state;
2891 int i;
2892
Chandra Kondurua1b22782015-04-07 15:28:45 -07002893 scaler_state = &intel_crtc->config->scaler_state;
2894
2895 /* loop through and disable scalers that aren't in use */
2896 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002897 if (!scaler_state->scalers[i].in_use)
2898 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002899 }
2900}
2901
Chandra Konduru6156a452015-04-27 13:48:39 -07002902u32 skl_plane_ctl_format(uint32_t pixel_format)
2903{
Chandra Konduru6156a452015-04-27 13:48:39 -07002904 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002905 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002906 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002907 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002908 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002909 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002910 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002911 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002912 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002913 /*
2914 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2915 * to be already pre-multiplied. We need to add a knob (or a different
2916 * DRM_FORMAT) for user-space to configure that.
2917 */
2918 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002919 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002920 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002921 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002922 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002923 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002924 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002925 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002926 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002927 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002928 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002929 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002930 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002931 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002932 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002933 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002934 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002935 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002936 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002937 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002938 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002939
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002940 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002941}
2942
2943u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2944{
Chandra Konduru6156a452015-04-27 13:48:39 -07002945 switch (fb_modifier) {
2946 case DRM_FORMAT_MOD_NONE:
2947 break;
2948 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002949 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002951 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002952 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002953 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 default:
2955 MISSING_CASE(fb_modifier);
2956 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002957
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002958 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002959}
2960
2961u32 skl_plane_ctl_rotation(unsigned int rotation)
2962{
Chandra Konduru6156a452015-04-27 13:48:39 -07002963 switch (rotation) {
2964 case BIT(DRM_ROTATE_0):
2965 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302966 /*
2967 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2968 * while i915 HW rotation is clockwise, thats why this swapping.
2969 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002970 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302971 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002972 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002973 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002974 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302975 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002976 default:
2977 MISSING_CASE(rotation);
2978 }
2979
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002980 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002981}
2982
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002983static void skylake_update_primary_plane(struct drm_plane *plane,
2984 const struct intel_crtc_state *crtc_state,
2985 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01002986{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002987 struct drm_device *dev = plane->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002988 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002989 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2990 struct drm_framebuffer *fb = plane_state->base.fb;
2991 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002992 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302993 u32 plane_ctl, stride_div, stride;
2994 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002995 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302996 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002997 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002998 int scaler_id = plane_state->scaler_id;
2999 int src_x = plane_state->src.x1 >> 16;
3000 int src_y = plane_state->src.y1 >> 16;
3001 int src_w = drm_rect_width(&plane_state->src) >> 16;
3002 int src_h = drm_rect_height(&plane_state->src) >> 16;
3003 int dst_x = plane_state->dst.x1;
3004 int dst_y = plane_state->dst.y1;
3005 int dst_w = drm_rect_width(&plane_state->dst);
3006 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003007
3008 plane_ctl = PLANE_CTL_ENABLE |
3009 PLANE_CTL_PIPE_GAMMA_ENABLE |
3010 PLANE_CTL_PIPE_CSC_ENABLE;
3011
Chandra Konduru6156a452015-04-27 13:48:39 -07003012 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3013 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003014 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003015 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003016
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003017 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003018 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003019 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303020
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003021 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003022
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303023 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003024 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3025
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303026 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003027 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303028 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003029 x_offset = stride * tile_height - src_y - src_h;
3030 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003031 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303032 } else {
3033 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003034 x_offset = src_x;
3035 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003036 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303037 }
3038 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003039
Paulo Zanoni2db33662015-09-14 15:20:03 -03003040 intel_crtc->adjusted_x = x_offset;
3041 intel_crtc->adjusted_y = y_offset;
3042
Damien Lespiau70d21f02013-07-03 21:06:04 +01003043 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303044 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3045 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3046 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003047
3048 if (scaler_id >= 0) {
3049 uint32_t ps_ctrl = 0;
3050
3051 WARN_ON(!dst_w || !dst_h);
3052 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3053 crtc_state->scaler_state.scalers[scaler_id].mode;
3054 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3055 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3056 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3057 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3058 I915_WRITE(PLANE_POS(pipe, 0), 0);
3059 } else {
3060 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3061 }
3062
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003063 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003064
3065 POSTING_READ(PLANE_SURF(pipe, 0));
3066}
3067
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003068static void skylake_disable_primary_plane(struct drm_plane *primary,
3069 struct drm_crtc *crtc)
3070{
3071 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003072 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003073 int pipe = to_intel_crtc(crtc)->pipe;
3074
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003075 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3076 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3077 POSTING_READ(PLANE_SURF(pipe, 0));
3078}
3079
Jesse Barnes17638cd2011-06-24 12:19:23 -07003080/* Assume fb object is pinned & idle & fenced and just update base pointers */
3081static int
3082intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3083 int x, int y, enum mode_set_atomic state)
3084{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003085 /* Support for kgdboc is disabled, this needs a major rework. */
3086 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003087
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003088 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003089}
3090
Daniel Vetter5a21b662016-05-24 17:13:53 +02003091static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3092{
3093 struct intel_crtc *crtc;
3094
Chris Wilson91c8a322016-07-05 10:40:23 +01003095 for_each_intel_crtc(&dev_priv->drm, crtc)
Daniel Vetter5a21b662016-05-24 17:13:53 +02003096 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3097}
3098
Ville Syrjälä75147472014-11-24 18:28:11 +02003099static void intel_update_primary_planes(struct drm_device *dev)
3100{
Ville Syrjälä75147472014-11-24 18:28:11 +02003101 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003102
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003103 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003104 struct intel_plane *plane = to_intel_plane(crtc->primary);
3105 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003106
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003107 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003108 plane_state = to_intel_plane_state(plane->base.state);
3109
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003110 if (plane_state->visible)
3111 plane->update_plane(&plane->base,
3112 to_intel_crtc_state(crtc->state),
3113 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003114
3115 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003116 }
3117}
3118
Chris Wilsonc0336662016-05-06 15:40:21 +01003119void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003120{
3121 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003122 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003123 return;
3124
3125 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003126 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003127 return;
3128
Chris Wilson91c8a322016-07-05 10:40:23 +01003129 drm_modeset_lock_all(&dev_priv->drm);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003130 /*
3131 * Disabling the crtcs gracefully seems nicer. Also the
3132 * g33 docs say we should at least disable all the planes.
3133 */
Chris Wilson91c8a322016-07-05 10:40:23 +01003134 intel_display_suspend(&dev_priv->drm);
Ville Syrjälä75147472014-11-24 18:28:11 +02003135}
3136
Chris Wilsonc0336662016-05-06 15:40:21 +01003137void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003138{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003139 /*
3140 * Flips in the rings will be nuked by the reset,
3141 * so complete all pending flips so that user space
3142 * will get its events and not get stuck.
3143 */
3144 intel_complete_page_flips(dev_priv);
3145
Ville Syrjälä75147472014-11-24 18:28:11 +02003146 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003147 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003148 return;
3149
3150 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003151 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
Ville Syrjälä75147472014-11-24 18:28:11 +02003152 /*
3153 * Flips in the rings have been nuked by the reset,
3154 * so update the base address of all primary
3155 * planes to the the last fb to make sure we're
3156 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003157 *
3158 * FIXME: Atomic will make this obsolete since we won't schedule
3159 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003160 */
Chris Wilson91c8a322016-07-05 10:40:23 +01003161 intel_update_primary_planes(&dev_priv->drm);
Ville Syrjälä75147472014-11-24 18:28:11 +02003162 return;
3163 }
3164
3165 /*
3166 * The display has been reset as well,
3167 * so need a full re-initialization.
3168 */
3169 intel_runtime_pm_disable_interrupts(dev_priv);
3170 intel_runtime_pm_enable_interrupts(dev_priv);
3171
Chris Wilson91c8a322016-07-05 10:40:23 +01003172 intel_modeset_init_hw(&dev_priv->drm);
Ville Syrjälä75147472014-11-24 18:28:11 +02003173
3174 spin_lock_irq(&dev_priv->irq_lock);
3175 if (dev_priv->display.hpd_irq_setup)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01003176 dev_priv->display.hpd_irq_setup(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003177 spin_unlock_irq(&dev_priv->irq_lock);
3178
Chris Wilson91c8a322016-07-05 10:40:23 +01003179 intel_display_resume(&dev_priv->drm);
Ville Syrjälä75147472014-11-24 18:28:11 +02003180
3181 intel_hpd_init(dev_priv);
3182
Chris Wilson91c8a322016-07-05 10:40:23 +01003183 drm_modeset_unlock_all(&dev_priv->drm);
Ville Syrjälä75147472014-11-24 18:28:11 +02003184}
3185
Chris Wilson7d5e3792014-03-04 13:15:08 +00003186static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3187{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003188 struct drm_device *dev = crtc->dev;
3189 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3190 unsigned reset_counter;
3191 bool pending;
3192
3193 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3194 if (intel_crtc->reset_counter != reset_counter)
3195 return false;
3196
3197 spin_lock_irq(&dev->event_lock);
3198 pending = to_intel_crtc(crtc)->flip_work != NULL;
3199 spin_unlock_irq(&dev->event_lock);
3200
3201 return pending;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003202}
3203
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003204static void intel_update_pipe_config(struct intel_crtc *crtc,
3205 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003206{
3207 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003208 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003209 struct intel_crtc_state *pipe_config =
3210 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003211
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003212 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3213 crtc->base.mode = crtc->base.state->mode;
3214
3215 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3216 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3217 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003218
3219 /*
3220 * Update pipe size and adjust fitter if needed: the reason for this is
3221 * that in compute_mode_changes we check the native mode (not the pfit
3222 * mode) to see if we can flip rather than do a full mode set. In the
3223 * fastboot case, we'll flip, but if we don't update the pipesrc and
3224 * pfit state, we'll end up with a big fb scanned out into the wrong
3225 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003226 */
3227
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003228 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003229 ((pipe_config->pipe_src_w - 1) << 16) |
3230 (pipe_config->pipe_src_h - 1));
3231
3232 /* on skylake this is done by detaching scalers */
3233 if (INTEL_INFO(dev)->gen >= 9) {
3234 skl_detach_scalers(crtc);
3235
3236 if (pipe_config->pch_pfit.enabled)
3237 skylake_pfit_enable(crtc);
3238 } else if (HAS_PCH_SPLIT(dev)) {
3239 if (pipe_config->pch_pfit.enabled)
3240 ironlake_pfit_enable(crtc);
3241 else if (old_crtc_state->pch_pfit.enabled)
3242 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003243 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003244}
3245
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003246static void intel_fdi_normal_train(struct drm_crtc *crtc)
3247{
3248 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003249 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003250 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3251 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003252 i915_reg_t reg;
3253 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003254
3255 /* enable normal train */
3256 reg = FDI_TX_CTL(pipe);
3257 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003258 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003259 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3260 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003261 } else {
3262 temp &= ~FDI_LINK_TRAIN_NONE;
3263 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003264 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003265 I915_WRITE(reg, temp);
3266
3267 reg = FDI_RX_CTL(pipe);
3268 temp = I915_READ(reg);
3269 if (HAS_PCH_CPT(dev)) {
3270 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3271 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3272 } else {
3273 temp &= ~FDI_LINK_TRAIN_NONE;
3274 temp |= FDI_LINK_TRAIN_NONE;
3275 }
3276 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3277
3278 /* wait one idle pattern time */
3279 POSTING_READ(reg);
3280 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003281
3282 /* IVB wants error correction enabled */
3283 if (IS_IVYBRIDGE(dev))
3284 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3285 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003286}
3287
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003288/* The FDI link training functions for ILK/Ibexpeak. */
3289static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3290{
3291 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003292 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003293 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3294 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003295 i915_reg_t reg;
3296 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003297
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003298 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003299 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003300
Adam Jacksone1a44742010-06-25 15:32:14 -04003301 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3302 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003303 reg = FDI_RX_IMR(pipe);
3304 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003305 temp &= ~FDI_RX_SYMBOL_LOCK;
3306 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003307 I915_WRITE(reg, temp);
3308 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003309 udelay(150);
3310
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003311 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003312 reg = FDI_TX_CTL(pipe);
3313 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003314 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003315 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003316 temp &= ~FDI_LINK_TRAIN_NONE;
3317 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003318 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003319
Chris Wilson5eddb702010-09-11 13:48:45 +01003320 reg = FDI_RX_CTL(pipe);
3321 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003322 temp &= ~FDI_LINK_TRAIN_NONE;
3323 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003324 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3325
3326 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003327 udelay(150);
3328
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003329 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003330 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3331 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3332 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003333
Chris Wilson5eddb702010-09-11 13:48:45 +01003334 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003335 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003336 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003337 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3338
3339 if ((temp & FDI_RX_BIT_LOCK)) {
3340 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003341 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003342 break;
3343 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003344 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003345 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003346 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003347
3348 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003349 reg = FDI_TX_CTL(pipe);
3350 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003351 temp &= ~FDI_LINK_TRAIN_NONE;
3352 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003353 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003354
Chris Wilson5eddb702010-09-11 13:48:45 +01003355 reg = FDI_RX_CTL(pipe);
3356 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003357 temp &= ~FDI_LINK_TRAIN_NONE;
3358 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003359 I915_WRITE(reg, temp);
3360
3361 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003362 udelay(150);
3363
Chris Wilson5eddb702010-09-11 13:48:45 +01003364 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003365 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003366 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003367 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3368
3369 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003370 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003371 DRM_DEBUG_KMS("FDI train 2 done.\n");
3372 break;
3373 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003374 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003375 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003376 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003377
3378 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003379
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003380}
3381
Akshay Joshi0206e352011-08-16 15:34:10 -04003382static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003383 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3384 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3385 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3386 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3387};
3388
3389/* The FDI link training functions for SNB/Cougarpoint. */
3390static void gen6_fdi_link_train(struct drm_crtc *crtc)
3391{
3392 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003393 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003394 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3395 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003396 i915_reg_t reg;
3397 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003398
Adam Jacksone1a44742010-06-25 15:32:14 -04003399 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3400 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003401 reg = FDI_RX_IMR(pipe);
3402 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003403 temp &= ~FDI_RX_SYMBOL_LOCK;
3404 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003405 I915_WRITE(reg, temp);
3406
3407 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003408 udelay(150);
3409
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003410 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003411 reg = FDI_TX_CTL(pipe);
3412 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003413 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003414 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003415 temp &= ~FDI_LINK_TRAIN_NONE;
3416 temp |= FDI_LINK_TRAIN_PATTERN_1;
3417 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3418 /* SNB-B */
3419 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003420 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003421
Daniel Vetterd74cf322012-10-26 10:58:13 +02003422 I915_WRITE(FDI_RX_MISC(pipe),
3423 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3424
Chris Wilson5eddb702010-09-11 13:48:45 +01003425 reg = FDI_RX_CTL(pipe);
3426 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003427 if (HAS_PCH_CPT(dev)) {
3428 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3429 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3430 } else {
3431 temp &= ~FDI_LINK_TRAIN_NONE;
3432 temp |= FDI_LINK_TRAIN_PATTERN_1;
3433 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003434 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3435
3436 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003437 udelay(150);
3438
Akshay Joshi0206e352011-08-16 15:34:10 -04003439 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003440 reg = FDI_TX_CTL(pipe);
3441 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003442 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3443 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003444 I915_WRITE(reg, temp);
3445
3446 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003447 udelay(500);
3448
Sean Paulfa37d392012-03-02 12:53:39 -05003449 for (retry = 0; retry < 5; retry++) {
3450 reg = FDI_RX_IIR(pipe);
3451 temp = I915_READ(reg);
3452 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3453 if (temp & FDI_RX_BIT_LOCK) {
3454 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3455 DRM_DEBUG_KMS("FDI train 1 done.\n");
3456 break;
3457 }
3458 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003459 }
Sean Paulfa37d392012-03-02 12:53:39 -05003460 if (retry < 5)
3461 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003462 }
3463 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003464 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003465
3466 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003467 reg = FDI_TX_CTL(pipe);
3468 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003469 temp &= ~FDI_LINK_TRAIN_NONE;
3470 temp |= FDI_LINK_TRAIN_PATTERN_2;
3471 if (IS_GEN6(dev)) {
3472 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3473 /* SNB-B */
3474 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3475 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003476 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003477
Chris Wilson5eddb702010-09-11 13:48:45 +01003478 reg = FDI_RX_CTL(pipe);
3479 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003480 if (HAS_PCH_CPT(dev)) {
3481 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3482 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3483 } else {
3484 temp &= ~FDI_LINK_TRAIN_NONE;
3485 temp |= FDI_LINK_TRAIN_PATTERN_2;
3486 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003487 I915_WRITE(reg, temp);
3488
3489 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003490 udelay(150);
3491
Akshay Joshi0206e352011-08-16 15:34:10 -04003492 for (i = 0; i < 4; i++) {
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_VOL_EMP_MASK;
3496 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003497 I915_WRITE(reg, temp);
3498
3499 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003500 udelay(500);
3501
Sean Paulfa37d392012-03-02 12:53:39 -05003502 for (retry = 0; retry < 5; retry++) {
3503 reg = FDI_RX_IIR(pipe);
3504 temp = I915_READ(reg);
3505 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3506 if (temp & FDI_RX_SYMBOL_LOCK) {
3507 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3508 DRM_DEBUG_KMS("FDI train 2 done.\n");
3509 break;
3510 }
3511 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003512 }
Sean Paulfa37d392012-03-02 12:53:39 -05003513 if (retry < 5)
3514 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003515 }
3516 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003517 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003518
3519 DRM_DEBUG_KMS("FDI train done.\n");
3520}
3521
Jesse Barnes357555c2011-04-28 15:09:55 -07003522/* Manual link training for Ivy Bridge A0 parts */
3523static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3524{
3525 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003526 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes357555c2011-04-28 15:09:55 -07003527 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3528 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003529 i915_reg_t reg;
3530 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003531
3532 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3533 for train result */
3534 reg = FDI_RX_IMR(pipe);
3535 temp = I915_READ(reg);
3536 temp &= ~FDI_RX_SYMBOL_LOCK;
3537 temp &= ~FDI_RX_BIT_LOCK;
3538 I915_WRITE(reg, temp);
3539
3540 POSTING_READ(reg);
3541 udelay(150);
3542
Daniel Vetter01a415f2012-10-27 15:58:40 +02003543 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3544 I915_READ(FDI_RX_IIR(pipe)));
3545
Jesse Barnes139ccd32013-08-19 11:04:55 -07003546 /* Try each vswing and preemphasis setting twice before moving on */
3547 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3548 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003549 reg = FDI_TX_CTL(pipe);
3550 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003551 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3552 temp &= ~FDI_TX_ENABLE;
3553 I915_WRITE(reg, temp);
3554
3555 reg = FDI_RX_CTL(pipe);
3556 temp = I915_READ(reg);
3557 temp &= ~FDI_LINK_TRAIN_AUTO;
3558 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3559 temp &= ~FDI_RX_ENABLE;
3560 I915_WRITE(reg, temp);
3561
3562 /* enable CPU FDI TX and PCH FDI RX */
3563 reg = FDI_TX_CTL(pipe);
3564 temp = I915_READ(reg);
3565 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003566 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003567 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003568 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003569 temp |= snb_b_fdi_train_param[j/2];
3570 temp |= FDI_COMPOSITE_SYNC;
3571 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3572
3573 I915_WRITE(FDI_RX_MISC(pipe),
3574 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3575
3576 reg = FDI_RX_CTL(pipe);
3577 temp = I915_READ(reg);
3578 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3579 temp |= FDI_COMPOSITE_SYNC;
3580 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3581
3582 POSTING_READ(reg);
3583 udelay(1); /* should be 0.5us */
3584
3585 for (i = 0; i < 4; i++) {
3586 reg = FDI_RX_IIR(pipe);
3587 temp = I915_READ(reg);
3588 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3589
3590 if (temp & FDI_RX_BIT_LOCK ||
3591 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3592 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3593 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3594 i);
3595 break;
3596 }
3597 udelay(1); /* should be 0.5us */
3598 }
3599 if (i == 4) {
3600 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3601 continue;
3602 }
3603
3604 /* Train 2 */
3605 reg = FDI_TX_CTL(pipe);
3606 temp = I915_READ(reg);
3607 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3608 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3609 I915_WRITE(reg, temp);
3610
3611 reg = FDI_RX_CTL(pipe);
3612 temp = I915_READ(reg);
3613 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3614 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003615 I915_WRITE(reg, temp);
3616
3617 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003618 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003619
Jesse Barnes139ccd32013-08-19 11:04:55 -07003620 for (i = 0; i < 4; i++) {
3621 reg = FDI_RX_IIR(pipe);
3622 temp = I915_READ(reg);
3623 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003624
Jesse Barnes139ccd32013-08-19 11:04:55 -07003625 if (temp & FDI_RX_SYMBOL_LOCK ||
3626 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3627 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3628 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3629 i);
3630 goto train_done;
3631 }
3632 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003633 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003634 if (i == 4)
3635 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003636 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003637
Jesse Barnes139ccd32013-08-19 11:04:55 -07003638train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003639 DRM_DEBUG_KMS("FDI train done.\n");
3640}
3641
Daniel Vetter88cefb62012-08-12 19:27:14 +02003642static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003643{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003644 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003645 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003646 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003647 i915_reg_t reg;
3648 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003649
Jesse Barnes0e23b992010-09-10 11:10:00 -07003650 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003651 reg = FDI_RX_CTL(pipe);
3652 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003653 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003654 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003655 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003656 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3657
3658 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003659 udelay(200);
3660
3661 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003662 temp = I915_READ(reg);
3663 I915_WRITE(reg, temp | FDI_PCDCLK);
3664
3665 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003666 udelay(200);
3667
Paulo Zanoni20749732012-11-23 15:30:38 -02003668 /* Enable CPU FDI TX PLL, always on for Ironlake */
3669 reg = FDI_TX_CTL(pipe);
3670 temp = I915_READ(reg);
3671 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3672 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003673
Paulo Zanoni20749732012-11-23 15:30:38 -02003674 POSTING_READ(reg);
3675 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003676 }
3677}
3678
Daniel Vetter88cefb62012-08-12 19:27:14 +02003679static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3680{
3681 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003682 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter88cefb62012-08-12 19:27:14 +02003683 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003684 i915_reg_t reg;
3685 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003686
3687 /* Switch from PCDclk to Rawclk */
3688 reg = FDI_RX_CTL(pipe);
3689 temp = I915_READ(reg);
3690 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3691
3692 /* Disable CPU FDI TX PLL */
3693 reg = FDI_TX_CTL(pipe);
3694 temp = I915_READ(reg);
3695 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3696
3697 POSTING_READ(reg);
3698 udelay(100);
3699
3700 reg = FDI_RX_CTL(pipe);
3701 temp = I915_READ(reg);
3702 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3703
3704 /* Wait for the clocks to turn off. */
3705 POSTING_READ(reg);
3706 udelay(100);
3707}
3708
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003709static void ironlake_fdi_disable(struct drm_crtc *crtc)
3710{
3711 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003712 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003713 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3714 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003715 i915_reg_t reg;
3716 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003717
3718 /* disable CPU FDI tx and PCH FDI rx */
3719 reg = FDI_TX_CTL(pipe);
3720 temp = I915_READ(reg);
3721 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3722 POSTING_READ(reg);
3723
3724 reg = FDI_RX_CTL(pipe);
3725 temp = I915_READ(reg);
3726 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003727 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003728 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3729
3730 POSTING_READ(reg);
3731 udelay(100);
3732
3733 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003734 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003735 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003736
3737 /* still set train pattern 1 */
3738 reg = FDI_TX_CTL(pipe);
3739 temp = I915_READ(reg);
3740 temp &= ~FDI_LINK_TRAIN_NONE;
3741 temp |= FDI_LINK_TRAIN_PATTERN_1;
3742 I915_WRITE(reg, temp);
3743
3744 reg = FDI_RX_CTL(pipe);
3745 temp = I915_READ(reg);
3746 if (HAS_PCH_CPT(dev)) {
3747 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3748 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3749 } else {
3750 temp &= ~FDI_LINK_TRAIN_NONE;
3751 temp |= FDI_LINK_TRAIN_PATTERN_1;
3752 }
3753 /* BPC in FDI rx is consistent with that in PIPECONF */
3754 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003755 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003756 I915_WRITE(reg, temp);
3757
3758 POSTING_READ(reg);
3759 udelay(100);
3760}
3761
Chris Wilson5dce5b932014-01-20 10:17:36 +00003762bool intel_has_pending_fb_unpin(struct drm_device *dev)
3763{
3764 struct intel_crtc *crtc;
3765
3766 /* Note that we don't need to be called with mode_config.lock here
3767 * as our list of CRTC objects is static for the lifetime of the
3768 * device and so cannot disappear as we iterate. Similarly, we can
3769 * happily treat the predicates as racy, atomic checks as userspace
3770 * cannot claim and pin a new fb without at least acquring the
3771 * struct_mutex and so serialising with us.
3772 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003773 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003774 if (atomic_read(&crtc->unpin_work_count) == 0)
3775 continue;
3776
Daniel Vetter5a21b662016-05-24 17:13:53 +02003777 if (crtc->flip_work)
Chris Wilson5dce5b932014-01-20 10:17:36 +00003778 intel_wait_for_vblank(dev, crtc->pipe);
3779
3780 return true;
3781 }
3782
3783 return false;
3784}
3785
Daniel Vetter5a21b662016-05-24 17:13:53 +02003786static void page_flip_completed(struct intel_crtc *intel_crtc)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003787{
3788 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003789 struct intel_flip_work *work = intel_crtc->flip_work;
3790
3791 intel_crtc->flip_work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003792
3793 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07003794 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003795
3796 drm_crtc_vblank_put(&intel_crtc->base);
3797
Daniel Vetter5a21b662016-05-24 17:13:53 +02003798 wake_up_all(&dev_priv->pending_flip_queue);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +02003799 queue_work(dev_priv->wq, &work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003800
3801 trace_i915_flip_complete(intel_crtc->plane,
3802 work->pending_flip_obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003803}
3804
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003805static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003806{
Chris Wilson0f911282012-04-17 10:05:38 +01003807 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003808 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003809 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003810
Daniel Vetter2c10d572012-12-20 21:24:07 +01003811 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003812
3813 ret = wait_event_interruptible_timeout(
3814 dev_priv->pending_flip_queue,
3815 !intel_crtc_has_pending_flip(crtc),
3816 60*HZ);
3817
3818 if (ret < 0)
3819 return ret;
3820
Daniel Vetter5a21b662016-05-24 17:13:53 +02003821 if (ret == 0) {
3822 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3823 struct intel_flip_work *work;
3824
3825 spin_lock_irq(&dev->event_lock);
3826 work = intel_crtc->flip_work;
3827 if (work && !is_mmio_work(work)) {
3828 WARN_ONCE(1, "Removing stuck page flip\n");
3829 page_flip_completed(intel_crtc);
3830 }
3831 spin_unlock_irq(&dev->event_lock);
3832 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003833
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003834 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003835}
3836
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003837static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3838{
3839 u32 temp;
3840
3841 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3842
3843 mutex_lock(&dev_priv->sb_lock);
3844
3845 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3846 temp |= SBI_SSCCTL_DISABLE;
3847 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3848
3849 mutex_unlock(&dev_priv->sb_lock);
3850}
3851
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003852/* Program iCLKIP clock to the desired frequency */
3853static void lpt_program_iclkip(struct drm_crtc *crtc)
3854{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003855 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003856 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003857 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3858 u32 temp;
3859
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003860 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003861
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003862 /* The iCLK virtual clock root frequency is in MHz,
3863 * but the adjusted_mode->crtc_clock in in KHz. To get the
3864 * divisors, it is necessary to divide one by another, so we
3865 * convert the virtual clock precision to KHz here for higher
3866 * precision.
3867 */
3868 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003869 u32 iclk_virtual_root_freq = 172800 * 1000;
3870 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003871 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003872
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003873 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3874 clock << auxdiv);
3875 divsel = (desired_divisor / iclk_pi_range) - 2;
3876 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003877
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003878 /*
3879 * Near 20MHz is a corner case which is
3880 * out of range for the 7-bit divisor
3881 */
3882 if (divsel <= 0x7f)
3883 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003884 }
3885
3886 /* This should not happen with any sane values */
3887 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3888 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3889 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3890 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3891
3892 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 +03003893 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003894 auxdiv,
3895 divsel,
3896 phasedir,
3897 phaseinc);
3898
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003899 mutex_lock(&dev_priv->sb_lock);
3900
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003901 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003902 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003903 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3904 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3905 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3906 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3907 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3908 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003909 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003910
3911 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003912 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003913 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3914 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003915 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003916
3917 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003918 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003919 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003920 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003921
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003922 mutex_unlock(&dev_priv->sb_lock);
3923
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003924 /* Wait for initialization time */
3925 udelay(24);
3926
3927 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3928}
3929
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003930int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3931{
3932 u32 divsel, phaseinc, auxdiv;
3933 u32 iclk_virtual_root_freq = 172800 * 1000;
3934 u32 iclk_pi_range = 64;
3935 u32 desired_divisor;
3936 u32 temp;
3937
3938 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3939 return 0;
3940
3941 mutex_lock(&dev_priv->sb_lock);
3942
3943 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3944 if (temp & SBI_SSCCTL_DISABLE) {
3945 mutex_unlock(&dev_priv->sb_lock);
3946 return 0;
3947 }
3948
3949 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3950 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3951 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3952 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3953 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3954
3955 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3956 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3957 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3958
3959 mutex_unlock(&dev_priv->sb_lock);
3960
3961 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3962
3963 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3964 desired_divisor << auxdiv);
3965}
3966
Daniel Vetter275f01b22013-05-03 11:49:47 +02003967static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3968 enum pipe pch_transcoder)
3969{
3970 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003971 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003972 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003973
3974 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3975 I915_READ(HTOTAL(cpu_transcoder)));
3976 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3977 I915_READ(HBLANK(cpu_transcoder)));
3978 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3979 I915_READ(HSYNC(cpu_transcoder)));
3980
3981 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3982 I915_READ(VTOTAL(cpu_transcoder)));
3983 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3984 I915_READ(VBLANK(cpu_transcoder)));
3985 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3986 I915_READ(VSYNC(cpu_transcoder)));
3987 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3988 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3989}
3990
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003991static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003992{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003993 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003994 uint32_t temp;
3995
3996 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003997 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003998 return;
3999
4000 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4001 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4002
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004003 temp &= ~FDI_BC_BIFURCATION_SELECT;
4004 if (enable)
4005 temp |= FDI_BC_BIFURCATION_SELECT;
4006
4007 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004008 I915_WRITE(SOUTH_CHICKEN1, temp);
4009 POSTING_READ(SOUTH_CHICKEN1);
4010}
4011
4012static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4013{
4014 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004015
4016 switch (intel_crtc->pipe) {
4017 case PIPE_A:
4018 break;
4019 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004020 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004021 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004022 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004023 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004024
4025 break;
4026 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004027 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004028
4029 break;
4030 default:
4031 BUG();
4032 }
4033}
4034
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004035/* Return which DP Port should be selected for Transcoder DP control */
4036static enum port
4037intel_trans_dp_port_sel(struct drm_crtc *crtc)
4038{
4039 struct drm_device *dev = crtc->dev;
4040 struct intel_encoder *encoder;
4041
4042 for_each_encoder_on_crtc(dev, crtc, encoder) {
Ville Syrjäläcca05022016-06-22 21:57:06 +03004043 if (encoder->type == INTEL_OUTPUT_DP ||
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004044 encoder->type == INTEL_OUTPUT_EDP)
4045 return enc_to_dig_port(&encoder->base)->port;
4046 }
4047
4048 return -1;
4049}
4050
Jesse Barnesf67a5592011-01-05 10:31:48 -08004051/*
4052 * Enable PCH resources required for PCH ports:
4053 * - PCH PLLs
4054 * - FDI training & RX/TX
4055 * - update transcoder timings
4056 * - DP transcoding bits
4057 * - transcoder
4058 */
4059static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004060{
4061 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004062 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004063 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4064 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004065 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004066
Daniel Vetterab9412b2013-05-03 11:49:46 +02004067 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004068
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004069 if (IS_IVYBRIDGE(dev))
4070 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4071
Daniel Vettercd986ab2012-10-26 10:58:12 +02004072 /* Write the TU size bits before fdi link training, so that error
4073 * detection works. */
4074 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4075 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4076
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004077 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004078 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004079
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004080 /* We need to program the right clock selection before writing the pixel
4081 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004082 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004083 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004084
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004085 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004086 temp |= TRANS_DPLL_ENABLE(pipe);
4087 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004088 if (intel_crtc->config->shared_dpll ==
4089 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004090 temp |= sel;
4091 else
4092 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004093 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004094 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004095
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004096 /* XXX: pch pll's can be enabled any time before we enable the PCH
4097 * transcoder, and we actually should do this to not upset any PCH
4098 * transcoder that already use the clock when we share it.
4099 *
4100 * Note that enable_shared_dpll tries to do the right thing, but
4101 * get_shared_dpll unconditionally resets the pll - we need that to have
4102 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004103 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004104
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004105 /* set transcoder timing, panel must allow it */
4106 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004107 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004108
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004109 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004110
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004111 /* For PCH DP, enable TRANS_DP_CTL */
Ville Syrjälä37a56502016-06-22 21:57:04 +03004112 if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004113 const struct drm_display_mode *adjusted_mode =
4114 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004115 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004116 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004117 temp = I915_READ(reg);
4118 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004119 TRANS_DP_SYNC_MASK |
4120 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004121 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004122 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004123
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004124 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004125 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004126 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004127 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004128
4129 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004130 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004131 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004132 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004133 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004134 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004135 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004136 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004137 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004138 break;
4139 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004140 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004141 }
4142
Chris Wilson5eddb702010-09-11 13:48:45 +01004143 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004144 }
4145
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004146 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004147}
4148
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004149static void lpt_pch_enable(struct drm_crtc *crtc)
4150{
4151 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004152 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004154 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004155
Daniel Vetterab9412b2013-05-03 11:49:46 +02004156 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004157
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004158 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004159
Paulo Zanoni0540e482012-10-31 18:12:40 -02004160 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004161 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004162
Paulo Zanoni937bb612012-10-31 18:12:47 -02004163 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004164}
4165
Daniel Vettera1520312013-05-03 11:49:50 +02004166static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004167{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004168 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004169 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004170 u32 temp;
4171
4172 temp = I915_READ(dslreg);
4173 udelay(500);
4174 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004175 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004176 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004177 }
4178}
4179
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004180static int
4181skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4182 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4183 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004184{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004185 struct intel_crtc_scaler_state *scaler_state =
4186 &crtc_state->scaler_state;
4187 struct intel_crtc *intel_crtc =
4188 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004189 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004190
4191 need_scaling = intel_rotation_90_or_270(rotation) ?
4192 (src_h != dst_w || src_w != dst_h):
4193 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004194
4195 /*
4196 * if plane is being disabled or scaler is no more required or force detach
4197 * - free scaler binded to this plane/crtc
4198 * - in order to do this, update crtc->scaler_usage
4199 *
4200 * Here scaler state in crtc_state is set free so that
4201 * scaler can be assigned to other user. Actual register
4202 * update to free the scaler is done in plane/panel-fit programming.
4203 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4204 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004205 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004206 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004207 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004208 scaler_state->scalers[*scaler_id].in_use = 0;
4209
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004210 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4211 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4212 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004213 scaler_state->scaler_users);
4214 *scaler_id = -1;
4215 }
4216 return 0;
4217 }
4218
4219 /* range checks */
4220 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4221 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4222
4223 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4224 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004225 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004226 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004227 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004228 return -EINVAL;
4229 }
4230
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004231 /* mark this plane as a scaler user in crtc_state */
4232 scaler_state->scaler_users |= (1 << scaler_user);
4233 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4234 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4235 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4236 scaler_state->scaler_users);
4237
4238 return 0;
4239}
4240
4241/**
4242 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4243 *
4244 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004245 *
4246 * Return
4247 * 0 - scaler_usage updated successfully
4248 * error - requested scaling cannot be supported or other error condition
4249 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004250int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004251{
4252 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004253 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004254
Ville Syrjälä78108b72016-05-27 20:59:19 +03004255 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4256 intel_crtc->base.base.id, intel_crtc->base.name,
4257 intel_crtc->pipe, SKL_CRTC_INDEX);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004258
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004259 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004260 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004261 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004262 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004263}
4264
4265/**
4266 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4267 *
4268 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004269 * @plane_state: atomic plane state to update
4270 *
4271 * Return
4272 * 0 - scaler_usage updated successfully
4273 * error - requested scaling cannot be supported or other error condition
4274 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004275static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4276 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004277{
4278
4279 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004280 struct intel_plane *intel_plane =
4281 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004282 struct drm_framebuffer *fb = plane_state->base.fb;
4283 int ret;
4284
4285 bool force_detach = !fb || !plane_state->visible;
4286
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004287 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4288 intel_plane->base.base.id, intel_plane->base.name,
4289 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004290
4291 ret = skl_update_scaler(crtc_state, force_detach,
4292 drm_plane_index(&intel_plane->base),
4293 &plane_state->scaler_id,
4294 plane_state->base.rotation,
4295 drm_rect_width(&plane_state->src) >> 16,
4296 drm_rect_height(&plane_state->src) >> 16,
4297 drm_rect_width(&plane_state->dst),
4298 drm_rect_height(&plane_state->dst));
4299
4300 if (ret || plane_state->scaler_id < 0)
4301 return ret;
4302
Chandra Kondurua1b22782015-04-07 15:28:45 -07004303 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004304 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004305 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4306 intel_plane->base.base.id,
4307 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004308 return -EINVAL;
4309 }
4310
4311 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004312 switch (fb->pixel_format) {
4313 case DRM_FORMAT_RGB565:
4314 case DRM_FORMAT_XBGR8888:
4315 case DRM_FORMAT_XRGB8888:
4316 case DRM_FORMAT_ABGR8888:
4317 case DRM_FORMAT_ARGB8888:
4318 case DRM_FORMAT_XRGB2101010:
4319 case DRM_FORMAT_XBGR2101010:
4320 case DRM_FORMAT_YUYV:
4321 case DRM_FORMAT_YVYU:
4322 case DRM_FORMAT_UYVY:
4323 case DRM_FORMAT_VYUY:
4324 break;
4325 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004326 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4327 intel_plane->base.base.id, intel_plane->base.name,
4328 fb->base.id, fb->pixel_format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004329 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004330 }
4331
Chandra Kondurua1b22782015-04-07 15:28:45 -07004332 return 0;
4333}
4334
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004335static void skylake_scaler_disable(struct intel_crtc *crtc)
4336{
4337 int i;
4338
4339 for (i = 0; i < crtc->num_scalers; i++)
4340 skl_detach_scaler(crtc, i);
4341}
4342
4343static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004344{
4345 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004346 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004347 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004348 struct intel_crtc_scaler_state *scaler_state =
4349 &crtc->config->scaler_state;
4350
4351 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4352
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004353 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004354 int id;
4355
4356 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4357 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4358 return;
4359 }
4360
4361 id = scaler_state->scaler_id;
4362 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4363 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4364 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4365 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4366
4367 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004368 }
4369}
4370
Jesse Barnesb074cec2013-04-25 12:55:02 -07004371static void ironlake_pfit_enable(struct intel_crtc *crtc)
4372{
4373 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004374 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb074cec2013-04-25 12:55:02 -07004375 int pipe = crtc->pipe;
4376
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004377 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004378 /* Force use of hard-coded filter coefficients
4379 * as some pre-programmed values are broken,
4380 * e.g. x201.
4381 */
4382 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4383 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4384 PF_PIPE_SEL_IVB(pipe));
4385 else
4386 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004387 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4388 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004389 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004390}
4391
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004392void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004393{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004394 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004395 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004396
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004397 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004398 return;
4399
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004400 /*
4401 * We can only enable IPS after we enable a plane and wait for a vblank
4402 * This function is called from post_plane_update, which is run after
4403 * a vblank wait.
4404 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004405
Paulo Zanonid77e4532013-09-24 13:52:55 -03004406 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004407 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004408 mutex_lock(&dev_priv->rps.hw_lock);
4409 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4410 mutex_unlock(&dev_priv->rps.hw_lock);
4411 /* Quoting Art Runyan: "its not safe to expect any particular
4412 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004413 * mailbox." Moreover, the mailbox may return a bogus state,
4414 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004415 */
4416 } else {
4417 I915_WRITE(IPS_CTL, IPS_ENABLE);
4418 /* The bit only becomes 1 in the next vblank, so this wait here
4419 * is essentially intel_wait_for_vblank. If we don't have this
4420 * and don't wait for vblanks until the end of crtc_enable, then
4421 * the HW state readout code will complain that the expected
4422 * IPS_CTL value is not the one we read. */
Chris Wilson2ec9ba32016-06-30 15:33:01 +01004423 if (intel_wait_for_register(dev_priv,
4424 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4425 50))
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004426 DRM_ERROR("Timed out waiting for IPS enable\n");
4427 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004428}
4429
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004430void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004431{
4432 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004433 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004434
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004435 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004436 return;
4437
4438 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004439 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004440 mutex_lock(&dev_priv->rps.hw_lock);
4441 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4442 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004443 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
Chris Wilsonb85c1ec2016-06-30 15:33:02 +01004444 if (intel_wait_for_register(dev_priv,
4445 IPS_CTL, IPS_ENABLE, 0,
4446 42))
Ben Widawsky23d0b132014-04-10 14:32:41 -07004447 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004448 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004449 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004450 POSTING_READ(IPS_CTL);
4451 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004452
4453 /* We need to wait for a vblank before we can disable the plane. */
4454 intel_wait_for_vblank(dev, crtc->pipe);
4455}
4456
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004457static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004458{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004459 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004460 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004461 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004462
4463 mutex_lock(&dev->struct_mutex);
4464 dev_priv->mm.interruptible = false;
4465 (void) intel_overlay_switch_off(intel_crtc->overlay);
4466 dev_priv->mm.interruptible = true;
4467 mutex_unlock(&dev->struct_mutex);
4468 }
4469
4470 /* Let userspace switch the overlay on again. In most cases userspace
4471 * has to recompute where to put it anyway.
4472 */
4473}
4474
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004475/**
4476 * intel_post_enable_primary - Perform operations after enabling primary plane
4477 * @crtc: the CRTC whose primary plane was just enabled
4478 *
4479 * Performs potentially sleeping operations that must be done after the primary
4480 * plane is enabled, such as updating FBC and IPS. Note that this may be
4481 * called due to an explicit primary plane update, or due to an implicit
4482 * re-enable that is caused when a sprite plane is updated to no longer
4483 * completely hide the primary plane.
4484 */
4485static void
4486intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004487{
4488 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004489 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004490 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4491 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004492
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004493 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004494 * FIXME IPS should be fine as long as one plane is
4495 * enabled, but in practice it seems to have problems
4496 * when going from primary only to sprite only and vice
4497 * versa.
4498 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004499 hsw_enable_ips(intel_crtc);
4500
Daniel Vetterf99d7062014-06-19 16:01:59 +02004501 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004502 * Gen2 reports pipe underruns whenever all planes are disabled.
4503 * So don't enable underrun reporting before at least some planes
4504 * are enabled.
4505 * FIXME: Need to fix the logic to work when we turn off all planes
4506 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004507 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004508 if (IS_GEN2(dev))
4509 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4510
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004511 /* Underruns don't always raise interrupts, so check manually. */
4512 intel_check_cpu_fifo_underruns(dev_priv);
4513 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004514}
4515
Ville Syrjälä2622a082016-03-09 19:07:26 +02004516/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004517static void
4518intel_pre_disable_primary(struct drm_crtc *crtc)
4519{
4520 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004521 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004522 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4523 int pipe = intel_crtc->pipe;
4524
4525 /*
4526 * Gen2 reports pipe underruns whenever all planes are disabled.
4527 * So diasble underrun reporting before all the planes get disabled.
4528 * FIXME: Need to fix the logic to work when we turn off all planes
4529 * but leave the pipe running.
4530 */
4531 if (IS_GEN2(dev))
4532 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4533
4534 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004535 * FIXME IPS should be fine as long as one plane is
4536 * enabled, but in practice it seems to have problems
4537 * when going from primary only to sprite only and vice
4538 * versa.
4539 */
4540 hsw_disable_ips(intel_crtc);
4541}
4542
4543/* FIXME get rid of this and use pre_plane_update */
4544static void
4545intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4546{
4547 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004548 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004549 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4550 int pipe = intel_crtc->pipe;
4551
4552 intel_pre_disable_primary(crtc);
4553
4554 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004555 * Vblank time updates from the shadow to live plane control register
4556 * are blocked if the memory self-refresh mode is active at that
4557 * moment. So to make sure the plane gets truly disabled, disable
4558 * first the self-refresh mode. The self-refresh enable bit in turn
4559 * will be checked/applied by the HW only at the next frame start
4560 * event which is after the vblank start event, so we need to have a
4561 * wait-for-vblank between disabling the plane and the pipe.
4562 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004563 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004564 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004565 dev_priv->wm.vlv.cxsr = false;
4566 intel_wait_for_vblank(dev, pipe);
4567 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004568}
4569
Daniel Vetter5a21b662016-05-24 17:13:53 +02004570static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4571{
4572 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4573 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4574 struct intel_crtc_state *pipe_config =
4575 to_intel_crtc_state(crtc->base.state);
4576 struct drm_device *dev = crtc->base.dev;
4577 struct drm_plane *primary = crtc->base.primary;
4578 struct drm_plane_state *old_pri_state =
4579 drm_atomic_get_existing_plane_state(old_state, primary);
4580
4581 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
4582
4583 crtc->wm.cxsr_allowed = true;
4584
4585 if (pipe_config->update_wm_post && pipe_config->base.active)
4586 intel_update_watermarks(&crtc->base);
4587
4588 if (old_pri_state) {
4589 struct intel_plane_state *primary_state =
4590 to_intel_plane_state(primary->state);
4591 struct intel_plane_state *old_primary_state =
4592 to_intel_plane_state(old_pri_state);
4593
4594 intel_fbc_post_update(crtc);
4595
4596 if (primary_state->visible &&
4597 (needs_modeset(&pipe_config->base) ||
4598 !old_primary_state->visible))
4599 intel_post_enable_primary(&crtc->base);
4600 }
4601}
4602
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004603static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004604{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004605 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004606 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004607 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004608 struct intel_crtc_state *pipe_config =
4609 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004610 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4611 struct drm_plane *primary = crtc->base.primary;
4612 struct drm_plane_state *old_pri_state =
4613 drm_atomic_get_existing_plane_state(old_state, primary);
4614 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004615
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004616 if (old_pri_state) {
4617 struct intel_plane_state *primary_state =
4618 to_intel_plane_state(primary->state);
4619 struct intel_plane_state *old_primary_state =
4620 to_intel_plane_state(old_pri_state);
4621
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +02004622 intel_fbc_pre_update(crtc, pipe_config, primary_state);
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004623
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004624 if (old_primary_state->visible &&
4625 (modeset || !primary_state->visible))
4626 intel_pre_disable_primary(&crtc->base);
4627 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004628
David Weinehalla4015f92016-05-19 15:50:36 +03004629 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004630 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004631
Ville Syrjälä2622a082016-03-09 19:07:26 +02004632 /*
4633 * Vblank time updates from the shadow to live plane control register
4634 * are blocked if the memory self-refresh mode is active at that
4635 * moment. So to make sure the plane gets truly disabled, disable
4636 * first the self-refresh mode. The self-refresh enable bit in turn
4637 * will be checked/applied by the HW only at the next frame start
4638 * event which is after the vblank start event, so we need to have a
4639 * wait-for-vblank between disabling the plane and the pipe.
4640 */
4641 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004642 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004643 dev_priv->wm.vlv.cxsr = false;
4644 intel_wait_for_vblank(dev, crtc->pipe);
4645 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004646 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004647
Matt Ropered4a6a72016-02-23 17:20:13 -08004648 /*
4649 * IVB workaround: must disable low power watermarks for at least
4650 * one frame before enabling scaling. LP watermarks can be re-enabled
4651 * when scaling is disabled.
4652 *
4653 * WaCxSRDisabledForSpriteScaling:ivb
4654 */
4655 if (pipe_config->disable_lp_wm) {
4656 ilk_disable_lp_wm(dev);
4657 intel_wait_for_vblank(dev, crtc->pipe);
4658 }
4659
4660 /*
4661 * If we're doing a modeset, we're done. No need to do any pre-vblank
4662 * watermark programming here.
4663 */
4664 if (needs_modeset(&pipe_config->base))
4665 return;
4666
4667 /*
4668 * For platforms that support atomic watermarks, program the
4669 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4670 * will be the intermediate values that are safe for both pre- and
4671 * post- vblank; when vblank happens, the 'active' values will be set
4672 * to the final 'target' values and we'll do this again to get the
4673 * optimal watermarks. For gen9+ platforms, the values we program here
4674 * will be the final target values which will get automatically latched
4675 * at vblank time; no further programming will be necessary.
4676 *
4677 * If a platform hasn't been transitioned to atomic watermarks yet,
4678 * we'll continue to update watermarks the old way, if flags tell
4679 * us to.
4680 */
4681 if (dev_priv->display.initial_watermarks != NULL)
4682 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004683 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004684 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004685}
4686
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004687static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004688{
4689 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004690 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004691 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004692 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004693
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004694 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004695
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004696 drm_for_each_plane_mask(p, dev, plane_mask)
4697 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004698
Daniel Vetterf99d7062014-06-19 16:01:59 +02004699 /*
4700 * FIXME: Once we grow proper nuclear flip support out of this we need
4701 * to compute the mask of flip planes precisely. For the time being
4702 * consider this a flip to a NULL plane.
4703 */
4704 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004705}
4706
Jesse Barnesf67a5592011-01-05 10:31:48 -08004707static void ironlake_crtc_enable(struct drm_crtc *crtc)
4708{
4709 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004710 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004711 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004712 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004713 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004714 struct intel_crtc_state *pipe_config =
4715 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004716
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004717 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004718 return;
4719
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004720 /*
4721 * Sometimes spurious CPU pipe underruns happen during FDI
4722 * training, at least with VGA+HDMI cloning. Suppress them.
4723 *
4724 * On ILK we get an occasional spurious CPU pipe underruns
4725 * between eDP port A enable and vdd enable. Also PCH port
4726 * enable seems to result in the occasional CPU pipe underrun.
4727 *
4728 * Spurious PCH underruns also occur during PCH enabling.
4729 */
4730 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4731 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004732 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004733 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4734
4735 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004736 intel_prepare_shared_dpll(intel_crtc);
4737
Ville Syrjälä37a56502016-06-22 21:57:04 +03004738 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304739 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004740
4741 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004742 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004743
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004744 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004745 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004746 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004747 }
4748
4749 ironlake_set_pipeconf(crtc);
4750
Jesse Barnesf67a5592011-01-05 10:31:48 -08004751 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004752
Daniel Vetterf6736a12013-06-05 13:34:30 +02004753 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004754 if (encoder->pre_enable)
4755 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004756
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004757 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004758 /* Note: FDI PLL enabling _must_ be done before we enable the
4759 * cpu pipes, hence this is separate from all the other fdi/pch
4760 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004761 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004762 } else {
4763 assert_fdi_tx_disabled(dev_priv, pipe);
4764 assert_fdi_rx_disabled(dev_priv, pipe);
4765 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004766
Jesse Barnesb074cec2013-04-25 12:55:02 -07004767 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004768
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004769 /*
4770 * On ILK+ LUT must be loaded before the pipe is running but with
4771 * clocks enabled
4772 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004773 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004774
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004775 if (dev_priv->display.initial_watermarks != NULL)
4776 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004777 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004778
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004779 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004780 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004781
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004782 assert_vblank_disabled(crtc);
4783 drm_crtc_vblank_on(crtc);
4784
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004785 for_each_encoder_on_crtc(dev, crtc, encoder)
4786 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004787
4788 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004789 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004790
4791 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4792 if (intel_crtc->config->has_pch_encoder)
4793 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004794 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004795 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004796}
4797
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004798/* IPS only exists on ULT machines and is tied to pipe A. */
4799static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4800{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004801 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004802}
4803
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004804static void haswell_crtc_enable(struct drm_crtc *crtc)
4805{
4806 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004807 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004808 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4809 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004810 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004811 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004812 struct intel_crtc_state *pipe_config =
4813 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004814
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004815 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004816 return;
4817
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004818 if (intel_crtc->config->has_pch_encoder)
4819 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4820 false);
4821
Imre Deak95a7a2a2016-06-13 16:44:35 +03004822 for_each_encoder_on_crtc(dev, crtc, encoder)
4823 if (encoder->pre_pll_enable)
4824 encoder->pre_pll_enable(encoder);
4825
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004826 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004827 intel_enable_shared_dpll(intel_crtc);
4828
Ville Syrjälä37a56502016-06-22 21:57:04 +03004829 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304830 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004831
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004832 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02004833 intel_set_pipe_timings(intel_crtc);
4834
Jani Nikulabc58be62016-03-18 17:05:39 +02004835 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004836
Jani Nikula4d1de972016-03-18 17:05:42 +02004837 if (cpu_transcoder != TRANSCODER_EDP &&
4838 !transcoder_is_dsi(cpu_transcoder)) {
4839 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004840 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004841 }
4842
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004843 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004844 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004845 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004846 }
4847
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004848 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02004849 haswell_set_pipeconf(crtc);
4850
Jani Nikula391bf042016-03-18 17:05:40 +02004851 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004852
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004853 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02004854
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004855 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004856
Daniel Vetter6b698512015-11-28 11:05:39 +01004857 if (intel_crtc->config->has_pch_encoder)
4858 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4859 else
4860 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4861
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304862 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004863 if (encoder->pre_enable)
4864 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304865 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004866
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004867 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004868 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004869
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004870 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304871 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004872
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004873 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004874 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004875 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004876 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004877
4878 /*
4879 * On ILK+ LUT must be loaded before the pipe is running but with
4880 * clocks enabled
4881 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004882 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004883
Paulo Zanoni1f544382012-10-24 11:32:00 -02004884 intel_ddi_set_pipe_settings(crtc);
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004885 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304886 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004887
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004888 if (dev_priv->display.initial_watermarks != NULL)
4889 dev_priv->display.initial_watermarks(pipe_config);
4890 else
4891 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004892
4893 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004894 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02004895 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004896
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004897 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004898 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004899
Jani Nikulaa65347b2015-11-27 12:21:46 +02004900 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004901 intel_ddi_set_vc_payload_alloc(crtc, true);
4902
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004903 assert_vblank_disabled(crtc);
4904 drm_crtc_vblank_on(crtc);
4905
Jani Nikula8807e552013-08-30 19:40:32 +03004906 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004907 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004908 intel_opregion_notify_encoder(encoder, true);
4909 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004910
Daniel Vetter6b698512015-11-28 11:05:39 +01004911 if (intel_crtc->config->has_pch_encoder) {
4912 intel_wait_for_vblank(dev, pipe);
4913 intel_wait_for_vblank(dev, pipe);
4914 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004915 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4916 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004917 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004918
Paulo Zanonie4916942013-09-20 16:21:19 -03004919 /* If we change the relative order between pipe/planes enabling, we need
4920 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004921 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4922 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4923 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4924 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4925 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004926}
4927
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004928static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004929{
4930 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004931 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004932 int pipe = crtc->pipe;
4933
4934 /* To avoid upsetting the power well on haswell only disable the pfit if
4935 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004936 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004937 I915_WRITE(PF_CTL(pipe), 0);
4938 I915_WRITE(PF_WIN_POS(pipe), 0);
4939 I915_WRITE(PF_WIN_SZ(pipe), 0);
4940 }
4941}
4942
Jesse Barnes6be4a602010-09-10 10:26:01 -07004943static void ironlake_crtc_disable(struct drm_crtc *crtc)
4944{
4945 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004946 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004947 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004948 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004949 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004950
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004951 /*
4952 * Sometimes spurious CPU pipe underruns happen when the
4953 * pipe is already disabled, but FDI RX/TX is still enabled.
4954 * Happens at least with VGA+HDMI cloning. Suppress them.
4955 */
4956 if (intel_crtc->config->has_pch_encoder) {
4957 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004958 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004959 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004960
Daniel Vetterea9d7582012-07-10 10:42:52 +02004961 for_each_encoder_on_crtc(dev, crtc, encoder)
4962 encoder->disable(encoder);
4963
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004964 drm_crtc_vblank_off(crtc);
4965 assert_vblank_disabled(crtc);
4966
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004967 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004968
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004969 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004970
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004971 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004972 ironlake_fdi_disable(crtc);
4973
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004974 for_each_encoder_on_crtc(dev, crtc, encoder)
4975 if (encoder->post_disable)
4976 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004977
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004978 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004979 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004980
Daniel Vetterd925c592013-06-05 13:34:04 +02004981 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004982 i915_reg_t reg;
4983 u32 temp;
4984
Daniel Vetterd925c592013-06-05 13:34:04 +02004985 /* disable TRANS_DP_CTL */
4986 reg = TRANS_DP_CTL(pipe);
4987 temp = I915_READ(reg);
4988 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4989 TRANS_DP_PORT_SEL_MASK);
4990 temp |= TRANS_DP_PORT_SEL_NONE;
4991 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004992
Daniel Vetterd925c592013-06-05 13:34:04 +02004993 /* disable DPLL_SEL */
4994 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004995 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004996 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004997 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004998
Daniel Vetterd925c592013-06-05 13:34:04 +02004999 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005000 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005001
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005002 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005003 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005004}
5005
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005006static void haswell_crtc_disable(struct drm_crtc *crtc)
5007{
5008 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005009 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5011 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005012 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005013
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005014 if (intel_crtc->config->has_pch_encoder)
5015 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5016 false);
5017
Jani Nikula8807e552013-08-30 19:40:32 +03005018 for_each_encoder_on_crtc(dev, crtc, encoder) {
5019 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005020 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005021 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005022
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005023 drm_crtc_vblank_off(crtc);
5024 assert_vblank_disabled(crtc);
5025
Jani Nikula4d1de972016-03-18 17:05:42 +02005026 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005027 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005028 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005029
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005030 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005031 intel_ddi_set_vc_payload_alloc(crtc, false);
5032
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005033 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305034 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005035
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005036 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005037 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005038 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005039 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005040
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005041 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305042 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005043
Imre Deak97b040a2014-06-25 22:01:50 +03005044 for_each_encoder_on_crtc(dev, crtc, encoder)
5045 if (encoder->post_disable)
5046 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005047
Ville Syrjälä92966a32015-12-08 16:05:48 +02005048 if (intel_crtc->config->has_pch_encoder) {
5049 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005050 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005051 intel_ddi_fdi_disable(crtc);
5052
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005053 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5054 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005055 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005056}
5057
Jesse Barnes2dd24552013-04-25 12:55:01 -07005058static void i9xx_pfit_enable(struct intel_crtc *crtc)
5059{
5060 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005061 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005062 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005063
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005064 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005065 return;
5066
Daniel Vetterc0b03412013-05-28 12:05:54 +02005067 /*
5068 * The panel fitter should only be adjusted whilst the pipe is disabled,
5069 * according to register description and PRM.
5070 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005071 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5072 assert_pipe_disabled(dev_priv, crtc->pipe);
5073
Jesse Barnesb074cec2013-04-25 12:55:02 -07005074 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5075 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005076
5077 /* Border color in case we don't scale up to the full screen. Black by
5078 * default, change to something else for debugging. */
5079 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005080}
5081
Dave Airlied05410f2014-06-05 13:22:59 +10005082static enum intel_display_power_domain port_to_power_domain(enum port port)
5083{
5084 switch (port) {
5085 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005086 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005087 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005088 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005089 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005090 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005091 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005092 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005093 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005094 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005095 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005096 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005097 return POWER_DOMAIN_PORT_OTHER;
5098 }
5099}
5100
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005101static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5102{
5103 switch (port) {
5104 case PORT_A:
5105 return POWER_DOMAIN_AUX_A;
5106 case PORT_B:
5107 return POWER_DOMAIN_AUX_B;
5108 case PORT_C:
5109 return POWER_DOMAIN_AUX_C;
5110 case PORT_D:
5111 return POWER_DOMAIN_AUX_D;
5112 case PORT_E:
5113 /* FIXME: Check VBT for actual wiring of PORT E */
5114 return POWER_DOMAIN_AUX_D;
5115 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005116 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005117 return POWER_DOMAIN_AUX_A;
5118 }
5119}
5120
Imre Deak319be8a2014-03-04 19:22:57 +02005121enum intel_display_power_domain
5122intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005123{
Imre Deak319be8a2014-03-04 19:22:57 +02005124 struct drm_device *dev = intel_encoder->base.dev;
5125 struct intel_digital_port *intel_dig_port;
5126
5127 switch (intel_encoder->type) {
5128 case INTEL_OUTPUT_UNKNOWN:
5129 /* Only DDI platforms should ever use this output type */
5130 WARN_ON_ONCE(!HAS_DDI(dev));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005131 case INTEL_OUTPUT_DP:
Imre Deak319be8a2014-03-04 19:22:57 +02005132 case INTEL_OUTPUT_HDMI:
5133 case INTEL_OUTPUT_EDP:
5134 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005135 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005136 case INTEL_OUTPUT_DP_MST:
5137 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5138 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005139 case INTEL_OUTPUT_ANALOG:
5140 return POWER_DOMAIN_PORT_CRT;
5141 case INTEL_OUTPUT_DSI:
5142 return POWER_DOMAIN_PORT_DSI;
5143 default:
5144 return POWER_DOMAIN_PORT_OTHER;
5145 }
5146}
5147
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005148enum intel_display_power_domain
5149intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5150{
5151 struct drm_device *dev = intel_encoder->base.dev;
5152 struct intel_digital_port *intel_dig_port;
5153
5154 switch (intel_encoder->type) {
5155 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005156 case INTEL_OUTPUT_HDMI:
5157 /*
5158 * Only DDI platforms should ever use these output types.
5159 * We can get here after the HDMI detect code has already set
5160 * the type of the shared encoder. Since we can't be sure
5161 * what's the status of the given connectors, play safe and
5162 * run the DP detection too.
5163 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005164 WARN_ON_ONCE(!HAS_DDI(dev));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005165 case INTEL_OUTPUT_DP:
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005166 case INTEL_OUTPUT_EDP:
5167 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5168 return port_to_aux_power_domain(intel_dig_port->port);
5169 case INTEL_OUTPUT_DP_MST:
5170 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5171 return port_to_aux_power_domain(intel_dig_port->port);
5172 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005173 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005174 return POWER_DOMAIN_AUX_A;
5175 }
5176}
5177
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005178static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5179 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005180{
5181 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005182 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5184 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005185 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005186 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005187
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005188 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005189 return 0;
5190
Imre Deak77d22dc2014-03-05 16:20:52 +02005191 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5192 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005193 if (crtc_state->pch_pfit.enabled ||
5194 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005195 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5196
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005197 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5198 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5199
Imre Deak319be8a2014-03-04 19:22:57 +02005200 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005201 }
Imre Deak319be8a2014-03-04 19:22:57 +02005202
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005203 if (crtc_state->shared_dpll)
5204 mask |= BIT(POWER_DOMAIN_PLLS);
5205
Imre Deak77d22dc2014-03-05 16:20:52 +02005206 return mask;
5207}
5208
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005209static unsigned long
5210modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5211 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005212{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005213 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005214 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5215 enum intel_display_power_domain domain;
Daniel Vetter5a21b662016-05-24 17:13:53 +02005216 unsigned long domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005217
5218 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005219 intel_crtc->enabled_power_domains = new_domains =
5220 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005221
Daniel Vetter5a21b662016-05-24 17:13:53 +02005222 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005223
5224 for_each_power_domain(domain, domains)
5225 intel_display_power_get(dev_priv, domain);
5226
Daniel Vetter5a21b662016-05-24 17:13:53 +02005227 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005228}
5229
5230static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5231 unsigned long domains)
5232{
5233 enum intel_display_power_domain domain;
5234
5235 for_each_power_domain(domain, domains)
5236 intel_display_power_put(dev_priv, domain);
5237}
5238
Mika Kaholaadafdc62015-08-18 14:36:59 +03005239static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5240{
5241 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5242
5243 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5244 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5245 return max_cdclk_freq;
5246 else if (IS_CHERRYVIEW(dev_priv))
5247 return max_cdclk_freq*95/100;
5248 else if (INTEL_INFO(dev_priv)->gen < 4)
5249 return 2*max_cdclk_freq*90/100;
5250 else
5251 return max_cdclk_freq*90/100;
5252}
5253
Ville Syrjäläb2045352016-05-13 23:41:27 +03005254static int skl_calc_cdclk(int max_pixclk, int vco);
5255
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005256static void intel_update_max_cdclk(struct drm_device *dev)
5257{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005258 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005259
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005260 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005261 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005262 int max_cdclk, vco;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005263
Ville Syrjäläb2045352016-05-13 23:41:27 +03005264 vco = dev_priv->skl_preferred_vco_freq;
Ville Syrjälä63911d72016-05-13 23:41:32 +03005265 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005266
5267 /*
5268 * Use the lower (vco 8640) cdclk values as a
5269 * first guess. skl_calc_cdclk() will correct it
5270 * if the preferred vco is 8100 instead.
5271 */
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005272 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005273 max_cdclk = 617143;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005274 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005275 max_cdclk = 540000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005276 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005277 max_cdclk = 432000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005278 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005279 max_cdclk = 308571;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005280
5281 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
Matt Roper281c1142016-04-05 14:37:19 -07005282 } else if (IS_BROXTON(dev)) {
5283 dev_priv->max_cdclk_freq = 624000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005284 } else if (IS_BROADWELL(dev)) {
5285 /*
5286 * FIXME with extra cooling we can allow
5287 * 540 MHz for ULX and 675 Mhz for ULT.
5288 * How can we know if extra cooling is
5289 * available? PCI ID, VTB, something else?
5290 */
5291 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5292 dev_priv->max_cdclk_freq = 450000;
5293 else if (IS_BDW_ULX(dev))
5294 dev_priv->max_cdclk_freq = 450000;
5295 else if (IS_BDW_ULT(dev))
5296 dev_priv->max_cdclk_freq = 540000;
5297 else
5298 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005299 } else if (IS_CHERRYVIEW(dev)) {
5300 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005301 } else if (IS_VALLEYVIEW(dev)) {
5302 dev_priv->max_cdclk_freq = 400000;
5303 } else {
5304 /* otherwise assume cdclk is fixed */
5305 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5306 }
5307
Mika Kaholaadafdc62015-08-18 14:36:59 +03005308 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5309
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005310 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5311 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005312
5313 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5314 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005315}
5316
5317static void intel_update_cdclk(struct drm_device *dev)
5318{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005319 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005320
5321 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005322
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005323 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005324 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5325 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5326 dev_priv->cdclk_pll.ref);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005327 else
5328 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5329 dev_priv->cdclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005330
5331 /*
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005332 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5333 * Programmng [sic] note: bit[9:2] should be programmed to the number
5334 * of cdclk that generates 4MHz reference clock freq which is used to
5335 * generate GMBus clock. This will vary with the cdclk freq.
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005336 */
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005337 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005338 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005339}
5340
Ville Syrjälä92891e42016-05-11 22:44:45 +03005341/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5342static int skl_cdclk_decimal(int cdclk)
5343{
5344 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5345}
5346
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005347static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5348{
5349 int ratio;
5350
5351 if (cdclk == dev_priv->cdclk_pll.ref)
5352 return 0;
5353
5354 switch (cdclk) {
5355 default:
5356 MISSING_CASE(cdclk);
5357 case 144000:
5358 case 288000:
5359 case 384000:
5360 case 576000:
5361 ratio = 60;
5362 break;
5363 case 624000:
5364 ratio = 65;
5365 break;
5366 }
5367
5368 return dev_priv->cdclk_pll.ref * ratio;
5369}
5370
Ville Syrjälä2b730012016-05-13 23:41:34 +03005371static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5372{
5373 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5374
5375 /* Timeout 200us */
Chris Wilson95cac282016-06-30 15:33:03 +01005376 if (intel_wait_for_register(dev_priv,
5377 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5378 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005379 DRM_ERROR("timeout waiting for DE PLL unlock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005380
5381 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005382}
5383
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005384static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005385{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005386 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005387 u32 val;
5388
5389 val = I915_READ(BXT_DE_PLL_CTL);
5390 val &= ~BXT_DE_PLL_RATIO_MASK;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005391 val |= BXT_DE_PLL_RATIO(ratio);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005392 I915_WRITE(BXT_DE_PLL_CTL, val);
5393
5394 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5395
5396 /* Timeout 200us */
Chris Wilsone084e1b2016-06-30 15:33:04 +01005397 if (intel_wait_for_register(dev_priv,
5398 BXT_DE_PLL_ENABLE,
5399 BXT_DE_PLL_LOCK,
5400 BXT_DE_PLL_LOCK,
5401 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005402 DRM_ERROR("timeout waiting for DE PLL lock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005403
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005404 dev_priv->cdclk_pll.vco = vco;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005405}
5406
Imre Deak324513c2016-06-13 16:44:36 +03005407static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305408{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005409 u32 val, divider;
5410 int vco, ret;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305411
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005412 vco = bxt_de_pll_vco(dev_priv, cdclk);
5413
5414 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5415
5416 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5417 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5418 case 8:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305419 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305420 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005421 case 4:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305422 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305423 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005424 case 3:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305425 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305426 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005427 case 2:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305428 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305429 break;
5430 default:
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005431 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5432 WARN_ON(vco != 0);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305433
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005434 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5435 break;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305436 }
5437
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305438 /* Inform power controller of upcoming frequency change */
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005439 mutex_lock(&dev_priv->rps.hw_lock);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305440 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5441 0x80000000);
5442 mutex_unlock(&dev_priv->rps.hw_lock);
5443
5444 if (ret) {
5445 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005446 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305447 return;
5448 }
5449
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005450 if (dev_priv->cdclk_pll.vco != 0 &&
5451 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005452 bxt_de_pll_disable(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305453
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005454 if (dev_priv->cdclk_pll.vco != vco)
5455 bxt_de_pll_enable(dev_priv, vco);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305456
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005457 val = divider | skl_cdclk_decimal(cdclk);
5458 /*
5459 * FIXME if only the cd2x divider needs changing, it could be done
5460 * without shutting off the pipe (if only one pipe is active).
5461 */
5462 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5463 /*
5464 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5465 * enable otherwise.
5466 */
5467 if (cdclk >= 500000)
5468 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5469 I915_WRITE(CDCLK_CTL, val);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305470
5471 mutex_lock(&dev_priv->rps.hw_lock);
5472 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005473 DIV_ROUND_UP(cdclk, 25000));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305474 mutex_unlock(&dev_priv->rps.hw_lock);
5475
5476 if (ret) {
5477 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005478 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305479 return;
5480 }
5481
Chris Wilson91c8a322016-07-05 10:40:23 +01005482 intel_update_cdclk(&dev_priv->drm);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305483}
5484
Imre Deakd66a2192016-05-24 15:38:33 +03005485static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305486{
Imre Deakd66a2192016-05-24 15:38:33 +03005487 u32 cdctl, expected;
5488
Chris Wilson91c8a322016-07-05 10:40:23 +01005489 intel_update_cdclk(&dev_priv->drm);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305490
Imre Deakd66a2192016-05-24 15:38:33 +03005491 if (dev_priv->cdclk_pll.vco == 0 ||
5492 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5493 goto sanitize;
5494
5495 /* DPLL okay; verify the cdclock
5496 *
5497 * Some BIOS versions leave an incorrect decimal frequency value and
5498 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5499 * so sanitize this register.
5500 */
5501 cdctl = I915_READ(CDCLK_CTL);
5502 /*
5503 * Let's ignore the pipe field, since BIOS could have configured the
5504 * dividers both synching to an active pipe, or asynchronously
5505 * (PIPE_NONE).
5506 */
5507 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5508
5509 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5510 skl_cdclk_decimal(dev_priv->cdclk_freq);
5511 /*
5512 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5513 * enable otherwise.
5514 */
5515 if (dev_priv->cdclk_freq >= 500000)
5516 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5517
5518 if (cdctl == expected)
5519 /* All well; nothing to sanitize */
5520 return;
5521
5522sanitize:
5523 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5524
5525 /* force cdclk programming */
5526 dev_priv->cdclk_freq = 0;
5527
5528 /* force full PLL disable + enable */
5529 dev_priv->cdclk_pll.vco = -1;
5530}
5531
Imre Deak324513c2016-06-13 16:44:36 +03005532void bxt_init_cdclk(struct drm_i915_private *dev_priv)
Imre Deakd66a2192016-05-24 15:38:33 +03005533{
5534 bxt_sanitize_cdclk(dev_priv);
5535
5536 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03005537 return;
Imre Deakc2e001e2016-04-01 16:02:43 +03005538
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305539 /*
5540 * FIXME:
5541 * - The initial CDCLK needs to be read from VBT.
5542 * Need to make this change after VBT has changes for BXT.
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305543 */
Imre Deak324513c2016-06-13 16:44:36 +03005544 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305545}
5546
Imre Deak324513c2016-06-13 16:44:36 +03005547void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305548{
Imre Deak324513c2016-06-13 16:44:36 +03005549 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305550}
5551
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005552static int skl_calc_cdclk(int max_pixclk, int vco)
5553{
Ville Syrjälä63911d72016-05-13 23:41:32 +03005554 if (vco == 8640000) {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005555 if (max_pixclk > 540000)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005556 return 617143;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005557 else if (max_pixclk > 432000)
5558 return 540000;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005559 else if (max_pixclk > 308571)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005560 return 432000;
5561 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005562 return 308571;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005563 } else {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005564 if (max_pixclk > 540000)
5565 return 675000;
5566 else if (max_pixclk > 450000)
5567 return 540000;
5568 else if (max_pixclk > 337500)
5569 return 450000;
5570 else
5571 return 337500;
5572 }
5573}
5574
Ville Syrjäläea617912016-05-13 23:41:24 +03005575static void
5576skl_dpll0_update(struct drm_i915_private *dev_priv)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005577{
Ville Syrjäläea617912016-05-13 23:41:24 +03005578 u32 val;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005579
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005580 dev_priv->cdclk_pll.ref = 24000;
Imre Deak1c3f7702016-05-24 15:38:32 +03005581 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005582
Ville Syrjäläea617912016-05-13 23:41:24 +03005583 val = I915_READ(LCPLL1_CTL);
Imre Deak1c3f7702016-05-24 15:38:32 +03005584 if ((val & LCPLL_PLL_ENABLE) == 0)
Ville Syrjäläea617912016-05-13 23:41:24 +03005585 return;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005586
Imre Deak1c3f7702016-05-24 15:38:32 +03005587 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5588 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005589
Ville Syrjäläea617912016-05-13 23:41:24 +03005590 val = I915_READ(DPLL_CTRL1);
5591
Imre Deak1c3f7702016-05-24 15:38:32 +03005592 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5593 DPLL_CTRL1_SSC(SKL_DPLL0) |
5594 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5595 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5596 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005597
Ville Syrjäläea617912016-05-13 23:41:24 +03005598 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5599 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5600 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5601 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5602 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005603 dev_priv->cdclk_pll.vco = 8100000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005604 break;
5605 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5606 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005607 dev_priv->cdclk_pll.vco = 8640000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005608 break;
5609 default:
5610 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
Ville Syrjäläea617912016-05-13 23:41:24 +03005611 break;
5612 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005613}
5614
Ville Syrjäläb2045352016-05-13 23:41:27 +03005615void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5616{
5617 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5618
5619 dev_priv->skl_preferred_vco_freq = vco;
5620
5621 if (changed)
Chris Wilson91c8a322016-07-05 10:40:23 +01005622 intel_update_max_cdclk(&dev_priv->drm);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005623}
5624
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005625static void
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005626skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005627{
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005628 int min_cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005629 u32 val;
5630
Ville Syrjälä63911d72016-05-13 23:41:32 +03005631 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005632
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005633 /* select the minimum CDCLK before enabling DPLL 0 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005634 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005635 I915_WRITE(CDCLK_CTL, val);
5636 POSTING_READ(CDCLK_CTL);
5637
5638 /*
5639 * We always enable DPLL0 with the lowest link rate possible, but still
5640 * taking into account the VCO required to operate the eDP panel at the
5641 * desired frequency. The usual DP link rates operate with a VCO of
5642 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5643 * The modeset code is responsible for the selection of the exact link
5644 * rate later on, with the constraint of choosing a frequency that
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005645 * works with vco.
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005646 */
5647 val = I915_READ(DPLL_CTRL1);
5648
5649 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5650 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5651 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Ville Syrjälä63911d72016-05-13 23:41:32 +03005652 if (vco == 8640000)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005653 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5654 SKL_DPLL0);
5655 else
5656 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5657 SKL_DPLL0);
5658
5659 I915_WRITE(DPLL_CTRL1, val);
5660 POSTING_READ(DPLL_CTRL1);
5661
5662 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5663
Chris Wilsone24ca052016-06-30 15:33:05 +01005664 if (intel_wait_for_register(dev_priv,
5665 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
5666 5))
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005667 DRM_ERROR("DPLL0 not locked\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005668
Ville Syrjälä63911d72016-05-13 23:41:32 +03005669 dev_priv->cdclk_pll.vco = vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005670
5671 /* We'll want to keep using the current vco from now on. */
5672 skl_set_preferred_cdclk_vco(dev_priv, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005673}
5674
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005675static void
5676skl_dpll0_disable(struct drm_i915_private *dev_priv)
5677{
5678 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
Chris Wilson8ad32a052016-06-30 15:33:06 +01005679 if (intel_wait_for_register(dev_priv,
5680 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
5681 1))
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005682 DRM_ERROR("Couldn't disable DPLL0\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005683
Ville Syrjälä63911d72016-05-13 23:41:32 +03005684 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005685}
5686
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005687static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5688{
5689 int ret;
5690 u32 val;
5691
5692 /* inform PCU we want to change CDCLK */
5693 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5694 mutex_lock(&dev_priv->rps.hw_lock);
5695 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5696 mutex_unlock(&dev_priv->rps.hw_lock);
5697
5698 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5699}
5700
5701static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5702{
5703 unsigned int i;
5704
5705 for (i = 0; i < 15; i++) {
5706 if (skl_cdclk_pcu_ready(dev_priv))
5707 return true;
5708 udelay(10);
5709 }
5710
5711 return false;
5712}
5713
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005714static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005715{
Chris Wilson91c8a322016-07-05 10:40:23 +01005716 struct drm_device *dev = &dev_priv->drm;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005717 u32 freq_select, pcu_ack;
5718
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005719 WARN_ON((cdclk == 24000) != (vco == 0));
5720
Ville Syrjälä63911d72016-05-13 23:41:32 +03005721 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005722
5723 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5724 DRM_ERROR("failed to inform PCU about cdclk change\n");
5725 return;
5726 }
5727
5728 /* set CDCLK_CTL */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005729 switch (cdclk) {
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005730 case 450000:
5731 case 432000:
5732 freq_select = CDCLK_FREQ_450_432;
5733 pcu_ack = 1;
5734 break;
5735 case 540000:
5736 freq_select = CDCLK_FREQ_540;
5737 pcu_ack = 2;
5738 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005739 case 308571:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005740 case 337500:
5741 default:
5742 freq_select = CDCLK_FREQ_337_308;
5743 pcu_ack = 0;
5744 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005745 case 617143:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005746 case 675000:
5747 freq_select = CDCLK_FREQ_675_617;
5748 pcu_ack = 3;
5749 break;
5750 }
5751
Ville Syrjälä63911d72016-05-13 23:41:32 +03005752 if (dev_priv->cdclk_pll.vco != 0 &&
5753 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005754 skl_dpll0_disable(dev_priv);
5755
Ville Syrjälä63911d72016-05-13 23:41:32 +03005756 if (dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005757 skl_dpll0_enable(dev_priv, vco);
5758
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005759 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005760 POSTING_READ(CDCLK_CTL);
5761
5762 /* inform PCU of the change */
5763 mutex_lock(&dev_priv->rps.hw_lock);
5764 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5765 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005766
5767 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005768}
5769
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005770static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5771
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005772void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5773{
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005774 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005775}
5776
5777void skl_init_cdclk(struct drm_i915_private *dev_priv)
5778{
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005779 int cdclk, vco;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005780
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005781 skl_sanitize_cdclk(dev_priv);
5782
Ville Syrjälä63911d72016-05-13 23:41:32 +03005783 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005784 /*
5785 * Use the current vco as our initial
5786 * guess as to what the preferred vco is.
5787 */
5788 if (dev_priv->skl_preferred_vco_freq == 0)
5789 skl_set_preferred_cdclk_vco(dev_priv,
Ville Syrjälä63911d72016-05-13 23:41:32 +03005790 dev_priv->cdclk_pll.vco);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005791 return;
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005792 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005793
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005794 vco = dev_priv->skl_preferred_vco_freq;
5795 if (vco == 0)
Ville Syrjälä63911d72016-05-13 23:41:32 +03005796 vco = 8100000;
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005797 cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005798
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005799 skl_set_cdclk(dev_priv, cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005800}
5801
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005802static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305803{
Ville Syrjälä09492492016-05-13 23:41:28 +03005804 uint32_t cdctl, expected;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305805
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305806 /*
5807 * check if the pre-os intialized the display
5808 * There is SWF18 scratchpad register defined which is set by the
5809 * pre-os which can be used by the OS drivers to check the status
5810 */
5811 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5812 goto sanitize;
5813
Chris Wilson91c8a322016-07-05 10:40:23 +01005814 intel_update_cdclk(&dev_priv->drm);
Imre Deak1c3f7702016-05-24 15:38:32 +03005815 /* Is PLL enabled and locked ? */
5816 if (dev_priv->cdclk_pll.vco == 0 ||
5817 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5818 goto sanitize;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005819
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305820 /* DPLL okay; verify the cdclock
5821 *
5822 * Noticed in some instances that the freq selection is correct but
5823 * decimal part is programmed wrong from BIOS where pre-os does not
5824 * enable display. Verify the same as well.
5825 */
Ville Syrjälä09492492016-05-13 23:41:28 +03005826 cdctl = I915_READ(CDCLK_CTL);
5827 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5828 skl_cdclk_decimal(dev_priv->cdclk_freq);
5829 if (cdctl == expected)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305830 /* All well; nothing to sanitize */
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005831 return;
5832
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305833sanitize:
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005834 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
Clint Taylorc89e39f2016-05-13 23:41:21 +03005835
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005836 /* force cdclk programming */
5837 dev_priv->cdclk_freq = 0;
5838 /* force full PLL disable + enable */
Ville Syrjälä63911d72016-05-13 23:41:32 +03005839 dev_priv->cdclk_pll.vco = -1;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305840}
5841
Jesse Barnes30a970c2013-11-04 13:48:12 -08005842/* Adjust CDclk dividers to allow high res or save power if possible */
5843static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5844{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005845 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005846 u32 val, cmd;
5847
Vandana Kannan164dfd22014-11-24 13:37:41 +05305848 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5849 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005850
Ville Syrjälädfcab172014-06-13 13:37:47 +03005851 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005852 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005853 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005854 cmd = 1;
5855 else
5856 cmd = 0;
5857
5858 mutex_lock(&dev_priv->rps.hw_lock);
5859 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5860 val &= ~DSPFREQGUAR_MASK;
5861 val |= (cmd << DSPFREQGUAR_SHIFT);
5862 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5863 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5864 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5865 50)) {
5866 DRM_ERROR("timed out waiting for CDclk change\n");
5867 }
5868 mutex_unlock(&dev_priv->rps.hw_lock);
5869
Ville Syrjälä54433e92015-05-26 20:42:31 +03005870 mutex_lock(&dev_priv->sb_lock);
5871
Ville Syrjälädfcab172014-06-13 13:37:47 +03005872 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005873 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005874
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005875 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005876
Jesse Barnes30a970c2013-11-04 13:48:12 -08005877 /* adjust cdclk divider */
5878 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005879 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005880 val |= divider;
5881 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005882
5883 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005884 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005885 50))
5886 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005887 }
5888
Jesse Barnes30a970c2013-11-04 13:48:12 -08005889 /* adjust self-refresh exit latency value */
5890 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5891 val &= ~0x7f;
5892
5893 /*
5894 * For high bandwidth configs, we set a higher latency in the bunit
5895 * so that the core display fetch happens in time to avoid underruns.
5896 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005897 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005898 val |= 4500 / 250; /* 4.5 usec */
5899 else
5900 val |= 3000 / 250; /* 3.0 usec */
5901 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005902
Ville Syrjäläa5805162015-05-26 20:42:30 +03005903 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005904
Ville Syrjäläb6283052015-06-03 15:45:07 +03005905 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005906}
5907
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005908static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5909{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005910 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005911 u32 val, cmd;
5912
Vandana Kannan164dfd22014-11-24 13:37:41 +05305913 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5914 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005915
5916 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005917 case 333333:
5918 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005919 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005920 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005921 break;
5922 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005923 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005924 return;
5925 }
5926
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005927 /*
5928 * Specs are full of misinformation, but testing on actual
5929 * hardware has shown that we just need to write the desired
5930 * CCK divider into the Punit register.
5931 */
5932 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5933
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005934 mutex_lock(&dev_priv->rps.hw_lock);
5935 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5936 val &= ~DSPFREQGUAR_MASK_CHV;
5937 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5938 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5939 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5940 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5941 50)) {
5942 DRM_ERROR("timed out waiting for CDclk change\n");
5943 }
5944 mutex_unlock(&dev_priv->rps.hw_lock);
5945
Ville Syrjäläb6283052015-06-03 15:45:07 +03005946 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005947}
5948
Jesse Barnes30a970c2013-11-04 13:48:12 -08005949static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5950 int max_pixclk)
5951{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005952 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005953 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005954
Jesse Barnes30a970c2013-11-04 13:48:12 -08005955 /*
5956 * Really only a few cases to deal with, as only 4 CDclks are supported:
5957 * 200MHz
5958 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005959 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005960 * 400MHz (VLV only)
5961 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5962 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005963 *
5964 * We seem to get an unstable or solid color picture at 200MHz.
5965 * Not sure what's wrong. For now use 200MHz only when all pipes
5966 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005967 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005968 if (!IS_CHERRYVIEW(dev_priv) &&
5969 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005970 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005971 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005972 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005973 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005974 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005975 else
5976 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005977}
5978
Imre Deak324513c2016-06-13 16:44:36 +03005979static int bxt_calc_cdclk(int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005980{
Ville Syrjälä760e1472016-05-11 22:44:46 +03005981 if (max_pixclk > 576000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305982 return 624000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005983 else if (max_pixclk > 384000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305984 return 576000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005985 else if (max_pixclk > 288000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305986 return 384000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005987 else if (max_pixclk > 144000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305988 return 288000;
5989 else
5990 return 144000;
5991}
5992
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005993/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005994static int intel_mode_max_pixclk(struct drm_device *dev,
5995 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005996{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005997 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +01005998 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005999 struct drm_crtc *crtc;
6000 struct drm_crtc_state *crtc_state;
6001 unsigned max_pixclk = 0, i;
6002 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006003
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006004 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6005 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006006
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006007 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6008 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006009
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006010 if (crtc_state->enable)
6011 pixclk = crtc_state->adjusted_mode.crtc_clock;
6012
6013 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006014 }
6015
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006016 for_each_pipe(dev_priv, pipe)
6017 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6018
Jesse Barnes30a970c2013-11-04 13:48:12 -08006019 return max_pixclk;
6020}
6021
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006022static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006023{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006024 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006025 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006026 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006027 struct intel_atomic_state *intel_state =
6028 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006029
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006030 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006031 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306032
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006033 if (!intel_state->active_crtcs)
6034 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6035
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006036 return 0;
6037}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006038
Imre Deak324513c2016-06-13 16:44:36 +03006039static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006040{
Ville Syrjälä4e5ca602016-05-11 22:44:44 +03006041 int max_pixclk = ilk_max_pixel_rate(state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006042 struct intel_atomic_state *intel_state =
6043 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006044
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006045 intel_state->cdclk = intel_state->dev_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +03006046 bxt_calc_cdclk(max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006047
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006048 if (!intel_state->active_crtcs)
Imre Deak324513c2016-06-13 16:44:36 +03006049 intel_state->dev_cdclk = bxt_calc_cdclk(0);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006050
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006051 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006052}
6053
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006054static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6055{
6056 unsigned int credits, default_credits;
6057
6058 if (IS_CHERRYVIEW(dev_priv))
6059 default_credits = PFI_CREDIT(12);
6060 else
6061 default_credits = PFI_CREDIT(8);
6062
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006063 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006064 /* CHV suggested value is 31 or 63 */
6065 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006066 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006067 else
6068 credits = PFI_CREDIT(15);
6069 } else {
6070 credits = default_credits;
6071 }
6072
6073 /*
6074 * WA - write default credits before re-programming
6075 * FIXME: should we also set the resend bit here?
6076 */
6077 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6078 default_credits);
6079
6080 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6081 credits | PFI_CREDIT_RESEND);
6082
6083 /*
6084 * FIXME is this guaranteed to clear
6085 * immediately or should we poll for it?
6086 */
6087 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6088}
6089
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006090static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006091{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006092 struct drm_device *dev = old_state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006093 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006094 struct intel_atomic_state *old_intel_state =
6095 to_intel_atomic_state(old_state);
6096 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006097
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006098 /*
6099 * FIXME: We can end up here with all power domains off, yet
6100 * with a CDCLK frequency other than the minimum. To account
6101 * for this take the PIPE-A power domain, which covers the HW
6102 * blocks needed for the following programming. This can be
6103 * removed once it's guaranteed that we get here either with
6104 * the minimum CDCLK set, or the required power domains
6105 * enabled.
6106 */
6107 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006108
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006109 if (IS_CHERRYVIEW(dev))
6110 cherryview_set_cdclk(dev, req_cdclk);
6111 else
6112 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006113
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006114 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006115
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006116 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006117}
6118
Jesse Barnes89b667f2013-04-18 14:51:36 -07006119static void valleyview_crtc_enable(struct drm_crtc *crtc)
6120{
6121 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006122 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006123 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6124 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006125 struct intel_crtc_state *pipe_config =
6126 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006127 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006128
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006129 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006130 return;
6131
Ville Syrjälä37a56502016-06-22 21:57:04 +03006132 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306133 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006134
6135 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006136 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006137
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006138 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
Chris Wilsonfac5e232016-07-04 11:34:36 +01006139 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006140
6141 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6142 I915_WRITE(CHV_CANVAS(pipe), 0);
6143 }
6144
Daniel Vetter5b18e572014-04-24 23:55:06 +02006145 i9xx_set_pipeconf(intel_crtc);
6146
Jesse Barnes89b667f2013-04-18 14:51:36 -07006147 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006148
Daniel Vettera72e4c92014-09-30 10:56:47 +02006149 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006150
Jesse Barnes89b667f2013-04-18 14:51:36 -07006151 for_each_encoder_on_crtc(dev, crtc, encoder)
6152 if (encoder->pre_pll_enable)
6153 encoder->pre_pll_enable(encoder);
6154
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006155 if (IS_CHERRYVIEW(dev)) {
6156 chv_prepare_pll(intel_crtc, intel_crtc->config);
6157 chv_enable_pll(intel_crtc, intel_crtc->config);
6158 } else {
6159 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6160 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006161 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006162
6163 for_each_encoder_on_crtc(dev, crtc, encoder)
6164 if (encoder->pre_enable)
6165 encoder->pre_enable(encoder);
6166
Jesse Barnes2dd24552013-04-25 12:55:01 -07006167 i9xx_pfit_enable(intel_crtc);
6168
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006169 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006170
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006171 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006172 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006173
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006174 assert_vblank_disabled(crtc);
6175 drm_crtc_vblank_on(crtc);
6176
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006177 for_each_encoder_on_crtc(dev, crtc, encoder)
6178 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006179}
6180
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006181static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6182{
6183 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006184 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006185
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006186 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6187 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006188}
6189
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006190static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006191{
6192 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006193 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006194 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006195 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006196 struct intel_crtc_state *pipe_config =
6197 to_intel_crtc_state(crtc->state);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006198 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006199
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006200 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006201 return;
6202
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006203 i9xx_set_pll_dividers(intel_crtc);
6204
Ville Syrjälä37a56502016-06-22 21:57:04 +03006205 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306206 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006207
6208 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006209 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006210
Daniel Vetter5b18e572014-04-24 23:55:06 +02006211 i9xx_set_pipeconf(intel_crtc);
6212
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006213 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006214
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006215 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006216 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006217
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006218 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006219 if (encoder->pre_enable)
6220 encoder->pre_enable(encoder);
6221
Daniel Vetterf6736a12013-06-05 13:34:30 +02006222 i9xx_enable_pll(intel_crtc);
6223
Jesse Barnes2dd24552013-04-25 12:55:01 -07006224 i9xx_pfit_enable(intel_crtc);
6225
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006226 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006227
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006228 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006229 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006230
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006231 assert_vblank_disabled(crtc);
6232 drm_crtc_vblank_on(crtc);
6233
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006234 for_each_encoder_on_crtc(dev, crtc, encoder)
6235 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006236}
6237
Daniel Vetter87476d62013-04-11 16:29:06 +02006238static void i9xx_pfit_disable(struct intel_crtc *crtc)
6239{
6240 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006241 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter328d8e82013-05-08 10:36:31 +02006242
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006243 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006244 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006245
6246 assert_pipe_disabled(dev_priv, crtc->pipe);
6247
Daniel Vetter328d8e82013-05-08 10:36:31 +02006248 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6249 I915_READ(PFIT_CONTROL));
6250 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006251}
6252
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006253static void i9xx_crtc_disable(struct drm_crtc *crtc)
6254{
6255 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006256 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006257 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006258 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006259 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006260
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006261 /*
6262 * On gen2 planes are double buffered but the pipe isn't, so we must
6263 * wait for planes to fully turn off before disabling the pipe.
6264 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006265 if (IS_GEN2(dev))
6266 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006267
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006268 for_each_encoder_on_crtc(dev, crtc, encoder)
6269 encoder->disable(encoder);
6270
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006271 drm_crtc_vblank_off(crtc);
6272 assert_vblank_disabled(crtc);
6273
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006274 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006275
Daniel Vetter87476d62013-04-11 16:29:06 +02006276 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006277
Jesse Barnes89b667f2013-04-18 14:51:36 -07006278 for_each_encoder_on_crtc(dev, crtc, encoder)
6279 if (encoder->post_disable)
6280 encoder->post_disable(encoder);
6281
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006282 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006283 if (IS_CHERRYVIEW(dev))
6284 chv_disable_pll(dev_priv, pipe);
6285 else if (IS_VALLEYVIEW(dev))
6286 vlv_disable_pll(dev_priv, pipe);
6287 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006288 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006289 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006290
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006291 for_each_encoder_on_crtc(dev, crtc, encoder)
6292 if (encoder->post_pll_disable)
6293 encoder->post_pll_disable(encoder);
6294
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006295 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006296 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006297}
6298
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006299static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006300{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006301 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006302 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006303 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006304 enum intel_display_power_domain domain;
6305 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006306
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006307 if (!intel_crtc->active)
6308 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006309
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006310 if (to_intel_plane_state(crtc->primary->state)->visible) {
Daniel Vetter5a21b662016-05-24 17:13:53 +02006311 WARN_ON(intel_crtc->flip_work);
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006312
Ville Syrjälä2622a082016-03-09 19:07:26 +02006313 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006314
6315 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6316 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006317 }
6318
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006319 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006320
Ville Syrjälä78108b72016-05-27 20:59:19 +03006321 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6322 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006323
6324 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6325 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006326 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006327 crtc->enabled = false;
6328 crtc->state->connector_mask = 0;
6329 crtc->state->encoder_mask = 0;
6330
6331 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6332 encoder->base.crtc = NULL;
6333
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006334 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006335 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006336 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006337
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006338 domains = intel_crtc->enabled_power_domains;
6339 for_each_power_domain(domain, domains)
6340 intel_display_power_put(dev_priv, domain);
6341 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006342
6343 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6344 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006345}
6346
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006347/*
6348 * turn all crtc's off, but do not adjust state
6349 * This has to be paired with a call to intel_modeset_setup_hw_state.
6350 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006351int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006352{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006353 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006354 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006355 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006356
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006357 state = drm_atomic_helper_suspend(dev);
6358 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006359 if (ret)
6360 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006361 else
6362 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006363 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006364}
6365
Chris Wilsonea5b2132010-08-04 13:50:23 +01006366void intel_encoder_destroy(struct drm_encoder *encoder)
6367{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006368 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006369
Chris Wilsonea5b2132010-08-04 13:50:23 +01006370 drm_encoder_cleanup(encoder);
6371 kfree(intel_encoder);
6372}
6373
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006374/* Cross check the actual hw state with our own modeset state tracking (and it's
6375 * internal consistency). */
Daniel Vetter5a21b662016-05-24 17:13:53 +02006376static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006377{
Daniel Vetter5a21b662016-05-24 17:13:53 +02006378 struct drm_crtc *crtc = connector->base.state->crtc;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006379
6380 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6381 connector->base.base.id,
6382 connector->base.name);
6383
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006384 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006385 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter5a21b662016-05-24 17:13:53 +02006386 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006387
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006388 I915_STATE_WARN(!crtc,
6389 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006390
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006391 if (!crtc)
6392 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006393
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006394 I915_STATE_WARN(!crtc->state->active,
6395 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006396
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006397 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006398 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006399
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006400 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006401 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006402
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006403 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006404 "attached encoder crtc differs from connector crtc\n");
6405 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006406 I915_STATE_WARN(crtc && crtc->state->active,
6407 "attached crtc is active, but connector isn't\n");
Daniel Vetter5a21b662016-05-24 17:13:53 +02006408 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006409 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006410 }
6411}
6412
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006413int intel_connector_init(struct intel_connector *connector)
6414{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006415 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006416
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006417 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006418 return -ENOMEM;
6419
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006420 return 0;
6421}
6422
6423struct intel_connector *intel_connector_alloc(void)
6424{
6425 struct intel_connector *connector;
6426
6427 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6428 if (!connector)
6429 return NULL;
6430
6431 if (intel_connector_init(connector) < 0) {
6432 kfree(connector);
6433 return NULL;
6434 }
6435
6436 return connector;
6437}
6438
Daniel Vetterf0947c32012-07-02 13:10:34 +02006439/* Simple connector->get_hw_state implementation for encoders that support only
6440 * one connector and no cloning and hence the encoder state determines the state
6441 * of the connector. */
6442bool intel_connector_get_hw_state(struct intel_connector *connector)
6443{
Daniel Vetter24929352012-07-02 20:28:59 +02006444 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006445 struct intel_encoder *encoder = connector->encoder;
6446
6447 return encoder->get_hw_state(encoder, &pipe);
6448}
6449
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006450static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006451{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006452 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6453 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006454
6455 return 0;
6456}
6457
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006458static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006459 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006460{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006461 struct drm_atomic_state *state = pipe_config->base.state;
6462 struct intel_crtc *other_crtc;
6463 struct intel_crtc_state *other_crtc_state;
6464
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006465 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6466 pipe_name(pipe), pipe_config->fdi_lanes);
6467 if (pipe_config->fdi_lanes > 4) {
6468 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6469 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006470 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006471 }
6472
Paulo Zanonibafb6552013-11-02 21:07:44 -07006473 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006474 if (pipe_config->fdi_lanes > 2) {
6475 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6476 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006477 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006478 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006479 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006480 }
6481 }
6482
6483 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006484 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006485
6486 /* Ivybridge 3 pipe is really complicated */
6487 switch (pipe) {
6488 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006489 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006490 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006491 if (pipe_config->fdi_lanes <= 2)
6492 return 0;
6493
6494 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6495 other_crtc_state =
6496 intel_atomic_get_crtc_state(state, other_crtc);
6497 if (IS_ERR(other_crtc_state))
6498 return PTR_ERR(other_crtc_state);
6499
6500 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006501 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6502 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006503 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006504 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006505 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006506 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006507 if (pipe_config->fdi_lanes > 2) {
6508 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6509 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006510 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006511 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006512
6513 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6514 other_crtc_state =
6515 intel_atomic_get_crtc_state(state, other_crtc);
6516 if (IS_ERR(other_crtc_state))
6517 return PTR_ERR(other_crtc_state);
6518
6519 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006520 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006521 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006522 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006523 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006524 default:
6525 BUG();
6526 }
6527}
6528
Daniel Vettere29c22c2013-02-21 00:00:16 +01006529#define RETRY 1
6530static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006531 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006532{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006533 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006534 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006535 int lane, link_bw, fdi_dotclock, ret;
6536 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006537
Daniel Vettere29c22c2013-02-21 00:00:16 +01006538retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006539 /* FDI is a binary signal running at ~2.7GHz, encoding
6540 * each output octet as 10 bits. The actual frequency
6541 * is stored as a divider into a 100MHz clock, and the
6542 * mode pixel clock is stored in units of 1KHz.
6543 * Hence the bw of each lane in terms of the mode signal
6544 * is:
6545 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006546 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006547
Damien Lespiau241bfc32013-09-25 16:45:37 +01006548 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006549
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006550 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006551 pipe_config->pipe_bpp);
6552
6553 pipe_config->fdi_lanes = lane;
6554
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006555 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006556 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006557
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006558 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006559 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006560 pipe_config->pipe_bpp -= 2*3;
6561 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6562 pipe_config->pipe_bpp);
6563 needs_recompute = true;
6564 pipe_config->bw_constrained = true;
6565
6566 goto retry;
6567 }
6568
6569 if (needs_recompute)
6570 return RETRY;
6571
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006572 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006573}
6574
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006575static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6576 struct intel_crtc_state *pipe_config)
6577{
6578 if (pipe_config->pipe_bpp > 24)
6579 return false;
6580
6581 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006582 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006583 return true;
6584
6585 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006586 * We compare against max which means we must take
6587 * the increased cdclk requirement into account when
6588 * calculating the new cdclk.
6589 *
6590 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006591 */
6592 return ilk_pipe_pixel_rate(pipe_config) <=
6593 dev_priv->max_cdclk_freq * 95 / 100;
6594}
6595
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006596static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006597 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006598{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006599 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006600 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006601
Jani Nikulad330a952014-01-21 11:24:25 +02006602 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006603 hsw_crtc_supports_ips(crtc) &&
6604 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006605}
6606
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006607static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6608{
6609 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6610
6611 /* GDG double wide on either pipe, otherwise pipe A only */
6612 return INTEL_INFO(dev_priv)->gen < 4 &&
6613 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6614}
6615
Daniel Vettera43f6e02013-06-07 23:10:32 +02006616static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006617 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006618{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006619 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006620 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006621 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03006622 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01006623
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006624 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006625 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006626
6627 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006628 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006629 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006630 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006631 if (intel_crtc_supports_double_wide(crtc) &&
6632 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006633 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006634 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006635 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03006636 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006637
Ville Syrjäläf3261152016-05-24 21:34:18 +03006638 if (adjusted_mode->crtc_clock > clock_limit) {
6639 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6640 adjusted_mode->crtc_clock, clock_limit,
6641 yesno(pipe_config->double_wide));
6642 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006643 }
Chris Wilson89749352010-09-12 18:25:19 +01006644
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006645 /*
6646 * Pipe horizontal size must be even in:
6647 * - DVO ganged mode
6648 * - LVDS dual channel mode
6649 * - Double wide pipe
6650 */
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006651 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006652 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6653 pipe_config->pipe_src_w &= ~1;
6654
Damien Lespiau8693a822013-05-03 18:48:11 +01006655 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6656 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006657 */
6658 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006659 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006660 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006661
Damien Lespiauf5adf942013-06-24 18:29:34 +01006662 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006663 hsw_compute_ips_config(crtc, pipe_config);
6664
Daniel Vetter877d48d2013-04-19 11:24:43 +02006665 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006666 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006667
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006668 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006669}
6670
Ville Syrjälä1652d192015-03-31 14:12:01 +03006671static int skylake_get_display_clock_speed(struct drm_device *dev)
6672{
6673 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläea617912016-05-13 23:41:24 +03006674 uint32_t cdctl;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006675
Ville Syrjäläea617912016-05-13 23:41:24 +03006676 skl_dpll0_update(dev_priv);
6677
Ville Syrjälä63911d72016-05-13 23:41:32 +03006678 if (dev_priv->cdclk_pll.vco == 0)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006679 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006680
Ville Syrjäläea617912016-05-13 23:41:24 +03006681 cdctl = I915_READ(CDCLK_CTL);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006682
Ville Syrjälä63911d72016-05-13 23:41:32 +03006683 if (dev_priv->cdclk_pll.vco == 8640000) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006684 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6685 case CDCLK_FREQ_450_432:
6686 return 432000;
6687 case CDCLK_FREQ_337_308:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006688 return 308571;
Ville Syrjäläea617912016-05-13 23:41:24 +03006689 case CDCLK_FREQ_540:
6690 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006691 case CDCLK_FREQ_675_617:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006692 return 617143;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006693 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006694 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006695 }
6696 } else {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006697 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6698 case CDCLK_FREQ_450_432:
6699 return 450000;
6700 case CDCLK_FREQ_337_308:
6701 return 337500;
Ville Syrjäläea617912016-05-13 23:41:24 +03006702 case CDCLK_FREQ_540:
6703 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006704 case CDCLK_FREQ_675_617:
6705 return 675000;
6706 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006707 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006708 }
6709 }
6710
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006711 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006712}
6713
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006714static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6715{
6716 u32 val;
6717
6718 dev_priv->cdclk_pll.ref = 19200;
Imre Deak1c3f7702016-05-24 15:38:32 +03006719 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006720
6721 val = I915_READ(BXT_DE_PLL_ENABLE);
Imre Deak1c3f7702016-05-24 15:38:32 +03006722 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006723 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006724
Imre Deak1c3f7702016-05-24 15:38:32 +03006725 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6726 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006727
6728 val = I915_READ(BXT_DE_PLL_CTL);
6729 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6730 dev_priv->cdclk_pll.ref;
6731}
6732
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006733static int broxton_get_display_clock_speed(struct drm_device *dev)
6734{
6735 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf5986242016-05-13 23:41:37 +03006736 u32 divider;
6737 int div, vco;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006738
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006739 bxt_de_pll_update(dev_priv);
6740
Ville Syrjäläf5986242016-05-13 23:41:37 +03006741 vco = dev_priv->cdclk_pll.vco;
6742 if (vco == 0)
6743 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006744
Ville Syrjäläf5986242016-05-13 23:41:37 +03006745 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006746
Ville Syrjäläf5986242016-05-13 23:41:37 +03006747 switch (divider) {
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006748 case BXT_CDCLK_CD2X_DIV_SEL_1:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006749 div = 2;
6750 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006751 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006752 div = 3;
6753 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006754 case BXT_CDCLK_CD2X_DIV_SEL_2:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006755 div = 4;
6756 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006757 case BXT_CDCLK_CD2X_DIV_SEL_4:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006758 div = 8;
6759 break;
6760 default:
6761 MISSING_CASE(divider);
6762 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006763 }
6764
Ville Syrjäläf5986242016-05-13 23:41:37 +03006765 return DIV_ROUND_CLOSEST(vco, div);
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006766}
6767
Ville Syrjälä1652d192015-03-31 14:12:01 +03006768static int broadwell_get_display_clock_speed(struct drm_device *dev)
6769{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006770 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006771 uint32_t lcpll = I915_READ(LCPLL_CTL);
6772 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6773
6774 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6775 return 800000;
6776 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6777 return 450000;
6778 else if (freq == LCPLL_CLK_FREQ_450)
6779 return 450000;
6780 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6781 return 540000;
6782 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6783 return 337500;
6784 else
6785 return 675000;
6786}
6787
6788static int haswell_get_display_clock_speed(struct drm_device *dev)
6789{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006790 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006791 uint32_t lcpll = I915_READ(LCPLL_CTL);
6792 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6793
6794 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6795 return 800000;
6796 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6797 return 450000;
6798 else if (freq == LCPLL_CLK_FREQ_450)
6799 return 450000;
6800 else if (IS_HSW_ULT(dev))
6801 return 337500;
6802 else
6803 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006804}
6805
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006806static int valleyview_get_display_clock_speed(struct drm_device *dev)
6807{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006808 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6809 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006810}
6811
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006812static int ilk_get_display_clock_speed(struct drm_device *dev)
6813{
6814 return 450000;
6815}
6816
Jesse Barnese70236a2009-09-21 10:42:27 -07006817static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006818{
Jesse Barnese70236a2009-09-21 10:42:27 -07006819 return 400000;
6820}
Jesse Barnes79e53942008-11-07 14:24:08 -08006821
Jesse Barnese70236a2009-09-21 10:42:27 -07006822static int i915_get_display_clock_speed(struct drm_device *dev)
6823{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006824 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006825}
Jesse Barnes79e53942008-11-07 14:24:08 -08006826
Jesse Barnese70236a2009-09-21 10:42:27 -07006827static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6828{
6829 return 200000;
6830}
Jesse Barnes79e53942008-11-07 14:24:08 -08006831
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006832static int pnv_get_display_clock_speed(struct drm_device *dev)
6833{
6834 u16 gcfgc = 0;
6835
6836 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6837
6838 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6839 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006840 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006841 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006842 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006843 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006844 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006845 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6846 return 200000;
6847 default:
6848 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6849 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006850 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006851 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006852 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006853 }
6854}
6855
Jesse Barnese70236a2009-09-21 10:42:27 -07006856static int i915gm_get_display_clock_speed(struct drm_device *dev)
6857{
6858 u16 gcfgc = 0;
6859
6860 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6861
6862 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006863 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006864 else {
6865 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6866 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006867 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006868 default:
6869 case GC_DISPLAY_CLOCK_190_200_MHZ:
6870 return 190000;
6871 }
6872 }
6873}
Jesse Barnes79e53942008-11-07 14:24:08 -08006874
Jesse Barnese70236a2009-09-21 10:42:27 -07006875static int i865_get_display_clock_speed(struct drm_device *dev)
6876{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006877 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006878}
6879
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006880static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006881{
6882 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006883
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006884 /*
6885 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6886 * encoding is different :(
6887 * FIXME is this the right way to detect 852GM/852GMV?
6888 */
6889 if (dev->pdev->revision == 0x1)
6890 return 133333;
6891
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006892 pci_bus_read_config_word(dev->pdev->bus,
6893 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6894
Jesse Barnese70236a2009-09-21 10:42:27 -07006895 /* Assume that the hardware is in the high speed state. This
6896 * should be the default.
6897 */
6898 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6899 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006900 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006901 case GC_CLOCK_100_200:
6902 return 200000;
6903 case GC_CLOCK_166_250:
6904 return 250000;
6905 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006906 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006907 case GC_CLOCK_133_266:
6908 case GC_CLOCK_133_266_2:
6909 case GC_CLOCK_166_266:
6910 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006911 }
6912
6913 /* Shouldn't happen */
6914 return 0;
6915}
6916
6917static int i830_get_display_clock_speed(struct drm_device *dev)
6918{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006919 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006920}
6921
Ville Syrjälä34edce22015-05-22 11:22:33 +03006922static unsigned int intel_hpll_vco(struct drm_device *dev)
6923{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006924 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä34edce22015-05-22 11:22:33 +03006925 static const unsigned int blb_vco[8] = {
6926 [0] = 3200000,
6927 [1] = 4000000,
6928 [2] = 5333333,
6929 [3] = 4800000,
6930 [4] = 6400000,
6931 };
6932 static const unsigned int pnv_vco[8] = {
6933 [0] = 3200000,
6934 [1] = 4000000,
6935 [2] = 5333333,
6936 [3] = 4800000,
6937 [4] = 2666667,
6938 };
6939 static const unsigned int cl_vco[8] = {
6940 [0] = 3200000,
6941 [1] = 4000000,
6942 [2] = 5333333,
6943 [3] = 6400000,
6944 [4] = 3333333,
6945 [5] = 3566667,
6946 [6] = 4266667,
6947 };
6948 static const unsigned int elk_vco[8] = {
6949 [0] = 3200000,
6950 [1] = 4000000,
6951 [2] = 5333333,
6952 [3] = 4800000,
6953 };
6954 static const unsigned int ctg_vco[8] = {
6955 [0] = 3200000,
6956 [1] = 4000000,
6957 [2] = 5333333,
6958 [3] = 6400000,
6959 [4] = 2666667,
6960 [5] = 4266667,
6961 };
6962 const unsigned int *vco_table;
6963 unsigned int vco;
6964 uint8_t tmp = 0;
6965
6966 /* FIXME other chipsets? */
6967 if (IS_GM45(dev))
6968 vco_table = ctg_vco;
6969 else if (IS_G4X(dev))
6970 vco_table = elk_vco;
6971 else if (IS_CRESTLINE(dev))
6972 vco_table = cl_vco;
6973 else if (IS_PINEVIEW(dev))
6974 vco_table = pnv_vco;
6975 else if (IS_G33(dev))
6976 vco_table = blb_vco;
6977 else
6978 return 0;
6979
6980 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6981
6982 vco = vco_table[tmp & 0x7];
6983 if (vco == 0)
6984 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6985 else
6986 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6987
6988 return vco;
6989}
6990
6991static int gm45_get_display_clock_speed(struct drm_device *dev)
6992{
6993 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6994 uint16_t tmp = 0;
6995
6996 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6997
6998 cdclk_sel = (tmp >> 12) & 0x1;
6999
7000 switch (vco) {
7001 case 2666667:
7002 case 4000000:
7003 case 5333333:
7004 return cdclk_sel ? 333333 : 222222;
7005 case 3200000:
7006 return cdclk_sel ? 320000 : 228571;
7007 default:
7008 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7009 return 222222;
7010 }
7011}
7012
7013static int i965gm_get_display_clock_speed(struct drm_device *dev)
7014{
7015 static const uint8_t div_3200[] = { 16, 10, 8 };
7016 static const uint8_t div_4000[] = { 20, 12, 10 };
7017 static const uint8_t div_5333[] = { 24, 16, 14 };
7018 const uint8_t *div_table;
7019 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7020 uint16_t tmp = 0;
7021
7022 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7023
7024 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7025
7026 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7027 goto fail;
7028
7029 switch (vco) {
7030 case 3200000:
7031 div_table = div_3200;
7032 break;
7033 case 4000000:
7034 div_table = div_4000;
7035 break;
7036 case 5333333:
7037 div_table = div_5333;
7038 break;
7039 default:
7040 goto fail;
7041 }
7042
7043 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7044
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007045fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007046 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7047 return 200000;
7048}
7049
7050static int g33_get_display_clock_speed(struct drm_device *dev)
7051{
7052 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7053 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7054 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7055 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7056 const uint8_t *div_table;
7057 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7058 uint16_t tmp = 0;
7059
7060 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7061
7062 cdclk_sel = (tmp >> 4) & 0x7;
7063
7064 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7065 goto fail;
7066
7067 switch (vco) {
7068 case 3200000:
7069 div_table = div_3200;
7070 break;
7071 case 4000000:
7072 div_table = div_4000;
7073 break;
7074 case 4800000:
7075 div_table = div_4800;
7076 break;
7077 case 5333333:
7078 div_table = div_5333;
7079 break;
7080 default:
7081 goto fail;
7082 }
7083
7084 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7085
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007086fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007087 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7088 return 190476;
7089}
7090
Zhenyu Wang2c072452009-06-05 15:38:42 +08007091static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007092intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007093{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007094 while (*num > DATA_LINK_M_N_MASK ||
7095 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007096 *num >>= 1;
7097 *den >>= 1;
7098 }
7099}
7100
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007101static void compute_m_n(unsigned int m, unsigned int n,
7102 uint32_t *ret_m, uint32_t *ret_n)
7103{
7104 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7105 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7106 intel_reduce_m_n_ratio(ret_m, ret_n);
7107}
7108
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007109void
7110intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7111 int pixel_clock, int link_clock,
7112 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007113{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007114 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007115
7116 compute_m_n(bits_per_pixel * pixel_clock,
7117 link_clock * nlanes * 8,
7118 &m_n->gmch_m, &m_n->gmch_n);
7119
7120 compute_m_n(pixel_clock, link_clock,
7121 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007122}
7123
Chris Wilsona7615032011-01-12 17:04:08 +00007124static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7125{
Jani Nikulad330a952014-01-21 11:24:25 +02007126 if (i915.panel_use_ssc >= 0)
7127 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007128 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007129 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007130}
7131
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007132static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007133{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007134 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007135}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007136
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007137static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7138{
7139 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007140}
7141
Daniel Vetterf47709a2013-03-28 10:42:02 +01007142static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007143 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007144 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08007145{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007146 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007147 u32 fp, fp2 = 0;
7148
7149 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007150 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007151 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007152 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007153 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007154 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007155 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007156 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007157 }
7158
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007159 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007160
Daniel Vetterf47709a2013-03-28 10:42:02 +01007161 crtc->lowfreq_avail = false;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007162 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007163 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007164 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007165 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007166 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007167 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007168 }
7169}
7170
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007171static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7172 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007173{
7174 u32 reg_val;
7175
7176 /*
7177 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7178 * and set it to a reasonable value instead.
7179 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007180 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007181 reg_val &= 0xffffff00;
7182 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007183 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007184
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007185 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007186 reg_val &= 0x8cffffff;
7187 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007188 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007189
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007190 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007191 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007192 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007193
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007194 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007195 reg_val &= 0x00ffffff;
7196 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007197 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007198}
7199
Daniel Vetterb5518422013-05-03 11:49:48 +02007200static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7201 struct intel_link_m_n *m_n)
7202{
7203 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007204 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007205 int pipe = crtc->pipe;
7206
Daniel Vettere3b95f12013-05-03 11:49:49 +02007207 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7208 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7209 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7210 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007211}
7212
7213static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007214 struct intel_link_m_n *m_n,
7215 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007216{
7217 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007218 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007219 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007220 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007221
7222 if (INTEL_INFO(dev)->gen >= 5) {
7223 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7224 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7225 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7226 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007227 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7228 * for gen < 8) and if DRRS is supported (to make sure the
7229 * registers are not unnecessarily accessed).
7230 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307231 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007232 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007233 I915_WRITE(PIPE_DATA_M2(transcoder),
7234 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7235 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7236 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7237 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7238 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007239 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007240 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7241 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7242 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7243 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007244 }
7245}
7246
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307247void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007248{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307249 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7250
7251 if (m_n == M1_N1) {
7252 dp_m_n = &crtc->config->dp_m_n;
7253 dp_m2_n2 = &crtc->config->dp_m2_n2;
7254 } else if (m_n == M2_N2) {
7255
7256 /*
7257 * M2_N2 registers are not supported. Hence m2_n2 divider value
7258 * needs to be programmed into M1_N1.
7259 */
7260 dp_m_n = &crtc->config->dp_m2_n2;
7261 } else {
7262 DRM_ERROR("Unsupported divider value\n");
7263 return;
7264 }
7265
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007266 if (crtc->config->has_pch_encoder)
7267 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007268 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307269 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007270}
7271
Daniel Vetter251ac862015-06-18 10:30:24 +02007272static void vlv_compute_dpll(struct intel_crtc *crtc,
7273 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007274{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007275 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007276 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007277 if (crtc->pipe != PIPE_A)
7278 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007279
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007280 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007281 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007282 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7283 DPLL_EXT_BUFFER_ENABLE_VLV;
7284
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007285 pipe_config->dpll_hw_state.dpll_md =
7286 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7287}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007288
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007289static void chv_compute_dpll(struct intel_crtc *crtc,
7290 struct intel_crtc_state *pipe_config)
7291{
7292 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007293 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007294 if (crtc->pipe != PIPE_A)
7295 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7296
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007297 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007298 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007299 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7300
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007301 pipe_config->dpll_hw_state.dpll_md =
7302 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007303}
7304
Ville Syrjäläd288f652014-10-28 13:20:22 +02007305static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007306 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007307{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007308 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007309 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007310 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007311 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007312 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007313 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007314
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007315 /* Enable Refclk */
7316 I915_WRITE(DPLL(pipe),
7317 pipe_config->dpll_hw_state.dpll &
7318 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7319
7320 /* No need to actually set up the DPLL with DSI */
7321 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7322 return;
7323
Ville Syrjäläa5805162015-05-26 20:42:30 +03007324 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007325
Ville Syrjäläd288f652014-10-28 13:20:22 +02007326 bestn = pipe_config->dpll.n;
7327 bestm1 = pipe_config->dpll.m1;
7328 bestm2 = pipe_config->dpll.m2;
7329 bestp1 = pipe_config->dpll.p1;
7330 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007331
Jesse Barnes89b667f2013-04-18 14:51:36 -07007332 /* See eDP HDMI DPIO driver vbios notes doc */
7333
7334 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007335 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007336 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007337
7338 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007339 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007340
7341 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007342 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007343 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007344 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007345
7346 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007347 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007348
7349 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007350 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7351 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7352 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007353 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007354
7355 /*
7356 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7357 * but we don't support that).
7358 * Note: don't use the DAC post divider as it seems unstable.
7359 */
7360 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007361 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007362
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007363 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007364 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007365
Jesse Barnes89b667f2013-04-18 14:51:36 -07007366 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007367 if (pipe_config->port_clock == 162000 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007368 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7369 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007370 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007371 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007372 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007373 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007374 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007375
Ville Syrjälä37a56502016-06-22 21:57:04 +03007376 if (intel_crtc_has_dp_encoder(pipe_config)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007377 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007378 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007379 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007380 0x0df40000);
7381 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007382 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007383 0x0df70000);
7384 } else { /* HDMI or VGA */
7385 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007386 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007387 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007388 0x0df70000);
7389 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007390 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007391 0x0df40000);
7392 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007393
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007394 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007395 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ville Syrjälä2210ce72016-06-22 21:57:05 +03007396 if (intel_crtc_has_dp_encoder(crtc->config))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007397 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007398 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007399
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007400 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007401 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007402}
7403
Ville Syrjäläd288f652014-10-28 13:20:22 +02007404static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007405 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007406{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007407 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007408 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007409 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007410 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307411 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007412 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307413 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307414 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007415
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007416 /* Enable Refclk and SSC */
7417 I915_WRITE(DPLL(pipe),
7418 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7419
7420 /* No need to actually set up the DPLL with DSI */
7421 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7422 return;
7423
Ville Syrjäläd288f652014-10-28 13:20:22 +02007424 bestn = pipe_config->dpll.n;
7425 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7426 bestm1 = pipe_config->dpll.m1;
7427 bestm2 = pipe_config->dpll.m2 >> 22;
7428 bestp1 = pipe_config->dpll.p1;
7429 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307430 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307431 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307432 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007433
Ville Syrjäläa5805162015-05-26 20:42:30 +03007434 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007435
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007436 /* p1 and p2 divider */
7437 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7438 5 << DPIO_CHV_S1_DIV_SHIFT |
7439 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7440 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7441 1 << DPIO_CHV_K_DIV_SHIFT);
7442
7443 /* Feedback post-divider - m2 */
7444 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7445
7446 /* Feedback refclk divider - n and m1 */
7447 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7448 DPIO_CHV_M1_DIV_BY_2 |
7449 1 << DPIO_CHV_N_DIV_SHIFT);
7450
7451 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007452 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007453
7454 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307455 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7456 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7457 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7458 if (bestm2_frac)
7459 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7460 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007461
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307462 /* Program digital lock detect threshold */
7463 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7464 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7465 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7466 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7467 if (!bestm2_frac)
7468 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7469 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7470
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007471 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307472 if (vco == 5400000) {
7473 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7474 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7475 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7476 tribuf_calcntr = 0x9;
7477 } else if (vco <= 6200000) {
7478 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7479 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7480 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7481 tribuf_calcntr = 0x9;
7482 } else if (vco <= 6480000) {
7483 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7484 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7485 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7486 tribuf_calcntr = 0x8;
7487 } else {
7488 /* Not supported. Apply the same limits as in the max case */
7489 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7490 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7491 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7492 tribuf_calcntr = 0;
7493 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007494 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7495
Ville Syrjälä968040b2015-03-11 22:52:08 +02007496 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307497 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7498 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7499 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7500
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007501 /* AFC Recal */
7502 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7503 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7504 DPIO_AFC_RECAL);
7505
Ville Syrjäläa5805162015-05-26 20:42:30 +03007506 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007507}
7508
Ville Syrjäläd288f652014-10-28 13:20:22 +02007509/**
7510 * vlv_force_pll_on - forcibly enable just the PLL
7511 * @dev_priv: i915 private structure
7512 * @pipe: pipe PLL to enable
7513 * @dpll: PLL configuration
7514 *
7515 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7516 * in cases where we need the PLL enabled even when @pipe is not going to
7517 * be enabled.
7518 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007519int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7520 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007521{
7522 struct intel_crtc *crtc =
7523 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007524 struct intel_crtc_state *pipe_config;
7525
7526 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7527 if (!pipe_config)
7528 return -ENOMEM;
7529
7530 pipe_config->base.crtc = &crtc->base;
7531 pipe_config->pixel_multiplier = 1;
7532 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007533
7534 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007535 chv_compute_dpll(crtc, pipe_config);
7536 chv_prepare_pll(crtc, pipe_config);
7537 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007538 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007539 vlv_compute_dpll(crtc, pipe_config);
7540 vlv_prepare_pll(crtc, pipe_config);
7541 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007542 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007543
7544 kfree(pipe_config);
7545
7546 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007547}
7548
7549/**
7550 * vlv_force_pll_off - forcibly disable just the PLL
7551 * @dev_priv: i915 private structure
7552 * @pipe: pipe PLL to disable
7553 *
7554 * Disable the PLL for @pipe. To be used in cases where we need
7555 * the PLL enabled even when @pipe is not going to be enabled.
7556 */
7557void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7558{
7559 if (IS_CHERRYVIEW(dev))
7560 chv_disable_pll(to_i915(dev), pipe);
7561 else
7562 vlv_disable_pll(to_i915(dev), pipe);
7563}
7564
Daniel Vetter251ac862015-06-18 10:30:24 +02007565static void i9xx_compute_dpll(struct intel_crtc *crtc,
7566 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007567 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007568{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007569 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007570 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007571 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007572 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007573
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007574 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307575
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007576 dpll = DPLL_VGA_MODE_DIS;
7577
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007578 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007579 dpll |= DPLLB_MODE_LVDS;
7580 else
7581 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007582
Daniel Vetteref1b4602013-06-01 17:17:04 +02007583 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007584 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007585 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007586 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007587
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03007588 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7589 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007590 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007591
Ville Syrjälä37a56502016-06-22 21:57:04 +03007592 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007593 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007594
7595 /* compute bitmask from p1 value */
7596 if (IS_PINEVIEW(dev))
7597 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7598 else {
7599 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7600 if (IS_G4X(dev) && reduced_clock)
7601 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7602 }
7603 switch (clock->p2) {
7604 case 5:
7605 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7606 break;
7607 case 7:
7608 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7609 break;
7610 case 10:
7611 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7612 break;
7613 case 14:
7614 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7615 break;
7616 }
7617 if (INTEL_INFO(dev)->gen >= 4)
7618 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7619
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007620 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007621 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007622 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007623 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007624 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7625 else
7626 dpll |= PLL_REF_INPUT_DREFCLK;
7627
7628 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007629 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007630
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007631 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007632 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007633 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007634 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007635 }
7636}
7637
Daniel Vetter251ac862015-06-18 10:30:24 +02007638static void i8xx_compute_dpll(struct intel_crtc *crtc,
7639 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007640 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007641{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007642 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007643 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007644 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007645 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007646
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007647 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307648
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007649 dpll = DPLL_VGA_MODE_DIS;
7650
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007651 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007652 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7653 } else {
7654 if (clock->p1 == 2)
7655 dpll |= PLL_P1_DIVIDE_BY_TWO;
7656 else
7657 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7658 if (clock->p2 == 4)
7659 dpll |= PLL_P2_DIVIDE_BY_4;
7660 }
7661
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007662 if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007663 dpll |= DPLL_DVO_2X_MODE;
7664
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007665 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007666 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007667 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7668 else
7669 dpll |= PLL_REF_INPUT_DREFCLK;
7670
7671 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007672 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007673}
7674
Daniel Vetter8a654f32013-06-01 17:16:22 +02007675static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007676{
7677 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007678 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007679 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007680 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007681 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007682 uint32_t crtc_vtotal, crtc_vblank_end;
7683 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007684
7685 /* We need to be careful not to changed the adjusted mode, for otherwise
7686 * the hw state checker will get angry at the mismatch. */
7687 crtc_vtotal = adjusted_mode->crtc_vtotal;
7688 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007689
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007690 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007691 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007692 crtc_vtotal -= 1;
7693 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007694
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007695 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007696 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7697 else
7698 vsyncshift = adjusted_mode->crtc_hsync_start -
7699 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007700 if (vsyncshift < 0)
7701 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007702 }
7703
7704 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007705 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007706
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007707 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007708 (adjusted_mode->crtc_hdisplay - 1) |
7709 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007710 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007711 (adjusted_mode->crtc_hblank_start - 1) |
7712 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007713 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007714 (adjusted_mode->crtc_hsync_start - 1) |
7715 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7716
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007717 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007718 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007719 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007720 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007721 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007722 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007723 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007724 (adjusted_mode->crtc_vsync_start - 1) |
7725 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7726
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007727 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7728 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7729 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7730 * bits. */
7731 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7732 (pipe == PIPE_B || pipe == PIPE_C))
7733 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7734
Jani Nikulabc58be62016-03-18 17:05:39 +02007735}
7736
7737static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7738{
7739 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007740 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007741 enum pipe pipe = intel_crtc->pipe;
7742
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007743 /* pipesrc controls the size that is scaled from, which should
7744 * always be the user's requested size.
7745 */
7746 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007747 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7748 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007749}
7750
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007751static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007752 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007753{
7754 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007755 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007756 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7757 uint32_t tmp;
7758
7759 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007760 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7761 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007762 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007763 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7764 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007765 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007766 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7767 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007768
7769 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007770 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7771 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007772 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007773 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7774 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007775 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007776 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7777 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007778
7779 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007780 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7781 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7782 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007783 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007784}
7785
7786static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7787 struct intel_crtc_state *pipe_config)
7788{
7789 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007790 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007791 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007792
7793 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007794 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7795 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7796
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007797 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7798 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007799}
7800
Daniel Vetterf6a83282014-02-11 15:28:57 -08007801void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007802 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007803{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007804 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7805 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7806 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7807 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007808
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007809 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7810 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7811 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7812 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007813
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007814 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007815 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007816
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007817 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7818 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007819
7820 mode->hsync = drm_mode_hsync(mode);
7821 mode->vrefresh = drm_mode_vrefresh(mode);
7822 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007823}
7824
Daniel Vetter84b046f2013-02-19 18:48:54 +01007825static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7826{
7827 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007828 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter84b046f2013-02-19 18:48:54 +01007829 uint32_t pipeconf;
7830
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007831 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007832
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007833 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7834 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7835 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007836
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007837 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007838 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007839
Daniel Vetterff9ce462013-04-24 14:57:17 +02007840 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007841 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007842 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007843 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007844 pipeconf |= PIPECONF_DITHER_EN |
7845 PIPECONF_DITHER_TYPE_SP;
7846
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007847 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007848 case 18:
7849 pipeconf |= PIPECONF_6BPC;
7850 break;
7851 case 24:
7852 pipeconf |= PIPECONF_8BPC;
7853 break;
7854 case 30:
7855 pipeconf |= PIPECONF_10BPC;
7856 break;
7857 default:
7858 /* Case prevented by intel_choose_pipe_bpp_dither. */
7859 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007860 }
7861 }
7862
7863 if (HAS_PIPE_CXSR(dev)) {
7864 if (intel_crtc->lowfreq_avail) {
7865 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7866 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7867 } else {
7868 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007869 }
7870 }
7871
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007872 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007873 if (INTEL_INFO(dev)->gen < 4 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007874 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007875 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7876 else
7877 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7878 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007879 pipeconf |= PIPECONF_PROGRESSIVE;
7880
Wayne Boyer666a4532015-12-09 12:29:35 -08007881 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7882 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007883 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007884
Daniel Vetter84b046f2013-02-19 18:48:54 +01007885 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7886 POSTING_READ(PIPECONF(intel_crtc->pipe));
7887}
7888
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007889static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7890 struct intel_crtc_state *crtc_state)
7891{
7892 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007893 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007894 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007895 int refclk = 48000;
7896
7897 memset(&crtc_state->dpll_hw_state, 0,
7898 sizeof(crtc_state->dpll_hw_state));
7899
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007900 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007901 if (intel_panel_use_ssc(dev_priv)) {
7902 refclk = dev_priv->vbt.lvds_ssc_freq;
7903 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7904 }
7905
7906 limit = &intel_limits_i8xx_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007907 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007908 limit = &intel_limits_i8xx_dvo;
7909 } else {
7910 limit = &intel_limits_i8xx_dac;
7911 }
7912
7913 if (!crtc_state->clock_set &&
7914 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7915 refclk, NULL, &crtc_state->dpll)) {
7916 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7917 return -EINVAL;
7918 }
7919
7920 i8xx_compute_dpll(crtc, crtc_state, NULL);
7921
7922 return 0;
7923}
7924
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007925static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7926 struct intel_crtc_state *crtc_state)
7927{
7928 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007929 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007930 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007931 int refclk = 96000;
7932
7933 memset(&crtc_state->dpll_hw_state, 0,
7934 sizeof(crtc_state->dpll_hw_state));
7935
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007936 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007937 if (intel_panel_use_ssc(dev_priv)) {
7938 refclk = dev_priv->vbt.lvds_ssc_freq;
7939 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7940 }
7941
7942 if (intel_is_dual_link_lvds(dev))
7943 limit = &intel_limits_g4x_dual_channel_lvds;
7944 else
7945 limit = &intel_limits_g4x_single_channel_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007946 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7947 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007948 limit = &intel_limits_g4x_hdmi;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007949 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007950 limit = &intel_limits_g4x_sdvo;
7951 } else {
7952 /* The option is for other outputs */
7953 limit = &intel_limits_i9xx_sdvo;
7954 }
7955
7956 if (!crtc_state->clock_set &&
7957 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7958 refclk, NULL, &crtc_state->dpll)) {
7959 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7960 return -EINVAL;
7961 }
7962
7963 i9xx_compute_dpll(crtc, crtc_state, NULL);
7964
7965 return 0;
7966}
7967
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007968static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7969 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007970{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007971 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007972 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007973 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007974 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007975
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007976 memset(&crtc_state->dpll_hw_state, 0,
7977 sizeof(crtc_state->dpll_hw_state));
7978
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007979 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007980 if (intel_panel_use_ssc(dev_priv)) {
7981 refclk = dev_priv->vbt.lvds_ssc_freq;
7982 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7983 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007984
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007985 limit = &intel_limits_pineview_lvds;
7986 } else {
7987 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007988 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007989
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007990 if (!crtc_state->clock_set &&
7991 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7992 refclk, NULL, &crtc_state->dpll)) {
7993 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7994 return -EINVAL;
7995 }
7996
7997 i9xx_compute_dpll(crtc, crtc_state, NULL);
7998
7999 return 0;
8000}
8001
8002static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8003 struct intel_crtc_state *crtc_state)
8004{
8005 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008006 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008007 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008008 int refclk = 96000;
8009
8010 memset(&crtc_state->dpll_hw_state, 0,
8011 sizeof(crtc_state->dpll_hw_state));
8012
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008013 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008014 if (intel_panel_use_ssc(dev_priv)) {
8015 refclk = dev_priv->vbt.lvds_ssc_freq;
8016 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008017 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008018
8019 limit = &intel_limits_i9xx_lvds;
8020 } else {
8021 limit = &intel_limits_i9xx_sdvo;
8022 }
8023
8024 if (!crtc_state->clock_set &&
8025 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8026 refclk, NULL, &crtc_state->dpll)) {
8027 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8028 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008029 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008030
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008031 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07008032
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008033 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008034}
8035
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008036static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8037 struct intel_crtc_state *crtc_state)
8038{
8039 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008040 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008041
8042 memset(&crtc_state->dpll_hw_state, 0,
8043 sizeof(crtc_state->dpll_hw_state));
8044
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008045 if (!crtc_state->clock_set &&
8046 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8047 refclk, NULL, &crtc_state->dpll)) {
8048 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8049 return -EINVAL;
8050 }
8051
8052 chv_compute_dpll(crtc, crtc_state);
8053
8054 return 0;
8055}
8056
8057static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8058 struct intel_crtc_state *crtc_state)
8059{
8060 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008061 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008062
8063 memset(&crtc_state->dpll_hw_state, 0,
8064 sizeof(crtc_state->dpll_hw_state));
8065
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008066 if (!crtc_state->clock_set &&
8067 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8068 refclk, NULL, &crtc_state->dpll)) {
8069 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8070 return -EINVAL;
8071 }
8072
8073 vlv_compute_dpll(crtc, crtc_state);
8074
8075 return 0;
8076}
8077
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008078static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008079 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008080{
8081 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008082 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008083 uint32_t tmp;
8084
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008085 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8086 return;
8087
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008088 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008089 if (!(tmp & PFIT_ENABLE))
8090 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008091
Daniel Vetter06922822013-07-11 13:35:40 +02008092 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008093 if (INTEL_INFO(dev)->gen < 4) {
8094 if (crtc->pipe != PIPE_B)
8095 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008096 } else {
8097 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8098 return;
8099 }
8100
Daniel Vetter06922822013-07-11 13:35:40 +02008101 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008102 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008103}
8104
Jesse Barnesacbec812013-09-20 11:29:32 -07008105static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008106 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008107{
8108 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008109 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesacbec812013-09-20 11:29:32 -07008110 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008111 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07008112 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008113 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008114
Ville Syrjäläb5219732016-03-15 16:40:01 +02008115 /* In case of DSI, DPLL will not be used */
8116 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308117 return;
8118
Ville Syrjäläa5805162015-05-26 20:42:30 +03008119 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008120 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008121 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008122
8123 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8124 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8125 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8126 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8127 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8128
Imre Deakdccbea32015-06-22 23:35:51 +03008129 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008130}
8131
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008132static void
8133i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8134 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008135{
8136 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008137 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008138 u32 val, base, offset;
8139 int pipe = crtc->pipe, plane = crtc->plane;
8140 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008141 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008142 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008143 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008144
Damien Lespiau42a7b082015-02-05 19:35:13 +00008145 val = I915_READ(DSPCNTR(plane));
8146 if (!(val & DISPLAY_PLANE_ENABLE))
8147 return;
8148
Damien Lespiaud9806c92015-01-21 14:07:19 +00008149 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008150 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008151 DRM_DEBUG_KMS("failed to alloc fb\n");
8152 return;
8153 }
8154
Damien Lespiau1b842c82015-01-21 13:50:54 +00008155 fb = &intel_fb->base;
8156
Daniel Vetter18c52472015-02-10 17:16:09 +00008157 if (INTEL_INFO(dev)->gen >= 4) {
8158 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008159 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008160 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8161 }
8162 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008163
8164 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008165 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008166 fb->pixel_format = fourcc;
8167 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008168
8169 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008170 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008171 offset = I915_READ(DSPTILEOFF(plane));
8172 else
8173 offset = I915_READ(DSPLINOFF(plane));
8174 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8175 } else {
8176 base = I915_READ(DSPADDR(plane));
8177 }
8178 plane_config->base = base;
8179
8180 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008181 fb->width = ((val >> 16) & 0xfff) + 1;
8182 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008183
8184 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008185 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008186
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008187 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008188 fb->pixel_format,
8189 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008190
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008191 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008192
Damien Lespiau2844a922015-01-20 12:51:48 +00008193 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8194 pipe_name(pipe), plane, fb->width, fb->height,
8195 fb->bits_per_pixel, base, fb->pitches[0],
8196 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008197
Damien Lespiau2d140302015-02-05 17:22:18 +00008198 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008199}
8200
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008201static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008202 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008203{
8204 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008205 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008206 int pipe = pipe_config->cpu_transcoder;
8207 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008208 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008209 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008210 int refclk = 100000;
8211
Ville Syrjäläb5219732016-03-15 16:40:01 +02008212 /* In case of DSI, DPLL will not be used */
8213 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8214 return;
8215
Ville Syrjäläa5805162015-05-26 20:42:30 +03008216 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008217 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8218 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8219 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8220 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008221 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008222 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008223
8224 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008225 clock.m2 = (pll_dw0 & 0xff) << 22;
8226 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8227 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008228 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8229 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8230 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8231
Imre Deakdccbea32015-06-22 23:35:51 +03008232 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008233}
8234
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008235static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008236 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008237{
8238 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008239 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02008240 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008241 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008242 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008243
Imre Deak17290502016-02-12 18:55:11 +02008244 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8245 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008246 return false;
8247
Daniel Vettere143a212013-07-04 12:01:15 +02008248 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008249 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008250
Imre Deak17290502016-02-12 18:55:11 +02008251 ret = false;
8252
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008253 tmp = I915_READ(PIPECONF(crtc->pipe));
8254 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008255 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008256
Wayne Boyer666a4532015-12-09 12:29:35 -08008257 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008258 switch (tmp & PIPECONF_BPC_MASK) {
8259 case PIPECONF_6BPC:
8260 pipe_config->pipe_bpp = 18;
8261 break;
8262 case PIPECONF_8BPC:
8263 pipe_config->pipe_bpp = 24;
8264 break;
8265 case PIPECONF_10BPC:
8266 pipe_config->pipe_bpp = 30;
8267 break;
8268 default:
8269 break;
8270 }
8271 }
8272
Wayne Boyer666a4532015-12-09 12:29:35 -08008273 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8274 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008275 pipe_config->limited_color_range = true;
8276
Ville Syrjälä282740f2013-09-04 18:30:03 +03008277 if (INTEL_INFO(dev)->gen < 4)
8278 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8279
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008280 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008281 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008282
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008283 i9xx_get_pfit_config(crtc, pipe_config);
8284
Daniel Vetter6c49f242013-06-06 12:45:25 +02008285 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008286 /* No way to read it out on pipes B and C */
8287 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8288 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8289 else
8290 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008291 pipe_config->pixel_multiplier =
8292 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8293 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008294 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008295 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8296 tmp = I915_READ(DPLL(crtc->pipe));
8297 pipe_config->pixel_multiplier =
8298 ((tmp & SDVO_MULTIPLIER_MASK)
8299 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8300 } else {
8301 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8302 * port and will be fixed up in the encoder->get_config
8303 * function. */
8304 pipe_config->pixel_multiplier = 1;
8305 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008306 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008307 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008308 /*
8309 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8310 * on 830. Filter it out here so that we don't
8311 * report errors due to that.
8312 */
8313 if (IS_I830(dev))
8314 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8315
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008316 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8317 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008318 } else {
8319 /* Mask out read-only status bits. */
8320 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8321 DPLL_PORTC_READY_MASK |
8322 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008323 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008324
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008325 if (IS_CHERRYVIEW(dev))
8326 chv_crtc_clock_get(crtc, pipe_config);
8327 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008328 vlv_crtc_clock_get(crtc, pipe_config);
8329 else
8330 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008331
Ville Syrjälä0f646142015-08-26 19:39:18 +03008332 /*
8333 * Normally the dotclock is filled in by the encoder .get_config()
8334 * but in case the pipe is enabled w/o any ports we need a sane
8335 * default.
8336 */
8337 pipe_config->base.adjusted_mode.crtc_clock =
8338 pipe_config->port_clock / pipe_config->pixel_multiplier;
8339
Imre Deak17290502016-02-12 18:55:11 +02008340 ret = true;
8341
8342out:
8343 intel_display_power_put(dev_priv, power_domain);
8344
8345 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008346}
8347
Paulo Zanonidde86e22012-12-01 12:04:25 -02008348static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008349{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008350 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008351 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04008352 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008353 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008354 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008355 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008356 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008357 bool has_ck505 = false;
8358 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04008359 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008360
8361 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008362 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008363 switch (encoder->type) {
8364 case INTEL_OUTPUT_LVDS:
8365 has_panel = true;
8366 has_lvds = true;
8367 break;
8368 case INTEL_OUTPUT_EDP:
8369 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008370 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008371 has_cpu_edp = true;
8372 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008373 default:
8374 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008375 }
8376 }
8377
Keith Packard99eb6a02011-09-26 14:29:12 -07008378 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008379 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008380 can_ssc = has_ck505;
8381 } else {
8382 has_ck505 = false;
8383 can_ssc = true;
8384 }
8385
Lyude1c1a24d2016-06-14 11:04:09 -04008386 /* Check if any DPLLs are using the SSC source */
8387 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8388 u32 temp = I915_READ(PCH_DPLL(i));
8389
8390 if (!(temp & DPLL_VCO_ENABLE))
8391 continue;
8392
8393 if ((temp & PLL_REF_INPUT_MASK) ==
8394 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8395 using_ssc_source = true;
8396 break;
8397 }
8398 }
8399
8400 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8401 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008402
8403 /* Ironlake: try to setup display ref clock before DPLL
8404 * enabling. This is only under driver's control after
8405 * PCH B stepping, previous chipset stepping should be
8406 * ignoring this setting.
8407 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008408 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008409
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008410 /* As we must carefully and slowly disable/enable each source in turn,
8411 * compute the final state we want first and check if we need to
8412 * make any changes at all.
8413 */
8414 final = val;
8415 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008416 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008417 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008418 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008419 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8420
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008421 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008422 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008423 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008424
Keith Packard199e5d72011-09-22 12:01:57 -07008425 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008426 final |= DREF_SSC_SOURCE_ENABLE;
8427
8428 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8429 final |= DREF_SSC1_ENABLE;
8430
8431 if (has_cpu_edp) {
8432 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8433 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8434 else
8435 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8436 } else
8437 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04008438 } else if (using_ssc_source) {
8439 final |= DREF_SSC_SOURCE_ENABLE;
8440 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008441 }
8442
8443 if (final == val)
8444 return;
8445
8446 /* Always enable nonspread source */
8447 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8448
8449 if (has_ck505)
8450 val |= DREF_NONSPREAD_CK505_ENABLE;
8451 else
8452 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8453
8454 if (has_panel) {
8455 val &= ~DREF_SSC_SOURCE_MASK;
8456 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008457
Keith Packard199e5d72011-09-22 12:01:57 -07008458 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008459 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008460 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008461 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008462 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008463 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008464
8465 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008466 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008467 POSTING_READ(PCH_DREF_CONTROL);
8468 udelay(200);
8469
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008470 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008471
8472 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008473 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008474 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008475 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008476 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008477 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008478 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008479 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008480 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008481
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008482 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008483 POSTING_READ(PCH_DREF_CONTROL);
8484 udelay(200);
8485 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04008486 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008487
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008488 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008489
8490 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008491 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008492
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008493 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008494 POSTING_READ(PCH_DREF_CONTROL);
8495 udelay(200);
8496
Lyude1c1a24d2016-06-14 11:04:09 -04008497 if (!using_ssc_source) {
8498 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008499
Lyude1c1a24d2016-06-14 11:04:09 -04008500 /* Turn off the SSC source */
8501 val &= ~DREF_SSC_SOURCE_MASK;
8502 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008503
Lyude1c1a24d2016-06-14 11:04:09 -04008504 /* Turn off SSC1 */
8505 val &= ~DREF_SSC1_ENABLE;
8506
8507 I915_WRITE(PCH_DREF_CONTROL, val);
8508 POSTING_READ(PCH_DREF_CONTROL);
8509 udelay(200);
8510 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07008511 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008512
8513 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008514}
8515
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008516static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008517{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008518 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008519
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008520 tmp = I915_READ(SOUTH_CHICKEN2);
8521 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8522 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008523
Imre Deakcf3598c2016-06-28 13:37:31 +03008524 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8525 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008526 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008527
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008528 tmp = I915_READ(SOUTH_CHICKEN2);
8529 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8530 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008531
Imre Deakcf3598c2016-06-28 13:37:31 +03008532 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8533 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008534 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008535}
8536
8537/* WaMPhyProgramming:hsw */
8538static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8539{
8540 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008541
8542 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8543 tmp &= ~(0xFF << 24);
8544 tmp |= (0x12 << 24);
8545 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8546
Paulo Zanonidde86e22012-12-01 12:04:25 -02008547 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8548 tmp |= (1 << 11);
8549 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8550
8551 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8552 tmp |= (1 << 11);
8553 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8554
Paulo Zanonidde86e22012-12-01 12:04:25 -02008555 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8556 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8557 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8558
8559 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8560 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8561 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8562
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008563 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8564 tmp &= ~(7 << 13);
8565 tmp |= (5 << 13);
8566 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008567
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008568 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8569 tmp &= ~(7 << 13);
8570 tmp |= (5 << 13);
8571 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008572
8573 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8574 tmp &= ~0xFF;
8575 tmp |= 0x1C;
8576 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8577
8578 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8579 tmp &= ~0xFF;
8580 tmp |= 0x1C;
8581 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8582
8583 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8584 tmp &= ~(0xFF << 16);
8585 tmp |= (0x1C << 16);
8586 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8587
8588 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8589 tmp &= ~(0xFF << 16);
8590 tmp |= (0x1C << 16);
8591 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8592
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008593 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8594 tmp |= (1 << 27);
8595 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008596
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008597 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8598 tmp |= (1 << 27);
8599 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008600
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008601 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8602 tmp &= ~(0xF << 28);
8603 tmp |= (4 << 28);
8604 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008605
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008606 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8607 tmp &= ~(0xF << 28);
8608 tmp |= (4 << 28);
8609 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008610}
8611
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008612/* Implements 3 different sequences from BSpec chapter "Display iCLK
8613 * Programming" based on the parameters passed:
8614 * - Sequence to enable CLKOUT_DP
8615 * - Sequence to enable CLKOUT_DP without spread
8616 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8617 */
8618static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8619 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008620{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008621 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008622 uint32_t reg, tmp;
8623
8624 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8625 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008626 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008627 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008628
Ville Syrjäläa5805162015-05-26 20:42:30 +03008629 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008630
8631 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8632 tmp &= ~SBI_SSCCTL_DISABLE;
8633 tmp |= SBI_SSCCTL_PATHALT;
8634 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8635
8636 udelay(24);
8637
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008638 if (with_spread) {
8639 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8640 tmp &= ~SBI_SSCCTL_PATHALT;
8641 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008642
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008643 if (with_fdi) {
8644 lpt_reset_fdi_mphy(dev_priv);
8645 lpt_program_fdi_mphy(dev_priv);
8646 }
8647 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008648
Ville Syrjäläc2699522015-08-27 23:55:59 +03008649 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008650 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8651 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8652 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008653
Ville Syrjäläa5805162015-05-26 20:42:30 +03008654 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008655}
8656
Paulo Zanoni47701c32013-07-23 11:19:25 -03008657/* Sequence to disable CLKOUT_DP */
8658static void lpt_disable_clkout_dp(struct drm_device *dev)
8659{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008660 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008661 uint32_t reg, tmp;
8662
Ville Syrjäläa5805162015-05-26 20:42:30 +03008663 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008664
Ville Syrjäläc2699522015-08-27 23:55:59 +03008665 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008666 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8667 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8668 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8669
8670 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8671 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8672 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8673 tmp |= SBI_SSCCTL_PATHALT;
8674 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8675 udelay(32);
8676 }
8677 tmp |= SBI_SSCCTL_DISABLE;
8678 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8679 }
8680
Ville Syrjäläa5805162015-05-26 20:42:30 +03008681 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008682}
8683
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008684#define BEND_IDX(steps) ((50 + (steps)) / 5)
8685
8686static const uint16_t sscdivintphase[] = {
8687 [BEND_IDX( 50)] = 0x3B23,
8688 [BEND_IDX( 45)] = 0x3B23,
8689 [BEND_IDX( 40)] = 0x3C23,
8690 [BEND_IDX( 35)] = 0x3C23,
8691 [BEND_IDX( 30)] = 0x3D23,
8692 [BEND_IDX( 25)] = 0x3D23,
8693 [BEND_IDX( 20)] = 0x3E23,
8694 [BEND_IDX( 15)] = 0x3E23,
8695 [BEND_IDX( 10)] = 0x3F23,
8696 [BEND_IDX( 5)] = 0x3F23,
8697 [BEND_IDX( 0)] = 0x0025,
8698 [BEND_IDX( -5)] = 0x0025,
8699 [BEND_IDX(-10)] = 0x0125,
8700 [BEND_IDX(-15)] = 0x0125,
8701 [BEND_IDX(-20)] = 0x0225,
8702 [BEND_IDX(-25)] = 0x0225,
8703 [BEND_IDX(-30)] = 0x0325,
8704 [BEND_IDX(-35)] = 0x0325,
8705 [BEND_IDX(-40)] = 0x0425,
8706 [BEND_IDX(-45)] = 0x0425,
8707 [BEND_IDX(-50)] = 0x0525,
8708};
8709
8710/*
8711 * Bend CLKOUT_DP
8712 * steps -50 to 50 inclusive, in steps of 5
8713 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8714 * change in clock period = -(steps / 10) * 5.787 ps
8715 */
8716static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8717{
8718 uint32_t tmp;
8719 int idx = BEND_IDX(steps);
8720
8721 if (WARN_ON(steps % 5 != 0))
8722 return;
8723
8724 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8725 return;
8726
8727 mutex_lock(&dev_priv->sb_lock);
8728
8729 if (steps % 10 != 0)
8730 tmp = 0xAAAAAAAB;
8731 else
8732 tmp = 0x00000000;
8733 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8734
8735 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8736 tmp &= 0xffff0000;
8737 tmp |= sscdivintphase[idx];
8738 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8739
8740 mutex_unlock(&dev_priv->sb_lock);
8741}
8742
8743#undef BEND_IDX
8744
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008745static void lpt_init_pch_refclk(struct drm_device *dev)
8746{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008747 struct intel_encoder *encoder;
8748 bool has_vga = false;
8749
Damien Lespiaub2784e12014-08-05 11:29:37 +01008750 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008751 switch (encoder->type) {
8752 case INTEL_OUTPUT_ANALOG:
8753 has_vga = true;
8754 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008755 default:
8756 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008757 }
8758 }
8759
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008760 if (has_vga) {
8761 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008762 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008763 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008764 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008765 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008766}
8767
Paulo Zanonidde86e22012-12-01 12:04:25 -02008768/*
8769 * Initialize reference clocks when the driver loads
8770 */
8771void intel_init_pch_refclk(struct drm_device *dev)
8772{
8773 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8774 ironlake_init_pch_refclk(dev);
8775 else if (HAS_PCH_LPT(dev))
8776 lpt_init_pch_refclk(dev);
8777}
8778
Daniel Vetter6ff93602013-04-19 11:24:36 +02008779static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008780{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008781 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanonic8203562012-09-12 10:06:29 -03008782 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8783 int pipe = intel_crtc->pipe;
8784 uint32_t val;
8785
Daniel Vetter78114072013-06-13 00:54:57 +02008786 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008787
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008788 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008789 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008790 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008791 break;
8792 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008793 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008794 break;
8795 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008796 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008797 break;
8798 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008799 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008800 break;
8801 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008802 /* Case prevented by intel_choose_pipe_bpp_dither. */
8803 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008804 }
8805
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008806 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008807 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8808
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008809 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008810 val |= PIPECONF_INTERLACED_ILK;
8811 else
8812 val |= PIPECONF_PROGRESSIVE;
8813
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008814 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008815 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008816
Paulo Zanonic8203562012-09-12 10:06:29 -03008817 I915_WRITE(PIPECONF(pipe), val);
8818 POSTING_READ(PIPECONF(pipe));
8819}
8820
Daniel Vetter6ff93602013-04-19 11:24:36 +02008821static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008822{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008823 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008824 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008825 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008826 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008827
Jani Nikula391bf042016-03-18 17:05:40 +02008828 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008829 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8830
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008831 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008832 val |= PIPECONF_INTERLACED_ILK;
8833 else
8834 val |= PIPECONF_PROGRESSIVE;
8835
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008836 I915_WRITE(PIPECONF(cpu_transcoder), val);
8837 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008838}
8839
Jani Nikula391bf042016-03-18 17:05:40 +02008840static void haswell_set_pipemisc(struct drm_crtc *crtc)
8841{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008842 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Jani Nikula391bf042016-03-18 17:05:40 +02008843 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8844
8845 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8846 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008847
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008848 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008849 case 18:
8850 val |= PIPEMISC_DITHER_6_BPC;
8851 break;
8852 case 24:
8853 val |= PIPEMISC_DITHER_8_BPC;
8854 break;
8855 case 30:
8856 val |= PIPEMISC_DITHER_10_BPC;
8857 break;
8858 case 36:
8859 val |= PIPEMISC_DITHER_12_BPC;
8860 break;
8861 default:
8862 /* Case prevented by pipe_config_set_bpp. */
8863 BUG();
8864 }
8865
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008866 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008867 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8868
Jani Nikula391bf042016-03-18 17:05:40 +02008869 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008870 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008871}
8872
Paulo Zanonid4b19312012-11-29 11:29:32 -02008873int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8874{
8875 /*
8876 * Account for spread spectrum to avoid
8877 * oversubscribing the link. Max center spread
8878 * is 2.5%; use 5% for safety's sake.
8879 */
8880 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008881 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008882}
8883
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008884static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008885{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008886 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008887}
8888
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008889static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8890 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008891 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008892{
8893 struct drm_crtc *crtc = &intel_crtc->base;
8894 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008895 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008896 u32 dpll, fp, fp2;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008897 int factor;
Jesse Barnes79e53942008-11-07 14:24:08 -08008898
Chris Wilsonc1858122010-12-03 21:35:48 +00008899 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008900 factor = 21;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008901 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Eric Anholt8febb292011-03-30 13:01:07 -07008902 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008903 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008904 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008905 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008906 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008907 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008908
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008909 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008910
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008911 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8912 fp |= FP_CB_TUNE;
8913
8914 if (reduced_clock) {
8915 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8916
8917 if (reduced_clock->m < factor * reduced_clock->n)
8918 fp2 |= FP_CB_TUNE;
8919 } else {
8920 fp2 = fp;
8921 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008922
Chris Wilson5eddb702010-09-11 13:48:45 +01008923 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008924
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008925 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Eric Anholta07d6782011-03-30 13:01:08 -07008926 dpll |= DPLLB_MODE_LVDS;
8927 else
8928 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008929
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008930 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008931 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008932
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008933 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8934 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008935 dpll |= DPLL_SDVO_HIGH_SPEED;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008936
Ville Syrjälä37a56502016-06-22 21:57:04 +03008937 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008938 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008939
Eric Anholta07d6782011-03-30 13:01:08 -07008940 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008941 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008942 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008943 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008944
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008945 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008946 case 5:
8947 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8948 break;
8949 case 7:
8950 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8951 break;
8952 case 10:
8953 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8954 break;
8955 case 14:
8956 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8957 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008958 }
8959
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008960 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8961 intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008962 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008963 else
8964 dpll |= PLL_REF_INPUT_DREFCLK;
8965
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008966 dpll |= DPLL_VCO_ENABLE;
8967
8968 crtc_state->dpll_hw_state.dpll = dpll;
8969 crtc_state->dpll_hw_state.fp0 = fp;
8970 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008971}
8972
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008973static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8974 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008975{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008976 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008977 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008978 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008979 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008980 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008981 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008982 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008983
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008984 memset(&crtc_state->dpll_hw_state, 0,
8985 sizeof(crtc_state->dpll_hw_state));
8986
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008987 crtc->lowfreq_avail = false;
8988
8989 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8990 if (!crtc_state->has_pch_encoder)
8991 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008992
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008993 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008994 if (intel_panel_use_ssc(dev_priv)) {
8995 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8996 dev_priv->vbt.lvds_ssc_freq);
8997 refclk = dev_priv->vbt.lvds_ssc_freq;
8998 }
8999
9000 if (intel_is_dual_link_lvds(dev)) {
9001 if (refclk == 100000)
9002 limit = &intel_limits_ironlake_dual_lvds_100m;
9003 else
9004 limit = &intel_limits_ironlake_dual_lvds;
9005 } else {
9006 if (refclk == 100000)
9007 limit = &intel_limits_ironlake_single_lvds_100m;
9008 else
9009 limit = &intel_limits_ironlake_single_lvds;
9010 }
9011 } else {
9012 limit = &intel_limits_ironlake_dac;
9013 }
9014
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009015 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009016 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9017 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009018 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9019 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009020 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009021
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009022 ironlake_compute_dpll(crtc, crtc_state,
9023 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009024
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009025 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9026 if (pll == NULL) {
9027 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9028 pipe_name(crtc->pipe));
9029 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009030 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009031
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03009032 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009033 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009034 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02009035
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009036 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009037}
9038
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009039static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9040 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009041{
9042 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009043 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009044 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009045
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009046 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9047 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9048 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9049 & ~TU_SIZE_MASK;
9050 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9051 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9052 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9053}
9054
9055static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9056 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009057 struct intel_link_m_n *m_n,
9058 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009059{
9060 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009061 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009062 enum pipe pipe = crtc->pipe;
9063
9064 if (INTEL_INFO(dev)->gen >= 5) {
9065 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9066 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9067 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9068 & ~TU_SIZE_MASK;
9069 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9070 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9071 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009072 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9073 * gen < 8) and if DRRS is supported (to make sure the
9074 * registers are not unnecessarily read).
9075 */
9076 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009077 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009078 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9079 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9080 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9081 & ~TU_SIZE_MASK;
9082 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9083 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9084 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9085 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009086 } else {
9087 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9088 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9089 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9090 & ~TU_SIZE_MASK;
9091 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9092 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9093 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9094 }
9095}
9096
9097void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009098 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009099{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009100 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009101 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9102 else
9103 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009104 &pipe_config->dp_m_n,
9105 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009106}
9107
Daniel Vetter72419202013-04-04 13:28:53 +02009108static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009109 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009110{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009111 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009112 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009113}
9114
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009115static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009116 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009117{
9118 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009119 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Kondurua1b22782015-04-07 15:28:45 -07009120 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9121 uint32_t ps_ctrl = 0;
9122 int id = -1;
9123 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009124
Chandra Kondurua1b22782015-04-07 15:28:45 -07009125 /* find scaler attached to this pipe */
9126 for (i = 0; i < crtc->num_scalers; i++) {
9127 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9128 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9129 id = i;
9130 pipe_config->pch_pfit.enabled = true;
9131 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9132 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9133 break;
9134 }
9135 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009136
Chandra Kondurua1b22782015-04-07 15:28:45 -07009137 scaler_state->scaler_id = id;
9138 if (id >= 0) {
9139 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9140 } else {
9141 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009142 }
9143}
9144
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009145static void
9146skylake_get_initial_plane_config(struct intel_crtc *crtc,
9147 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009148{
9149 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009150 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau40f46282015-02-27 11:15:21 +00009151 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009152 int pipe = crtc->pipe;
9153 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009154 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009155 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009156 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009157
Damien Lespiaud9806c92015-01-21 14:07:19 +00009158 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009159 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009160 DRM_DEBUG_KMS("failed to alloc fb\n");
9161 return;
9162 }
9163
Damien Lespiau1b842c82015-01-21 13:50:54 +00009164 fb = &intel_fb->base;
9165
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009166 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009167 if (!(val & PLANE_CTL_ENABLE))
9168 goto error;
9169
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009170 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9171 fourcc = skl_format_to_fourcc(pixel_format,
9172 val & PLANE_CTL_ORDER_RGBX,
9173 val & PLANE_CTL_ALPHA_MASK);
9174 fb->pixel_format = fourcc;
9175 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9176
Damien Lespiau40f46282015-02-27 11:15:21 +00009177 tiling = val & PLANE_CTL_TILED_MASK;
9178 switch (tiling) {
9179 case PLANE_CTL_TILED_LINEAR:
9180 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9181 break;
9182 case PLANE_CTL_TILED_X:
9183 plane_config->tiling = I915_TILING_X;
9184 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9185 break;
9186 case PLANE_CTL_TILED_Y:
9187 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9188 break;
9189 case PLANE_CTL_TILED_YF:
9190 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9191 break;
9192 default:
9193 MISSING_CASE(tiling);
9194 goto error;
9195 }
9196
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009197 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9198 plane_config->base = base;
9199
9200 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9201
9202 val = I915_READ(PLANE_SIZE(pipe, 0));
9203 fb->height = ((val >> 16) & 0xfff) + 1;
9204 fb->width = ((val >> 0) & 0x1fff) + 1;
9205
9206 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009207 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009208 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009209 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9210
9211 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009212 fb->pixel_format,
9213 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009214
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009215 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009216
9217 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9218 pipe_name(pipe), fb->width, fb->height,
9219 fb->bits_per_pixel, base, fb->pitches[0],
9220 plane_config->size);
9221
Damien Lespiau2d140302015-02-05 17:22:18 +00009222 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009223 return;
9224
9225error:
9226 kfree(fb);
9227}
9228
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009229static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009230 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009231{
9232 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009233 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009234 uint32_t tmp;
9235
9236 tmp = I915_READ(PF_CTL(crtc->pipe));
9237
9238 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009239 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009240 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9241 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009242
9243 /* We currently do not free assignements of panel fitters on
9244 * ivb/hsw (since we don't use the higher upscaling modes which
9245 * differentiates them) so just WARN about this case for now. */
9246 if (IS_GEN7(dev)) {
9247 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9248 PF_PIPE_SEL_IVB(crtc->pipe));
9249 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009250 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009251}
9252
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009253static void
9254ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9255 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009256{
9257 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009258 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009259 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009260 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009261 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009262 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009263 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009264 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009265
Damien Lespiau42a7b082015-02-05 19:35:13 +00009266 val = I915_READ(DSPCNTR(pipe));
9267 if (!(val & DISPLAY_PLANE_ENABLE))
9268 return;
9269
Damien Lespiaud9806c92015-01-21 14:07:19 +00009270 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009271 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009272 DRM_DEBUG_KMS("failed to alloc fb\n");
9273 return;
9274 }
9275
Damien Lespiau1b842c82015-01-21 13:50:54 +00009276 fb = &intel_fb->base;
9277
Daniel Vetter18c52472015-02-10 17:16:09 +00009278 if (INTEL_INFO(dev)->gen >= 4) {
9279 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009280 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009281 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9282 }
9283 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009284
9285 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009286 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009287 fb->pixel_format = fourcc;
9288 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009289
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009290 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009291 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009292 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009293 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009294 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009295 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009296 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009297 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009298 }
9299 plane_config->base = base;
9300
9301 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009302 fb->width = ((val >> 16) & 0xfff) + 1;
9303 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009304
9305 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009306 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009307
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009308 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009309 fb->pixel_format,
9310 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009311
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009312 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009313
Damien Lespiau2844a922015-01-20 12:51:48 +00009314 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9315 pipe_name(pipe), fb->width, fb->height,
9316 fb->bits_per_pixel, base, fb->pitches[0],
9317 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009318
Damien Lespiau2d140302015-02-05 17:22:18 +00009319 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009320}
9321
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009322static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009323 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009324{
9325 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009326 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02009327 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009328 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009329 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009330
Imre Deak17290502016-02-12 18:55:11 +02009331 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9332 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009333 return false;
9334
Daniel Vettere143a212013-07-04 12:01:15 +02009335 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009336 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009337
Imre Deak17290502016-02-12 18:55:11 +02009338 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009339 tmp = I915_READ(PIPECONF(crtc->pipe));
9340 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009341 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009342
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009343 switch (tmp & PIPECONF_BPC_MASK) {
9344 case PIPECONF_6BPC:
9345 pipe_config->pipe_bpp = 18;
9346 break;
9347 case PIPECONF_8BPC:
9348 pipe_config->pipe_bpp = 24;
9349 break;
9350 case PIPECONF_10BPC:
9351 pipe_config->pipe_bpp = 30;
9352 break;
9353 case PIPECONF_12BPC:
9354 pipe_config->pipe_bpp = 36;
9355 break;
9356 default:
9357 break;
9358 }
9359
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009360 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9361 pipe_config->limited_color_range = true;
9362
Daniel Vetterab9412b2013-05-03 11:49:46 +02009363 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009364 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009365 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009366
Daniel Vetter88adfff2013-03-28 10:42:01 +01009367 pipe_config->has_pch_encoder = true;
9368
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009369 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9370 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9371 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009372
9373 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009374
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009375 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03009376 /*
9377 * The pipe->pch transcoder and pch transcoder->pll
9378 * mapping is fixed.
9379 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009380 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009381 } else {
9382 tmp = I915_READ(PCH_DPLL_SEL);
9383 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009384 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009385 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009386 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009387 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009388
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009389 pipe_config->shared_dpll =
9390 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9391 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009392
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009393 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9394 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009395
9396 tmp = pipe_config->dpll_hw_state.dpll;
9397 pipe_config->pixel_multiplier =
9398 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9399 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009400
9401 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009402 } else {
9403 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009404 }
9405
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009406 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009407 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009408
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009409 ironlake_get_pfit_config(crtc, pipe_config);
9410
Imre Deak17290502016-02-12 18:55:11 +02009411 ret = true;
9412
9413out:
9414 intel_display_power_put(dev_priv, power_domain);
9415
9416 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009417}
9418
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009419static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9420{
Chris Wilson91c8a322016-07-05 10:40:23 +01009421 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009422 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009423
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009424 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009425 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009426 pipe_name(crtc->pipe));
9427
Rob Clarke2c719b2014-12-15 13:56:32 -05009428 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9429 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009430 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9431 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009432 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9433 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009434 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009435 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009436 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009437 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009438 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009439 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009440 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009441 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009442 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009443
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009444 /*
9445 * In theory we can still leave IRQs enabled, as long as only the HPD
9446 * interrupts remain enabled. We used to check for that, but since it's
9447 * gen-specific and since we only disable LCPLL after we fully disable
9448 * the interrupts, the check below should be enough.
9449 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009450 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009451}
9452
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009453static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9454{
Chris Wilson91c8a322016-07-05 10:40:23 +01009455 struct drm_device *dev = &dev_priv->drm;
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009456
9457 if (IS_HASWELL(dev))
9458 return I915_READ(D_COMP_HSW);
9459 else
9460 return I915_READ(D_COMP_BDW);
9461}
9462
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009463static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9464{
Chris Wilson91c8a322016-07-05 10:40:23 +01009465 struct drm_device *dev = &dev_priv->drm;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009466
9467 if (IS_HASWELL(dev)) {
9468 mutex_lock(&dev_priv->rps.hw_lock);
9469 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9470 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009471 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009472 mutex_unlock(&dev_priv->rps.hw_lock);
9473 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009474 I915_WRITE(D_COMP_BDW, val);
9475 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009476 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009477}
9478
9479/*
9480 * This function implements pieces of two sequences from BSpec:
9481 * - Sequence for display software to disable LCPLL
9482 * - Sequence for display software to allow package C8+
9483 * The steps implemented here are just the steps that actually touch the LCPLL
9484 * register. Callers should take care of disabling all the display engine
9485 * functions, doing the mode unset, fixing interrupts, etc.
9486 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009487static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9488 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009489{
9490 uint32_t val;
9491
9492 assert_can_disable_lcpll(dev_priv);
9493
9494 val = I915_READ(LCPLL_CTL);
9495
9496 if (switch_to_fclk) {
9497 val |= LCPLL_CD_SOURCE_FCLK;
9498 I915_WRITE(LCPLL_CTL, val);
9499
Imre Deakf53dd632016-06-28 13:37:32 +03009500 if (wait_for_us(I915_READ(LCPLL_CTL) &
9501 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009502 DRM_ERROR("Switching to FCLK failed\n");
9503
9504 val = I915_READ(LCPLL_CTL);
9505 }
9506
9507 val |= LCPLL_PLL_DISABLE;
9508 I915_WRITE(LCPLL_CTL, val);
9509 POSTING_READ(LCPLL_CTL);
9510
Chris Wilson24d84412016-06-30 15:33:07 +01009511 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009512 DRM_ERROR("LCPLL still locked\n");
9513
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009514 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009515 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009516 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009517 ndelay(100);
9518
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009519 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9520 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009521 DRM_ERROR("D_COMP RCOMP still in progress\n");
9522
9523 if (allow_power_down) {
9524 val = I915_READ(LCPLL_CTL);
9525 val |= LCPLL_POWER_DOWN_ALLOW;
9526 I915_WRITE(LCPLL_CTL, val);
9527 POSTING_READ(LCPLL_CTL);
9528 }
9529}
9530
9531/*
9532 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9533 * source.
9534 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009535static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009536{
9537 uint32_t val;
9538
9539 val = I915_READ(LCPLL_CTL);
9540
9541 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9542 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9543 return;
9544
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009545 /*
9546 * Make sure we're not on PC8 state before disabling PC8, otherwise
9547 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009548 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009549 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009550
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009551 if (val & LCPLL_POWER_DOWN_ALLOW) {
9552 val &= ~LCPLL_POWER_DOWN_ALLOW;
9553 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009554 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009555 }
9556
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009557 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009558 val |= D_COMP_COMP_FORCE;
9559 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009560 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009561
9562 val = I915_READ(LCPLL_CTL);
9563 val &= ~LCPLL_PLL_DISABLE;
9564 I915_WRITE(LCPLL_CTL, val);
9565
Chris Wilson93220c02016-06-30 15:33:08 +01009566 if (intel_wait_for_register(dev_priv,
9567 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
9568 5))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009569 DRM_ERROR("LCPLL not locked yet\n");
9570
9571 if (val & LCPLL_CD_SOURCE_FCLK) {
9572 val = I915_READ(LCPLL_CTL);
9573 val &= ~LCPLL_CD_SOURCE_FCLK;
9574 I915_WRITE(LCPLL_CTL, val);
9575
Imre Deakf53dd632016-06-28 13:37:32 +03009576 if (wait_for_us((I915_READ(LCPLL_CTL) &
9577 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009578 DRM_ERROR("Switching back to LCPLL failed\n");
9579 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009580
Mika Kuoppala59bad942015-01-16 11:34:40 +02009581 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Chris Wilson91c8a322016-07-05 10:40:23 +01009582 intel_update_cdclk(&dev_priv->drm);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009583}
9584
Paulo Zanoni765dab672014-03-07 20:08:18 -03009585/*
9586 * Package states C8 and deeper are really deep PC states that can only be
9587 * reached when all the devices on the system allow it, so even if the graphics
9588 * device allows PC8+, it doesn't mean the system will actually get to these
9589 * states. Our driver only allows PC8+ when going into runtime PM.
9590 *
9591 * The requirements for PC8+ are that all the outputs are disabled, the power
9592 * well is disabled and most interrupts are disabled, and these are also
9593 * requirements for runtime PM. When these conditions are met, we manually do
9594 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9595 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9596 * hang the machine.
9597 *
9598 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9599 * the state of some registers, so when we come back from PC8+ we need to
9600 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9601 * need to take care of the registers kept by RC6. Notice that this happens even
9602 * if we don't put the device in PCI D3 state (which is what currently happens
9603 * because of the runtime PM support).
9604 *
9605 * For more, read "Display Sequences for Package C8" on the hardware
9606 * documentation.
9607 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009608void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009609{
Chris Wilson91c8a322016-07-05 10:40:23 +01009610 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonic67a4702013-08-19 13:18:09 -03009611 uint32_t val;
9612
Paulo Zanonic67a4702013-08-19 13:18:09 -03009613 DRM_DEBUG_KMS("Enabling package C8+\n");
9614
Ville Syrjäläc2699522015-08-27 23:55:59 +03009615 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009616 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9617 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9618 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9619 }
9620
9621 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009622 hsw_disable_lcpll(dev_priv, true, true);
9623}
9624
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009625void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009626{
Chris Wilson91c8a322016-07-05 10:40:23 +01009627 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonic67a4702013-08-19 13:18:09 -03009628 uint32_t val;
9629
Paulo Zanonic67a4702013-08-19 13:18:09 -03009630 DRM_DEBUG_KMS("Disabling package C8+\n");
9631
9632 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009633 lpt_init_pch_refclk(dev);
9634
Ville Syrjäläc2699522015-08-27 23:55:59 +03009635 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009636 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9637 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9638 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9639 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009640}
9641
Imre Deak324513c2016-06-13 16:44:36 +03009642static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309643{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009644 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009645 struct intel_atomic_state *old_intel_state =
9646 to_intel_atomic_state(old_state);
9647 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309648
Imre Deak324513c2016-06-13 16:44:36 +03009649 bxt_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309650}
9651
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009652/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009653static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009654{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009655 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +01009656 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009657 struct drm_crtc *crtc;
9658 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009659 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009660 unsigned max_pixel_rate = 0, i;
9661 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009662
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009663 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9664 sizeof(intel_state->min_pixclk));
9665
9666 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009667 int pixel_rate;
9668
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009669 crtc_state = to_intel_crtc_state(cstate);
9670 if (!crtc_state->base.enable) {
9671 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009672 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009673 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009674
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009675 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009676
9677 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009678 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009679 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9680
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009681 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009682 }
9683
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009684 for_each_pipe(dev_priv, pipe)
9685 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9686
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009687 return max_pixel_rate;
9688}
9689
9690static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9691{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009692 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009693 uint32_t val, data;
9694 int ret;
9695
9696 if (WARN((I915_READ(LCPLL_CTL) &
9697 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9698 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9699 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9700 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9701 "trying to change cdclk frequency with cdclk not enabled\n"))
9702 return;
9703
9704 mutex_lock(&dev_priv->rps.hw_lock);
9705 ret = sandybridge_pcode_write(dev_priv,
9706 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9707 mutex_unlock(&dev_priv->rps.hw_lock);
9708 if (ret) {
9709 DRM_ERROR("failed to inform pcode about cdclk change\n");
9710 return;
9711 }
9712
9713 val = I915_READ(LCPLL_CTL);
9714 val |= LCPLL_CD_SOURCE_FCLK;
9715 I915_WRITE(LCPLL_CTL, val);
9716
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009717 if (wait_for_us(I915_READ(LCPLL_CTL) &
9718 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009719 DRM_ERROR("Switching to FCLK failed\n");
9720
9721 val = I915_READ(LCPLL_CTL);
9722 val &= ~LCPLL_CLK_FREQ_MASK;
9723
9724 switch (cdclk) {
9725 case 450000:
9726 val |= LCPLL_CLK_FREQ_450;
9727 data = 0;
9728 break;
9729 case 540000:
9730 val |= LCPLL_CLK_FREQ_54O_BDW;
9731 data = 1;
9732 break;
9733 case 337500:
9734 val |= LCPLL_CLK_FREQ_337_5_BDW;
9735 data = 2;
9736 break;
9737 case 675000:
9738 val |= LCPLL_CLK_FREQ_675_BDW;
9739 data = 3;
9740 break;
9741 default:
9742 WARN(1, "invalid cdclk frequency\n");
9743 return;
9744 }
9745
9746 I915_WRITE(LCPLL_CTL, val);
9747
9748 val = I915_READ(LCPLL_CTL);
9749 val &= ~LCPLL_CD_SOURCE_FCLK;
9750 I915_WRITE(LCPLL_CTL, val);
9751
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009752 if (wait_for_us((I915_READ(LCPLL_CTL) &
9753 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009754 DRM_ERROR("Switching back to LCPLL failed\n");
9755
9756 mutex_lock(&dev_priv->rps.hw_lock);
9757 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9758 mutex_unlock(&dev_priv->rps.hw_lock);
9759
Ville Syrjälä7f1052a2016-04-26 19:46:32 +03009760 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9761
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009762 intel_update_cdclk(dev);
9763
9764 WARN(cdclk != dev_priv->cdclk_freq,
9765 "cdclk requested %d kHz but got %d kHz\n",
9766 cdclk, dev_priv->cdclk_freq);
9767}
9768
Ville Syrjälä587c7912016-05-11 22:44:41 +03009769static int broadwell_calc_cdclk(int max_pixclk)
9770{
9771 if (max_pixclk > 540000)
9772 return 675000;
9773 else if (max_pixclk > 450000)
9774 return 540000;
9775 else if (max_pixclk > 337500)
9776 return 450000;
9777 else
9778 return 337500;
9779}
9780
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009781static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009782{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009783 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009784 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009785 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009786 int cdclk;
9787
9788 /*
9789 * FIXME should also account for plane ratio
9790 * once 64bpp pixel formats are supported.
9791 */
Ville Syrjälä587c7912016-05-11 22:44:41 +03009792 cdclk = broadwell_calc_cdclk(max_pixclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009793
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009794 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009795 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9796 cdclk, dev_priv->max_cdclk_freq);
9797 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009798 }
9799
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009800 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9801 if (!intel_state->active_crtcs)
Ville Syrjälä587c7912016-05-11 22:44:41 +03009802 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009803
9804 return 0;
9805}
9806
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009807static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009808{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009809 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009810 struct intel_atomic_state *old_intel_state =
9811 to_intel_atomic_state(old_state);
9812 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009813
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009814 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009815}
9816
Clint Taylorc89e39f2016-05-13 23:41:21 +03009817static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9818{
9819 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9820 struct drm_i915_private *dev_priv = to_i915(state->dev);
9821 const int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009822 int vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009823 int cdclk;
9824
9825 /*
9826 * FIXME should also account for plane ratio
9827 * once 64bpp pixel formats are supported.
9828 */
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009829 cdclk = skl_calc_cdclk(max_pixclk, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009830
9831 /*
9832 * FIXME move the cdclk caclulation to
9833 * compute_config() so we can fail gracegully.
9834 */
9835 if (cdclk > dev_priv->max_cdclk_freq) {
9836 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9837 cdclk, dev_priv->max_cdclk_freq);
9838 cdclk = dev_priv->max_cdclk_freq;
9839 }
9840
9841 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9842 if (!intel_state->active_crtcs)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009843 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009844
9845 return 0;
9846}
9847
9848static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9849{
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009850 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9851 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9852 unsigned int req_cdclk = intel_state->dev_cdclk;
9853 unsigned int req_vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009854
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009855 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009856}
9857
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009858static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9859 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009860{
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009861 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009862 if (!intel_ddi_pll_select(crtc, crtc_state))
9863 return -EINVAL;
9864 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009865
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009866 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009867
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009868 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009869}
9870
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309871static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9872 enum port port,
9873 struct intel_crtc_state *pipe_config)
9874{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009875 enum intel_dpll_id id;
9876
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309877 switch (port) {
9878 case PORT_A:
9879 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009880 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309881 break;
9882 case PORT_B:
9883 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009884 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309885 break;
9886 case PORT_C:
9887 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009888 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309889 break;
9890 default:
9891 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009892 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309893 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009894
9895 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309896}
9897
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009898static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9899 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009900 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009901{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009902 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009903 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009904
9905 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9906 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9907
9908 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009909 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009910 id = DPLL_ID_SKL_DPLL0;
9911 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009912 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009913 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009914 break;
9915 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009916 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009917 break;
9918 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009919 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009920 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009921 default:
9922 MISSING_CASE(pipe_config->ddi_pll_sel);
9923 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009924 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009925
9926 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009927}
9928
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009929static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9930 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009931 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009932{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009933 enum intel_dpll_id id;
9934
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009935 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9936
9937 switch (pipe_config->ddi_pll_sel) {
9938 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009939 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009940 break;
9941 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009942 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009943 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009944 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009945 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009946 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009947 case PORT_CLK_SEL_LCPLL_810:
9948 id = DPLL_ID_LCPLL_810;
9949 break;
9950 case PORT_CLK_SEL_LCPLL_1350:
9951 id = DPLL_ID_LCPLL_1350;
9952 break;
9953 case PORT_CLK_SEL_LCPLL_2700:
9954 id = DPLL_ID_LCPLL_2700;
9955 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009956 default:
9957 MISSING_CASE(pipe_config->ddi_pll_sel);
9958 /* fall through */
9959 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009960 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009961 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009962
9963 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009964}
9965
Jani Nikulacf304292016-03-18 17:05:41 +02009966static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9967 struct intel_crtc_state *pipe_config,
9968 unsigned long *power_domain_mask)
9969{
9970 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009971 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulacf304292016-03-18 17:05:41 +02009972 enum intel_display_power_domain power_domain;
9973 u32 tmp;
9974
Imre Deakd9a7bc62016-05-12 16:18:50 +03009975 /*
9976 * The pipe->transcoder mapping is fixed with the exception of the eDP
9977 * transcoder handled below.
9978 */
Jani Nikulacf304292016-03-18 17:05:41 +02009979 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9980
9981 /*
9982 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9983 * consistency and less surprising code; it's in always on power).
9984 */
9985 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9986 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9987 enum pipe trans_edp_pipe;
9988 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9989 default:
9990 WARN(1, "unknown pipe linked to edp transcoder\n");
9991 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9992 case TRANS_DDI_EDP_INPUT_A_ON:
9993 trans_edp_pipe = PIPE_A;
9994 break;
9995 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9996 trans_edp_pipe = PIPE_B;
9997 break;
9998 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9999 trans_edp_pipe = PIPE_C;
10000 break;
10001 }
10002
10003 if (trans_edp_pipe == crtc->pipe)
10004 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10005 }
10006
10007 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10008 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10009 return false;
10010 *power_domain_mask |= BIT(power_domain);
10011
10012 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10013
10014 return tmp & PIPECONF_ENABLE;
10015}
10016
Jani Nikula4d1de972016-03-18 17:05:42 +020010017static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10018 struct intel_crtc_state *pipe_config,
10019 unsigned long *power_domain_mask)
10020{
10021 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010022 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020010023 enum intel_display_power_domain power_domain;
10024 enum port port;
10025 enum transcoder cpu_transcoder;
10026 u32 tmp;
10027
Jani Nikula4d1de972016-03-18 17:05:42 +020010028 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10029 if (port == PORT_A)
10030 cpu_transcoder = TRANSCODER_DSI_A;
10031 else
10032 cpu_transcoder = TRANSCODER_DSI_C;
10033
10034 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10035 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10036 continue;
10037 *power_domain_mask |= BIT(power_domain);
10038
Imre Deakdb18b6a2016-03-24 12:41:40 +020010039 /*
10040 * The PLL needs to be enabled with a valid divider
10041 * configuration, otherwise accessing DSI registers will hang
10042 * the machine. See BSpec North Display Engine
10043 * registers/MIPI[BXT]. We can break out here early, since we
10044 * need the same DSI PLL to be enabled for both DSI ports.
10045 */
10046 if (!intel_dsi_pll_is_enabled(dev_priv))
10047 break;
10048
Jani Nikula4d1de972016-03-18 17:05:42 +020010049 /* XXX: this works for video mode only */
10050 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10051 if (!(tmp & DPI_ENABLE))
10052 continue;
10053
10054 tmp = I915_READ(MIPI_CTRL(port));
10055 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10056 continue;
10057
10058 pipe_config->cpu_transcoder = cpu_transcoder;
Jani Nikula4d1de972016-03-18 17:05:42 +020010059 break;
10060 }
10061
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010062 return transcoder_is_dsi(pipe_config->cpu_transcoder);
Jani Nikula4d1de972016-03-18 17:05:42 +020010063}
10064
Daniel Vetter26804af2014-06-25 22:01:55 +030010065static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010066 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +030010067{
10068 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010069 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010070 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +030010071 enum port port;
10072 uint32_t tmp;
10073
10074 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10075
10076 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10077
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070010078 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010079 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010080 else if (IS_BROXTON(dev))
10081 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010082 else
10083 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010084
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010085 pll = pipe_config->shared_dpll;
10086 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010087 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10088 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010089 }
10090
Daniel Vetter26804af2014-06-25 22:01:55 +030010091 /*
10092 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10093 * DDI E. So just check whether this pipe is wired to DDI E and whether
10094 * the PCH transcoder is on.
10095 */
Damien Lespiauca370452013-12-03 13:56:24 +000010096 if (INTEL_INFO(dev)->gen < 9 &&
10097 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010098 pipe_config->has_pch_encoder = true;
10099
10100 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10101 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10102 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10103
10104 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10105 }
10106}
10107
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010108static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010109 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010110{
10111 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010112 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +020010113 enum intel_display_power_domain power_domain;
10114 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +020010115 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010116
Imre Deak17290502016-02-12 18:55:11 +020010117 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10118 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010119 return false;
Imre Deak17290502016-02-12 18:55:11 +020010120 power_domain_mask = BIT(power_domain);
10121
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010122 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010123
Jani Nikulacf304292016-03-18 17:05:41 +020010124 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +020010125
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010126 if (IS_BROXTON(dev_priv) &&
10127 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10128 WARN_ON(active);
10129 active = true;
Jani Nikula4d1de972016-03-18 17:05:42 +020010130 }
10131
Jani Nikulacf304292016-03-18 17:05:41 +020010132 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010133 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010134
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010135 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +020010136 haswell_get_ddi_port_state(crtc, pipe_config);
10137 intel_get_pipe_timings(crtc, pipe_config);
10138 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010139
Jani Nikulabc58be62016-03-18 17:05:39 +020010140 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010141
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010142 pipe_config->gamma_mode =
10143 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10144
Chandra Kondurua1b22782015-04-07 15:28:45 -070010145 if (INTEL_INFO(dev)->gen >= 9) {
10146 skl_init_scalers(dev, crtc, pipe_config);
10147 }
10148
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070010149 if (INTEL_INFO(dev)->gen >= 9) {
10150 pipe_config->scaler_state.scaler_id = -1;
10151 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10152 }
10153
Imre Deak17290502016-02-12 18:55:11 +020010154 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10155 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10156 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010157 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010158 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010159 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010160 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010161 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010162
Jesse Barnese59150d2014-01-07 13:30:45 -080010163 if (IS_HASWELL(dev))
10164 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10165 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010166
Jani Nikula4d1de972016-03-18 17:05:42 +020010167 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10168 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010169 pipe_config->pixel_multiplier =
10170 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10171 } else {
10172 pipe_config->pixel_multiplier = 1;
10173 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010174
Imre Deak17290502016-02-12 18:55:11 +020010175out:
10176 for_each_power_domain(power_domain, power_domain_mask)
10177 intel_display_power_put(dev_priv, power_domain);
10178
Jani Nikulacf304292016-03-18 17:05:41 +020010179 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010180}
10181
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010182static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10183 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010184{
10185 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010186 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010187 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010188 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010189
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010190 if (plane_state && plane_state->visible) {
10191 unsigned int width = plane_state->base.crtc_w;
10192 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010193 unsigned int stride = roundup_pow_of_two(width) * 4;
10194
10195 switch (stride) {
10196 default:
10197 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10198 width, stride);
10199 stride = 256;
10200 /* fallthrough */
10201 case 256:
10202 case 512:
10203 case 1024:
10204 case 2048:
10205 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010206 }
10207
Ville Syrjälädc41c152014-08-13 11:57:05 +030010208 cntl |= CURSOR_ENABLE |
10209 CURSOR_GAMMA_ENABLE |
10210 CURSOR_FORMAT_ARGB |
10211 CURSOR_STRIDE(stride);
10212
10213 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010214 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010215
Ville Syrjälädc41c152014-08-13 11:57:05 +030010216 if (intel_crtc->cursor_cntl != 0 &&
10217 (intel_crtc->cursor_base != base ||
10218 intel_crtc->cursor_size != size ||
10219 intel_crtc->cursor_cntl != cntl)) {
10220 /* On these chipsets we can only modify the base/size/stride
10221 * whilst the cursor is disabled.
10222 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010223 I915_WRITE(CURCNTR(PIPE_A), 0);
10224 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010225 intel_crtc->cursor_cntl = 0;
10226 }
10227
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010228 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010229 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010230 intel_crtc->cursor_base = base;
10231 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010232
10233 if (intel_crtc->cursor_size != size) {
10234 I915_WRITE(CURSIZE, size);
10235 intel_crtc->cursor_size = size;
10236 }
10237
Chris Wilson4b0e3332014-05-30 16:35:26 +030010238 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010239 I915_WRITE(CURCNTR(PIPE_A), cntl);
10240 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010241 intel_crtc->cursor_cntl = cntl;
10242 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010243}
10244
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010245static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10246 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010247{
10248 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010249 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010250 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10251 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010252 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010253
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010254 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010255 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010256 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010257 case 64:
10258 cntl |= CURSOR_MODE_64_ARGB_AX;
10259 break;
10260 case 128:
10261 cntl |= CURSOR_MODE_128_ARGB_AX;
10262 break;
10263 case 256:
10264 cntl |= CURSOR_MODE_256_ARGB_AX;
10265 break;
10266 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010267 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010268 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010269 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010270 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010271
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010272 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010273 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010274
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010275 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10276 cntl |= CURSOR_ROTATE_180;
10277 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010278
Chris Wilson4b0e3332014-05-30 16:35:26 +030010279 if (intel_crtc->cursor_cntl != cntl) {
10280 I915_WRITE(CURCNTR(pipe), cntl);
10281 POSTING_READ(CURCNTR(pipe));
10282 intel_crtc->cursor_cntl = cntl;
10283 }
10284
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010285 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010286 I915_WRITE(CURBASE(pipe), base);
10287 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010288
10289 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010290}
10291
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010292/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010293static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010294 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010295{
10296 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010297 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010298 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10299 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010300 u32 base = intel_crtc->cursor_addr;
10301 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010302
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010303 if (plane_state) {
10304 int x = plane_state->base.crtc_x;
10305 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010306
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010307 if (x < 0) {
10308 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10309 x = -x;
10310 }
10311 pos |= x << CURSOR_X_SHIFT;
10312
10313 if (y < 0) {
10314 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10315 y = -y;
10316 }
10317 pos |= y << CURSOR_Y_SHIFT;
10318
10319 /* ILK+ do this automagically */
10320 if (HAS_GMCH_DISPLAY(dev) &&
10321 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10322 base += (plane_state->base.crtc_h *
10323 plane_state->base.crtc_w - 1) * 4;
10324 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010325 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010326
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010327 I915_WRITE(CURPOS(pipe), pos);
10328
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010329 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010330 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010331 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010332 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010333}
10334
Ville Syrjälädc41c152014-08-13 11:57:05 +030010335static bool cursor_size_ok(struct drm_device *dev,
10336 uint32_t width, uint32_t height)
10337{
10338 if (width == 0 || height == 0)
10339 return false;
10340
10341 /*
10342 * 845g/865g are special in that they are only limited by
10343 * the width of their cursors, the height is arbitrary up to
10344 * the precision of the register. Everything else requires
10345 * square cursors, limited to a few power-of-two sizes.
10346 */
10347 if (IS_845G(dev) || IS_I865G(dev)) {
10348 if ((width & 63) != 0)
10349 return false;
10350
10351 if (width > (IS_845G(dev) ? 64 : 512))
10352 return false;
10353
10354 if (height > 1023)
10355 return false;
10356 } else {
10357 switch (width | height) {
10358 case 256:
10359 case 128:
10360 if (IS_GEN2(dev))
10361 return false;
10362 case 64:
10363 break;
10364 default:
10365 return false;
10366 }
10367 }
10368
10369 return true;
10370}
10371
Jesse Barnes79e53942008-11-07 14:24:08 -080010372/* VESA 640x480x72Hz mode to set on the pipe */
10373static struct drm_display_mode load_detect_mode = {
10374 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10375 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10376};
10377
Daniel Vettera8bb6812014-02-10 18:00:39 +010010378struct drm_framebuffer *
10379__intel_framebuffer_create(struct drm_device *dev,
10380 struct drm_mode_fb_cmd2 *mode_cmd,
10381 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010382{
10383 struct intel_framebuffer *intel_fb;
10384 int ret;
10385
10386 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010387 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010388 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010389
10390 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010391 if (ret)
10392 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010393
10394 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010395
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010396err:
10397 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010398 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010399}
10400
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010401static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010402intel_framebuffer_create(struct drm_device *dev,
10403 struct drm_mode_fb_cmd2 *mode_cmd,
10404 struct drm_i915_gem_object *obj)
10405{
10406 struct drm_framebuffer *fb;
10407 int ret;
10408
10409 ret = i915_mutex_lock_interruptible(dev);
10410 if (ret)
10411 return ERR_PTR(ret);
10412 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10413 mutex_unlock(&dev->struct_mutex);
10414
10415 return fb;
10416}
10417
Chris Wilsond2dff872011-04-19 08:36:26 +010010418static u32
10419intel_framebuffer_pitch_for_width(int width, int bpp)
10420{
10421 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10422 return ALIGN(pitch, 64);
10423}
10424
10425static u32
10426intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10427{
10428 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010429 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010430}
10431
10432static struct drm_framebuffer *
10433intel_framebuffer_create_for_mode(struct drm_device *dev,
10434 struct drm_display_mode *mode,
10435 int depth, int bpp)
10436{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010437 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010438 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010439 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010440
Dave Gordond37cd8a2016-04-22 19:14:32 +010010441 obj = i915_gem_object_create(dev,
Chris Wilsond2dff872011-04-19 08:36:26 +010010442 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +010010443 if (IS_ERR(obj))
10444 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010445
10446 mode_cmd.width = mode->hdisplay;
10447 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010448 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10449 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010450 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010451
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010452 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10453 if (IS_ERR(fb))
10454 drm_gem_object_unreference_unlocked(&obj->base);
10455
10456 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010457}
10458
10459static struct drm_framebuffer *
10460mode_fits_in_fbdev(struct drm_device *dev,
10461 struct drm_display_mode *mode)
10462{
Daniel Vetter06957262015-08-10 13:34:08 +020010463#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsonfac5e232016-07-04 11:34:36 +010010464 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsond2dff872011-04-19 08:36:26 +010010465 struct drm_i915_gem_object *obj;
10466 struct drm_framebuffer *fb;
10467
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010468 if (!dev_priv->fbdev)
10469 return NULL;
10470
10471 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010472 return NULL;
10473
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010474 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010475 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010476
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010477 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010478 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10479 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010480 return NULL;
10481
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010482 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010483 return NULL;
10484
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010485 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010486 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010487#else
10488 return NULL;
10489#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010490}
10491
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010492static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10493 struct drm_crtc *crtc,
10494 struct drm_display_mode *mode,
10495 struct drm_framebuffer *fb,
10496 int x, int y)
10497{
10498 struct drm_plane_state *plane_state;
10499 int hdisplay, vdisplay;
10500 int ret;
10501
10502 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10503 if (IS_ERR(plane_state))
10504 return PTR_ERR(plane_state);
10505
10506 if (mode)
10507 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10508 else
10509 hdisplay = vdisplay = 0;
10510
10511 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10512 if (ret)
10513 return ret;
10514 drm_atomic_set_fb_for_plane(plane_state, fb);
10515 plane_state->crtc_x = 0;
10516 plane_state->crtc_y = 0;
10517 plane_state->crtc_w = hdisplay;
10518 plane_state->crtc_h = vdisplay;
10519 plane_state->src_x = x << 16;
10520 plane_state->src_y = y << 16;
10521 plane_state->src_w = hdisplay << 16;
10522 plane_state->src_h = vdisplay << 16;
10523
10524 return 0;
10525}
10526
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010527bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010528 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010529 struct intel_load_detect_pipe *old,
10530 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010531{
10532 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010533 struct intel_encoder *intel_encoder =
10534 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010535 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010536 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010537 struct drm_crtc *crtc = NULL;
10538 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010539 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010540 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010541 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010542 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010543 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010544 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010545
Chris Wilsond2dff872011-04-19 08:36:26 +010010546 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010547 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010548 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010549
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010550 old->restore_state = NULL;
10551
Rob Clark51fd3712013-11-19 12:10:12 -050010552retry:
10553 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10554 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010555 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010556
Jesse Barnes79e53942008-11-07 14:24:08 -080010557 /*
10558 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010559 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010560 * - if the connector already has an assigned crtc, use it (but make
10561 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010562 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010563 * - try to find the first unused crtc that can drive this connector,
10564 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010565 */
10566
10567 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010568 if (connector->state->crtc) {
10569 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010570
Rob Clark51fd3712013-11-19 12:10:12 -050010571 ret = drm_modeset_lock(&crtc->mutex, ctx);
10572 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010573 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010574
10575 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010576 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010577 }
10578
10579 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010580 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010581 i++;
10582 if (!(encoder->possible_crtcs & (1 << i)))
10583 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010584
10585 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10586 if (ret)
10587 goto fail;
10588
10589 if (possible_crtc->state->enable) {
10590 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010591 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010592 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010593
10594 crtc = possible_crtc;
10595 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010596 }
10597
10598 /*
10599 * If we didn't find an unused CRTC, don't use any.
10600 */
10601 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010602 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010603 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010604 }
10605
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010606found:
10607 intel_crtc = to_intel_crtc(crtc);
10608
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010609 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10610 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010611 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010612
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010613 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010614 restore_state = drm_atomic_state_alloc(dev);
10615 if (!state || !restore_state) {
10616 ret = -ENOMEM;
10617 goto fail;
10618 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010619
10620 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010621 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010622
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010623 connector_state = drm_atomic_get_connector_state(state, connector);
10624 if (IS_ERR(connector_state)) {
10625 ret = PTR_ERR(connector_state);
10626 goto fail;
10627 }
10628
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010629 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10630 if (ret)
10631 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010632
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010633 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10634 if (IS_ERR(crtc_state)) {
10635 ret = PTR_ERR(crtc_state);
10636 goto fail;
10637 }
10638
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010639 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010640
Chris Wilson64927112011-04-20 07:25:26 +010010641 if (!mode)
10642 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010643
Chris Wilsond2dff872011-04-19 08:36:26 +010010644 /* We need a framebuffer large enough to accommodate all accesses
10645 * that the plane may generate whilst we perform load detection.
10646 * We can not rely on the fbcon either being present (we get called
10647 * during its initialisation to detect all boot displays, or it may
10648 * not even exist) or that it is large enough to satisfy the
10649 * requested mode.
10650 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010651 fb = mode_fits_in_fbdev(dev, mode);
10652 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010653 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010654 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010655 } else
10656 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010657 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010658 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010659 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010660 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010661
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010662 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10663 if (ret)
10664 goto fail;
10665
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010666 drm_framebuffer_unreference(fb);
10667
10668 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10669 if (ret)
10670 goto fail;
10671
10672 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10673 if (!ret)
10674 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10675 if (!ret)
10676 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10677 if (ret) {
10678 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10679 goto fail;
10680 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010681
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010682 ret = drm_atomic_commit(state);
10683 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010684 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010685 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010686 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010687
10688 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010689
Jesse Barnes79e53942008-11-07 14:24:08 -080010690 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010691 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010692 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010693
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010694fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010695 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010696 drm_atomic_state_free(restore_state);
10697 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010698
Rob Clark51fd3712013-11-19 12:10:12 -050010699 if (ret == -EDEADLK) {
10700 drm_modeset_backoff(ctx);
10701 goto retry;
10702 }
10703
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010704 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010705}
10706
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010707void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010708 struct intel_load_detect_pipe *old,
10709 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010710{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010711 struct intel_encoder *intel_encoder =
10712 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010713 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010714 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010715 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010716
Chris Wilsond2dff872011-04-19 08:36:26 +010010717 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010718 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010719 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010720
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010721 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010722 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010723
10724 ret = drm_atomic_commit(state);
10725 if (ret) {
10726 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10727 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010728 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010729}
10730
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010731static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010732 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010733{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010734 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010735 u32 dpll = pipe_config->dpll_hw_state.dpll;
10736
10737 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010738 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010739 else if (HAS_PCH_SPLIT(dev))
10740 return 120000;
10741 else if (!IS_GEN2(dev))
10742 return 96000;
10743 else
10744 return 48000;
10745}
10746
Jesse Barnes79e53942008-11-07 14:24:08 -080010747/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010748static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010749 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010750{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010751 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010752 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010753 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010754 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010755 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030010756 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010757 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010758 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010759
10760 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010761 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010762 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010763 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010764
10765 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010766 if (IS_PINEVIEW(dev)) {
10767 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10768 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010769 } else {
10770 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10771 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10772 }
10773
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010774 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010775 if (IS_PINEVIEW(dev))
10776 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10777 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010778 else
10779 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010780 DPLL_FPA01_P1_POST_DIV_SHIFT);
10781
10782 switch (dpll & DPLL_MODE_MASK) {
10783 case DPLLB_MODE_DAC_SERIAL:
10784 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10785 5 : 10;
10786 break;
10787 case DPLLB_MODE_LVDS:
10788 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10789 7 : 14;
10790 break;
10791 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010792 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010793 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010794 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010795 }
10796
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010797 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010798 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010799 else
Imre Deakdccbea32015-06-22 23:35:51 +030010800 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010801 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010802 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010803 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010804
10805 if (is_lvds) {
10806 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10807 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010808
10809 if (lvds & LVDS_CLKB_POWER_UP)
10810 clock.p2 = 7;
10811 else
10812 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010813 } else {
10814 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10815 clock.p1 = 2;
10816 else {
10817 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10818 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10819 }
10820 if (dpll & PLL_P2_DIVIDE_BY_4)
10821 clock.p2 = 4;
10822 else
10823 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010824 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010825
Imre Deakdccbea32015-06-22 23:35:51 +030010826 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010827 }
10828
Ville Syrjälä18442d02013-09-13 16:00:08 +030010829 /*
10830 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010831 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010832 * encoder's get_config() function.
10833 */
Imre Deakdccbea32015-06-22 23:35:51 +030010834 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010835}
10836
Ville Syrjälä6878da02013-09-13 15:59:11 +030010837int intel_dotclock_calculate(int link_freq,
10838 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010839{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010840 /*
10841 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010842 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010843 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010844 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010845 *
10846 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010847 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010848 */
10849
Ville Syrjälä6878da02013-09-13 15:59:11 +030010850 if (!m_n->link_n)
10851 return 0;
10852
10853 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10854}
10855
Ville Syrjälä18442d02013-09-13 16:00:08 +030010856static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010857 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010858{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010859 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010860
10861 /* read out port_clock from the DPLL */
10862 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010863
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010864 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010865 * In case there is an active pipe without active ports,
10866 * we may need some idea for the dotclock anyway.
10867 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010868 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010869 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010870 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010871 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010872}
10873
10874/** Returns the currently programmed mode of the given pipe. */
10875struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10876 struct drm_crtc *crtc)
10877{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010878 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010879 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010880 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010881 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010882 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010883 int htot = I915_READ(HTOTAL(cpu_transcoder));
10884 int hsync = I915_READ(HSYNC(cpu_transcoder));
10885 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10886 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010887 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010888
10889 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10890 if (!mode)
10891 return NULL;
10892
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010893 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10894 if (!pipe_config) {
10895 kfree(mode);
10896 return NULL;
10897 }
10898
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010899 /*
10900 * Construct a pipe_config sufficient for getting the clock info
10901 * back out of crtc_clock_get.
10902 *
10903 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10904 * to use a real value here instead.
10905 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010906 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10907 pipe_config->pixel_multiplier = 1;
10908 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10909 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10910 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10911 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010912
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010913 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010914 mode->hdisplay = (htot & 0xffff) + 1;
10915 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10916 mode->hsync_start = (hsync & 0xffff) + 1;
10917 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10918 mode->vdisplay = (vtot & 0xffff) + 1;
10919 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10920 mode->vsync_start = (vsync & 0xffff) + 1;
10921 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10922
10923 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010924
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010925 kfree(pipe_config);
10926
Jesse Barnes79e53942008-11-07 14:24:08 -080010927 return mode;
10928}
10929
10930static void intel_crtc_destroy(struct drm_crtc *crtc)
10931{
10932 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010933 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010934 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010935
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010936 spin_lock_irq(&dev->event_lock);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010937 work = intel_crtc->flip_work;
10938 intel_crtc->flip_work = NULL;
10939 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010940
Daniel Vetter5a21b662016-05-24 17:13:53 +020010941 if (work) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010942 cancel_work_sync(&work->mmio_work);
10943 cancel_work_sync(&work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010944 kfree(work);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010945 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010946
10947 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010948
Jesse Barnes79e53942008-11-07 14:24:08 -080010949 kfree(intel_crtc);
10950}
10951
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010952static void intel_unpin_work_fn(struct work_struct *__work)
10953{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010954 struct intel_flip_work *work =
10955 container_of(__work, struct intel_flip_work, unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010956 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10957 struct drm_device *dev = crtc->base.dev;
10958 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010959
Daniel Vetter5a21b662016-05-24 17:13:53 +020010960 if (is_mmio_work(work))
10961 flush_work(&work->mmio_work);
10962
10963 mutex_lock(&dev->struct_mutex);
10964 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
10965 drm_gem_object_unreference(&work->pending_flip_obj->base);
10966
10967 if (work->flip_queued_req)
10968 i915_gem_request_assign(&work->flip_queued_req, NULL);
10969 mutex_unlock(&dev->struct_mutex);
10970
10971 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
10972 intel_fbc_post_update(crtc);
10973 drm_framebuffer_unreference(work->old_fb);
10974
10975 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10976 atomic_dec(&crtc->unpin_work_count);
10977
10978 kfree(work);
10979}
10980
10981/* Is 'a' after or equal to 'b'? */
10982static bool g4x_flip_count_after_eq(u32 a, u32 b)
10983{
10984 return !((a - b) & 0x80000000);
10985}
10986
10987static bool __pageflip_finished_cs(struct intel_crtc *crtc,
10988 struct intel_flip_work *work)
10989{
10990 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010991 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010992 unsigned reset_counter;
10993
10994 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
10995 if (crtc->reset_counter != reset_counter)
10996 return true;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010997
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010998 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020010999 * The relevant registers doen't exist on pre-ctg.
11000 * As the flip done interrupt doesn't trigger for mmio
11001 * flips on gmch platforms, a flip count check isn't
11002 * really needed there. But since ctg has the registers,
11003 * include it in the check anyway.
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011004 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011005 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11006 return true;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011007
Daniel Vetter5a21b662016-05-24 17:13:53 +020011008 /*
11009 * BDW signals flip done immediately if the plane
11010 * is disabled, even if the plane enable is already
11011 * armed to occur at the next vblank :(
11012 */
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011013
Daniel Vetter5a21b662016-05-24 17:13:53 +020011014 /*
11015 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11016 * used the same base address. In that case the mmio flip might
11017 * have completed, but the CS hasn't even executed the flip yet.
11018 *
11019 * A flip count check isn't enough as the CS might have updated
11020 * the base address just after start of vblank, but before we
11021 * managed to process the interrupt. This means we'd complete the
11022 * CS flip too soon.
11023 *
11024 * Combining both checks should get us a good enough result. It may
11025 * still happen that the CS flip has been executed, but has not
11026 * yet actually completed. But in case the base address is the same
11027 * anyway, we don't really care.
11028 */
11029 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11030 crtc->flip_work->gtt_offset &&
11031 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11032 crtc->flip_work->flip_count);
11033}
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011034
Daniel Vetter5a21b662016-05-24 17:13:53 +020011035static bool
11036__pageflip_finished_mmio(struct intel_crtc *crtc,
11037 struct intel_flip_work *work)
11038{
11039 /*
11040 * MMIO work completes when vblank is different from
11041 * flip_queued_vblank.
11042 *
11043 * Reset counter value doesn't matter, this is handled by
11044 * i915_wait_request finishing early, so no need to handle
11045 * reset here.
11046 */
11047 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011048}
11049
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011050
11051static bool pageflip_finished(struct intel_crtc *crtc,
11052 struct intel_flip_work *work)
11053{
11054 if (!atomic_read(&work->pending))
11055 return false;
11056
11057 smp_rmb();
11058
Daniel Vetter5a21b662016-05-24 17:13:53 +020011059 if (is_mmio_work(work))
11060 return __pageflip_finished_mmio(crtc, work);
11061 else
11062 return __pageflip_finished_cs(crtc, work);
11063}
11064
11065void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11066{
Chris Wilson91c8a322016-07-05 10:40:23 +010011067 struct drm_device *dev = &dev_priv->drm;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011068 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11069 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11070 struct intel_flip_work *work;
11071 unsigned long flags;
11072
11073 /* Ignore early vblank irqs */
11074 if (!crtc)
11075 return;
11076
Daniel Vetterf3260382014-09-15 14:55:23 +020011077 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011078 * This is called both by irq handlers and the reset code (to complete
11079 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000011080 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011081 spin_lock_irqsave(&dev->event_lock, flags);
11082 work = intel_crtc->flip_work;
11083
11084 if (work != NULL &&
11085 !is_mmio_work(work) &&
11086 pageflip_finished(intel_crtc, work))
11087 page_flip_completed(intel_crtc);
11088
11089 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011090}
11091
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011092void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011093{
Chris Wilson91c8a322016-07-05 10:40:23 +010011094 struct drm_device *dev = &dev_priv->drm;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011095 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11096 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11097 struct intel_flip_work *work;
11098 unsigned long flags;
11099
11100 /* Ignore early vblank irqs */
11101 if (!crtc)
11102 return;
11103
11104 /*
11105 * This is called both by irq handlers and the reset code (to complete
11106 * lost pageflips) so needs the full irqsave spinlocks.
11107 */
11108 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011109 work = intel_crtc->flip_work;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011110
Daniel Vetter5a21b662016-05-24 17:13:53 +020011111 if (work != NULL &&
11112 is_mmio_work(work) &&
11113 pageflip_finished(intel_crtc, work))
11114 page_flip_completed(intel_crtc);
Maarten Lankhorst68858432016-05-17 15:07:52 +020011115
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011116 spin_unlock_irqrestore(&dev->event_lock, flags);
11117}
11118
Daniel Vetter5a21b662016-05-24 17:13:53 +020011119static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11120 struct intel_flip_work *work)
11121{
11122 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
11123
11124 /* Ensure that the work item is consistent when activating it ... */
11125 smp_mb__before_atomic();
11126 atomic_set(&work->pending, 1);
11127}
11128
11129static int intel_gen2_queue_flip(struct drm_device *dev,
11130 struct drm_crtc *crtc,
11131 struct drm_framebuffer *fb,
11132 struct drm_i915_gem_object *obj,
11133 struct drm_i915_gem_request *req,
11134 uint32_t flags)
11135{
11136 struct intel_engine_cs *engine = req->engine;
11137 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11138 u32 flip_mask;
11139 int ret;
11140
11141 ret = intel_ring_begin(req, 6);
11142 if (ret)
11143 return ret;
11144
11145 /* Can't queue multiple flips, so wait for the previous
11146 * one to finish before executing the next.
11147 */
11148 if (intel_crtc->plane)
11149 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11150 else
11151 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11152 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11153 intel_ring_emit(engine, MI_NOOP);
11154 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11155 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11156 intel_ring_emit(engine, fb->pitches[0]);
11157 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11158 intel_ring_emit(engine, 0); /* aux display base address, unused */
11159
11160 return 0;
11161}
11162
11163static int intel_gen3_queue_flip(struct drm_device *dev,
11164 struct drm_crtc *crtc,
11165 struct drm_framebuffer *fb,
11166 struct drm_i915_gem_object *obj,
11167 struct drm_i915_gem_request *req,
11168 uint32_t flags)
11169{
11170 struct intel_engine_cs *engine = req->engine;
11171 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11172 u32 flip_mask;
11173 int ret;
11174
11175 ret = intel_ring_begin(req, 6);
11176 if (ret)
11177 return ret;
11178
11179 if (intel_crtc->plane)
11180 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11181 else
11182 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11183 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11184 intel_ring_emit(engine, MI_NOOP);
11185 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
11186 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11187 intel_ring_emit(engine, fb->pitches[0]);
11188 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11189 intel_ring_emit(engine, MI_NOOP);
11190
11191 return 0;
11192}
11193
11194static int intel_gen4_queue_flip(struct drm_device *dev,
11195 struct drm_crtc *crtc,
11196 struct drm_framebuffer *fb,
11197 struct drm_i915_gem_object *obj,
11198 struct drm_i915_gem_request *req,
11199 uint32_t flags)
11200{
11201 struct intel_engine_cs *engine = req->engine;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011202 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011203 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11204 uint32_t pf, pipesrc;
11205 int ret;
11206
11207 ret = intel_ring_begin(req, 4);
11208 if (ret)
11209 return ret;
11210
11211 /* i965+ uses the linear or tiled offsets from the
11212 * Display Registers (which do not change across a page-flip)
11213 * so we need only reprogram the base address.
11214 */
11215 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11216 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11217 intel_ring_emit(engine, fb->pitches[0]);
11218 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
11219 obj->tiling_mode);
11220
11221 /* XXX Enabling the panel-fitter across page-flip is so far
11222 * untested on non-native modes, so ignore it for now.
11223 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11224 */
11225 pf = 0;
11226 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11227 intel_ring_emit(engine, pf | pipesrc);
11228
11229 return 0;
11230}
11231
11232static int intel_gen6_queue_flip(struct drm_device *dev,
11233 struct drm_crtc *crtc,
11234 struct drm_framebuffer *fb,
11235 struct drm_i915_gem_object *obj,
11236 struct drm_i915_gem_request *req,
11237 uint32_t flags)
11238{
11239 struct intel_engine_cs *engine = req->engine;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011240 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11242 uint32_t pf, pipesrc;
11243 int ret;
11244
11245 ret = intel_ring_begin(req, 4);
11246 if (ret)
11247 return ret;
11248
11249 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11250 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11251 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11252 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11253
11254 /* Contrary to the suggestions in the documentation,
11255 * "Enable Panel Fitter" does not seem to be required when page
11256 * flipping with a non-native mode, and worse causes a normal
11257 * modeset to fail.
11258 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11259 */
11260 pf = 0;
11261 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11262 intel_ring_emit(engine, pf | pipesrc);
11263
11264 return 0;
11265}
11266
11267static int intel_gen7_queue_flip(struct drm_device *dev,
11268 struct drm_crtc *crtc,
11269 struct drm_framebuffer *fb,
11270 struct drm_i915_gem_object *obj,
11271 struct drm_i915_gem_request *req,
11272 uint32_t flags)
11273{
11274 struct intel_engine_cs *engine = req->engine;
11275 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11276 uint32_t plane_bit = 0;
11277 int len, ret;
11278
11279 switch (intel_crtc->plane) {
11280 case PLANE_A:
11281 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11282 break;
11283 case PLANE_B:
11284 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11285 break;
11286 case PLANE_C:
11287 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11288 break;
11289 default:
11290 WARN_ONCE(1, "unknown plane in flip command\n");
11291 return -ENODEV;
11292 }
11293
11294 len = 4;
11295 if (engine->id == RCS) {
11296 len += 6;
11297 /*
11298 * On Gen 8, SRM is now taking an extra dword to accommodate
11299 * 48bits addresses, and we need a NOOP for the batch size to
11300 * stay even.
11301 */
11302 if (IS_GEN8(dev))
11303 len += 2;
11304 }
11305
11306 /*
11307 * BSpec MI_DISPLAY_FLIP for IVB:
11308 * "The full packet must be contained within the same cache line."
11309 *
11310 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11311 * cacheline, if we ever start emitting more commands before
11312 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11313 * then do the cacheline alignment, and finally emit the
11314 * MI_DISPLAY_FLIP.
11315 */
11316 ret = intel_ring_cacheline_align(req);
11317 if (ret)
11318 return ret;
11319
11320 ret = intel_ring_begin(req, len);
11321 if (ret)
11322 return ret;
11323
11324 /* Unmask the flip-done completion message. Note that the bspec says that
11325 * we should do this for both the BCS and RCS, and that we must not unmask
11326 * more than one flip event at any time (or ensure that one flip message
11327 * can be sent by waiting for flip-done prior to queueing new flips).
11328 * Experimentation says that BCS works despite DERRMR masking all
11329 * flip-done completion events and that unmasking all planes at once
11330 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11331 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11332 */
11333 if (engine->id == RCS) {
11334 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11335 intel_ring_emit_reg(engine, DERRMR);
11336 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11337 DERRMR_PIPEB_PRI_FLIP_DONE |
11338 DERRMR_PIPEC_PRI_FLIP_DONE));
11339 if (IS_GEN8(dev))
11340 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
11341 MI_SRM_LRM_GLOBAL_GTT);
11342 else
11343 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
11344 MI_SRM_LRM_GLOBAL_GTT);
11345 intel_ring_emit_reg(engine, DERRMR);
11346 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
11347 if (IS_GEN8(dev)) {
11348 intel_ring_emit(engine, 0);
11349 intel_ring_emit(engine, MI_NOOP);
11350 }
11351 }
11352
11353 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11354 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11355 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11356 intel_ring_emit(engine, (MI_NOOP));
11357
11358 return 0;
11359}
11360
11361static bool use_mmio_flip(struct intel_engine_cs *engine,
11362 struct drm_i915_gem_object *obj)
11363{
Chris Wilsonc37efb92016-06-17 08:28:47 +010011364 struct reservation_object *resv;
11365
Daniel Vetter5a21b662016-05-24 17:13:53 +020011366 /*
11367 * This is not being used for older platforms, because
11368 * non-availability of flip done interrupt forces us to use
11369 * CS flips. Older platforms derive flip done using some clever
11370 * tricks involving the flip_pending status bits and vblank irqs.
11371 * So using MMIO flips there would disrupt this mechanism.
11372 */
11373
11374 if (engine == NULL)
11375 return true;
11376
11377 if (INTEL_GEN(engine->i915) < 5)
11378 return false;
11379
11380 if (i915.use_mmio_flip < 0)
11381 return false;
11382 else if (i915.use_mmio_flip > 0)
11383 return true;
11384 else if (i915.enable_execlists)
11385 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011386
11387 resv = i915_gem_object_get_dmabuf_resv(obj);
11388 if (resv && !reservation_object_test_signaled_rcu(resv, false))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011389 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011390
11391 return engine != i915_gem_request_get_engine(obj->last_write_req);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011392}
11393
11394static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11395 unsigned int rotation,
11396 struct intel_flip_work *work)
11397{
11398 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011399 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011400 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11401 const enum pipe pipe = intel_crtc->pipe;
11402 u32 ctl, stride, tile_height;
11403
11404 ctl = I915_READ(PLANE_CTL(pipe, 0));
11405 ctl &= ~PLANE_CTL_TILED_MASK;
11406 switch (fb->modifier[0]) {
11407 case DRM_FORMAT_MOD_NONE:
11408 break;
11409 case I915_FORMAT_MOD_X_TILED:
11410 ctl |= PLANE_CTL_TILED_X;
11411 break;
11412 case I915_FORMAT_MOD_Y_TILED:
11413 ctl |= PLANE_CTL_TILED_Y;
11414 break;
11415 case I915_FORMAT_MOD_Yf_TILED:
11416 ctl |= PLANE_CTL_TILED_YF;
11417 break;
11418 default:
11419 MISSING_CASE(fb->modifier[0]);
11420 }
11421
11422 /*
11423 * The stride is either expressed as a multiple of 64 bytes chunks for
11424 * linear buffers or in number of tiles for tiled buffers.
11425 */
11426 if (intel_rotation_90_or_270(rotation)) {
11427 /* stride = Surface height in tiles */
11428 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
11429 stride = DIV_ROUND_UP(fb->height, tile_height);
11430 } else {
11431 stride = fb->pitches[0] /
11432 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11433 fb->pixel_format);
11434 }
11435
11436 /*
11437 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11438 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11439 */
11440 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11441 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11442
11443 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11444 POSTING_READ(PLANE_SURF(pipe, 0));
11445}
11446
11447static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11448 struct intel_flip_work *work)
11449{
11450 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011451 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011452 struct intel_framebuffer *intel_fb =
11453 to_intel_framebuffer(intel_crtc->base.primary->fb);
11454 struct drm_i915_gem_object *obj = intel_fb->obj;
11455 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11456 u32 dspcntr;
11457
11458 dspcntr = I915_READ(reg);
11459
11460 if (obj->tiling_mode != I915_TILING_NONE)
11461 dspcntr |= DISPPLANE_TILED;
11462 else
11463 dspcntr &= ~DISPPLANE_TILED;
11464
11465 I915_WRITE(reg, dspcntr);
11466
11467 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11468 POSTING_READ(DSPSURF(intel_crtc->plane));
11469}
11470
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011471static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000011472{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011473 struct intel_flip_work *work =
11474 container_of(w, struct intel_flip_work, mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011475 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11476 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11477 struct intel_framebuffer *intel_fb =
11478 to_intel_framebuffer(crtc->base.primary->fb);
11479 struct drm_i915_gem_object *obj = intel_fb->obj;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011480 struct reservation_object *resv;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011481
11482 if (work->flip_queued_req)
11483 WARN_ON(__i915_wait_request(work->flip_queued_req,
11484 false, NULL,
11485 &dev_priv->rps.mmioflips));
11486
11487 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010011488 resv = i915_gem_object_get_dmabuf_resv(obj);
11489 if (resv)
11490 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
Daniel Vetter5a21b662016-05-24 17:13:53 +020011491 MAX_SCHEDULE_TIMEOUT) < 0);
11492
11493 intel_pipe_update_start(crtc);
11494
11495 if (INTEL_GEN(dev_priv) >= 9)
11496 skl_do_mmio_flip(crtc, work->rotation, work);
11497 else
11498 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11499 ilk_do_mmio_flip(crtc, work);
11500
11501 intel_pipe_update_end(crtc, work);
11502}
11503
11504static int intel_default_queue_flip(struct drm_device *dev,
11505 struct drm_crtc *crtc,
11506 struct drm_framebuffer *fb,
11507 struct drm_i915_gem_object *obj,
11508 struct drm_i915_gem_request *req,
11509 uint32_t flags)
11510{
11511 return -ENODEV;
11512}
11513
11514static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11515 struct intel_crtc *intel_crtc,
11516 struct intel_flip_work *work)
11517{
11518 u32 addr, vblank;
11519
11520 if (!atomic_read(&work->pending))
11521 return false;
11522
11523 smp_rmb();
11524
11525 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11526 if (work->flip_ready_vblank == 0) {
11527 if (work->flip_queued_req &&
Chris Wilsonf69a02c2016-07-01 17:23:16 +010011528 !i915_gem_request_completed(work->flip_queued_req))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011529 return false;
11530
11531 work->flip_ready_vblank = vblank;
11532 }
11533
11534 if (vblank - work->flip_ready_vblank < 3)
11535 return false;
11536
11537 /* Potential stall - if we see that the flip has happened,
11538 * assume a missed interrupt. */
11539 if (INTEL_GEN(dev_priv) >= 4)
11540 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11541 else
11542 addr = I915_READ(DSPADDR(intel_crtc->plane));
11543
11544 /* There is a potential issue here with a false positive after a flip
11545 * to the same address. We could address this by checking for a
11546 * non-incrementing frame counter.
11547 */
11548 return addr == work->gtt_offset;
11549}
11550
11551void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11552{
Chris Wilson91c8a322016-07-05 10:40:23 +010011553 struct drm_device *dev = &dev_priv->drm;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011554 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011555 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011556 struct intel_flip_work *work;
11557
11558 WARN_ON(!in_interrupt());
11559
11560 if (crtc == NULL)
11561 return;
11562
11563 spin_lock(&dev->event_lock);
11564 work = intel_crtc->flip_work;
11565
11566 if (work != NULL && !is_mmio_work(work) &&
11567 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11568 WARN_ONCE(1,
11569 "Kicking stuck page flip: queued at %d, now %d\n",
11570 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11571 page_flip_completed(intel_crtc);
11572 work = NULL;
11573 }
11574
11575 if (work != NULL && !is_mmio_work(work) &&
11576 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11577 intel_queue_rps_boost_for_request(work->flip_queued_req);
11578 spin_unlock(&dev->event_lock);
11579}
11580
11581static int intel_crtc_page_flip(struct drm_crtc *crtc,
11582 struct drm_framebuffer *fb,
11583 struct drm_pending_vblank_event *event,
11584 uint32_t page_flip_flags)
11585{
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011586 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011587 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011588 struct drm_framebuffer *old_fb = crtc->primary->fb;
11589 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11590 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11591 struct drm_plane *primary = crtc->primary;
11592 enum pipe pipe = intel_crtc->pipe;
11593 struct intel_flip_work *work;
11594 struct intel_engine_cs *engine;
11595 bool mmio_flip;
11596 struct drm_i915_gem_request *request = NULL;
11597 int ret;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011598
Daniel Vetter5a21b662016-05-24 17:13:53 +020011599 /*
11600 * drm_mode_page_flip_ioctl() should already catch this, but double
11601 * check to be safe. In the future we may enable pageflipping from
11602 * a disabled primary plane.
11603 */
11604 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11605 return -EBUSY;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011606
Daniel Vetter5a21b662016-05-24 17:13:53 +020011607 /* Can't change pixel format via MI display flips. */
11608 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11609 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011610
Daniel Vetter5a21b662016-05-24 17:13:53 +020011611 /*
11612 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11613 * Note that pitch changes could also affect these register.
11614 */
11615 if (INTEL_INFO(dev)->gen > 3 &&
11616 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11617 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11618 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011619
Daniel Vetter5a21b662016-05-24 17:13:53 +020011620 if (i915_terminally_wedged(&dev_priv->gpu_error))
11621 goto out_hang;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011622
Daniel Vetter5a21b662016-05-24 17:13:53 +020011623 work = kzalloc(sizeof(*work), GFP_KERNEL);
11624 if (work == NULL)
11625 return -ENOMEM;
11626
11627 work->event = event;
11628 work->crtc = crtc;
11629 work->old_fb = old_fb;
11630 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011631
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011632 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011633 if (ret)
11634 goto free_work;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011635
Daniel Vetter5a21b662016-05-24 17:13:53 +020011636 /* We borrow the event spin lock for protecting flip_work */
11637 spin_lock_irq(&dev->event_lock);
11638 if (intel_crtc->flip_work) {
11639 /* Before declaring the flip queue wedged, check if
11640 * the hardware completed the operation behind our backs.
11641 */
11642 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11643 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11644 page_flip_completed(intel_crtc);
11645 } else {
11646 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11647 spin_unlock_irq(&dev->event_lock);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011648
Daniel Vetter5a21b662016-05-24 17:13:53 +020011649 drm_crtc_vblank_put(crtc);
11650 kfree(work);
11651 return -EBUSY;
11652 }
11653 }
11654 intel_crtc->flip_work = work;
11655 spin_unlock_irq(&dev->event_lock);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011656
Daniel Vetter5a21b662016-05-24 17:13:53 +020011657 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11658 flush_workqueue(dev_priv->wq);
11659
11660 /* Reference the objects for the scheduled work. */
11661 drm_framebuffer_reference(work->old_fb);
11662 drm_gem_object_reference(&obj->base);
11663
11664 crtc->primary->fb = fb;
11665 update_state_fb(crtc->primary);
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020011666
11667 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
11668 to_intel_plane_state(primary->state));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011669
11670 work->pending_flip_obj = obj;
11671
11672 ret = i915_mutex_lock_interruptible(dev);
11673 if (ret)
11674 goto cleanup;
11675
11676 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11677 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11678 ret = -EIO;
11679 goto cleanup;
11680 }
11681
11682 atomic_inc(&intel_crtc->unpin_work_count);
11683
11684 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11685 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11686
11687 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11688 engine = &dev_priv->engine[BCS];
11689 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
11690 /* vlv: DISPLAY_FLIP fails to change tiling */
11691 engine = NULL;
11692 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11693 engine = &dev_priv->engine[BCS];
11694 } else if (INTEL_INFO(dev)->gen >= 7) {
11695 engine = i915_gem_request_get_engine(obj->last_write_req);
11696 if (engine == NULL || engine->id != RCS)
11697 engine = &dev_priv->engine[BCS];
11698 } else {
11699 engine = &dev_priv->engine[RCS];
11700 }
11701
11702 mmio_flip = use_mmio_flip(engine, obj);
11703
11704 /* When using CS flips, we want to emit semaphores between rings.
11705 * However, when using mmio flips we will create a task to do the
11706 * synchronisation, so all we want here is to pin the framebuffer
11707 * into the display plane and skip any waits.
11708 */
11709 if (!mmio_flip) {
11710 ret = i915_gem_object_sync(obj, engine, &request);
11711 if (!ret && !request) {
11712 request = i915_gem_request_alloc(engine, NULL);
11713 ret = PTR_ERR_OR_ZERO(request);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011714 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011715
Daniel Vetter5a21b662016-05-24 17:13:53 +020011716 if (ret)
11717 goto cleanup_pending;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011718 }
11719
Daniel Vetter5a21b662016-05-24 17:13:53 +020011720 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11721 if (ret)
11722 goto cleanup_pending;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011723
Daniel Vetter5a21b662016-05-24 17:13:53 +020011724 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11725 obj, 0);
11726 work->gtt_offset += intel_crtc->dspaddr_offset;
11727 work->rotation = crtc->primary->state->rotation;
11728
11729 if (mmio_flip) {
11730 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11731
11732 i915_gem_request_assign(&work->flip_queued_req,
11733 obj->last_write_req);
11734
11735 schedule_work(&work->mmio_work);
11736 } else {
11737 i915_gem_request_assign(&work->flip_queued_req, request);
11738 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11739 page_flip_flags);
11740 if (ret)
11741 goto cleanup_unpin;
11742
11743 intel_mark_page_flip_active(intel_crtc, work);
11744
11745 i915_add_request_no_flush(request);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011746 }
11747
Daniel Vetter5a21b662016-05-24 17:13:53 +020011748 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11749 to_intel_plane(primary)->frontbuffer_bit);
11750 mutex_unlock(&dev->struct_mutex);
11751
11752 intel_frontbuffer_flip_prepare(dev,
11753 to_intel_plane(primary)->frontbuffer_bit);
11754
11755 trace_i915_flip_request(intel_crtc->plane, obj);
11756
11757 return 0;
11758
11759cleanup_unpin:
11760 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11761cleanup_pending:
11762 if (!IS_ERR_OR_NULL(request))
11763 i915_add_request_no_flush(request);
11764 atomic_dec(&intel_crtc->unpin_work_count);
11765 mutex_unlock(&dev->struct_mutex);
11766cleanup:
11767 crtc->primary->fb = old_fb;
11768 update_state_fb(crtc->primary);
11769
11770 drm_gem_object_unreference_unlocked(&obj->base);
11771 drm_framebuffer_unreference(work->old_fb);
11772
11773 spin_lock_irq(&dev->event_lock);
11774 intel_crtc->flip_work = NULL;
11775 spin_unlock_irq(&dev->event_lock);
11776
11777 drm_crtc_vblank_put(crtc);
11778free_work:
11779 kfree(work);
11780
11781 if (ret == -EIO) {
11782 struct drm_atomic_state *state;
11783 struct drm_plane_state *plane_state;
11784
11785out_hang:
11786 state = drm_atomic_state_alloc(dev);
11787 if (!state)
11788 return -ENOMEM;
11789 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11790
11791retry:
11792 plane_state = drm_atomic_get_plane_state(state, primary);
11793 ret = PTR_ERR_OR_ZERO(plane_state);
11794 if (!ret) {
11795 drm_atomic_set_fb_for_plane(plane_state, fb);
11796
11797 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11798 if (!ret)
11799 ret = drm_atomic_commit(state);
11800 }
11801
11802 if (ret == -EDEADLK) {
11803 drm_modeset_backoff(state->acquire_ctx);
11804 drm_atomic_state_clear(state);
11805 goto retry;
11806 }
11807
11808 if (ret)
11809 drm_atomic_state_free(state);
11810
11811 if (ret == 0 && event) {
11812 spin_lock_irq(&dev->event_lock);
11813 drm_crtc_send_vblank_event(crtc, event);
11814 spin_unlock_irq(&dev->event_lock);
11815 }
11816 }
11817 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011818}
11819
Daniel Vetter5a21b662016-05-24 17:13:53 +020011820
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011821/**
11822 * intel_wm_need_update - Check whether watermarks need updating
11823 * @plane: drm plane
11824 * @state: new plane state
11825 *
11826 * Check current plane state versus the new one to determine whether
11827 * watermarks need to be recalculated.
11828 *
11829 * Returns true or false.
11830 */
11831static bool intel_wm_need_update(struct drm_plane *plane,
11832 struct drm_plane_state *state)
11833{
Matt Roperd21fbe82015-09-24 15:53:12 -070011834 struct intel_plane_state *new = to_intel_plane_state(state);
11835 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11836
11837 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011838 if (new->visible != cur->visible)
11839 return true;
11840
11841 if (!cur->base.fb || !new->base.fb)
11842 return false;
11843
11844 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11845 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011846 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11847 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11848 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11849 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011850 return true;
11851
11852 return false;
11853}
11854
Matt Roperd21fbe82015-09-24 15:53:12 -070011855static bool needs_scaling(struct intel_plane_state *state)
11856{
11857 int src_w = drm_rect_width(&state->src) >> 16;
11858 int src_h = drm_rect_height(&state->src) >> 16;
11859 int dst_w = drm_rect_width(&state->dst);
11860 int dst_h = drm_rect_height(&state->dst);
11861
11862 return (src_w != dst_w || src_h != dst_h);
11863}
11864
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011865int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11866 struct drm_plane_state *plane_state)
11867{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011868 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011869 struct drm_crtc *crtc = crtc_state->crtc;
11870 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11871 struct drm_plane *plane = plane_state->plane;
11872 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011873 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011874 struct intel_plane_state *old_plane_state =
11875 to_intel_plane_state(plane->state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011876 bool mode_changed = needs_modeset(crtc_state);
11877 bool was_crtc_enabled = crtc->state->active;
11878 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011879 bool turn_off, turn_on, visible, was_visible;
11880 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030011881 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011882
Chris Wilson84114992016-07-02 15:36:06 +010011883 if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011884 ret = skl_update_scaler_plane(
11885 to_intel_crtc_state(crtc_state),
11886 to_intel_plane_state(plane_state));
11887 if (ret)
11888 return ret;
11889 }
11890
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011891 was_visible = old_plane_state->visible;
11892 visible = to_intel_plane_state(plane_state)->visible;
11893
11894 if (!was_crtc_enabled && WARN_ON(was_visible))
11895 was_visible = false;
11896
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011897 /*
11898 * Visibility is calculated as if the crtc was on, but
11899 * after scaler setup everything depends on it being off
11900 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030011901 *
11902 * FIXME this is wrong for watermarks. Watermarks should also
11903 * be computed as if the pipe would be active. Perhaps move
11904 * per-plane wm computation to the .check_plane() hook, and
11905 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011906 */
11907 if (!is_crtc_enabled)
11908 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011909
11910 if (!was_visible && !visible)
11911 return 0;
11912
Maarten Lankhorste8861672016-02-24 11:24:26 +010011913 if (fb != old_plane_state->base.fb)
11914 pipe_config->fb_changed = true;
11915
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011916 turn_off = was_visible && (!visible || mode_changed);
11917 turn_on = visible && (!was_visible || mode_changed);
11918
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011919 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjälä78108b72016-05-27 20:59:19 +030011920 intel_crtc->base.base.id,
11921 intel_crtc->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011922 plane->base.id, plane->name,
11923 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011924
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011925 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
11926 plane->base.id, plane->name,
11927 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011928 turn_off, turn_on, mode_changed);
11929
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011930 if (turn_on) {
11931 pipe_config->update_wm_pre = true;
11932
11933 /* must disable cxsr around plane enable/disable */
11934 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11935 pipe_config->disable_cxsr = true;
11936 } else if (turn_off) {
11937 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011938
Ville Syrjälä852eb002015-06-24 22:00:07 +030011939 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011940 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011941 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011942 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011943 /* FIXME bollocks */
11944 pipe_config->update_wm_pre = true;
11945 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011946 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011947
Matt Ropered4a6a72016-02-23 17:20:13 -080011948 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011949 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11950 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011951 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11952
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011953 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011954 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011955
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011956 /*
11957 * WaCxSRDisabledForSpriteScaling:ivb
11958 *
11959 * cstate->update_wm was already set above, so this flag will
11960 * take effect when we commit and program watermarks.
11961 */
11962 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11963 needs_scaling(to_intel_plane_state(plane_state)) &&
11964 !needs_scaling(old_plane_state))
11965 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011966
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011967 return 0;
11968}
11969
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011970static bool encoders_cloneable(const struct intel_encoder *a,
11971 const struct intel_encoder *b)
11972{
11973 /* masks could be asymmetric, so check both ways */
11974 return a == b || (a->cloneable & (1 << b->type) &&
11975 b->cloneable & (1 << a->type));
11976}
11977
11978static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11979 struct intel_crtc *crtc,
11980 struct intel_encoder *encoder)
11981{
11982 struct intel_encoder *source_encoder;
11983 struct drm_connector *connector;
11984 struct drm_connector_state *connector_state;
11985 int i;
11986
11987 for_each_connector_in_state(state, connector, connector_state, i) {
11988 if (connector_state->crtc != &crtc->base)
11989 continue;
11990
11991 source_encoder =
11992 to_intel_encoder(connector_state->best_encoder);
11993 if (!encoders_cloneable(encoder, source_encoder))
11994 return false;
11995 }
11996
11997 return true;
11998}
11999
12000static bool check_encoder_cloning(struct drm_atomic_state *state,
12001 struct intel_crtc *crtc)
12002{
12003 struct intel_encoder *encoder;
12004 struct drm_connector *connector;
12005 struct drm_connector_state *connector_state;
12006 int i;
12007
12008 for_each_connector_in_state(state, connector, connector_state, i) {
12009 if (connector_state->crtc != &crtc->base)
12010 continue;
12011
12012 encoder = to_intel_encoder(connector_state->best_encoder);
12013 if (!check_single_encoder_cloning(state, crtc, encoder))
12014 return false;
12015 }
12016
12017 return true;
12018}
12019
12020static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12021 struct drm_crtc_state *crtc_state)
12022{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012023 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012024 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012025 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012026 struct intel_crtc_state *pipe_config =
12027 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012028 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012029 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012030 bool mode_changed = needs_modeset(crtc_state);
12031
12032 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
12033 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12034 return -EINVAL;
12035 }
12036
Ville Syrjälä852eb002015-06-24 22:00:07 +030012037 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012038 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012039
Maarten Lankhorstad421372015-06-15 12:33:42 +020012040 if (mode_changed && crtc_state->enable &&
12041 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012042 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012043 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12044 pipe_config);
12045 if (ret)
12046 return ret;
12047 }
12048
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012049 if (crtc_state->color_mgmt_changed) {
12050 ret = intel_color_check(crtc, crtc_state);
12051 if (ret)
12052 return ret;
12053 }
12054
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012055 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012056 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010012057 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080012058 if (ret) {
12059 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012060 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080012061 }
12062 }
12063
12064 if (dev_priv->display.compute_intermediate_wm &&
12065 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12066 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12067 return 0;
12068
12069 /*
12070 * Calculate 'intermediate' watermarks that satisfy both the
12071 * old state and the new state. We can program these
12072 * immediately.
12073 */
12074 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12075 intel_crtc,
12076 pipe_config);
12077 if (ret) {
12078 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12079 return ret;
12080 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070012081 } else if (dev_priv->display.compute_intermediate_wm) {
12082 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12083 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012084 }
12085
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012086 if (INTEL_INFO(dev)->gen >= 9) {
12087 if (mode_changed)
12088 ret = skl_update_scaler_crtc(pipe_config);
12089
12090 if (!ret)
12091 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12092 pipe_config);
12093 }
12094
12095 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012096}
12097
Jani Nikula65b38e02015-04-13 11:26:56 +030012098static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012099 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Daniel Vetter5a21b662016-05-24 17:13:53 +020012100 .atomic_begin = intel_begin_crtc_commit,
12101 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012102 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012103};
12104
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012105static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12106{
12107 struct intel_connector *connector;
12108
12109 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020012110 if (connector->base.state->crtc)
12111 drm_connector_unreference(&connector->base);
12112
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012113 if (connector->base.encoder) {
12114 connector->base.state->best_encoder =
12115 connector->base.encoder;
12116 connector->base.state->crtc =
12117 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020012118
12119 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012120 } else {
12121 connector->base.state->best_encoder = NULL;
12122 connector->base.state->crtc = NULL;
12123 }
12124 }
12125}
12126
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012127static void
Robin Schroereba905b2014-05-18 02:24:50 +020012128connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012129 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012130{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012131 int bpp = pipe_config->pipe_bpp;
12132
12133 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12134 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012135 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012136
12137 /* Don't use an invalid EDID bpc value */
12138 if (connector->base.display_info.bpc &&
12139 connector->base.display_info.bpc * 3 < bpp) {
12140 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12141 bpp, connector->base.display_info.bpc*3);
12142 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12143 }
12144
Jani Nikula013dd9e2016-01-13 16:35:20 +020012145 /* Clamp bpp to default limit on screens without EDID 1.4 */
12146 if (connector->base.display_info.bpc == 0) {
12147 int type = connector->base.connector_type;
12148 int clamp_bpp = 24;
12149
12150 /* Fall back to 18 bpp when DP sink capability is unknown. */
12151 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12152 type == DRM_MODE_CONNECTOR_eDP)
12153 clamp_bpp = 18;
12154
12155 if (bpp > clamp_bpp) {
12156 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12157 bpp, clamp_bpp);
12158 pipe_config->pipe_bpp = clamp_bpp;
12159 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012160 }
12161}
12162
12163static int
12164compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012165 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012166{
12167 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012168 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012169 struct drm_connector *connector;
12170 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012171 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012172
Wayne Boyer666a4532015-12-09 12:29:35 -080012173 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012174 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012175 else if (INTEL_INFO(dev)->gen >= 5)
12176 bpp = 12*3;
12177 else
12178 bpp = 8*3;
12179
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012180
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012181 pipe_config->pipe_bpp = bpp;
12182
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012183 state = pipe_config->base.state;
12184
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012185 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012186 for_each_connector_in_state(state, connector, connector_state, i) {
12187 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012188 continue;
12189
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012190 connected_sink_compute_bpp(to_intel_connector(connector),
12191 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012192 }
12193
12194 return bpp;
12195}
12196
Daniel Vetter644db712013-09-19 14:53:58 +020012197static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12198{
12199 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12200 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012201 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012202 mode->crtc_hdisplay, mode->crtc_hsync_start,
12203 mode->crtc_hsync_end, mode->crtc_htotal,
12204 mode->crtc_vdisplay, mode->crtc_vsync_start,
12205 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12206}
12207
Daniel Vetterc0b03412013-05-28 12:05:54 +020012208static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012209 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012210 const char *context)
12211{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012212 struct drm_device *dev = crtc->base.dev;
12213 struct drm_plane *plane;
12214 struct intel_plane *intel_plane;
12215 struct intel_plane_state *state;
12216 struct drm_framebuffer *fb;
12217
Ville Syrjälä78108b72016-05-27 20:59:19 +030012218 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12219 crtc->base.base.id, crtc->base.name,
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012220 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012221
Jani Nikulada205632016-03-15 21:51:10 +020012222 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012223 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12224 pipe_config->pipe_bpp, pipe_config->dither);
12225 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12226 pipe_config->has_pch_encoder,
12227 pipe_config->fdi_lanes,
12228 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12229 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12230 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012231 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
Ville Syrjälä37a56502016-06-22 21:57:04 +030012232 intel_crtc_has_dp_encoder(pipe_config),
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012233 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012234 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12235 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12236 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012237
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012238 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
Ville Syrjälä37a56502016-06-22 21:57:04 +030012239 intel_crtc_has_dp_encoder(pipe_config),
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012240 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012241 pipe_config->dp_m2_n2.gmch_m,
12242 pipe_config->dp_m2_n2.gmch_n,
12243 pipe_config->dp_m2_n2.link_m,
12244 pipe_config->dp_m2_n2.link_n,
12245 pipe_config->dp_m2_n2.tu);
12246
Daniel Vetter55072d12014-11-20 16:10:28 +010012247 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12248 pipe_config->has_audio,
12249 pipe_config->has_infoframe);
12250
Daniel Vetterc0b03412013-05-28 12:05:54 +020012251 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012252 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012253 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012254 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12255 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012256 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012257 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12258 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012259 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12260 crtc->num_scalers,
12261 pipe_config->scaler_state.scaler_users,
12262 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012263 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12264 pipe_config->gmch_pfit.control,
12265 pipe_config->gmch_pfit.pgm_ratios,
12266 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012267 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012268 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012269 pipe_config->pch_pfit.size,
12270 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012271 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012272 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012273
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012274 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012275 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012276 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012277 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012278 pipe_config->ddi_pll_sel,
12279 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012280 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012281 pipe_config->dpll_hw_state.pll0,
12282 pipe_config->dpll_hw_state.pll1,
12283 pipe_config->dpll_hw_state.pll2,
12284 pipe_config->dpll_hw_state.pll3,
12285 pipe_config->dpll_hw_state.pll6,
12286 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012287 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012288 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012289 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012290 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012291 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12292 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12293 pipe_config->ddi_pll_sel,
12294 pipe_config->dpll_hw_state.ctrl1,
12295 pipe_config->dpll_hw_state.cfgcr1,
12296 pipe_config->dpll_hw_state.cfgcr2);
12297 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012298 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 +010012299 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012300 pipe_config->dpll_hw_state.wrpll,
12301 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012302 } else {
12303 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12304 "fp0: 0x%x, fp1: 0x%x\n",
12305 pipe_config->dpll_hw_state.dpll,
12306 pipe_config->dpll_hw_state.dpll_md,
12307 pipe_config->dpll_hw_state.fp0,
12308 pipe_config->dpll_hw_state.fp1);
12309 }
12310
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012311 DRM_DEBUG_KMS("planes on this crtc\n");
12312 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12313 intel_plane = to_intel_plane(plane);
12314 if (intel_plane->pipe != crtc->pipe)
12315 continue;
12316
12317 state = to_intel_plane_state(plane->state);
12318 fb = state->base.fb;
12319 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012320 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12321 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012322 continue;
12323 }
12324
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012325 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12326 plane->base.id, plane->name);
12327 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12328 fb->base.id, fb->width, fb->height,
12329 drm_get_format_name(fb->pixel_format));
12330 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12331 state->scaler_id,
12332 state->src.x1 >> 16, state->src.y1 >> 16,
12333 drm_rect_width(&state->src) >> 16,
12334 drm_rect_height(&state->src) >> 16,
12335 state->dst.x1, state->dst.y1,
12336 drm_rect_width(&state->dst),
12337 drm_rect_height(&state->dst));
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012338 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012339}
12340
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012341static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012342{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012343 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012344 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012345 unsigned int used_ports = 0;
12346
12347 /*
12348 * Walk the connector list instead of the encoder
12349 * list to detect the problem on ddi platforms
12350 * where there's just one encoder per digital port.
12351 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012352 drm_for_each_connector(connector, dev) {
12353 struct drm_connector_state *connector_state;
12354 struct intel_encoder *encoder;
12355
12356 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12357 if (!connector_state)
12358 connector_state = connector->state;
12359
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012360 if (!connector_state->best_encoder)
12361 continue;
12362
12363 encoder = to_intel_encoder(connector_state->best_encoder);
12364
12365 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012366
12367 switch (encoder->type) {
12368 unsigned int port_mask;
12369 case INTEL_OUTPUT_UNKNOWN:
12370 if (WARN_ON(!HAS_DDI(dev)))
12371 break;
Ville Syrjäläcca05022016-06-22 21:57:06 +030012372 case INTEL_OUTPUT_DP:
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012373 case INTEL_OUTPUT_HDMI:
12374 case INTEL_OUTPUT_EDP:
12375 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12376
12377 /* the same port mustn't appear more than once */
12378 if (used_ports & port_mask)
12379 return false;
12380
12381 used_ports |= port_mask;
12382 default:
12383 break;
12384 }
12385 }
12386
12387 return true;
12388}
12389
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012390static void
12391clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12392{
12393 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012394 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012395 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012396 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012397 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012398 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012399
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012400 /* FIXME: before the switch to atomic started, a new pipe_config was
12401 * kzalloc'd. Code that depends on any field being zero should be
12402 * fixed, so that the crtc_state can be safely duplicated. For now,
12403 * only fields that are know to not cause problems are preserved. */
12404
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012405 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012406 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012407 shared_dpll = crtc_state->shared_dpll;
12408 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012409 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012410 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012411
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012412 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012413
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012414 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012415 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012416 crtc_state->shared_dpll = shared_dpll;
12417 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012418 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012419 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012420}
12421
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012422static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012423intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012424 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012425{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012426 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012427 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012428 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012429 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012430 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012431 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012432 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012433
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012434 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012435
Daniel Vettere143a212013-07-04 12:01:15 +020012436 pipe_config->cpu_transcoder =
12437 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012438
Imre Deak2960bc92013-07-30 13:36:32 +030012439 /*
12440 * Sanitize sync polarity flags based on requested ones. If neither
12441 * positive or negative polarity is requested, treat this as meaning
12442 * negative polarity.
12443 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012444 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012445 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012446 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012447
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012448 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012449 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012450 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012451
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012452 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12453 pipe_config);
12454 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012455 goto fail;
12456
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012457 /*
12458 * Determine the real pipe dimensions. Note that stereo modes can
12459 * increase the actual pipe size due to the frame doubling and
12460 * insertion of additional space for blanks between the frame. This
12461 * is stored in the crtc timings. We use the requested mode to do this
12462 * computation to clearly distinguish it from the adjusted mode, which
12463 * can be changed by the connectors in the below retry loop.
12464 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012465 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012466 &pipe_config->pipe_src_w,
12467 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012468
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012469 for_each_connector_in_state(state, connector, connector_state, i) {
12470 if (connector_state->crtc != crtc)
12471 continue;
12472
12473 encoder = to_intel_encoder(connector_state->best_encoder);
12474
12475 /*
12476 * Determine output_types before calling the .compute_config()
12477 * hooks so that the hooks can use this information safely.
12478 */
12479 pipe_config->output_types |= 1 << encoder->type;
12480 }
12481
Daniel Vettere29c22c2013-02-21 00:00:16 +010012482encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012483 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012484 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012485 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012486
Daniel Vetter135c81b2013-07-21 21:37:09 +020012487 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012488 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12489 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012490
Daniel Vetter7758a112012-07-08 19:40:39 +020012491 /* Pass our mode to the connectors and the CRTC to give them a chance to
12492 * adjust it according to limitations or connector properties, and also
12493 * a chance to reject the mode entirely.
12494 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012495 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012496 if (connector_state->crtc != crtc)
12497 continue;
12498
12499 encoder = to_intel_encoder(connector_state->best_encoder);
12500
Daniel Vetterefea6e82013-07-21 21:36:59 +020012501 if (!(encoder->compute_config(encoder, pipe_config))) {
12502 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012503 goto fail;
12504 }
12505 }
12506
Daniel Vetterff9a6752013-06-01 17:16:21 +020012507 /* Set default port clock if not overwritten by the encoder. Needs to be
12508 * done afterwards in case the encoder adjusts the mode. */
12509 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012510 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012511 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012512
Daniel Vettera43f6e02013-06-07 23:10:32 +020012513 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012514 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012515 DRM_DEBUG_KMS("CRTC fixup failed\n");
12516 goto fail;
12517 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012518
12519 if (ret == RETRY) {
12520 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12521 ret = -EINVAL;
12522 goto fail;
12523 }
12524
12525 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12526 retry = false;
12527 goto encoder_retry;
12528 }
12529
Daniel Vettere8fa4272015-08-12 11:43:34 +020012530 /* Dithering seems to not pass-through bits correctly when it should, so
12531 * only enable it on 6bpc panels. */
12532 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012533 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012534 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012535
Daniel Vetter7758a112012-07-08 19:40:39 +020012536fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012537 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012538}
12539
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012540static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012541intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012542{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012543 struct drm_crtc *crtc;
12544 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012545 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012546
Ville Syrjälä76688512014-01-10 11:28:06 +020012547 /* Double check state. */
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012548 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012549 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012550
12551 /* Update hwmode for vblank functions */
12552 if (crtc->state->active)
12553 crtc->hwmode = crtc->state->adjusted_mode;
12554 else
12555 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012556
12557 /*
12558 * Update legacy state to satisfy fbc code. This can
12559 * be removed when fbc uses the atomic state.
12560 */
12561 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12562 struct drm_plane_state *plane_state = crtc->primary->state;
12563
12564 crtc->primary->fb = plane_state->fb;
12565 crtc->x = plane_state->src_x >> 16;
12566 crtc->y = plane_state->src_y >> 16;
12567 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012568 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012569}
12570
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012571static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012572{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012573 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012574
12575 if (clock1 == clock2)
12576 return true;
12577
12578 if (!clock1 || !clock2)
12579 return false;
12580
12581 diff = abs(clock1 - clock2);
12582
12583 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12584 return true;
12585
12586 return false;
12587}
12588
Daniel Vetter25c5b262012-07-08 22:08:04 +020012589#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12590 list_for_each_entry((intel_crtc), \
12591 &(dev)->mode_config.crtc_list, \
12592 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012593 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012594
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012595static bool
12596intel_compare_m_n(unsigned int m, unsigned int n,
12597 unsigned int m2, unsigned int n2,
12598 bool exact)
12599{
12600 if (m == m2 && n == n2)
12601 return true;
12602
12603 if (exact || !m || !n || !m2 || !n2)
12604 return false;
12605
12606 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12607
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012608 if (n > n2) {
12609 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012610 m2 <<= 1;
12611 n2 <<= 1;
12612 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012613 } else if (n < n2) {
12614 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012615 m <<= 1;
12616 n <<= 1;
12617 }
12618 }
12619
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012620 if (n != n2)
12621 return false;
12622
12623 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012624}
12625
12626static bool
12627intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12628 struct intel_link_m_n *m2_n2,
12629 bool adjust)
12630{
12631 if (m_n->tu == m2_n2->tu &&
12632 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12633 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12634 intel_compare_m_n(m_n->link_m, m_n->link_n,
12635 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12636 if (adjust)
12637 *m2_n2 = *m_n;
12638
12639 return true;
12640 }
12641
12642 return false;
12643}
12644
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012645static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012646intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012647 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012648 struct intel_crtc_state *pipe_config,
12649 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012650{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012651 bool ret = true;
12652
12653#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12654 do { \
12655 if (!adjust) \
12656 DRM_ERROR(fmt, ##__VA_ARGS__); \
12657 else \
12658 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12659 } while (0)
12660
Daniel Vetter66e985c2013-06-05 13:34:20 +020012661#define PIPE_CONF_CHECK_X(name) \
12662 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012663 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012664 "(expected 0x%08x, found 0x%08x)\n", \
12665 current_config->name, \
12666 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012667 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012668 }
12669
Daniel Vetter08a24032013-04-19 11:25:34 +020012670#define PIPE_CONF_CHECK_I(name) \
12671 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012672 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012673 "(expected %i, found %i)\n", \
12674 current_config->name, \
12675 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012676 ret = false; \
12677 }
12678
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012679#define PIPE_CONF_CHECK_P(name) \
12680 if (current_config->name != pipe_config->name) { \
12681 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12682 "(expected %p, found %p)\n", \
12683 current_config->name, \
12684 pipe_config->name); \
12685 ret = false; \
12686 }
12687
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012688#define PIPE_CONF_CHECK_M_N(name) \
12689 if (!intel_compare_link_m_n(&current_config->name, \
12690 &pipe_config->name,\
12691 adjust)) { \
12692 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12693 "(expected tu %i gmch %i/%i link %i/%i, " \
12694 "found tu %i, gmch %i/%i link %i/%i)\n", \
12695 current_config->name.tu, \
12696 current_config->name.gmch_m, \
12697 current_config->name.gmch_n, \
12698 current_config->name.link_m, \
12699 current_config->name.link_n, \
12700 pipe_config->name.tu, \
12701 pipe_config->name.gmch_m, \
12702 pipe_config->name.gmch_n, \
12703 pipe_config->name.link_m, \
12704 pipe_config->name.link_n); \
12705 ret = false; \
12706 }
12707
Daniel Vetter55c561a2016-03-30 11:34:36 +020012708/* This is required for BDW+ where there is only one set of registers for
12709 * switching between high and low RR.
12710 * This macro can be used whenever a comparison has to be made between one
12711 * hw state and multiple sw state variables.
12712 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012713#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12714 if (!intel_compare_link_m_n(&current_config->name, \
12715 &pipe_config->name, adjust) && \
12716 !intel_compare_link_m_n(&current_config->alt_name, \
12717 &pipe_config->name, adjust)) { \
12718 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12719 "(expected tu %i gmch %i/%i link %i/%i, " \
12720 "or tu %i gmch %i/%i link %i/%i, " \
12721 "found tu %i, gmch %i/%i link %i/%i)\n", \
12722 current_config->name.tu, \
12723 current_config->name.gmch_m, \
12724 current_config->name.gmch_n, \
12725 current_config->name.link_m, \
12726 current_config->name.link_n, \
12727 current_config->alt_name.tu, \
12728 current_config->alt_name.gmch_m, \
12729 current_config->alt_name.gmch_n, \
12730 current_config->alt_name.link_m, \
12731 current_config->alt_name.link_n, \
12732 pipe_config->name.tu, \
12733 pipe_config->name.gmch_m, \
12734 pipe_config->name.gmch_n, \
12735 pipe_config->name.link_m, \
12736 pipe_config->name.link_n); \
12737 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012738 }
12739
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012740#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12741 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012742 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012743 "(expected %i, found %i)\n", \
12744 current_config->name & (mask), \
12745 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012746 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012747 }
12748
Ville Syrjälä5e550652013-09-06 23:29:07 +030012749#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12750 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012751 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012752 "(expected %i, found %i)\n", \
12753 current_config->name, \
12754 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012755 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012756 }
12757
Daniel Vetterbb760062013-06-06 14:55:52 +020012758#define PIPE_CONF_QUIRK(quirk) \
12759 ((current_config->quirks | pipe_config->quirks) & (quirk))
12760
Daniel Vettereccb1402013-05-22 00:50:22 +020012761 PIPE_CONF_CHECK_I(cpu_transcoder);
12762
Daniel Vetter08a24032013-04-19 11:25:34 +020012763 PIPE_CONF_CHECK_I(has_pch_encoder);
12764 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012765 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012766
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012767 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030012768 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012769
12770 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012771 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012772
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012773 if (current_config->has_drrs)
12774 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12775 } else
12776 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012777
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012778 PIPE_CONF_CHECK_X(output_types);
Jani Nikulaa65347b2015-11-27 12:21:46 +020012779
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012780 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12781 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12782 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12783 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12784 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12785 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012786
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012787 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12788 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12789 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12790 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12791 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12792 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012793
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012794 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012795 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012796 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012797 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012798 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012799 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012800
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012801 PIPE_CONF_CHECK_I(has_audio);
12802
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012803 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012804 DRM_MODE_FLAG_INTERLACE);
12805
Daniel Vetterbb760062013-06-06 14:55:52 +020012806 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012807 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012808 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012809 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012810 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012811 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012812 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012813 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012814 DRM_MODE_FLAG_NVSYNC);
12815 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012816
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012817 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012818 /* pfit ratios are autocomputed by the hw on gen4+ */
12819 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020012820 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012821 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012822
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012823 if (!adjust) {
12824 PIPE_CONF_CHECK_I(pipe_src_w);
12825 PIPE_CONF_CHECK_I(pipe_src_h);
12826
12827 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12828 if (current_config->pch_pfit.enabled) {
12829 PIPE_CONF_CHECK_X(pch_pfit.pos);
12830 PIPE_CONF_CHECK_X(pch_pfit.size);
12831 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012832
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012833 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12834 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012835
Jesse Barnese59150d2014-01-07 13:30:45 -080012836 /* BDW+ don't expose a synchronous way to read the state */
12837 if (IS_HASWELL(dev))
12838 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012839
Ville Syrjälä282740f2013-09-04 18:30:03 +030012840 PIPE_CONF_CHECK_I(double_wide);
12841
Daniel Vetter26804af2014-06-25 22:01:55 +030012842 PIPE_CONF_CHECK_X(ddi_pll_sel);
12843
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012844 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012845 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012846 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012847 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12848 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012849 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012850 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012851 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12852 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12853 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012854
Ville Syrjälä47eacba2016-04-12 22:14:35 +030012855 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12856 PIPE_CONF_CHECK_X(dsi_pll.div);
12857
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012858 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12859 PIPE_CONF_CHECK_I(pipe_bpp);
12860
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012861 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012862 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012863
Daniel Vetter66e985c2013-06-05 13:34:20 +020012864#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012865#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012866#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012867#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012868#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012869#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012870#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012871
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012872 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012873}
12874
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012875static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12876 const struct intel_crtc_state *pipe_config)
12877{
12878 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012879 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012880 &pipe_config->fdi_m_n);
12881 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12882
12883 /*
12884 * FDI already provided one idea for the dotclock.
12885 * Yell if the encoder disagrees.
12886 */
12887 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12888 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12889 fdi_dotclock, dotclock);
12890 }
12891}
12892
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012893static void verify_wm_state(struct drm_crtc *crtc,
12894 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000012895{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012896 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012897 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau08db6652014-11-04 17:06:52 +000012898 struct skl_ddb_allocation hw_ddb, *sw_ddb;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012899 struct skl_ddb_entry *hw_entry, *sw_entry;
12900 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12901 const enum pipe pipe = intel_crtc->pipe;
Damien Lespiau08db6652014-11-04 17:06:52 +000012902 int plane;
12903
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012904 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000012905 return;
12906
12907 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12908 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12909
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012910 /* planes */
12911 for_each_plane(dev_priv, pipe, plane) {
12912 hw_entry = &hw_ddb.plane[pipe][plane];
12913 sw_entry = &sw_ddb->plane[pipe][plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000012914
12915 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12916 continue;
12917
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012918 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12919 "(expected (%u,%u), found (%u,%u))\n",
12920 pipe_name(pipe), plane + 1,
12921 sw_entry->start, sw_entry->end,
12922 hw_entry->start, hw_entry->end);
12923 }
12924
12925 /* cursor */
12926 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12927 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
12928
12929 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012930 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12931 "(expected (%u,%u), found (%u,%u))\n",
12932 pipe_name(pipe),
12933 sw_entry->start, sw_entry->end,
12934 hw_entry->start, hw_entry->end);
12935 }
12936}
12937
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012938static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012939verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012940{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012941 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012942
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012943 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012944 struct drm_encoder *encoder = connector->encoder;
12945 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012946
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012947 if (state->crtc != crtc)
12948 continue;
12949
Daniel Vetter5a21b662016-05-24 17:13:53 +020012950 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012951
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012952 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012953 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012954 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012955}
12956
12957static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012958verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012959{
12960 struct intel_encoder *encoder;
12961 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012962
Damien Lespiaub2784e12014-08-05 11:29:37 +010012963 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012964 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012965 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012966
12967 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12968 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012969 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012970
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012971 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012972 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012973 continue;
12974 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012975
12976 I915_STATE_WARN(connector->base.state->crtc !=
12977 encoder->base.crtc,
12978 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012979 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012980
Rob Clarke2c719b2014-12-15 13:56:32 -050012981 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012982 "encoder's enabled state mismatch "
12983 "(expected %i, found %i)\n",
12984 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012985
12986 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012987 bool active;
12988
12989 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012990 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012991 "encoder detached but still enabled on pipe %c.\n",
12992 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012993 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012994 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012995}
12996
12997static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012998verify_crtc_state(struct drm_crtc *crtc,
12999 struct drm_crtc_state *old_crtc_state,
13000 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013001{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013002 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010013003 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013004 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013005 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13006 struct intel_crtc_state *pipe_config, *sw_config;
13007 struct drm_atomic_state *old_state;
13008 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013009
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013010 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020013011 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013012 pipe_config = to_intel_crtc_state(old_crtc_state);
13013 memset(pipe_config, 0, sizeof(*pipe_config));
13014 pipe_config->base.crtc = crtc;
13015 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013016
Ville Syrjälä78108b72016-05-27 20:59:19 +030013017 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013018
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013019 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013020
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013021 /* hw state is inconsistent with the pipe quirk */
13022 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13023 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13024 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013025
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013026 I915_STATE_WARN(new_crtc_state->active != active,
13027 "crtc active state doesn't match with hw state "
13028 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013029
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013030 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13031 "transitional active state does not match atomic hw state "
13032 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013033
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013034 for_each_encoder_on_crtc(dev, crtc, encoder) {
13035 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013036
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013037 active = encoder->get_hw_state(encoder, &pipe);
13038 I915_STATE_WARN(active != new_crtc_state->active,
13039 "[ENCODER:%i] active %i with crtc active %i\n",
13040 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013041
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013042 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13043 "Encoder connected to wrong pipe %c\n",
13044 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013045
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013046 if (active) {
13047 pipe_config->output_types |= 1 << encoder->type;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013048 encoder->get_config(encoder, pipe_config);
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013049 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013050 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013051
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013052 if (!new_crtc_state->active)
13053 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013054
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013055 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013056
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013057 sw_config = to_intel_crtc_state(crtc->state);
13058 if (!intel_pipe_config_compare(dev, sw_config,
13059 pipe_config, false)) {
13060 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13061 intel_dump_pipe_config(intel_crtc, pipe_config,
13062 "[hw state]");
13063 intel_dump_pipe_config(intel_crtc, sw_config,
13064 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013065 }
13066}
13067
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013068static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013069verify_single_dpll_state(struct drm_i915_private *dev_priv,
13070 struct intel_shared_dpll *pll,
13071 struct drm_crtc *crtc,
13072 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013073{
13074 struct intel_dpll_hw_state dpll_hw_state;
13075 unsigned crtc_mask;
13076 bool active;
13077
13078 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13079
13080 DRM_DEBUG_KMS("%s\n", pll->name);
13081
13082 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
13083
13084 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13085 I915_STATE_WARN(!pll->on && pll->active_mask,
13086 "pll in active use but not on in sw tracking\n");
13087 I915_STATE_WARN(pll->on && !pll->active_mask,
13088 "pll is on but not used by any active crtc\n");
13089 I915_STATE_WARN(pll->on != active,
13090 "pll on state mismatch (expected %i, found %i)\n",
13091 pll->on, active);
13092 }
13093
13094 if (!crtc) {
13095 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13096 "more active pll users than references: %x vs %x\n",
13097 pll->active_mask, pll->config.crtc_mask);
13098
13099 return;
13100 }
13101
13102 crtc_mask = 1 << drm_crtc_index(crtc);
13103
13104 if (new_state->active)
13105 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13106 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13107 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13108 else
13109 I915_STATE_WARN(pll->active_mask & crtc_mask,
13110 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13111 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13112
13113 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13114 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13115 crtc_mask, pll->config.crtc_mask);
13116
13117 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13118 &dpll_hw_state,
13119 sizeof(dpll_hw_state)),
13120 "pll hw state mismatch\n");
13121}
13122
13123static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013124verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13125 struct drm_crtc_state *old_crtc_state,
13126 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013127{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013128 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013129 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13130 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13131
13132 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013133 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013134
13135 if (old_state->shared_dpll &&
13136 old_state->shared_dpll != new_state->shared_dpll) {
13137 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13138 struct intel_shared_dpll *pll = old_state->shared_dpll;
13139
13140 I915_STATE_WARN(pll->active_mask & crtc_mask,
13141 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13142 pipe_name(drm_crtc_index(crtc)));
13143 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13144 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13145 pipe_name(drm_crtc_index(crtc)));
13146 }
13147}
13148
13149static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013150intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013151 struct drm_crtc_state *old_state,
13152 struct drm_crtc_state *new_state)
13153{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013154 if (!needs_modeset(new_state) &&
13155 !to_intel_crtc_state(new_state)->update_pipe)
13156 return;
13157
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013158 verify_wm_state(crtc, new_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013159 verify_connector_state(crtc->dev, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013160 verify_crtc_state(crtc, old_state, new_state);
13161 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013162}
13163
13164static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013165verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013166{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013167 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013168 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013169
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013170 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013171 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013172}
Daniel Vetter53589012013-06-05 13:34:16 +020013173
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013174static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013175intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013176{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013177 verify_encoder_state(dev);
13178 verify_connector_state(dev, NULL);
13179 verify_disabled_dpll_state(dev);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013180}
13181
Ville Syrjälä80715b22014-05-15 20:23:23 +030013182static void update_scanline_offset(struct intel_crtc *crtc)
13183{
13184 struct drm_device *dev = crtc->base.dev;
13185
13186 /*
13187 * The scanline counter increments at the leading edge of hsync.
13188 *
13189 * On most platforms it starts counting from vtotal-1 on the
13190 * first active line. That means the scanline counter value is
13191 * always one less than what we would expect. Ie. just after
13192 * start of vblank, which also occurs at start of hsync (on the
13193 * last active line), the scanline counter will read vblank_start-1.
13194 *
13195 * On gen2 the scanline counter starts counting from 1 instead
13196 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13197 * to keep the value positive), instead of adding one.
13198 *
13199 * On HSW+ the behaviour of the scanline counter depends on the output
13200 * type. For DP ports it behaves like most other platforms, but on HDMI
13201 * there's an extra 1 line difference. So we need to add two instead of
13202 * one to the value.
13203 */
13204 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013205 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013206 int vtotal;
13207
Ville Syrjälä124abe02015-09-08 13:40:45 +030013208 vtotal = adjusted_mode->crtc_vtotal;
13209 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013210 vtotal /= 2;
13211
13212 crtc->scanline_offset = vtotal - 1;
13213 } else if (HAS_DDI(dev) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +030013214 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013215 crtc->scanline_offset = 2;
13216 } else
13217 crtc->scanline_offset = 1;
13218}
13219
Maarten Lankhorstad421372015-06-15 12:33:42 +020013220static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013221{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013222 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013223 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013224 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013225 struct drm_crtc *crtc;
13226 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013227 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013228
13229 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013230 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013231
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013232 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013233 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013234 struct intel_shared_dpll *old_dpll =
13235 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013236
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013237 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013238 continue;
13239
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013240 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013241
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013242 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013243 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013244
Maarten Lankhorstad421372015-06-15 12:33:42 +020013245 if (!shared_dpll)
13246 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13247
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013248 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013249 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013250}
13251
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013252/*
13253 * This implements the workaround described in the "notes" section of the mode
13254 * set sequence documentation. When going from no pipes or single pipe to
13255 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13256 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13257 */
13258static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13259{
13260 struct drm_crtc_state *crtc_state;
13261 struct intel_crtc *intel_crtc;
13262 struct drm_crtc *crtc;
13263 struct intel_crtc_state *first_crtc_state = NULL;
13264 struct intel_crtc_state *other_crtc_state = NULL;
13265 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13266 int i;
13267
13268 /* look at all crtc's that are going to be enabled in during modeset */
13269 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13270 intel_crtc = to_intel_crtc(crtc);
13271
13272 if (!crtc_state->active || !needs_modeset(crtc_state))
13273 continue;
13274
13275 if (first_crtc_state) {
13276 other_crtc_state = to_intel_crtc_state(crtc_state);
13277 break;
13278 } else {
13279 first_crtc_state = to_intel_crtc_state(crtc_state);
13280 first_pipe = intel_crtc->pipe;
13281 }
13282 }
13283
13284 /* No workaround needed? */
13285 if (!first_crtc_state)
13286 return 0;
13287
13288 /* w/a possibly needed, check how many crtc's are already enabled. */
13289 for_each_intel_crtc(state->dev, intel_crtc) {
13290 struct intel_crtc_state *pipe_config;
13291
13292 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13293 if (IS_ERR(pipe_config))
13294 return PTR_ERR(pipe_config);
13295
13296 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13297
13298 if (!pipe_config->base.active ||
13299 needs_modeset(&pipe_config->base))
13300 continue;
13301
13302 /* 2 or more enabled crtcs means no need for w/a */
13303 if (enabled_pipe != INVALID_PIPE)
13304 return 0;
13305
13306 enabled_pipe = intel_crtc->pipe;
13307 }
13308
13309 if (enabled_pipe != INVALID_PIPE)
13310 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13311 else if (other_crtc_state)
13312 other_crtc_state->hsw_workaround_pipe = first_pipe;
13313
13314 return 0;
13315}
13316
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013317static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13318{
13319 struct drm_crtc *crtc;
13320 struct drm_crtc_state *crtc_state;
13321 int ret = 0;
13322
13323 /* add all active pipes to the state */
13324 for_each_crtc(state->dev, crtc) {
13325 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13326 if (IS_ERR(crtc_state))
13327 return PTR_ERR(crtc_state);
13328
13329 if (!crtc_state->active || needs_modeset(crtc_state))
13330 continue;
13331
13332 crtc_state->mode_changed = true;
13333
13334 ret = drm_atomic_add_affected_connectors(state, crtc);
13335 if (ret)
13336 break;
13337
13338 ret = drm_atomic_add_affected_planes(state, crtc);
13339 if (ret)
13340 break;
13341 }
13342
13343 return ret;
13344}
13345
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013346static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013347{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013348 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013349 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013350 struct drm_crtc *crtc;
13351 struct drm_crtc_state *crtc_state;
13352 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013353
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013354 if (!check_digital_port_conflicts(state)) {
13355 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13356 return -EINVAL;
13357 }
13358
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013359 intel_state->modeset = true;
13360 intel_state->active_crtcs = dev_priv->active_crtcs;
13361
13362 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13363 if (crtc_state->active)
13364 intel_state->active_crtcs |= 1 << i;
13365 else
13366 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070013367
13368 if (crtc_state->active != crtc->state->active)
13369 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013370 }
13371
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013372 /*
13373 * See if the config requires any additional preparation, e.g.
13374 * to adjust global state with pipes off. We need to do this
13375 * here so we can get the modeset_pipe updated config for the new
13376 * mode set on this crtc. For other crtcs we need to use the
13377 * adjusted_mode bits in the crtc directly.
13378 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013379 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030013380 if (!intel_state->cdclk_pll_vco)
Ville Syrjälä63911d72016-05-13 23:41:32 +030013381 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +030013382 if (!intel_state->cdclk_pll_vco)
13383 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013384
Clint Taylorc89e39f2016-05-13 23:41:21 +030013385 ret = dev_priv->display.modeset_calc_cdclk(state);
13386 if (ret < 0)
13387 return ret;
13388
13389 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013390 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013391 ret = intel_modeset_all_pipes(state);
13392
13393 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013394 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013395
13396 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13397 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013398 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013399 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013400
Maarten Lankhorstad421372015-06-15 12:33:42 +020013401 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013402
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013403 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013404 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013405
Maarten Lankhorstad421372015-06-15 12:33:42 +020013406 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013407}
13408
Matt Roperaa363132015-09-24 15:53:18 -070013409/*
13410 * Handle calculation of various watermark data at the end of the atomic check
13411 * phase. The code here should be run after the per-crtc and per-plane 'check'
13412 * handlers to ensure that all derived state has been updated.
13413 */
Matt Roper55994c22016-05-12 07:06:08 -070013414static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070013415{
13416 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070013417 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070013418
13419 /* Is there platform-specific watermark information to calculate? */
13420 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070013421 return dev_priv->display.compute_global_watermarks(state);
13422
13423 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070013424}
13425
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013426/**
13427 * intel_atomic_check - validate state object
13428 * @dev: drm device
13429 * @state: state to validate
13430 */
13431static int intel_atomic_check(struct drm_device *dev,
13432 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013433{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013434 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013435 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013436 struct drm_crtc *crtc;
13437 struct drm_crtc_state *crtc_state;
13438 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013439 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013440
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013441 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013442 if (ret)
13443 return ret;
13444
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013445 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013446 struct intel_crtc_state *pipe_config =
13447 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013448
13449 /* Catch I915_MODE_FLAG_INHERITED */
13450 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13451 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013452
Daniel Vetter26495482015-07-15 14:15:52 +020013453 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013454 continue;
13455
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013456 if (!crtc_state->enable) {
13457 any_ms = true;
13458 continue;
13459 }
13460
Daniel Vetter26495482015-07-15 14:15:52 +020013461 /* FIXME: For only active_changed we shouldn't need to do any
13462 * state recomputation at all. */
13463
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013464 ret = drm_atomic_add_affected_connectors(state, crtc);
13465 if (ret)
13466 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013467
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013468 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013469 if (ret) {
13470 intel_dump_pipe_config(to_intel_crtc(crtc),
13471 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013472 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013473 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013474
Jani Nikula73831232015-11-19 10:26:30 +020013475 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013476 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013477 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013478 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013479 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013480 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013481 }
13482
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013483 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020013484 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013485
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013486 ret = drm_atomic_add_affected_planes(state, crtc);
13487 if (ret)
13488 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013489
Daniel Vetter26495482015-07-15 14:15:52 +020013490 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13491 needs_modeset(crtc_state) ?
13492 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013493 }
13494
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013495 if (any_ms) {
13496 ret = intel_modeset_checks(state);
13497
13498 if (ret)
13499 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013500 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013501 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013502
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013503 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013504 if (ret)
13505 return ret;
13506
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013507 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070013508 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013509}
13510
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013511static int intel_atomic_prepare_commit(struct drm_device *dev,
13512 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013513 bool nonblock)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013514{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013515 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013516 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013517 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013518 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013519 struct drm_crtc *crtc;
13520 int i, ret;
13521
Daniel Vetter5a21b662016-05-24 17:13:53 +020013522 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13523 if (state->legacy_cursor_update)
13524 continue;
13525
13526 ret = intel_crtc_wait_for_pending_flips(crtc);
13527 if (ret)
13528 return ret;
13529
13530 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13531 flush_workqueue(dev_priv->wq);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013532 }
13533
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013534 ret = mutex_lock_interruptible(&dev->struct_mutex);
13535 if (ret)
13536 return ret;
13537
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013538 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013539 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013540
Dave Airlie21daaee2016-05-05 09:56:30 +100013541 if (!ret && !nonblock) {
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013542 for_each_plane_in_state(state, plane, plane_state, i) {
13543 struct intel_plane_state *intel_plane_state =
13544 to_intel_plane_state(plane_state);
13545
13546 if (!intel_plane_state->wait_req)
13547 continue;
13548
13549 ret = __i915_wait_request(intel_plane_state->wait_req,
Chris Wilson299259a2016-04-13 17:35:06 +010013550 true, NULL, NULL);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013551 if (ret) {
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013552 /* Any hang should be swallowed by the wait */
13553 WARN_ON(ret == -EIO);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013554 mutex_lock(&dev->struct_mutex);
13555 drm_atomic_helper_cleanup_planes(dev, state);
13556 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013557 break;
Chris Wilsonf7e58382016-04-13 17:35:07 +010013558 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013559 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013560 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013561
13562 return ret;
13563}
13564
Maarten Lankhorsta2991412016-05-17 15:07:48 +020013565u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13566{
13567 struct drm_device *dev = crtc->base.dev;
13568
13569 if (!dev->max_vblank_count)
13570 return drm_accurate_vblank_count(&crtc->base);
13571
13572 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13573}
13574
Daniel Vetter5a21b662016-05-24 17:13:53 +020013575static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13576 struct drm_i915_private *dev_priv,
13577 unsigned crtc_mask)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013578{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013579 unsigned last_vblank_count[I915_MAX_PIPES];
13580 enum pipe pipe;
13581 int ret;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013582
Daniel Vetter5a21b662016-05-24 17:13:53 +020013583 if (!crtc_mask)
13584 return;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013585
Daniel Vetter5a21b662016-05-24 17:13:53 +020013586 for_each_pipe(dev_priv, pipe) {
13587 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorste8861672016-02-24 11:24:26 +010013588
Daniel Vetter5a21b662016-05-24 17:13:53 +020013589 if (!((1 << pipe) & crtc_mask))
Maarten Lankhorste8861672016-02-24 11:24:26 +010013590 continue;
13591
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013592 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013593 if (WARN_ON(ret != 0)) {
13594 crtc_mask &= ~(1 << pipe);
13595 continue;
13596 }
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013597
Daniel Vetter5a21b662016-05-24 17:13:53 +020013598 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13599 }
13600
13601 for_each_pipe(dev_priv, pipe) {
13602 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13603 long lret;
13604
13605 if (!((1 << pipe) & crtc_mask))
13606 continue;
13607
13608 lret = wait_event_timeout(dev->vblank[pipe].queue,
13609 last_vblank_count[pipe] !=
13610 drm_crtc_vblank_count(crtc),
13611 msecs_to_jiffies(50));
13612
13613 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
13614
13615 drm_crtc_vblank_put(crtc);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013616 }
13617}
13618
Daniel Vetter5a21b662016-05-24 17:13:53 +020013619static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013620{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013621 /* fb updated, need to unpin old fb */
13622 if (crtc_state->fb_changed)
13623 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013624
Daniel Vetter5a21b662016-05-24 17:13:53 +020013625 /* wm changes, need vblank before final wm's */
13626 if (crtc_state->update_wm_post)
13627 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013628
Daniel Vetter5a21b662016-05-24 17:13:53 +020013629 /*
13630 * cxsr is re-enabled after vblank.
13631 * This is already handled by crtc_state->update_wm_post,
13632 * but added for clarity.
13633 */
13634 if (crtc_state->disable_cxsr)
13635 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013636
Daniel Vetter5a21b662016-05-24 17:13:53 +020013637 return false;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013638}
13639
Daniel Vetter94f05022016-06-14 18:01:00 +020013640static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013641{
Daniel Vetter94f05022016-06-14 18:01:00 +020013642 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013643 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013644 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013645 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013646 struct drm_crtc *crtc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013647 struct intel_crtc_state *intel_cstate;
Daniel Vetter94f05022016-06-14 18:01:00 +020013648 struct drm_plane *plane;
13649 struct drm_plane_state *plane_state;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013650 bool hw_check = intel_state->modeset;
13651 unsigned long put_domains[I915_MAX_PIPES] = {};
13652 unsigned crtc_vblank_mask = 0;
Daniel Vetter94f05022016-06-14 18:01:00 +020013653 int i, ret;
Daniel Vettera6778b32012-07-02 09:56:42 +020013654
Daniel Vetter94f05022016-06-14 18:01:00 +020013655 for_each_plane_in_state(state, plane, plane_state, i) {
13656 struct intel_plane_state *intel_plane_state =
13657 to_intel_plane_state(plane_state);
Daniel Vetterea0000f2016-06-13 16:13:46 +020013658
Daniel Vetter94f05022016-06-14 18:01:00 +020013659 if (!intel_plane_state->wait_req)
13660 continue;
13661
13662 ret = __i915_wait_request(intel_plane_state->wait_req,
13663 true, NULL, NULL);
13664 /* EIO should be eaten, and we can't get interrupted in the
13665 * worker, and blocking commits have waited already. */
13666 WARN_ON(ret);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013667 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013668
Daniel Vetterea0000f2016-06-13 16:13:46 +020013669 drm_atomic_helper_wait_for_dependencies(state);
13670
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013671 if (intel_state->modeset) {
13672 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13673 sizeof(intel_state->min_pixclk));
13674 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013675 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013676
13677 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013678 }
13679
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013680 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13682
Daniel Vetter5a21b662016-05-24 17:13:53 +020013683 if (needs_modeset(crtc->state) ||
13684 to_intel_crtc_state(crtc->state)->update_pipe) {
13685 hw_check = true;
13686
13687 put_domains[to_intel_crtc(crtc)->pipe] =
13688 modeset_get_crtc_power_domains(crtc,
13689 to_intel_crtc_state(crtc->state));
13690 }
13691
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013692 if (!needs_modeset(crtc->state))
13693 continue;
13694
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013695 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013696
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013697 if (old_crtc_state->active) {
13698 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013699 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013700 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013701 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013702 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013703
13704 /*
13705 * Underruns don't always raise
13706 * interrupts, so check manually.
13707 */
13708 intel_check_cpu_fifo_underruns(dev_priv);
13709 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013710
13711 if (!crtc->state->active)
13712 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013713 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013714 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013715
Daniel Vetterea9d7582012-07-10 10:42:52 +020013716 /* Only after disabling all output pipelines that will be changed can we
13717 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013718 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013719
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013720 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013721 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013722
13723 if (dev_priv->display.modeset_commit_cdclk &&
Clint Taylorc89e39f2016-05-13 23:41:21 +030013724 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013725 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013726 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013727
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013728 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013729 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013730
Daniel Vettera6778b32012-07-02 09:56:42 +020013731 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013732 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13734 bool modeset = needs_modeset(crtc->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013735 struct intel_crtc_state *pipe_config =
13736 to_intel_crtc_state(crtc->state);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013737
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013738 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013739 update_scanline_offset(to_intel_crtc(crtc));
13740 dev_priv->display.crtc_enable(crtc);
13741 }
13742
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013743 /* Complete events for now disable pipes here. */
13744 if (modeset && !crtc->state->active && crtc->state->event) {
13745 spin_lock_irq(&dev->event_lock);
13746 drm_crtc_send_vblank_event(crtc, crtc->state->event);
13747 spin_unlock_irq(&dev->event_lock);
13748
13749 crtc->state->event = NULL;
13750 }
13751
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013752 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013753 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013754
Daniel Vetter5a21b662016-05-24 17:13:53 +020013755 if (crtc->state->active &&
13756 drm_atomic_get_existing_plane_state(state, crtc->primary))
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020013757 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013758
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013759 if (crtc->state->active)
Daniel Vetter5a21b662016-05-24 17:13:53 +020013760 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013761
Daniel Vetter5a21b662016-05-24 17:13:53 +020013762 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13763 crtc_vblank_mask |= 1 << i;
Matt Ropered4a6a72016-02-23 17:20:13 -080013764 }
13765
Daniel Vetter94f05022016-06-14 18:01:00 +020013766 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13767 * already, but still need the state for the delayed optimization. To
13768 * fix this:
13769 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13770 * - schedule that vblank worker _before_ calling hw_done
13771 * - at the start of commit_tail, cancel it _synchrously
13772 * - switch over to the vblank wait helper in the core after that since
13773 * we don't need out special handling any more.
13774 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020013775 if (!state->legacy_cursor_update)
13776 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13777
13778 /*
13779 * Now that the vblank has passed, we can go ahead and program the
13780 * optimal watermarks on platforms that need two-step watermark
13781 * programming.
13782 *
13783 * TODO: Move this (and other cleanup) to an async worker eventually.
13784 */
13785 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13786 intel_cstate = to_intel_crtc_state(crtc->state);
13787
13788 if (dev_priv->display.optimize_watermarks)
13789 dev_priv->display.optimize_watermarks(intel_cstate);
13790 }
13791
13792 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13793 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13794
13795 if (put_domains[i])
13796 modeset_put_power_domains(dev_priv, put_domains[i]);
13797
13798 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13799 }
13800
Daniel Vetter94f05022016-06-14 18:01:00 +020013801 drm_atomic_helper_commit_hw_done(state);
13802
Daniel Vetter5a21b662016-05-24 17:13:53 +020013803 if (intel_state->modeset)
13804 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13805
13806 mutex_lock(&dev->struct_mutex);
13807 drm_atomic_helper_cleanup_planes(dev, state);
13808 mutex_unlock(&dev->struct_mutex);
13809
Daniel Vetterea0000f2016-06-13 16:13:46 +020013810 drm_atomic_helper_commit_cleanup_done(state);
13811
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013812 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013813
Mika Kuoppala75714942015-12-16 09:26:48 +020013814 /* As one of the primary mmio accessors, KMS has a high likelihood
13815 * of triggering bugs in unclaimed access. After we finish
13816 * modesetting, see if an error has been flagged, and if so
13817 * enable debugging for the next modeset - and hope we catch
13818 * the culprit.
13819 *
13820 * XXX note that we assume display power is on at this point.
13821 * This might hold true now but we need to add pm helper to check
13822 * unclaimed only when the hardware is on, as atomic commits
13823 * can happen also when the device is completely off.
13824 */
13825 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter94f05022016-06-14 18:01:00 +020013826}
13827
13828static void intel_atomic_commit_work(struct work_struct *work)
13829{
13830 struct drm_atomic_state *state = container_of(work,
13831 struct drm_atomic_state,
13832 commit_work);
13833 intel_atomic_commit_tail(state);
13834}
13835
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013836static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13837{
13838 struct drm_plane_state *old_plane_state;
13839 struct drm_plane *plane;
13840 struct drm_i915_gem_object *obj, *old_obj;
13841 struct intel_plane *intel_plane;
13842 int i;
13843
13844 mutex_lock(&state->dev->struct_mutex);
13845 for_each_plane_in_state(state, plane, old_plane_state, i) {
13846 obj = intel_fb_obj(plane->state->fb);
13847 old_obj = intel_fb_obj(old_plane_state->fb);
13848 intel_plane = to_intel_plane(plane);
13849
13850 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
13851 }
13852 mutex_unlock(&state->dev->struct_mutex);
13853}
13854
Daniel Vetter94f05022016-06-14 18:01:00 +020013855/**
13856 * intel_atomic_commit - commit validated state object
13857 * @dev: DRM device
13858 * @state: the top-level driver state object
13859 * @nonblock: nonblocking commit
13860 *
13861 * This function commits a top-level state object that has been validated
13862 * with drm_atomic_helper_check().
13863 *
13864 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13865 * nonblocking commits are only safe for pure plane updates. Everything else
13866 * should work though.
13867 *
13868 * RETURNS
13869 * Zero for success or -errno.
13870 */
13871static int intel_atomic_commit(struct drm_device *dev,
13872 struct drm_atomic_state *state,
13873 bool nonblock)
13874{
13875 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013876 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94f05022016-06-14 18:01:00 +020013877 int ret = 0;
13878
13879 if (intel_state->modeset && nonblock) {
13880 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
13881 return -EINVAL;
13882 }
13883
13884 ret = drm_atomic_helper_setup_commit(state, nonblock);
13885 if (ret)
13886 return ret;
13887
13888 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
13889
13890 ret = intel_atomic_prepare_commit(dev, state, nonblock);
13891 if (ret) {
13892 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13893 return ret;
13894 }
13895
13896 drm_atomic_helper_swap_state(state, true);
13897 dev_priv->wm.distrust_bios_wm = false;
13898 dev_priv->wm.skl_results = intel_state->wm_results;
13899 intel_shared_dpll_commit(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013900 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020013901
13902 if (nonblock)
13903 queue_work(system_unbound_wq, &state->commit_work);
13904 else
13905 intel_atomic_commit_tail(state);
Mika Kuoppala75714942015-12-16 09:26:48 +020013906
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013907 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013908}
13909
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013910void intel_crtc_restore_mode(struct drm_crtc *crtc)
13911{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013912 struct drm_device *dev = crtc->dev;
13913 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013914 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013915 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013916
13917 state = drm_atomic_state_alloc(dev);
13918 if (!state) {
Ville Syrjälä78108b72016-05-27 20:59:19 +030013919 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13920 crtc->base.id, crtc->name);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013921 return;
13922 }
13923
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013924 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013925
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013926retry:
13927 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13928 ret = PTR_ERR_OR_ZERO(crtc_state);
13929 if (!ret) {
13930 if (!crtc_state->active)
13931 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013932
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013933 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013934 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013935 }
13936
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013937 if (ret == -EDEADLK) {
13938 drm_atomic_state_clear(state);
13939 drm_modeset_backoff(state->acquire_ctx);
13940 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013941 }
13942
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013943 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013944out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013945 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013946}
13947
Daniel Vetter25c5b262012-07-08 22:08:04 +020013948#undef for_each_intel_crtc_masked
13949
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013950static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013951 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013952 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013953 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013954 .destroy = intel_crtc_destroy,
Chris Wilson527b6ab2016-06-24 13:44:03 +010013955 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013956 .atomic_duplicate_state = intel_crtc_duplicate_state,
13957 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013958};
13959
Matt Roper6beb8c232014-12-01 15:40:14 -080013960/**
13961 * intel_prepare_plane_fb - Prepare fb for usage on plane
13962 * @plane: drm plane to prepare for
13963 * @fb: framebuffer to prepare for presentation
13964 *
13965 * Prepares a framebuffer for usage on a display plane. Generally this
13966 * involves pinning the underlying object and updating the frontbuffer tracking
13967 * bits. Some older platforms need special physical address handling for
13968 * cursor planes.
13969 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013970 * Must be called with struct_mutex held.
13971 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013972 * Returns 0 on success, negative error code on failure.
13973 */
13974int
13975intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013976 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013977{
13978 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013979 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013980 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013981 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc37efb92016-06-17 08:28:47 +010013982 struct reservation_object *resv;
Matt Roper6beb8c232014-12-01 15:40:14 -080013983 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013984
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013985 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013986 return 0;
13987
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013988 if (old_obj) {
13989 struct drm_crtc_state *crtc_state =
13990 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
13991
13992 /* Big Hammer, we also need to ensure that any pending
13993 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
13994 * current scanout is retired before unpinning the old
13995 * framebuffer. Note that we rely on userspace rendering
13996 * into the buffer attached to the pipe they are waiting
13997 * on. If not, userspace generates a GPU hang with IPEHR
13998 * point to the MI_WAIT_FOR_EVENT.
13999 *
14000 * This should only fail upon a hung GPU, in which case we
14001 * can safely continue.
14002 */
14003 if (needs_modeset(crtc_state))
14004 ret = i915_gem_object_wait_rendering(old_obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014005 if (ret) {
14006 /* GPU hangs should have been swallowed by the wait */
14007 WARN_ON(ret == -EIO);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014008 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014009 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014010 }
14011
Chris Wilsonc37efb92016-06-17 08:28:47 +010014012 if (!obj)
14013 return 0;
14014
Daniel Vetter5a21b662016-05-24 17:13:53 +020014015 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010014016 resv = i915_gem_object_get_dmabuf_resv(obj);
14017 if (resv) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020014018 long lret;
14019
Chris Wilsonc37efb92016-06-17 08:28:47 +010014020 lret = reservation_object_wait_timeout_rcu(resv, false, true,
Daniel Vetter5a21b662016-05-24 17:13:53 +020014021 MAX_SCHEDULE_TIMEOUT);
14022 if (lret == -ERESTARTSYS)
14023 return lret;
14024
14025 WARN(lret < 0, "waiting returns %li\n", lret);
14026 }
14027
Chris Wilsonc37efb92016-06-17 08:28:47 +010014028 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080014029 INTEL_INFO(dev)->cursor_needs_physical) {
14030 int align = IS_I830(dev) ? 16 * 1024 : 256;
14031 ret = i915_gem_object_attach_phys(obj, align);
14032 if (ret)
14033 DRM_DEBUG_KMS("failed to attach phys object\n");
14034 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020014035 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080014036 }
14037
Chris Wilsonc37efb92016-06-17 08:28:47 +010014038 if (ret == 0) {
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014039 struct intel_plane_state *plane_state =
14040 to_intel_plane_state(new_state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014041
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014042 i915_gem_request_assign(&plane_state->wait_req,
14043 obj->last_write_req);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014044 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014045
Matt Roper6beb8c232014-12-01 15:40:14 -080014046 return ret;
14047}
14048
Matt Roper38f3ce32014-12-02 07:45:25 -080014049/**
14050 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14051 * @plane: drm plane to clean up for
14052 * @fb: old framebuffer that was on plane
14053 *
14054 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014055 *
14056 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080014057 */
14058void
14059intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000014060 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080014061{
14062 struct drm_device *dev = plane->dev;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014063 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014064 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14065 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080014066
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014067 old_intel_state = to_intel_plane_state(old_state);
14068
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014069 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080014070 return;
14071
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014072 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14073 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020014074 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014075
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014076 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070014077}
14078
Chandra Konduru6156a452015-04-27 13:48:39 -070014079int
14080skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14081{
14082 int max_scale;
Chandra Konduru6156a452015-04-27 13:48:39 -070014083 int crtc_clock, cdclk;
14084
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010014085 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070014086 return DRM_PLANE_HELPER_NO_SCALING;
14087
Chandra Konduru6156a452015-04-27 13:48:39 -070014088 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014089 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070014090
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010014091 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070014092 return DRM_PLANE_HELPER_NO_SCALING;
14093
14094 /*
14095 * skl max scale is lower of:
14096 * close to 3 but not 3, -1 is for that purpose
14097 * or
14098 * cdclk/crtc_clock
14099 */
14100 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14101
14102 return max_scale;
14103}
14104
Matt Roper465c1202014-05-29 08:06:54 -070014105static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014106intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014107 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014108 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014109{
Matt Roper2b875c22014-12-01 15:40:13 -080014110 struct drm_crtc *crtc = state->base.crtc;
14111 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070014112 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014113 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14114 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014115
Ville Syrjälä693bdc22016-01-15 20:46:53 +020014116 if (INTEL_INFO(plane->dev)->gen >= 9) {
14117 /* use scaler when colorkey is not required */
14118 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14119 min_scale = 1;
14120 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14121 }
Sonika Jindald8106362015-04-10 14:37:28 +053014122 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014123 }
Sonika Jindald8106362015-04-10 14:37:28 +053014124
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014125 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14126 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014127 state->base.rotation,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014128 min_scale, max_scale,
14129 can_position, true,
14130 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070014131}
14132
Daniel Vetter5a21b662016-05-24 17:13:53 +020014133static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14134 struct drm_crtc_state *old_crtc_state)
14135{
14136 struct drm_device *dev = crtc->dev;
14137 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14138 struct intel_crtc_state *old_intel_state =
14139 to_intel_crtc_state(old_crtc_state);
14140 bool modeset = needs_modeset(crtc->state);
14141
14142 /* Perform vblank evasion around commit operation */
14143 intel_pipe_update_start(intel_crtc);
14144
14145 if (modeset)
14146 return;
14147
14148 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14149 intel_color_set_csc(crtc->state);
14150 intel_color_load_luts(crtc->state);
14151 }
14152
14153 if (to_intel_crtc_state(crtc->state)->update_pipe)
14154 intel_update_pipe_config(intel_crtc, old_intel_state);
14155 else if (INTEL_INFO(dev)->gen >= 9)
14156 skl_detach_scalers(intel_crtc);
14157}
14158
14159static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14160 struct drm_crtc_state *old_crtc_state)
14161{
14162 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14163
14164 intel_pipe_update_end(intel_crtc, NULL);
14165}
14166
Matt Ropercf4c7c12014-12-04 10:27:42 -080014167/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014168 * intel_plane_destroy - destroy a plane
14169 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014170 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014171 * Common destruction function for all types of planes (primary, cursor,
14172 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014173 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014174void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014175{
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014176 if (!plane)
14177 return;
14178
Matt Roper465c1202014-05-29 08:06:54 -070014179 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014180 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070014181}
14182
Matt Roper65a3fea2015-01-21 16:35:42 -080014183const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014184 .update_plane = drm_atomic_helper_update_plane,
14185 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014186 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014187 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014188 .atomic_get_property = intel_plane_atomic_get_property,
14189 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014190 .atomic_duplicate_state = intel_plane_duplicate_state,
14191 .atomic_destroy_state = intel_plane_destroy_state,
14192
Matt Roper465c1202014-05-29 08:06:54 -070014193};
14194
14195static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14196 int pipe)
14197{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014198 struct intel_plane *primary = NULL;
14199 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014200 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020014201 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014202 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014203
14204 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014205 if (!primary)
14206 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070014207
Matt Roper8e7d6882015-01-21 16:35:41 -080014208 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014209 if (!state)
14210 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014211 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014212
Matt Roper465c1202014-05-29 08:06:54 -070014213 primary->can_scale = false;
14214 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014215 if (INTEL_INFO(dev)->gen >= 9) {
14216 primary->can_scale = true;
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070014217 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014218 }
Matt Roper465c1202014-05-29 08:06:54 -070014219 primary->pipe = pipe;
14220 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014221 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014222 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014223 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14224 primary->plane = !pipe;
14225
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014226 if (INTEL_INFO(dev)->gen >= 9) {
14227 intel_primary_formats = skl_primary_formats;
14228 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014229
14230 primary->update_plane = skylake_update_primary_plane;
14231 primary->disable_plane = skylake_disable_primary_plane;
14232 } else if (HAS_PCH_SPLIT(dev)) {
14233 intel_primary_formats = i965_primary_formats;
14234 num_formats = ARRAY_SIZE(i965_primary_formats);
14235
14236 primary->update_plane = ironlake_update_primary_plane;
14237 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014238 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014239 intel_primary_formats = i965_primary_formats;
14240 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014241
14242 primary->update_plane = i9xx_update_primary_plane;
14243 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014244 } else {
14245 intel_primary_formats = i8xx_primary_formats;
14246 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014247
14248 primary->update_plane = i9xx_update_primary_plane;
14249 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014250 }
14251
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014252 if (INTEL_INFO(dev)->gen >= 9)
14253 ret = drm_universal_plane_init(dev, &primary->base, 0,
14254 &intel_plane_funcs,
14255 intel_primary_formats, num_formats,
14256 DRM_PLANE_TYPE_PRIMARY,
14257 "plane 1%c", pipe_name(pipe));
14258 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14259 ret = drm_universal_plane_init(dev, &primary->base, 0,
14260 &intel_plane_funcs,
14261 intel_primary_formats, num_formats,
14262 DRM_PLANE_TYPE_PRIMARY,
14263 "primary %c", pipe_name(pipe));
14264 else
14265 ret = drm_universal_plane_init(dev, &primary->base, 0,
14266 &intel_plane_funcs,
14267 intel_primary_formats, num_formats,
14268 DRM_PLANE_TYPE_PRIMARY,
14269 "plane %c", plane_name(primary->plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014270 if (ret)
14271 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053014272
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014273 if (INTEL_INFO(dev)->gen >= 4)
14274 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014275
Matt Roperea2c67b2014-12-23 10:41:52 -080014276 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14277
Matt Roper465c1202014-05-29 08:06:54 -070014278 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014279
14280fail:
14281 kfree(state);
14282 kfree(primary);
14283
14284 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014285}
14286
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014287void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14288{
14289 if (!dev->mode_config.rotation_property) {
14290 unsigned long flags = BIT(DRM_ROTATE_0) |
14291 BIT(DRM_ROTATE_180);
14292
14293 if (INTEL_INFO(dev)->gen >= 9)
14294 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14295
14296 dev->mode_config.rotation_property =
14297 drm_mode_create_rotation_property(dev, flags);
14298 }
14299 if (dev->mode_config.rotation_property)
14300 drm_object_attach_property(&plane->base.base,
14301 dev->mode_config.rotation_property,
14302 plane->base.state->rotation);
14303}
14304
Matt Roper3d7d6512014-06-10 08:28:13 -070014305static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014306intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014307 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014308 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014309{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014310 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014311 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014312 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014313 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014314 unsigned stride;
14315 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014316
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014317 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14318 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014319 state->base.rotation,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014320 DRM_PLANE_HELPER_NO_SCALING,
14321 DRM_PLANE_HELPER_NO_SCALING,
14322 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014323 if (ret)
14324 return ret;
14325
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014326 /* if we want to turn off the cursor ignore width and height */
14327 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014328 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014329
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014330 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014331 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014332 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14333 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014334 return -EINVAL;
14335 }
14336
Matt Roperea2c67b2014-12-23 10:41:52 -080014337 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14338 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014339 DRM_DEBUG_KMS("buffer is too small\n");
14340 return -ENOMEM;
14341 }
14342
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014343 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014344 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014345 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014346 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014347
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014348 /*
14349 * There's something wrong with the cursor on CHV pipe C.
14350 * If it straddles the left edge of the screen then
14351 * moving it away from the edge or disabling it often
14352 * results in a pipe underrun, and often that can lead to
14353 * dead pipe (constant underrun reported, and it scans
14354 * out just a solid color). To recover from that, the
14355 * display power well must be turned off and on again.
14356 * Refuse the put the cursor into that compromised position.
14357 */
14358 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14359 state->visible && state->base.crtc_x < 0) {
14360 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14361 return -EINVAL;
14362 }
14363
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014364 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014365}
14366
Matt Roperf4a2cf22014-12-01 15:40:12 -080014367static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014368intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014369 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014370{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014371 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14372
14373 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014374 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014375}
14376
14377static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014378intel_update_cursor_plane(struct drm_plane *plane,
14379 const struct intel_crtc_state *crtc_state,
14380 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014381{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014382 struct drm_crtc *crtc = crtc_state->base.crtc;
14383 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014384 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014385 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014386 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014387
Matt Roperf4a2cf22014-12-01 15:40:12 -080014388 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014389 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014390 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014391 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014392 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014393 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014394
Gustavo Padovana912f122014-12-01 15:40:10 -080014395 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014396 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014397}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014398
Matt Roper3d7d6512014-06-10 08:28:13 -070014399static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14400 int pipe)
14401{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014402 struct intel_plane *cursor = NULL;
14403 struct intel_plane_state *state = NULL;
14404 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070014405
14406 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014407 if (!cursor)
14408 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070014409
Matt Roper8e7d6882015-01-21 16:35:41 -080014410 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014411 if (!state)
14412 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014413 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014414
Matt Roper3d7d6512014-06-10 08:28:13 -070014415 cursor->can_scale = false;
14416 cursor->max_downscale = 1;
14417 cursor->pipe = pipe;
14418 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014419 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014420 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014421 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014422 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014423
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014424 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14425 &intel_plane_funcs,
14426 intel_cursor_formats,
14427 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014428 DRM_PLANE_TYPE_CURSOR,
14429 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014430 if (ret)
14431 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014432
14433 if (INTEL_INFO(dev)->gen >= 4) {
14434 if (!dev->mode_config.rotation_property)
14435 dev->mode_config.rotation_property =
14436 drm_mode_create_rotation_property(dev,
14437 BIT(DRM_ROTATE_0) |
14438 BIT(DRM_ROTATE_180));
14439 if (dev->mode_config.rotation_property)
14440 drm_object_attach_property(&cursor->base.base,
14441 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014442 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014443 }
14444
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070014445 if (INTEL_INFO(dev)->gen >=9)
14446 state->scaler_id = -1;
14447
Matt Roperea2c67b2014-12-23 10:41:52 -080014448 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14449
Matt Roper3d7d6512014-06-10 08:28:13 -070014450 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014451
14452fail:
14453 kfree(state);
14454 kfree(cursor);
14455
14456 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014457}
14458
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014459static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14460 struct intel_crtc_state *crtc_state)
14461{
14462 int i;
14463 struct intel_scaler *intel_scaler;
14464 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14465
14466 for (i = 0; i < intel_crtc->num_scalers; i++) {
14467 intel_scaler = &scaler_state->scalers[i];
14468 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014469 intel_scaler->mode = PS_SCALER_MODE_DYN;
14470 }
14471
14472 scaler_state->scaler_id = -1;
14473}
14474
Hannes Ederb358d0a2008-12-18 21:18:47 +010014475static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014476{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014477 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014478 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014479 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014480 struct drm_plane *primary = NULL;
14481 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014482 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014483
Daniel Vetter955382f2013-09-19 14:05:45 +020014484 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014485 if (intel_crtc == NULL)
14486 return;
14487
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014488 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14489 if (!crtc_state)
14490 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014491 intel_crtc->config = crtc_state;
14492 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014493 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014494
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014495 /* initialize shared scalers */
14496 if (INTEL_INFO(dev)->gen >= 9) {
14497 if (pipe == PIPE_C)
14498 intel_crtc->num_scalers = 1;
14499 else
14500 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14501
14502 skl_init_scalers(dev, intel_crtc, crtc_state);
14503 }
14504
Matt Roper465c1202014-05-29 08:06:54 -070014505 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014506 if (!primary)
14507 goto fail;
14508
14509 cursor = intel_cursor_plane_create(dev, pipe);
14510 if (!cursor)
14511 goto fail;
14512
Matt Roper465c1202014-05-29 08:06:54 -070014513 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030014514 cursor, &intel_crtc_funcs,
14515 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070014516 if (ret)
14517 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014518
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014519 /*
14520 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014521 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014522 */
Jesse Barnes80824002009-09-10 15:28:06 -070014523 intel_crtc->pipe = pipe;
14524 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014525 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014526 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014527 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014528 }
14529
Chris Wilson4b0e3332014-05-30 16:35:26 +030014530 intel_crtc->cursor_base = ~0;
14531 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014532 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014533
Ville Syrjälä852eb002015-06-24 22:00:07 +030014534 intel_crtc->wm.cxsr_allowed = true;
14535
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014536 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14537 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14538 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14539 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14540
Jesse Barnes79e53942008-11-07 14:24:08 -080014541 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014542
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014543 intel_color_init(&intel_crtc->base);
14544
Daniel Vetter87b6b102014-05-15 15:33:46 +020014545 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014546 return;
14547
14548fail:
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014549 intel_plane_destroy(primary);
14550 intel_plane_destroy(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014551 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014552 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014553}
14554
Jesse Barnes752aa882013-10-31 18:55:49 +020014555enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14556{
14557 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014558 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014559
Rob Clark51fd3712013-11-19 12:10:12 -050014560 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014561
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014562 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014563 return INVALID_PIPE;
14564
14565 return to_intel_crtc(encoder->crtc)->pipe;
14566}
14567
Carl Worth08d7b3d2009-04-29 14:43:54 -070014568int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014569 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014570{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014571 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014572 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014573 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014574
Rob Clark7707e652014-07-17 23:30:04 -040014575 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010014576 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014577 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014578
Rob Clark7707e652014-07-17 23:30:04 -040014579 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014580 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014581
Daniel Vetterc05422d2009-08-11 16:05:30 +020014582 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014583}
14584
Daniel Vetter66a92782012-07-12 20:08:18 +020014585static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014586{
Daniel Vetter66a92782012-07-12 20:08:18 +020014587 struct drm_device *dev = encoder->base.dev;
14588 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014589 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014590 int entry = 0;
14591
Damien Lespiaub2784e12014-08-05 11:29:37 +010014592 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014593 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014594 index_mask |= (1 << entry);
14595
Jesse Barnes79e53942008-11-07 14:24:08 -080014596 entry++;
14597 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014598
Jesse Barnes79e53942008-11-07 14:24:08 -080014599 return index_mask;
14600}
14601
Chris Wilson4d302442010-12-14 19:21:29 +000014602static bool has_edp_a(struct drm_device *dev)
14603{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014604 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson4d302442010-12-14 19:21:29 +000014605
14606 if (!IS_MOBILE(dev))
14607 return false;
14608
14609 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14610 return false;
14611
Damien Lespiaue3589902014-02-07 19:12:50 +000014612 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014613 return false;
14614
14615 return true;
14616}
14617
Jesse Barnes84b4e042014-06-25 08:24:29 -070014618static bool intel_crt_present(struct drm_device *dev)
14619{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014620 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes84b4e042014-06-25 08:24:29 -070014621
Damien Lespiau884497e2013-12-03 13:56:23 +000014622 if (INTEL_INFO(dev)->gen >= 9)
14623 return false;
14624
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014625 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014626 return false;
14627
14628 if (IS_CHERRYVIEW(dev))
14629 return false;
14630
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014631 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14632 return false;
14633
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014634 /* DDI E can't be used if DDI A requires 4 lanes */
14635 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14636 return false;
14637
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014638 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014639 return false;
14640
14641 return true;
14642}
14643
Jesse Barnes79e53942008-11-07 14:24:08 -080014644static void intel_setup_outputs(struct drm_device *dev)
14645{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014646 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson4ef69c72010-09-09 15:14:28 +010014647 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014648 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014649
Imre Deak97a824e12016-06-21 11:51:47 +030014650 /*
14651 * intel_edp_init_connector() depends on this completing first, to
14652 * prevent the registeration of both eDP and LVDS and the incorrect
14653 * sharing of the PPS.
14654 */
Daniel Vetterc9093352013-06-06 22:22:47 +020014655 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014656
Jesse Barnes84b4e042014-06-25 08:24:29 -070014657 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014658 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014659
Vandana Kannanc776eb22014-08-19 12:05:01 +053014660 if (IS_BROXTON(dev)) {
14661 /*
14662 * FIXME: Broxton doesn't support port detection via the
14663 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14664 * detect the ports.
14665 */
14666 intel_ddi_init(dev, PORT_A);
14667 intel_ddi_init(dev, PORT_B);
14668 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014669
14670 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014671 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014672 int found;
14673
Jesse Barnesde31fac2015-03-06 15:53:32 -080014674 /*
14675 * Haswell uses DDI functions to detect digital outputs.
14676 * On SKL pre-D0 the strap isn't connected, so we assume
14677 * it's there.
14678 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014679 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014680 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014681 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014682 intel_ddi_init(dev, PORT_A);
14683
14684 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14685 * register */
14686 found = I915_READ(SFUSE_STRAP);
14687
14688 if (found & SFUSE_STRAP_DDIB_DETECTED)
14689 intel_ddi_init(dev, PORT_B);
14690 if (found & SFUSE_STRAP_DDIC_DETECTED)
14691 intel_ddi_init(dev, PORT_C);
14692 if (found & SFUSE_STRAP_DDID_DETECTED)
14693 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014694 /*
14695 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14696 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014697 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014698 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14699 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14700 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14701 intel_ddi_init(dev, PORT_E);
14702
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014703 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014704 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014705 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014706
14707 if (has_edp_a(dev))
14708 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014709
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014710 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014711 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014712 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014713 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014714 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014715 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014716 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014717 }
14718
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014719 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014720 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014721
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014722 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014723 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014724
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014725 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014726 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014727
Daniel Vetter270b3042012-10-27 15:52:05 +020014728 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014729 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014730 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014731 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010014732
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014733 /*
14734 * The DP_DETECTED bit is the latched state of the DDC
14735 * SDA pin at boot. However since eDP doesn't require DDC
14736 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14737 * eDP ports may have been muxed to an alternate function.
14738 * Thus we can't rely on the DP_DETECTED bit alone to detect
14739 * eDP ports. Consult the VBT as well as DP_DETECTED to
14740 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030014741 *
14742 * Sadly the straps seem to be missing sometimes even for HDMI
14743 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14744 * and VBT for the presence of the port. Additionally we can't
14745 * trust the port type the VBT declares as we've seen at least
14746 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014747 */
Chris Wilson457c52d2016-06-01 08:27:50 +010014748 has_edp = intel_dp_is_edp(dev, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014749 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14750 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010014751 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014752 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014753 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014754
Chris Wilson457c52d2016-06-01 08:27:50 +010014755 has_edp = intel_dp_is_edp(dev, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014756 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14757 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010014758 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014759 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014760 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014761
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014762 if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014763 /*
14764 * eDP not supported on port D,
14765 * so no need to worry about it
14766 */
14767 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14768 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014769 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014770 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
14771 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014772 }
14773
Jani Nikula3cfca972013-08-27 15:12:26 +030014774 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014775 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014776 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014777
Paulo Zanonie2debe92013-02-18 19:00:27 -030014778 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014779 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014780 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014781 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014782 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014783 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014784 }
Ma Ling27185ae2009-08-24 13:50:23 +080014785
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014786 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014787 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014788 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014789
14790 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014791
Paulo Zanonie2debe92013-02-18 19:00:27 -030014792 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014793 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014794 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014795 }
Ma Ling27185ae2009-08-24 13:50:23 +080014796
Paulo Zanonie2debe92013-02-18 19:00:27 -030014797 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014798
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014799 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014800 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014801 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014802 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014803 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014804 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014805 }
Ma Ling27185ae2009-08-24 13:50:23 +080014806
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014807 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014808 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014809 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014810 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014811 intel_dvo_init(dev);
14812
Zhenyu Wang103a1962009-11-27 11:44:36 +080014813 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014814 intel_tv_init(dev);
14815
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014816 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014817
Damien Lespiaub2784e12014-08-05 11:29:37 +010014818 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014819 encoder->base.possible_crtcs = encoder->crtc_mask;
14820 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014821 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014822 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014823
Paulo Zanonidde86e22012-12-01 12:04:25 -020014824 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014825
14826 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014827}
14828
14829static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14830{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014831 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014832 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014833
Daniel Vetteref2d6332014-02-10 18:00:38 +010014834 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014835 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014836 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014837 drm_gem_object_unreference(&intel_fb->obj->base);
14838 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014839 kfree(intel_fb);
14840}
14841
14842static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014843 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014844 unsigned int *handle)
14845{
14846 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014847 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014848
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014849 if (obj->userptr.mm) {
14850 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14851 return -EINVAL;
14852 }
14853
Chris Wilson05394f32010-11-08 19:18:58 +000014854 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014855}
14856
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014857static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14858 struct drm_file *file,
14859 unsigned flags, unsigned color,
14860 struct drm_clip_rect *clips,
14861 unsigned num_clips)
14862{
14863 struct drm_device *dev = fb->dev;
14864 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14865 struct drm_i915_gem_object *obj = intel_fb->obj;
14866
14867 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014868 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014869 mutex_unlock(&dev->struct_mutex);
14870
14871 return 0;
14872}
14873
Jesse Barnes79e53942008-11-07 14:24:08 -080014874static const struct drm_framebuffer_funcs intel_fb_funcs = {
14875 .destroy = intel_user_framebuffer_destroy,
14876 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014877 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014878};
14879
Damien Lespiaub3218032015-02-27 11:15:18 +000014880static
14881u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14882 uint32_t pixel_format)
14883{
14884 u32 gen = INTEL_INFO(dev)->gen;
14885
14886 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014887 int cpp = drm_format_plane_cpp(pixel_format, 0);
14888
Damien Lespiaub3218032015-02-27 11:15:18 +000014889 /* "The stride in bytes must not exceed the of the size of 8K
14890 * pixels and 32K bytes."
14891 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014892 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014893 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014894 return 32*1024;
14895 } else if (gen >= 4) {
14896 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14897 return 16*1024;
14898 else
14899 return 32*1024;
14900 } else if (gen >= 3) {
14901 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14902 return 8*1024;
14903 else
14904 return 16*1024;
14905 } else {
14906 /* XXX DSPC is limited to 4k tiled */
14907 return 8*1024;
14908 }
14909}
14910
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014911static int intel_framebuffer_init(struct drm_device *dev,
14912 struct intel_framebuffer *intel_fb,
14913 struct drm_mode_fb_cmd2 *mode_cmd,
14914 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014915{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014916 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014917 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014918 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014919 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014920
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014921 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14922
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014923 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14924 /* Enforce that fb modifier and tiling mode match, but only for
14925 * X-tiled. This is needed for FBC. */
14926 if (!!(obj->tiling_mode == I915_TILING_X) !=
14927 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14928 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14929 return -EINVAL;
14930 }
14931 } else {
14932 if (obj->tiling_mode == I915_TILING_X)
14933 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14934 else if (obj->tiling_mode == I915_TILING_Y) {
14935 DRM_DEBUG("No Y tiling for legacy addfb\n");
14936 return -EINVAL;
14937 }
14938 }
14939
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014940 /* Passed in modifier sanity checking. */
14941 switch (mode_cmd->modifier[0]) {
14942 case I915_FORMAT_MOD_Y_TILED:
14943 case I915_FORMAT_MOD_Yf_TILED:
14944 if (INTEL_INFO(dev)->gen < 9) {
14945 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14946 mode_cmd->modifier[0]);
14947 return -EINVAL;
14948 }
14949 case DRM_FORMAT_MOD_NONE:
14950 case I915_FORMAT_MOD_X_TILED:
14951 break;
14952 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014953 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14954 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014955 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014956 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014957
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014958 stride_alignment = intel_fb_stride_alignment(dev_priv,
14959 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014960 mode_cmd->pixel_format);
14961 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14962 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14963 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014964 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014965 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014966
Damien Lespiaub3218032015-02-27 11:15:18 +000014967 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14968 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014969 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014970 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14971 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014972 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014973 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014974 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014975 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014976
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014977 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014978 mode_cmd->pitches[0] != obj->stride) {
14979 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14980 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014981 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014982 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014983
Ville Syrjälä57779d02012-10-31 17:50:14 +020014984 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014985 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014986 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014987 case DRM_FORMAT_RGB565:
14988 case DRM_FORMAT_XRGB8888:
14989 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014990 break;
14991 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014992 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014993 DRM_DEBUG("unsupported pixel format: %s\n",
14994 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014995 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014996 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014997 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014998 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080014999 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15000 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015001 DRM_DEBUG("unsupported pixel format: %s\n",
15002 drm_get_format_name(mode_cmd->pixel_format));
15003 return -EINVAL;
15004 }
15005 break;
15006 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015007 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015008 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015009 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015010 DRM_DEBUG("unsupported pixel format: %s\n",
15011 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015012 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015013 }
Jesse Barnesb5626742011-06-24 12:19:27 -070015014 break;
Damien Lespiau75312082015-05-15 19:06:01 +010015015 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080015016 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010015017 DRM_DEBUG("unsupported pixel format: %s\n",
15018 drm_get_format_name(mode_cmd->pixel_format));
15019 return -EINVAL;
15020 }
15021 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020015022 case DRM_FORMAT_YUYV:
15023 case DRM_FORMAT_UYVY:
15024 case DRM_FORMAT_YVYU:
15025 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015026 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015027 DRM_DEBUG("unsupported pixel format: %s\n",
15028 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015029 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015030 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015031 break;
15032 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015033 DRM_DEBUG("unsupported pixel format: %s\n",
15034 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010015035 return -EINVAL;
15036 }
15037
Ville Syrjälä90f9a332012-10-31 17:50:19 +020015038 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15039 if (mode_cmd->offsets[0] != 0)
15040 return -EINVAL;
15041
Damien Lespiauec2c9812015-01-20 12:51:45 +000015042 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000015043 mode_cmd->pixel_format,
15044 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020015045 /* FIXME drm helper for size checks (especially planar formats)? */
15046 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
15047 return -EINVAL;
15048
Daniel Vetterc7d73f62012-12-13 23:38:38 +010015049 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15050 intel_fb->obj = obj;
15051
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020015052 intel_fill_fb_info(dev_priv, &intel_fb->base);
15053
Jesse Barnes79e53942008-11-07 14:24:08 -080015054 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15055 if (ret) {
15056 DRM_ERROR("framebuffer init failed %d\n", ret);
15057 return ret;
15058 }
15059
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020015060 intel_fb->obj->framebuffer_references++;
15061
Jesse Barnes79e53942008-11-07 14:24:08 -080015062 return 0;
15063}
15064
Jesse Barnes79e53942008-11-07 14:24:08 -080015065static struct drm_framebuffer *
15066intel_user_framebuffer_create(struct drm_device *dev,
15067 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020015068 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080015069{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015070 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000015071 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020015072 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080015073
Chris Wilsona8ad0bd2016-05-09 11:04:54 +010015074 obj = to_intel_bo(drm_gem_object_lookup(filp, mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000015075 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010015076 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080015077
Daniel Vetter92907cb2015-11-23 09:04:05 +010015078 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015079 if (IS_ERR(fb))
15080 drm_gem_object_unreference_unlocked(&obj->base);
15081
15082 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080015083}
15084
Daniel Vetter06957262015-08-10 13:34:08 +020015085#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020015086static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020015087{
15088}
15089#endif
15090
Jesse Barnes79e53942008-11-07 14:24:08 -080015091static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080015092 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020015093 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080015094 .atomic_check = intel_atomic_check,
15095 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020015096 .atomic_state_alloc = intel_atomic_state_alloc,
15097 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080015098};
15099
Imre Deak88212942016-03-16 13:38:53 +020015100/**
15101 * intel_init_display_hooks - initialize the display modesetting hooks
15102 * @dev_priv: device private
15103 */
15104void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070015105{
Imre Deak88212942016-03-16 13:38:53 +020015106 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015107 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015108 dev_priv->display.get_initial_plane_config =
15109 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015110 dev_priv->display.crtc_compute_clock =
15111 haswell_crtc_compute_clock;
15112 dev_priv->display.crtc_enable = haswell_crtc_enable;
15113 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015114 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015115 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015116 dev_priv->display.get_initial_plane_config =
15117 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020015118 dev_priv->display.crtc_compute_clock =
15119 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020015120 dev_priv->display.crtc_enable = haswell_crtc_enable;
15121 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015122 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015123 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015124 dev_priv->display.get_initial_plane_config =
15125 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020015126 dev_priv->display.crtc_compute_clock =
15127 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015128 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15129 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015130 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070015131 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015132 dev_priv->display.get_initial_plane_config =
15133 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015134 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15135 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15136 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15137 } else if (IS_VALLEYVIEW(dev_priv)) {
15138 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15139 dev_priv->display.get_initial_plane_config =
15140 i9xx_get_initial_plane_config;
15141 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070015142 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15143 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020015144 } else if (IS_G4X(dev_priv)) {
15145 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15146 dev_priv->display.get_initial_plane_config =
15147 i9xx_get_initial_plane_config;
15148 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15149 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15150 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020015151 } else if (IS_PINEVIEW(dev_priv)) {
15152 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15153 dev_priv->display.get_initial_plane_config =
15154 i9xx_get_initial_plane_config;
15155 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15156 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15157 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015158 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015159 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015160 dev_priv->display.get_initial_plane_config =
15161 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020015162 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015163 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15164 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015165 } else {
15166 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15167 dev_priv->display.get_initial_plane_config =
15168 i9xx_get_initial_plane_config;
15169 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15170 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15171 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070015172 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015173
Jesse Barnese70236a2009-09-21 10:42:27 -070015174 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020015175 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015176 dev_priv->display.get_display_clock_speed =
15177 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015178 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070015179 dev_priv->display.get_display_clock_speed =
15180 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015181 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015182 dev_priv->display.get_display_clock_speed =
15183 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015184 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015185 dev_priv->display.get_display_clock_speed =
15186 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015187 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070015188 dev_priv->display.get_display_clock_speed =
15189 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015190 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030015191 dev_priv->display.get_display_clock_speed =
15192 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015193 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15194 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015195 dev_priv->display.get_display_clock_speed =
15196 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015197 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015198 dev_priv->display.get_display_clock_speed =
15199 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015200 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015201 dev_priv->display.get_display_clock_speed =
15202 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015203 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015204 dev_priv->display.get_display_clock_speed =
15205 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015206 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015207 dev_priv->display.get_display_clock_speed =
15208 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015209 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015210 dev_priv->display.get_display_clock_speed =
15211 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015212 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015213 dev_priv->display.get_display_clock_speed =
15214 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015215 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015216 dev_priv->display.get_display_clock_speed =
15217 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015218 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015219 dev_priv->display.get_display_clock_speed =
15220 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015221 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015222 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030015223 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015224 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020015225 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070015226 dev_priv->display.get_display_clock_speed =
15227 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015228 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015229
Imre Deak88212942016-03-16 13:38:53 +020015230 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015231 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015232 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015233 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015234 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015235 /* FIXME: detect B0+ stepping and use auto training */
15236 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015237 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015238 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030015239 }
15240
15241 if (IS_BROADWELL(dev_priv)) {
15242 dev_priv->display.modeset_commit_cdclk =
15243 broadwell_modeset_commit_cdclk;
15244 dev_priv->display.modeset_calc_cdclk =
15245 broadwell_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015246 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015247 dev_priv->display.modeset_commit_cdclk =
15248 valleyview_modeset_commit_cdclk;
15249 dev_priv->display.modeset_calc_cdclk =
15250 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015251 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015252 dev_priv->display.modeset_commit_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015253 bxt_modeset_commit_cdclk;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015254 dev_priv->display.modeset_calc_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015255 bxt_modeset_calc_cdclk;
Clint Taylorc89e39f2016-05-13 23:41:21 +030015256 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15257 dev_priv->display.modeset_commit_cdclk =
15258 skl_modeset_commit_cdclk;
15259 dev_priv->display.modeset_calc_cdclk =
15260 skl_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015261 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020015262
15263 switch (INTEL_INFO(dev_priv)->gen) {
15264 case 2:
15265 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15266 break;
15267
15268 case 3:
15269 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15270 break;
15271
15272 case 4:
15273 case 5:
15274 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15275 break;
15276
15277 case 6:
15278 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15279 break;
15280 case 7:
15281 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15282 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15283 break;
15284 case 9:
15285 /* Drop through - unsupported since execlist only. */
15286 default:
15287 /* Default just returns -ENODEV to indicate unsupported */
15288 dev_priv->display.queue_flip = intel_default_queue_flip;
15289 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015290}
15291
Jesse Barnesb690e962010-07-19 13:53:12 -070015292/*
15293 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15294 * resume, or other times. This quirk makes sure that's the case for
15295 * affected systems.
15296 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015297static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015298{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015299 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb690e962010-07-19 13:53:12 -070015300
15301 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015302 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015303}
15304
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015305static void quirk_pipeb_force(struct drm_device *dev)
15306{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015307 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015308
15309 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15310 DRM_INFO("applying pipe b force quirk\n");
15311}
15312
Keith Packard435793d2011-07-12 14:56:22 -070015313/*
15314 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15315 */
15316static void quirk_ssc_force_disable(struct drm_device *dev)
15317{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015318 struct drm_i915_private *dev_priv = to_i915(dev);
Keith Packard435793d2011-07-12 14:56:22 -070015319 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015320 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015321}
15322
Carsten Emde4dca20e2012-03-15 15:56:26 +010015323/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015324 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15325 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015326 */
15327static void quirk_invert_brightness(struct drm_device *dev)
15328{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015329 struct drm_i915_private *dev_priv = to_i915(dev);
Carsten Emde4dca20e2012-03-15 15:56:26 +010015330 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015331 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015332}
15333
Scot Doyle9c72cc62014-07-03 23:27:50 +000015334/* Some VBT's incorrectly indicate no backlight is present */
15335static void quirk_backlight_present(struct drm_device *dev)
15336{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015337 struct drm_i915_private *dev_priv = to_i915(dev);
Scot Doyle9c72cc62014-07-03 23:27:50 +000015338 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15339 DRM_INFO("applying backlight present quirk\n");
15340}
15341
Jesse Barnesb690e962010-07-19 13:53:12 -070015342struct intel_quirk {
15343 int device;
15344 int subsystem_vendor;
15345 int subsystem_device;
15346 void (*hook)(struct drm_device *dev);
15347};
15348
Egbert Eich5f85f172012-10-14 15:46:38 +020015349/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15350struct intel_dmi_quirk {
15351 void (*hook)(struct drm_device *dev);
15352 const struct dmi_system_id (*dmi_id_list)[];
15353};
15354
15355static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15356{
15357 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15358 return 1;
15359}
15360
15361static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15362 {
15363 .dmi_id_list = &(const struct dmi_system_id[]) {
15364 {
15365 .callback = intel_dmi_reverse_brightness,
15366 .ident = "NCR Corporation",
15367 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15368 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15369 },
15370 },
15371 { } /* terminating entry */
15372 },
15373 .hook = quirk_invert_brightness,
15374 },
15375};
15376
Ben Widawskyc43b5632012-04-16 14:07:40 -070015377static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015378 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15379 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15380
Jesse Barnesb690e962010-07-19 13:53:12 -070015381 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15382 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15383
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015384 /* 830 needs to leave pipe A & dpll A up */
15385 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15386
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015387 /* 830 needs to leave pipe B & dpll B up */
15388 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15389
Keith Packard435793d2011-07-12 14:56:22 -070015390 /* Lenovo U160 cannot use SSC on LVDS */
15391 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015392
15393 /* Sony Vaio Y cannot use SSC on LVDS */
15394 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015395
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015396 /* Acer Aspire 5734Z must invert backlight brightness */
15397 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15398
15399 /* Acer/eMachines G725 */
15400 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15401
15402 /* Acer/eMachines e725 */
15403 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15404
15405 /* Acer/Packard Bell NCL20 */
15406 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15407
15408 /* Acer Aspire 4736Z */
15409 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015410
15411 /* Acer Aspire 5336 */
15412 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015413
15414 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15415 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015416
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015417 /* Acer C720 Chromebook (Core i3 4005U) */
15418 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15419
jens steinb2a96012014-10-28 20:25:53 +010015420 /* Apple Macbook 2,1 (Core 2 T7400) */
15421 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15422
Jani Nikula1b9448b2015-11-05 11:49:59 +020015423 /* Apple Macbook 4,1 */
15424 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15425
Scot Doyled4967d82014-07-03 23:27:52 +000015426 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15427 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015428
15429 /* HP Chromebook 14 (Celeron 2955U) */
15430 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015431
15432 /* Dell Chromebook 11 */
15433 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015434
15435 /* Dell Chromebook 11 (2015 version) */
15436 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015437};
15438
15439static void intel_init_quirks(struct drm_device *dev)
15440{
15441 struct pci_dev *d = dev->pdev;
15442 int i;
15443
15444 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15445 struct intel_quirk *q = &intel_quirks[i];
15446
15447 if (d->device == q->device &&
15448 (d->subsystem_vendor == q->subsystem_vendor ||
15449 q->subsystem_vendor == PCI_ANY_ID) &&
15450 (d->subsystem_device == q->subsystem_device ||
15451 q->subsystem_device == PCI_ANY_ID))
15452 q->hook(dev);
15453 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015454 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15455 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15456 intel_dmi_quirks[i].hook(dev);
15457 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015458}
15459
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015460/* Disable the VGA plane that we never use */
15461static void i915_disable_vga(struct drm_device *dev)
15462{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015463 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015464 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015465 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015466
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015467 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015468 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015469 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015470 sr1 = inb(VGA_SR_DATA);
15471 outb(sr1 | 1<<5, VGA_SR_DATA);
15472 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15473 udelay(300);
15474
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015475 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015476 POSTING_READ(vga_reg);
15477}
15478
Daniel Vetterf8175862012-04-10 15:50:11 +020015479void intel_modeset_init_hw(struct drm_device *dev)
15480{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015481 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015482
Ville Syrjäläb6283052015-06-03 15:45:07 +030015483 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015484
15485 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15486
Daniel Vetterf8175862012-04-10 15:50:11 +020015487 intel_init_clock_gating(dev);
Chris Wilsondc979972016-05-10 14:10:04 +010015488 intel_enable_gt_powersave(dev_priv);
Daniel Vetterf8175862012-04-10 15:50:11 +020015489}
15490
Matt Roperd93c0372015-12-03 11:37:41 -080015491/*
15492 * Calculate what we think the watermarks should be for the state we've read
15493 * out of the hardware and then immediately program those watermarks so that
15494 * we ensure the hardware settings match our internal state.
15495 *
15496 * We can calculate what we think WM's should be by creating a duplicate of the
15497 * current state (which was constructed during hardware readout) and running it
15498 * through the atomic check code to calculate new watermark values in the
15499 * state object.
15500 */
15501static void sanitize_watermarks(struct drm_device *dev)
15502{
15503 struct drm_i915_private *dev_priv = to_i915(dev);
15504 struct drm_atomic_state *state;
15505 struct drm_crtc *crtc;
15506 struct drm_crtc_state *cstate;
15507 struct drm_modeset_acquire_ctx ctx;
15508 int ret;
15509 int i;
15510
15511 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015512 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015513 return;
15514
15515 /*
15516 * We need to hold connection_mutex before calling duplicate_state so
15517 * that the connector loop is protected.
15518 */
15519 drm_modeset_acquire_init(&ctx, 0);
15520retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015521 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015522 if (ret == -EDEADLK) {
15523 drm_modeset_backoff(&ctx);
15524 goto retry;
15525 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015526 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015527 }
15528
15529 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15530 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015531 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015532
Matt Ropered4a6a72016-02-23 17:20:13 -080015533 /*
15534 * Hardware readout is the only time we don't want to calculate
15535 * intermediate watermarks (since we don't trust the current
15536 * watermarks).
15537 */
15538 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15539
Matt Roperd93c0372015-12-03 11:37:41 -080015540 ret = intel_atomic_check(dev, state);
15541 if (ret) {
15542 /*
15543 * If we fail here, it means that the hardware appears to be
15544 * programmed in a way that shouldn't be possible, given our
15545 * understanding of watermark requirements. This might mean a
15546 * mistake in the hardware readout code or a mistake in the
15547 * watermark calculations for a given platform. Raise a WARN
15548 * so that this is noticeable.
15549 *
15550 * If this actually happens, we'll have to just leave the
15551 * BIOS-programmed watermarks untouched and hope for the best.
15552 */
15553 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015554 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015555 }
15556
15557 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080015558 for_each_crtc_in_state(state, crtc, cstate, i) {
15559 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15560
Matt Ropered4a6a72016-02-23 17:20:13 -080015561 cs->wm.need_postvbl_update = true;
15562 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015563 }
15564
15565 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015566fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015567 drm_modeset_drop_locks(&ctx);
15568 drm_modeset_acquire_fini(&ctx);
15569}
15570
Jesse Barnes79e53942008-11-07 14:24:08 -080015571void intel_modeset_init(struct drm_device *dev)
15572{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015573 struct drm_i915_private *dev_priv = to_i915(dev);
15574 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015575 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015576 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015577 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015578
15579 drm_mode_config_init(dev);
15580
15581 dev->mode_config.min_width = 0;
15582 dev->mode_config.min_height = 0;
15583
Dave Airlie019d96c2011-09-29 16:20:42 +010015584 dev->mode_config.preferred_depth = 24;
15585 dev->mode_config.prefer_shadow = 1;
15586
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015587 dev->mode_config.allow_fb_modifiers = true;
15588
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015589 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015590
Jesse Barnesb690e962010-07-19 13:53:12 -070015591 intel_init_quirks(dev);
15592
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015593 intel_init_pm(dev);
15594
Ben Widawskye3c74752013-04-05 13:12:39 -070015595 if (INTEL_INFO(dev)->num_pipes == 0)
15596 return;
15597
Lukas Wunner69f92f62015-07-15 13:57:35 +020015598 /*
15599 * There may be no VBT; and if the BIOS enabled SSC we can
15600 * just keep using it to avoid unnecessary flicker. Whereas if the
15601 * BIOS isn't using it, don't assume it will work even if the VBT
15602 * indicates as much.
15603 */
15604 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15605 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15606 DREF_SSC1_ENABLE);
15607
15608 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15609 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15610 bios_lvds_use_ssc ? "en" : "dis",
15611 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15612 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15613 }
15614 }
15615
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015616 if (IS_GEN2(dev)) {
15617 dev->mode_config.max_width = 2048;
15618 dev->mode_config.max_height = 2048;
15619 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015620 dev->mode_config.max_width = 4096;
15621 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015622 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015623 dev->mode_config.max_width = 8192;
15624 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015625 }
Damien Lespiau068be562014-03-28 14:17:49 +000015626
Ville Syrjälädc41c152014-08-13 11:57:05 +030015627 if (IS_845G(dev) || IS_I865G(dev)) {
15628 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15629 dev->mode_config.cursor_height = 1023;
15630 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015631 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15632 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15633 } else {
15634 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15635 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15636 }
15637
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015638 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015639
Zhao Yakui28c97732009-10-09 11:39:41 +080015640 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015641 INTEL_INFO(dev)->num_pipes,
15642 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015643
Damien Lespiau055e3932014-08-18 13:49:10 +010015644 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015645 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015646 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015647 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015648 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015649 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015650 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015651 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015652 }
15653
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015654 intel_update_czclk(dev_priv);
15655 intel_update_cdclk(dev);
15656
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015657 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015658
Ville Syrjäläb2045352016-05-13 23:41:27 +030015659 if (dev_priv->max_cdclk_freq == 0)
15660 intel_update_max_cdclk(dev);
15661
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015662 /* Just disable it once at startup */
15663 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015664 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015665
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015666 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015667 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015668 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015669
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015670 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015671 struct intel_initial_plane_config plane_config = {};
15672
Jesse Barnes46f297f2014-03-07 08:57:48 -080015673 if (!crtc->active)
15674 continue;
15675
Jesse Barnes46f297f2014-03-07 08:57:48 -080015676 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015677 * Note that reserving the BIOS fb up front prevents us
15678 * from stuffing other stolen allocations like the ring
15679 * on top. This prevents some ugliness at boot time, and
15680 * can even allow for smooth boot transitions if the BIOS
15681 * fb is large enough for the active pipe configuration.
15682 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015683 dev_priv->display.get_initial_plane_config(crtc,
15684 &plane_config);
15685
15686 /*
15687 * If the fb is shared between multiple heads, we'll
15688 * just get the first one.
15689 */
15690 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015691 }
Matt Roperd93c0372015-12-03 11:37:41 -080015692
15693 /*
15694 * Make sure hardware watermarks really match the state we read out.
15695 * Note that we need to do this after reconstructing the BIOS fb's
15696 * since the watermark calculation done here will use pstate->fb.
15697 */
15698 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015699}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015700
Daniel Vetter7fad7982012-07-04 17:51:47 +020015701static void intel_enable_pipe_a(struct drm_device *dev)
15702{
15703 struct intel_connector *connector;
15704 struct drm_connector *crt = NULL;
15705 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015706 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015707
15708 /* We can't just switch on the pipe A, we need to set things up with a
15709 * proper mode and output configuration. As a gross hack, enable pipe A
15710 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015711 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015712 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15713 crt = &connector->base;
15714 break;
15715 }
15716 }
15717
15718 if (!crt)
15719 return;
15720
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015721 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015722 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015723}
15724
Daniel Vetterfa555832012-10-10 23:14:00 +020015725static bool
15726intel_check_plane_mapping(struct intel_crtc *crtc)
15727{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015728 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010015729 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä649636e2015-09-22 19:50:01 +030015730 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015731
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015732 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015733 return true;
15734
Ville Syrjälä649636e2015-09-22 19:50:01 +030015735 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015736
15737 if ((val & DISPLAY_PLANE_ENABLE) &&
15738 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15739 return false;
15740
15741 return true;
15742}
15743
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015744static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15745{
15746 struct drm_device *dev = crtc->base.dev;
15747 struct intel_encoder *encoder;
15748
15749 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15750 return true;
15751
15752 return false;
15753}
15754
Ville Syrjälädd756192016-02-17 21:28:45 +020015755static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15756{
15757 struct drm_device *dev = encoder->base.dev;
15758 struct intel_connector *connector;
15759
15760 for_each_connector_on_encoder(dev, &encoder->base, connector)
15761 return true;
15762
15763 return false;
15764}
15765
Daniel Vetter24929352012-07-02 20:28:59 +020015766static void intel_sanitize_crtc(struct intel_crtc *crtc)
15767{
15768 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010015769 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020015770 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015771
Daniel Vetter24929352012-07-02 20:28:59 +020015772 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015773 if (!transcoder_is_dsi(cpu_transcoder)) {
15774 i915_reg_t reg = PIPECONF(cpu_transcoder);
15775
15776 I915_WRITE(reg,
15777 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15778 }
Daniel Vetter24929352012-07-02 20:28:59 +020015779
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015780 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015781 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015782 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015783 struct intel_plane *plane;
15784
Daniel Vetter96256042015-02-13 21:03:42 +010015785 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015786
15787 /* Disable everything but the primary plane */
15788 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15789 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15790 continue;
15791
15792 plane->disable_plane(&plane->base, &crtc->base);
15793 }
Daniel Vetter96256042015-02-13 21:03:42 +010015794 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015795
Daniel Vetter24929352012-07-02 20:28:59 +020015796 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015797 * disable the crtc (and hence change the state) if it is wrong. Note
15798 * that gen4+ has a fixed plane -> pipe mapping. */
15799 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015800 bool plane;
15801
Ville Syrjälä78108b72016-05-27 20:59:19 +030015802 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15803 crtc->base.base.id, crtc->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015804
15805 /* Pipe has the wrong plane attached and the plane is active.
15806 * Temporarily change the plane mapping and disable everything
15807 * ... */
15808 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015809 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015810 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015811 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015812 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015813 }
Daniel Vetter24929352012-07-02 20:28:59 +020015814
Daniel Vetter7fad7982012-07-04 17:51:47 +020015815 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15816 crtc->pipe == PIPE_A && !crtc->active) {
15817 /* BIOS forgot to enable pipe A, this mostly happens after
15818 * resume. Force-enable the pipe to fix this, the update_dpms
15819 * call below we restore the pipe to the right state, but leave
15820 * the required bits on. */
15821 intel_enable_pipe_a(dev);
15822 }
15823
Daniel Vetter24929352012-07-02 20:28:59 +020015824 /* Adjust the state of the output pipe according to whether we
15825 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015826 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015827 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015828
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015829 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015830 /*
15831 * We start out with underrun reporting disabled to avoid races.
15832 * For correct bookkeeping mark this on active crtcs.
15833 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015834 * Also on gmch platforms we dont have any hardware bits to
15835 * disable the underrun reporting. Which means we need to start
15836 * out with underrun reporting disabled also on inactive pipes,
15837 * since otherwise we'll complain about the garbage we read when
15838 * e.g. coming up after runtime pm.
15839 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015840 * No protection against concurrent access is required - at
15841 * worst a fifo underrun happens which also sets this to false.
15842 */
15843 crtc->cpu_fifo_underrun_disabled = true;
15844 crtc->pch_fifo_underrun_disabled = true;
15845 }
Daniel Vetter24929352012-07-02 20:28:59 +020015846}
15847
15848static void intel_sanitize_encoder(struct intel_encoder *encoder)
15849{
15850 struct intel_connector *connector;
15851 struct drm_device *dev = encoder->base.dev;
15852
15853 /* We need to check both for a crtc link (meaning that the
15854 * encoder is active and trying to read from a pipe) and the
15855 * pipe itself being active. */
15856 bool has_active_crtc = encoder->base.crtc &&
15857 to_intel_crtc(encoder->base.crtc)->active;
15858
Ville Syrjälädd756192016-02-17 21:28:45 +020015859 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015860 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15861 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015862 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015863
15864 /* Connector is active, but has no active pipe. This is
15865 * fallout from our resume register restoring. Disable
15866 * the encoder manually again. */
15867 if (encoder->base.crtc) {
15868 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15869 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015870 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015871 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015872 if (encoder->post_disable)
15873 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015874 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015875 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015876
15877 /* Inconsistent output/port/pipe state happens presumably due to
15878 * a bug in one of the get_hw_state functions. Or someplace else
15879 * in our code, like the register restore mess on resume. Clamp
15880 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015881 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015882 if (connector->encoder != encoder)
15883 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015884 connector->base.dpms = DRM_MODE_DPMS_OFF;
15885 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015886 }
15887 }
15888 /* Enabled encoders without active connectors will be fixed in
15889 * the crtc fixup. */
15890}
15891
Imre Deak04098752014-02-18 00:02:16 +020015892void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015893{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015894 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015895 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015896
Imre Deak04098752014-02-18 00:02:16 +020015897 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15898 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15899 i915_disable_vga(dev);
15900 }
15901}
15902
15903void i915_redisable_vga(struct drm_device *dev)
15904{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015905 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak04098752014-02-18 00:02:16 +020015906
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015907 /* This function can be called both from intel_modeset_setup_hw_state or
15908 * at a very early point in our resume sequence, where the power well
15909 * structures are not yet restored. Since this function is at a very
15910 * paranoid "someone might have enabled VGA while we were not looking"
15911 * level, just check if the power well is enabled instead of trying to
15912 * follow the "don't touch the power well if we don't need it" policy
15913 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015914 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015915 return;
15916
Imre Deak04098752014-02-18 00:02:16 +020015917 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015918
15919 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015920}
15921
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015922static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015923{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015924 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015925
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015926 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015927}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015928
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015929/* FIXME read out full plane state for all planes */
15930static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015931{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015932 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015933 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015934 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015935
Matt Roper19b8d382015-09-24 15:53:17 -070015936 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015937 primary_get_hw_state(to_intel_plane(primary));
15938
15939 if (plane_state->visible)
15940 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015941}
15942
Daniel Vetter30e984d2013-06-05 13:34:17 +020015943static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015944{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015945 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015946 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015947 struct intel_crtc *crtc;
15948 struct intel_encoder *encoder;
15949 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015950 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015951
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015952 dev_priv->active_crtcs = 0;
15953
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015954 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015955 struct intel_crtc_state *crtc_state = crtc->config;
15956 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015957
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020015958 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015959 memset(crtc_state, 0, sizeof(*crtc_state));
15960 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015961
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015962 crtc_state->base.active = crtc_state->base.enable =
15963 dev_priv->display.get_pipe_config(crtc, crtc_state);
15964
15965 crtc->base.enabled = crtc_state->base.enable;
15966 crtc->active = crtc_state->base.active;
15967
15968 if (crtc_state->base.active) {
15969 dev_priv->active_crtcs |= 1 << crtc->pipe;
15970
Clint Taylorc89e39f2016-05-13 23:41:21 +030015971 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015972 pixclk = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjälä9558d152016-05-13 23:41:20 +030015973 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015974 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15975 else
15976 WARN_ON(dev_priv->display.modeset_calc_cdclk);
Ville Syrjälä9558d152016-05-13 23:41:20 +030015977
15978 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15979 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
15980 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015981 }
15982
15983 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015984
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015985 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015986
Ville Syrjälä78108b72016-05-27 20:59:19 +030015987 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15988 crtc->base.base.id, crtc->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015989 crtc->active ? "enabled" : "disabled");
15990 }
15991
Daniel Vetter53589012013-06-05 13:34:16 +020015992 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15993 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15994
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020015995 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
15996 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015997 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015998 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010015999 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016000 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020016001 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016002 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020016003
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020016004 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016005 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020016006 }
16007
Damien Lespiaub2784e12014-08-05 11:29:37 +010016008 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016009 pipe = 0;
16010
16011 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070016012 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16013 encoder->base.crtc = &crtc->base;
Ville Syrjälä253c84c2016-06-22 21:57:01 +030016014 crtc->config->output_types |= 1 << encoder->type;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016015 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020016016 } else {
16017 encoder->base.crtc = NULL;
16018 }
16019
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016020 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020016021 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016022 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016023 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016024 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020016025 }
16026
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016027 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020016028 if (connector->get_hw_state(connector)) {
16029 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016030
16031 encoder = connector->encoder;
16032 connector->base.encoder = &encoder->base;
16033
16034 if (encoder->base.crtc &&
16035 encoder->base.crtc->state->active) {
16036 /*
16037 * This has to be done during hardware readout
16038 * because anything calling .crtc_disable may
16039 * rely on the connector_mask being accurate.
16040 */
16041 encoder->base.crtc->state->connector_mask |=
16042 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010016043 encoder->base.crtc->state->encoder_mask |=
16044 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016045 }
16046
Daniel Vetter24929352012-07-02 20:28:59 +020016047 } else {
16048 connector->base.dpms = DRM_MODE_DPMS_OFF;
16049 connector->base.encoder = NULL;
16050 }
16051 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16052 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030016053 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016054 connector->base.encoder ? "enabled" : "disabled");
16055 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016056
16057 for_each_intel_crtc(dev, crtc) {
16058 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16059
16060 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16061 if (crtc->base.state->active) {
16062 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16063 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16064 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16065
16066 /*
16067 * The initial mode needs to be set in order to keep
16068 * the atomic core happy. It wants a valid mode if the
16069 * crtc's enabled, so we do the above call.
16070 *
16071 * At this point some state updated by the connectors
16072 * in their ->detect() callback has not run yet, so
16073 * no recalculation can be done yet.
16074 *
16075 * Even if we could do a recalculation and modeset
16076 * right now it would cause a double modeset if
16077 * fbdev or userspace chooses a different initial mode.
16078 *
16079 * If that happens, someone indicated they wanted a
16080 * mode change, which means it's safe to do a full
16081 * recalculation.
16082 */
16083 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030016084
16085 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16086 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016087 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020016088
16089 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016090 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020016091}
16092
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016093/* Scan out the current hw modeset state,
16094 * and sanitizes it to the current state
16095 */
16096static void
16097intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020016098{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016099 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter30e984d2013-06-05 13:34:17 +020016100 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016101 struct intel_crtc *crtc;
16102 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020016103 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016104
16105 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016106
16107 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010016108 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016109 intel_sanitize_encoder(encoder);
16110 }
16111
Damien Lespiau055e3932014-08-18 13:49:10 +010016112 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020016113 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16114 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016115 intel_dump_pipe_config(crtc, crtc->config,
16116 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020016117 }
Daniel Vetter9a935852012-07-05 22:34:27 +020016118
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020016119 intel_modeset_update_connector_atomic_state(dev);
16120
Daniel Vetter35c95372013-07-17 06:55:04 +020016121 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16122 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16123
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016124 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020016125 continue;
16126
16127 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16128
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016129 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020016130 pll->on = false;
16131 }
16132
Wayne Boyer666a4532015-12-09 12:29:35 -080016133 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030016134 vlv_wm_get_hw_state(dev);
16135 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000016136 skl_wm_get_hw_state(dev);
16137 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030016138 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016139
16140 for_each_intel_crtc(dev, crtc) {
16141 unsigned long put_domains;
16142
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010016143 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016144 if (WARN_ON(put_domains))
16145 modeset_put_power_domains(dev_priv, put_domains);
16146 }
16147 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020016148
16149 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016150}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030016151
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016152void intel_display_resume(struct drm_device *dev)
16153{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016154 struct drm_i915_private *dev_priv = to_i915(dev);
16155 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16156 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016157 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016158 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020016159
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016160 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016161
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016162 /*
16163 * This is a cludge because with real atomic modeset mode_config.mutex
16164 * won't be taken. Unfortunately some probed state like
16165 * audio_codec_enable is still protected by mode_config.mutex, so lock
16166 * it here for now.
16167 */
16168 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016169 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016170
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016171retry:
16172 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016173
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016174 if (ret == 0 && !setup) {
16175 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016176
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016177 intel_modeset_setup_hw_state(dev);
16178 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010016179 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020016180
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016181 if (ret == 0 && state) {
16182 struct drm_crtc_state *crtc_state;
16183 struct drm_crtc *crtc;
16184 int i;
16185
16186 state->acquire_ctx = &ctx;
16187
Ville Syrjäläe3d54572016-05-13 10:10:42 -070016188 /* ignore any reset values/BIOS leftovers in the WM registers */
16189 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16190
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016191 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16192 /*
16193 * Force recalculation even if we restore
16194 * current state. With fast modeset this may not result
16195 * in a modeset when the state is compatible.
16196 */
16197 crtc_state->mode_changed = true;
16198 }
16199
16200 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016201 }
16202
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016203 if (ret == -EDEADLK) {
16204 drm_modeset_backoff(&ctx);
16205 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016206 }
16207
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016208 drm_modeset_drop_locks(&ctx);
16209 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016210 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016211
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016212 if (ret) {
16213 DRM_ERROR("Restoring old state failed with %i\n", ret);
16214 drm_atomic_state_free(state);
16215 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010016216}
16217
16218void intel_modeset_gem_init(struct drm_device *dev)
16219{
Chris Wilsondc979972016-05-10 14:10:04 +010016220 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016221 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070016222 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016223 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080016224
Chris Wilsondc979972016-05-10 14:10:04 +010016225 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016226
Chris Wilson1833b132012-05-09 11:56:28 +010016227 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020016228
Chris Wilson1ee8da62016-05-12 12:43:23 +010016229 intel_setup_overlay(dev_priv);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016230
16231 /*
16232 * Make sure any fbs we allocated at startup are properly
16233 * pinned & fenced. When we do the allocation it's too early
16234 * for this.
16235 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010016236 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070016237 obj = intel_fb_obj(c->primary->fb);
16238 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080016239 continue;
16240
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016241 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020016242 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16243 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016244 mutex_unlock(&dev->struct_mutex);
16245 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016246 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16247 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016248 drm_framebuffer_unreference(c->primary->fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020016249 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016250 c->primary->crtc = c->primary->state->crtc = NULL;
Daniel Vetter5a21b662016-05-24 17:13:53 +020016251 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016252 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016253 }
16254 }
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016255}
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016256
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016257int intel_connector_register(struct drm_connector *connector)
16258{
16259 struct intel_connector *intel_connector = to_intel_connector(connector);
16260 int ret;
16261
16262 ret = intel_backlight_device_register(intel_connector);
16263 if (ret)
16264 goto err;
16265
16266 return 0;
16267
16268err:
16269 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080016270}
16271
Chris Wilsonc191eca2016-06-17 11:40:33 +010016272void intel_connector_unregister(struct drm_connector *connector)
Imre Deak4932e2c2014-02-11 17:12:48 +020016273{
Chris Wilsone63d87c2016-06-17 11:40:34 +010016274 struct intel_connector *intel_connector = to_intel_connector(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016275
Chris Wilsone63d87c2016-06-17 11:40:34 +010016276 intel_backlight_device_unregister(intel_connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016277 intel_panel_destroy_backlight(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016278}
16279
Jesse Barnes79e53942008-11-07 14:24:08 -080016280void intel_modeset_cleanup(struct drm_device *dev)
16281{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016282 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -070016283
Chris Wilsondc979972016-05-10 14:10:04 +010016284 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020016285
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016286 /*
16287 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016288 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016289 * experience fancy races otherwise.
16290 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016291 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016292
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016293 /*
16294 * Due to the hpd irq storm handling the hotplug work can re-arm the
16295 * poll handlers. Hence disable polling after hpd handling is shut down.
16296 */
Keith Packardf87ea762010-10-03 19:36:26 -070016297 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016298
Jesse Barnes723bfd72010-10-07 16:01:13 -070016299 intel_unregister_dsm_handler();
16300
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016301 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016302
Chris Wilson1630fe72011-07-08 12:22:42 +010016303 /* flush any delayed tasks or pending work */
16304 flush_scheduled_work();
16305
Jesse Barnes79e53942008-11-07 14:24:08 -080016306 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016307
Chris Wilson1ee8da62016-05-12 12:43:23 +010016308 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016309
Chris Wilsondc979972016-05-10 14:10:04 +010016310 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010016311
16312 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016313}
16314
Chris Wilsondf0e9242010-09-09 16:20:55 +010016315void intel_connector_attach_encoder(struct intel_connector *connector,
16316 struct intel_encoder *encoder)
16317{
16318 connector->encoder = encoder;
16319 drm_mode_connector_attach_encoder(&connector->base,
16320 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016321}
Dave Airlie28d52042009-09-21 14:33:58 +100016322
16323/*
16324 * set vga decode state - true == enable VGA decode
16325 */
16326int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16327{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016328 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsona885b3c2013-12-17 14:34:50 +000016329 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016330 u16 gmch_ctrl;
16331
Chris Wilson75fa0412014-02-07 18:37:02 -020016332 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16333 DRM_ERROR("failed to read control word\n");
16334 return -EIO;
16335 }
16336
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016337 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16338 return 0;
16339
Dave Airlie28d52042009-09-21 14:33:58 +100016340 if (state)
16341 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16342 else
16343 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016344
16345 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16346 DRM_ERROR("failed to write control word\n");
16347 return -EIO;
16348 }
16349
Dave Airlie28d52042009-09-21 14:33:58 +100016350 return 0;
16351}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016352
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016353struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016354
16355 u32 power_well_driver;
16356
Chris Wilson63b66e52013-08-08 15:12:06 +020016357 int num_transcoders;
16358
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016359 struct intel_cursor_error_state {
16360 u32 control;
16361 u32 position;
16362 u32 base;
16363 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016364 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016365
16366 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016367 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016368 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030016369 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016370 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016371
16372 struct intel_plane_error_state {
16373 u32 control;
16374 u32 stride;
16375 u32 size;
16376 u32 pos;
16377 u32 addr;
16378 u32 surface;
16379 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016380 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016381
16382 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016383 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016384 enum transcoder cpu_transcoder;
16385
16386 u32 conf;
16387
16388 u32 htotal;
16389 u32 hblank;
16390 u32 hsync;
16391 u32 vtotal;
16392 u32 vblank;
16393 u32 vsync;
16394 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016395};
16396
16397struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010016398intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016399{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016400 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016401 int transcoders[] = {
16402 TRANSCODER_A,
16403 TRANSCODER_B,
16404 TRANSCODER_C,
16405 TRANSCODER_EDP,
16406 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016407 int i;
16408
Chris Wilsonc0336662016-05-06 15:40:21 +010016409 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020016410 return NULL;
16411
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016412 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016413 if (error == NULL)
16414 return NULL;
16415
Chris Wilsonc0336662016-05-06 15:40:21 +010016416 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016417 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16418
Damien Lespiau055e3932014-08-18 13:49:10 +010016419 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016420 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016421 __intel_display_power_is_enabled(dev_priv,
16422 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016423 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016424 continue;
16425
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016426 error->cursor[i].control = I915_READ(CURCNTR(i));
16427 error->cursor[i].position = I915_READ(CURPOS(i));
16428 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016429
16430 error->plane[i].control = I915_READ(DSPCNTR(i));
16431 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016432 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016433 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016434 error->plane[i].pos = I915_READ(DSPPOS(i));
16435 }
Chris Wilsonc0336662016-05-06 15:40:21 +010016436 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030016437 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016438 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016439 error->plane[i].surface = I915_READ(DSPSURF(i));
16440 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16441 }
16442
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016443 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030016444
Chris Wilsonc0336662016-05-06 15:40:21 +010016445 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e2014-04-18 15:55:04 +030016446 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016447 }
16448
Jani Nikula4d1de972016-03-18 17:05:42 +020016449 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010016450 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030016451 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020016452 error->num_transcoders++; /* Account for eDP. */
16453
16454 for (i = 0; i < error->num_transcoders; i++) {
16455 enum transcoder cpu_transcoder = transcoders[i];
16456
Imre Deakddf9c532013-11-27 22:02:02 +020016457 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016458 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016459 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016460 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016461 continue;
16462
Chris Wilson63b66e52013-08-08 15:12:06 +020016463 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16464
16465 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16466 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16467 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16468 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16469 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16470 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16471 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016472 }
16473
16474 return error;
16475}
16476
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016477#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16478
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016479void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016480intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016481 struct drm_device *dev,
16482 struct intel_display_error_state *error)
16483{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016484 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016485 int i;
16486
Chris Wilson63b66e52013-08-08 15:12:06 +020016487 if (!error)
16488 return;
16489
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016490 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016491 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016492 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016493 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016494 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016495 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016496 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016497 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016498 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016499 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016500
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016501 err_printf(m, "Plane [%d]:\n", i);
16502 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16503 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016504 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016505 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16506 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016507 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016508 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016509 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016510 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016511 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16512 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016513 }
16514
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016515 err_printf(m, "Cursor [%d]:\n", i);
16516 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16517 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16518 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016519 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016520
16521 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016522 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016523 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016524 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016525 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016526 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16527 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16528 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16529 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16530 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16531 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16532 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16533 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016534}