blob: d902a70edb84fc3e0b8a0e20ec7abdd4dd5b5efe [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
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300533/**
534 * Returns whether any output on the specified pipe is of the specified type
535 */
Damien Lespiau40935612014-10-29 11:16:59 +0000536bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300537{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300538 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300539 struct intel_encoder *encoder;
540
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300541 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300542 if (encoder->type == type)
543 return true;
544
545 return false;
546}
547
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200548/**
549 * Returns whether any output on the specified pipe will have the specified
550 * type after a staged modeset is complete, i.e., the same as
551 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
552 * encoder->crtc.
553 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200554static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
555 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200556{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200557 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300558 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200559 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200560 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200561 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200562
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300563 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200564 if (connector_state->crtc != crtc_state->base.crtc)
565 continue;
566
567 num_connectors++;
568
569 encoder = to_intel_encoder(connector_state->best_encoder);
570 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200571 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200572 }
573
574 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200575
576 return false;
577}
578
Imre Deakdccbea32015-06-22 23:35:51 +0300579/*
580 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
581 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
582 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
583 * The helpers' return value is the rate of the clock that is fed to the
584 * display engine's pipe which can be the above fast dot clock rate or a
585 * divided-down version of it.
586 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500587/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300588static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800589{
Shaohua Li21778322009-02-23 15:19:16 +0800590 clock->m = clock->m2 + 2;
591 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200592 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300593 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300594 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
595 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300596
597 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800598}
599
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200600static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
601{
602 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
603}
604
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300605static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800606{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200607 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800608 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200609 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300610 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300611 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
612 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300613
614 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800615}
616
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300617static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300618{
619 clock->m = clock->m1 * clock->m2;
620 clock->p = clock->p1 * clock->p2;
621 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300622 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300623 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
624 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300625
626 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300627}
628
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300629int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300630{
631 clock->m = clock->m1 * clock->m2;
632 clock->p = clock->p1 * clock->p2;
633 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300634 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300635 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
636 clock->n << 22);
637 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300638
639 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300640}
641
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800642#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800643/**
644 * Returns whether the given set of divisors are valid for a given refclk with
645 * the given connectors.
646 */
647
Chris Wilson1b894b52010-12-14 20:04:54 +0000648static bool intel_PLL_is_valid(struct drm_device *dev,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300649 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300650 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800651{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300652 if (clock->n < limit->n.min || limit->n.max < clock->n)
653 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800654 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400655 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800656 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400657 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800658 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400659 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300660
Wayne Boyer666a4532015-12-09 12:29:35 -0800661 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
662 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300663 if (clock->m1 <= clock->m2)
664 INTELPllInvalid("m1 <= m2\n");
665
Wayne Boyer666a4532015-12-09 12:29:35 -0800666 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300667 if (clock->p < limit->p.min || limit->p.max < clock->p)
668 INTELPllInvalid("p out of range\n");
669 if (clock->m < limit->m.min || limit->m.max < clock->m)
670 INTELPllInvalid("m out of range\n");
671 }
672
Jesse Barnes79e53942008-11-07 14:24:08 -0800673 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400674 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800675 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
676 * connector, etc., rather than just a single range.
677 */
678 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400679 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800680
681 return true;
682}
683
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300684static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300685i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300686 const struct intel_crtc_state *crtc_state,
687 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800688{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300689 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800690
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200691 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800692 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100693 * For LVDS just rely on its current settings for dual-channel.
694 * We haven't figured out how to reliably set up different
695 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800696 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100697 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300698 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800699 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300700 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800701 } else {
702 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300703 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800704 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300705 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800706 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300707}
708
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200709/*
710 * Returns a set of divisors for the desired target clock with the given
711 * refclk, or FALSE. The returned values represent the clock equation:
712 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
713 *
714 * Target and reference clocks are specified in kHz.
715 *
716 * If match_clock is provided, then best_clock P divider must match the P
717 * divider from @match_clock used for LVDS downclocking.
718 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300719static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300720i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300721 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300722 int target, int refclk, struct dpll *match_clock,
723 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300724{
725 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300726 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300727 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800728
Akshay Joshi0206e352011-08-16 15:34:10 -0400729 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800730
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300731 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
732
Zhao Yakui42158662009-11-20 11:24:18 +0800733 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
734 clock.m1++) {
735 for (clock.m2 = limit->m2.min;
736 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200737 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800738 break;
739 for (clock.n = limit->n.min;
740 clock.n <= limit->n.max; clock.n++) {
741 for (clock.p1 = limit->p1.min;
742 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800743 int this_err;
744
Imre Deakdccbea32015-06-22 23:35:51 +0300745 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000746 if (!intel_PLL_is_valid(dev, limit,
747 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800748 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800749 if (match_clock &&
750 clock.p != match_clock->p)
751 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800752
753 this_err = abs(clock.dot - target);
754 if (this_err < err) {
755 *best_clock = clock;
756 err = this_err;
757 }
758 }
759 }
760 }
761 }
762
763 return (err != target);
764}
765
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200766/*
767 * Returns a set of divisors for the desired target clock with the given
768 * refclk, or FALSE. The returned values represent the clock equation:
769 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
770 *
771 * Target and reference clocks are specified in kHz.
772 *
773 * If match_clock is provided, then best_clock P divider must match the P
774 * divider from @match_clock used for LVDS downclocking.
775 */
Ma Lingd4906092009-03-18 20:13:27 +0800776static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300777pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200778 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300779 int target, int refclk, struct dpll *match_clock,
780 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200781{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300782 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300783 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200784 int err = target;
785
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200786 memset(best_clock, 0, sizeof(*best_clock));
787
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300788 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
789
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200790 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
791 clock.m1++) {
792 for (clock.m2 = limit->m2.min;
793 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200794 for (clock.n = limit->n.min;
795 clock.n <= limit->n.max; clock.n++) {
796 for (clock.p1 = limit->p1.min;
797 clock.p1 <= limit->p1.max; clock.p1++) {
798 int this_err;
799
Imre Deakdccbea32015-06-22 23:35:51 +0300800 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800801 if (!intel_PLL_is_valid(dev, limit,
802 &clock))
803 continue;
804 if (match_clock &&
805 clock.p != match_clock->p)
806 continue;
807
808 this_err = abs(clock.dot - target);
809 if (this_err < err) {
810 *best_clock = clock;
811 err = this_err;
812 }
813 }
814 }
815 }
816 }
817
818 return (err != target);
819}
820
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200821/*
822 * Returns a set of divisors for the desired target clock with the given
823 * refclk, or FALSE. The returned values represent the clock equation:
824 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200825 *
826 * Target and reference clocks are specified in kHz.
827 *
828 * If match_clock is provided, then best_clock P divider must match the P
829 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200830 */
Ma Lingd4906092009-03-18 20:13:27 +0800831static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300832g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200833 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300834 int target, int refclk, struct dpll *match_clock,
835 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800836{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300837 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300838 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800839 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300840 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400841 /* approximately equals target * 0.00585 */
842 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800843
844 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300845
846 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
847
Ma Lingd4906092009-03-18 20:13:27 +0800848 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200849 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800850 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200851 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800852 for (clock.m1 = limit->m1.max;
853 clock.m1 >= limit->m1.min; clock.m1--) {
854 for (clock.m2 = limit->m2.max;
855 clock.m2 >= limit->m2.min; clock.m2--) {
856 for (clock.p1 = limit->p1.max;
857 clock.p1 >= limit->p1.min; clock.p1--) {
858 int this_err;
859
Imre Deakdccbea32015-06-22 23:35:51 +0300860 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000861 if (!intel_PLL_is_valid(dev, limit,
862 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800863 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000864
865 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800866 if (this_err < err_most) {
867 *best_clock = clock;
868 err_most = this_err;
869 max_n = clock.n;
870 found = true;
871 }
872 }
873 }
874 }
875 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800876 return found;
877}
Ma Lingd4906092009-03-18 20:13:27 +0800878
Imre Deakd5dd62b2015-03-17 11:40:03 +0200879/*
880 * Check if the calculated PLL configuration is more optimal compared to the
881 * best configuration and error found so far. Return the calculated error.
882 */
883static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300884 const struct dpll *calculated_clock,
885 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200886 unsigned int best_error_ppm,
887 unsigned int *error_ppm)
888{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200889 /*
890 * For CHV ignore the error and consider only the P value.
891 * Prefer a bigger P value based on HW requirements.
892 */
893 if (IS_CHERRYVIEW(dev)) {
894 *error_ppm = 0;
895
896 return calculated_clock->p > best_clock->p;
897 }
898
Imre Deak24be4e42015-03-17 11:40:04 +0200899 if (WARN_ON_ONCE(!target_freq))
900 return false;
901
Imre Deakd5dd62b2015-03-17 11:40:03 +0200902 *error_ppm = div_u64(1000000ULL *
903 abs(target_freq - calculated_clock->dot),
904 target_freq);
905 /*
906 * Prefer a better P value over a better (smaller) error if the error
907 * is small. Ensure this preference for future configurations too by
908 * setting the error to 0.
909 */
910 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
911 *error_ppm = 0;
912
913 return true;
914 }
915
916 return *error_ppm + 10 < best_error_ppm;
917}
918
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200919/*
920 * Returns a set of divisors for the desired target clock with the given
921 * refclk, or FALSE. The returned values represent the clock equation:
922 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
923 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800924static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300925vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200926 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300927 int target, int refclk, struct dpll *match_clock,
928 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700929{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200930 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300931 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300932 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300933 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300934 /* min update 19.2 MHz */
935 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300936 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700937
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300938 target *= 5; /* fast clock */
939
940 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700941
942 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300943 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300944 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300945 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300946 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300947 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700948 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300949 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200950 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300951
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300952 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
953 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300954
Imre Deakdccbea32015-06-22 23:35:51 +0300955 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300956
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300957 if (!intel_PLL_is_valid(dev, limit,
958 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300959 continue;
960
Imre Deakd5dd62b2015-03-17 11:40:03 +0200961 if (!vlv_PLL_is_optimal(dev, target,
962 &clock,
963 best_clock,
964 bestppm, &ppm))
965 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300966
Imre Deakd5dd62b2015-03-17 11:40:03 +0200967 *best_clock = clock;
968 bestppm = ppm;
969 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700970 }
971 }
972 }
973 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700974
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300975 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700976}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700977
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200978/*
979 * Returns a set of divisors for the desired target clock with the given
980 * refclk, or FALSE. The returned values represent the clock equation:
981 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
982 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300983static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300984chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200985 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300986 int target, int refclk, struct dpll *match_clock,
987 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300988{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200989 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300990 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200991 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300992 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300993 uint64_t m2;
994 int found = false;
995
996 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200997 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300998
999 /*
1000 * Based on hardware doc, the n always set to 1, and m1 always
1001 * set to 2. If requires to support 200Mhz refclk, we need to
1002 * revisit this because n may not 1 anymore.
1003 */
1004 clock.n = 1, clock.m1 = 2;
1005 target *= 5; /* fast clock */
1006
1007 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1008 for (clock.p2 = limit->p2.p2_fast;
1009 clock.p2 >= limit->p2.p2_slow;
1010 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +02001011 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001012
1013 clock.p = clock.p1 * clock.p2;
1014
1015 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1016 clock.n) << 22, refclk * clock.m1);
1017
1018 if (m2 > INT_MAX/clock.m1)
1019 continue;
1020
1021 clock.m2 = m2;
1022
Imre Deakdccbea32015-06-22 23:35:51 +03001023 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001024
1025 if (!intel_PLL_is_valid(dev, limit, &clock))
1026 continue;
1027
Imre Deak9ca3ba02015-03-17 11:40:05 +02001028 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1029 best_error_ppm, &error_ppm))
1030 continue;
1031
1032 *best_clock = clock;
1033 best_error_ppm = error_ppm;
1034 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001035 }
1036 }
1037
1038 return found;
1039}
1040
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001041bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03001042 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001043{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001044 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03001045 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001046
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001047 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001048 target_clock, refclk, NULL, best_clock);
1049}
1050
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001051bool intel_crtc_active(struct drm_crtc *crtc)
1052{
1053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1054
1055 /* Be paranoid as we can arrive here with only partial
1056 * state retrieved from the hardware during setup.
1057 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001058 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001059 * as Haswell has gained clock readout/fastboot support.
1060 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001061 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001062 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001063 *
1064 * FIXME: The intel_crtc->active here should be switched to
1065 * crtc->state->active once we have proper CRTC states wired up
1066 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001067 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001068 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001069 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001070}
1071
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001072enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1073 enum pipe pipe)
1074{
1075 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1076 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1077
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001078 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001079}
1080
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001081static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1082{
1083 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001084 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001085 u32 line1, line2;
1086 u32 line_mask;
1087
1088 if (IS_GEN2(dev))
1089 line_mask = DSL_LINEMASK_GEN2;
1090 else
1091 line_mask = DSL_LINEMASK_GEN3;
1092
1093 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001094 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001095 line2 = I915_READ(reg) & line_mask;
1096
1097 return line1 == line2;
1098}
1099
Keith Packardab7ad7f2010-10-03 00:33:06 -07001100/*
1101 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001102 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001103 *
1104 * After disabling a pipe, we can't wait for vblank in the usual way,
1105 * spinning on the vblank interrupt status bit, since we won't actually
1106 * see an interrupt when the pipe is disabled.
1107 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001108 * On Gen4 and above:
1109 * wait for the pipe register state bit to turn off
1110 *
1111 * Otherwise:
1112 * wait for the display line value to settle (it usually
1113 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001114 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001115 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001116static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001117{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001118 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001119 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001120 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001121 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001122
Keith Packardab7ad7f2010-10-03 00:33:06 -07001123 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001124 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001125
Keith Packardab7ad7f2010-10-03 00:33:06 -07001126 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001127 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1128 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001129 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001130 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001131 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001132 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001133 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001134 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001135}
1136
Jesse Barnesb24e7172011-01-04 15:09:30 -08001137/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001138void assert_pll(struct drm_i915_private *dev_priv,
1139 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001140{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001141 u32 val;
1142 bool cur_state;
1143
Ville Syrjälä649636e2015-09-22 19:50:01 +03001144 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001145 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001146 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001147 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001148 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001149}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001150
Jani Nikula23538ef2013-08-27 15:12:22 +03001151/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001152void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001153{
1154 u32 val;
1155 bool cur_state;
1156
Ville Syrjäläa5805162015-05-26 20:42:30 +03001157 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001158 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001159 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001160
1161 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001162 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001163 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001164 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001165}
Jani Nikula23538ef2013-08-27 15:12:22 +03001166
Jesse Barnes040484a2011-01-03 12:14:26 -08001167static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1168 enum pipe pipe, bool state)
1169{
Jesse Barnes040484a2011-01-03 12:14:26 -08001170 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001171 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1172 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001173
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001174 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001175 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001176 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001177 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001178 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001179 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001180 cur_state = !!(val & FDI_TX_ENABLE);
1181 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001182 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001183 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001184 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001185}
1186#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1187#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1188
1189static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1190 enum pipe pipe, bool state)
1191{
Jesse Barnes040484a2011-01-03 12:14:26 -08001192 u32 val;
1193 bool cur_state;
1194
Ville Syrjälä649636e2015-09-22 19:50:01 +03001195 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001196 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001197 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001198 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001199 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001200}
1201#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1202#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1203
1204static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1205 enum pipe pipe)
1206{
Jesse Barnes040484a2011-01-03 12:14:26 -08001207 u32 val;
1208
1209 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001210 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001211 return;
1212
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001213 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001214 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001215 return;
1216
Ville Syrjälä649636e2015-09-22 19:50:01 +03001217 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001218 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
Jesse Barnes040484a2011-01-03 12:14:26 -08001219}
1220
Daniel Vetter55607e82013-06-16 21:42:39 +02001221void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1222 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001223{
Jesse Barnes040484a2011-01-03 12:14:26 -08001224 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001225 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001226
Ville Syrjälä649636e2015-09-22 19:50:01 +03001227 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001228 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001229 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001230 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001231 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001232}
1233
Daniel Vetterb680c372014-09-19 18:27:27 +02001234void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1235 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001236{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001237 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001238 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001239 u32 val;
1240 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001241 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001242
Jani Nikulabedd4db2014-08-22 15:04:13 +03001243 if (WARN_ON(HAS_DDI(dev)))
1244 return;
1245
1246 if (HAS_PCH_SPLIT(dev)) {
1247 u32 port_sel;
1248
Jesse Barnesea0760c2011-01-04 15:09:32 -08001249 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001250 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1251
1252 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1253 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1254 panel_pipe = PIPE_B;
1255 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001256 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001257 /* presumably write lock depends on pipe, not port select */
1258 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1259 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001260 } else {
1261 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001262 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1263 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001264 }
1265
1266 val = I915_READ(pp_reg);
1267 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001268 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001269 locked = false;
1270
Rob Clarke2c719b2014-12-15 13:56:32 -05001271 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001272 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001273 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001274}
1275
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001276static void assert_cursor(struct drm_i915_private *dev_priv,
1277 enum pipe pipe, bool state)
1278{
1279 struct drm_device *dev = dev_priv->dev;
1280 bool cur_state;
1281
Paulo Zanonid9d82082014-02-27 16:30:56 -03001282 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001283 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001284 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001285 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001286
Rob Clarke2c719b2014-12-15 13:56:32 -05001287 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001288 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001289 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001290}
1291#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1292#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1293
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001294void assert_pipe(struct drm_i915_private *dev_priv,
1295 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001296{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001297 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001298 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1299 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001300 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001301
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001302 /* if we need the pipe quirk it must be always on */
1303 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1304 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001305 state = true;
1306
Imre Deak4feed0e2016-02-12 18:55:14 +02001307 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1308 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001309 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001310 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001311
1312 intel_display_power_put(dev_priv, power_domain);
1313 } else {
1314 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001315 }
1316
Rob Clarke2c719b2014-12-15 13:56:32 -05001317 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001318 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001319 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001320}
1321
Chris Wilson931872f2012-01-16 23:01:13 +00001322static void assert_plane(struct drm_i915_private *dev_priv,
1323 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001324{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001325 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001326 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001327
Ville Syrjälä649636e2015-09-22 19:50:01 +03001328 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001329 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001330 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001331 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001332 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001333}
1334
Chris Wilson931872f2012-01-16 23:01:13 +00001335#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1336#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1337
Jesse Barnesb24e7172011-01-04 15:09:30 -08001338static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1339 enum pipe pipe)
1340{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001341 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001342 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001343
Ville Syrjälä653e1022013-06-04 13:49:05 +03001344 /* Primary planes are fixed to pipes on gen4+ */
1345 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001346 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001347 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001348 "plane %c assertion failure, should be disabled but not\n",
1349 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001350 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001351 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001352
Jesse Barnesb24e7172011-01-04 15:09:30 -08001353 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001354 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001355 u32 val = I915_READ(DSPCNTR(i));
1356 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001357 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001358 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001359 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1360 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001361 }
1362}
1363
Jesse Barnes19332d72013-03-28 09:55:38 -07001364static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1365 enum pipe pipe)
1366{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001367 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001368 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001369
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001370 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001371 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001372 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001373 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001374 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1375 sprite, pipe_name(pipe));
1376 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001377 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001378 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001379 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001380 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001381 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001382 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001383 }
1384 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001385 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001386 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001387 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001388 plane_name(pipe), pipe_name(pipe));
1389 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001390 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001391 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001392 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1393 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001394 }
1395}
1396
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001397static void assert_vblank_disabled(struct drm_crtc *crtc)
1398{
Rob Clarke2c719b2014-12-15 13:56:32 -05001399 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001400 drm_crtc_vblank_put(crtc);
1401}
1402
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001403void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1404 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001405{
Jesse Barnes92f25842011-01-04 15:09:34 -08001406 u32 val;
1407 bool enabled;
1408
Ville Syrjälä649636e2015-09-22 19:50:01 +03001409 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001410 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001411 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001412 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1413 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001414}
1415
Keith Packard4e634382011-08-06 10:39:45 -07001416static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1417 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001418{
1419 if ((val & DP_PORT_EN) == 0)
1420 return false;
1421
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001422 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001423 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001424 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1425 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001426 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001427 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1428 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001429 } else {
1430 if ((val & DP_PIPE_MASK) != (pipe << 30))
1431 return false;
1432 }
1433 return true;
1434}
1435
Keith Packard1519b992011-08-06 10:35:34 -07001436static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1437 enum pipe pipe, u32 val)
1438{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001439 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001440 return false;
1441
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001442 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001443 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001444 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001445 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001446 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1447 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001448 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001449 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001450 return false;
1451 }
1452 return true;
1453}
1454
1455static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1456 enum pipe pipe, u32 val)
1457{
1458 if ((val & LVDS_PORT_EN) == 0)
1459 return false;
1460
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001461 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001462 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1463 return false;
1464 } else {
1465 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1466 return false;
1467 }
1468 return true;
1469}
1470
1471static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1472 enum pipe pipe, u32 val)
1473{
1474 if ((val & ADPA_DAC_ENABLE) == 0)
1475 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001476 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001477 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1478 return false;
1479 } else {
1480 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1481 return false;
1482 }
1483 return true;
1484}
1485
Jesse Barnes291906f2011-02-02 12:28:03 -08001486static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001487 enum pipe pipe, i915_reg_t reg,
1488 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001489{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001490 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001491 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001492 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001493 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001494
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001495 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001496 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001497 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001498}
1499
1500static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001501 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001502{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001503 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001504 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001505 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001506 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001507
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001508 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001509 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001510 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001511}
1512
1513static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1514 enum pipe pipe)
1515{
Jesse Barnes291906f2011-02-02 12:28:03 -08001516 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001517
Keith Packardf0575e92011-07-25 22:12:43 -07001518 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1519 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1520 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001521
Ville Syrjälä649636e2015-09-22 19:50:01 +03001522 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001523 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001524 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001525 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001526
Ville Syrjälä649636e2015-09-22 19:50:01 +03001527 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001528 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001529 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001530 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001531
Paulo Zanonie2debe92013-02-18 19:00:27 -03001532 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1533 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1534 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001535}
1536
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001537static void _vlv_enable_pll(struct intel_crtc *crtc,
1538 const struct intel_crtc_state *pipe_config)
1539{
1540 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1541 enum pipe pipe = crtc->pipe;
1542
1543 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1544 POSTING_READ(DPLL(pipe));
1545 udelay(150);
1546
1547 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1548 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1549}
1550
Ville Syrjäläd288f652014-10-28 13:20:22 +02001551static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001552 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001553{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001554 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001555 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001556
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001557 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001558
Daniel Vetter87442f72013-06-06 00:52:17 +02001559 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001560 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001561
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001562 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1563 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001564
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001565 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1566 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001567}
1568
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001569
1570static void _chv_enable_pll(struct intel_crtc *crtc,
1571 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001572{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001573 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001574 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001575 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001576 u32 tmp;
1577
Ville Syrjäläa5805162015-05-26 20:42:30 +03001578 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001579
1580 /* Enable back the 10bit clock to display controller */
1581 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1582 tmp |= DPIO_DCLKP_EN;
1583 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1584
Ville Syrjälä54433e92015-05-26 20:42:31 +03001585 mutex_unlock(&dev_priv->sb_lock);
1586
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001587 /*
1588 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1589 */
1590 udelay(1);
1591
1592 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001593 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001594
1595 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001596 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001597 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001598}
1599
1600static void chv_enable_pll(struct intel_crtc *crtc,
1601 const struct intel_crtc_state *pipe_config)
1602{
1603 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1604 enum pipe pipe = crtc->pipe;
1605
1606 assert_pipe_disabled(dev_priv, pipe);
1607
1608 /* PLL is protected by panel, make sure we can write it */
1609 assert_panel_unlocked(dev_priv, pipe);
1610
1611 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1612 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001613
Ville Syrjäläc2317752016-03-15 16:39:56 +02001614 if (pipe != PIPE_A) {
1615 /*
1616 * WaPixelRepeatModeFixForC0:chv
1617 *
1618 * DPLLCMD is AWOL. Use chicken bits to propagate
1619 * the value from DPLLBMD to either pipe B or C.
1620 */
1621 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1622 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1623 I915_WRITE(CBR4_VLV, 0);
1624 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1625
1626 /*
1627 * DPLLB VGA mode also seems to cause problems.
1628 * We should always have it disabled.
1629 */
1630 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1631 } else {
1632 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1633 POSTING_READ(DPLL_MD(pipe));
1634 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001635}
1636
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001637static int intel_num_dvo_pipes(struct drm_device *dev)
1638{
1639 struct intel_crtc *crtc;
1640 int count = 0;
1641
1642 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001643 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001644 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001645
1646 return count;
1647}
1648
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001649static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001650{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001651 struct drm_device *dev = crtc->base.dev;
1652 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001653 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001654 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001655
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001656 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001657
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001658 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001659 if (IS_MOBILE(dev) && !IS_I830(dev))
1660 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001661
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001662 /* Enable DVO 2x clock on both PLLs if necessary */
1663 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1664 /*
1665 * It appears to be important that we don't enable this
1666 * for the current pipe before otherwise configuring the
1667 * PLL. No idea how this should be handled if multiple
1668 * DVO outputs are enabled simultaneosly.
1669 */
1670 dpll |= DPLL_DVO_2X_MODE;
1671 I915_WRITE(DPLL(!crtc->pipe),
1672 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1673 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001674
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001675 /*
1676 * Apparently we need to have VGA mode enabled prior to changing
1677 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1678 * dividers, even though the register value does change.
1679 */
1680 I915_WRITE(reg, 0);
1681
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001682 I915_WRITE(reg, dpll);
1683
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001684 /* Wait for the clocks to stabilize. */
1685 POSTING_READ(reg);
1686 udelay(150);
1687
1688 if (INTEL_INFO(dev)->gen >= 4) {
1689 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001690 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001691 } else {
1692 /* The pixel multiplier can only be updated once the
1693 * DPLL is enabled and the clocks are stable.
1694 *
1695 * So write it again.
1696 */
1697 I915_WRITE(reg, dpll);
1698 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001699
1700 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001701 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001702 POSTING_READ(reg);
1703 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001704 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001705 POSTING_READ(reg);
1706 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001707 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001708 POSTING_READ(reg);
1709 udelay(150); /* wait for warmup */
1710}
1711
1712/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001713 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001714 * @dev_priv: i915 private structure
1715 * @pipe: pipe PLL to disable
1716 *
1717 * Disable the PLL for @pipe, making sure the pipe is off first.
1718 *
1719 * Note! This is for pre-ILK only.
1720 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001721static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001722{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001723 struct drm_device *dev = crtc->base.dev;
1724 struct drm_i915_private *dev_priv = dev->dev_private;
1725 enum pipe pipe = crtc->pipe;
1726
1727 /* Disable DVO 2x clock on both PLLs if necessary */
1728 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001729 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001730 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001731 I915_WRITE(DPLL(PIPE_B),
1732 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1733 I915_WRITE(DPLL(PIPE_A),
1734 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1735 }
1736
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001737 /* Don't disable pipe or pipe PLLs if needed */
1738 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1739 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001740 return;
1741
1742 /* Make sure the pipe isn't still relying on us */
1743 assert_pipe_disabled(dev_priv, pipe);
1744
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001745 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001746 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001747}
1748
Jesse Barnesf6071162013-10-01 10:41:38 -07001749static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1750{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001751 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001752
1753 /* Make sure the pipe isn't still relying on us */
1754 assert_pipe_disabled(dev_priv, pipe);
1755
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001756 val = DPLL_INTEGRATED_REF_CLK_VLV |
1757 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1758 if (pipe != PIPE_A)
1759 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1760
Jesse Barnesf6071162013-10-01 10:41:38 -07001761 I915_WRITE(DPLL(pipe), val);
1762 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001763}
1764
1765static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1766{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001767 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001768 u32 val;
1769
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001770 /* Make sure the pipe isn't still relying on us */
1771 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001772
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001773 val = DPLL_SSC_REF_CLK_CHV |
1774 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001775 if (pipe != PIPE_A)
1776 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001777
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001778 I915_WRITE(DPLL(pipe), val);
1779 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001780
Ville Syrjäläa5805162015-05-26 20:42:30 +03001781 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001782
1783 /* Disable 10bit clock to display controller */
1784 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1785 val &= ~DPIO_DCLKP_EN;
1786 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1787
Ville Syrjäläa5805162015-05-26 20:42:30 +03001788 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001789}
1790
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001791void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001792 struct intel_digital_port *dport,
1793 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001794{
1795 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001796 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001797
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001798 switch (dport->port) {
1799 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001800 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001801 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001802 break;
1803 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001804 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001805 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001806 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001807 break;
1808 case PORT_D:
1809 port_mask = DPLL_PORTD_READY_MASK;
1810 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001811 break;
1812 default:
1813 BUG();
1814 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001815
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001816 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1817 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1818 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001819}
1820
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001821static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1822 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001823{
Daniel Vetter23670b322012-11-01 09:15:30 +01001824 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001825 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001826 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001827 i915_reg_t reg;
1828 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001829
Jesse Barnes040484a2011-01-03 12:14:26 -08001830 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001831 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001832
1833 /* FDI must be feeding us bits for PCH ports */
1834 assert_fdi_tx_enabled(dev_priv, pipe);
1835 assert_fdi_rx_enabled(dev_priv, pipe);
1836
Daniel Vetter23670b322012-11-01 09:15:30 +01001837 if (HAS_PCH_CPT(dev)) {
1838 /* Workaround: Set the timing override bit before enabling the
1839 * pch transcoder. */
1840 reg = TRANS_CHICKEN2(pipe);
1841 val = I915_READ(reg);
1842 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1843 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001844 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001845
Daniel Vetterab9412b2013-05-03 11:49:46 +02001846 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001847 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001848 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001849
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001850 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001851 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001852 * Make the BPC in transcoder be consistent with
1853 * that in pipeconf reg. For HDMI we must use 8bpc
1854 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001855 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001856 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001857 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1858 val |= PIPECONF_8BPC;
1859 else
1860 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001861 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001862
1863 val &= ~TRANS_INTERLACE_MASK;
1864 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001865 if (HAS_PCH_IBX(dev_priv) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001866 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001867 val |= TRANS_LEGACY_INTERLACED_ILK;
1868 else
1869 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001870 else
1871 val |= TRANS_PROGRESSIVE;
1872
Jesse Barnes040484a2011-01-03 12:14:26 -08001873 I915_WRITE(reg, val | TRANS_ENABLE);
1874 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001875 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001876}
1877
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001878static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001879 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001880{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001881 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001882
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001883 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001884 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001885 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001886
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001887 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001888 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001889 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001890 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001891
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001892 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001893 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001894
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001895 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1896 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001897 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001898 else
1899 val |= TRANS_PROGRESSIVE;
1900
Daniel Vetterab9412b2013-05-03 11:49:46 +02001901 I915_WRITE(LPT_TRANSCONF, val);
1902 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001903 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001904}
1905
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001906static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1907 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001908{
Daniel Vetter23670b322012-11-01 09:15:30 +01001909 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001910 i915_reg_t reg;
1911 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001912
1913 /* FDI relies on the transcoder */
1914 assert_fdi_tx_disabled(dev_priv, pipe);
1915 assert_fdi_rx_disabled(dev_priv, pipe);
1916
Jesse Barnes291906f2011-02-02 12:28:03 -08001917 /* Ports must be off as well */
1918 assert_pch_ports_disabled(dev_priv, pipe);
1919
Daniel Vetterab9412b2013-05-03 11:49:46 +02001920 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001921 val = I915_READ(reg);
1922 val &= ~TRANS_ENABLE;
1923 I915_WRITE(reg, val);
1924 /* wait for PCH transcoder off, transcoder state */
1925 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001926 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001927
Ville Syrjäläc4656132015-10-29 21:25:56 +02001928 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001929 /* Workaround: Clear the timing override chicken bit again. */
1930 reg = TRANS_CHICKEN2(pipe);
1931 val = I915_READ(reg);
1932 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1933 I915_WRITE(reg, val);
1934 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001935}
1936
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001937static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001938{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001939 u32 val;
1940
Daniel Vetterab9412b2013-05-03 11:49:46 +02001941 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001942 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001943 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001944 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001945 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001946 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001947
1948 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001949 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001950 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001951 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001952}
1953
1954/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001955 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001956 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001957 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001958 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001959 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001960 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001961static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001962{
Paulo Zanoni03722642014-01-17 13:51:09 -02001963 struct drm_device *dev = crtc->base.dev;
1964 struct drm_i915_private *dev_priv = dev->dev_private;
1965 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001966 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001967 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001968 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001969 u32 val;
1970
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001971 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1972
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001973 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001974 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001975 assert_sprites_disabled(dev_priv, pipe);
1976
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001977 if (HAS_PCH_LPT(dev_priv))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001978 pch_transcoder = TRANSCODER_A;
1979 else
1980 pch_transcoder = pipe;
1981
Jesse Barnesb24e7172011-01-04 15:09:30 -08001982 /*
1983 * A pipe without a PLL won't actually be able to drive bits from
1984 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1985 * need the check.
1986 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001987 if (HAS_GMCH_DISPLAY(dev_priv))
Jani Nikulaa65347b2015-11-27 12:21:46 +02001988 if (crtc->config->has_dsi_encoder)
Jani Nikula23538ef2013-08-27 15:12:22 +03001989 assert_dsi_pll_enabled(dev_priv);
1990 else
1991 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001992 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001993 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001994 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001995 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001996 assert_fdi_tx_pll_enabled(dev_priv,
1997 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001998 }
1999 /* FIXME: assert CPU port conditions for SNB+ */
2000 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002001
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002002 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002003 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002004 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002005 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2006 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002007 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002008 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002009
2010 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002011 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02002012
2013 /*
2014 * Until the pipe starts DSL will read as 0, which would cause
2015 * an apparent vblank timestamp jump, which messes up also the
2016 * frame count when it's derived from the timestamps. So let's
2017 * wait for the pipe to start properly before we call
2018 * drm_crtc_vblank_on()
2019 */
2020 if (dev->max_vblank_count == 0 &&
2021 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
2022 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08002023}
2024
2025/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002026 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002027 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002028 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002029 * Disable the pipe of @crtc, making sure that various hardware
2030 * specific requirements are met, if applicable, e.g. plane
2031 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002032 *
2033 * Will wait until the pipe has shut down before returning.
2034 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002035static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002036{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002037 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002038 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002039 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002040 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002041 u32 val;
2042
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002043 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2044
Jesse Barnesb24e7172011-01-04 15:09:30 -08002045 /*
2046 * Make sure planes won't keep trying to pump pixels to us,
2047 * or we might hang the display.
2048 */
2049 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002050 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002051 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002052
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002053 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002054 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002055 if ((val & PIPECONF_ENABLE) == 0)
2056 return;
2057
Ville Syrjälä67adc642014-08-15 01:21:57 +03002058 /*
2059 * Double wide has implications for planes
2060 * so best keep it disabled when not needed.
2061 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002062 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002063 val &= ~PIPECONF_DOUBLE_WIDE;
2064
2065 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002066 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2067 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002068 val &= ~PIPECONF_ENABLE;
2069
2070 I915_WRITE(reg, val);
2071 if ((val & PIPECONF_ENABLE) == 0)
2072 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002073}
2074
Chris Wilson693db182013-03-05 14:52:39 +00002075static bool need_vtd_wa(struct drm_device *dev)
2076{
2077#ifdef CONFIG_INTEL_IOMMU
2078 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2079 return true;
2080#endif
2081 return false;
2082}
2083
Ville Syrjälä832be822016-01-12 21:08:33 +02002084static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2085{
2086 return IS_GEN2(dev_priv) ? 2048 : 4096;
2087}
2088
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002089static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2090 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002091{
2092 switch (fb_modifier) {
2093 case DRM_FORMAT_MOD_NONE:
2094 return cpp;
2095 case I915_FORMAT_MOD_X_TILED:
2096 if (IS_GEN2(dev_priv))
2097 return 128;
2098 else
2099 return 512;
2100 case I915_FORMAT_MOD_Y_TILED:
2101 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2102 return 128;
2103 else
2104 return 512;
2105 case I915_FORMAT_MOD_Yf_TILED:
2106 switch (cpp) {
2107 case 1:
2108 return 64;
2109 case 2:
2110 case 4:
2111 return 128;
2112 case 8:
2113 case 16:
2114 return 256;
2115 default:
2116 MISSING_CASE(cpp);
2117 return cpp;
2118 }
2119 break;
2120 default:
2121 MISSING_CASE(fb_modifier);
2122 return cpp;
2123 }
2124}
2125
Ville Syrjälä832be822016-01-12 21:08:33 +02002126unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2127 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002128{
Ville Syrjälä832be822016-01-12 21:08:33 +02002129 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2130 return 1;
2131 else
2132 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002133 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002134}
2135
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002136/* Return the tile dimensions in pixel units */
2137static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2138 unsigned int *tile_width,
2139 unsigned int *tile_height,
2140 uint64_t fb_modifier,
2141 unsigned int cpp)
2142{
2143 unsigned int tile_width_bytes =
2144 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2145
2146 *tile_width = tile_width_bytes / cpp;
2147 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2148}
2149
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002150unsigned int
2151intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002152 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002153{
Ville Syrjälä832be822016-01-12 21:08:33 +02002154 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2155 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2156
2157 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002158}
2159
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002160unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2161{
2162 unsigned int size = 0;
2163 int i;
2164
2165 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2166 size += rot_info->plane[i].width * rot_info->plane[i].height;
2167
2168 return size;
2169}
2170
Daniel Vetter75c82a52015-10-14 16:51:04 +02002171static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002172intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2173 const struct drm_framebuffer *fb,
2174 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002175{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002176 if (intel_rotation_90_or_270(rotation)) {
2177 *view = i915_ggtt_view_rotated;
2178 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2179 } else {
2180 *view = i915_ggtt_view_normal;
2181 }
2182}
2183
2184static void
2185intel_fill_fb_info(struct drm_i915_private *dev_priv,
2186 struct drm_framebuffer *fb)
2187{
2188 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002189 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002190
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002191 tile_size = intel_tile_size(dev_priv);
2192
2193 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002194 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2195 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002196
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002197 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2198 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002199
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002200 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002201 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002202 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2203 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002204
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002205 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002206 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2207 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002208 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002209}
2210
Ville Syrjälä603525d2016-01-12 21:08:37 +02002211static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002212{
2213 if (INTEL_INFO(dev_priv)->gen >= 9)
2214 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002215 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002216 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002217 return 128 * 1024;
2218 else if (INTEL_INFO(dev_priv)->gen >= 4)
2219 return 4 * 1024;
2220 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002221 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002222}
2223
Ville Syrjälä603525d2016-01-12 21:08:37 +02002224static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2225 uint64_t fb_modifier)
2226{
2227 switch (fb_modifier) {
2228 case DRM_FORMAT_MOD_NONE:
2229 return intel_linear_alignment(dev_priv);
2230 case I915_FORMAT_MOD_X_TILED:
2231 if (INTEL_INFO(dev_priv)->gen >= 9)
2232 return 256 * 1024;
2233 return 0;
2234 case I915_FORMAT_MOD_Y_TILED:
2235 case I915_FORMAT_MOD_Yf_TILED:
2236 return 1 * 1024 * 1024;
2237 default:
2238 MISSING_CASE(fb_modifier);
2239 return 0;
2240 }
2241}
2242
Chris Wilson127bd2a2010-07-23 23:32:05 +01002243int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002244intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2245 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002246{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002247 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002248 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002249 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002250 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002251 u32 alignment;
2252 int ret;
2253
Matt Roperebcdd392014-07-09 16:22:11 -07002254 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2255
Ville Syrjälä603525d2016-01-12 21:08:37 +02002256 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002257
Ville Syrjälä3465c582016-02-15 22:54:43 +02002258 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002259
Chris Wilson693db182013-03-05 14:52:39 +00002260 /* Note that the w/a also requires 64 PTE of padding following the
2261 * bo. We currently fill all unused PTE with the shadow page and so
2262 * we should always have valid PTE following the scanout preventing
2263 * the VT-d warning.
2264 */
2265 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2266 alignment = 256 * 1024;
2267
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002268 /*
2269 * Global gtt pte registers are special registers which actually forward
2270 * writes to a chunk of system memory. Which means that there is no risk
2271 * that the register values disappear as soon as we call
2272 * intel_runtime_pm_put(), so it is correct to wrap only the
2273 * pin/unpin/fence and not more.
2274 */
2275 intel_runtime_pm_get(dev_priv);
2276
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002277 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2278 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002279 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002280 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002281
2282 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2283 * fence, whereas 965+ only requires a fence if using
2284 * framebuffer compression. For simplicity, we always install
2285 * a fence as the cost is not that onerous.
2286 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002287 if (view.type == I915_GGTT_VIEW_NORMAL) {
2288 ret = i915_gem_object_get_fence(obj);
2289 if (ret == -EDEADLK) {
2290 /*
2291 * -EDEADLK means there are no free fences
2292 * no pending flips.
2293 *
2294 * This is propagated to atomic, but it uses
2295 * -EDEADLK to force a locking recovery, so
2296 * change the returned error to -EBUSY.
2297 */
2298 ret = -EBUSY;
2299 goto err_unpin;
2300 } else if (ret)
2301 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002302
Vivek Kasireddy98072162015-10-29 18:54:38 -07002303 i915_gem_object_pin_fence(obj);
2304 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002305
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002306 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002307 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002308
2309err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002310 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002311err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002312 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002313 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002314}
2315
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +01002316void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002317{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002318 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002319 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002320
Matt Roperebcdd392014-07-09 16:22:11 -07002321 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2322
Ville Syrjälä3465c582016-02-15 22:54:43 +02002323 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002324
Vivek Kasireddy98072162015-10-29 18:54:38 -07002325 if (view.type == I915_GGTT_VIEW_NORMAL)
2326 i915_gem_object_unpin_fence(obj);
2327
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002328 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002329}
2330
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002331/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002332 * Adjust the tile offset by moving the difference into
2333 * the x/y offsets.
2334 *
2335 * Input tile dimensions and pitch must already be
2336 * rotated to match x and y, and in pixel units.
2337 */
2338static u32 intel_adjust_tile_offset(int *x, int *y,
2339 unsigned int tile_width,
2340 unsigned int tile_height,
2341 unsigned int tile_size,
2342 unsigned int pitch_tiles,
2343 u32 old_offset,
2344 u32 new_offset)
2345{
2346 unsigned int tiles;
2347
2348 WARN_ON(old_offset & (tile_size - 1));
2349 WARN_ON(new_offset & (tile_size - 1));
2350 WARN_ON(new_offset > old_offset);
2351
2352 tiles = (old_offset - new_offset) / tile_size;
2353
2354 *y += tiles / pitch_tiles * tile_height;
2355 *x += tiles % pitch_tiles * tile_width;
2356
2357 return new_offset;
2358}
2359
2360/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002361 * Computes the linear offset to the base tile and adjusts
2362 * x, y. bytes per pixel is assumed to be a power-of-two.
2363 *
2364 * In the 90/270 rotated case, x and y are assumed
2365 * to be already rotated to match the rotated GTT view, and
2366 * pitch is the tile_height aligned framebuffer height.
2367 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002368u32 intel_compute_tile_offset(int *x, int *y,
2369 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002370 unsigned int pitch,
2371 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002372{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002373 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2374 uint64_t fb_modifier = fb->modifier[plane];
2375 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002376 u32 offset, offset_aligned, alignment;
2377
2378 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2379 if (alignment)
2380 alignment--;
2381
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002382 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002383 unsigned int tile_size, tile_width, tile_height;
2384 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002385
Ville Syrjäläd8433102016-01-12 21:08:35 +02002386 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002387 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2388 fb_modifier, cpp);
2389
2390 if (intel_rotation_90_or_270(rotation)) {
2391 pitch_tiles = pitch / tile_height;
2392 swap(tile_width, tile_height);
2393 } else {
2394 pitch_tiles = pitch / (tile_width * cpp);
2395 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002396
Ville Syrjäläd8433102016-01-12 21:08:35 +02002397 tile_rows = *y / tile_height;
2398 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002399
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002400 tiles = *x / tile_width;
2401 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002402
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002403 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2404 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002405
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002406 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2407 tile_size, pitch_tiles,
2408 offset, offset_aligned);
2409 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002410 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002411 offset_aligned = offset & ~alignment;
2412
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002413 *y = (offset & alignment) / pitch;
2414 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002415 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002416
2417 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002418}
2419
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002420static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002421{
2422 switch (format) {
2423 case DISPPLANE_8BPP:
2424 return DRM_FORMAT_C8;
2425 case DISPPLANE_BGRX555:
2426 return DRM_FORMAT_XRGB1555;
2427 case DISPPLANE_BGRX565:
2428 return DRM_FORMAT_RGB565;
2429 default:
2430 case DISPPLANE_BGRX888:
2431 return DRM_FORMAT_XRGB8888;
2432 case DISPPLANE_RGBX888:
2433 return DRM_FORMAT_XBGR8888;
2434 case DISPPLANE_BGRX101010:
2435 return DRM_FORMAT_XRGB2101010;
2436 case DISPPLANE_RGBX101010:
2437 return DRM_FORMAT_XBGR2101010;
2438 }
2439}
2440
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002441static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2442{
2443 switch (format) {
2444 case PLANE_CTL_FORMAT_RGB_565:
2445 return DRM_FORMAT_RGB565;
2446 default:
2447 case PLANE_CTL_FORMAT_XRGB_8888:
2448 if (rgb_order) {
2449 if (alpha)
2450 return DRM_FORMAT_ABGR8888;
2451 else
2452 return DRM_FORMAT_XBGR8888;
2453 } else {
2454 if (alpha)
2455 return DRM_FORMAT_ARGB8888;
2456 else
2457 return DRM_FORMAT_XRGB8888;
2458 }
2459 case PLANE_CTL_FORMAT_XRGB_2101010:
2460 if (rgb_order)
2461 return DRM_FORMAT_XBGR2101010;
2462 else
2463 return DRM_FORMAT_XRGB2101010;
2464 }
2465}
2466
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002467static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002468intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2469 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002470{
2471 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002472 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002473 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002474 struct drm_i915_gem_object *obj = NULL;
2475 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002476 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002477 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2478 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2479 PAGE_SIZE);
2480
2481 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002482
Chris Wilsonff2652e2014-03-10 08:07:02 +00002483 if (plane_config->size == 0)
2484 return false;
2485
Paulo Zanoni3badb492015-09-23 12:52:23 -03002486 /* If the FB is too big, just don't use it since fbdev is not very
2487 * important and we should probably use that space with FBC or other
2488 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002489 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002490 return false;
2491
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002492 mutex_lock(&dev->struct_mutex);
2493
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002494 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2495 base_aligned,
2496 base_aligned,
2497 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002498 if (!obj) {
2499 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002500 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002501 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002502
Damien Lespiau49af4492015-01-20 12:51:44 +00002503 obj->tiling_mode = plane_config->tiling;
2504 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002505 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002506
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002507 mode_cmd.pixel_format = fb->pixel_format;
2508 mode_cmd.width = fb->width;
2509 mode_cmd.height = fb->height;
2510 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002511 mode_cmd.modifier[0] = fb->modifier[0];
2512 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002513
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002514 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002515 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002516 DRM_DEBUG_KMS("intel fb init failed\n");
2517 goto out_unref_obj;
2518 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002519
Jesse Barnes46f297f2014-03-07 08:57:48 -08002520 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002521
Daniel Vetterf6936e22015-03-26 12:17:05 +01002522 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002523 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002524
2525out_unref_obj:
2526 drm_gem_object_unreference(&obj->base);
2527 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002528 return false;
2529}
2530
Daniel Vetter5a21b662016-05-24 17:13:53 +02002531/* Update plane->state->fb to match plane->fb after driver-internal updates */
2532static void
2533update_state_fb(struct drm_plane *plane)
2534{
2535 if (plane->fb == plane->state->fb)
2536 return;
2537
2538 if (plane->state->fb)
2539 drm_framebuffer_unreference(plane->state->fb);
2540 plane->state->fb = plane->fb;
2541 if (plane->state->fb)
2542 drm_framebuffer_reference(plane->state->fb);
2543}
2544
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002545static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002546intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2547 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002548{
2549 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002550 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002551 struct drm_crtc *c;
2552 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002553 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002554 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002555 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002556 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2557 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002558 struct intel_plane_state *intel_state =
2559 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002560 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002561
Damien Lespiau2d140302015-02-05 17:22:18 +00002562 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002563 return;
2564
Daniel Vetterf6936e22015-03-26 12:17:05 +01002565 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002566 fb = &plane_config->fb->base;
2567 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002568 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002569
Damien Lespiau2d140302015-02-05 17:22:18 +00002570 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002571
2572 /*
2573 * Failed to alloc the obj, check to see if we should share
2574 * an fb with another CRTC instead
2575 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002576 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002577 i = to_intel_crtc(c);
2578
2579 if (c == &intel_crtc->base)
2580 continue;
2581
Matt Roper2ff8fde2014-07-08 07:50:07 -07002582 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002583 continue;
2584
Daniel Vetter88595ac2015-03-26 12:42:24 +01002585 fb = c->primary->fb;
2586 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002587 continue;
2588
Daniel Vetter88595ac2015-03-26 12:42:24 +01002589 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002590 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002591 drm_framebuffer_reference(fb);
2592 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002593 }
2594 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002595
Matt Roper200757f2015-12-03 11:37:36 -08002596 /*
2597 * We've failed to reconstruct the BIOS FB. Current display state
2598 * indicates that the primary plane is visible, but has a NULL FB,
2599 * which will lead to problems later if we don't fix it up. The
2600 * simplest solution is to just disable the primary plane now and
2601 * pretend the BIOS never had it enabled.
2602 */
2603 to_intel_plane_state(plane_state)->visible = false;
2604 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002605 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002606 intel_plane->disable_plane(primary, &intel_crtc->base);
2607
Daniel Vetter88595ac2015-03-26 12:42:24 +01002608 return;
2609
2610valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002611 plane_state->src_x = 0;
2612 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002613 plane_state->src_w = fb->width << 16;
2614 plane_state->src_h = fb->height << 16;
2615
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002616 plane_state->crtc_x = 0;
2617 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002618 plane_state->crtc_w = fb->width;
2619 plane_state->crtc_h = fb->height;
2620
Matt Roper0a8d8a82015-12-03 11:37:38 -08002621 intel_state->src.x1 = plane_state->src_x;
2622 intel_state->src.y1 = plane_state->src_y;
2623 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2624 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2625 intel_state->dst.x1 = plane_state->crtc_x;
2626 intel_state->dst.y1 = plane_state->crtc_y;
2627 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2628 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2629
Daniel Vetter88595ac2015-03-26 12:42:24 +01002630 obj = intel_fb_obj(fb);
2631 if (obj->tiling_mode != I915_TILING_NONE)
2632 dev_priv->preserve_bios_swizzle = true;
2633
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002634 drm_framebuffer_reference(fb);
2635 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002636 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002637 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002638 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002639}
2640
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002641static void i9xx_update_primary_plane(struct drm_plane *primary,
2642 const struct intel_crtc_state *crtc_state,
2643 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002644{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002645 struct drm_device *dev = primary->dev;
Jesse Barnes81255562010-08-02 12:07:50 -07002646 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002647 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2648 struct drm_framebuffer *fb = plane_state->base.fb;
2649 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002650 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002651 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002652 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002653 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002654 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002655 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002656 int x = plane_state->src.x1 >> 16;
2657 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002658
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002659 dspcntr = DISPPLANE_GAMMA_ENABLE;
2660
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002661 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002662
2663 if (INTEL_INFO(dev)->gen < 4) {
2664 if (intel_crtc->pipe == PIPE_B)
2665 dspcntr |= DISPPLANE_SEL_PIPE_B;
2666
2667 /* pipesrc and dspsize control the size that is scaled from,
2668 * which should always be the user's requested size.
2669 */
2670 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002671 ((crtc_state->pipe_src_h - 1) << 16) |
2672 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002673 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002674 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2675 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002676 ((crtc_state->pipe_src_h - 1) << 16) |
2677 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002678 I915_WRITE(PRIMPOS(plane), 0);
2679 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002680 }
2681
Ville Syrjälä57779d02012-10-31 17:50:14 +02002682 switch (fb->pixel_format) {
2683 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002684 dspcntr |= DISPPLANE_8BPP;
2685 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002686 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002687 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002688 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002689 case DRM_FORMAT_RGB565:
2690 dspcntr |= DISPPLANE_BGRX565;
2691 break;
2692 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002693 dspcntr |= DISPPLANE_BGRX888;
2694 break;
2695 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002696 dspcntr |= DISPPLANE_RGBX888;
2697 break;
2698 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002699 dspcntr |= DISPPLANE_BGRX101010;
2700 break;
2701 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002702 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002703 break;
2704 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002705 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002706 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002707
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002708 if (INTEL_INFO(dev)->gen >= 4 &&
2709 obj->tiling_mode != I915_TILING_NONE)
2710 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002711
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002712 if (IS_G4X(dev))
2713 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2714
Ville Syrjäläac484962016-01-20 21:05:26 +02002715 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002716
Daniel Vetterc2c75132012-07-05 12:17:30 +02002717 if (INTEL_INFO(dev)->gen >= 4) {
2718 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002719 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002720 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002721 linear_offset -= intel_crtc->dspaddr_offset;
2722 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002723 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002724 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002725
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002726 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302727 dspcntr |= DISPPLANE_ROTATE_180;
2728
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002729 x += (crtc_state->pipe_src_w - 1);
2730 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302731
2732 /* Finding the last pixel of the last line of the display
2733 data and adding to linear_offset*/
2734 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002735 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002736 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302737 }
2738
Paulo Zanoni2db33662015-09-14 15:20:03 -03002739 intel_crtc->adjusted_x = x;
2740 intel_crtc->adjusted_y = y;
2741
Sonika Jindal48404c12014-08-22 14:06:04 +05302742 I915_WRITE(reg, dspcntr);
2743
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002744 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002745 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002746 I915_WRITE(DSPSURF(plane),
2747 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002748 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002749 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002750 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002751 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002752 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002753}
2754
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002755static void i9xx_disable_primary_plane(struct drm_plane *primary,
2756 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002757{
2758 struct drm_device *dev = crtc->dev;
2759 struct drm_i915_private *dev_priv = dev->dev_private;
2760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002761 int plane = intel_crtc->plane;
2762
2763 I915_WRITE(DSPCNTR(plane), 0);
2764 if (INTEL_INFO(dev_priv)->gen >= 4)
2765 I915_WRITE(DSPSURF(plane), 0);
2766 else
2767 I915_WRITE(DSPADDR(plane), 0);
2768 POSTING_READ(DSPCNTR(plane));
2769}
2770
2771static void ironlake_update_primary_plane(struct drm_plane *primary,
2772 const struct intel_crtc_state *crtc_state,
2773 const struct intel_plane_state *plane_state)
2774{
2775 struct drm_device *dev = primary->dev;
2776 struct drm_i915_private *dev_priv = dev->dev_private;
2777 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2778 struct drm_framebuffer *fb = plane_state->base.fb;
2779 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002780 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002781 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002782 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002783 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002784 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002785 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002786 int x = plane_state->src.x1 >> 16;
2787 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002788
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002789 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002790 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002791
2792 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2793 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2794
Ville Syrjälä57779d02012-10-31 17:50:14 +02002795 switch (fb->pixel_format) {
2796 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002797 dspcntr |= DISPPLANE_8BPP;
2798 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002799 case DRM_FORMAT_RGB565:
2800 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002801 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002802 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002803 dspcntr |= DISPPLANE_BGRX888;
2804 break;
2805 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002806 dspcntr |= DISPPLANE_RGBX888;
2807 break;
2808 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002809 dspcntr |= DISPPLANE_BGRX101010;
2810 break;
2811 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002812 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002813 break;
2814 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002815 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002816 }
2817
2818 if (obj->tiling_mode != I915_TILING_NONE)
2819 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002820
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002821 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002822 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002823
Ville Syrjäläac484962016-01-20 21:05:26 +02002824 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002825 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002826 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002827 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002828 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002829 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302830 dspcntr |= DISPPLANE_ROTATE_180;
2831
2832 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002833 x += (crtc_state->pipe_src_w - 1);
2834 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302835
2836 /* Finding the last pixel of the last line of the display
2837 data and adding to linear_offset*/
2838 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002839 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002840 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302841 }
2842 }
2843
Paulo Zanoni2db33662015-09-14 15:20:03 -03002844 intel_crtc->adjusted_x = x;
2845 intel_crtc->adjusted_y = y;
2846
Sonika Jindal48404c12014-08-22 14:06:04 +05302847 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002848
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002849 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002850 I915_WRITE(DSPSURF(plane),
2851 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002852 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002853 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2854 } else {
2855 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2856 I915_WRITE(DSPLINOFF(plane), linear_offset);
2857 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002858 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002859}
2860
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002861u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2862 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002863{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002864 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2865 return 64;
2866 } else {
2867 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002868
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002869 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002870 }
2871}
2872
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002873u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2874 struct drm_i915_gem_object *obj,
2875 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002876{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002877 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002878 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002879 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002880
Ville Syrjäläe7941292016-01-19 18:23:17 +02002881 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002882 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002883
Daniel Vetterce7f1722015-10-14 16:51:06 +02002884 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002885 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002886 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002887 return -1;
2888
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002889 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002890
2891 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002892 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002893 PAGE_SIZE;
2894 }
2895
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002896 WARN_ON(upper_32_bits(offset));
2897
2898 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002899}
2900
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002901static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2902{
2903 struct drm_device *dev = intel_crtc->base.dev;
2904 struct drm_i915_private *dev_priv = dev->dev_private;
2905
2906 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2907 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2908 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002909}
2910
Chandra Kondurua1b22782015-04-07 15:28:45 -07002911/*
2912 * This function detaches (aka. unbinds) unused scalers in hardware
2913 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002914static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002915{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002916 struct intel_crtc_scaler_state *scaler_state;
2917 int i;
2918
Chandra Kondurua1b22782015-04-07 15:28:45 -07002919 scaler_state = &intel_crtc->config->scaler_state;
2920
2921 /* loop through and disable scalers that aren't in use */
2922 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002923 if (!scaler_state->scalers[i].in_use)
2924 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002925 }
2926}
2927
Chandra Konduru6156a452015-04-27 13:48:39 -07002928u32 skl_plane_ctl_format(uint32_t pixel_format)
2929{
Chandra Konduru6156a452015-04-27 13:48:39 -07002930 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002931 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002932 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002933 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002934 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002935 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002936 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002937 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002938 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002939 /*
2940 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2941 * to be already pre-multiplied. We need to add a knob (or a different
2942 * DRM_FORMAT) for user-space to configure that.
2943 */
2944 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002945 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002946 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002947 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002948 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002949 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002951 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002952 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002953 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002955 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002956 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002957 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002959 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002960 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002961 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002962 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002963 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002964 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002965
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002966 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002967}
2968
2969u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2970{
Chandra Konduru6156a452015-04-27 13:48:39 -07002971 switch (fb_modifier) {
2972 case DRM_FORMAT_MOD_NONE:
2973 break;
2974 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002975 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002976 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002977 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002978 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002979 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002980 default:
2981 MISSING_CASE(fb_modifier);
2982 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002983
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002984 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002985}
2986
2987u32 skl_plane_ctl_rotation(unsigned int rotation)
2988{
Chandra Konduru6156a452015-04-27 13:48:39 -07002989 switch (rotation) {
2990 case BIT(DRM_ROTATE_0):
2991 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302992 /*
2993 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2994 * while i915 HW rotation is clockwise, thats why this swapping.
2995 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002996 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302997 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002998 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002999 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003000 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303001 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003002 default:
3003 MISSING_CASE(rotation);
3004 }
3005
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003006 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003007}
3008
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003009static void skylake_update_primary_plane(struct drm_plane *plane,
3010 const struct intel_crtc_state *crtc_state,
3011 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01003012{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003013 struct drm_device *dev = plane->dev;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003014 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003015 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3016 struct drm_framebuffer *fb = plane_state->base.fb;
3017 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003018 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303019 u32 plane_ctl, stride_div, stride;
3020 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003021 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303022 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02003023 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003024 int scaler_id = plane_state->scaler_id;
3025 int src_x = plane_state->src.x1 >> 16;
3026 int src_y = plane_state->src.y1 >> 16;
3027 int src_w = drm_rect_width(&plane_state->src) >> 16;
3028 int src_h = drm_rect_height(&plane_state->src) >> 16;
3029 int dst_x = plane_state->dst.x1;
3030 int dst_y = plane_state->dst.y1;
3031 int dst_w = drm_rect_width(&plane_state->dst);
3032 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003033
3034 plane_ctl = PLANE_CTL_ENABLE |
3035 PLANE_CTL_PIPE_GAMMA_ENABLE |
3036 PLANE_CTL_PIPE_CSC_ENABLE;
3037
Chandra Konduru6156a452015-04-27 13:48:39 -07003038 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3039 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003040 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003041 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003042
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003043 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003044 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003045 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303046
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003047 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003048
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303049 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003050 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3051
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303052 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003053 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303054 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003055 x_offset = stride * tile_height - src_y - src_h;
3056 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003057 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303058 } else {
3059 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003060 x_offset = src_x;
3061 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003062 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303063 }
3064 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003065
Paulo Zanoni2db33662015-09-14 15:20:03 -03003066 intel_crtc->adjusted_x = x_offset;
3067 intel_crtc->adjusted_y = y_offset;
3068
Damien Lespiau70d21f02013-07-03 21:06:04 +01003069 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303070 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3071 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3072 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003073
3074 if (scaler_id >= 0) {
3075 uint32_t ps_ctrl = 0;
3076
3077 WARN_ON(!dst_w || !dst_h);
3078 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3079 crtc_state->scaler_state.scalers[scaler_id].mode;
3080 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3081 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3082 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3083 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3084 I915_WRITE(PLANE_POS(pipe, 0), 0);
3085 } else {
3086 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3087 }
3088
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003089 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003090
3091 POSTING_READ(PLANE_SURF(pipe, 0));
3092}
3093
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003094static void skylake_disable_primary_plane(struct drm_plane *primary,
3095 struct drm_crtc *crtc)
3096{
3097 struct drm_device *dev = crtc->dev;
3098 struct drm_i915_private *dev_priv = dev->dev_private;
3099 int pipe = to_intel_crtc(crtc)->pipe;
3100
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003101 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3102 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3103 POSTING_READ(PLANE_SURF(pipe, 0));
3104}
3105
Jesse Barnes17638cd2011-06-24 12:19:23 -07003106/* Assume fb object is pinned & idle & fenced and just update base pointers */
3107static int
3108intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3109 int x, int y, enum mode_set_atomic state)
3110{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003111 /* Support for kgdboc is disabled, this needs a major rework. */
3112 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003113
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003114 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003115}
3116
Daniel Vetter5a21b662016-05-24 17:13:53 +02003117static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3118{
3119 struct intel_crtc *crtc;
3120
3121 for_each_intel_crtc(dev_priv->dev, crtc)
3122 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3123}
3124
Ville Syrjälä75147472014-11-24 18:28:11 +02003125static void intel_update_primary_planes(struct drm_device *dev)
3126{
Ville Syrjälä75147472014-11-24 18:28:11 +02003127 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003128
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003129 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003130 struct intel_plane *plane = to_intel_plane(crtc->primary);
3131 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003132
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003133 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003134 plane_state = to_intel_plane_state(plane->base.state);
3135
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003136 if (plane_state->visible)
3137 plane->update_plane(&plane->base,
3138 to_intel_crtc_state(crtc->state),
3139 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003140
3141 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003142 }
3143}
3144
Chris Wilsonc0336662016-05-06 15:40:21 +01003145void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003146{
3147 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003148 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003149 return;
3150
3151 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003152 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003153 return;
3154
Chris Wilsonc0336662016-05-06 15:40:21 +01003155 drm_modeset_lock_all(dev_priv->dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003156 /*
3157 * Disabling the crtcs gracefully seems nicer. Also the
3158 * g33 docs say we should at least disable all the planes.
3159 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003160 intel_display_suspend(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003161}
3162
Chris Wilsonc0336662016-05-06 15:40:21 +01003163void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003164{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003165 /*
3166 * Flips in the rings will be nuked by the reset,
3167 * so complete all pending flips so that user space
3168 * will get its events and not get stuck.
3169 */
3170 intel_complete_page_flips(dev_priv);
3171
Ville Syrjälä75147472014-11-24 18:28:11 +02003172 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003173 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003174 return;
3175
3176 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003177 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
Ville Syrjälä75147472014-11-24 18:28:11 +02003178 /*
3179 * Flips in the rings have been nuked by the reset,
3180 * so update the base address of all primary
3181 * planes to the the last fb to make sure we're
3182 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003183 *
3184 * FIXME: Atomic will make this obsolete since we won't schedule
3185 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003186 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003187 intel_update_primary_planes(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003188 return;
3189 }
3190
3191 /*
3192 * The display has been reset as well,
3193 * so need a full re-initialization.
3194 */
3195 intel_runtime_pm_disable_interrupts(dev_priv);
3196 intel_runtime_pm_enable_interrupts(dev_priv);
3197
Chris Wilsonc0336662016-05-06 15:40:21 +01003198 intel_modeset_init_hw(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003199
3200 spin_lock_irq(&dev_priv->irq_lock);
3201 if (dev_priv->display.hpd_irq_setup)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01003202 dev_priv->display.hpd_irq_setup(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003203 spin_unlock_irq(&dev_priv->irq_lock);
3204
Chris Wilsonc0336662016-05-06 15:40:21 +01003205 intel_display_resume(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003206
3207 intel_hpd_init(dev_priv);
3208
Chris Wilsonc0336662016-05-06 15:40:21 +01003209 drm_modeset_unlock_all(dev_priv->dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003210}
3211
Chris Wilson7d5e3792014-03-04 13:15:08 +00003212static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3213{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003214 struct drm_device *dev = crtc->dev;
3215 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3216 unsigned reset_counter;
3217 bool pending;
3218
3219 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3220 if (intel_crtc->reset_counter != reset_counter)
3221 return false;
3222
3223 spin_lock_irq(&dev->event_lock);
3224 pending = to_intel_crtc(crtc)->flip_work != NULL;
3225 spin_unlock_irq(&dev->event_lock);
3226
3227 return pending;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003228}
3229
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003230static void intel_update_pipe_config(struct intel_crtc *crtc,
3231 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003232{
3233 struct drm_device *dev = crtc->base.dev;
3234 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003235 struct intel_crtc_state *pipe_config =
3236 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003237
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003238 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3239 crtc->base.mode = crtc->base.state->mode;
3240
3241 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3242 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3243 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003244
3245 /*
3246 * Update pipe size and adjust fitter if needed: the reason for this is
3247 * that in compute_mode_changes we check the native mode (not the pfit
3248 * mode) to see if we can flip rather than do a full mode set. In the
3249 * fastboot case, we'll flip, but if we don't update the pipesrc and
3250 * pfit state, we'll end up with a big fb scanned out into the wrong
3251 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003252 */
3253
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003254 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003255 ((pipe_config->pipe_src_w - 1) << 16) |
3256 (pipe_config->pipe_src_h - 1));
3257
3258 /* on skylake this is done by detaching scalers */
3259 if (INTEL_INFO(dev)->gen >= 9) {
3260 skl_detach_scalers(crtc);
3261
3262 if (pipe_config->pch_pfit.enabled)
3263 skylake_pfit_enable(crtc);
3264 } else if (HAS_PCH_SPLIT(dev)) {
3265 if (pipe_config->pch_pfit.enabled)
3266 ironlake_pfit_enable(crtc);
3267 else if (old_crtc_state->pch_pfit.enabled)
3268 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003269 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003270}
3271
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003272static void intel_fdi_normal_train(struct drm_crtc *crtc)
3273{
3274 struct drm_device *dev = crtc->dev;
3275 struct drm_i915_private *dev_priv = dev->dev_private;
3276 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3277 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003278 i915_reg_t reg;
3279 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003280
3281 /* enable normal train */
3282 reg = FDI_TX_CTL(pipe);
3283 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003284 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003285 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3286 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003287 } else {
3288 temp &= ~FDI_LINK_TRAIN_NONE;
3289 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003290 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003291 I915_WRITE(reg, temp);
3292
3293 reg = FDI_RX_CTL(pipe);
3294 temp = I915_READ(reg);
3295 if (HAS_PCH_CPT(dev)) {
3296 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3297 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3298 } else {
3299 temp &= ~FDI_LINK_TRAIN_NONE;
3300 temp |= FDI_LINK_TRAIN_NONE;
3301 }
3302 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3303
3304 /* wait one idle pattern time */
3305 POSTING_READ(reg);
3306 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003307
3308 /* IVB wants error correction enabled */
3309 if (IS_IVYBRIDGE(dev))
3310 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3311 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003312}
3313
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003314/* The FDI link training functions for ILK/Ibexpeak. */
3315static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3316{
3317 struct drm_device *dev = crtc->dev;
3318 struct drm_i915_private *dev_priv = dev->dev_private;
3319 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3320 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003321 i915_reg_t reg;
3322 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003323
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003324 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003325 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003326
Adam Jacksone1a44742010-06-25 15:32:14 -04003327 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3328 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003329 reg = FDI_RX_IMR(pipe);
3330 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003331 temp &= ~FDI_RX_SYMBOL_LOCK;
3332 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003333 I915_WRITE(reg, temp);
3334 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003335 udelay(150);
3336
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003337 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003338 reg = FDI_TX_CTL(pipe);
3339 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003340 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003341 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003342 temp &= ~FDI_LINK_TRAIN_NONE;
3343 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003344 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003345
Chris Wilson5eddb702010-09-11 13:48:45 +01003346 reg = FDI_RX_CTL(pipe);
3347 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003348 temp &= ~FDI_LINK_TRAIN_NONE;
3349 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003350 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3351
3352 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003353 udelay(150);
3354
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003355 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003356 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3357 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3358 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003359
Chris Wilson5eddb702010-09-11 13:48:45 +01003360 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003361 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003362 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003363 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3364
3365 if ((temp & FDI_RX_BIT_LOCK)) {
3366 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003367 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003368 break;
3369 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003370 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003371 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003372 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003373
3374 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003375 reg = FDI_TX_CTL(pipe);
3376 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003377 temp &= ~FDI_LINK_TRAIN_NONE;
3378 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003379 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003380
Chris Wilson5eddb702010-09-11 13:48:45 +01003381 reg = FDI_RX_CTL(pipe);
3382 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003383 temp &= ~FDI_LINK_TRAIN_NONE;
3384 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003385 I915_WRITE(reg, temp);
3386
3387 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003388 udelay(150);
3389
Chris Wilson5eddb702010-09-11 13:48:45 +01003390 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003391 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003392 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003393 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3394
3395 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003396 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003397 DRM_DEBUG_KMS("FDI train 2 done.\n");
3398 break;
3399 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003400 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003401 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003402 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003403
3404 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003405
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003406}
3407
Akshay Joshi0206e352011-08-16 15:34:10 -04003408static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003409 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3410 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3411 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3412 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3413};
3414
3415/* The FDI link training functions for SNB/Cougarpoint. */
3416static void gen6_fdi_link_train(struct drm_crtc *crtc)
3417{
3418 struct drm_device *dev = crtc->dev;
3419 struct drm_i915_private *dev_priv = dev->dev_private;
3420 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3421 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003422 i915_reg_t reg;
3423 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003424
Adam Jacksone1a44742010-06-25 15:32:14 -04003425 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3426 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003427 reg = FDI_RX_IMR(pipe);
3428 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003429 temp &= ~FDI_RX_SYMBOL_LOCK;
3430 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003431 I915_WRITE(reg, temp);
3432
3433 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003434 udelay(150);
3435
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003436 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003437 reg = FDI_TX_CTL(pipe);
3438 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003439 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003440 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003441 temp &= ~FDI_LINK_TRAIN_NONE;
3442 temp |= FDI_LINK_TRAIN_PATTERN_1;
3443 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3444 /* SNB-B */
3445 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003446 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003447
Daniel Vetterd74cf322012-10-26 10:58:13 +02003448 I915_WRITE(FDI_RX_MISC(pipe),
3449 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3450
Chris Wilson5eddb702010-09-11 13:48:45 +01003451 reg = FDI_RX_CTL(pipe);
3452 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003453 if (HAS_PCH_CPT(dev)) {
3454 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3455 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3456 } else {
3457 temp &= ~FDI_LINK_TRAIN_NONE;
3458 temp |= FDI_LINK_TRAIN_PATTERN_1;
3459 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003460 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3461
3462 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003463 udelay(150);
3464
Akshay Joshi0206e352011-08-16 15:34:10 -04003465 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003466 reg = FDI_TX_CTL(pipe);
3467 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003468 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3469 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003470 I915_WRITE(reg, temp);
3471
3472 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003473 udelay(500);
3474
Sean Paulfa37d392012-03-02 12:53:39 -05003475 for (retry = 0; retry < 5; retry++) {
3476 reg = FDI_RX_IIR(pipe);
3477 temp = I915_READ(reg);
3478 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3479 if (temp & FDI_RX_BIT_LOCK) {
3480 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3481 DRM_DEBUG_KMS("FDI train 1 done.\n");
3482 break;
3483 }
3484 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003485 }
Sean Paulfa37d392012-03-02 12:53:39 -05003486 if (retry < 5)
3487 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003488 }
3489 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003490 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003491
3492 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003493 reg = FDI_TX_CTL(pipe);
3494 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003495 temp &= ~FDI_LINK_TRAIN_NONE;
3496 temp |= FDI_LINK_TRAIN_PATTERN_2;
3497 if (IS_GEN6(dev)) {
3498 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3499 /* SNB-B */
3500 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3501 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003502 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003503
Chris Wilson5eddb702010-09-11 13:48:45 +01003504 reg = FDI_RX_CTL(pipe);
3505 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003506 if (HAS_PCH_CPT(dev)) {
3507 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3508 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3509 } else {
3510 temp &= ~FDI_LINK_TRAIN_NONE;
3511 temp |= FDI_LINK_TRAIN_PATTERN_2;
3512 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003513 I915_WRITE(reg, temp);
3514
3515 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003516 udelay(150);
3517
Akshay Joshi0206e352011-08-16 15:34:10 -04003518 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003519 reg = FDI_TX_CTL(pipe);
3520 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003521 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3522 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003523 I915_WRITE(reg, temp);
3524
3525 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003526 udelay(500);
3527
Sean Paulfa37d392012-03-02 12:53:39 -05003528 for (retry = 0; retry < 5; retry++) {
3529 reg = FDI_RX_IIR(pipe);
3530 temp = I915_READ(reg);
3531 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3532 if (temp & FDI_RX_SYMBOL_LOCK) {
3533 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3534 DRM_DEBUG_KMS("FDI train 2 done.\n");
3535 break;
3536 }
3537 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003538 }
Sean Paulfa37d392012-03-02 12:53:39 -05003539 if (retry < 5)
3540 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003541 }
3542 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003543 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003544
3545 DRM_DEBUG_KMS("FDI train done.\n");
3546}
3547
Jesse Barnes357555c2011-04-28 15:09:55 -07003548/* Manual link training for Ivy Bridge A0 parts */
3549static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3550{
3551 struct drm_device *dev = crtc->dev;
3552 struct drm_i915_private *dev_priv = dev->dev_private;
3553 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3554 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003555 i915_reg_t reg;
3556 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003557
3558 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3559 for train result */
3560 reg = FDI_RX_IMR(pipe);
3561 temp = I915_READ(reg);
3562 temp &= ~FDI_RX_SYMBOL_LOCK;
3563 temp &= ~FDI_RX_BIT_LOCK;
3564 I915_WRITE(reg, temp);
3565
3566 POSTING_READ(reg);
3567 udelay(150);
3568
Daniel Vetter01a415f2012-10-27 15:58:40 +02003569 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3570 I915_READ(FDI_RX_IIR(pipe)));
3571
Jesse Barnes139ccd32013-08-19 11:04:55 -07003572 /* Try each vswing and preemphasis setting twice before moving on */
3573 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3574 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003575 reg = FDI_TX_CTL(pipe);
3576 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003577 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3578 temp &= ~FDI_TX_ENABLE;
3579 I915_WRITE(reg, temp);
3580
3581 reg = FDI_RX_CTL(pipe);
3582 temp = I915_READ(reg);
3583 temp &= ~FDI_LINK_TRAIN_AUTO;
3584 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3585 temp &= ~FDI_RX_ENABLE;
3586 I915_WRITE(reg, temp);
3587
3588 /* enable CPU FDI TX and PCH FDI RX */
3589 reg = FDI_TX_CTL(pipe);
3590 temp = I915_READ(reg);
3591 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003592 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003593 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003594 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003595 temp |= snb_b_fdi_train_param[j/2];
3596 temp |= FDI_COMPOSITE_SYNC;
3597 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3598
3599 I915_WRITE(FDI_RX_MISC(pipe),
3600 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3601
3602 reg = FDI_RX_CTL(pipe);
3603 temp = I915_READ(reg);
3604 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3605 temp |= FDI_COMPOSITE_SYNC;
3606 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3607
3608 POSTING_READ(reg);
3609 udelay(1); /* should be 0.5us */
3610
3611 for (i = 0; i < 4; i++) {
3612 reg = FDI_RX_IIR(pipe);
3613 temp = I915_READ(reg);
3614 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3615
3616 if (temp & FDI_RX_BIT_LOCK ||
3617 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3618 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3619 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3620 i);
3621 break;
3622 }
3623 udelay(1); /* should be 0.5us */
3624 }
3625 if (i == 4) {
3626 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3627 continue;
3628 }
3629
3630 /* Train 2 */
3631 reg = FDI_TX_CTL(pipe);
3632 temp = I915_READ(reg);
3633 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3634 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3635 I915_WRITE(reg, temp);
3636
3637 reg = FDI_RX_CTL(pipe);
3638 temp = I915_READ(reg);
3639 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3640 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003641 I915_WRITE(reg, temp);
3642
3643 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003644 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003645
Jesse Barnes139ccd32013-08-19 11:04:55 -07003646 for (i = 0; i < 4; i++) {
3647 reg = FDI_RX_IIR(pipe);
3648 temp = I915_READ(reg);
3649 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003650
Jesse Barnes139ccd32013-08-19 11:04:55 -07003651 if (temp & FDI_RX_SYMBOL_LOCK ||
3652 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3653 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3654 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3655 i);
3656 goto train_done;
3657 }
3658 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003659 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003660 if (i == 4)
3661 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003662 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003663
Jesse Barnes139ccd32013-08-19 11:04:55 -07003664train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003665 DRM_DEBUG_KMS("FDI train done.\n");
3666}
3667
Daniel Vetter88cefb62012-08-12 19:27:14 +02003668static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003669{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003670 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003671 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003672 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003673 i915_reg_t reg;
3674 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003675
Jesse Barnes0e23b992010-09-10 11:10:00 -07003676 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003677 reg = FDI_RX_CTL(pipe);
3678 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003679 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003680 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003681 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003682 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3683
3684 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003685 udelay(200);
3686
3687 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003688 temp = I915_READ(reg);
3689 I915_WRITE(reg, temp | FDI_PCDCLK);
3690
3691 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003692 udelay(200);
3693
Paulo Zanoni20749732012-11-23 15:30:38 -02003694 /* Enable CPU FDI TX PLL, always on for Ironlake */
3695 reg = FDI_TX_CTL(pipe);
3696 temp = I915_READ(reg);
3697 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3698 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003699
Paulo Zanoni20749732012-11-23 15:30:38 -02003700 POSTING_READ(reg);
3701 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003702 }
3703}
3704
Daniel Vetter88cefb62012-08-12 19:27:14 +02003705static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3706{
3707 struct drm_device *dev = intel_crtc->base.dev;
3708 struct drm_i915_private *dev_priv = dev->dev_private;
3709 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003710 i915_reg_t reg;
3711 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003712
3713 /* Switch from PCDclk to Rawclk */
3714 reg = FDI_RX_CTL(pipe);
3715 temp = I915_READ(reg);
3716 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3717
3718 /* Disable CPU FDI TX PLL */
3719 reg = FDI_TX_CTL(pipe);
3720 temp = I915_READ(reg);
3721 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3722
3723 POSTING_READ(reg);
3724 udelay(100);
3725
3726 reg = FDI_RX_CTL(pipe);
3727 temp = I915_READ(reg);
3728 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3729
3730 /* Wait for the clocks to turn off. */
3731 POSTING_READ(reg);
3732 udelay(100);
3733}
3734
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003735static void ironlake_fdi_disable(struct drm_crtc *crtc)
3736{
3737 struct drm_device *dev = crtc->dev;
3738 struct drm_i915_private *dev_priv = dev->dev_private;
3739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3740 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003741 i915_reg_t reg;
3742 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003743
3744 /* disable CPU FDI tx and PCH FDI rx */
3745 reg = FDI_TX_CTL(pipe);
3746 temp = I915_READ(reg);
3747 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3748 POSTING_READ(reg);
3749
3750 reg = FDI_RX_CTL(pipe);
3751 temp = I915_READ(reg);
3752 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003753 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003754 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3755
3756 POSTING_READ(reg);
3757 udelay(100);
3758
3759 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003760 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003761 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003762
3763 /* still set train pattern 1 */
3764 reg = FDI_TX_CTL(pipe);
3765 temp = I915_READ(reg);
3766 temp &= ~FDI_LINK_TRAIN_NONE;
3767 temp |= FDI_LINK_TRAIN_PATTERN_1;
3768 I915_WRITE(reg, temp);
3769
3770 reg = FDI_RX_CTL(pipe);
3771 temp = I915_READ(reg);
3772 if (HAS_PCH_CPT(dev)) {
3773 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3774 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3775 } else {
3776 temp &= ~FDI_LINK_TRAIN_NONE;
3777 temp |= FDI_LINK_TRAIN_PATTERN_1;
3778 }
3779 /* BPC in FDI rx is consistent with that in PIPECONF */
3780 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003781 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003782 I915_WRITE(reg, temp);
3783
3784 POSTING_READ(reg);
3785 udelay(100);
3786}
3787
Chris Wilson5dce5b932014-01-20 10:17:36 +00003788bool intel_has_pending_fb_unpin(struct drm_device *dev)
3789{
3790 struct intel_crtc *crtc;
3791
3792 /* Note that we don't need to be called with mode_config.lock here
3793 * as our list of CRTC objects is static for the lifetime of the
3794 * device and so cannot disappear as we iterate. Similarly, we can
3795 * happily treat the predicates as racy, atomic checks as userspace
3796 * cannot claim and pin a new fb without at least acquring the
3797 * struct_mutex and so serialising with us.
3798 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003799 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003800 if (atomic_read(&crtc->unpin_work_count) == 0)
3801 continue;
3802
Daniel Vetter5a21b662016-05-24 17:13:53 +02003803 if (crtc->flip_work)
Chris Wilson5dce5b932014-01-20 10:17:36 +00003804 intel_wait_for_vblank(dev, crtc->pipe);
3805
3806 return true;
3807 }
3808
3809 return false;
3810}
3811
Daniel Vetter5a21b662016-05-24 17:13:53 +02003812static void page_flip_completed(struct intel_crtc *intel_crtc)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003813{
3814 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003815 struct intel_flip_work *work = intel_crtc->flip_work;
3816
3817 intel_crtc->flip_work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003818
3819 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07003820 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003821
3822 drm_crtc_vblank_put(&intel_crtc->base);
3823
Daniel Vetter5a21b662016-05-24 17:13:53 +02003824 wake_up_all(&dev_priv->pending_flip_queue);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +02003825 queue_work(dev_priv->wq, &work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003826
3827 trace_i915_flip_complete(intel_crtc->plane,
3828 work->pending_flip_obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003829}
3830
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003831static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003832{
Chris Wilson0f911282012-04-17 10:05:38 +01003833 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003834 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003835 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003836
Daniel Vetter2c10d572012-12-20 21:24:07 +01003837 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003838
3839 ret = wait_event_interruptible_timeout(
3840 dev_priv->pending_flip_queue,
3841 !intel_crtc_has_pending_flip(crtc),
3842 60*HZ);
3843
3844 if (ret < 0)
3845 return ret;
3846
Daniel Vetter5a21b662016-05-24 17:13:53 +02003847 if (ret == 0) {
3848 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3849 struct intel_flip_work *work;
3850
3851 spin_lock_irq(&dev->event_lock);
3852 work = intel_crtc->flip_work;
3853 if (work && !is_mmio_work(work)) {
3854 WARN_ONCE(1, "Removing stuck page flip\n");
3855 page_flip_completed(intel_crtc);
3856 }
3857 spin_unlock_irq(&dev->event_lock);
3858 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003859
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003860 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003861}
3862
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003863static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3864{
3865 u32 temp;
3866
3867 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3868
3869 mutex_lock(&dev_priv->sb_lock);
3870
3871 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3872 temp |= SBI_SSCCTL_DISABLE;
3873 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3874
3875 mutex_unlock(&dev_priv->sb_lock);
3876}
3877
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003878/* Program iCLKIP clock to the desired frequency */
3879static void lpt_program_iclkip(struct drm_crtc *crtc)
3880{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003881 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003882 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003883 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3884 u32 temp;
3885
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003886 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003887
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003888 /* The iCLK virtual clock root frequency is in MHz,
3889 * but the adjusted_mode->crtc_clock in in KHz. To get the
3890 * divisors, it is necessary to divide one by another, so we
3891 * convert the virtual clock precision to KHz here for higher
3892 * precision.
3893 */
3894 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003895 u32 iclk_virtual_root_freq = 172800 * 1000;
3896 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003897 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003898
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003899 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3900 clock << auxdiv);
3901 divsel = (desired_divisor / iclk_pi_range) - 2;
3902 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003903
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003904 /*
3905 * Near 20MHz is a corner case which is
3906 * out of range for the 7-bit divisor
3907 */
3908 if (divsel <= 0x7f)
3909 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003910 }
3911
3912 /* This should not happen with any sane values */
3913 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3914 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3915 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3916 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3917
3918 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003919 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003920 auxdiv,
3921 divsel,
3922 phasedir,
3923 phaseinc);
3924
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003925 mutex_lock(&dev_priv->sb_lock);
3926
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003927 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003928 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003929 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3930 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3931 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3932 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3933 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3934 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003935 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003936
3937 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003938 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003939 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3940 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003941 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003942
3943 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003944 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003945 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003946 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003947
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003948 mutex_unlock(&dev_priv->sb_lock);
3949
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003950 /* Wait for initialization time */
3951 udelay(24);
3952
3953 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3954}
3955
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003956int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3957{
3958 u32 divsel, phaseinc, auxdiv;
3959 u32 iclk_virtual_root_freq = 172800 * 1000;
3960 u32 iclk_pi_range = 64;
3961 u32 desired_divisor;
3962 u32 temp;
3963
3964 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3965 return 0;
3966
3967 mutex_lock(&dev_priv->sb_lock);
3968
3969 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3970 if (temp & SBI_SSCCTL_DISABLE) {
3971 mutex_unlock(&dev_priv->sb_lock);
3972 return 0;
3973 }
3974
3975 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3976 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3977 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3978 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3979 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3980
3981 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3982 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3983 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3984
3985 mutex_unlock(&dev_priv->sb_lock);
3986
3987 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3988
3989 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3990 desired_divisor << auxdiv);
3991}
3992
Daniel Vetter275f01b22013-05-03 11:49:47 +02003993static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3994 enum pipe pch_transcoder)
3995{
3996 struct drm_device *dev = crtc->base.dev;
3997 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003998 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003999
4000 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4001 I915_READ(HTOTAL(cpu_transcoder)));
4002 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4003 I915_READ(HBLANK(cpu_transcoder)));
4004 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4005 I915_READ(HSYNC(cpu_transcoder)));
4006
4007 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4008 I915_READ(VTOTAL(cpu_transcoder)));
4009 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4010 I915_READ(VBLANK(cpu_transcoder)));
4011 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4012 I915_READ(VSYNC(cpu_transcoder)));
4013 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4014 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4015}
4016
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004017static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004018{
4019 struct drm_i915_private *dev_priv = dev->dev_private;
4020 uint32_t temp;
4021
4022 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004023 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004024 return;
4025
4026 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4027 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4028
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004029 temp &= ~FDI_BC_BIFURCATION_SELECT;
4030 if (enable)
4031 temp |= FDI_BC_BIFURCATION_SELECT;
4032
4033 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004034 I915_WRITE(SOUTH_CHICKEN1, temp);
4035 POSTING_READ(SOUTH_CHICKEN1);
4036}
4037
4038static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4039{
4040 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004041
4042 switch (intel_crtc->pipe) {
4043 case PIPE_A:
4044 break;
4045 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004046 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004047 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004048 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004049 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004050
4051 break;
4052 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004053 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004054
4055 break;
4056 default:
4057 BUG();
4058 }
4059}
4060
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004061/* Return which DP Port should be selected for Transcoder DP control */
4062static enum port
4063intel_trans_dp_port_sel(struct drm_crtc *crtc)
4064{
4065 struct drm_device *dev = crtc->dev;
4066 struct intel_encoder *encoder;
4067
4068 for_each_encoder_on_crtc(dev, crtc, encoder) {
4069 if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
4070 encoder->type == INTEL_OUTPUT_EDP)
4071 return enc_to_dig_port(&encoder->base)->port;
4072 }
4073
4074 return -1;
4075}
4076
Jesse Barnesf67a5592011-01-05 10:31:48 -08004077/*
4078 * Enable PCH resources required for PCH ports:
4079 * - PCH PLLs
4080 * - FDI training & RX/TX
4081 * - update transcoder timings
4082 * - DP transcoding bits
4083 * - transcoder
4084 */
4085static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004086{
4087 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004088 struct drm_i915_private *dev_priv = dev->dev_private;
4089 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4090 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004091 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004092
Daniel Vetterab9412b2013-05-03 11:49:46 +02004093 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004094
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004095 if (IS_IVYBRIDGE(dev))
4096 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4097
Daniel Vettercd986ab2012-10-26 10:58:12 +02004098 /* Write the TU size bits before fdi link training, so that error
4099 * detection works. */
4100 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4101 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4102
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004103 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004104 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004105
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004106 /* We need to program the right clock selection before writing the pixel
4107 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004108 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004109 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004110
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004111 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004112 temp |= TRANS_DPLL_ENABLE(pipe);
4113 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004114 if (intel_crtc->config->shared_dpll ==
4115 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004116 temp |= sel;
4117 else
4118 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004119 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004120 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004121
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004122 /* XXX: pch pll's can be enabled any time before we enable the PCH
4123 * transcoder, and we actually should do this to not upset any PCH
4124 * transcoder that already use the clock when we share it.
4125 *
4126 * Note that enable_shared_dpll tries to do the right thing, but
4127 * get_shared_dpll unconditionally resets the pll - we need that to have
4128 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004129 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004130
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004131 /* set transcoder timing, panel must allow it */
4132 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004133 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004134
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004135 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004136
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004137 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004138 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004139 const struct drm_display_mode *adjusted_mode =
4140 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004141 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004142 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004143 temp = I915_READ(reg);
4144 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004145 TRANS_DP_SYNC_MASK |
4146 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004147 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004148 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004149
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004150 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004151 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004152 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004153 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004154
4155 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004156 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004157 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004158 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004159 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004160 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004161 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004162 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004163 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004164 break;
4165 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004166 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004167 }
4168
Chris Wilson5eddb702010-09-11 13:48:45 +01004169 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004170 }
4171
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004172 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004173}
4174
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004175static void lpt_pch_enable(struct drm_crtc *crtc)
4176{
4177 struct drm_device *dev = crtc->dev;
4178 struct drm_i915_private *dev_priv = dev->dev_private;
4179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004180 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004181
Daniel Vetterab9412b2013-05-03 11:49:46 +02004182 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004183
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004184 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004185
Paulo Zanoni0540e482012-10-31 18:12:40 -02004186 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004187 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004188
Paulo Zanoni937bb612012-10-31 18:12:47 -02004189 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004190}
4191
Daniel Vettera1520312013-05-03 11:49:50 +02004192static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004193{
4194 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004195 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004196 u32 temp;
4197
4198 temp = I915_READ(dslreg);
4199 udelay(500);
4200 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004201 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004202 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004203 }
4204}
4205
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004206static int
4207skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4208 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4209 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004210{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004211 struct intel_crtc_scaler_state *scaler_state =
4212 &crtc_state->scaler_state;
4213 struct intel_crtc *intel_crtc =
4214 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004215 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004216
4217 need_scaling = intel_rotation_90_or_270(rotation) ?
4218 (src_h != dst_w || src_w != dst_h):
4219 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004220
4221 /*
4222 * if plane is being disabled or scaler is no more required or force detach
4223 * - free scaler binded to this plane/crtc
4224 * - in order to do this, update crtc->scaler_usage
4225 *
4226 * Here scaler state in crtc_state is set free so that
4227 * scaler can be assigned to other user. Actual register
4228 * update to free the scaler is done in plane/panel-fit programming.
4229 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4230 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004231 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004232 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004233 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004234 scaler_state->scalers[*scaler_id].in_use = 0;
4235
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004236 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4237 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4238 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004239 scaler_state->scaler_users);
4240 *scaler_id = -1;
4241 }
4242 return 0;
4243 }
4244
4245 /* range checks */
4246 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4247 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4248
4249 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4250 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004251 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004252 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004253 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004254 return -EINVAL;
4255 }
4256
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004257 /* mark this plane as a scaler user in crtc_state */
4258 scaler_state->scaler_users |= (1 << scaler_user);
4259 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4260 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4261 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4262 scaler_state->scaler_users);
4263
4264 return 0;
4265}
4266
4267/**
4268 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4269 *
4270 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004271 *
4272 * Return
4273 * 0 - scaler_usage updated successfully
4274 * error - requested scaling cannot be supported or other error condition
4275 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004276int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004277{
4278 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004279 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004280
Ville Syrjälä78108b72016-05-27 20:59:19 +03004281 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4282 intel_crtc->base.base.id, intel_crtc->base.name,
4283 intel_crtc->pipe, SKL_CRTC_INDEX);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004284
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004285 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004286 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004287 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004288 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004289}
4290
4291/**
4292 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4293 *
4294 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004295 * @plane_state: atomic plane state to update
4296 *
4297 * Return
4298 * 0 - scaler_usage updated successfully
4299 * error - requested scaling cannot be supported or other error condition
4300 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004301static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4302 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004303{
4304
4305 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004306 struct intel_plane *intel_plane =
4307 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004308 struct drm_framebuffer *fb = plane_state->base.fb;
4309 int ret;
4310
4311 bool force_detach = !fb || !plane_state->visible;
4312
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004313 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4314 intel_plane->base.base.id, intel_plane->base.name,
4315 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004316
4317 ret = skl_update_scaler(crtc_state, force_detach,
4318 drm_plane_index(&intel_plane->base),
4319 &plane_state->scaler_id,
4320 plane_state->base.rotation,
4321 drm_rect_width(&plane_state->src) >> 16,
4322 drm_rect_height(&plane_state->src) >> 16,
4323 drm_rect_width(&plane_state->dst),
4324 drm_rect_height(&plane_state->dst));
4325
4326 if (ret || plane_state->scaler_id < 0)
4327 return ret;
4328
Chandra Kondurua1b22782015-04-07 15:28:45 -07004329 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004330 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004331 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4332 intel_plane->base.base.id,
4333 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004334 return -EINVAL;
4335 }
4336
4337 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004338 switch (fb->pixel_format) {
4339 case DRM_FORMAT_RGB565:
4340 case DRM_FORMAT_XBGR8888:
4341 case DRM_FORMAT_XRGB8888:
4342 case DRM_FORMAT_ABGR8888:
4343 case DRM_FORMAT_ARGB8888:
4344 case DRM_FORMAT_XRGB2101010:
4345 case DRM_FORMAT_XBGR2101010:
4346 case DRM_FORMAT_YUYV:
4347 case DRM_FORMAT_YVYU:
4348 case DRM_FORMAT_UYVY:
4349 case DRM_FORMAT_VYUY:
4350 break;
4351 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004352 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4353 intel_plane->base.base.id, intel_plane->base.name,
4354 fb->base.id, fb->pixel_format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004355 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004356 }
4357
Chandra Kondurua1b22782015-04-07 15:28:45 -07004358 return 0;
4359}
4360
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004361static void skylake_scaler_disable(struct intel_crtc *crtc)
4362{
4363 int i;
4364
4365 for (i = 0; i < crtc->num_scalers; i++)
4366 skl_detach_scaler(crtc, i);
4367}
4368
4369static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004370{
4371 struct drm_device *dev = crtc->base.dev;
4372 struct drm_i915_private *dev_priv = dev->dev_private;
4373 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004374 struct intel_crtc_scaler_state *scaler_state =
4375 &crtc->config->scaler_state;
4376
4377 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4378
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004379 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004380 int id;
4381
4382 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4383 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4384 return;
4385 }
4386
4387 id = scaler_state->scaler_id;
4388 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4389 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4390 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4391 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4392
4393 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004394 }
4395}
4396
Jesse Barnesb074cec2013-04-25 12:55:02 -07004397static void ironlake_pfit_enable(struct intel_crtc *crtc)
4398{
4399 struct drm_device *dev = crtc->base.dev;
4400 struct drm_i915_private *dev_priv = dev->dev_private;
4401 int pipe = crtc->pipe;
4402
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004403 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004404 /* Force use of hard-coded filter coefficients
4405 * as some pre-programmed values are broken,
4406 * e.g. x201.
4407 */
4408 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4409 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4410 PF_PIPE_SEL_IVB(pipe));
4411 else
4412 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004413 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4414 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004415 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004416}
4417
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004418void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004419{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004420 struct drm_device *dev = crtc->base.dev;
4421 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004422
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004423 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004424 return;
4425
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004426 /*
4427 * We can only enable IPS after we enable a plane and wait for a vblank
4428 * This function is called from post_plane_update, which is run after
4429 * a vblank wait.
4430 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004431
Paulo Zanonid77e4532013-09-24 13:52:55 -03004432 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004433 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004434 mutex_lock(&dev_priv->rps.hw_lock);
4435 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4436 mutex_unlock(&dev_priv->rps.hw_lock);
4437 /* Quoting Art Runyan: "its not safe to expect any particular
4438 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004439 * mailbox." Moreover, the mailbox may return a bogus state,
4440 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004441 */
4442 } else {
4443 I915_WRITE(IPS_CTL, IPS_ENABLE);
4444 /* The bit only becomes 1 in the next vblank, so this wait here
4445 * is essentially intel_wait_for_vblank. If we don't have this
4446 * and don't wait for vblanks until the end of crtc_enable, then
4447 * the HW state readout code will complain that the expected
4448 * IPS_CTL value is not the one we read. */
4449 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4450 DRM_ERROR("Timed out waiting for IPS enable\n");
4451 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004452}
4453
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004454void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004455{
4456 struct drm_device *dev = crtc->base.dev;
4457 struct drm_i915_private *dev_priv = dev->dev_private;
4458
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004459 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004460 return;
4461
4462 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004463 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004464 mutex_lock(&dev_priv->rps.hw_lock);
4465 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4466 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004467 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4468 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4469 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004470 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004471 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004472 POSTING_READ(IPS_CTL);
4473 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004474
4475 /* We need to wait for a vblank before we can disable the plane. */
4476 intel_wait_for_vblank(dev, crtc->pipe);
4477}
4478
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004479static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004480{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004481 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004482 struct drm_device *dev = intel_crtc->base.dev;
4483 struct drm_i915_private *dev_priv = dev->dev_private;
4484
4485 mutex_lock(&dev->struct_mutex);
4486 dev_priv->mm.interruptible = false;
4487 (void) intel_overlay_switch_off(intel_crtc->overlay);
4488 dev_priv->mm.interruptible = true;
4489 mutex_unlock(&dev->struct_mutex);
4490 }
4491
4492 /* Let userspace switch the overlay on again. In most cases userspace
4493 * has to recompute where to put it anyway.
4494 */
4495}
4496
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004497/**
4498 * intel_post_enable_primary - Perform operations after enabling primary plane
4499 * @crtc: the CRTC whose primary plane was just enabled
4500 *
4501 * Performs potentially sleeping operations that must be done after the primary
4502 * plane is enabled, such as updating FBC and IPS. Note that this may be
4503 * called due to an explicit primary plane update, or due to an implicit
4504 * re-enable that is caused when a sprite plane is updated to no longer
4505 * completely hide the primary plane.
4506 */
4507static void
4508intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004509{
4510 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004511 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004512 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4513 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004514
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004515 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004516 * FIXME IPS should be fine as long as one plane is
4517 * enabled, but in practice it seems to have problems
4518 * when going from primary only to sprite only and vice
4519 * versa.
4520 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004521 hsw_enable_ips(intel_crtc);
4522
Daniel Vetterf99d7062014-06-19 16:01:59 +02004523 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004524 * Gen2 reports pipe underruns whenever all planes are disabled.
4525 * So don't enable underrun reporting before at least some planes
4526 * are enabled.
4527 * FIXME: Need to fix the logic to work when we turn off all planes
4528 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004529 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004530 if (IS_GEN2(dev))
4531 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4532
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004533 /* Underruns don't always raise interrupts, so check manually. */
4534 intel_check_cpu_fifo_underruns(dev_priv);
4535 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004536}
4537
Ville Syrjälä2622a082016-03-09 19:07:26 +02004538/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004539static void
4540intel_pre_disable_primary(struct drm_crtc *crtc)
4541{
4542 struct drm_device *dev = crtc->dev;
4543 struct drm_i915_private *dev_priv = dev->dev_private;
4544 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4545 int pipe = intel_crtc->pipe;
4546
4547 /*
4548 * Gen2 reports pipe underruns whenever all planes are disabled.
4549 * So diasble underrun reporting before all the planes get disabled.
4550 * FIXME: Need to fix the logic to work when we turn off all planes
4551 * but leave the pipe running.
4552 */
4553 if (IS_GEN2(dev))
4554 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4555
4556 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004557 * FIXME IPS should be fine as long as one plane is
4558 * enabled, but in practice it seems to have problems
4559 * when going from primary only to sprite only and vice
4560 * versa.
4561 */
4562 hsw_disable_ips(intel_crtc);
4563}
4564
4565/* FIXME get rid of this and use pre_plane_update */
4566static void
4567intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4568{
4569 struct drm_device *dev = crtc->dev;
4570 struct drm_i915_private *dev_priv = dev->dev_private;
4571 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4572 int pipe = intel_crtc->pipe;
4573
4574 intel_pre_disable_primary(crtc);
4575
4576 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004577 * Vblank time updates from the shadow to live plane control register
4578 * are blocked if the memory self-refresh mode is active at that
4579 * moment. So to make sure the plane gets truly disabled, disable
4580 * first the self-refresh mode. The self-refresh enable bit in turn
4581 * will be checked/applied by the HW only at the next frame start
4582 * event which is after the vblank start event, so we need to have a
4583 * wait-for-vblank between disabling the plane and the pipe.
4584 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004585 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004586 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004587 dev_priv->wm.vlv.cxsr = false;
4588 intel_wait_for_vblank(dev, pipe);
4589 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004590}
4591
Daniel Vetter5a21b662016-05-24 17:13:53 +02004592static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4593{
4594 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4595 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4596 struct intel_crtc_state *pipe_config =
4597 to_intel_crtc_state(crtc->base.state);
4598 struct drm_device *dev = crtc->base.dev;
4599 struct drm_plane *primary = crtc->base.primary;
4600 struct drm_plane_state *old_pri_state =
4601 drm_atomic_get_existing_plane_state(old_state, primary);
4602
4603 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
4604
4605 crtc->wm.cxsr_allowed = true;
4606
4607 if (pipe_config->update_wm_post && pipe_config->base.active)
4608 intel_update_watermarks(&crtc->base);
4609
4610 if (old_pri_state) {
4611 struct intel_plane_state *primary_state =
4612 to_intel_plane_state(primary->state);
4613 struct intel_plane_state *old_primary_state =
4614 to_intel_plane_state(old_pri_state);
4615
4616 intel_fbc_post_update(crtc);
4617
4618 if (primary_state->visible &&
4619 (needs_modeset(&pipe_config->base) ||
4620 !old_primary_state->visible))
4621 intel_post_enable_primary(&crtc->base);
4622 }
4623}
4624
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004625static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004626{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004627 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004628 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004629 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004630 struct intel_crtc_state *pipe_config =
4631 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004632 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4633 struct drm_plane *primary = crtc->base.primary;
4634 struct drm_plane_state *old_pri_state =
4635 drm_atomic_get_existing_plane_state(old_state, primary);
4636 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004637
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004638 if (old_pri_state) {
4639 struct intel_plane_state *primary_state =
4640 to_intel_plane_state(primary->state);
4641 struct intel_plane_state *old_primary_state =
4642 to_intel_plane_state(old_pri_state);
4643
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +02004644 intel_fbc_pre_update(crtc, pipe_config, primary_state);
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004645
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004646 if (old_primary_state->visible &&
4647 (modeset || !primary_state->visible))
4648 intel_pre_disable_primary(&crtc->base);
4649 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004650
David Weinehalla4015f92016-05-19 15:50:36 +03004651 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004652 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004653
Ville Syrjälä2622a082016-03-09 19:07:26 +02004654 /*
4655 * Vblank time updates from the shadow to live plane control register
4656 * are blocked if the memory self-refresh mode is active at that
4657 * moment. So to make sure the plane gets truly disabled, disable
4658 * first the self-refresh mode. The self-refresh enable bit in turn
4659 * will be checked/applied by the HW only at the next frame start
4660 * event which is after the vblank start event, so we need to have a
4661 * wait-for-vblank between disabling the plane and the pipe.
4662 */
4663 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004664 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004665 dev_priv->wm.vlv.cxsr = false;
4666 intel_wait_for_vblank(dev, crtc->pipe);
4667 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004668 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004669
Matt Ropered4a6a72016-02-23 17:20:13 -08004670 /*
4671 * IVB workaround: must disable low power watermarks for at least
4672 * one frame before enabling scaling. LP watermarks can be re-enabled
4673 * when scaling is disabled.
4674 *
4675 * WaCxSRDisabledForSpriteScaling:ivb
4676 */
4677 if (pipe_config->disable_lp_wm) {
4678 ilk_disable_lp_wm(dev);
4679 intel_wait_for_vblank(dev, crtc->pipe);
4680 }
4681
4682 /*
4683 * If we're doing a modeset, we're done. No need to do any pre-vblank
4684 * watermark programming here.
4685 */
4686 if (needs_modeset(&pipe_config->base))
4687 return;
4688
4689 /*
4690 * For platforms that support atomic watermarks, program the
4691 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4692 * will be the intermediate values that are safe for both pre- and
4693 * post- vblank; when vblank happens, the 'active' values will be set
4694 * to the final 'target' values and we'll do this again to get the
4695 * optimal watermarks. For gen9+ platforms, the values we program here
4696 * will be the final target values which will get automatically latched
4697 * at vblank time; no further programming will be necessary.
4698 *
4699 * If a platform hasn't been transitioned to atomic watermarks yet,
4700 * we'll continue to update watermarks the old way, if flags tell
4701 * us to.
4702 */
4703 if (dev_priv->display.initial_watermarks != NULL)
4704 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004705 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004706 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004707}
4708
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004709static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004710{
4711 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004712 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004713 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004714 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004715
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004716 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004717
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004718 drm_for_each_plane_mask(p, dev, plane_mask)
4719 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004720
Daniel Vetterf99d7062014-06-19 16:01:59 +02004721 /*
4722 * FIXME: Once we grow proper nuclear flip support out of this we need
4723 * to compute the mask of flip planes precisely. For the time being
4724 * consider this a flip to a NULL plane.
4725 */
4726 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004727}
4728
Jesse Barnesf67a5592011-01-05 10:31:48 -08004729static void ironlake_crtc_enable(struct drm_crtc *crtc)
4730{
4731 struct drm_device *dev = crtc->dev;
4732 struct drm_i915_private *dev_priv = dev->dev_private;
4733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004734 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004735 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004736 struct intel_crtc_state *pipe_config =
4737 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004738
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004739 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004740 return;
4741
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004742 /*
4743 * Sometimes spurious CPU pipe underruns happen during FDI
4744 * training, at least with VGA+HDMI cloning. Suppress them.
4745 *
4746 * On ILK we get an occasional spurious CPU pipe underruns
4747 * between eDP port A enable and vdd enable. Also PCH port
4748 * enable seems to result in the occasional CPU pipe underrun.
4749 *
4750 * Spurious PCH underruns also occur during PCH enabling.
4751 */
4752 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4753 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004754 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004755 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4756
4757 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004758 intel_prepare_shared_dpll(intel_crtc);
4759
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004760 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304761 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004762
4763 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004764 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004765
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004766 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004767 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004768 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004769 }
4770
4771 ironlake_set_pipeconf(crtc);
4772
Jesse Barnesf67a5592011-01-05 10:31:48 -08004773 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004774
Daniel Vetterf6736a12013-06-05 13:34:30 +02004775 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004776 if (encoder->pre_enable)
4777 encoder->pre_enable(encoder);
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) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004780 /* Note: FDI PLL enabling _must_ be done before we enable the
4781 * cpu pipes, hence this is separate from all the other fdi/pch
4782 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004783 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004784 } else {
4785 assert_fdi_tx_disabled(dev_priv, pipe);
4786 assert_fdi_rx_disabled(dev_priv, pipe);
4787 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004788
Jesse Barnesb074cec2013-04-25 12:55:02 -07004789 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004790
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004791 /*
4792 * On ILK+ LUT must be loaded before the pipe is running but with
4793 * clocks enabled
4794 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004795 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004796
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004797 if (dev_priv->display.initial_watermarks != NULL)
4798 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004799 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004800
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004801 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004802 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004803
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004804 assert_vblank_disabled(crtc);
4805 drm_crtc_vblank_on(crtc);
4806
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004807 for_each_encoder_on_crtc(dev, crtc, encoder)
4808 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004809
4810 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004811 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004812
4813 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4814 if (intel_crtc->config->has_pch_encoder)
4815 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004816 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004817 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004818}
4819
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004820/* IPS only exists on ULT machines and is tied to pipe A. */
4821static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4822{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004823 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004824}
4825
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004826static void haswell_crtc_enable(struct drm_crtc *crtc)
4827{
4828 struct drm_device *dev = crtc->dev;
4829 struct drm_i915_private *dev_priv = dev->dev_private;
4830 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4831 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004832 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004833 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004834 struct intel_crtc_state *pipe_config =
4835 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004836
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004837 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004838 return;
4839
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004840 if (intel_crtc->config->has_pch_encoder)
4841 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4842 false);
4843
Imre Deak95a7a2a2016-06-13 16:44:35 +03004844 for_each_encoder_on_crtc(dev, crtc, encoder)
4845 if (encoder->pre_pll_enable)
4846 encoder->pre_pll_enable(encoder);
4847
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004848 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004849 intel_enable_shared_dpll(intel_crtc);
4850
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004851 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304852 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004853
Jani Nikula4d1de972016-03-18 17:05:42 +02004854 if (!intel_crtc->config->has_dsi_encoder)
4855 intel_set_pipe_timings(intel_crtc);
4856
Jani Nikulabc58be62016-03-18 17:05:39 +02004857 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004858
Jani Nikula4d1de972016-03-18 17:05:42 +02004859 if (cpu_transcoder != TRANSCODER_EDP &&
4860 !transcoder_is_dsi(cpu_transcoder)) {
4861 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004862 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004863 }
4864
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004865 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004866 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004867 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004868 }
4869
Jani Nikula4d1de972016-03-18 17:05:42 +02004870 if (!intel_crtc->config->has_dsi_encoder)
4871 haswell_set_pipeconf(crtc);
4872
Jani Nikula391bf042016-03-18 17:05:40 +02004873 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004874
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004875 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02004876
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004877 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004878
Daniel Vetter6b698512015-11-28 11:05:39 +01004879 if (intel_crtc->config->has_pch_encoder)
4880 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4881 else
4882 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4883
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304884 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004885 if (encoder->pre_enable)
4886 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304887 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004888
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004889 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004890 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004891
Jani Nikulaa65347b2015-11-27 12:21:46 +02004892 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304893 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004894
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004895 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004896 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004897 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004898 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004899
4900 /*
4901 * On ILK+ LUT must be loaded before the pipe is running but with
4902 * clocks enabled
4903 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004904 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004905
Paulo Zanoni1f544382012-10-24 11:32:00 -02004906 intel_ddi_set_pipe_settings(crtc);
Jani Nikulaa65347b2015-11-27 12:21:46 +02004907 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304908 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004909
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004910 if (dev_priv->display.initial_watermarks != NULL)
4911 dev_priv->display.initial_watermarks(pipe_config);
4912 else
4913 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004914
4915 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
4916 if (!intel_crtc->config->has_dsi_encoder)
4917 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004918
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004919 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004920 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004921
Jani Nikulaa65347b2015-11-27 12:21:46 +02004922 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004923 intel_ddi_set_vc_payload_alloc(crtc, true);
4924
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004925 assert_vblank_disabled(crtc);
4926 drm_crtc_vblank_on(crtc);
4927
Jani Nikula8807e552013-08-30 19:40:32 +03004928 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004929 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004930 intel_opregion_notify_encoder(encoder, true);
4931 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004932
Daniel Vetter6b698512015-11-28 11:05:39 +01004933 if (intel_crtc->config->has_pch_encoder) {
4934 intel_wait_for_vblank(dev, pipe);
4935 intel_wait_for_vblank(dev, pipe);
4936 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004937 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4938 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004939 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004940
Paulo Zanonie4916942013-09-20 16:21:19 -03004941 /* If we change the relative order between pipe/planes enabling, we need
4942 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004943 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4944 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4945 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4946 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4947 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004948}
4949
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004950static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004951{
4952 struct drm_device *dev = crtc->base.dev;
4953 struct drm_i915_private *dev_priv = dev->dev_private;
4954 int pipe = crtc->pipe;
4955
4956 /* To avoid upsetting the power well on haswell only disable the pfit if
4957 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004958 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004959 I915_WRITE(PF_CTL(pipe), 0);
4960 I915_WRITE(PF_WIN_POS(pipe), 0);
4961 I915_WRITE(PF_WIN_SZ(pipe), 0);
4962 }
4963}
4964
Jesse Barnes6be4a602010-09-10 10:26:01 -07004965static void ironlake_crtc_disable(struct drm_crtc *crtc)
4966{
4967 struct drm_device *dev = crtc->dev;
4968 struct drm_i915_private *dev_priv = dev->dev_private;
4969 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004970 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004971 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004972
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004973 /*
4974 * Sometimes spurious CPU pipe underruns happen when the
4975 * pipe is already disabled, but FDI RX/TX is still enabled.
4976 * Happens at least with VGA+HDMI cloning. Suppress them.
4977 */
4978 if (intel_crtc->config->has_pch_encoder) {
4979 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004980 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004981 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004982
Daniel Vetterea9d7582012-07-10 10:42:52 +02004983 for_each_encoder_on_crtc(dev, crtc, encoder)
4984 encoder->disable(encoder);
4985
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004986 drm_crtc_vblank_off(crtc);
4987 assert_vblank_disabled(crtc);
4988
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004989 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004990
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004991 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004992
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004993 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004994 ironlake_fdi_disable(crtc);
4995
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004996 for_each_encoder_on_crtc(dev, crtc, encoder)
4997 if (encoder->post_disable)
4998 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004999
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005000 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005001 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005002
Daniel Vetterd925c592013-06-05 13:34:04 +02005003 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02005004 i915_reg_t reg;
5005 u32 temp;
5006
Daniel Vetterd925c592013-06-05 13:34:04 +02005007 /* disable TRANS_DP_CTL */
5008 reg = TRANS_DP_CTL(pipe);
5009 temp = I915_READ(reg);
5010 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5011 TRANS_DP_PORT_SEL_MASK);
5012 temp |= TRANS_DP_PORT_SEL_NONE;
5013 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005014
Daniel Vetterd925c592013-06-05 13:34:04 +02005015 /* disable DPLL_SEL */
5016 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005017 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005018 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005019 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005020
Daniel Vetterd925c592013-06-05 13:34:04 +02005021 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005022 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005023
Ville Syrjäläb2c05932016-04-01 21:53:17 +03005024 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005025 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005026}
5027
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005028static void haswell_crtc_disable(struct drm_crtc *crtc)
5029{
5030 struct drm_device *dev = crtc->dev;
5031 struct drm_i915_private *dev_priv = dev->dev_private;
5032 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5033 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005034 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005035
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005036 if (intel_crtc->config->has_pch_encoder)
5037 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5038 false);
5039
Jani Nikula8807e552013-08-30 19:40:32 +03005040 for_each_encoder_on_crtc(dev, crtc, encoder) {
5041 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005042 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005043 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005044
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005045 drm_crtc_vblank_off(crtc);
5046 assert_vblank_disabled(crtc);
5047
Jani Nikula4d1de972016-03-18 17:05:42 +02005048 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
5049 if (!intel_crtc->config->has_dsi_encoder)
5050 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005051
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005052 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005053 intel_ddi_set_vc_payload_alloc(crtc, false);
5054
Jani Nikulaa65347b2015-11-27 12:21:46 +02005055 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305056 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005057
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005058 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005059 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005060 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005061 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005062
Jani Nikulaa65347b2015-11-27 12:21:46 +02005063 if (!intel_crtc->config->has_dsi_encoder)
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305064 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005065
Imre Deak97b040a2014-06-25 22:01:50 +03005066 for_each_encoder_on_crtc(dev, crtc, encoder)
5067 if (encoder->post_disable)
5068 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005069
Ville Syrjälä92966a32015-12-08 16:05:48 +02005070 if (intel_crtc->config->has_pch_encoder) {
5071 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005072 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005073 intel_ddi_fdi_disable(crtc);
5074
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005075 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5076 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005077 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005078}
5079
Jesse Barnes2dd24552013-04-25 12:55:01 -07005080static void i9xx_pfit_enable(struct intel_crtc *crtc)
5081{
5082 struct drm_device *dev = crtc->base.dev;
5083 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005084 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005085
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005086 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005087 return;
5088
Daniel Vetterc0b03412013-05-28 12:05:54 +02005089 /*
5090 * The panel fitter should only be adjusted whilst the pipe is disabled,
5091 * according to register description and PRM.
5092 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005093 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5094 assert_pipe_disabled(dev_priv, crtc->pipe);
5095
Jesse Barnesb074cec2013-04-25 12:55:02 -07005096 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5097 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005098
5099 /* Border color in case we don't scale up to the full screen. Black by
5100 * default, change to something else for debugging. */
5101 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005102}
5103
Dave Airlied05410f2014-06-05 13:22:59 +10005104static enum intel_display_power_domain port_to_power_domain(enum port port)
5105{
5106 switch (port) {
5107 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005108 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005109 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005110 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005111 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005112 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005113 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005114 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005115 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005116 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005117 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005118 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005119 return POWER_DOMAIN_PORT_OTHER;
5120 }
5121}
5122
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005123static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5124{
5125 switch (port) {
5126 case PORT_A:
5127 return POWER_DOMAIN_AUX_A;
5128 case PORT_B:
5129 return POWER_DOMAIN_AUX_B;
5130 case PORT_C:
5131 return POWER_DOMAIN_AUX_C;
5132 case PORT_D:
5133 return POWER_DOMAIN_AUX_D;
5134 case PORT_E:
5135 /* FIXME: Check VBT for actual wiring of PORT E */
5136 return POWER_DOMAIN_AUX_D;
5137 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005138 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005139 return POWER_DOMAIN_AUX_A;
5140 }
5141}
5142
Imre Deak319be8a2014-03-04 19:22:57 +02005143enum intel_display_power_domain
5144intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005145{
Imre Deak319be8a2014-03-04 19:22:57 +02005146 struct drm_device *dev = intel_encoder->base.dev;
5147 struct intel_digital_port *intel_dig_port;
5148
5149 switch (intel_encoder->type) {
5150 case INTEL_OUTPUT_UNKNOWN:
5151 /* Only DDI platforms should ever use this output type */
5152 WARN_ON_ONCE(!HAS_DDI(dev));
5153 case INTEL_OUTPUT_DISPLAYPORT:
5154 case INTEL_OUTPUT_HDMI:
5155 case INTEL_OUTPUT_EDP:
5156 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005157 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005158 case INTEL_OUTPUT_DP_MST:
5159 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5160 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005161 case INTEL_OUTPUT_ANALOG:
5162 return POWER_DOMAIN_PORT_CRT;
5163 case INTEL_OUTPUT_DSI:
5164 return POWER_DOMAIN_PORT_DSI;
5165 default:
5166 return POWER_DOMAIN_PORT_OTHER;
5167 }
5168}
5169
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005170enum intel_display_power_domain
5171intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5172{
5173 struct drm_device *dev = intel_encoder->base.dev;
5174 struct intel_digital_port *intel_dig_port;
5175
5176 switch (intel_encoder->type) {
5177 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005178 case INTEL_OUTPUT_HDMI:
5179 /*
5180 * Only DDI platforms should ever use these output types.
5181 * We can get here after the HDMI detect code has already set
5182 * the type of the shared encoder. Since we can't be sure
5183 * what's the status of the given connectors, play safe and
5184 * run the DP detection too.
5185 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005186 WARN_ON_ONCE(!HAS_DDI(dev));
5187 case INTEL_OUTPUT_DISPLAYPORT:
5188 case INTEL_OUTPUT_EDP:
5189 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5190 return port_to_aux_power_domain(intel_dig_port->port);
5191 case INTEL_OUTPUT_DP_MST:
5192 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5193 return port_to_aux_power_domain(intel_dig_port->port);
5194 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005195 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005196 return POWER_DOMAIN_AUX_A;
5197 }
5198}
5199
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005200static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5201 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005202{
5203 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005204 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005205 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5206 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005207 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005208 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005209
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005210 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005211 return 0;
5212
Imre Deak77d22dc2014-03-05 16:20:52 +02005213 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5214 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005215 if (crtc_state->pch_pfit.enabled ||
5216 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005217 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5218
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005219 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5220 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5221
Imre Deak319be8a2014-03-04 19:22:57 +02005222 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005223 }
Imre Deak319be8a2014-03-04 19:22:57 +02005224
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005225 if (crtc_state->shared_dpll)
5226 mask |= BIT(POWER_DOMAIN_PLLS);
5227
Imre Deak77d22dc2014-03-05 16:20:52 +02005228 return mask;
5229}
5230
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005231static unsigned long
5232modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5233 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005234{
5235 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5237 enum intel_display_power_domain domain;
Daniel Vetter5a21b662016-05-24 17:13:53 +02005238 unsigned long domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005239
5240 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005241 intel_crtc->enabled_power_domains = new_domains =
5242 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005243
Daniel Vetter5a21b662016-05-24 17:13:53 +02005244 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005245
5246 for_each_power_domain(domain, domains)
5247 intel_display_power_get(dev_priv, domain);
5248
Daniel Vetter5a21b662016-05-24 17:13:53 +02005249 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005250}
5251
5252static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5253 unsigned long domains)
5254{
5255 enum intel_display_power_domain domain;
5256
5257 for_each_power_domain(domain, domains)
5258 intel_display_power_put(dev_priv, domain);
5259}
5260
Mika Kaholaadafdc62015-08-18 14:36:59 +03005261static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5262{
5263 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5264
5265 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5266 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5267 return max_cdclk_freq;
5268 else if (IS_CHERRYVIEW(dev_priv))
5269 return max_cdclk_freq*95/100;
5270 else if (INTEL_INFO(dev_priv)->gen < 4)
5271 return 2*max_cdclk_freq*90/100;
5272 else
5273 return max_cdclk_freq*90/100;
5274}
5275
Ville Syrjäläb2045352016-05-13 23:41:27 +03005276static int skl_calc_cdclk(int max_pixclk, int vco);
5277
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005278static void intel_update_max_cdclk(struct drm_device *dev)
5279{
5280 struct drm_i915_private *dev_priv = dev->dev_private;
5281
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005282 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005283 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005284 int max_cdclk, vco;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005285
Ville Syrjäläb2045352016-05-13 23:41:27 +03005286 vco = dev_priv->skl_preferred_vco_freq;
Ville Syrjälä63911d72016-05-13 23:41:32 +03005287 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005288
5289 /*
5290 * Use the lower (vco 8640) cdclk values as a
5291 * first guess. skl_calc_cdclk() will correct it
5292 * if the preferred vco is 8100 instead.
5293 */
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005294 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005295 max_cdclk = 617143;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005296 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005297 max_cdclk = 540000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005298 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005299 max_cdclk = 432000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005300 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005301 max_cdclk = 308571;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005302
5303 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
Matt Roper281c1142016-04-05 14:37:19 -07005304 } else if (IS_BROXTON(dev)) {
5305 dev_priv->max_cdclk_freq = 624000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005306 } else if (IS_BROADWELL(dev)) {
5307 /*
5308 * FIXME with extra cooling we can allow
5309 * 540 MHz for ULX and 675 Mhz for ULT.
5310 * How can we know if extra cooling is
5311 * available? PCI ID, VTB, something else?
5312 */
5313 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5314 dev_priv->max_cdclk_freq = 450000;
5315 else if (IS_BDW_ULX(dev))
5316 dev_priv->max_cdclk_freq = 450000;
5317 else if (IS_BDW_ULT(dev))
5318 dev_priv->max_cdclk_freq = 540000;
5319 else
5320 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005321 } else if (IS_CHERRYVIEW(dev)) {
5322 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005323 } else if (IS_VALLEYVIEW(dev)) {
5324 dev_priv->max_cdclk_freq = 400000;
5325 } else {
5326 /* otherwise assume cdclk is fixed */
5327 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5328 }
5329
Mika Kaholaadafdc62015-08-18 14:36:59 +03005330 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5331
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005332 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5333 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005334
5335 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5336 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005337}
5338
5339static void intel_update_cdclk(struct drm_device *dev)
5340{
5341 struct drm_i915_private *dev_priv = dev->dev_private;
5342
5343 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005344
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005345 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005346 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5347 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5348 dev_priv->cdclk_pll.ref);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005349 else
5350 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5351 dev_priv->cdclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005352
5353 /*
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005354 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5355 * Programmng [sic] note: bit[9:2] should be programmed to the number
5356 * of cdclk that generates 4MHz reference clock freq which is used to
5357 * generate GMBus clock. This will vary with the cdclk freq.
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005358 */
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005359 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005360 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005361}
5362
Ville Syrjälä92891e42016-05-11 22:44:45 +03005363/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5364static int skl_cdclk_decimal(int cdclk)
5365{
5366 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5367}
5368
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005369static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5370{
5371 int ratio;
5372
5373 if (cdclk == dev_priv->cdclk_pll.ref)
5374 return 0;
5375
5376 switch (cdclk) {
5377 default:
5378 MISSING_CASE(cdclk);
5379 case 144000:
5380 case 288000:
5381 case 384000:
5382 case 576000:
5383 ratio = 60;
5384 break;
5385 case 624000:
5386 ratio = 65;
5387 break;
5388 }
5389
5390 return dev_priv->cdclk_pll.ref * ratio;
5391}
5392
Ville Syrjälä2b730012016-05-13 23:41:34 +03005393static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5394{
5395 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5396
5397 /* Timeout 200us */
5398 if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) == 0, 1))
5399 DRM_ERROR("timeout waiting for DE PLL unlock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005400
5401 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005402}
5403
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005404static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005405{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005406 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005407 u32 val;
5408
5409 val = I915_READ(BXT_DE_PLL_CTL);
5410 val &= ~BXT_DE_PLL_RATIO_MASK;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005411 val |= BXT_DE_PLL_RATIO(ratio);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005412 I915_WRITE(BXT_DE_PLL_CTL, val);
5413
5414 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5415
5416 /* Timeout 200us */
5417 if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) != 0, 1))
5418 DRM_ERROR("timeout waiting for DE PLL lock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005419
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005420 dev_priv->cdclk_pll.vco = vco;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005421}
5422
Imre Deak324513c2016-06-13 16:44:36 +03005423static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305424{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005425 u32 val, divider;
5426 int vco, ret;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305427
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005428 vco = bxt_de_pll_vco(dev_priv, cdclk);
5429
5430 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5431
5432 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5433 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5434 case 8:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305435 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305436 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005437 case 4:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305438 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305439 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005440 case 3:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305441 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305442 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005443 case 2:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305444 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305445 break;
5446 default:
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005447 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5448 WARN_ON(vco != 0);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305449
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005450 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5451 break;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305452 }
5453
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305454 /* Inform power controller of upcoming frequency change */
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005455 mutex_lock(&dev_priv->rps.hw_lock);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305456 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5457 0x80000000);
5458 mutex_unlock(&dev_priv->rps.hw_lock);
5459
5460 if (ret) {
5461 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005462 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305463 return;
5464 }
5465
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005466 if (dev_priv->cdclk_pll.vco != 0 &&
5467 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005468 bxt_de_pll_disable(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305469
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005470 if (dev_priv->cdclk_pll.vco != vco)
5471 bxt_de_pll_enable(dev_priv, vco);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305472
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005473 val = divider | skl_cdclk_decimal(cdclk);
5474 /*
5475 * FIXME if only the cd2x divider needs changing, it could be done
5476 * without shutting off the pipe (if only one pipe is active).
5477 */
5478 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5479 /*
5480 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5481 * enable otherwise.
5482 */
5483 if (cdclk >= 500000)
5484 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5485 I915_WRITE(CDCLK_CTL, val);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305486
5487 mutex_lock(&dev_priv->rps.hw_lock);
5488 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005489 DIV_ROUND_UP(cdclk, 25000));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305490 mutex_unlock(&dev_priv->rps.hw_lock);
5491
5492 if (ret) {
5493 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005494 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305495 return;
5496 }
5497
Imre Deakc6c46962016-04-01 16:02:40 +03005498 intel_update_cdclk(dev_priv->dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305499}
5500
Imre Deakd66a2192016-05-24 15:38:33 +03005501static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305502{
Imre Deakd66a2192016-05-24 15:38:33 +03005503 u32 cdctl, expected;
5504
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03005505 intel_update_cdclk(dev_priv->dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305506
Imre Deakd66a2192016-05-24 15:38:33 +03005507 if (dev_priv->cdclk_pll.vco == 0 ||
5508 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5509 goto sanitize;
5510
5511 /* DPLL okay; verify the cdclock
5512 *
5513 * Some BIOS versions leave an incorrect decimal frequency value and
5514 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5515 * so sanitize this register.
5516 */
5517 cdctl = I915_READ(CDCLK_CTL);
5518 /*
5519 * Let's ignore the pipe field, since BIOS could have configured the
5520 * dividers both synching to an active pipe, or asynchronously
5521 * (PIPE_NONE).
5522 */
5523 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5524
5525 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5526 skl_cdclk_decimal(dev_priv->cdclk_freq);
5527 /*
5528 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5529 * enable otherwise.
5530 */
5531 if (dev_priv->cdclk_freq >= 500000)
5532 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5533
5534 if (cdctl == expected)
5535 /* All well; nothing to sanitize */
5536 return;
5537
5538sanitize:
5539 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5540
5541 /* force cdclk programming */
5542 dev_priv->cdclk_freq = 0;
5543
5544 /* force full PLL disable + enable */
5545 dev_priv->cdclk_pll.vco = -1;
5546}
5547
Imre Deak324513c2016-06-13 16:44:36 +03005548void bxt_init_cdclk(struct drm_i915_private *dev_priv)
Imre Deakd66a2192016-05-24 15:38:33 +03005549{
5550 bxt_sanitize_cdclk(dev_priv);
5551
5552 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03005553 return;
Imre Deakc2e001e2016-04-01 16:02:43 +03005554
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305555 /*
5556 * FIXME:
5557 * - The initial CDCLK needs to be read from VBT.
5558 * Need to make this change after VBT has changes for BXT.
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305559 */
Imre Deak324513c2016-06-13 16:44:36 +03005560 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305561}
5562
Imre Deak324513c2016-06-13 16:44:36 +03005563void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305564{
Imre Deak324513c2016-06-13 16:44:36 +03005565 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305566}
5567
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005568static int skl_calc_cdclk(int max_pixclk, int vco)
5569{
Ville Syrjälä63911d72016-05-13 23:41:32 +03005570 if (vco == 8640000) {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005571 if (max_pixclk > 540000)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005572 return 617143;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005573 else if (max_pixclk > 432000)
5574 return 540000;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005575 else if (max_pixclk > 308571)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005576 return 432000;
5577 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005578 return 308571;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005579 } else {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005580 if (max_pixclk > 540000)
5581 return 675000;
5582 else if (max_pixclk > 450000)
5583 return 540000;
5584 else if (max_pixclk > 337500)
5585 return 450000;
5586 else
5587 return 337500;
5588 }
5589}
5590
Ville Syrjäläea617912016-05-13 23:41:24 +03005591static void
5592skl_dpll0_update(struct drm_i915_private *dev_priv)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005593{
Ville Syrjäläea617912016-05-13 23:41:24 +03005594 u32 val;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005595
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005596 dev_priv->cdclk_pll.ref = 24000;
Imre Deak1c3f7702016-05-24 15:38:32 +03005597 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005598
Ville Syrjäläea617912016-05-13 23:41:24 +03005599 val = I915_READ(LCPLL1_CTL);
Imre Deak1c3f7702016-05-24 15:38:32 +03005600 if ((val & LCPLL_PLL_ENABLE) == 0)
Ville Syrjäläea617912016-05-13 23:41:24 +03005601 return;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005602
Imre Deak1c3f7702016-05-24 15:38:32 +03005603 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5604 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005605
Ville Syrjäläea617912016-05-13 23:41:24 +03005606 val = I915_READ(DPLL_CTRL1);
5607
Imre Deak1c3f7702016-05-24 15:38:32 +03005608 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5609 DPLL_CTRL1_SSC(SKL_DPLL0) |
5610 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5611 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5612 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005613
Ville Syrjäläea617912016-05-13 23:41:24 +03005614 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5615 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5616 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5617 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5618 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005619 dev_priv->cdclk_pll.vco = 8100000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005620 break;
5621 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5622 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005623 dev_priv->cdclk_pll.vco = 8640000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005624 break;
5625 default:
5626 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
Ville Syrjäläea617912016-05-13 23:41:24 +03005627 break;
5628 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005629}
5630
Ville Syrjäläb2045352016-05-13 23:41:27 +03005631void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5632{
5633 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5634
5635 dev_priv->skl_preferred_vco_freq = vco;
5636
5637 if (changed)
5638 intel_update_max_cdclk(dev_priv->dev);
5639}
5640
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005641static void
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005642skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005643{
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005644 int min_cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005645 u32 val;
5646
Ville Syrjälä63911d72016-05-13 23:41:32 +03005647 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005648
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005649 /* select the minimum CDCLK before enabling DPLL 0 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005650 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005651 I915_WRITE(CDCLK_CTL, val);
5652 POSTING_READ(CDCLK_CTL);
5653
5654 /*
5655 * We always enable DPLL0 with the lowest link rate possible, but still
5656 * taking into account the VCO required to operate the eDP panel at the
5657 * desired frequency. The usual DP link rates operate with a VCO of
5658 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5659 * The modeset code is responsible for the selection of the exact link
5660 * rate later on, with the constraint of choosing a frequency that
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005661 * works with vco.
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005662 */
5663 val = I915_READ(DPLL_CTRL1);
5664
5665 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5666 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5667 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Ville Syrjälä63911d72016-05-13 23:41:32 +03005668 if (vco == 8640000)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005669 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5670 SKL_DPLL0);
5671 else
5672 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5673 SKL_DPLL0);
5674
5675 I915_WRITE(DPLL_CTRL1, val);
5676 POSTING_READ(DPLL_CTRL1);
5677
5678 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5679
5680 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5681 DRM_ERROR("DPLL0 not locked\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005682
Ville Syrjälä63911d72016-05-13 23:41:32 +03005683 dev_priv->cdclk_pll.vco = vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005684
5685 /* We'll want to keep using the current vco from now on. */
5686 skl_set_preferred_cdclk_vco(dev_priv, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005687}
5688
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005689static void
5690skl_dpll0_disable(struct drm_i915_private *dev_priv)
5691{
5692 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5693 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5694 DRM_ERROR("Couldn't disable DPLL0\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005695
Ville Syrjälä63911d72016-05-13 23:41:32 +03005696 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005697}
5698
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005699static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5700{
5701 int ret;
5702 u32 val;
5703
5704 /* inform PCU we want to change CDCLK */
5705 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5706 mutex_lock(&dev_priv->rps.hw_lock);
5707 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5708 mutex_unlock(&dev_priv->rps.hw_lock);
5709
5710 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5711}
5712
5713static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5714{
5715 unsigned int i;
5716
5717 for (i = 0; i < 15; i++) {
5718 if (skl_cdclk_pcu_ready(dev_priv))
5719 return true;
5720 udelay(10);
5721 }
5722
5723 return false;
5724}
5725
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005726static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005727{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005728 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005729 u32 freq_select, pcu_ack;
5730
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005731 WARN_ON((cdclk == 24000) != (vco == 0));
5732
Ville Syrjälä63911d72016-05-13 23:41:32 +03005733 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005734
5735 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5736 DRM_ERROR("failed to inform PCU about cdclk change\n");
5737 return;
5738 }
5739
5740 /* set CDCLK_CTL */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005741 switch (cdclk) {
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005742 case 450000:
5743 case 432000:
5744 freq_select = CDCLK_FREQ_450_432;
5745 pcu_ack = 1;
5746 break;
5747 case 540000:
5748 freq_select = CDCLK_FREQ_540;
5749 pcu_ack = 2;
5750 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005751 case 308571:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005752 case 337500:
5753 default:
5754 freq_select = CDCLK_FREQ_337_308;
5755 pcu_ack = 0;
5756 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005757 case 617143:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005758 case 675000:
5759 freq_select = CDCLK_FREQ_675_617;
5760 pcu_ack = 3;
5761 break;
5762 }
5763
Ville Syrjälä63911d72016-05-13 23:41:32 +03005764 if (dev_priv->cdclk_pll.vco != 0 &&
5765 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005766 skl_dpll0_disable(dev_priv);
5767
Ville Syrjälä63911d72016-05-13 23:41:32 +03005768 if (dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005769 skl_dpll0_enable(dev_priv, vco);
5770
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005771 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005772 POSTING_READ(CDCLK_CTL);
5773
5774 /* inform PCU of the change */
5775 mutex_lock(&dev_priv->rps.hw_lock);
5776 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5777 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005778
5779 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005780}
5781
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005782static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5783
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005784void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5785{
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005786 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005787}
5788
5789void skl_init_cdclk(struct drm_i915_private *dev_priv)
5790{
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005791 int cdclk, vco;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005792
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005793 skl_sanitize_cdclk(dev_priv);
5794
Ville Syrjälä63911d72016-05-13 23:41:32 +03005795 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005796 /*
5797 * Use the current vco as our initial
5798 * guess as to what the preferred vco is.
5799 */
5800 if (dev_priv->skl_preferred_vco_freq == 0)
5801 skl_set_preferred_cdclk_vco(dev_priv,
Ville Syrjälä63911d72016-05-13 23:41:32 +03005802 dev_priv->cdclk_pll.vco);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005803 return;
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005804 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005805
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005806 vco = dev_priv->skl_preferred_vco_freq;
5807 if (vco == 0)
Ville Syrjälä63911d72016-05-13 23:41:32 +03005808 vco = 8100000;
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005809 cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005810
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005811 skl_set_cdclk(dev_priv, cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005812}
5813
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005814static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305815{
Ville Syrjälä09492492016-05-13 23:41:28 +03005816 uint32_t cdctl, expected;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305817
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305818 /*
5819 * check if the pre-os intialized the display
5820 * There is SWF18 scratchpad register defined which is set by the
5821 * pre-os which can be used by the OS drivers to check the status
5822 */
5823 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5824 goto sanitize;
5825
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005826 intel_update_cdclk(dev_priv->dev);
Imre Deak1c3f7702016-05-24 15:38:32 +03005827 /* Is PLL enabled and locked ? */
5828 if (dev_priv->cdclk_pll.vco == 0 ||
5829 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5830 goto sanitize;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005831
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305832 /* DPLL okay; verify the cdclock
5833 *
5834 * Noticed in some instances that the freq selection is correct but
5835 * decimal part is programmed wrong from BIOS where pre-os does not
5836 * enable display. Verify the same as well.
5837 */
Ville Syrjälä09492492016-05-13 23:41:28 +03005838 cdctl = I915_READ(CDCLK_CTL);
5839 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5840 skl_cdclk_decimal(dev_priv->cdclk_freq);
5841 if (cdctl == expected)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305842 /* All well; nothing to sanitize */
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005843 return;
5844
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305845sanitize:
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005846 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
Clint Taylorc89e39f2016-05-13 23:41:21 +03005847
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005848 /* force cdclk programming */
5849 dev_priv->cdclk_freq = 0;
5850 /* force full PLL disable + enable */
Ville Syrjälä63911d72016-05-13 23:41:32 +03005851 dev_priv->cdclk_pll.vco = -1;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305852}
5853
Jesse Barnes30a970c2013-11-04 13:48:12 -08005854/* Adjust CDclk dividers to allow high res or save power if possible */
5855static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5856{
5857 struct drm_i915_private *dev_priv = dev->dev_private;
5858 u32 val, cmd;
5859
Vandana Kannan164dfd22014-11-24 13:37:41 +05305860 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5861 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005862
Ville Syrjälädfcab172014-06-13 13:37:47 +03005863 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005864 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005865 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005866 cmd = 1;
5867 else
5868 cmd = 0;
5869
5870 mutex_lock(&dev_priv->rps.hw_lock);
5871 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5872 val &= ~DSPFREQGUAR_MASK;
5873 val |= (cmd << DSPFREQGUAR_SHIFT);
5874 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5875 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5876 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5877 50)) {
5878 DRM_ERROR("timed out waiting for CDclk change\n");
5879 }
5880 mutex_unlock(&dev_priv->rps.hw_lock);
5881
Ville Syrjälä54433e92015-05-26 20:42:31 +03005882 mutex_lock(&dev_priv->sb_lock);
5883
Ville Syrjälädfcab172014-06-13 13:37:47 +03005884 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005885 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005886
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005887 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005888
Jesse Barnes30a970c2013-11-04 13:48:12 -08005889 /* adjust cdclk divider */
5890 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005891 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005892 val |= divider;
5893 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005894
5895 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005896 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005897 50))
5898 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005899 }
5900
Jesse Barnes30a970c2013-11-04 13:48:12 -08005901 /* adjust self-refresh exit latency value */
5902 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5903 val &= ~0x7f;
5904
5905 /*
5906 * For high bandwidth configs, we set a higher latency in the bunit
5907 * so that the core display fetch happens in time to avoid underruns.
5908 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005909 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005910 val |= 4500 / 250; /* 4.5 usec */
5911 else
5912 val |= 3000 / 250; /* 3.0 usec */
5913 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005914
Ville Syrjäläa5805162015-05-26 20:42:30 +03005915 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005916
Ville Syrjäläb6283052015-06-03 15:45:07 +03005917 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005918}
5919
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005920static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5921{
5922 struct drm_i915_private *dev_priv = dev->dev_private;
5923 u32 val, cmd;
5924
Vandana Kannan164dfd22014-11-24 13:37:41 +05305925 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5926 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005927
5928 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005929 case 333333:
5930 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005931 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005932 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005933 break;
5934 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005935 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005936 return;
5937 }
5938
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005939 /*
5940 * Specs are full of misinformation, but testing on actual
5941 * hardware has shown that we just need to write the desired
5942 * CCK divider into the Punit register.
5943 */
5944 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5945
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005946 mutex_lock(&dev_priv->rps.hw_lock);
5947 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5948 val &= ~DSPFREQGUAR_MASK_CHV;
5949 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5950 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5951 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5952 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5953 50)) {
5954 DRM_ERROR("timed out waiting for CDclk change\n");
5955 }
5956 mutex_unlock(&dev_priv->rps.hw_lock);
5957
Ville Syrjäläb6283052015-06-03 15:45:07 +03005958 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005959}
5960
Jesse Barnes30a970c2013-11-04 13:48:12 -08005961static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5962 int max_pixclk)
5963{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005964 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005965 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005966
Jesse Barnes30a970c2013-11-04 13:48:12 -08005967 /*
5968 * Really only a few cases to deal with, as only 4 CDclks are supported:
5969 * 200MHz
5970 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005971 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005972 * 400MHz (VLV only)
5973 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5974 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005975 *
5976 * We seem to get an unstable or solid color picture at 200MHz.
5977 * Not sure what's wrong. For now use 200MHz only when all pipes
5978 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005979 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005980 if (!IS_CHERRYVIEW(dev_priv) &&
5981 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005982 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005983 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005984 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005985 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005986 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005987 else
5988 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005989}
5990
Imre Deak324513c2016-06-13 16:44:36 +03005991static int bxt_calc_cdclk(int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005992{
Ville Syrjälä760e1472016-05-11 22:44:46 +03005993 if (max_pixclk > 576000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305994 return 624000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005995 else if (max_pixclk > 384000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305996 return 576000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005997 else if (max_pixclk > 288000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305998 return 384000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005999 else if (max_pixclk > 144000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306000 return 288000;
6001 else
6002 return 144000;
6003}
6004
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01006005/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006006static int intel_mode_max_pixclk(struct drm_device *dev,
6007 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006008{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006009 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
6010 struct drm_i915_private *dev_priv = dev->dev_private;
6011 struct drm_crtc *crtc;
6012 struct drm_crtc_state *crtc_state;
6013 unsigned max_pixclk = 0, i;
6014 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006015
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006016 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6017 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006018
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006019 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6020 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006021
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006022 if (crtc_state->enable)
6023 pixclk = crtc_state->adjusted_mode.crtc_clock;
6024
6025 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006026 }
6027
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006028 for_each_pipe(dev_priv, pipe)
6029 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6030
Jesse Barnes30a970c2013-11-04 13:48:12 -08006031 return max_pixclk;
6032}
6033
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006034static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006035{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006036 struct drm_device *dev = state->dev;
6037 struct drm_i915_private *dev_priv = dev->dev_private;
6038 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006039 struct intel_atomic_state *intel_state =
6040 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006041
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006042 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006043 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306044
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006045 if (!intel_state->active_crtcs)
6046 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6047
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006048 return 0;
6049}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006050
Imre Deak324513c2016-06-13 16:44:36 +03006051static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006052{
Ville Syrjälä4e5ca602016-05-11 22:44:44 +03006053 int max_pixclk = ilk_max_pixel_rate(state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006054 struct intel_atomic_state *intel_state =
6055 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006056
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006057 intel_state->cdclk = intel_state->dev_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +03006058 bxt_calc_cdclk(max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006059
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006060 if (!intel_state->active_crtcs)
Imre Deak324513c2016-06-13 16:44:36 +03006061 intel_state->dev_cdclk = bxt_calc_cdclk(0);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006062
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006063 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006064}
6065
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006066static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6067{
6068 unsigned int credits, default_credits;
6069
6070 if (IS_CHERRYVIEW(dev_priv))
6071 default_credits = PFI_CREDIT(12);
6072 else
6073 default_credits = PFI_CREDIT(8);
6074
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006075 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006076 /* CHV suggested value is 31 or 63 */
6077 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006078 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006079 else
6080 credits = PFI_CREDIT(15);
6081 } else {
6082 credits = default_credits;
6083 }
6084
6085 /*
6086 * WA - write default credits before re-programming
6087 * FIXME: should we also set the resend bit here?
6088 */
6089 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6090 default_credits);
6091
6092 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6093 credits | PFI_CREDIT_RESEND);
6094
6095 /*
6096 * FIXME is this guaranteed to clear
6097 * immediately or should we poll for it?
6098 */
6099 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6100}
6101
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006102static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006103{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006104 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006105 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006106 struct intel_atomic_state *old_intel_state =
6107 to_intel_atomic_state(old_state);
6108 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006109
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006110 /*
6111 * FIXME: We can end up here with all power domains off, yet
6112 * with a CDCLK frequency other than the minimum. To account
6113 * for this take the PIPE-A power domain, which covers the HW
6114 * blocks needed for the following programming. This can be
6115 * removed once it's guaranteed that we get here either with
6116 * the minimum CDCLK set, or the required power domains
6117 * enabled.
6118 */
6119 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006120
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006121 if (IS_CHERRYVIEW(dev))
6122 cherryview_set_cdclk(dev, req_cdclk);
6123 else
6124 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006125
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006126 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006127
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006128 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006129}
6130
Jesse Barnes89b667f2013-04-18 14:51:36 -07006131static void valleyview_crtc_enable(struct drm_crtc *crtc)
6132{
6133 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006134 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006135 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6136 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006137 struct intel_crtc_state *pipe_config =
6138 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006139 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006140
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006141 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006142 return;
6143
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006144 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306145 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006146
6147 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006148 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006149
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006150 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6151 struct drm_i915_private *dev_priv = dev->dev_private;
6152
6153 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6154 I915_WRITE(CHV_CANVAS(pipe), 0);
6155 }
6156
Daniel Vetter5b18e572014-04-24 23:55:06 +02006157 i9xx_set_pipeconf(intel_crtc);
6158
Jesse Barnes89b667f2013-04-18 14:51:36 -07006159 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006160
Daniel Vettera72e4c92014-09-30 10:56:47 +02006161 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006162
Jesse Barnes89b667f2013-04-18 14:51:36 -07006163 for_each_encoder_on_crtc(dev, crtc, encoder)
6164 if (encoder->pre_pll_enable)
6165 encoder->pre_pll_enable(encoder);
6166
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006167 if (IS_CHERRYVIEW(dev)) {
6168 chv_prepare_pll(intel_crtc, intel_crtc->config);
6169 chv_enable_pll(intel_crtc, intel_crtc->config);
6170 } else {
6171 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6172 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006173 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006174
6175 for_each_encoder_on_crtc(dev, crtc, encoder)
6176 if (encoder->pre_enable)
6177 encoder->pre_enable(encoder);
6178
Jesse Barnes2dd24552013-04-25 12:55:01 -07006179 i9xx_pfit_enable(intel_crtc);
6180
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006181 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006182
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006183 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006184 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006185
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006186 assert_vblank_disabled(crtc);
6187 drm_crtc_vblank_on(crtc);
6188
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006189 for_each_encoder_on_crtc(dev, crtc, encoder)
6190 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006191}
6192
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006193static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6194{
6195 struct drm_device *dev = crtc->base.dev;
6196 struct drm_i915_private *dev_priv = dev->dev_private;
6197
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006198 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6199 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006200}
6201
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006202static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006203{
6204 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006205 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006206 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006207 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006208 struct intel_crtc_state *pipe_config =
6209 to_intel_crtc_state(crtc->state);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006210 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006211
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006212 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006213 return;
6214
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006215 i9xx_set_pll_dividers(intel_crtc);
6216
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006217 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306218 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006219
6220 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006221 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006222
Daniel Vetter5b18e572014-04-24 23:55:06 +02006223 i9xx_set_pipeconf(intel_crtc);
6224
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006225 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006226
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006227 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006228 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006229
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006230 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006231 if (encoder->pre_enable)
6232 encoder->pre_enable(encoder);
6233
Daniel Vetterf6736a12013-06-05 13:34:30 +02006234 i9xx_enable_pll(intel_crtc);
6235
Jesse Barnes2dd24552013-04-25 12:55:01 -07006236 i9xx_pfit_enable(intel_crtc);
6237
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006238 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006239
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006240 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006241 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006242
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006243 assert_vblank_disabled(crtc);
6244 drm_crtc_vblank_on(crtc);
6245
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006246 for_each_encoder_on_crtc(dev, crtc, encoder)
6247 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006248}
6249
Daniel Vetter87476d62013-04-11 16:29:06 +02006250static void i9xx_pfit_disable(struct intel_crtc *crtc)
6251{
6252 struct drm_device *dev = crtc->base.dev;
6253 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006254
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006255 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006256 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006257
6258 assert_pipe_disabled(dev_priv, crtc->pipe);
6259
Daniel Vetter328d8e82013-05-08 10:36:31 +02006260 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6261 I915_READ(PFIT_CONTROL));
6262 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006263}
6264
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006265static void i9xx_crtc_disable(struct drm_crtc *crtc)
6266{
6267 struct drm_device *dev = crtc->dev;
6268 struct drm_i915_private *dev_priv = dev->dev_private;
6269 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006270 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006271 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006272
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006273 /*
6274 * On gen2 planes are double buffered but the pipe isn't, so we must
6275 * wait for planes to fully turn off before disabling the pipe.
6276 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006277 if (IS_GEN2(dev))
6278 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006279
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006280 for_each_encoder_on_crtc(dev, crtc, encoder)
6281 encoder->disable(encoder);
6282
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006283 drm_crtc_vblank_off(crtc);
6284 assert_vblank_disabled(crtc);
6285
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006286 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006287
Daniel Vetter87476d62013-04-11 16:29:06 +02006288 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006289
Jesse Barnes89b667f2013-04-18 14:51:36 -07006290 for_each_encoder_on_crtc(dev, crtc, encoder)
6291 if (encoder->post_disable)
6292 encoder->post_disable(encoder);
6293
Jani Nikulaa65347b2015-11-27 12:21:46 +02006294 if (!intel_crtc->config->has_dsi_encoder) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006295 if (IS_CHERRYVIEW(dev))
6296 chv_disable_pll(dev_priv, pipe);
6297 else if (IS_VALLEYVIEW(dev))
6298 vlv_disable_pll(dev_priv, pipe);
6299 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006300 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006301 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006302
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006303 for_each_encoder_on_crtc(dev, crtc, encoder)
6304 if (encoder->post_pll_disable)
6305 encoder->post_pll_disable(encoder);
6306
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006307 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006308 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006309}
6310
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006311static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006312{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006313 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006314 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006315 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006316 enum intel_display_power_domain domain;
6317 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006318
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006319 if (!intel_crtc->active)
6320 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006321
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006322 if (to_intel_plane_state(crtc->primary->state)->visible) {
Daniel Vetter5a21b662016-05-24 17:13:53 +02006323 WARN_ON(intel_crtc->flip_work);
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006324
Ville Syrjälä2622a082016-03-09 19:07:26 +02006325 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006326
6327 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6328 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006329 }
6330
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006331 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006332
Ville Syrjälä78108b72016-05-27 20:59:19 +03006333 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6334 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006335
6336 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6337 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006338 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006339 crtc->enabled = false;
6340 crtc->state->connector_mask = 0;
6341 crtc->state->encoder_mask = 0;
6342
6343 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6344 encoder->base.crtc = NULL;
6345
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006346 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006347 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006348 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006349
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006350 domains = intel_crtc->enabled_power_domains;
6351 for_each_power_domain(domain, domains)
6352 intel_display_power_put(dev_priv, domain);
6353 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006354
6355 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6356 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006357}
6358
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006359/*
6360 * turn all crtc's off, but do not adjust state
6361 * This has to be paired with a call to intel_modeset_setup_hw_state.
6362 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006363int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006364{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006365 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006366 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006367 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006368
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006369 state = drm_atomic_helper_suspend(dev);
6370 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006371 if (ret)
6372 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006373 else
6374 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006375 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006376}
6377
Chris Wilsonea5b2132010-08-04 13:50:23 +01006378void intel_encoder_destroy(struct drm_encoder *encoder)
6379{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006380 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006381
Chris Wilsonea5b2132010-08-04 13:50:23 +01006382 drm_encoder_cleanup(encoder);
6383 kfree(intel_encoder);
6384}
6385
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006386/* Cross check the actual hw state with our own modeset state tracking (and it's
6387 * internal consistency). */
Daniel Vetter5a21b662016-05-24 17:13:53 +02006388static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006389{
Daniel Vetter5a21b662016-05-24 17:13:53 +02006390 struct drm_crtc *crtc = connector->base.state->crtc;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006391
6392 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6393 connector->base.base.id,
6394 connector->base.name);
6395
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006396 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006397 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter5a21b662016-05-24 17:13:53 +02006398 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006399
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006400 I915_STATE_WARN(!crtc,
6401 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006402
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006403 if (!crtc)
6404 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006405
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006406 I915_STATE_WARN(!crtc->state->active,
6407 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006408
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006409 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006410 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006411
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006412 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006413 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006414
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006415 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006416 "attached encoder crtc differs from connector crtc\n");
6417 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006418 I915_STATE_WARN(crtc && crtc->state->active,
6419 "attached crtc is active, but connector isn't\n");
Daniel Vetter5a21b662016-05-24 17:13:53 +02006420 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006421 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006422 }
6423}
6424
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006425int intel_connector_init(struct intel_connector *connector)
6426{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006427 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006428
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006429 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006430 return -ENOMEM;
6431
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006432 return 0;
6433}
6434
6435struct intel_connector *intel_connector_alloc(void)
6436{
6437 struct intel_connector *connector;
6438
6439 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6440 if (!connector)
6441 return NULL;
6442
6443 if (intel_connector_init(connector) < 0) {
6444 kfree(connector);
6445 return NULL;
6446 }
6447
6448 return connector;
6449}
6450
Daniel Vetterf0947c32012-07-02 13:10:34 +02006451/* Simple connector->get_hw_state implementation for encoders that support only
6452 * one connector and no cloning and hence the encoder state determines the state
6453 * of the connector. */
6454bool intel_connector_get_hw_state(struct intel_connector *connector)
6455{
Daniel Vetter24929352012-07-02 20:28:59 +02006456 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006457 struct intel_encoder *encoder = connector->encoder;
6458
6459 return encoder->get_hw_state(encoder, &pipe);
6460}
6461
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006462static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006463{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006464 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6465 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006466
6467 return 0;
6468}
6469
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006470static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006471 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006472{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006473 struct drm_atomic_state *state = pipe_config->base.state;
6474 struct intel_crtc *other_crtc;
6475 struct intel_crtc_state *other_crtc_state;
6476
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006477 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6478 pipe_name(pipe), pipe_config->fdi_lanes);
6479 if (pipe_config->fdi_lanes > 4) {
6480 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6481 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006482 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006483 }
6484
Paulo Zanonibafb6552013-11-02 21:07:44 -07006485 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006486 if (pipe_config->fdi_lanes > 2) {
6487 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6488 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006489 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006490 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006491 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006492 }
6493 }
6494
6495 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006496 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006497
6498 /* Ivybridge 3 pipe is really complicated */
6499 switch (pipe) {
6500 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006501 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006502 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006503 if (pipe_config->fdi_lanes <= 2)
6504 return 0;
6505
6506 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6507 other_crtc_state =
6508 intel_atomic_get_crtc_state(state, other_crtc);
6509 if (IS_ERR(other_crtc_state))
6510 return PTR_ERR(other_crtc_state);
6511
6512 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006513 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6514 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006515 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006516 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006517 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006518 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006519 if (pipe_config->fdi_lanes > 2) {
6520 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6521 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006522 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006523 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006524
6525 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6526 other_crtc_state =
6527 intel_atomic_get_crtc_state(state, other_crtc);
6528 if (IS_ERR(other_crtc_state))
6529 return PTR_ERR(other_crtc_state);
6530
6531 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006532 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006533 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006534 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006535 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006536 default:
6537 BUG();
6538 }
6539}
6540
Daniel Vettere29c22c2013-02-21 00:00:16 +01006541#define RETRY 1
6542static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006543 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006544{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006545 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006546 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006547 int lane, link_bw, fdi_dotclock, ret;
6548 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006549
Daniel Vettere29c22c2013-02-21 00:00:16 +01006550retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006551 /* FDI is a binary signal running at ~2.7GHz, encoding
6552 * each output octet as 10 bits. The actual frequency
6553 * is stored as a divider into a 100MHz clock, and the
6554 * mode pixel clock is stored in units of 1KHz.
6555 * Hence the bw of each lane in terms of the mode signal
6556 * is:
6557 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006558 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006559
Damien Lespiau241bfc32013-09-25 16:45:37 +01006560 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006561
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006562 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006563 pipe_config->pipe_bpp);
6564
6565 pipe_config->fdi_lanes = lane;
6566
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006567 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006568 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006569
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006570 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006571 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006572 pipe_config->pipe_bpp -= 2*3;
6573 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6574 pipe_config->pipe_bpp);
6575 needs_recompute = true;
6576 pipe_config->bw_constrained = true;
6577
6578 goto retry;
6579 }
6580
6581 if (needs_recompute)
6582 return RETRY;
6583
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006584 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006585}
6586
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006587static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6588 struct intel_crtc_state *pipe_config)
6589{
6590 if (pipe_config->pipe_bpp > 24)
6591 return false;
6592
6593 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006594 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006595 return true;
6596
6597 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006598 * We compare against max which means we must take
6599 * the increased cdclk requirement into account when
6600 * calculating the new cdclk.
6601 *
6602 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006603 */
6604 return ilk_pipe_pixel_rate(pipe_config) <=
6605 dev_priv->max_cdclk_freq * 95 / 100;
6606}
6607
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006608static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006609 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006610{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006611 struct drm_device *dev = crtc->base.dev;
6612 struct drm_i915_private *dev_priv = dev->dev_private;
6613
Jani Nikulad330a952014-01-21 11:24:25 +02006614 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006615 hsw_crtc_supports_ips(crtc) &&
6616 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006617}
6618
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006619static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6620{
6621 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6622
6623 /* GDG double wide on either pipe, otherwise pipe A only */
6624 return INTEL_INFO(dev_priv)->gen < 4 &&
6625 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6626}
6627
Daniel Vettera43f6e02013-06-07 23:10:32 +02006628static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006629 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006630{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006631 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006632 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006633 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03006634 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01006635
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006636 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006637 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006638
6639 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006640 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006641 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006642 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006643 if (intel_crtc_supports_double_wide(crtc) &&
6644 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006645 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006646 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006647 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03006648 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006649
Ville Syrjäläf3261152016-05-24 21:34:18 +03006650 if (adjusted_mode->crtc_clock > clock_limit) {
6651 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6652 adjusted_mode->crtc_clock, clock_limit,
6653 yesno(pipe_config->double_wide));
6654 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006655 }
Chris Wilson89749352010-09-12 18:25:19 +01006656
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006657 /*
6658 * Pipe horizontal size must be even in:
6659 * - DVO ganged mode
6660 * - LVDS dual channel mode
6661 * - Double wide pipe
6662 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006663 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006664 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6665 pipe_config->pipe_src_w &= ~1;
6666
Damien Lespiau8693a822013-05-03 18:48:11 +01006667 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6668 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006669 */
6670 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006671 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006672 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006673
Damien Lespiauf5adf942013-06-24 18:29:34 +01006674 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006675 hsw_compute_ips_config(crtc, pipe_config);
6676
Daniel Vetter877d48d2013-04-19 11:24:43 +02006677 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006678 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006679
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006680 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006681}
6682
Ville Syrjälä1652d192015-03-31 14:12:01 +03006683static int skylake_get_display_clock_speed(struct drm_device *dev)
6684{
6685 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläea617912016-05-13 23:41:24 +03006686 uint32_t cdctl;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006687
Ville Syrjäläea617912016-05-13 23:41:24 +03006688 skl_dpll0_update(dev_priv);
6689
Ville Syrjälä63911d72016-05-13 23:41:32 +03006690 if (dev_priv->cdclk_pll.vco == 0)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006691 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006692
Ville Syrjäläea617912016-05-13 23:41:24 +03006693 cdctl = I915_READ(CDCLK_CTL);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006694
Ville Syrjälä63911d72016-05-13 23:41:32 +03006695 if (dev_priv->cdclk_pll.vco == 8640000) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006696 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6697 case CDCLK_FREQ_450_432:
6698 return 432000;
6699 case CDCLK_FREQ_337_308:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006700 return 308571;
Ville Syrjäläea617912016-05-13 23:41:24 +03006701 case CDCLK_FREQ_540:
6702 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006703 case CDCLK_FREQ_675_617:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006704 return 617143;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006705 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006706 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006707 }
6708 } else {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006709 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6710 case CDCLK_FREQ_450_432:
6711 return 450000;
6712 case CDCLK_FREQ_337_308:
6713 return 337500;
Ville Syrjäläea617912016-05-13 23:41:24 +03006714 case CDCLK_FREQ_540:
6715 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006716 case CDCLK_FREQ_675_617:
6717 return 675000;
6718 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006719 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006720 }
6721 }
6722
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006723 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006724}
6725
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006726static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6727{
6728 u32 val;
6729
6730 dev_priv->cdclk_pll.ref = 19200;
Imre Deak1c3f7702016-05-24 15:38:32 +03006731 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006732
6733 val = I915_READ(BXT_DE_PLL_ENABLE);
Imre Deak1c3f7702016-05-24 15:38:32 +03006734 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006735 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006736
Imre Deak1c3f7702016-05-24 15:38:32 +03006737 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6738 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006739
6740 val = I915_READ(BXT_DE_PLL_CTL);
6741 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6742 dev_priv->cdclk_pll.ref;
6743}
6744
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006745static int broxton_get_display_clock_speed(struct drm_device *dev)
6746{
6747 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf5986242016-05-13 23:41:37 +03006748 u32 divider;
6749 int div, vco;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006750
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006751 bxt_de_pll_update(dev_priv);
6752
Ville Syrjäläf5986242016-05-13 23:41:37 +03006753 vco = dev_priv->cdclk_pll.vco;
6754 if (vco == 0)
6755 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006756
Ville Syrjäläf5986242016-05-13 23:41:37 +03006757 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006758
Ville Syrjäläf5986242016-05-13 23:41:37 +03006759 switch (divider) {
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006760 case BXT_CDCLK_CD2X_DIV_SEL_1:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006761 div = 2;
6762 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006763 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006764 div = 3;
6765 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006766 case BXT_CDCLK_CD2X_DIV_SEL_2:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006767 div = 4;
6768 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006769 case BXT_CDCLK_CD2X_DIV_SEL_4:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006770 div = 8;
6771 break;
6772 default:
6773 MISSING_CASE(divider);
6774 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006775 }
6776
Ville Syrjäläf5986242016-05-13 23:41:37 +03006777 return DIV_ROUND_CLOSEST(vco, div);
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006778}
6779
Ville Syrjälä1652d192015-03-31 14:12:01 +03006780static int broadwell_get_display_clock_speed(struct drm_device *dev)
6781{
6782 struct drm_i915_private *dev_priv = dev->dev_private;
6783 uint32_t lcpll = I915_READ(LCPLL_CTL);
6784 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6785
6786 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6787 return 800000;
6788 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6789 return 450000;
6790 else if (freq == LCPLL_CLK_FREQ_450)
6791 return 450000;
6792 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6793 return 540000;
6794 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6795 return 337500;
6796 else
6797 return 675000;
6798}
6799
6800static int haswell_get_display_clock_speed(struct drm_device *dev)
6801{
6802 struct drm_i915_private *dev_priv = dev->dev_private;
6803 uint32_t lcpll = I915_READ(LCPLL_CTL);
6804 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6805
6806 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6807 return 800000;
6808 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6809 return 450000;
6810 else if (freq == LCPLL_CLK_FREQ_450)
6811 return 450000;
6812 else if (IS_HSW_ULT(dev))
6813 return 337500;
6814 else
6815 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006816}
6817
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006818static int valleyview_get_display_clock_speed(struct drm_device *dev)
6819{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006820 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6821 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006822}
6823
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006824static int ilk_get_display_clock_speed(struct drm_device *dev)
6825{
6826 return 450000;
6827}
6828
Jesse Barnese70236a2009-09-21 10:42:27 -07006829static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006830{
Jesse Barnese70236a2009-09-21 10:42:27 -07006831 return 400000;
6832}
Jesse Barnes79e53942008-11-07 14:24:08 -08006833
Jesse Barnese70236a2009-09-21 10:42:27 -07006834static int i915_get_display_clock_speed(struct drm_device *dev)
6835{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006836 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006837}
Jesse Barnes79e53942008-11-07 14:24:08 -08006838
Jesse Barnese70236a2009-09-21 10:42:27 -07006839static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6840{
6841 return 200000;
6842}
Jesse Barnes79e53942008-11-07 14:24:08 -08006843
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006844static int pnv_get_display_clock_speed(struct drm_device *dev)
6845{
6846 u16 gcfgc = 0;
6847
6848 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6849
6850 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6851 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006852 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006853 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006854 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006855 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006856 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006857 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6858 return 200000;
6859 default:
6860 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6861 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006862 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006863 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006864 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006865 }
6866}
6867
Jesse Barnese70236a2009-09-21 10:42:27 -07006868static int i915gm_get_display_clock_speed(struct drm_device *dev)
6869{
6870 u16 gcfgc = 0;
6871
6872 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6873
6874 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006875 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006876 else {
6877 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6878 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006879 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006880 default:
6881 case GC_DISPLAY_CLOCK_190_200_MHZ:
6882 return 190000;
6883 }
6884 }
6885}
Jesse Barnes79e53942008-11-07 14:24:08 -08006886
Jesse Barnese70236a2009-09-21 10:42:27 -07006887static int i865_get_display_clock_speed(struct drm_device *dev)
6888{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006889 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006890}
6891
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006892static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006893{
6894 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006895
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006896 /*
6897 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6898 * encoding is different :(
6899 * FIXME is this the right way to detect 852GM/852GMV?
6900 */
6901 if (dev->pdev->revision == 0x1)
6902 return 133333;
6903
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006904 pci_bus_read_config_word(dev->pdev->bus,
6905 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6906
Jesse Barnese70236a2009-09-21 10:42:27 -07006907 /* Assume that the hardware is in the high speed state. This
6908 * should be the default.
6909 */
6910 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6911 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006912 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006913 case GC_CLOCK_100_200:
6914 return 200000;
6915 case GC_CLOCK_166_250:
6916 return 250000;
6917 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006918 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006919 case GC_CLOCK_133_266:
6920 case GC_CLOCK_133_266_2:
6921 case GC_CLOCK_166_266:
6922 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006923 }
6924
6925 /* Shouldn't happen */
6926 return 0;
6927}
6928
6929static int i830_get_display_clock_speed(struct drm_device *dev)
6930{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006931 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006932}
6933
Ville Syrjälä34edce22015-05-22 11:22:33 +03006934static unsigned int intel_hpll_vco(struct drm_device *dev)
6935{
6936 struct drm_i915_private *dev_priv = dev->dev_private;
6937 static const unsigned int blb_vco[8] = {
6938 [0] = 3200000,
6939 [1] = 4000000,
6940 [2] = 5333333,
6941 [3] = 4800000,
6942 [4] = 6400000,
6943 };
6944 static const unsigned int pnv_vco[8] = {
6945 [0] = 3200000,
6946 [1] = 4000000,
6947 [2] = 5333333,
6948 [3] = 4800000,
6949 [4] = 2666667,
6950 };
6951 static const unsigned int cl_vco[8] = {
6952 [0] = 3200000,
6953 [1] = 4000000,
6954 [2] = 5333333,
6955 [3] = 6400000,
6956 [4] = 3333333,
6957 [5] = 3566667,
6958 [6] = 4266667,
6959 };
6960 static const unsigned int elk_vco[8] = {
6961 [0] = 3200000,
6962 [1] = 4000000,
6963 [2] = 5333333,
6964 [3] = 4800000,
6965 };
6966 static const unsigned int ctg_vco[8] = {
6967 [0] = 3200000,
6968 [1] = 4000000,
6969 [2] = 5333333,
6970 [3] = 6400000,
6971 [4] = 2666667,
6972 [5] = 4266667,
6973 };
6974 const unsigned int *vco_table;
6975 unsigned int vco;
6976 uint8_t tmp = 0;
6977
6978 /* FIXME other chipsets? */
6979 if (IS_GM45(dev))
6980 vco_table = ctg_vco;
6981 else if (IS_G4X(dev))
6982 vco_table = elk_vco;
6983 else if (IS_CRESTLINE(dev))
6984 vco_table = cl_vco;
6985 else if (IS_PINEVIEW(dev))
6986 vco_table = pnv_vco;
6987 else if (IS_G33(dev))
6988 vco_table = blb_vco;
6989 else
6990 return 0;
6991
6992 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6993
6994 vco = vco_table[tmp & 0x7];
6995 if (vco == 0)
6996 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6997 else
6998 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6999
7000 return vco;
7001}
7002
7003static int gm45_get_display_clock_speed(struct drm_device *dev)
7004{
7005 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7006 uint16_t tmp = 0;
7007
7008 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7009
7010 cdclk_sel = (tmp >> 12) & 0x1;
7011
7012 switch (vco) {
7013 case 2666667:
7014 case 4000000:
7015 case 5333333:
7016 return cdclk_sel ? 333333 : 222222;
7017 case 3200000:
7018 return cdclk_sel ? 320000 : 228571;
7019 default:
7020 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7021 return 222222;
7022 }
7023}
7024
7025static int i965gm_get_display_clock_speed(struct drm_device *dev)
7026{
7027 static const uint8_t div_3200[] = { 16, 10, 8 };
7028 static const uint8_t div_4000[] = { 20, 12, 10 };
7029 static const uint8_t div_5333[] = { 24, 16, 14 };
7030 const uint8_t *div_table;
7031 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7032 uint16_t tmp = 0;
7033
7034 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7035
7036 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7037
7038 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7039 goto fail;
7040
7041 switch (vco) {
7042 case 3200000:
7043 div_table = div_3200;
7044 break;
7045 case 4000000:
7046 div_table = div_4000;
7047 break;
7048 case 5333333:
7049 div_table = div_5333;
7050 break;
7051 default:
7052 goto fail;
7053 }
7054
7055 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7056
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007057fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007058 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7059 return 200000;
7060}
7061
7062static int g33_get_display_clock_speed(struct drm_device *dev)
7063{
7064 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7065 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7066 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7067 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7068 const uint8_t *div_table;
7069 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7070 uint16_t tmp = 0;
7071
7072 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7073
7074 cdclk_sel = (tmp >> 4) & 0x7;
7075
7076 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7077 goto fail;
7078
7079 switch (vco) {
7080 case 3200000:
7081 div_table = div_3200;
7082 break;
7083 case 4000000:
7084 div_table = div_4000;
7085 break;
7086 case 4800000:
7087 div_table = div_4800;
7088 break;
7089 case 5333333:
7090 div_table = div_5333;
7091 break;
7092 default:
7093 goto fail;
7094 }
7095
7096 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7097
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007098fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007099 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7100 return 190476;
7101}
7102
Zhenyu Wang2c072452009-06-05 15:38:42 +08007103static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007104intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007105{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007106 while (*num > DATA_LINK_M_N_MASK ||
7107 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007108 *num >>= 1;
7109 *den >>= 1;
7110 }
7111}
7112
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007113static void compute_m_n(unsigned int m, unsigned int n,
7114 uint32_t *ret_m, uint32_t *ret_n)
7115{
7116 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7117 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7118 intel_reduce_m_n_ratio(ret_m, ret_n);
7119}
7120
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007121void
7122intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7123 int pixel_clock, int link_clock,
7124 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007125{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007126 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007127
7128 compute_m_n(bits_per_pixel * pixel_clock,
7129 link_clock * nlanes * 8,
7130 &m_n->gmch_m, &m_n->gmch_n);
7131
7132 compute_m_n(pixel_clock, link_clock,
7133 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007134}
7135
Chris Wilsona7615032011-01-12 17:04:08 +00007136static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7137{
Jani Nikulad330a952014-01-21 11:24:25 +02007138 if (i915.panel_use_ssc >= 0)
7139 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007140 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007141 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007142}
7143
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007144static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007145{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007146 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007147}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007148
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007149static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7150{
7151 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007152}
7153
Daniel Vetterf47709a2013-03-28 10:42:02 +01007154static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007155 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007156 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08007157{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007158 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007159 u32 fp, fp2 = 0;
7160
7161 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007162 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007163 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007164 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007165 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007166 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007167 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007168 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007169 }
7170
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007171 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007172
Daniel Vetterf47709a2013-03-28 10:42:02 +01007173 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007174 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007175 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007176 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007177 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007178 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007179 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007180 }
7181}
7182
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007183static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7184 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007185{
7186 u32 reg_val;
7187
7188 /*
7189 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7190 * and set it to a reasonable value instead.
7191 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007192 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007193 reg_val &= 0xffffff00;
7194 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007195 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007196
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007197 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007198 reg_val &= 0x8cffffff;
7199 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007200 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007201
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007202 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007203 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007204 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007205
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007206 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007207 reg_val &= 0x00ffffff;
7208 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007209 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007210}
7211
Daniel Vetterb5518422013-05-03 11:49:48 +02007212static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7213 struct intel_link_m_n *m_n)
7214{
7215 struct drm_device *dev = crtc->base.dev;
7216 struct drm_i915_private *dev_priv = dev->dev_private;
7217 int pipe = crtc->pipe;
7218
Daniel Vettere3b95f12013-05-03 11:49:49 +02007219 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7220 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7221 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7222 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007223}
7224
7225static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007226 struct intel_link_m_n *m_n,
7227 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007228{
7229 struct drm_device *dev = crtc->base.dev;
7230 struct drm_i915_private *dev_priv = dev->dev_private;
7231 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007232 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007233
7234 if (INTEL_INFO(dev)->gen >= 5) {
7235 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7236 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7237 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7238 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007239 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7240 * for gen < 8) and if DRRS is supported (to make sure the
7241 * registers are not unnecessarily accessed).
7242 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307243 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007244 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007245 I915_WRITE(PIPE_DATA_M2(transcoder),
7246 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7247 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7248 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7249 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7250 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007251 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007252 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7253 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7254 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7255 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007256 }
7257}
7258
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307259void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007260{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307261 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7262
7263 if (m_n == M1_N1) {
7264 dp_m_n = &crtc->config->dp_m_n;
7265 dp_m2_n2 = &crtc->config->dp_m2_n2;
7266 } else if (m_n == M2_N2) {
7267
7268 /*
7269 * M2_N2 registers are not supported. Hence m2_n2 divider value
7270 * needs to be programmed into M1_N1.
7271 */
7272 dp_m_n = &crtc->config->dp_m2_n2;
7273 } else {
7274 DRM_ERROR("Unsupported divider value\n");
7275 return;
7276 }
7277
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007278 if (crtc->config->has_pch_encoder)
7279 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007280 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307281 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007282}
7283
Daniel Vetter251ac862015-06-18 10:30:24 +02007284static void vlv_compute_dpll(struct intel_crtc *crtc,
7285 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007286{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007287 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007288 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007289 if (crtc->pipe != PIPE_A)
7290 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007291
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007292 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjälä187a1c02016-04-18 20:34:04 +03007293 if (!pipe_config->has_dsi_encoder)
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007294 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7295 DPLL_EXT_BUFFER_ENABLE_VLV;
7296
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007297 pipe_config->dpll_hw_state.dpll_md =
7298 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7299}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007300
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007301static void chv_compute_dpll(struct intel_crtc *crtc,
7302 struct intel_crtc_state *pipe_config)
7303{
7304 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007305 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007306 if (crtc->pipe != PIPE_A)
7307 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7308
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007309 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjälä187a1c02016-04-18 20:34:04 +03007310 if (!pipe_config->has_dsi_encoder)
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007311 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7312
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007313 pipe_config->dpll_hw_state.dpll_md =
7314 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007315}
7316
Ville Syrjäläd288f652014-10-28 13:20:22 +02007317static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007318 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007319{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007320 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007321 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007322 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007323 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007324 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007325 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007326
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007327 /* Enable Refclk */
7328 I915_WRITE(DPLL(pipe),
7329 pipe_config->dpll_hw_state.dpll &
7330 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7331
7332 /* No need to actually set up the DPLL with DSI */
7333 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7334 return;
7335
Ville Syrjäläa5805162015-05-26 20:42:30 +03007336 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007337
Ville Syrjäläd288f652014-10-28 13:20:22 +02007338 bestn = pipe_config->dpll.n;
7339 bestm1 = pipe_config->dpll.m1;
7340 bestm2 = pipe_config->dpll.m2;
7341 bestp1 = pipe_config->dpll.p1;
7342 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007343
Jesse Barnes89b667f2013-04-18 14:51:36 -07007344 /* See eDP HDMI DPIO driver vbios notes doc */
7345
7346 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007347 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007348 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007349
7350 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007351 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007352
7353 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007354 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007355 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007356 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007357
7358 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007359 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007360
7361 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007362 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7363 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7364 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007365 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007366
7367 /*
7368 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7369 * but we don't support that).
7370 * Note: don't use the DAC post divider as it seems unstable.
7371 */
7372 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007373 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007374
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007375 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007376 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007377
Jesse Barnes89b667f2013-04-18 14:51:36 -07007378 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007379 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007380 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7381 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007382 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007383 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007384 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007385 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007386 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007387
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007388 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007389 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007390 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007391 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007392 0x0df40000);
7393 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007394 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007395 0x0df70000);
7396 } else { /* HDMI or VGA */
7397 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007398 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007399 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007400 0x0df70000);
7401 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007402 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007403 0x0df40000);
7404 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007405
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007406 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007407 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007408 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7409 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007410 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007411 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007412
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007413 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007414 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007415}
7416
Ville Syrjäläd288f652014-10-28 13:20:22 +02007417static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007418 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007419{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007420 struct drm_device *dev = crtc->base.dev;
7421 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007422 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007423 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307424 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007425 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307426 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307427 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007428
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007429 /* Enable Refclk and SSC */
7430 I915_WRITE(DPLL(pipe),
7431 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7432
7433 /* No need to actually set up the DPLL with DSI */
7434 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7435 return;
7436
Ville Syrjäläd288f652014-10-28 13:20:22 +02007437 bestn = pipe_config->dpll.n;
7438 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7439 bestm1 = pipe_config->dpll.m1;
7440 bestm2 = pipe_config->dpll.m2 >> 22;
7441 bestp1 = pipe_config->dpll.p1;
7442 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307443 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307444 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307445 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007446
Ville Syrjäläa5805162015-05-26 20:42:30 +03007447 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007448
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007449 /* p1 and p2 divider */
7450 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7451 5 << DPIO_CHV_S1_DIV_SHIFT |
7452 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7453 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7454 1 << DPIO_CHV_K_DIV_SHIFT);
7455
7456 /* Feedback post-divider - m2 */
7457 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7458
7459 /* Feedback refclk divider - n and m1 */
7460 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7461 DPIO_CHV_M1_DIV_BY_2 |
7462 1 << DPIO_CHV_N_DIV_SHIFT);
7463
7464 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007465 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007466
7467 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307468 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7469 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7470 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7471 if (bestm2_frac)
7472 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7473 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007474
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307475 /* Program digital lock detect threshold */
7476 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7477 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7478 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7479 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7480 if (!bestm2_frac)
7481 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7482 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7483
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007484 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307485 if (vco == 5400000) {
7486 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7487 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7488 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7489 tribuf_calcntr = 0x9;
7490 } else if (vco <= 6200000) {
7491 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7492 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7493 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7494 tribuf_calcntr = 0x9;
7495 } else if (vco <= 6480000) {
7496 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7497 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7498 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7499 tribuf_calcntr = 0x8;
7500 } else {
7501 /* Not supported. Apply the same limits as in the max case */
7502 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7503 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7504 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7505 tribuf_calcntr = 0;
7506 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007507 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7508
Ville Syrjälä968040b2015-03-11 22:52:08 +02007509 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307510 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7511 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7512 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7513
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007514 /* AFC Recal */
7515 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7516 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7517 DPIO_AFC_RECAL);
7518
Ville Syrjäläa5805162015-05-26 20:42:30 +03007519 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007520}
7521
Ville Syrjäläd288f652014-10-28 13:20:22 +02007522/**
7523 * vlv_force_pll_on - forcibly enable just the PLL
7524 * @dev_priv: i915 private structure
7525 * @pipe: pipe PLL to enable
7526 * @dpll: PLL configuration
7527 *
7528 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7529 * in cases where we need the PLL enabled even when @pipe is not going to
7530 * be enabled.
7531 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007532int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7533 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007534{
7535 struct intel_crtc *crtc =
7536 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007537 struct intel_crtc_state *pipe_config;
7538
7539 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7540 if (!pipe_config)
7541 return -ENOMEM;
7542
7543 pipe_config->base.crtc = &crtc->base;
7544 pipe_config->pixel_multiplier = 1;
7545 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007546
7547 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007548 chv_compute_dpll(crtc, pipe_config);
7549 chv_prepare_pll(crtc, pipe_config);
7550 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007551 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007552 vlv_compute_dpll(crtc, pipe_config);
7553 vlv_prepare_pll(crtc, pipe_config);
7554 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007555 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007556
7557 kfree(pipe_config);
7558
7559 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007560}
7561
7562/**
7563 * vlv_force_pll_off - forcibly disable just the PLL
7564 * @dev_priv: i915 private structure
7565 * @pipe: pipe PLL to disable
7566 *
7567 * Disable the PLL for @pipe. To be used in cases where we need
7568 * the PLL enabled even when @pipe is not going to be enabled.
7569 */
7570void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7571{
7572 if (IS_CHERRYVIEW(dev))
7573 chv_disable_pll(to_i915(dev), pipe);
7574 else
7575 vlv_disable_pll(to_i915(dev), pipe);
7576}
7577
Daniel Vetter251ac862015-06-18 10:30:24 +02007578static void i9xx_compute_dpll(struct intel_crtc *crtc,
7579 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007580 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007581{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007582 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007583 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007584 u32 dpll;
7585 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007586 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007587
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007588 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307589
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007590 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7591 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007592
7593 dpll = DPLL_VGA_MODE_DIS;
7594
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007595 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007596 dpll |= DPLLB_MODE_LVDS;
7597 else
7598 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007599
Daniel Vetteref1b4602013-06-01 17:17:04 +02007600 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007601 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007602 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007603 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007604
7605 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007606 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007607
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007608 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007609 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007610
7611 /* compute bitmask from p1 value */
7612 if (IS_PINEVIEW(dev))
7613 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7614 else {
7615 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7616 if (IS_G4X(dev) && reduced_clock)
7617 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7618 }
7619 switch (clock->p2) {
7620 case 5:
7621 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7622 break;
7623 case 7:
7624 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7625 break;
7626 case 10:
7627 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7628 break;
7629 case 14:
7630 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7631 break;
7632 }
7633 if (INTEL_INFO(dev)->gen >= 4)
7634 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7635
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007636 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007637 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007638 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007639 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007640 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7641 else
7642 dpll |= PLL_REF_INPUT_DREFCLK;
7643
7644 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007645 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007646
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007647 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007648 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007649 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007650 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007651 }
7652}
7653
Daniel Vetter251ac862015-06-18 10:30:24 +02007654static void i8xx_compute_dpll(struct intel_crtc *crtc,
7655 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007656 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007657{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007658 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007659 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007660 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007661 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007662
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007663 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307664
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007665 dpll = DPLL_VGA_MODE_DIS;
7666
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007667 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007668 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7669 } else {
7670 if (clock->p1 == 2)
7671 dpll |= PLL_P1_DIVIDE_BY_TWO;
7672 else
7673 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7674 if (clock->p2 == 4)
7675 dpll |= PLL_P2_DIVIDE_BY_4;
7676 }
7677
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007678 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007679 dpll |= DPLL_DVO_2X_MODE;
7680
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007681 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007682 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007683 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7684 else
7685 dpll |= PLL_REF_INPUT_DREFCLK;
7686
7687 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007688 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007689}
7690
Daniel Vetter8a654f32013-06-01 17:16:22 +02007691static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007692{
7693 struct drm_device *dev = intel_crtc->base.dev;
7694 struct drm_i915_private *dev_priv = dev->dev_private;
7695 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007696 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007697 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007698 uint32_t crtc_vtotal, crtc_vblank_end;
7699 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007700
7701 /* We need to be careful not to changed the adjusted mode, for otherwise
7702 * the hw state checker will get angry at the mismatch. */
7703 crtc_vtotal = adjusted_mode->crtc_vtotal;
7704 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007705
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007706 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007707 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007708 crtc_vtotal -= 1;
7709 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007710
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007711 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007712 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7713 else
7714 vsyncshift = adjusted_mode->crtc_hsync_start -
7715 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007716 if (vsyncshift < 0)
7717 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007718 }
7719
7720 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007721 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007722
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007723 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007724 (adjusted_mode->crtc_hdisplay - 1) |
7725 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007726 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007727 (adjusted_mode->crtc_hblank_start - 1) |
7728 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007729 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007730 (adjusted_mode->crtc_hsync_start - 1) |
7731 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7732
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007733 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007734 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007735 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007736 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007737 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007738 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007739 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007740 (adjusted_mode->crtc_vsync_start - 1) |
7741 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7742
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007743 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7744 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7745 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7746 * bits. */
7747 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7748 (pipe == PIPE_B || pipe == PIPE_C))
7749 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7750
Jani Nikulabc58be62016-03-18 17:05:39 +02007751}
7752
7753static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7754{
7755 struct drm_device *dev = intel_crtc->base.dev;
7756 struct drm_i915_private *dev_priv = dev->dev_private;
7757 enum pipe pipe = intel_crtc->pipe;
7758
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007759 /* pipesrc controls the size that is scaled from, which should
7760 * always be the user's requested size.
7761 */
7762 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007763 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7764 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007765}
7766
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007767static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007768 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007769{
7770 struct drm_device *dev = crtc->base.dev;
7771 struct drm_i915_private *dev_priv = dev->dev_private;
7772 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7773 uint32_t tmp;
7774
7775 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007776 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7777 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007778 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007779 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7780 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007781 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007782 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7783 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007784
7785 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007786 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7787 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007788 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007789 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7790 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007791 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007792 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7793 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007794
7795 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007796 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7797 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7798 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007799 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007800}
7801
7802static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7803 struct intel_crtc_state *pipe_config)
7804{
7805 struct drm_device *dev = crtc->base.dev;
7806 struct drm_i915_private *dev_priv = dev->dev_private;
7807 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007808
7809 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007810 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7811 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7812
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007813 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7814 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007815}
7816
Daniel Vetterf6a83282014-02-11 15:28:57 -08007817void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007818 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007819{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007820 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7821 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7822 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7823 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007824
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007825 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7826 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7827 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7828 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007829
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007830 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007831 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007832
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007833 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7834 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007835
7836 mode->hsync = drm_mode_hsync(mode);
7837 mode->vrefresh = drm_mode_vrefresh(mode);
7838 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007839}
7840
Daniel Vetter84b046f2013-02-19 18:48:54 +01007841static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7842{
7843 struct drm_device *dev = intel_crtc->base.dev;
7844 struct drm_i915_private *dev_priv = dev->dev_private;
7845 uint32_t pipeconf;
7846
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007847 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007848
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007849 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7850 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7851 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007852
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007853 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007854 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007855
Daniel Vetterff9ce462013-04-24 14:57:17 +02007856 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007857 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007858 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007859 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007860 pipeconf |= PIPECONF_DITHER_EN |
7861 PIPECONF_DITHER_TYPE_SP;
7862
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007863 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007864 case 18:
7865 pipeconf |= PIPECONF_6BPC;
7866 break;
7867 case 24:
7868 pipeconf |= PIPECONF_8BPC;
7869 break;
7870 case 30:
7871 pipeconf |= PIPECONF_10BPC;
7872 break;
7873 default:
7874 /* Case prevented by intel_choose_pipe_bpp_dither. */
7875 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007876 }
7877 }
7878
7879 if (HAS_PIPE_CXSR(dev)) {
7880 if (intel_crtc->lowfreq_avail) {
7881 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7882 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7883 } else {
7884 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007885 }
7886 }
7887
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007888 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007889 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007890 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007891 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7892 else
7893 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7894 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007895 pipeconf |= PIPECONF_PROGRESSIVE;
7896
Wayne Boyer666a4532015-12-09 12:29:35 -08007897 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7898 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007899 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007900
Daniel Vetter84b046f2013-02-19 18:48:54 +01007901 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7902 POSTING_READ(PIPECONF(intel_crtc->pipe));
7903}
7904
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007905static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7906 struct intel_crtc_state *crtc_state)
7907{
7908 struct drm_device *dev = crtc->base.dev;
7909 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007910 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007911 int refclk = 48000;
7912
7913 memset(&crtc_state->dpll_hw_state, 0,
7914 sizeof(crtc_state->dpll_hw_state));
7915
7916 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7917 if (intel_panel_use_ssc(dev_priv)) {
7918 refclk = dev_priv->vbt.lvds_ssc_freq;
7919 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7920 }
7921
7922 limit = &intel_limits_i8xx_lvds;
7923 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
7924 limit = &intel_limits_i8xx_dvo;
7925 } else {
7926 limit = &intel_limits_i8xx_dac;
7927 }
7928
7929 if (!crtc_state->clock_set &&
7930 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7931 refclk, NULL, &crtc_state->dpll)) {
7932 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7933 return -EINVAL;
7934 }
7935
7936 i8xx_compute_dpll(crtc, crtc_state, NULL);
7937
7938 return 0;
7939}
7940
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007941static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7942 struct intel_crtc_state *crtc_state)
7943{
7944 struct drm_device *dev = crtc->base.dev;
7945 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007946 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007947 int refclk = 96000;
7948
7949 memset(&crtc_state->dpll_hw_state, 0,
7950 sizeof(crtc_state->dpll_hw_state));
7951
7952 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7953 if (intel_panel_use_ssc(dev_priv)) {
7954 refclk = dev_priv->vbt.lvds_ssc_freq;
7955 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7956 }
7957
7958 if (intel_is_dual_link_lvds(dev))
7959 limit = &intel_limits_g4x_dual_channel_lvds;
7960 else
7961 limit = &intel_limits_g4x_single_channel_lvds;
7962 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7963 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
7964 limit = &intel_limits_g4x_hdmi;
7965 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
7966 limit = &intel_limits_g4x_sdvo;
7967 } else {
7968 /* The option is for other outputs */
7969 limit = &intel_limits_i9xx_sdvo;
7970 }
7971
7972 if (!crtc_state->clock_set &&
7973 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7974 refclk, NULL, &crtc_state->dpll)) {
7975 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7976 return -EINVAL;
7977 }
7978
7979 i9xx_compute_dpll(crtc, crtc_state, NULL);
7980
7981 return 0;
7982}
7983
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007984static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7985 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007986{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007987 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007988 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007989 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007990 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007991
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007992 memset(&crtc_state->dpll_hw_state, 0,
7993 sizeof(crtc_state->dpll_hw_state));
7994
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007995 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7996 if (intel_panel_use_ssc(dev_priv)) {
7997 refclk = dev_priv->vbt.lvds_ssc_freq;
7998 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7999 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008000
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008001 limit = &intel_limits_pineview_lvds;
8002 } else {
8003 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008004 }
Jani Nikulaf2335332013-09-13 11:03:09 +03008005
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008006 if (!crtc_state->clock_set &&
8007 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8008 refclk, NULL, &crtc_state->dpll)) {
8009 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8010 return -EINVAL;
8011 }
8012
8013 i9xx_compute_dpll(crtc, crtc_state, NULL);
8014
8015 return 0;
8016}
8017
8018static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8019 struct intel_crtc_state *crtc_state)
8020{
8021 struct drm_device *dev = crtc->base.dev;
8022 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008023 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008024 int refclk = 96000;
8025
8026 memset(&crtc_state->dpll_hw_state, 0,
8027 sizeof(crtc_state->dpll_hw_state));
8028
8029 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8030 if (intel_panel_use_ssc(dev_priv)) {
8031 refclk = dev_priv->vbt.lvds_ssc_freq;
8032 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008033 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008034
8035 limit = &intel_limits_i9xx_lvds;
8036 } else {
8037 limit = &intel_limits_i9xx_sdvo;
8038 }
8039
8040 if (!crtc_state->clock_set &&
8041 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8042 refclk, NULL, &crtc_state->dpll)) {
8043 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8044 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008045 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008046
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008047 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07008048
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008049 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008050}
8051
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008052static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8053 struct intel_crtc_state *crtc_state)
8054{
8055 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008056 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008057
8058 memset(&crtc_state->dpll_hw_state, 0,
8059 sizeof(crtc_state->dpll_hw_state));
8060
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008061 if (!crtc_state->clock_set &&
8062 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8063 refclk, NULL, &crtc_state->dpll)) {
8064 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8065 return -EINVAL;
8066 }
8067
8068 chv_compute_dpll(crtc, crtc_state);
8069
8070 return 0;
8071}
8072
8073static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8074 struct intel_crtc_state *crtc_state)
8075{
8076 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008077 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008078
8079 memset(&crtc_state->dpll_hw_state, 0,
8080 sizeof(crtc_state->dpll_hw_state));
8081
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008082 if (!crtc_state->clock_set &&
8083 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8084 refclk, NULL, &crtc_state->dpll)) {
8085 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8086 return -EINVAL;
8087 }
8088
8089 vlv_compute_dpll(crtc, crtc_state);
8090
8091 return 0;
8092}
8093
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008094static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008095 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008096{
8097 struct drm_device *dev = crtc->base.dev;
8098 struct drm_i915_private *dev_priv = dev->dev_private;
8099 uint32_t tmp;
8100
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008101 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8102 return;
8103
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008104 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008105 if (!(tmp & PFIT_ENABLE))
8106 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008107
Daniel Vetter06922822013-07-11 13:35:40 +02008108 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008109 if (INTEL_INFO(dev)->gen < 4) {
8110 if (crtc->pipe != PIPE_B)
8111 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008112 } else {
8113 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8114 return;
8115 }
8116
Daniel Vetter06922822013-07-11 13:35:40 +02008117 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008118 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008119}
8120
Jesse Barnesacbec812013-09-20 11:29:32 -07008121static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008122 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008123{
8124 struct drm_device *dev = crtc->base.dev;
8125 struct drm_i915_private *dev_priv = dev->dev_private;
8126 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008127 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07008128 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008129 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008130
Ville Syrjäläb5219732016-03-15 16:40:01 +02008131 /* In case of DSI, DPLL will not be used */
8132 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308133 return;
8134
Ville Syrjäläa5805162015-05-26 20:42:30 +03008135 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008136 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008137 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008138
8139 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8140 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8141 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8142 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8143 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8144
Imre Deakdccbea32015-06-22 23:35:51 +03008145 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008146}
8147
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008148static void
8149i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8150 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008151{
8152 struct drm_device *dev = crtc->base.dev;
8153 struct drm_i915_private *dev_priv = dev->dev_private;
8154 u32 val, base, offset;
8155 int pipe = crtc->pipe, plane = crtc->plane;
8156 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008157 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008158 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008159 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008160
Damien Lespiau42a7b082015-02-05 19:35:13 +00008161 val = I915_READ(DSPCNTR(plane));
8162 if (!(val & DISPLAY_PLANE_ENABLE))
8163 return;
8164
Damien Lespiaud9806c92015-01-21 14:07:19 +00008165 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008166 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008167 DRM_DEBUG_KMS("failed to alloc fb\n");
8168 return;
8169 }
8170
Damien Lespiau1b842c82015-01-21 13:50:54 +00008171 fb = &intel_fb->base;
8172
Daniel Vetter18c52472015-02-10 17:16:09 +00008173 if (INTEL_INFO(dev)->gen >= 4) {
8174 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008175 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008176 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8177 }
8178 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008179
8180 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008181 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008182 fb->pixel_format = fourcc;
8183 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008184
8185 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008186 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008187 offset = I915_READ(DSPTILEOFF(plane));
8188 else
8189 offset = I915_READ(DSPLINOFF(plane));
8190 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8191 } else {
8192 base = I915_READ(DSPADDR(plane));
8193 }
8194 plane_config->base = base;
8195
8196 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008197 fb->width = ((val >> 16) & 0xfff) + 1;
8198 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008199
8200 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008201 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008202
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008203 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008204 fb->pixel_format,
8205 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008206
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008207 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008208
Damien Lespiau2844a922015-01-20 12:51:48 +00008209 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8210 pipe_name(pipe), plane, fb->width, fb->height,
8211 fb->bits_per_pixel, base, fb->pitches[0],
8212 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008213
Damien Lespiau2d140302015-02-05 17:22:18 +00008214 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008215}
8216
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008217static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008218 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008219{
8220 struct drm_device *dev = crtc->base.dev;
8221 struct drm_i915_private *dev_priv = dev->dev_private;
8222 int pipe = pipe_config->cpu_transcoder;
8223 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008224 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008225 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008226 int refclk = 100000;
8227
Ville Syrjäläb5219732016-03-15 16:40:01 +02008228 /* In case of DSI, DPLL will not be used */
8229 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8230 return;
8231
Ville Syrjäläa5805162015-05-26 20:42:30 +03008232 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008233 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8234 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8235 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8236 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008237 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008238 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008239
8240 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008241 clock.m2 = (pll_dw0 & 0xff) << 22;
8242 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8243 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008244 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8245 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8246 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8247
Imre Deakdccbea32015-06-22 23:35:51 +03008248 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008249}
8250
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008251static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008252 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008253{
8254 struct drm_device *dev = crtc->base.dev;
8255 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02008256 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008257 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008258 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008259
Imre Deak17290502016-02-12 18:55:11 +02008260 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8261 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008262 return false;
8263
Daniel Vettere143a212013-07-04 12:01:15 +02008264 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008265 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008266
Imre Deak17290502016-02-12 18:55:11 +02008267 ret = false;
8268
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008269 tmp = I915_READ(PIPECONF(crtc->pipe));
8270 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008271 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008272
Wayne Boyer666a4532015-12-09 12:29:35 -08008273 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008274 switch (tmp & PIPECONF_BPC_MASK) {
8275 case PIPECONF_6BPC:
8276 pipe_config->pipe_bpp = 18;
8277 break;
8278 case PIPECONF_8BPC:
8279 pipe_config->pipe_bpp = 24;
8280 break;
8281 case PIPECONF_10BPC:
8282 pipe_config->pipe_bpp = 30;
8283 break;
8284 default:
8285 break;
8286 }
8287 }
8288
Wayne Boyer666a4532015-12-09 12:29:35 -08008289 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8290 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008291 pipe_config->limited_color_range = true;
8292
Ville Syrjälä282740f2013-09-04 18:30:03 +03008293 if (INTEL_INFO(dev)->gen < 4)
8294 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8295
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008296 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008297 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008298
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008299 i9xx_get_pfit_config(crtc, pipe_config);
8300
Daniel Vetter6c49f242013-06-06 12:45:25 +02008301 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008302 /* No way to read it out on pipes B and C */
8303 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8304 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8305 else
8306 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008307 pipe_config->pixel_multiplier =
8308 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8309 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008310 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008311 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8312 tmp = I915_READ(DPLL(crtc->pipe));
8313 pipe_config->pixel_multiplier =
8314 ((tmp & SDVO_MULTIPLIER_MASK)
8315 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8316 } else {
8317 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8318 * port and will be fixed up in the encoder->get_config
8319 * function. */
8320 pipe_config->pixel_multiplier = 1;
8321 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008322 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008323 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008324 /*
8325 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8326 * on 830. Filter it out here so that we don't
8327 * report errors due to that.
8328 */
8329 if (IS_I830(dev))
8330 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8331
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008332 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8333 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008334 } else {
8335 /* Mask out read-only status bits. */
8336 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8337 DPLL_PORTC_READY_MASK |
8338 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008339 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008340
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008341 if (IS_CHERRYVIEW(dev))
8342 chv_crtc_clock_get(crtc, pipe_config);
8343 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008344 vlv_crtc_clock_get(crtc, pipe_config);
8345 else
8346 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008347
Ville Syrjälä0f646142015-08-26 19:39:18 +03008348 /*
8349 * Normally the dotclock is filled in by the encoder .get_config()
8350 * but in case the pipe is enabled w/o any ports we need a sane
8351 * default.
8352 */
8353 pipe_config->base.adjusted_mode.crtc_clock =
8354 pipe_config->port_clock / pipe_config->pixel_multiplier;
8355
Imre Deak17290502016-02-12 18:55:11 +02008356 ret = true;
8357
8358out:
8359 intel_display_power_put(dev_priv, power_domain);
8360
8361 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008362}
8363
Paulo Zanonidde86e22012-12-01 12:04:25 -02008364static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008365{
8366 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008367 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04008368 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008369 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008370 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008371 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008372 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008373 bool has_ck505 = false;
8374 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04008375 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008376
8377 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008378 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008379 switch (encoder->type) {
8380 case INTEL_OUTPUT_LVDS:
8381 has_panel = true;
8382 has_lvds = true;
8383 break;
8384 case INTEL_OUTPUT_EDP:
8385 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008386 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008387 has_cpu_edp = true;
8388 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008389 default:
8390 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008391 }
8392 }
8393
Keith Packard99eb6a02011-09-26 14:29:12 -07008394 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008395 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008396 can_ssc = has_ck505;
8397 } else {
8398 has_ck505 = false;
8399 can_ssc = true;
8400 }
8401
Lyude1c1a24d2016-06-14 11:04:09 -04008402 /* Check if any DPLLs are using the SSC source */
8403 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8404 u32 temp = I915_READ(PCH_DPLL(i));
8405
8406 if (!(temp & DPLL_VCO_ENABLE))
8407 continue;
8408
8409 if ((temp & PLL_REF_INPUT_MASK) ==
8410 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8411 using_ssc_source = true;
8412 break;
8413 }
8414 }
8415
8416 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8417 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008418
8419 /* Ironlake: try to setup display ref clock before DPLL
8420 * enabling. This is only under driver's control after
8421 * PCH B stepping, previous chipset stepping should be
8422 * ignoring this setting.
8423 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008424 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008425
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008426 /* As we must carefully and slowly disable/enable each source in turn,
8427 * compute the final state we want first and check if we need to
8428 * make any changes at all.
8429 */
8430 final = val;
8431 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008432 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008433 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008434 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008435 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8436
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008437 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008438 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008439 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008440
Keith Packard199e5d72011-09-22 12:01:57 -07008441 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008442 final |= DREF_SSC_SOURCE_ENABLE;
8443
8444 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8445 final |= DREF_SSC1_ENABLE;
8446
8447 if (has_cpu_edp) {
8448 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8449 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8450 else
8451 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8452 } else
8453 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04008454 } else if (using_ssc_source) {
8455 final |= DREF_SSC_SOURCE_ENABLE;
8456 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008457 }
8458
8459 if (final == val)
8460 return;
8461
8462 /* Always enable nonspread source */
8463 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8464
8465 if (has_ck505)
8466 val |= DREF_NONSPREAD_CK505_ENABLE;
8467 else
8468 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8469
8470 if (has_panel) {
8471 val &= ~DREF_SSC_SOURCE_MASK;
8472 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008473
Keith Packard199e5d72011-09-22 12:01:57 -07008474 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008475 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008476 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008477 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008478 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008479 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008480
8481 /* Get SSC going before enabling the outputs */
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
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008486 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008487
8488 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008489 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008490 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008491 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008492 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008493 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008494 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008495 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008496 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008497
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008498 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008499 POSTING_READ(PCH_DREF_CONTROL);
8500 udelay(200);
8501 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04008502 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008503
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008504 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008505
8506 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008507 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008508
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008509 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008510 POSTING_READ(PCH_DREF_CONTROL);
8511 udelay(200);
8512
Lyude1c1a24d2016-06-14 11:04:09 -04008513 if (!using_ssc_source) {
8514 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008515
Lyude1c1a24d2016-06-14 11:04:09 -04008516 /* Turn off the SSC source */
8517 val &= ~DREF_SSC_SOURCE_MASK;
8518 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008519
Lyude1c1a24d2016-06-14 11:04:09 -04008520 /* Turn off SSC1 */
8521 val &= ~DREF_SSC1_ENABLE;
8522
8523 I915_WRITE(PCH_DREF_CONTROL, val);
8524 POSTING_READ(PCH_DREF_CONTROL);
8525 udelay(200);
8526 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07008527 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008528
8529 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008530}
8531
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008532static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008533{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008534 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008535
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008536 tmp = I915_READ(SOUTH_CHICKEN2);
8537 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8538 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008539
Imre Deakcf3598c2016-06-28 13:37:31 +03008540 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8541 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008542 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008543
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008544 tmp = I915_READ(SOUTH_CHICKEN2);
8545 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8546 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008547
Imre Deakcf3598c2016-06-28 13:37:31 +03008548 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8549 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008550 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008551}
8552
8553/* WaMPhyProgramming:hsw */
8554static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8555{
8556 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008557
8558 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8559 tmp &= ~(0xFF << 24);
8560 tmp |= (0x12 << 24);
8561 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8562
Paulo Zanonidde86e22012-12-01 12:04:25 -02008563 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8564 tmp |= (1 << 11);
8565 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8566
8567 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8568 tmp |= (1 << 11);
8569 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8570
Paulo Zanonidde86e22012-12-01 12:04:25 -02008571 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8572 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8573 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8574
8575 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8576 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8577 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8578
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008579 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8580 tmp &= ~(7 << 13);
8581 tmp |= (5 << 13);
8582 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008583
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008584 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8585 tmp &= ~(7 << 13);
8586 tmp |= (5 << 13);
8587 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008588
8589 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8590 tmp &= ~0xFF;
8591 tmp |= 0x1C;
8592 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8593
8594 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8595 tmp &= ~0xFF;
8596 tmp |= 0x1C;
8597 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8598
8599 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8600 tmp &= ~(0xFF << 16);
8601 tmp |= (0x1C << 16);
8602 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8603
8604 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8605 tmp &= ~(0xFF << 16);
8606 tmp |= (0x1C << 16);
8607 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8608
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008609 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8610 tmp |= (1 << 27);
8611 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008612
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008613 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8614 tmp |= (1 << 27);
8615 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008616
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008617 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8618 tmp &= ~(0xF << 28);
8619 tmp |= (4 << 28);
8620 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008621
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008622 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8623 tmp &= ~(0xF << 28);
8624 tmp |= (4 << 28);
8625 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008626}
8627
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008628/* Implements 3 different sequences from BSpec chapter "Display iCLK
8629 * Programming" based on the parameters passed:
8630 * - Sequence to enable CLKOUT_DP
8631 * - Sequence to enable CLKOUT_DP without spread
8632 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8633 */
8634static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8635 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008636{
8637 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008638 uint32_t reg, tmp;
8639
8640 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8641 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008642 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008643 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008644
Ville Syrjäläa5805162015-05-26 20:42:30 +03008645 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008646
8647 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8648 tmp &= ~SBI_SSCCTL_DISABLE;
8649 tmp |= SBI_SSCCTL_PATHALT;
8650 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8651
8652 udelay(24);
8653
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008654 if (with_spread) {
8655 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8656 tmp &= ~SBI_SSCCTL_PATHALT;
8657 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008658
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008659 if (with_fdi) {
8660 lpt_reset_fdi_mphy(dev_priv);
8661 lpt_program_fdi_mphy(dev_priv);
8662 }
8663 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008664
Ville Syrjäläc2699522015-08-27 23:55:59 +03008665 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -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);
Daniel Vetterc00db242013-01-22 15:33:27 +01008669
Ville Syrjäläa5805162015-05-26 20:42:30 +03008670 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008671}
8672
Paulo Zanoni47701c32013-07-23 11:19:25 -03008673/* Sequence to disable CLKOUT_DP */
8674static void lpt_disable_clkout_dp(struct drm_device *dev)
8675{
8676 struct drm_i915_private *dev_priv = dev->dev_private;
8677 uint32_t reg, tmp;
8678
Ville Syrjäläa5805162015-05-26 20:42:30 +03008679 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008680
Ville Syrjäläc2699522015-08-27 23:55:59 +03008681 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008682 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8683 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8684 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8685
8686 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8687 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8688 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8689 tmp |= SBI_SSCCTL_PATHALT;
8690 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8691 udelay(32);
8692 }
8693 tmp |= SBI_SSCCTL_DISABLE;
8694 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8695 }
8696
Ville Syrjäläa5805162015-05-26 20:42:30 +03008697 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008698}
8699
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008700#define BEND_IDX(steps) ((50 + (steps)) / 5)
8701
8702static const uint16_t sscdivintphase[] = {
8703 [BEND_IDX( 50)] = 0x3B23,
8704 [BEND_IDX( 45)] = 0x3B23,
8705 [BEND_IDX( 40)] = 0x3C23,
8706 [BEND_IDX( 35)] = 0x3C23,
8707 [BEND_IDX( 30)] = 0x3D23,
8708 [BEND_IDX( 25)] = 0x3D23,
8709 [BEND_IDX( 20)] = 0x3E23,
8710 [BEND_IDX( 15)] = 0x3E23,
8711 [BEND_IDX( 10)] = 0x3F23,
8712 [BEND_IDX( 5)] = 0x3F23,
8713 [BEND_IDX( 0)] = 0x0025,
8714 [BEND_IDX( -5)] = 0x0025,
8715 [BEND_IDX(-10)] = 0x0125,
8716 [BEND_IDX(-15)] = 0x0125,
8717 [BEND_IDX(-20)] = 0x0225,
8718 [BEND_IDX(-25)] = 0x0225,
8719 [BEND_IDX(-30)] = 0x0325,
8720 [BEND_IDX(-35)] = 0x0325,
8721 [BEND_IDX(-40)] = 0x0425,
8722 [BEND_IDX(-45)] = 0x0425,
8723 [BEND_IDX(-50)] = 0x0525,
8724};
8725
8726/*
8727 * Bend CLKOUT_DP
8728 * steps -50 to 50 inclusive, in steps of 5
8729 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8730 * change in clock period = -(steps / 10) * 5.787 ps
8731 */
8732static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8733{
8734 uint32_t tmp;
8735 int idx = BEND_IDX(steps);
8736
8737 if (WARN_ON(steps % 5 != 0))
8738 return;
8739
8740 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8741 return;
8742
8743 mutex_lock(&dev_priv->sb_lock);
8744
8745 if (steps % 10 != 0)
8746 tmp = 0xAAAAAAAB;
8747 else
8748 tmp = 0x00000000;
8749 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8750
8751 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8752 tmp &= 0xffff0000;
8753 tmp |= sscdivintphase[idx];
8754 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8755
8756 mutex_unlock(&dev_priv->sb_lock);
8757}
8758
8759#undef BEND_IDX
8760
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008761static void lpt_init_pch_refclk(struct drm_device *dev)
8762{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008763 struct intel_encoder *encoder;
8764 bool has_vga = false;
8765
Damien Lespiaub2784e12014-08-05 11:29:37 +01008766 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008767 switch (encoder->type) {
8768 case INTEL_OUTPUT_ANALOG:
8769 has_vga = true;
8770 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008771 default:
8772 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008773 }
8774 }
8775
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008776 if (has_vga) {
8777 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008778 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008779 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008780 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008781 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008782}
8783
Paulo Zanonidde86e22012-12-01 12:04:25 -02008784/*
8785 * Initialize reference clocks when the driver loads
8786 */
8787void intel_init_pch_refclk(struct drm_device *dev)
8788{
8789 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8790 ironlake_init_pch_refclk(dev);
8791 else if (HAS_PCH_LPT(dev))
8792 lpt_init_pch_refclk(dev);
8793}
8794
Daniel Vetter6ff93602013-04-19 11:24:36 +02008795static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008796{
8797 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8798 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8799 int pipe = intel_crtc->pipe;
8800 uint32_t val;
8801
Daniel Vetter78114072013-06-13 00:54:57 +02008802 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008803
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008804 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008805 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008806 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008807 break;
8808 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008809 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008810 break;
8811 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008812 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008813 break;
8814 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008815 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008816 break;
8817 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008818 /* Case prevented by intel_choose_pipe_bpp_dither. */
8819 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008820 }
8821
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008822 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008823 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8824
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008825 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008826 val |= PIPECONF_INTERLACED_ILK;
8827 else
8828 val |= PIPECONF_PROGRESSIVE;
8829
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008830 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008831 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008832
Paulo Zanonic8203562012-09-12 10:06:29 -03008833 I915_WRITE(PIPECONF(pipe), val);
8834 POSTING_READ(PIPECONF(pipe));
8835}
8836
Daniel Vetter6ff93602013-04-19 11:24:36 +02008837static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008838{
Jani Nikula391bf042016-03-18 17:05:40 +02008839 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008840 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008841 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008842 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008843
Jani Nikula391bf042016-03-18 17:05:40 +02008844 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008845 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8846
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008847 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008848 val |= PIPECONF_INTERLACED_ILK;
8849 else
8850 val |= PIPECONF_PROGRESSIVE;
8851
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008852 I915_WRITE(PIPECONF(cpu_transcoder), val);
8853 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008854}
8855
Jani Nikula391bf042016-03-18 17:05:40 +02008856static void haswell_set_pipemisc(struct drm_crtc *crtc)
8857{
8858 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8860
8861 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8862 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008863
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008864 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008865 case 18:
8866 val |= PIPEMISC_DITHER_6_BPC;
8867 break;
8868 case 24:
8869 val |= PIPEMISC_DITHER_8_BPC;
8870 break;
8871 case 30:
8872 val |= PIPEMISC_DITHER_10_BPC;
8873 break;
8874 case 36:
8875 val |= PIPEMISC_DITHER_12_BPC;
8876 break;
8877 default:
8878 /* Case prevented by pipe_config_set_bpp. */
8879 BUG();
8880 }
8881
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008882 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008883 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8884
Jani Nikula391bf042016-03-18 17:05:40 +02008885 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008886 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008887}
8888
Paulo Zanonid4b19312012-11-29 11:29:32 -02008889int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8890{
8891 /*
8892 * Account for spread spectrum to avoid
8893 * oversubscribing the link. Max center spread
8894 * is 2.5%; use 5% for safety's sake.
8895 */
8896 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008897 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008898}
8899
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008900static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008901{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008902 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008903}
8904
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008905static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8906 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008907 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008908{
8909 struct drm_crtc *crtc = &intel_crtc->base;
8910 struct drm_device *dev = crtc->dev;
8911 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008912 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008913 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008914 struct drm_connector_state *connector_state;
8915 struct intel_encoder *encoder;
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008916 u32 dpll, fp, fp2;
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008917 int factor, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008918 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008919
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008920 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008921 if (connector_state->crtc != crtc_state->base.crtc)
8922 continue;
8923
8924 encoder = to_intel_encoder(connector_state->best_encoder);
8925
8926 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008927 case INTEL_OUTPUT_LVDS:
8928 is_lvds = true;
8929 break;
8930 case INTEL_OUTPUT_SDVO:
8931 case INTEL_OUTPUT_HDMI:
8932 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008933 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008934 default:
8935 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008936 }
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008937 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008938
Chris Wilsonc1858122010-12-03 21:35:48 +00008939 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008940 factor = 21;
8941 if (is_lvds) {
8942 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008943 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008944 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008945 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008946 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008947 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008948
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008949 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008950
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008951 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8952 fp |= FP_CB_TUNE;
8953
8954 if (reduced_clock) {
8955 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8956
8957 if (reduced_clock->m < factor * reduced_clock->n)
8958 fp2 |= FP_CB_TUNE;
8959 } else {
8960 fp2 = fp;
8961 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008962
Chris Wilson5eddb702010-09-11 13:48:45 +01008963 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008964
Eric Anholta07d6782011-03-30 13:01:08 -07008965 if (is_lvds)
8966 dpll |= DPLLB_MODE_LVDS;
8967 else
8968 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008969
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008970 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008971 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008972
8973 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008974 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008975 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008976 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008977
Eric Anholta07d6782011-03-30 13:01:08 -07008978 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008979 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008980 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008981 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008982
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008983 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008984 case 5:
8985 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8986 break;
8987 case 7:
8988 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8989 break;
8990 case 10:
8991 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8992 break;
8993 case 14:
8994 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8995 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008996 }
8997
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02008998 if (is_lvds && intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008999 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08009000 else
9001 dpll |= PLL_REF_INPUT_DREFCLK;
9002
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009003 dpll |= DPLL_VCO_ENABLE;
9004
9005 crtc_state->dpll_hw_state.dpll = dpll;
9006 crtc_state->dpll_hw_state.fp0 = fp;
9007 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03009008}
9009
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009010static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9011 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08009012{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009013 struct drm_device *dev = crtc->base.dev;
9014 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03009015 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02009016 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009017 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03009018 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009019 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08009020
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03009021 memset(&crtc_state->dpll_hw_state, 0,
9022 sizeof(crtc_state->dpll_hw_state));
9023
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009024 crtc->lowfreq_avail = false;
9025
9026 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9027 if (!crtc_state->has_pch_encoder)
9028 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009029
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009030 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9031 if (intel_panel_use_ssc(dev_priv)) {
9032 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9033 dev_priv->vbt.lvds_ssc_freq);
9034 refclk = dev_priv->vbt.lvds_ssc_freq;
9035 }
9036
9037 if (intel_is_dual_link_lvds(dev)) {
9038 if (refclk == 100000)
9039 limit = &intel_limits_ironlake_dual_lvds_100m;
9040 else
9041 limit = &intel_limits_ironlake_dual_lvds;
9042 } else {
9043 if (refclk == 100000)
9044 limit = &intel_limits_ironlake_single_lvds_100m;
9045 else
9046 limit = &intel_limits_ironlake_single_lvds;
9047 }
9048 } else {
9049 limit = &intel_limits_ironlake_dac;
9050 }
9051
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009052 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02009053 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9054 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009055 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9056 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009057 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009058
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009059 ironlake_compute_dpll(crtc, crtc_state,
9060 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009061
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009062 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9063 if (pll == NULL) {
9064 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9065 pipe_name(crtc->pipe));
9066 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009067 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009068
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009069 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9070 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009071 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02009072
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009073 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009074}
9075
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009076static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9077 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009078{
9079 struct drm_device *dev = crtc->base.dev;
9080 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009081 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009082
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009083 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9084 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9085 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9086 & ~TU_SIZE_MASK;
9087 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9088 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9089 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9090}
9091
9092static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9093 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009094 struct intel_link_m_n *m_n,
9095 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009096{
9097 struct drm_device *dev = crtc->base.dev;
9098 struct drm_i915_private *dev_priv = dev->dev_private;
9099 enum pipe pipe = crtc->pipe;
9100
9101 if (INTEL_INFO(dev)->gen >= 5) {
9102 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9103 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9104 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9105 & ~TU_SIZE_MASK;
9106 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9107 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9108 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009109 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9110 * gen < 8) and if DRRS is supported (to make sure the
9111 * registers are not unnecessarily read).
9112 */
9113 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009114 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009115 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9116 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9117 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9118 & ~TU_SIZE_MASK;
9119 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9120 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9121 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9122 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009123 } else {
9124 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9125 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9126 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9127 & ~TU_SIZE_MASK;
9128 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9129 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9130 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9131 }
9132}
9133
9134void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009135 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009136{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009137 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009138 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9139 else
9140 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009141 &pipe_config->dp_m_n,
9142 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009143}
9144
Daniel Vetter72419202013-04-04 13:28:53 +02009145static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009146 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009147{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009148 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009149 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009150}
9151
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009152static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009153 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009154{
9155 struct drm_device *dev = crtc->base.dev;
9156 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009157 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9158 uint32_t ps_ctrl = 0;
9159 int id = -1;
9160 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009161
Chandra Kondurua1b22782015-04-07 15:28:45 -07009162 /* find scaler attached to this pipe */
9163 for (i = 0; i < crtc->num_scalers; i++) {
9164 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9165 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9166 id = i;
9167 pipe_config->pch_pfit.enabled = true;
9168 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9169 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9170 break;
9171 }
9172 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009173
Chandra Kondurua1b22782015-04-07 15:28:45 -07009174 scaler_state->scaler_id = id;
9175 if (id >= 0) {
9176 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9177 } else {
9178 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009179 }
9180}
9181
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009182static void
9183skylake_get_initial_plane_config(struct intel_crtc *crtc,
9184 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009185{
9186 struct drm_device *dev = crtc->base.dev;
9187 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009188 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009189 int pipe = crtc->pipe;
9190 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009191 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009192 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009193 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009194
Damien Lespiaud9806c92015-01-21 14:07:19 +00009195 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009196 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009197 DRM_DEBUG_KMS("failed to alloc fb\n");
9198 return;
9199 }
9200
Damien Lespiau1b842c82015-01-21 13:50:54 +00009201 fb = &intel_fb->base;
9202
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009203 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009204 if (!(val & PLANE_CTL_ENABLE))
9205 goto error;
9206
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009207 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9208 fourcc = skl_format_to_fourcc(pixel_format,
9209 val & PLANE_CTL_ORDER_RGBX,
9210 val & PLANE_CTL_ALPHA_MASK);
9211 fb->pixel_format = fourcc;
9212 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9213
Damien Lespiau40f46282015-02-27 11:15:21 +00009214 tiling = val & PLANE_CTL_TILED_MASK;
9215 switch (tiling) {
9216 case PLANE_CTL_TILED_LINEAR:
9217 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9218 break;
9219 case PLANE_CTL_TILED_X:
9220 plane_config->tiling = I915_TILING_X;
9221 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9222 break;
9223 case PLANE_CTL_TILED_Y:
9224 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9225 break;
9226 case PLANE_CTL_TILED_YF:
9227 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9228 break;
9229 default:
9230 MISSING_CASE(tiling);
9231 goto error;
9232 }
9233
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009234 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9235 plane_config->base = base;
9236
9237 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9238
9239 val = I915_READ(PLANE_SIZE(pipe, 0));
9240 fb->height = ((val >> 16) & 0xfff) + 1;
9241 fb->width = ((val >> 0) & 0x1fff) + 1;
9242
9243 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009244 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009245 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009246 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9247
9248 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009249 fb->pixel_format,
9250 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009251
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009252 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009253
9254 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9255 pipe_name(pipe), fb->width, fb->height,
9256 fb->bits_per_pixel, base, fb->pitches[0],
9257 plane_config->size);
9258
Damien Lespiau2d140302015-02-05 17:22:18 +00009259 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009260 return;
9261
9262error:
9263 kfree(fb);
9264}
9265
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009266static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009267 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009268{
9269 struct drm_device *dev = crtc->base.dev;
9270 struct drm_i915_private *dev_priv = dev->dev_private;
9271 uint32_t tmp;
9272
9273 tmp = I915_READ(PF_CTL(crtc->pipe));
9274
9275 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009276 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009277 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9278 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009279
9280 /* We currently do not free assignements of panel fitters on
9281 * ivb/hsw (since we don't use the higher upscaling modes which
9282 * differentiates them) so just WARN about this case for now. */
9283 if (IS_GEN7(dev)) {
9284 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9285 PF_PIPE_SEL_IVB(crtc->pipe));
9286 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009287 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009288}
9289
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009290static void
9291ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9292 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009293{
9294 struct drm_device *dev = crtc->base.dev;
9295 struct drm_i915_private *dev_priv = dev->dev_private;
9296 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009297 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009298 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009299 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009300 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009301 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009302
Damien Lespiau42a7b082015-02-05 19:35:13 +00009303 val = I915_READ(DSPCNTR(pipe));
9304 if (!(val & DISPLAY_PLANE_ENABLE))
9305 return;
9306
Damien Lespiaud9806c92015-01-21 14:07:19 +00009307 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009308 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009309 DRM_DEBUG_KMS("failed to alloc fb\n");
9310 return;
9311 }
9312
Damien Lespiau1b842c82015-01-21 13:50:54 +00009313 fb = &intel_fb->base;
9314
Daniel Vetter18c52472015-02-10 17:16:09 +00009315 if (INTEL_INFO(dev)->gen >= 4) {
9316 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009317 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009318 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9319 }
9320 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009321
9322 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009323 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009324 fb->pixel_format = fourcc;
9325 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009326
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009327 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009328 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009329 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009330 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009331 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009332 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009333 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009334 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009335 }
9336 plane_config->base = base;
9337
9338 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009339 fb->width = ((val >> 16) & 0xfff) + 1;
9340 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009341
9342 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009343 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009344
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009345 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009346 fb->pixel_format,
9347 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009348
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009349 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009350
Damien Lespiau2844a922015-01-20 12:51:48 +00009351 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9352 pipe_name(pipe), fb->width, fb->height,
9353 fb->bits_per_pixel, base, fb->pitches[0],
9354 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009355
Damien Lespiau2d140302015-02-05 17:22:18 +00009356 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009357}
9358
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009359static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009360 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009361{
9362 struct drm_device *dev = crtc->base.dev;
9363 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +02009364 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009365 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009366 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009367
Imre Deak17290502016-02-12 18:55:11 +02009368 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9369 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009370 return false;
9371
Daniel Vettere143a212013-07-04 12:01:15 +02009372 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009373 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009374
Imre Deak17290502016-02-12 18:55:11 +02009375 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009376 tmp = I915_READ(PIPECONF(crtc->pipe));
9377 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009378 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009379
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009380 switch (tmp & PIPECONF_BPC_MASK) {
9381 case PIPECONF_6BPC:
9382 pipe_config->pipe_bpp = 18;
9383 break;
9384 case PIPECONF_8BPC:
9385 pipe_config->pipe_bpp = 24;
9386 break;
9387 case PIPECONF_10BPC:
9388 pipe_config->pipe_bpp = 30;
9389 break;
9390 case PIPECONF_12BPC:
9391 pipe_config->pipe_bpp = 36;
9392 break;
9393 default:
9394 break;
9395 }
9396
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009397 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9398 pipe_config->limited_color_range = true;
9399
Daniel Vetterab9412b2013-05-03 11:49:46 +02009400 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009401 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009402 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009403
Daniel Vetter88adfff2013-03-28 10:42:01 +01009404 pipe_config->has_pch_encoder = true;
9405
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009406 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9407 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9408 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009409
9410 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009411
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009412 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03009413 /*
9414 * The pipe->pch transcoder and pch transcoder->pll
9415 * mapping is fixed.
9416 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009417 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009418 } else {
9419 tmp = I915_READ(PCH_DPLL_SEL);
9420 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009421 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009422 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009423 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009424 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009425
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009426 pipe_config->shared_dpll =
9427 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9428 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009429
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009430 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9431 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009432
9433 tmp = pipe_config->dpll_hw_state.dpll;
9434 pipe_config->pixel_multiplier =
9435 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9436 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009437
9438 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009439 } else {
9440 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009441 }
9442
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009443 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009444 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009445
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009446 ironlake_get_pfit_config(crtc, pipe_config);
9447
Imre Deak17290502016-02-12 18:55:11 +02009448 ret = true;
9449
9450out:
9451 intel_display_power_put(dev_priv, power_domain);
9452
9453 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009454}
9455
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009456static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9457{
9458 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009459 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009460
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009461 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009462 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009463 pipe_name(crtc->pipe));
9464
Rob Clarke2c719b2014-12-15 13:56:32 -05009465 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9466 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009467 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9468 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009469 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9470 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009471 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009472 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009473 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009474 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009475 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009476 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009477 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009478 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009479 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009480
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009481 /*
9482 * In theory we can still leave IRQs enabled, as long as only the HPD
9483 * interrupts remain enabled. We used to check for that, but since it's
9484 * gen-specific and since we only disable LCPLL after we fully disable
9485 * the interrupts, the check below should be enough.
9486 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009487 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009488}
9489
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009490static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9491{
9492 struct drm_device *dev = dev_priv->dev;
9493
9494 if (IS_HASWELL(dev))
9495 return I915_READ(D_COMP_HSW);
9496 else
9497 return I915_READ(D_COMP_BDW);
9498}
9499
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009500static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9501{
9502 struct drm_device *dev = dev_priv->dev;
9503
9504 if (IS_HASWELL(dev)) {
9505 mutex_lock(&dev_priv->rps.hw_lock);
9506 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9507 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009508 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009509 mutex_unlock(&dev_priv->rps.hw_lock);
9510 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009511 I915_WRITE(D_COMP_BDW, val);
9512 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009513 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009514}
9515
9516/*
9517 * This function implements pieces of two sequences from BSpec:
9518 * - Sequence for display software to disable LCPLL
9519 * - Sequence for display software to allow package C8+
9520 * The steps implemented here are just the steps that actually touch the LCPLL
9521 * register. Callers should take care of disabling all the display engine
9522 * functions, doing the mode unset, fixing interrupts, etc.
9523 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009524static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9525 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009526{
9527 uint32_t val;
9528
9529 assert_can_disable_lcpll(dev_priv);
9530
9531 val = I915_READ(LCPLL_CTL);
9532
9533 if (switch_to_fclk) {
9534 val |= LCPLL_CD_SOURCE_FCLK;
9535 I915_WRITE(LCPLL_CTL, val);
9536
Imre Deakf53dd632016-06-28 13:37:32 +03009537 if (wait_for_us(I915_READ(LCPLL_CTL) &
9538 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009539 DRM_ERROR("Switching to FCLK failed\n");
9540
9541 val = I915_READ(LCPLL_CTL);
9542 }
9543
9544 val |= LCPLL_PLL_DISABLE;
9545 I915_WRITE(LCPLL_CTL, val);
9546 POSTING_READ(LCPLL_CTL);
9547
9548 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9549 DRM_ERROR("LCPLL still locked\n");
9550
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009551 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009552 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009553 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009554 ndelay(100);
9555
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009556 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9557 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009558 DRM_ERROR("D_COMP RCOMP still in progress\n");
9559
9560 if (allow_power_down) {
9561 val = I915_READ(LCPLL_CTL);
9562 val |= LCPLL_POWER_DOWN_ALLOW;
9563 I915_WRITE(LCPLL_CTL, val);
9564 POSTING_READ(LCPLL_CTL);
9565 }
9566}
9567
9568/*
9569 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9570 * source.
9571 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009572static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009573{
9574 uint32_t val;
9575
9576 val = I915_READ(LCPLL_CTL);
9577
9578 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9579 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9580 return;
9581
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009582 /*
9583 * Make sure we're not on PC8 state before disabling PC8, otherwise
9584 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009585 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009586 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009587
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009588 if (val & LCPLL_POWER_DOWN_ALLOW) {
9589 val &= ~LCPLL_POWER_DOWN_ALLOW;
9590 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009591 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009592 }
9593
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009594 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009595 val |= D_COMP_COMP_FORCE;
9596 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009597 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009598
9599 val = I915_READ(LCPLL_CTL);
9600 val &= ~LCPLL_PLL_DISABLE;
9601 I915_WRITE(LCPLL_CTL, val);
9602
9603 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9604 DRM_ERROR("LCPLL not locked yet\n");
9605
9606 if (val & LCPLL_CD_SOURCE_FCLK) {
9607 val = I915_READ(LCPLL_CTL);
9608 val &= ~LCPLL_CD_SOURCE_FCLK;
9609 I915_WRITE(LCPLL_CTL, val);
9610
Imre Deakf53dd632016-06-28 13:37:32 +03009611 if (wait_for_us((I915_READ(LCPLL_CTL) &
9612 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009613 DRM_ERROR("Switching back to LCPLL failed\n");
9614 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009615
Mika Kuoppala59bad942015-01-16 11:34:40 +02009616 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009617 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009618}
9619
Paulo Zanoni765dab672014-03-07 20:08:18 -03009620/*
9621 * Package states C8 and deeper are really deep PC states that can only be
9622 * reached when all the devices on the system allow it, so even if the graphics
9623 * device allows PC8+, it doesn't mean the system will actually get to these
9624 * states. Our driver only allows PC8+ when going into runtime PM.
9625 *
9626 * The requirements for PC8+ are that all the outputs are disabled, the power
9627 * well is disabled and most interrupts are disabled, and these are also
9628 * requirements for runtime PM. When these conditions are met, we manually do
9629 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9630 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9631 * hang the machine.
9632 *
9633 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9634 * the state of some registers, so when we come back from PC8+ we need to
9635 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9636 * need to take care of the registers kept by RC6. Notice that this happens even
9637 * if we don't put the device in PCI D3 state (which is what currently happens
9638 * because of the runtime PM support).
9639 *
9640 * For more, read "Display Sequences for Package C8" on the hardware
9641 * documentation.
9642 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009643void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009644{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009645 struct drm_device *dev = dev_priv->dev;
9646 uint32_t val;
9647
Paulo Zanonic67a4702013-08-19 13:18:09 -03009648 DRM_DEBUG_KMS("Enabling package C8+\n");
9649
Ville Syrjäläc2699522015-08-27 23:55:59 +03009650 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009651 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9652 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9653 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9654 }
9655
9656 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009657 hsw_disable_lcpll(dev_priv, true, true);
9658}
9659
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009660void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009661{
9662 struct drm_device *dev = dev_priv->dev;
9663 uint32_t val;
9664
Paulo Zanonic67a4702013-08-19 13:18:09 -03009665 DRM_DEBUG_KMS("Disabling package C8+\n");
9666
9667 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009668 lpt_init_pch_refclk(dev);
9669
Ville Syrjäläc2699522015-08-27 23:55:59 +03009670 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009671 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9672 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9673 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9674 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009675}
9676
Imre Deak324513c2016-06-13 16:44:36 +03009677static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309678{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009679 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009680 struct intel_atomic_state *old_intel_state =
9681 to_intel_atomic_state(old_state);
9682 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309683
Imre Deak324513c2016-06-13 16:44:36 +03009684 bxt_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309685}
9686
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009687/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009688static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009689{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009690 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9691 struct drm_i915_private *dev_priv = state->dev->dev_private;
9692 struct drm_crtc *crtc;
9693 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009694 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009695 unsigned max_pixel_rate = 0, i;
9696 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009697
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009698 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9699 sizeof(intel_state->min_pixclk));
9700
9701 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009702 int pixel_rate;
9703
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009704 crtc_state = to_intel_crtc_state(cstate);
9705 if (!crtc_state->base.enable) {
9706 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009707 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009708 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009709
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009710 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009711
9712 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009713 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009714 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9715
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009716 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009717 }
9718
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009719 for_each_pipe(dev_priv, pipe)
9720 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9721
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009722 return max_pixel_rate;
9723}
9724
9725static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9726{
9727 struct drm_i915_private *dev_priv = dev->dev_private;
9728 uint32_t val, data;
9729 int ret;
9730
9731 if (WARN((I915_READ(LCPLL_CTL) &
9732 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9733 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9734 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9735 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9736 "trying to change cdclk frequency with cdclk not enabled\n"))
9737 return;
9738
9739 mutex_lock(&dev_priv->rps.hw_lock);
9740 ret = sandybridge_pcode_write(dev_priv,
9741 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9742 mutex_unlock(&dev_priv->rps.hw_lock);
9743 if (ret) {
9744 DRM_ERROR("failed to inform pcode about cdclk change\n");
9745 return;
9746 }
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, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009754 DRM_ERROR("Switching to FCLK failed\n");
9755
9756 val = I915_READ(LCPLL_CTL);
9757 val &= ~LCPLL_CLK_FREQ_MASK;
9758
9759 switch (cdclk) {
9760 case 450000:
9761 val |= LCPLL_CLK_FREQ_450;
9762 data = 0;
9763 break;
9764 case 540000:
9765 val |= LCPLL_CLK_FREQ_54O_BDW;
9766 data = 1;
9767 break;
9768 case 337500:
9769 val |= LCPLL_CLK_FREQ_337_5_BDW;
9770 data = 2;
9771 break;
9772 case 675000:
9773 val |= LCPLL_CLK_FREQ_675_BDW;
9774 data = 3;
9775 break;
9776 default:
9777 WARN(1, "invalid cdclk frequency\n");
9778 return;
9779 }
9780
9781 I915_WRITE(LCPLL_CTL, val);
9782
9783 val = I915_READ(LCPLL_CTL);
9784 val &= ~LCPLL_CD_SOURCE_FCLK;
9785 I915_WRITE(LCPLL_CTL, val);
9786
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009787 if (wait_for_us((I915_READ(LCPLL_CTL) &
9788 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009789 DRM_ERROR("Switching back to LCPLL failed\n");
9790
9791 mutex_lock(&dev_priv->rps.hw_lock);
9792 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9793 mutex_unlock(&dev_priv->rps.hw_lock);
9794
Ville Syrjälä7f1052a2016-04-26 19:46:32 +03009795 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9796
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009797 intel_update_cdclk(dev);
9798
9799 WARN(cdclk != dev_priv->cdclk_freq,
9800 "cdclk requested %d kHz but got %d kHz\n",
9801 cdclk, dev_priv->cdclk_freq);
9802}
9803
Ville Syrjälä587c7912016-05-11 22:44:41 +03009804static int broadwell_calc_cdclk(int max_pixclk)
9805{
9806 if (max_pixclk > 540000)
9807 return 675000;
9808 else if (max_pixclk > 450000)
9809 return 540000;
9810 else if (max_pixclk > 337500)
9811 return 450000;
9812 else
9813 return 337500;
9814}
9815
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009816static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009817{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009818 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009819 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009820 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009821 int cdclk;
9822
9823 /*
9824 * FIXME should also account for plane ratio
9825 * once 64bpp pixel formats are supported.
9826 */
Ville Syrjälä587c7912016-05-11 22:44:41 +03009827 cdclk = broadwell_calc_cdclk(max_pixclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009828
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009829 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009830 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9831 cdclk, dev_priv->max_cdclk_freq);
9832 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009833 }
9834
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009835 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9836 if (!intel_state->active_crtcs)
Ville Syrjälä587c7912016-05-11 22:44:41 +03009837 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009838
9839 return 0;
9840}
9841
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009842static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009843{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009844 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009845 struct intel_atomic_state *old_intel_state =
9846 to_intel_atomic_state(old_state);
9847 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009848
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009849 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009850}
9851
Clint Taylorc89e39f2016-05-13 23:41:21 +03009852static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9853{
9854 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9855 struct drm_i915_private *dev_priv = to_i915(state->dev);
9856 const int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009857 int vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009858 int cdclk;
9859
9860 /*
9861 * FIXME should also account for plane ratio
9862 * once 64bpp pixel formats are supported.
9863 */
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009864 cdclk = skl_calc_cdclk(max_pixclk, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009865
9866 /*
9867 * FIXME move the cdclk caclulation to
9868 * compute_config() so we can fail gracegully.
9869 */
9870 if (cdclk > dev_priv->max_cdclk_freq) {
9871 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9872 cdclk, dev_priv->max_cdclk_freq);
9873 cdclk = dev_priv->max_cdclk_freq;
9874 }
9875
9876 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9877 if (!intel_state->active_crtcs)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009878 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009879
9880 return 0;
9881}
9882
9883static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9884{
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009885 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9886 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9887 unsigned int req_cdclk = intel_state->dev_cdclk;
9888 unsigned int req_vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009889
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009890 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009891}
9892
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009893static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9894 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009895{
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009896 struct intel_encoder *intel_encoder =
9897 intel_ddi_get_crtc_new_encoder(crtc_state);
9898
9899 if (intel_encoder->type != INTEL_OUTPUT_DSI) {
9900 if (!intel_ddi_pll_select(crtc, crtc_state))
9901 return -EINVAL;
9902 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009903
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009904 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009905
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009906 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009907}
9908
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309909static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9910 enum port port,
9911 struct intel_crtc_state *pipe_config)
9912{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009913 enum intel_dpll_id id;
9914
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309915 switch (port) {
9916 case PORT_A:
9917 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009918 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309919 break;
9920 case PORT_B:
9921 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009922 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309923 break;
9924 case PORT_C:
9925 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009926 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309927 break;
9928 default:
9929 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009930 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309931 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009932
9933 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309934}
9935
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009936static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9937 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009938 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009939{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009940 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009941 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009942
9943 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9944 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9945
9946 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009947 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009948 id = DPLL_ID_SKL_DPLL0;
9949 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009950 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009951 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009952 break;
9953 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009954 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009955 break;
9956 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009957 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009958 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009959 default:
9960 MISSING_CASE(pipe_config->ddi_pll_sel);
9961 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009962 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009963
9964 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009965}
9966
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009967static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9968 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009969 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009970{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009971 enum intel_dpll_id id;
9972
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009973 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9974
9975 switch (pipe_config->ddi_pll_sel) {
9976 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009977 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009978 break;
9979 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009980 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009981 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009982 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009983 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009984 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009985 case PORT_CLK_SEL_LCPLL_810:
9986 id = DPLL_ID_LCPLL_810;
9987 break;
9988 case PORT_CLK_SEL_LCPLL_1350:
9989 id = DPLL_ID_LCPLL_1350;
9990 break;
9991 case PORT_CLK_SEL_LCPLL_2700:
9992 id = DPLL_ID_LCPLL_2700;
9993 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009994 default:
9995 MISSING_CASE(pipe_config->ddi_pll_sel);
9996 /* fall through */
9997 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009998 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009999 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010000
10001 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +010010002}
10003
Jani Nikulacf304292016-03-18 17:05:41 +020010004static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10005 struct intel_crtc_state *pipe_config,
10006 unsigned long *power_domain_mask)
10007{
10008 struct drm_device *dev = crtc->base.dev;
10009 struct drm_i915_private *dev_priv = dev->dev_private;
10010 enum intel_display_power_domain power_domain;
10011 u32 tmp;
10012
Imre Deakd9a7bc62016-05-12 16:18:50 +030010013 /*
10014 * The pipe->transcoder mapping is fixed with the exception of the eDP
10015 * transcoder handled below.
10016 */
Jani Nikulacf304292016-03-18 17:05:41 +020010017 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10018
10019 /*
10020 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10021 * consistency and less surprising code; it's in always on power).
10022 */
10023 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10024 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10025 enum pipe trans_edp_pipe;
10026 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10027 default:
10028 WARN(1, "unknown pipe linked to edp transcoder\n");
10029 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10030 case TRANS_DDI_EDP_INPUT_A_ON:
10031 trans_edp_pipe = PIPE_A;
10032 break;
10033 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10034 trans_edp_pipe = PIPE_B;
10035 break;
10036 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10037 trans_edp_pipe = PIPE_C;
10038 break;
10039 }
10040
10041 if (trans_edp_pipe == crtc->pipe)
10042 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10043 }
10044
10045 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10046 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10047 return false;
10048 *power_domain_mask |= BIT(power_domain);
10049
10050 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10051
10052 return tmp & PIPECONF_ENABLE;
10053}
10054
Jani Nikula4d1de972016-03-18 17:05:42 +020010055static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10056 struct intel_crtc_state *pipe_config,
10057 unsigned long *power_domain_mask)
10058{
10059 struct drm_device *dev = crtc->base.dev;
10060 struct drm_i915_private *dev_priv = dev->dev_private;
10061 enum intel_display_power_domain power_domain;
10062 enum port port;
10063 enum transcoder cpu_transcoder;
10064 u32 tmp;
10065
10066 pipe_config->has_dsi_encoder = false;
10067
10068 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10069 if (port == PORT_A)
10070 cpu_transcoder = TRANSCODER_DSI_A;
10071 else
10072 cpu_transcoder = TRANSCODER_DSI_C;
10073
10074 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10075 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10076 continue;
10077 *power_domain_mask |= BIT(power_domain);
10078
Imre Deakdb18b6a2016-03-24 12:41:40 +020010079 /*
10080 * The PLL needs to be enabled with a valid divider
10081 * configuration, otherwise accessing DSI registers will hang
10082 * the machine. See BSpec North Display Engine
10083 * registers/MIPI[BXT]. We can break out here early, since we
10084 * need the same DSI PLL to be enabled for both DSI ports.
10085 */
10086 if (!intel_dsi_pll_is_enabled(dev_priv))
10087 break;
10088
Jani Nikula4d1de972016-03-18 17:05:42 +020010089 /* XXX: this works for video mode only */
10090 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10091 if (!(tmp & DPI_ENABLE))
10092 continue;
10093
10094 tmp = I915_READ(MIPI_CTRL(port));
10095 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10096 continue;
10097
10098 pipe_config->cpu_transcoder = cpu_transcoder;
10099 pipe_config->has_dsi_encoder = true;
10100 break;
10101 }
10102
10103 return pipe_config->has_dsi_encoder;
10104}
10105
Daniel Vetter26804af2014-06-25 22:01:55 +030010106static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010107 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +030010108{
10109 struct drm_device *dev = crtc->base.dev;
10110 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010111 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +030010112 enum port port;
10113 uint32_t tmp;
10114
10115 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10116
10117 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10118
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070010119 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010120 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010121 else if (IS_BROXTON(dev))
10122 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010123 else
10124 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010125
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010126 pll = pipe_config->shared_dpll;
10127 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010128 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10129 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010130 }
10131
Daniel Vetter26804af2014-06-25 22:01:55 +030010132 /*
10133 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10134 * DDI E. So just check whether this pipe is wired to DDI E and whether
10135 * the PCH transcoder is on.
10136 */
Damien Lespiauca370452013-12-03 13:56:24 +000010137 if (INTEL_INFO(dev)->gen < 9 &&
10138 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010139 pipe_config->has_pch_encoder = true;
10140
10141 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10142 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10143 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10144
10145 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10146 }
10147}
10148
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010149static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010150 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010151{
10152 struct drm_device *dev = crtc->base.dev;
10153 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak17290502016-02-12 18:55:11 +020010154 enum intel_display_power_domain power_domain;
10155 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +020010156 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010157
Imre Deak17290502016-02-12 18:55:11 +020010158 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10159 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010160 return false;
Imre Deak17290502016-02-12 18:55:11 +020010161 power_domain_mask = BIT(power_domain);
10162
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010163 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010164
Jani Nikulacf304292016-03-18 17:05:41 +020010165 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +020010166
Jani Nikula4d1de972016-03-18 17:05:42 +020010167 if (IS_BROXTON(dev_priv)) {
10168 bxt_get_dsi_transcoder_state(crtc, pipe_config,
10169 &power_domain_mask);
10170 WARN_ON(active && pipe_config->has_dsi_encoder);
10171 if (pipe_config->has_dsi_encoder)
10172 active = true;
10173 }
10174
Jani Nikulacf304292016-03-18 17:05:41 +020010175 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010176 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010177
Jani Nikula4d1de972016-03-18 17:05:42 +020010178 if (!pipe_config->has_dsi_encoder) {
10179 haswell_get_ddi_port_state(crtc, pipe_config);
10180 intel_get_pipe_timings(crtc, pipe_config);
10181 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010182
Jani Nikulabc58be62016-03-18 17:05:39 +020010183 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010184
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010185 pipe_config->gamma_mode =
10186 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10187
Chandra Kondurua1b22782015-04-07 15:28:45 -070010188 if (INTEL_INFO(dev)->gen >= 9) {
10189 skl_init_scalers(dev, crtc, pipe_config);
10190 }
10191
Chandra Konduruaf99ced2015-05-11 14:35:47 -070010192 if (INTEL_INFO(dev)->gen >= 9) {
10193 pipe_config->scaler_state.scaler_id = -1;
10194 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10195 }
10196
Imre Deak17290502016-02-12 18:55:11 +020010197 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10198 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10199 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010200 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010201 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010202 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010203 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010204 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010205
Jesse Barnese59150d2014-01-07 13:30:45 -080010206 if (IS_HASWELL(dev))
10207 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10208 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010209
Jani Nikula4d1de972016-03-18 17:05:42 +020010210 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10211 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010212 pipe_config->pixel_multiplier =
10213 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10214 } else {
10215 pipe_config->pixel_multiplier = 1;
10216 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010217
Imre Deak17290502016-02-12 18:55:11 +020010218out:
10219 for_each_power_domain(power_domain, power_domain_mask)
10220 intel_display_power_put(dev_priv, power_domain);
10221
Jani Nikulacf304292016-03-18 17:05:41 +020010222 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010223}
10224
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010225static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10226 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010227{
10228 struct drm_device *dev = crtc->dev;
10229 struct drm_i915_private *dev_priv = dev->dev_private;
10230 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010231 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010232
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010233 if (plane_state && plane_state->visible) {
10234 unsigned int width = plane_state->base.crtc_w;
10235 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010236 unsigned int stride = roundup_pow_of_two(width) * 4;
10237
10238 switch (stride) {
10239 default:
10240 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10241 width, stride);
10242 stride = 256;
10243 /* fallthrough */
10244 case 256:
10245 case 512:
10246 case 1024:
10247 case 2048:
10248 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010249 }
10250
Ville Syrjälädc41c152014-08-13 11:57:05 +030010251 cntl |= CURSOR_ENABLE |
10252 CURSOR_GAMMA_ENABLE |
10253 CURSOR_FORMAT_ARGB |
10254 CURSOR_STRIDE(stride);
10255
10256 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010257 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010258
Ville Syrjälädc41c152014-08-13 11:57:05 +030010259 if (intel_crtc->cursor_cntl != 0 &&
10260 (intel_crtc->cursor_base != base ||
10261 intel_crtc->cursor_size != size ||
10262 intel_crtc->cursor_cntl != cntl)) {
10263 /* On these chipsets we can only modify the base/size/stride
10264 * whilst the cursor is disabled.
10265 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010266 I915_WRITE(CURCNTR(PIPE_A), 0);
10267 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010268 intel_crtc->cursor_cntl = 0;
10269 }
10270
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010271 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010272 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010273 intel_crtc->cursor_base = base;
10274 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010275
10276 if (intel_crtc->cursor_size != size) {
10277 I915_WRITE(CURSIZE, size);
10278 intel_crtc->cursor_size = size;
10279 }
10280
Chris Wilson4b0e3332014-05-30 16:35:26 +030010281 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010282 I915_WRITE(CURCNTR(PIPE_A), cntl);
10283 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010284 intel_crtc->cursor_cntl = cntl;
10285 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010286}
10287
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010288static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10289 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010290{
10291 struct drm_device *dev = crtc->dev;
10292 struct drm_i915_private *dev_priv = dev->dev_private;
10293 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10294 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010295 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010296
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010297 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010298 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010299 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010300 case 64:
10301 cntl |= CURSOR_MODE_64_ARGB_AX;
10302 break;
10303 case 128:
10304 cntl |= CURSOR_MODE_128_ARGB_AX;
10305 break;
10306 case 256:
10307 cntl |= CURSOR_MODE_256_ARGB_AX;
10308 break;
10309 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010310 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010311 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010312 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010313 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010314
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010315 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010316 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010317
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010318 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10319 cntl |= CURSOR_ROTATE_180;
10320 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010321
Chris Wilson4b0e3332014-05-30 16:35:26 +030010322 if (intel_crtc->cursor_cntl != cntl) {
10323 I915_WRITE(CURCNTR(pipe), cntl);
10324 POSTING_READ(CURCNTR(pipe));
10325 intel_crtc->cursor_cntl = cntl;
10326 }
10327
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010328 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010329 I915_WRITE(CURBASE(pipe), base);
10330 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010331
10332 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010333}
10334
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010335/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010336static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010337 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010338{
10339 struct drm_device *dev = crtc->dev;
10340 struct drm_i915_private *dev_priv = dev->dev_private;
10341 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10342 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010343 u32 base = intel_crtc->cursor_addr;
10344 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010345
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010346 if (plane_state) {
10347 int x = plane_state->base.crtc_x;
10348 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010349
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010350 if (x < 0) {
10351 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10352 x = -x;
10353 }
10354 pos |= x << CURSOR_X_SHIFT;
10355
10356 if (y < 0) {
10357 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10358 y = -y;
10359 }
10360 pos |= y << CURSOR_Y_SHIFT;
10361
10362 /* ILK+ do this automagically */
10363 if (HAS_GMCH_DISPLAY(dev) &&
10364 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10365 base += (plane_state->base.crtc_h *
10366 plane_state->base.crtc_w - 1) * 4;
10367 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010368 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010369
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010370 I915_WRITE(CURPOS(pipe), pos);
10371
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010372 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010373 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010374 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010375 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010376}
10377
Ville Syrjälädc41c152014-08-13 11:57:05 +030010378static bool cursor_size_ok(struct drm_device *dev,
10379 uint32_t width, uint32_t height)
10380{
10381 if (width == 0 || height == 0)
10382 return false;
10383
10384 /*
10385 * 845g/865g are special in that they are only limited by
10386 * the width of their cursors, the height is arbitrary up to
10387 * the precision of the register. Everything else requires
10388 * square cursors, limited to a few power-of-two sizes.
10389 */
10390 if (IS_845G(dev) || IS_I865G(dev)) {
10391 if ((width & 63) != 0)
10392 return false;
10393
10394 if (width > (IS_845G(dev) ? 64 : 512))
10395 return false;
10396
10397 if (height > 1023)
10398 return false;
10399 } else {
10400 switch (width | height) {
10401 case 256:
10402 case 128:
10403 if (IS_GEN2(dev))
10404 return false;
10405 case 64:
10406 break;
10407 default:
10408 return false;
10409 }
10410 }
10411
10412 return true;
10413}
10414
Jesse Barnes79e53942008-11-07 14:24:08 -080010415/* VESA 640x480x72Hz mode to set on the pipe */
10416static struct drm_display_mode load_detect_mode = {
10417 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10418 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10419};
10420
Daniel Vettera8bb6812014-02-10 18:00:39 +010010421struct drm_framebuffer *
10422__intel_framebuffer_create(struct drm_device *dev,
10423 struct drm_mode_fb_cmd2 *mode_cmd,
10424 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010425{
10426 struct intel_framebuffer *intel_fb;
10427 int ret;
10428
10429 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010430 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010431 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010432
10433 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010434 if (ret)
10435 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010436
10437 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010438
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010439err:
10440 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010441 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010442}
10443
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010444static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010445intel_framebuffer_create(struct drm_device *dev,
10446 struct drm_mode_fb_cmd2 *mode_cmd,
10447 struct drm_i915_gem_object *obj)
10448{
10449 struct drm_framebuffer *fb;
10450 int ret;
10451
10452 ret = i915_mutex_lock_interruptible(dev);
10453 if (ret)
10454 return ERR_PTR(ret);
10455 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10456 mutex_unlock(&dev->struct_mutex);
10457
10458 return fb;
10459}
10460
Chris Wilsond2dff872011-04-19 08:36:26 +010010461static u32
10462intel_framebuffer_pitch_for_width(int width, int bpp)
10463{
10464 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10465 return ALIGN(pitch, 64);
10466}
10467
10468static u32
10469intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10470{
10471 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010472 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010473}
10474
10475static struct drm_framebuffer *
10476intel_framebuffer_create_for_mode(struct drm_device *dev,
10477 struct drm_display_mode *mode,
10478 int depth, int bpp)
10479{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010480 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010481 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010482 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010483
Dave Gordond37cd8a2016-04-22 19:14:32 +010010484 obj = i915_gem_object_create(dev,
Chris Wilsond2dff872011-04-19 08:36:26 +010010485 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +010010486 if (IS_ERR(obj))
10487 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010488
10489 mode_cmd.width = mode->hdisplay;
10490 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010491 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10492 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010493 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010494
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010495 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10496 if (IS_ERR(fb))
10497 drm_gem_object_unreference_unlocked(&obj->base);
10498
10499 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010500}
10501
10502static struct drm_framebuffer *
10503mode_fits_in_fbdev(struct drm_device *dev,
10504 struct drm_display_mode *mode)
10505{
Daniel Vetter06957262015-08-10 13:34:08 +020010506#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010507 struct drm_i915_private *dev_priv = dev->dev_private;
10508 struct drm_i915_gem_object *obj;
10509 struct drm_framebuffer *fb;
10510
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010511 if (!dev_priv->fbdev)
10512 return NULL;
10513
10514 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010515 return NULL;
10516
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010517 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010518 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010519
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010520 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010521 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10522 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010523 return NULL;
10524
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010525 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010526 return NULL;
10527
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010528 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010529 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010530#else
10531 return NULL;
10532#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010533}
10534
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010535static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10536 struct drm_crtc *crtc,
10537 struct drm_display_mode *mode,
10538 struct drm_framebuffer *fb,
10539 int x, int y)
10540{
10541 struct drm_plane_state *plane_state;
10542 int hdisplay, vdisplay;
10543 int ret;
10544
10545 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10546 if (IS_ERR(plane_state))
10547 return PTR_ERR(plane_state);
10548
10549 if (mode)
10550 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10551 else
10552 hdisplay = vdisplay = 0;
10553
10554 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10555 if (ret)
10556 return ret;
10557 drm_atomic_set_fb_for_plane(plane_state, fb);
10558 plane_state->crtc_x = 0;
10559 plane_state->crtc_y = 0;
10560 plane_state->crtc_w = hdisplay;
10561 plane_state->crtc_h = vdisplay;
10562 plane_state->src_x = x << 16;
10563 plane_state->src_y = y << 16;
10564 plane_state->src_w = hdisplay << 16;
10565 plane_state->src_h = vdisplay << 16;
10566
10567 return 0;
10568}
10569
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010570bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010571 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010572 struct intel_load_detect_pipe *old,
10573 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010574{
10575 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010576 struct intel_encoder *intel_encoder =
10577 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010578 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010579 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010580 struct drm_crtc *crtc = NULL;
10581 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010582 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010583 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010584 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010585 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010586 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010587 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010588
Chris Wilsond2dff872011-04-19 08:36:26 +010010589 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010590 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010591 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010592
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010593 old->restore_state = NULL;
10594
Rob Clark51fd3712013-11-19 12:10:12 -050010595retry:
10596 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10597 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010598 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010599
Jesse Barnes79e53942008-11-07 14:24:08 -080010600 /*
10601 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010602 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010603 * - if the connector already has an assigned crtc, use it (but make
10604 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010605 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010606 * - try to find the first unused crtc that can drive this connector,
10607 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010608 */
10609
10610 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010611 if (connector->state->crtc) {
10612 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010613
Rob Clark51fd3712013-11-19 12:10:12 -050010614 ret = drm_modeset_lock(&crtc->mutex, ctx);
10615 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010616 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010617
10618 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010619 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010620 }
10621
10622 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010623 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010624 i++;
10625 if (!(encoder->possible_crtcs & (1 << i)))
10626 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010627
10628 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10629 if (ret)
10630 goto fail;
10631
10632 if (possible_crtc->state->enable) {
10633 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010634 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010635 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010636
10637 crtc = possible_crtc;
10638 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010639 }
10640
10641 /*
10642 * If we didn't find an unused CRTC, don't use any.
10643 */
10644 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010645 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010646 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010647 }
10648
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010649found:
10650 intel_crtc = to_intel_crtc(crtc);
10651
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010652 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10653 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010654 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010655
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010656 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010657 restore_state = drm_atomic_state_alloc(dev);
10658 if (!state || !restore_state) {
10659 ret = -ENOMEM;
10660 goto fail;
10661 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010662
10663 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010664 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010665
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010666 connector_state = drm_atomic_get_connector_state(state, connector);
10667 if (IS_ERR(connector_state)) {
10668 ret = PTR_ERR(connector_state);
10669 goto fail;
10670 }
10671
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010672 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10673 if (ret)
10674 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010675
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010676 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10677 if (IS_ERR(crtc_state)) {
10678 ret = PTR_ERR(crtc_state);
10679 goto fail;
10680 }
10681
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010682 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010683
Chris Wilson64927112011-04-20 07:25:26 +010010684 if (!mode)
10685 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010686
Chris Wilsond2dff872011-04-19 08:36:26 +010010687 /* We need a framebuffer large enough to accommodate all accesses
10688 * that the plane may generate whilst we perform load detection.
10689 * We can not rely on the fbcon either being present (we get called
10690 * during its initialisation to detect all boot displays, or it may
10691 * not even exist) or that it is large enough to satisfy the
10692 * requested mode.
10693 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010694 fb = mode_fits_in_fbdev(dev, mode);
10695 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010696 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010697 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010698 } else
10699 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010700 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010701 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010702 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010703 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010704
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010705 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10706 if (ret)
10707 goto fail;
10708
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010709 drm_framebuffer_unreference(fb);
10710
10711 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10712 if (ret)
10713 goto fail;
10714
10715 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10716 if (!ret)
10717 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10718 if (!ret)
10719 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10720 if (ret) {
10721 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10722 goto fail;
10723 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010724
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010725 ret = drm_atomic_commit(state);
10726 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010727 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010728 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010729 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010730
10731 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010732
Jesse Barnes79e53942008-11-07 14:24:08 -080010733 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010734 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010735 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010736
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010737fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010738 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010739 drm_atomic_state_free(restore_state);
10740 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010741
Rob Clark51fd3712013-11-19 12:10:12 -050010742 if (ret == -EDEADLK) {
10743 drm_modeset_backoff(ctx);
10744 goto retry;
10745 }
10746
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010747 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010748}
10749
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010750void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010751 struct intel_load_detect_pipe *old,
10752 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010753{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010754 struct intel_encoder *intel_encoder =
10755 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010756 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010757 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010758 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010759
Chris Wilsond2dff872011-04-19 08:36:26 +010010760 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010761 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010762 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010763
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010764 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010765 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010766
10767 ret = drm_atomic_commit(state);
10768 if (ret) {
10769 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10770 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010771 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010772}
10773
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010774static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010775 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010776{
10777 struct drm_i915_private *dev_priv = dev->dev_private;
10778 u32 dpll = pipe_config->dpll_hw_state.dpll;
10779
10780 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010781 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010782 else if (HAS_PCH_SPLIT(dev))
10783 return 120000;
10784 else if (!IS_GEN2(dev))
10785 return 96000;
10786 else
10787 return 48000;
10788}
10789
Jesse Barnes79e53942008-11-07 14:24:08 -080010790/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010791static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010792 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010793{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010794 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010795 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010796 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010797 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010798 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030010799 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010800 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010801 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010802
10803 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010804 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010805 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010806 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010807
10808 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010809 if (IS_PINEVIEW(dev)) {
10810 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10811 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010812 } else {
10813 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10814 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10815 }
10816
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010817 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010818 if (IS_PINEVIEW(dev))
10819 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10820 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010821 else
10822 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010823 DPLL_FPA01_P1_POST_DIV_SHIFT);
10824
10825 switch (dpll & DPLL_MODE_MASK) {
10826 case DPLLB_MODE_DAC_SERIAL:
10827 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10828 5 : 10;
10829 break;
10830 case DPLLB_MODE_LVDS:
10831 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10832 7 : 14;
10833 break;
10834 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010835 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010836 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010837 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010838 }
10839
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010840 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010841 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010842 else
Imre Deakdccbea32015-06-22 23:35:51 +030010843 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010844 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010845 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010846 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010847
10848 if (is_lvds) {
10849 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10850 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010851
10852 if (lvds & LVDS_CLKB_POWER_UP)
10853 clock.p2 = 7;
10854 else
10855 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010856 } else {
10857 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10858 clock.p1 = 2;
10859 else {
10860 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10861 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10862 }
10863 if (dpll & PLL_P2_DIVIDE_BY_4)
10864 clock.p2 = 4;
10865 else
10866 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010867 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010868
Imre Deakdccbea32015-06-22 23:35:51 +030010869 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010870 }
10871
Ville Syrjälä18442d02013-09-13 16:00:08 +030010872 /*
10873 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010874 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010875 * encoder's get_config() function.
10876 */
Imre Deakdccbea32015-06-22 23:35:51 +030010877 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010878}
10879
Ville Syrjälä6878da02013-09-13 15:59:11 +030010880int intel_dotclock_calculate(int link_freq,
10881 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010882{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010883 /*
10884 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010885 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010886 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010887 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010888 *
10889 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010890 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010891 */
10892
Ville Syrjälä6878da02013-09-13 15:59:11 +030010893 if (!m_n->link_n)
10894 return 0;
10895
10896 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10897}
10898
Ville Syrjälä18442d02013-09-13 16:00:08 +030010899static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010900 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010901{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010902 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010903
10904 /* read out port_clock from the DPLL */
10905 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010906
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010907 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010908 * In case there is an active pipe without active ports,
10909 * we may need some idea for the dotclock anyway.
10910 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010911 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010912 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010913 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010914 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010915}
10916
10917/** Returns the currently programmed mode of the given pipe. */
10918struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10919 struct drm_crtc *crtc)
10920{
Jesse Barnes548f2452011-02-17 10:40:53 -080010921 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010922 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010923 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010924 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010925 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010926 int htot = I915_READ(HTOTAL(cpu_transcoder));
10927 int hsync = I915_READ(HSYNC(cpu_transcoder));
10928 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10929 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010930 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010931
10932 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10933 if (!mode)
10934 return NULL;
10935
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010936 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10937 if (!pipe_config) {
10938 kfree(mode);
10939 return NULL;
10940 }
10941
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010942 /*
10943 * Construct a pipe_config sufficient for getting the clock info
10944 * back out of crtc_clock_get.
10945 *
10946 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10947 * to use a real value here instead.
10948 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010949 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10950 pipe_config->pixel_multiplier = 1;
10951 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10952 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10953 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10954 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010955
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010956 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010957 mode->hdisplay = (htot & 0xffff) + 1;
10958 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10959 mode->hsync_start = (hsync & 0xffff) + 1;
10960 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10961 mode->vdisplay = (vtot & 0xffff) + 1;
10962 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10963 mode->vsync_start = (vsync & 0xffff) + 1;
10964 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10965
10966 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010967
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010968 kfree(pipe_config);
10969
Jesse Barnes79e53942008-11-07 14:24:08 -080010970 return mode;
10971}
10972
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010973void intel_mark_busy(struct drm_i915_private *dev_priv)
Jesse Barnes652c3932009-08-17 13:31:43 -070010974{
Chris Wilsonf62a0072014-02-21 17:55:39 +000010975 if (dev_priv->mm.busy)
10976 return;
10977
Paulo Zanoni43694d62014-03-07 20:08:08 -030010978 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010979 i915_update_gfx_val(dev_priv);
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010980 if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010981 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010982 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010983}
10984
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010985void intel_mark_idle(struct drm_i915_private *dev_priv)
Chris Wilsonf047e392012-07-21 12:31:41 +010010986{
Chris Wilsonf62a0072014-02-21 17:55:39 +000010987 if (!dev_priv->mm.busy)
10988 return;
10989
10990 dev_priv->mm.busy = false;
10991
Tvrtko Ursulin7d993732016-04-28 12:57:00 +010010992 if (INTEL_GEN(dev_priv) >= 6)
10993 gen6_rps_idle(dev_priv);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010994
Paulo Zanoni43694d62014-03-07 20:08:08 -030010995 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010996}
10997
Jesse Barnes79e53942008-11-07 14:24:08 -080010998static void intel_crtc_destroy(struct drm_crtc *crtc)
10999{
11000 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011001 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011002 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020011003
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011004 spin_lock_irq(&dev->event_lock);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011005 work = intel_crtc->flip_work;
11006 intel_crtc->flip_work = NULL;
11007 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011008
Daniel Vetter5a21b662016-05-24 17:13:53 +020011009 if (work) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011010 cancel_work_sync(&work->mmio_work);
11011 cancel_work_sync(&work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011012 kfree(work);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011013 }
Jesse Barnes79e53942008-11-07 14:24:08 -080011014
11015 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020011016
Jesse Barnes79e53942008-11-07 14:24:08 -080011017 kfree(intel_crtc);
11018}
11019
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011020static void intel_unpin_work_fn(struct work_struct *__work)
11021{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011022 struct intel_flip_work *work =
11023 container_of(__work, struct intel_flip_work, unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011024 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11025 struct drm_device *dev = crtc->base.dev;
11026 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011027
Daniel Vetter5a21b662016-05-24 17:13:53 +020011028 if (is_mmio_work(work))
11029 flush_work(&work->mmio_work);
11030
11031 mutex_lock(&dev->struct_mutex);
11032 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
11033 drm_gem_object_unreference(&work->pending_flip_obj->base);
11034
11035 if (work->flip_queued_req)
11036 i915_gem_request_assign(&work->flip_queued_req, NULL);
11037 mutex_unlock(&dev->struct_mutex);
11038
11039 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
11040 intel_fbc_post_update(crtc);
11041 drm_framebuffer_unreference(work->old_fb);
11042
11043 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11044 atomic_dec(&crtc->unpin_work_count);
11045
11046 kfree(work);
11047}
11048
11049/* Is 'a' after or equal to 'b'? */
11050static bool g4x_flip_count_after_eq(u32 a, u32 b)
11051{
11052 return !((a - b) & 0x80000000);
11053}
11054
11055static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11056 struct intel_flip_work *work)
11057{
11058 struct drm_device *dev = crtc->base.dev;
11059 struct drm_i915_private *dev_priv = dev->dev_private;
11060 unsigned reset_counter;
11061
11062 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11063 if (crtc->reset_counter != reset_counter)
11064 return true;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011065
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011066 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011067 * The relevant registers doen't exist on pre-ctg.
11068 * As the flip done interrupt doesn't trigger for mmio
11069 * flips on gmch platforms, a flip count check isn't
11070 * really needed there. But since ctg has the registers,
11071 * include it in the check anyway.
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011072 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011073 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11074 return true;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011075
Daniel Vetter5a21b662016-05-24 17:13:53 +020011076 /*
11077 * BDW signals flip done immediately if the plane
11078 * is disabled, even if the plane enable is already
11079 * armed to occur at the next vblank :(
11080 */
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011081
Daniel Vetter5a21b662016-05-24 17:13:53 +020011082 /*
11083 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11084 * used the same base address. In that case the mmio flip might
11085 * have completed, but the CS hasn't even executed the flip yet.
11086 *
11087 * A flip count check isn't enough as the CS might have updated
11088 * the base address just after start of vblank, but before we
11089 * managed to process the interrupt. This means we'd complete the
11090 * CS flip too soon.
11091 *
11092 * Combining both checks should get us a good enough result. It may
11093 * still happen that the CS flip has been executed, but has not
11094 * yet actually completed. But in case the base address is the same
11095 * anyway, we don't really care.
11096 */
11097 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11098 crtc->flip_work->gtt_offset &&
11099 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11100 crtc->flip_work->flip_count);
11101}
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011102
Daniel Vetter5a21b662016-05-24 17:13:53 +020011103static bool
11104__pageflip_finished_mmio(struct intel_crtc *crtc,
11105 struct intel_flip_work *work)
11106{
11107 /*
11108 * MMIO work completes when vblank is different from
11109 * flip_queued_vblank.
11110 *
11111 * Reset counter value doesn't matter, this is handled by
11112 * i915_wait_request finishing early, so no need to handle
11113 * reset here.
11114 */
11115 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011116}
11117
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011118
11119static bool pageflip_finished(struct intel_crtc *crtc,
11120 struct intel_flip_work *work)
11121{
11122 if (!atomic_read(&work->pending))
11123 return false;
11124
11125 smp_rmb();
11126
Daniel Vetter5a21b662016-05-24 17:13:53 +020011127 if (is_mmio_work(work))
11128 return __pageflip_finished_mmio(crtc, work);
11129 else
11130 return __pageflip_finished_cs(crtc, work);
11131}
11132
11133void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11134{
11135 struct drm_device *dev = dev_priv->dev;
11136 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11137 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11138 struct intel_flip_work *work;
11139 unsigned long flags;
11140
11141 /* Ignore early vblank irqs */
11142 if (!crtc)
11143 return;
11144
Daniel Vetterf3260382014-09-15 14:55:23 +020011145 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011146 * This is called both by irq handlers and the reset code (to complete
11147 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000011148 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011149 spin_lock_irqsave(&dev->event_lock, flags);
11150 work = intel_crtc->flip_work;
11151
11152 if (work != NULL &&
11153 !is_mmio_work(work) &&
11154 pageflip_finished(intel_crtc, work))
11155 page_flip_completed(intel_crtc);
11156
11157 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011158}
11159
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011160void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011161{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011162 struct drm_device *dev = dev_priv->dev;
11163 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11164 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11165 struct intel_flip_work *work;
11166 unsigned long flags;
11167
11168 /* Ignore early vblank irqs */
11169 if (!crtc)
11170 return;
11171
11172 /*
11173 * This is called both by irq handlers and the reset code (to complete
11174 * lost pageflips) so needs the full irqsave spinlocks.
11175 */
11176 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011177 work = intel_crtc->flip_work;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011178
Daniel Vetter5a21b662016-05-24 17:13:53 +020011179 if (work != NULL &&
11180 is_mmio_work(work) &&
11181 pageflip_finished(intel_crtc, work))
11182 page_flip_completed(intel_crtc);
Maarten Lankhorst68858432016-05-17 15:07:52 +020011183
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011184 spin_unlock_irqrestore(&dev->event_lock, flags);
11185}
11186
Daniel Vetter5a21b662016-05-24 17:13:53 +020011187static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11188 struct intel_flip_work *work)
11189{
11190 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
11191
11192 /* Ensure that the work item is consistent when activating it ... */
11193 smp_mb__before_atomic();
11194 atomic_set(&work->pending, 1);
11195}
11196
11197static int intel_gen2_queue_flip(struct drm_device *dev,
11198 struct drm_crtc *crtc,
11199 struct drm_framebuffer *fb,
11200 struct drm_i915_gem_object *obj,
11201 struct drm_i915_gem_request *req,
11202 uint32_t flags)
11203{
11204 struct intel_engine_cs *engine = req->engine;
11205 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11206 u32 flip_mask;
11207 int ret;
11208
11209 ret = intel_ring_begin(req, 6);
11210 if (ret)
11211 return ret;
11212
11213 /* Can't queue multiple flips, so wait for the previous
11214 * one to finish before executing the next.
11215 */
11216 if (intel_crtc->plane)
11217 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11218 else
11219 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11220 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11221 intel_ring_emit(engine, MI_NOOP);
11222 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11223 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11224 intel_ring_emit(engine, fb->pitches[0]);
11225 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11226 intel_ring_emit(engine, 0); /* aux display base address, unused */
11227
11228 return 0;
11229}
11230
11231static int intel_gen3_queue_flip(struct drm_device *dev,
11232 struct drm_crtc *crtc,
11233 struct drm_framebuffer *fb,
11234 struct drm_i915_gem_object *obj,
11235 struct drm_i915_gem_request *req,
11236 uint32_t flags)
11237{
11238 struct intel_engine_cs *engine = req->engine;
11239 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11240 u32 flip_mask;
11241 int ret;
11242
11243 ret = intel_ring_begin(req, 6);
11244 if (ret)
11245 return ret;
11246
11247 if (intel_crtc->plane)
11248 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11249 else
11250 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11251 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
11252 intel_ring_emit(engine, MI_NOOP);
11253 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
11254 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11255 intel_ring_emit(engine, fb->pitches[0]);
11256 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11257 intel_ring_emit(engine, MI_NOOP);
11258
11259 return 0;
11260}
11261
11262static int intel_gen4_queue_flip(struct drm_device *dev,
11263 struct drm_crtc *crtc,
11264 struct drm_framebuffer *fb,
11265 struct drm_i915_gem_object *obj,
11266 struct drm_i915_gem_request *req,
11267 uint32_t flags)
11268{
11269 struct intel_engine_cs *engine = req->engine;
11270 struct drm_i915_private *dev_priv = dev->dev_private;
11271 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11272 uint32_t pf, pipesrc;
11273 int ret;
11274
11275 ret = intel_ring_begin(req, 4);
11276 if (ret)
11277 return ret;
11278
11279 /* i965+ uses the linear or tiled offsets from the
11280 * Display Registers (which do not change across a page-flip)
11281 * so we need only reprogram the base address.
11282 */
11283 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11284 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11285 intel_ring_emit(engine, fb->pitches[0]);
11286 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
11287 obj->tiling_mode);
11288
11289 /* XXX Enabling the panel-fitter across page-flip is so far
11290 * untested on non-native modes, so ignore it for now.
11291 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11292 */
11293 pf = 0;
11294 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11295 intel_ring_emit(engine, pf | pipesrc);
11296
11297 return 0;
11298}
11299
11300static int intel_gen6_queue_flip(struct drm_device *dev,
11301 struct drm_crtc *crtc,
11302 struct drm_framebuffer *fb,
11303 struct drm_i915_gem_object *obj,
11304 struct drm_i915_gem_request *req,
11305 uint32_t flags)
11306{
11307 struct intel_engine_cs *engine = req->engine;
11308 struct drm_i915_private *dev_priv = dev->dev_private;
11309 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11310 uint32_t pf, pipesrc;
11311 int ret;
11312
11313 ret = intel_ring_begin(req, 4);
11314 if (ret)
11315 return ret;
11316
11317 intel_ring_emit(engine, MI_DISPLAY_FLIP |
11318 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11319 intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
11320 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11321
11322 /* Contrary to the suggestions in the documentation,
11323 * "Enable Panel Fitter" does not seem to be required when page
11324 * flipping with a non-native mode, and worse causes a normal
11325 * modeset to fail.
11326 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11327 */
11328 pf = 0;
11329 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11330 intel_ring_emit(engine, pf | pipesrc);
11331
11332 return 0;
11333}
11334
11335static int intel_gen7_queue_flip(struct drm_device *dev,
11336 struct drm_crtc *crtc,
11337 struct drm_framebuffer *fb,
11338 struct drm_i915_gem_object *obj,
11339 struct drm_i915_gem_request *req,
11340 uint32_t flags)
11341{
11342 struct intel_engine_cs *engine = req->engine;
11343 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11344 uint32_t plane_bit = 0;
11345 int len, ret;
11346
11347 switch (intel_crtc->plane) {
11348 case PLANE_A:
11349 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11350 break;
11351 case PLANE_B:
11352 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11353 break;
11354 case PLANE_C:
11355 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11356 break;
11357 default:
11358 WARN_ONCE(1, "unknown plane in flip command\n");
11359 return -ENODEV;
11360 }
11361
11362 len = 4;
11363 if (engine->id == RCS) {
11364 len += 6;
11365 /*
11366 * On Gen 8, SRM is now taking an extra dword to accommodate
11367 * 48bits addresses, and we need a NOOP for the batch size to
11368 * stay even.
11369 */
11370 if (IS_GEN8(dev))
11371 len += 2;
11372 }
11373
11374 /*
11375 * BSpec MI_DISPLAY_FLIP for IVB:
11376 * "The full packet must be contained within the same cache line."
11377 *
11378 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11379 * cacheline, if we ever start emitting more commands before
11380 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11381 * then do the cacheline alignment, and finally emit the
11382 * MI_DISPLAY_FLIP.
11383 */
11384 ret = intel_ring_cacheline_align(req);
11385 if (ret)
11386 return ret;
11387
11388 ret = intel_ring_begin(req, len);
11389 if (ret)
11390 return ret;
11391
11392 /* Unmask the flip-done completion message. Note that the bspec says that
11393 * we should do this for both the BCS and RCS, and that we must not unmask
11394 * more than one flip event at any time (or ensure that one flip message
11395 * can be sent by waiting for flip-done prior to queueing new flips).
11396 * Experimentation says that BCS works despite DERRMR masking all
11397 * flip-done completion events and that unmasking all planes at once
11398 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11399 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11400 */
11401 if (engine->id == RCS) {
11402 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
11403 intel_ring_emit_reg(engine, DERRMR);
11404 intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11405 DERRMR_PIPEB_PRI_FLIP_DONE |
11406 DERRMR_PIPEC_PRI_FLIP_DONE));
11407 if (IS_GEN8(dev))
11408 intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
11409 MI_SRM_LRM_GLOBAL_GTT);
11410 else
11411 intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
11412 MI_SRM_LRM_GLOBAL_GTT);
11413 intel_ring_emit_reg(engine, DERRMR);
11414 intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
11415 if (IS_GEN8(dev)) {
11416 intel_ring_emit(engine, 0);
11417 intel_ring_emit(engine, MI_NOOP);
11418 }
11419 }
11420
11421 intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
11422 intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
11423 intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
11424 intel_ring_emit(engine, (MI_NOOP));
11425
11426 return 0;
11427}
11428
11429static bool use_mmio_flip(struct intel_engine_cs *engine,
11430 struct drm_i915_gem_object *obj)
11431{
Chris Wilsonc37efb92016-06-17 08:28:47 +010011432 struct reservation_object *resv;
11433
Daniel Vetter5a21b662016-05-24 17:13:53 +020011434 /*
11435 * This is not being used for older platforms, because
11436 * non-availability of flip done interrupt forces us to use
11437 * CS flips. Older platforms derive flip done using some clever
11438 * tricks involving the flip_pending status bits and vblank irqs.
11439 * So using MMIO flips there would disrupt this mechanism.
11440 */
11441
11442 if (engine == NULL)
11443 return true;
11444
11445 if (INTEL_GEN(engine->i915) < 5)
11446 return false;
11447
11448 if (i915.use_mmio_flip < 0)
11449 return false;
11450 else if (i915.use_mmio_flip > 0)
11451 return true;
11452 else if (i915.enable_execlists)
11453 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011454
11455 resv = i915_gem_object_get_dmabuf_resv(obj);
11456 if (resv && !reservation_object_test_signaled_rcu(resv, false))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011457 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011458
11459 return engine != i915_gem_request_get_engine(obj->last_write_req);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011460}
11461
11462static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11463 unsigned int rotation,
11464 struct intel_flip_work *work)
11465{
11466 struct drm_device *dev = intel_crtc->base.dev;
11467 struct drm_i915_private *dev_priv = dev->dev_private;
11468 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11469 const enum pipe pipe = intel_crtc->pipe;
11470 u32 ctl, stride, tile_height;
11471
11472 ctl = I915_READ(PLANE_CTL(pipe, 0));
11473 ctl &= ~PLANE_CTL_TILED_MASK;
11474 switch (fb->modifier[0]) {
11475 case DRM_FORMAT_MOD_NONE:
11476 break;
11477 case I915_FORMAT_MOD_X_TILED:
11478 ctl |= PLANE_CTL_TILED_X;
11479 break;
11480 case I915_FORMAT_MOD_Y_TILED:
11481 ctl |= PLANE_CTL_TILED_Y;
11482 break;
11483 case I915_FORMAT_MOD_Yf_TILED:
11484 ctl |= PLANE_CTL_TILED_YF;
11485 break;
11486 default:
11487 MISSING_CASE(fb->modifier[0]);
11488 }
11489
11490 /*
11491 * The stride is either expressed as a multiple of 64 bytes chunks for
11492 * linear buffers or in number of tiles for tiled buffers.
11493 */
11494 if (intel_rotation_90_or_270(rotation)) {
11495 /* stride = Surface height in tiles */
11496 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
11497 stride = DIV_ROUND_UP(fb->height, tile_height);
11498 } else {
11499 stride = fb->pitches[0] /
11500 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11501 fb->pixel_format);
11502 }
11503
11504 /*
11505 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11506 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11507 */
11508 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11509 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11510
11511 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11512 POSTING_READ(PLANE_SURF(pipe, 0));
11513}
11514
11515static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11516 struct intel_flip_work *work)
11517{
11518 struct drm_device *dev = intel_crtc->base.dev;
11519 struct drm_i915_private *dev_priv = dev->dev_private;
11520 struct intel_framebuffer *intel_fb =
11521 to_intel_framebuffer(intel_crtc->base.primary->fb);
11522 struct drm_i915_gem_object *obj = intel_fb->obj;
11523 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11524 u32 dspcntr;
11525
11526 dspcntr = I915_READ(reg);
11527
11528 if (obj->tiling_mode != I915_TILING_NONE)
11529 dspcntr |= DISPPLANE_TILED;
11530 else
11531 dspcntr &= ~DISPPLANE_TILED;
11532
11533 I915_WRITE(reg, dspcntr);
11534
11535 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11536 POSTING_READ(DSPSURF(intel_crtc->plane));
11537}
11538
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011539static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000011540{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011541 struct intel_flip_work *work =
11542 container_of(w, struct intel_flip_work, mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011543 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11544 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11545 struct intel_framebuffer *intel_fb =
11546 to_intel_framebuffer(crtc->base.primary->fb);
11547 struct drm_i915_gem_object *obj = intel_fb->obj;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011548 struct reservation_object *resv;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011549
11550 if (work->flip_queued_req)
11551 WARN_ON(__i915_wait_request(work->flip_queued_req,
11552 false, NULL,
11553 &dev_priv->rps.mmioflips));
11554
11555 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010011556 resv = i915_gem_object_get_dmabuf_resv(obj);
11557 if (resv)
11558 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
Daniel Vetter5a21b662016-05-24 17:13:53 +020011559 MAX_SCHEDULE_TIMEOUT) < 0);
11560
11561 intel_pipe_update_start(crtc);
11562
11563 if (INTEL_GEN(dev_priv) >= 9)
11564 skl_do_mmio_flip(crtc, work->rotation, work);
11565 else
11566 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11567 ilk_do_mmio_flip(crtc, work);
11568
11569 intel_pipe_update_end(crtc, work);
11570}
11571
11572static int intel_default_queue_flip(struct drm_device *dev,
11573 struct drm_crtc *crtc,
11574 struct drm_framebuffer *fb,
11575 struct drm_i915_gem_object *obj,
11576 struct drm_i915_gem_request *req,
11577 uint32_t flags)
11578{
11579 return -ENODEV;
11580}
11581
11582static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11583 struct intel_crtc *intel_crtc,
11584 struct intel_flip_work *work)
11585{
11586 u32 addr, vblank;
11587
11588 if (!atomic_read(&work->pending))
11589 return false;
11590
11591 smp_rmb();
11592
11593 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11594 if (work->flip_ready_vblank == 0) {
11595 if (work->flip_queued_req &&
11596 !i915_gem_request_completed(work->flip_queued_req, true))
11597 return false;
11598
11599 work->flip_ready_vblank = vblank;
11600 }
11601
11602 if (vblank - work->flip_ready_vblank < 3)
11603 return false;
11604
11605 /* Potential stall - if we see that the flip has happened,
11606 * assume a missed interrupt. */
11607 if (INTEL_GEN(dev_priv) >= 4)
11608 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11609 else
11610 addr = I915_READ(DSPADDR(intel_crtc->plane));
11611
11612 /* There is a potential issue here with a false positive after a flip
11613 * to the same address. We could address this by checking for a
11614 * non-incrementing frame counter.
11615 */
11616 return addr == work->gtt_offset;
11617}
11618
11619void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11620{
11621 struct drm_device *dev = dev_priv->dev;
11622 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011624 struct intel_flip_work *work;
11625
11626 WARN_ON(!in_interrupt());
11627
11628 if (crtc == NULL)
11629 return;
11630
11631 spin_lock(&dev->event_lock);
11632 work = intel_crtc->flip_work;
11633
11634 if (work != NULL && !is_mmio_work(work) &&
11635 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11636 WARN_ONCE(1,
11637 "Kicking stuck page flip: queued at %d, now %d\n",
11638 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11639 page_flip_completed(intel_crtc);
11640 work = NULL;
11641 }
11642
11643 if (work != NULL && !is_mmio_work(work) &&
11644 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11645 intel_queue_rps_boost_for_request(work->flip_queued_req);
11646 spin_unlock(&dev->event_lock);
11647}
11648
Daniel Vetteree042aa2016-06-13 16:13:49 +020011649__maybe_unused
Daniel Vetter5a21b662016-05-24 17:13:53 +020011650static int intel_crtc_page_flip(struct drm_crtc *crtc,
11651 struct drm_framebuffer *fb,
11652 struct drm_pending_vblank_event *event,
11653 uint32_t page_flip_flags)
11654{
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011655 struct drm_device *dev = crtc->dev;
Maarten Lankhorstaa420dd2016-05-17 15:07:51 +020011656 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011657 struct drm_framebuffer *old_fb = crtc->primary->fb;
11658 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11659 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11660 struct drm_plane *primary = crtc->primary;
11661 enum pipe pipe = intel_crtc->pipe;
11662 struct intel_flip_work *work;
11663 struct intel_engine_cs *engine;
11664 bool mmio_flip;
11665 struct drm_i915_gem_request *request = NULL;
11666 int ret;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011667
Daniel Vetter5a21b662016-05-24 17:13:53 +020011668 /*
11669 * drm_mode_page_flip_ioctl() should already catch this, but double
11670 * check to be safe. In the future we may enable pageflipping from
11671 * a disabled primary plane.
11672 */
11673 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11674 return -EBUSY;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011675
Daniel Vetter5a21b662016-05-24 17:13:53 +020011676 /* Can't change pixel format via MI display flips. */
11677 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11678 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011679
Daniel Vetter5a21b662016-05-24 17:13:53 +020011680 /*
11681 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11682 * Note that pitch changes could also affect these register.
11683 */
11684 if (INTEL_INFO(dev)->gen > 3 &&
11685 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11686 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11687 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011688
Daniel Vetter5a21b662016-05-24 17:13:53 +020011689 if (i915_terminally_wedged(&dev_priv->gpu_error))
11690 goto out_hang;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011691
Daniel Vetter5a21b662016-05-24 17:13:53 +020011692 work = kzalloc(sizeof(*work), GFP_KERNEL);
11693 if (work == NULL)
11694 return -ENOMEM;
11695
11696 work->event = event;
11697 work->crtc = crtc;
11698 work->old_fb = old_fb;
11699 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011700
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011701 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011702 if (ret)
11703 goto free_work;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011704
Daniel Vetter5a21b662016-05-24 17:13:53 +020011705 /* We borrow the event spin lock for protecting flip_work */
11706 spin_lock_irq(&dev->event_lock);
11707 if (intel_crtc->flip_work) {
11708 /* Before declaring the flip queue wedged, check if
11709 * the hardware completed the operation behind our backs.
11710 */
11711 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11712 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11713 page_flip_completed(intel_crtc);
11714 } else {
11715 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11716 spin_unlock_irq(&dev->event_lock);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011717
Daniel Vetter5a21b662016-05-24 17:13:53 +020011718 drm_crtc_vblank_put(crtc);
11719 kfree(work);
11720 return -EBUSY;
11721 }
11722 }
11723 intel_crtc->flip_work = work;
11724 spin_unlock_irq(&dev->event_lock);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011725
Daniel Vetter5a21b662016-05-24 17:13:53 +020011726 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11727 flush_workqueue(dev_priv->wq);
11728
11729 /* Reference the objects for the scheduled work. */
11730 drm_framebuffer_reference(work->old_fb);
11731 drm_gem_object_reference(&obj->base);
11732
11733 crtc->primary->fb = fb;
11734 update_state_fb(crtc->primary);
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020011735
11736 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
11737 to_intel_plane_state(primary->state));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011738
11739 work->pending_flip_obj = obj;
11740
11741 ret = i915_mutex_lock_interruptible(dev);
11742 if (ret)
11743 goto cleanup;
11744
11745 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11746 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11747 ret = -EIO;
11748 goto cleanup;
11749 }
11750
11751 atomic_inc(&intel_crtc->unpin_work_count);
11752
11753 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11754 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11755
11756 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11757 engine = &dev_priv->engine[BCS];
11758 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
11759 /* vlv: DISPLAY_FLIP fails to change tiling */
11760 engine = NULL;
11761 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11762 engine = &dev_priv->engine[BCS];
11763 } else if (INTEL_INFO(dev)->gen >= 7) {
11764 engine = i915_gem_request_get_engine(obj->last_write_req);
11765 if (engine == NULL || engine->id != RCS)
11766 engine = &dev_priv->engine[BCS];
11767 } else {
11768 engine = &dev_priv->engine[RCS];
11769 }
11770
11771 mmio_flip = use_mmio_flip(engine, obj);
11772
11773 /* When using CS flips, we want to emit semaphores between rings.
11774 * However, when using mmio flips we will create a task to do the
11775 * synchronisation, so all we want here is to pin the framebuffer
11776 * into the display plane and skip any waits.
11777 */
11778 if (!mmio_flip) {
11779 ret = i915_gem_object_sync(obj, engine, &request);
11780 if (!ret && !request) {
11781 request = i915_gem_request_alloc(engine, NULL);
11782 ret = PTR_ERR_OR_ZERO(request);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011783 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011784
Daniel Vetter5a21b662016-05-24 17:13:53 +020011785 if (ret)
11786 goto cleanup_pending;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011787 }
11788
Daniel Vetter5a21b662016-05-24 17:13:53 +020011789 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11790 if (ret)
11791 goto cleanup_pending;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011792
Daniel Vetter5a21b662016-05-24 17:13:53 +020011793 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11794 obj, 0);
11795 work->gtt_offset += intel_crtc->dspaddr_offset;
11796 work->rotation = crtc->primary->state->rotation;
11797
11798 if (mmio_flip) {
11799 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11800
11801 i915_gem_request_assign(&work->flip_queued_req,
11802 obj->last_write_req);
11803
11804 schedule_work(&work->mmio_work);
11805 } else {
11806 i915_gem_request_assign(&work->flip_queued_req, request);
11807 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11808 page_flip_flags);
11809 if (ret)
11810 goto cleanup_unpin;
11811
11812 intel_mark_page_flip_active(intel_crtc, work);
11813
11814 i915_add_request_no_flush(request);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011815 }
11816
Daniel Vetter5a21b662016-05-24 17:13:53 +020011817 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11818 to_intel_plane(primary)->frontbuffer_bit);
11819 mutex_unlock(&dev->struct_mutex);
11820
11821 intel_frontbuffer_flip_prepare(dev,
11822 to_intel_plane(primary)->frontbuffer_bit);
11823
11824 trace_i915_flip_request(intel_crtc->plane, obj);
11825
11826 return 0;
11827
11828cleanup_unpin:
11829 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11830cleanup_pending:
11831 if (!IS_ERR_OR_NULL(request))
11832 i915_add_request_no_flush(request);
11833 atomic_dec(&intel_crtc->unpin_work_count);
11834 mutex_unlock(&dev->struct_mutex);
11835cleanup:
11836 crtc->primary->fb = old_fb;
11837 update_state_fb(crtc->primary);
11838
11839 drm_gem_object_unreference_unlocked(&obj->base);
11840 drm_framebuffer_unreference(work->old_fb);
11841
11842 spin_lock_irq(&dev->event_lock);
11843 intel_crtc->flip_work = NULL;
11844 spin_unlock_irq(&dev->event_lock);
11845
11846 drm_crtc_vblank_put(crtc);
11847free_work:
11848 kfree(work);
11849
11850 if (ret == -EIO) {
11851 struct drm_atomic_state *state;
11852 struct drm_plane_state *plane_state;
11853
11854out_hang:
11855 state = drm_atomic_state_alloc(dev);
11856 if (!state)
11857 return -ENOMEM;
11858 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11859
11860retry:
11861 plane_state = drm_atomic_get_plane_state(state, primary);
11862 ret = PTR_ERR_OR_ZERO(plane_state);
11863 if (!ret) {
11864 drm_atomic_set_fb_for_plane(plane_state, fb);
11865
11866 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11867 if (!ret)
11868 ret = drm_atomic_commit(state);
11869 }
11870
11871 if (ret == -EDEADLK) {
11872 drm_modeset_backoff(state->acquire_ctx);
11873 drm_atomic_state_clear(state);
11874 goto retry;
11875 }
11876
11877 if (ret)
11878 drm_atomic_state_free(state);
11879
11880 if (ret == 0 && event) {
11881 spin_lock_irq(&dev->event_lock);
11882 drm_crtc_send_vblank_event(crtc, event);
11883 spin_unlock_irq(&dev->event_lock);
11884 }
11885 }
11886 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011887}
11888
Daniel Vetter5a21b662016-05-24 17:13:53 +020011889
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011890/**
11891 * intel_wm_need_update - Check whether watermarks need updating
11892 * @plane: drm plane
11893 * @state: new plane state
11894 *
11895 * Check current plane state versus the new one to determine whether
11896 * watermarks need to be recalculated.
11897 *
11898 * Returns true or false.
11899 */
11900static bool intel_wm_need_update(struct drm_plane *plane,
11901 struct drm_plane_state *state)
11902{
Matt Roperd21fbe82015-09-24 15:53:12 -070011903 struct intel_plane_state *new = to_intel_plane_state(state);
11904 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11905
11906 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011907 if (new->visible != cur->visible)
11908 return true;
11909
11910 if (!cur->base.fb || !new->base.fb)
11911 return false;
11912
11913 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11914 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011915 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11916 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11917 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11918 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011919 return true;
11920
11921 return false;
11922}
11923
Matt Roperd21fbe82015-09-24 15:53:12 -070011924static bool needs_scaling(struct intel_plane_state *state)
11925{
11926 int src_w = drm_rect_width(&state->src) >> 16;
11927 int src_h = drm_rect_height(&state->src) >> 16;
11928 int dst_w = drm_rect_width(&state->dst);
11929 int dst_h = drm_rect_height(&state->dst);
11930
11931 return (src_w != dst_w || src_h != dst_h);
11932}
11933
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011934int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11935 struct drm_plane_state *plane_state)
11936{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011937 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011938 struct drm_crtc *crtc = crtc_state->crtc;
11939 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11940 struct drm_plane *plane = plane_state->plane;
11941 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011942 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011943 struct intel_plane_state *old_plane_state =
11944 to_intel_plane_state(plane->state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011945 bool mode_changed = needs_modeset(crtc_state);
11946 bool was_crtc_enabled = crtc->state->active;
11947 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011948 bool turn_off, turn_on, visible, was_visible;
11949 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030011950 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011951
11952 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11953 plane->type != DRM_PLANE_TYPE_CURSOR) {
11954 ret = skl_update_scaler_plane(
11955 to_intel_crtc_state(crtc_state),
11956 to_intel_plane_state(plane_state));
11957 if (ret)
11958 return ret;
11959 }
11960
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011961 was_visible = old_plane_state->visible;
11962 visible = to_intel_plane_state(plane_state)->visible;
11963
11964 if (!was_crtc_enabled && WARN_ON(was_visible))
11965 was_visible = false;
11966
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011967 /*
11968 * Visibility is calculated as if the crtc was on, but
11969 * after scaler setup everything depends on it being off
11970 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030011971 *
11972 * FIXME this is wrong for watermarks. Watermarks should also
11973 * be computed as if the pipe would be active. Perhaps move
11974 * per-plane wm computation to the .check_plane() hook, and
11975 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011976 */
11977 if (!is_crtc_enabled)
11978 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011979
11980 if (!was_visible && !visible)
11981 return 0;
11982
Maarten Lankhorste8861672016-02-24 11:24:26 +010011983 if (fb != old_plane_state->base.fb)
11984 pipe_config->fb_changed = true;
11985
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011986 turn_off = was_visible && (!visible || mode_changed);
11987 turn_on = visible && (!was_visible || mode_changed);
11988
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011989 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjälä78108b72016-05-27 20:59:19 +030011990 intel_crtc->base.base.id,
11991 intel_crtc->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011992 plane->base.id, plane->name,
11993 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011994
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011995 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
11996 plane->base.id, plane->name,
11997 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011998 turn_off, turn_on, mode_changed);
11999
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012000 if (turn_on) {
12001 pipe_config->update_wm_pre = true;
12002
12003 /* must disable cxsr around plane enable/disable */
12004 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12005 pipe_config->disable_cxsr = true;
12006 } else if (turn_off) {
12007 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010012008
Ville Syrjälä852eb002015-06-24 22:00:07 +030012009 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010012010 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010012011 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012012 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012013 /* FIXME bollocks */
12014 pipe_config->update_wm_pre = true;
12015 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030012016 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012017
Matt Ropered4a6a72016-02-23 17:20:13 -080012018 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012019 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
12020 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080012021 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12022
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070012023 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010012024 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030012025
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010012026 /*
12027 * WaCxSRDisabledForSpriteScaling:ivb
12028 *
12029 * cstate->update_wm was already set above, so this flag will
12030 * take effect when we commit and program watermarks.
12031 */
12032 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
12033 needs_scaling(to_intel_plane_state(plane_state)) &&
12034 !needs_scaling(old_plane_state))
12035 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012036
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020012037 return 0;
12038}
12039
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012040static bool encoders_cloneable(const struct intel_encoder *a,
12041 const struct intel_encoder *b)
12042{
12043 /* masks could be asymmetric, so check both ways */
12044 return a == b || (a->cloneable & (1 << b->type) &&
12045 b->cloneable & (1 << a->type));
12046}
12047
12048static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12049 struct intel_crtc *crtc,
12050 struct intel_encoder *encoder)
12051{
12052 struct intel_encoder *source_encoder;
12053 struct drm_connector *connector;
12054 struct drm_connector_state *connector_state;
12055 int i;
12056
12057 for_each_connector_in_state(state, connector, connector_state, i) {
12058 if (connector_state->crtc != &crtc->base)
12059 continue;
12060
12061 source_encoder =
12062 to_intel_encoder(connector_state->best_encoder);
12063 if (!encoders_cloneable(encoder, source_encoder))
12064 return false;
12065 }
12066
12067 return true;
12068}
12069
12070static bool check_encoder_cloning(struct drm_atomic_state *state,
12071 struct intel_crtc *crtc)
12072{
12073 struct intel_encoder *encoder;
12074 struct drm_connector *connector;
12075 struct drm_connector_state *connector_state;
12076 int i;
12077
12078 for_each_connector_in_state(state, connector, connector_state, i) {
12079 if (connector_state->crtc != &crtc->base)
12080 continue;
12081
12082 encoder = to_intel_encoder(connector_state->best_encoder);
12083 if (!check_single_encoder_cloning(state, crtc, encoder))
12084 return false;
12085 }
12086
12087 return true;
12088}
12089
12090static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12091 struct drm_crtc_state *crtc_state)
12092{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012093 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020012094 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020012096 struct intel_crtc_state *pipe_config =
12097 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012098 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012099 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012100 bool mode_changed = needs_modeset(crtc_state);
12101
12102 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
12103 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12104 return -EINVAL;
12105 }
12106
Ville Syrjälä852eb002015-06-24 22:00:07 +030012107 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020012108 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020012109
Maarten Lankhorstad421372015-06-15 12:33:42 +020012110 if (mode_changed && crtc_state->enable &&
12111 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012112 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012113 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12114 pipe_config);
12115 if (ret)
12116 return ret;
12117 }
12118
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012119 if (crtc_state->color_mgmt_changed) {
12120 ret = intel_color_check(crtc, crtc_state);
12121 if (ret)
12122 return ret;
12123 }
12124
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012125 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012126 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010012127 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080012128 if (ret) {
12129 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012130 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080012131 }
12132 }
12133
12134 if (dev_priv->display.compute_intermediate_wm &&
12135 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12136 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12137 return 0;
12138
12139 /*
12140 * Calculate 'intermediate' watermarks that satisfy both the
12141 * old state and the new state. We can program these
12142 * immediately.
12143 */
12144 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12145 intel_crtc,
12146 pipe_config);
12147 if (ret) {
12148 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12149 return ret;
12150 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070012151 } else if (dev_priv->display.compute_intermediate_wm) {
12152 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12153 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012154 }
12155
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012156 if (INTEL_INFO(dev)->gen >= 9) {
12157 if (mode_changed)
12158 ret = skl_update_scaler_crtc(pipe_config);
12159
12160 if (!ret)
12161 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12162 pipe_config);
12163 }
12164
12165 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012166}
12167
Jani Nikula65b38e02015-04-13 11:26:56 +030012168static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012169 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Daniel Vetter5a21b662016-05-24 17:13:53 +020012170 .atomic_begin = intel_begin_crtc_commit,
12171 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012172 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012173};
12174
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012175static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12176{
12177 struct intel_connector *connector;
12178
12179 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020012180 if (connector->base.state->crtc)
12181 drm_connector_unreference(&connector->base);
12182
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012183 if (connector->base.encoder) {
12184 connector->base.state->best_encoder =
12185 connector->base.encoder;
12186 connector->base.state->crtc =
12187 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020012188
12189 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012190 } else {
12191 connector->base.state->best_encoder = NULL;
12192 connector->base.state->crtc = NULL;
12193 }
12194 }
12195}
12196
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012197static void
Robin Schroereba905b2014-05-18 02:24:50 +020012198connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012199 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012200{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012201 int bpp = pipe_config->pipe_bpp;
12202
12203 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12204 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012205 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012206
12207 /* Don't use an invalid EDID bpc value */
12208 if (connector->base.display_info.bpc &&
12209 connector->base.display_info.bpc * 3 < bpp) {
12210 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12211 bpp, connector->base.display_info.bpc*3);
12212 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12213 }
12214
Jani Nikula013dd9e2016-01-13 16:35:20 +020012215 /* Clamp bpp to default limit on screens without EDID 1.4 */
12216 if (connector->base.display_info.bpc == 0) {
12217 int type = connector->base.connector_type;
12218 int clamp_bpp = 24;
12219
12220 /* Fall back to 18 bpp when DP sink capability is unknown. */
12221 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12222 type == DRM_MODE_CONNECTOR_eDP)
12223 clamp_bpp = 18;
12224
12225 if (bpp > clamp_bpp) {
12226 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12227 bpp, clamp_bpp);
12228 pipe_config->pipe_bpp = clamp_bpp;
12229 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012230 }
12231}
12232
12233static int
12234compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012235 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012236{
12237 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012238 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012239 struct drm_connector *connector;
12240 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012241 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012242
Wayne Boyer666a4532015-12-09 12:29:35 -080012243 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012244 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012245 else if (INTEL_INFO(dev)->gen >= 5)
12246 bpp = 12*3;
12247 else
12248 bpp = 8*3;
12249
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012250
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012251 pipe_config->pipe_bpp = bpp;
12252
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012253 state = pipe_config->base.state;
12254
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012255 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012256 for_each_connector_in_state(state, connector, connector_state, i) {
12257 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012258 continue;
12259
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012260 connected_sink_compute_bpp(to_intel_connector(connector),
12261 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012262 }
12263
12264 return bpp;
12265}
12266
Daniel Vetter644db712013-09-19 14:53:58 +020012267static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12268{
12269 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12270 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012271 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012272 mode->crtc_hdisplay, mode->crtc_hsync_start,
12273 mode->crtc_hsync_end, mode->crtc_htotal,
12274 mode->crtc_vdisplay, mode->crtc_vsync_start,
12275 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12276}
12277
Daniel Vetterc0b03412013-05-28 12:05:54 +020012278static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012279 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012280 const char *context)
12281{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012282 struct drm_device *dev = crtc->base.dev;
12283 struct drm_plane *plane;
12284 struct intel_plane *intel_plane;
12285 struct intel_plane_state *state;
12286 struct drm_framebuffer *fb;
12287
Ville Syrjälä78108b72016-05-27 20:59:19 +030012288 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12289 crtc->base.base.id, crtc->base.name,
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012290 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012291
Jani Nikulada205632016-03-15 21:51:10 +020012292 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012293 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12294 pipe_config->pipe_bpp, pipe_config->dither);
12295 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12296 pipe_config->has_pch_encoder,
12297 pipe_config->fdi_lanes,
12298 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12299 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12300 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012301 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012302 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012303 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012304 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12305 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12306 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012307
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012308 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012309 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012310 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012311 pipe_config->dp_m2_n2.gmch_m,
12312 pipe_config->dp_m2_n2.gmch_n,
12313 pipe_config->dp_m2_n2.link_m,
12314 pipe_config->dp_m2_n2.link_n,
12315 pipe_config->dp_m2_n2.tu);
12316
Daniel Vetter55072d12014-11-20 16:10:28 +010012317 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12318 pipe_config->has_audio,
12319 pipe_config->has_infoframe);
12320
Daniel Vetterc0b03412013-05-28 12:05:54 +020012321 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012322 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012323 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012324 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12325 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012326 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012327 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12328 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012329 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12330 crtc->num_scalers,
12331 pipe_config->scaler_state.scaler_users,
12332 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012333 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12334 pipe_config->gmch_pfit.control,
12335 pipe_config->gmch_pfit.pgm_ratios,
12336 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012337 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012338 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012339 pipe_config->pch_pfit.size,
12340 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012341 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012342 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012343
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012344 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012345 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012346 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012347 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012348 pipe_config->ddi_pll_sel,
12349 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012350 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012351 pipe_config->dpll_hw_state.pll0,
12352 pipe_config->dpll_hw_state.pll1,
12353 pipe_config->dpll_hw_state.pll2,
12354 pipe_config->dpll_hw_state.pll3,
12355 pipe_config->dpll_hw_state.pll6,
12356 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012357 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012358 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012359 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012360 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012361 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12362 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12363 pipe_config->ddi_pll_sel,
12364 pipe_config->dpll_hw_state.ctrl1,
12365 pipe_config->dpll_hw_state.cfgcr1,
12366 pipe_config->dpll_hw_state.cfgcr2);
12367 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012368 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 +010012369 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012370 pipe_config->dpll_hw_state.wrpll,
12371 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012372 } else {
12373 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12374 "fp0: 0x%x, fp1: 0x%x\n",
12375 pipe_config->dpll_hw_state.dpll,
12376 pipe_config->dpll_hw_state.dpll_md,
12377 pipe_config->dpll_hw_state.fp0,
12378 pipe_config->dpll_hw_state.fp1);
12379 }
12380
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012381 DRM_DEBUG_KMS("planes on this crtc\n");
12382 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12383 intel_plane = to_intel_plane(plane);
12384 if (intel_plane->pipe != crtc->pipe)
12385 continue;
12386
12387 state = to_intel_plane_state(plane->state);
12388 fb = state->base.fb;
12389 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012390 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12391 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012392 continue;
12393 }
12394
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012395 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12396 plane->base.id, plane->name);
12397 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12398 fb->base.id, fb->width, fb->height,
12399 drm_get_format_name(fb->pixel_format));
12400 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12401 state->scaler_id,
12402 state->src.x1 >> 16, state->src.y1 >> 16,
12403 drm_rect_width(&state->src) >> 16,
12404 drm_rect_height(&state->src) >> 16,
12405 state->dst.x1, state->dst.y1,
12406 drm_rect_width(&state->dst),
12407 drm_rect_height(&state->dst));
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012408 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012409}
12410
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012411static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012412{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012413 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012414 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012415 unsigned int used_ports = 0;
12416
12417 /*
12418 * Walk the connector list instead of the encoder
12419 * list to detect the problem on ddi platforms
12420 * where there's just one encoder per digital port.
12421 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012422 drm_for_each_connector(connector, dev) {
12423 struct drm_connector_state *connector_state;
12424 struct intel_encoder *encoder;
12425
12426 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12427 if (!connector_state)
12428 connector_state = connector->state;
12429
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012430 if (!connector_state->best_encoder)
12431 continue;
12432
12433 encoder = to_intel_encoder(connector_state->best_encoder);
12434
12435 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012436
12437 switch (encoder->type) {
12438 unsigned int port_mask;
12439 case INTEL_OUTPUT_UNKNOWN:
12440 if (WARN_ON(!HAS_DDI(dev)))
12441 break;
12442 case INTEL_OUTPUT_DISPLAYPORT:
12443 case INTEL_OUTPUT_HDMI:
12444 case INTEL_OUTPUT_EDP:
12445 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12446
12447 /* the same port mustn't appear more than once */
12448 if (used_ports & port_mask)
12449 return false;
12450
12451 used_ports |= port_mask;
12452 default:
12453 break;
12454 }
12455 }
12456
12457 return true;
12458}
12459
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012460static void
12461clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12462{
12463 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012464 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012465 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012466 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012467 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012468 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012469
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012470 /* FIXME: before the switch to atomic started, a new pipe_config was
12471 * kzalloc'd. Code that depends on any field being zero should be
12472 * fixed, so that the crtc_state can be safely duplicated. For now,
12473 * only fields that are know to not cause problems are preserved. */
12474
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012475 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012476 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012477 shared_dpll = crtc_state->shared_dpll;
12478 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012479 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012480 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012481
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012482 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012483
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012484 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012485 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012486 crtc_state->shared_dpll = shared_dpll;
12487 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012488 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012489 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012490}
12491
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012492static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012493intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012494 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012495{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012496 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012497 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012498 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012499 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012500 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012501 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012502 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012503
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012504 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012505
Daniel Vettere143a212013-07-04 12:01:15 +020012506 pipe_config->cpu_transcoder =
12507 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012508
Imre Deak2960bc92013-07-30 13:36:32 +030012509 /*
12510 * Sanitize sync polarity flags based on requested ones. If neither
12511 * positive or negative polarity is requested, treat this as meaning
12512 * negative polarity.
12513 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012514 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012515 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012516 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012517
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012518 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012519 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012520 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012521
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012522 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12523 pipe_config);
12524 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012525 goto fail;
12526
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012527 /*
12528 * Determine the real pipe dimensions. Note that stereo modes can
12529 * increase the actual pipe size due to the frame doubling and
12530 * insertion of additional space for blanks between the frame. This
12531 * is stored in the crtc timings. We use the requested mode to do this
12532 * computation to clearly distinguish it from the adjusted mode, which
12533 * can be changed by the connectors in the below retry loop.
12534 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012535 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012536 &pipe_config->pipe_src_w,
12537 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012538
Daniel Vettere29c22c2013-02-21 00:00:16 +010012539encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012540 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012541 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012542 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012543
Daniel Vetter135c81b2013-07-21 21:37:09 +020012544 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012545 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12546 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012547
Daniel Vetter7758a112012-07-08 19:40:39 +020012548 /* Pass our mode to the connectors and the CRTC to give them a chance to
12549 * adjust it according to limitations or connector properties, and also
12550 * a chance to reject the mode entirely.
12551 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012552 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012553 if (connector_state->crtc != crtc)
12554 continue;
12555
12556 encoder = to_intel_encoder(connector_state->best_encoder);
12557
Daniel Vetterefea6e82013-07-21 21:36:59 +020012558 if (!(encoder->compute_config(encoder, pipe_config))) {
12559 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012560 goto fail;
12561 }
12562 }
12563
Daniel Vetterff9a6752013-06-01 17:16:21 +020012564 /* Set default port clock if not overwritten by the encoder. Needs to be
12565 * done afterwards in case the encoder adjusts the mode. */
12566 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012567 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012568 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012569
Daniel Vettera43f6e02013-06-07 23:10:32 +020012570 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012571 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012572 DRM_DEBUG_KMS("CRTC fixup failed\n");
12573 goto fail;
12574 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012575
12576 if (ret == RETRY) {
12577 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12578 ret = -EINVAL;
12579 goto fail;
12580 }
12581
12582 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12583 retry = false;
12584 goto encoder_retry;
12585 }
12586
Daniel Vettere8fa4272015-08-12 11:43:34 +020012587 /* Dithering seems to not pass-through bits correctly when it should, so
12588 * only enable it on 6bpc panels. */
12589 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012590 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012591 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012592
Daniel Vetter7758a112012-07-08 19:40:39 +020012593fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012594 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012595}
12596
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012597static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012598intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012599{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012600 struct drm_crtc *crtc;
12601 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012602 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012603
Ville Syrjälä76688512014-01-10 11:28:06 +020012604 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012605 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012606 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012607
12608 /* Update hwmode for vblank functions */
12609 if (crtc->state->active)
12610 crtc->hwmode = crtc->state->adjusted_mode;
12611 else
12612 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012613
12614 /*
12615 * Update legacy state to satisfy fbc code. This can
12616 * be removed when fbc uses the atomic state.
12617 */
12618 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12619 struct drm_plane_state *plane_state = crtc->primary->state;
12620
12621 crtc->primary->fb = plane_state->fb;
12622 crtc->x = plane_state->src_x >> 16;
12623 crtc->y = plane_state->src_y >> 16;
12624 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012625 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012626}
12627
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012628static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012629{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012630 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012631
12632 if (clock1 == clock2)
12633 return true;
12634
12635 if (!clock1 || !clock2)
12636 return false;
12637
12638 diff = abs(clock1 - clock2);
12639
12640 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12641 return true;
12642
12643 return false;
12644}
12645
Daniel Vetter25c5b262012-07-08 22:08:04 +020012646#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12647 list_for_each_entry((intel_crtc), \
12648 &(dev)->mode_config.crtc_list, \
12649 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012650 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012651
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012652static bool
12653intel_compare_m_n(unsigned int m, unsigned int n,
12654 unsigned int m2, unsigned int n2,
12655 bool exact)
12656{
12657 if (m == m2 && n == n2)
12658 return true;
12659
12660 if (exact || !m || !n || !m2 || !n2)
12661 return false;
12662
12663 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12664
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012665 if (n > n2) {
12666 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012667 m2 <<= 1;
12668 n2 <<= 1;
12669 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012670 } else if (n < n2) {
12671 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012672 m <<= 1;
12673 n <<= 1;
12674 }
12675 }
12676
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012677 if (n != n2)
12678 return false;
12679
12680 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012681}
12682
12683static bool
12684intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12685 struct intel_link_m_n *m2_n2,
12686 bool adjust)
12687{
12688 if (m_n->tu == m2_n2->tu &&
12689 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12690 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12691 intel_compare_m_n(m_n->link_m, m_n->link_n,
12692 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12693 if (adjust)
12694 *m2_n2 = *m_n;
12695
12696 return true;
12697 }
12698
12699 return false;
12700}
12701
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012702static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012703intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012704 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012705 struct intel_crtc_state *pipe_config,
12706 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012707{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012708 bool ret = true;
12709
12710#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12711 do { \
12712 if (!adjust) \
12713 DRM_ERROR(fmt, ##__VA_ARGS__); \
12714 else \
12715 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12716 } while (0)
12717
Daniel Vetter66e985c2013-06-05 13:34:20 +020012718#define PIPE_CONF_CHECK_X(name) \
12719 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012720 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012721 "(expected 0x%08x, found 0x%08x)\n", \
12722 current_config->name, \
12723 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012724 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012725 }
12726
Daniel Vetter08a24032013-04-19 11:25:34 +020012727#define PIPE_CONF_CHECK_I(name) \
12728 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012729 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012730 "(expected %i, found %i)\n", \
12731 current_config->name, \
12732 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012733 ret = false; \
12734 }
12735
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012736#define PIPE_CONF_CHECK_P(name) \
12737 if (current_config->name != pipe_config->name) { \
12738 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12739 "(expected %p, found %p)\n", \
12740 current_config->name, \
12741 pipe_config->name); \
12742 ret = false; \
12743 }
12744
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012745#define PIPE_CONF_CHECK_M_N(name) \
12746 if (!intel_compare_link_m_n(&current_config->name, \
12747 &pipe_config->name,\
12748 adjust)) { \
12749 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12750 "(expected tu %i gmch %i/%i link %i/%i, " \
12751 "found tu %i, gmch %i/%i link %i/%i)\n", \
12752 current_config->name.tu, \
12753 current_config->name.gmch_m, \
12754 current_config->name.gmch_n, \
12755 current_config->name.link_m, \
12756 current_config->name.link_n, \
12757 pipe_config->name.tu, \
12758 pipe_config->name.gmch_m, \
12759 pipe_config->name.gmch_n, \
12760 pipe_config->name.link_m, \
12761 pipe_config->name.link_n); \
12762 ret = false; \
12763 }
12764
Daniel Vetter55c561a2016-03-30 11:34:36 +020012765/* This is required for BDW+ where there is only one set of registers for
12766 * switching between high and low RR.
12767 * This macro can be used whenever a comparison has to be made between one
12768 * hw state and multiple sw state variables.
12769 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012770#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12771 if (!intel_compare_link_m_n(&current_config->name, \
12772 &pipe_config->name, adjust) && \
12773 !intel_compare_link_m_n(&current_config->alt_name, \
12774 &pipe_config->name, adjust)) { \
12775 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12776 "(expected tu %i gmch %i/%i link %i/%i, " \
12777 "or tu %i gmch %i/%i link %i/%i, " \
12778 "found tu %i, gmch %i/%i link %i/%i)\n", \
12779 current_config->name.tu, \
12780 current_config->name.gmch_m, \
12781 current_config->name.gmch_n, \
12782 current_config->name.link_m, \
12783 current_config->name.link_n, \
12784 current_config->alt_name.tu, \
12785 current_config->alt_name.gmch_m, \
12786 current_config->alt_name.gmch_n, \
12787 current_config->alt_name.link_m, \
12788 current_config->alt_name.link_n, \
12789 pipe_config->name.tu, \
12790 pipe_config->name.gmch_m, \
12791 pipe_config->name.gmch_n, \
12792 pipe_config->name.link_m, \
12793 pipe_config->name.link_n); \
12794 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012795 }
12796
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012797#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12798 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012799 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012800 "(expected %i, found %i)\n", \
12801 current_config->name & (mask), \
12802 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012803 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012804 }
12805
Ville Syrjälä5e550652013-09-06 23:29:07 +030012806#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12807 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012808 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012809 "(expected %i, found %i)\n", \
12810 current_config->name, \
12811 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012812 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012813 }
12814
Daniel Vetterbb760062013-06-06 14:55:52 +020012815#define PIPE_CONF_QUIRK(quirk) \
12816 ((current_config->quirks | pipe_config->quirks) & (quirk))
12817
Daniel Vettereccb1402013-05-22 00:50:22 +020012818 PIPE_CONF_CHECK_I(cpu_transcoder);
12819
Daniel Vetter08a24032013-04-19 11:25:34 +020012820 PIPE_CONF_CHECK_I(has_pch_encoder);
12821 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012822 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012823
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012824 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012825 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030012826 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012827
12828 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012829 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012830
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012831 if (current_config->has_drrs)
12832 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12833 } else
12834 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012835
Jani Nikulaa65347b2015-11-27 12:21:46 +020012836 PIPE_CONF_CHECK_I(has_dsi_encoder);
12837
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012838 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12839 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12840 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12841 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12842 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12843 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012844
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012845 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12846 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12847 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12848 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12849 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12850 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012851
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012852 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012853 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012854 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012855 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012856 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012857 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012858
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012859 PIPE_CONF_CHECK_I(has_audio);
12860
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012861 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012862 DRM_MODE_FLAG_INTERLACE);
12863
Daniel Vetterbb760062013-06-06 14:55:52 +020012864 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012865 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012866 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012867 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012868 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012869 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012870 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012871 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012872 DRM_MODE_FLAG_NVSYNC);
12873 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012874
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012875 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012876 /* pfit ratios are autocomputed by the hw on gen4+ */
12877 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020012878 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012879 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012880
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012881 if (!adjust) {
12882 PIPE_CONF_CHECK_I(pipe_src_w);
12883 PIPE_CONF_CHECK_I(pipe_src_h);
12884
12885 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12886 if (current_config->pch_pfit.enabled) {
12887 PIPE_CONF_CHECK_X(pch_pfit.pos);
12888 PIPE_CONF_CHECK_X(pch_pfit.size);
12889 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012890
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012891 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12892 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012893
Jesse Barnese59150d2014-01-07 13:30:45 -080012894 /* BDW+ don't expose a synchronous way to read the state */
12895 if (IS_HASWELL(dev))
12896 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012897
Ville Syrjälä282740f2013-09-04 18:30:03 +030012898 PIPE_CONF_CHECK_I(double_wide);
12899
Daniel Vetter26804af2014-06-25 22:01:55 +030012900 PIPE_CONF_CHECK_X(ddi_pll_sel);
12901
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012902 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012903 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012904 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012905 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12906 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012907 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012908 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012909 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12910 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12911 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012912
Ville Syrjälä47eacba2016-04-12 22:14:35 +030012913 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12914 PIPE_CONF_CHECK_X(dsi_pll.div);
12915
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012916 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12917 PIPE_CONF_CHECK_I(pipe_bpp);
12918
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012919 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012920 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012921
Daniel Vetter66e985c2013-06-05 13:34:20 +020012922#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012923#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012924#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012925#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012926#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012927#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012928#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012929
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012930 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012931}
12932
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012933static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12934 const struct intel_crtc_state *pipe_config)
12935{
12936 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012937 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012938 &pipe_config->fdi_m_n);
12939 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12940
12941 /*
12942 * FDI already provided one idea for the dotclock.
12943 * Yell if the encoder disagrees.
12944 */
12945 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12946 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12947 fdi_dotclock, dotclock);
12948 }
12949}
12950
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012951static void verify_wm_state(struct drm_crtc *crtc,
12952 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000012953{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012954 struct drm_device *dev = crtc->dev;
Damien Lespiau08db6652014-11-04 17:06:52 +000012955 struct drm_i915_private *dev_priv = dev->dev_private;
12956 struct skl_ddb_allocation hw_ddb, *sw_ddb;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012957 struct skl_ddb_entry *hw_entry, *sw_entry;
12958 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12959 const enum pipe pipe = intel_crtc->pipe;
Damien Lespiau08db6652014-11-04 17:06:52 +000012960 int plane;
12961
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012962 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000012963 return;
12964
12965 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12966 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12967
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012968 /* planes */
12969 for_each_plane(dev_priv, pipe, plane) {
12970 hw_entry = &hw_ddb.plane[pipe][plane];
12971 sw_entry = &sw_ddb->plane[pipe][plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000012972
12973 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12974 continue;
12975
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012976 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12977 "(expected (%u,%u), found (%u,%u))\n",
12978 pipe_name(pipe), plane + 1,
12979 sw_entry->start, sw_entry->end,
12980 hw_entry->start, hw_entry->end);
12981 }
12982
12983 /* cursor */
12984 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12985 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
12986
12987 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012988 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12989 "(expected (%u,%u), found (%u,%u))\n",
12990 pipe_name(pipe),
12991 sw_entry->start, sw_entry->end,
12992 hw_entry->start, hw_entry->end);
12993 }
12994}
12995
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012996static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012997verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012998{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012999 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013000
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013001 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013002 struct drm_encoder *encoder = connector->encoder;
13003 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013004
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013005 if (state->crtc != crtc)
13006 continue;
13007
Daniel Vetter5a21b662016-05-24 17:13:53 +020013008 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013009
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013010 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020013011 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013012 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013013}
13014
13015static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013016verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013017{
13018 struct intel_encoder *encoder;
13019 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013020
Damien Lespiaub2784e12014-08-05 11:29:37 +010013021 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013022 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013023 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013024
13025 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13026 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013027 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013028
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013029 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013030 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013031 continue;
13032 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020013033
13034 I915_STATE_WARN(connector->base.state->crtc !=
13035 encoder->base.crtc,
13036 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013037 }
Dave Airlie0e32b392014-05-02 14:02:48 +100013038
Rob Clarke2c719b2014-12-15 13:56:32 -050013039 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013040 "encoder's enabled state mismatch "
13041 "(expected %i, found %i)\n",
13042 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013043
13044 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013045 bool active;
13046
13047 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013048 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013049 "encoder detached but still enabled on pipe %c.\n",
13050 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020013051 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013052 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013053}
13054
13055static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013056verify_crtc_state(struct drm_crtc *crtc,
13057 struct drm_crtc_state *old_crtc_state,
13058 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013059{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013060 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030013061 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013062 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013063 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13064 struct intel_crtc_state *pipe_config, *sw_config;
13065 struct drm_atomic_state *old_state;
13066 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013067
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013068 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020013069 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013070 pipe_config = to_intel_crtc_state(old_crtc_state);
13071 memset(pipe_config, 0, sizeof(*pipe_config));
13072 pipe_config->base.crtc = crtc;
13073 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013074
Ville Syrjälä78108b72016-05-27 20:59:19 +030013075 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013076
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013077 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013078
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013079 /* hw state is inconsistent with the pipe quirk */
13080 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13081 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13082 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013083
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013084 I915_STATE_WARN(new_crtc_state->active != active,
13085 "crtc active state doesn't match with hw state "
13086 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013087
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013088 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13089 "transitional active state does not match atomic hw state "
13090 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013091
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013092 for_each_encoder_on_crtc(dev, crtc, encoder) {
13093 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013094
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013095 active = encoder->get_hw_state(encoder, &pipe);
13096 I915_STATE_WARN(active != new_crtc_state->active,
13097 "[ENCODER:%i] active %i with crtc active %i\n",
13098 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013099
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013100 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13101 "Encoder connected to wrong pipe %c\n",
13102 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013103
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013104 if (active)
13105 encoder->get_config(encoder, pipe_config);
13106 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013107
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013108 if (!new_crtc_state->active)
13109 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013110
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013111 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013112
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013113 sw_config = to_intel_crtc_state(crtc->state);
13114 if (!intel_pipe_config_compare(dev, sw_config,
13115 pipe_config, false)) {
13116 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13117 intel_dump_pipe_config(intel_crtc, pipe_config,
13118 "[hw state]");
13119 intel_dump_pipe_config(intel_crtc, sw_config,
13120 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013121 }
13122}
13123
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013124static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013125verify_single_dpll_state(struct drm_i915_private *dev_priv,
13126 struct intel_shared_dpll *pll,
13127 struct drm_crtc *crtc,
13128 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013129{
13130 struct intel_dpll_hw_state dpll_hw_state;
13131 unsigned crtc_mask;
13132 bool active;
13133
13134 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13135
13136 DRM_DEBUG_KMS("%s\n", pll->name);
13137
13138 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
13139
13140 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13141 I915_STATE_WARN(!pll->on && pll->active_mask,
13142 "pll in active use but not on in sw tracking\n");
13143 I915_STATE_WARN(pll->on && !pll->active_mask,
13144 "pll is on but not used by any active crtc\n");
13145 I915_STATE_WARN(pll->on != active,
13146 "pll on state mismatch (expected %i, found %i)\n",
13147 pll->on, active);
13148 }
13149
13150 if (!crtc) {
13151 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13152 "more active pll users than references: %x vs %x\n",
13153 pll->active_mask, pll->config.crtc_mask);
13154
13155 return;
13156 }
13157
13158 crtc_mask = 1 << drm_crtc_index(crtc);
13159
13160 if (new_state->active)
13161 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13162 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13163 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13164 else
13165 I915_STATE_WARN(pll->active_mask & crtc_mask,
13166 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13167 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13168
13169 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13170 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13171 crtc_mask, pll->config.crtc_mask);
13172
13173 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13174 &dpll_hw_state,
13175 sizeof(dpll_hw_state)),
13176 "pll hw state mismatch\n");
13177}
13178
13179static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013180verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13181 struct drm_crtc_state *old_crtc_state,
13182 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013183{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013184 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013185 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13186 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13187
13188 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013189 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013190
13191 if (old_state->shared_dpll &&
13192 old_state->shared_dpll != new_state->shared_dpll) {
13193 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13194 struct intel_shared_dpll *pll = old_state->shared_dpll;
13195
13196 I915_STATE_WARN(pll->active_mask & crtc_mask,
13197 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13198 pipe_name(drm_crtc_index(crtc)));
13199 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13200 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13201 pipe_name(drm_crtc_index(crtc)));
13202 }
13203}
13204
13205static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013206intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013207 struct drm_crtc_state *old_state,
13208 struct drm_crtc_state *new_state)
13209{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013210 if (!needs_modeset(new_state) &&
13211 !to_intel_crtc_state(new_state)->update_pipe)
13212 return;
13213
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013214 verify_wm_state(crtc, new_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013215 verify_connector_state(crtc->dev, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013216 verify_crtc_state(crtc, old_state, new_state);
13217 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013218}
13219
13220static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013221verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013222{
13223 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013224 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013225
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013226 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013227 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013228}
Daniel Vetter53589012013-06-05 13:34:16 +020013229
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013230static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013231intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013232{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013233 verify_encoder_state(dev);
13234 verify_connector_state(dev, NULL);
13235 verify_disabled_dpll_state(dev);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013236}
13237
Ville Syrjälä80715b22014-05-15 20:23:23 +030013238static void update_scanline_offset(struct intel_crtc *crtc)
13239{
13240 struct drm_device *dev = crtc->base.dev;
13241
13242 /*
13243 * The scanline counter increments at the leading edge of hsync.
13244 *
13245 * On most platforms it starts counting from vtotal-1 on the
13246 * first active line. That means the scanline counter value is
13247 * always one less than what we would expect. Ie. just after
13248 * start of vblank, which also occurs at start of hsync (on the
13249 * last active line), the scanline counter will read vblank_start-1.
13250 *
13251 * On gen2 the scanline counter starts counting from 1 instead
13252 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13253 * to keep the value positive), instead of adding one.
13254 *
13255 * On HSW+ the behaviour of the scanline counter depends on the output
13256 * type. For DP ports it behaves like most other platforms, but on HDMI
13257 * there's an extra 1 line difference. So we need to add two instead of
13258 * one to the value.
13259 */
13260 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013261 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013262 int vtotal;
13263
Ville Syrjälä124abe02015-09-08 13:40:45 +030013264 vtotal = adjusted_mode->crtc_vtotal;
13265 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013266 vtotal /= 2;
13267
13268 crtc->scanline_offset = vtotal - 1;
13269 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030013270 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013271 crtc->scanline_offset = 2;
13272 } else
13273 crtc->scanline_offset = 1;
13274}
13275
Maarten Lankhorstad421372015-06-15 12:33:42 +020013276static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013277{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013278 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013279 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013280 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013281 struct drm_crtc *crtc;
13282 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013283 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013284
13285 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013286 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013287
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013288 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013289 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013290 struct intel_shared_dpll *old_dpll =
13291 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013292
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013293 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013294 continue;
13295
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013296 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013297
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013298 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013299 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013300
Maarten Lankhorstad421372015-06-15 12:33:42 +020013301 if (!shared_dpll)
13302 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13303
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013304 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013305 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013306}
13307
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013308/*
13309 * This implements the workaround described in the "notes" section of the mode
13310 * set sequence documentation. When going from no pipes or single pipe to
13311 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13312 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13313 */
13314static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13315{
13316 struct drm_crtc_state *crtc_state;
13317 struct intel_crtc *intel_crtc;
13318 struct drm_crtc *crtc;
13319 struct intel_crtc_state *first_crtc_state = NULL;
13320 struct intel_crtc_state *other_crtc_state = NULL;
13321 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13322 int i;
13323
13324 /* look at all crtc's that are going to be enabled in during modeset */
13325 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13326 intel_crtc = to_intel_crtc(crtc);
13327
13328 if (!crtc_state->active || !needs_modeset(crtc_state))
13329 continue;
13330
13331 if (first_crtc_state) {
13332 other_crtc_state = to_intel_crtc_state(crtc_state);
13333 break;
13334 } else {
13335 first_crtc_state = to_intel_crtc_state(crtc_state);
13336 first_pipe = intel_crtc->pipe;
13337 }
13338 }
13339
13340 /* No workaround needed? */
13341 if (!first_crtc_state)
13342 return 0;
13343
13344 /* w/a possibly needed, check how many crtc's are already enabled. */
13345 for_each_intel_crtc(state->dev, intel_crtc) {
13346 struct intel_crtc_state *pipe_config;
13347
13348 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13349 if (IS_ERR(pipe_config))
13350 return PTR_ERR(pipe_config);
13351
13352 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13353
13354 if (!pipe_config->base.active ||
13355 needs_modeset(&pipe_config->base))
13356 continue;
13357
13358 /* 2 or more enabled crtcs means no need for w/a */
13359 if (enabled_pipe != INVALID_PIPE)
13360 return 0;
13361
13362 enabled_pipe = intel_crtc->pipe;
13363 }
13364
13365 if (enabled_pipe != INVALID_PIPE)
13366 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13367 else if (other_crtc_state)
13368 other_crtc_state->hsw_workaround_pipe = first_pipe;
13369
13370 return 0;
13371}
13372
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013373static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13374{
13375 struct drm_crtc *crtc;
13376 struct drm_crtc_state *crtc_state;
13377 int ret = 0;
13378
13379 /* add all active pipes to the state */
13380 for_each_crtc(state->dev, crtc) {
13381 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13382 if (IS_ERR(crtc_state))
13383 return PTR_ERR(crtc_state);
13384
13385 if (!crtc_state->active || needs_modeset(crtc_state))
13386 continue;
13387
13388 crtc_state->mode_changed = true;
13389
13390 ret = drm_atomic_add_affected_connectors(state, crtc);
13391 if (ret)
13392 break;
13393
13394 ret = drm_atomic_add_affected_planes(state, crtc);
13395 if (ret)
13396 break;
13397 }
13398
13399 return ret;
13400}
13401
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013402static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013403{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013404 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13405 struct drm_i915_private *dev_priv = state->dev->dev_private;
13406 struct drm_crtc *crtc;
13407 struct drm_crtc_state *crtc_state;
13408 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013409
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013410 if (!check_digital_port_conflicts(state)) {
13411 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13412 return -EINVAL;
13413 }
13414
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013415 intel_state->modeset = true;
13416 intel_state->active_crtcs = dev_priv->active_crtcs;
13417
13418 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13419 if (crtc_state->active)
13420 intel_state->active_crtcs |= 1 << i;
13421 else
13422 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070013423
13424 if (crtc_state->active != crtc->state->active)
13425 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013426 }
13427
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013428 /*
13429 * See if the config requires any additional preparation, e.g.
13430 * to adjust global state with pipes off. We need to do this
13431 * here so we can get the modeset_pipe updated config for the new
13432 * mode set on this crtc. For other crtcs we need to use the
13433 * adjusted_mode bits in the crtc directly.
13434 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013435 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030013436 if (!intel_state->cdclk_pll_vco)
Ville Syrjälä63911d72016-05-13 23:41:32 +030013437 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +030013438 if (!intel_state->cdclk_pll_vco)
13439 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013440
Clint Taylorc89e39f2016-05-13 23:41:21 +030013441 ret = dev_priv->display.modeset_calc_cdclk(state);
13442 if (ret < 0)
13443 return ret;
13444
13445 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013446 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013447 ret = intel_modeset_all_pipes(state);
13448
13449 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013450 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013451
13452 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13453 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013454 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013455 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013456
Maarten Lankhorstad421372015-06-15 12:33:42 +020013457 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013458
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013459 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013460 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013461
Maarten Lankhorstad421372015-06-15 12:33:42 +020013462 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013463}
13464
Matt Roperaa363132015-09-24 15:53:18 -070013465/*
13466 * Handle calculation of various watermark data at the end of the atomic check
13467 * phase. The code here should be run after the per-crtc and per-plane 'check'
13468 * handlers to ensure that all derived state has been updated.
13469 */
Matt Roper55994c22016-05-12 07:06:08 -070013470static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070013471{
13472 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070013473 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070013474
13475 /* Is there platform-specific watermark information to calculate? */
13476 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070013477 return dev_priv->display.compute_global_watermarks(state);
13478
13479 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070013480}
13481
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013482/**
13483 * intel_atomic_check - validate state object
13484 * @dev: drm device
13485 * @state: state to validate
13486 */
13487static int intel_atomic_check(struct drm_device *dev,
13488 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013489{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013490 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013491 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013492 struct drm_crtc *crtc;
13493 struct drm_crtc_state *crtc_state;
13494 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013495 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013496
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013497 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013498 if (ret)
13499 return ret;
13500
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013501 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013502 struct intel_crtc_state *pipe_config =
13503 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013504
13505 /* Catch I915_MODE_FLAG_INHERITED */
13506 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13507 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013508
Daniel Vetter26495482015-07-15 14:15:52 +020013509 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013510 continue;
13511
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013512 if (!crtc_state->enable) {
13513 any_ms = true;
13514 continue;
13515 }
13516
Daniel Vetter26495482015-07-15 14:15:52 +020013517 /* FIXME: For only active_changed we shouldn't need to do any
13518 * state recomputation at all. */
13519
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013520 ret = drm_atomic_add_affected_connectors(state, crtc);
13521 if (ret)
13522 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013523
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013524 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013525 if (ret) {
13526 intel_dump_pipe_config(to_intel_crtc(crtc),
13527 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013528 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013529 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013530
Jani Nikula73831232015-11-19 10:26:30 +020013531 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013532 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013533 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013534 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013535 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013536 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013537 }
13538
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013539 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020013540 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013541
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013542 ret = drm_atomic_add_affected_planes(state, crtc);
13543 if (ret)
13544 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013545
Daniel Vetter26495482015-07-15 14:15:52 +020013546 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13547 needs_modeset(crtc_state) ?
13548 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013549 }
13550
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013551 if (any_ms) {
13552 ret = intel_modeset_checks(state);
13553
13554 if (ret)
13555 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013556 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013557 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013558
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013559 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013560 if (ret)
13561 return ret;
13562
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013563 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070013564 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013565}
13566
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013567static int intel_atomic_prepare_commit(struct drm_device *dev,
13568 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013569 bool nonblock)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013570{
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013571 struct drm_i915_private *dev_priv = dev->dev_private;
13572 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013573 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013574 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013575 struct drm_crtc *crtc;
13576 int i, ret;
13577
Daniel Vetter5a21b662016-05-24 17:13:53 +020013578 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13579 if (state->legacy_cursor_update)
13580 continue;
13581
13582 ret = intel_crtc_wait_for_pending_flips(crtc);
13583 if (ret)
13584 return ret;
13585
13586 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13587 flush_workqueue(dev_priv->wq);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013588 }
13589
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013590 ret = mutex_lock_interruptible(&dev->struct_mutex);
13591 if (ret)
13592 return ret;
13593
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013594 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013595 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013596
Dave Airlie21daaee2016-05-05 09:56:30 +100013597 if (!ret && !nonblock) {
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013598 for_each_plane_in_state(state, plane, plane_state, i) {
13599 struct intel_plane_state *intel_plane_state =
13600 to_intel_plane_state(plane_state);
13601
13602 if (!intel_plane_state->wait_req)
13603 continue;
13604
13605 ret = __i915_wait_request(intel_plane_state->wait_req,
Chris Wilson299259a2016-04-13 17:35:06 +010013606 true, NULL, NULL);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013607 if (ret) {
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013608 /* Any hang should be swallowed by the wait */
13609 WARN_ON(ret == -EIO);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013610 mutex_lock(&dev->struct_mutex);
13611 drm_atomic_helper_cleanup_planes(dev, state);
13612 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013613 break;
Chris Wilsonf7e58382016-04-13 17:35:07 +010013614 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013615 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013616 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013617
13618 return ret;
13619}
13620
Maarten Lankhorsta2991412016-05-17 15:07:48 +020013621u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13622{
13623 struct drm_device *dev = crtc->base.dev;
13624
13625 if (!dev->max_vblank_count)
13626 return drm_accurate_vblank_count(&crtc->base);
13627
13628 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13629}
13630
Daniel Vetter5a21b662016-05-24 17:13:53 +020013631static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13632 struct drm_i915_private *dev_priv,
13633 unsigned crtc_mask)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013634{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013635 unsigned last_vblank_count[I915_MAX_PIPES];
13636 enum pipe pipe;
13637 int ret;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013638
Daniel Vetter5a21b662016-05-24 17:13:53 +020013639 if (!crtc_mask)
13640 return;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013641
Daniel Vetter5a21b662016-05-24 17:13:53 +020013642 for_each_pipe(dev_priv, pipe) {
13643 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorste8861672016-02-24 11:24:26 +010013644
Daniel Vetter5a21b662016-05-24 17:13:53 +020013645 if (!((1 << pipe) & crtc_mask))
Maarten Lankhorste8861672016-02-24 11:24:26 +010013646 continue;
13647
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013648 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013649 if (WARN_ON(ret != 0)) {
13650 crtc_mask &= ~(1 << pipe);
13651 continue;
13652 }
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013653
Daniel Vetter5a21b662016-05-24 17:13:53 +020013654 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13655 }
13656
13657 for_each_pipe(dev_priv, pipe) {
13658 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13659 long lret;
13660
13661 if (!((1 << pipe) & crtc_mask))
13662 continue;
13663
13664 lret = wait_event_timeout(dev->vblank[pipe].queue,
13665 last_vblank_count[pipe] !=
13666 drm_crtc_vblank_count(crtc),
13667 msecs_to_jiffies(50));
13668
13669 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
13670
13671 drm_crtc_vblank_put(crtc);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013672 }
13673}
13674
Daniel Vetter5a21b662016-05-24 17:13:53 +020013675static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013676{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013677 /* fb updated, need to unpin old fb */
13678 if (crtc_state->fb_changed)
13679 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013680
Daniel Vetter5a21b662016-05-24 17:13:53 +020013681 /* wm changes, need vblank before final wm's */
13682 if (crtc_state->update_wm_post)
13683 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013684
Daniel Vetter5a21b662016-05-24 17:13:53 +020013685 /*
13686 * cxsr is re-enabled after vblank.
13687 * This is already handled by crtc_state->update_wm_post,
13688 * but added for clarity.
13689 */
13690 if (crtc_state->disable_cxsr)
13691 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013692
Daniel Vetter5a21b662016-05-24 17:13:53 +020013693 return false;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013694}
13695
Daniel Vetter94f05022016-06-14 18:01:00 +020013696static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013697{
Daniel Vetter94f05022016-06-14 18:01:00 +020013698 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013699 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Jani Nikulafbee40d2014-03-31 14:27:18 +030013700 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013701 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013702 struct drm_crtc *crtc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013703 struct intel_crtc_state *intel_cstate;
Daniel Vetter94f05022016-06-14 18:01:00 +020013704 struct drm_plane *plane;
13705 struct drm_plane_state *plane_state;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013706 bool hw_check = intel_state->modeset;
13707 unsigned long put_domains[I915_MAX_PIPES] = {};
13708 unsigned crtc_vblank_mask = 0;
Daniel Vetter94f05022016-06-14 18:01:00 +020013709 int i, ret;
Daniel Vettera6778b32012-07-02 09:56:42 +020013710
Daniel Vetter94f05022016-06-14 18:01:00 +020013711 for_each_plane_in_state(state, plane, plane_state, i) {
13712 struct intel_plane_state *intel_plane_state =
13713 to_intel_plane_state(plane_state);
Daniel Vetterea0000f2016-06-13 16:13:46 +020013714
Daniel Vetter94f05022016-06-14 18:01:00 +020013715 if (!intel_plane_state->wait_req)
13716 continue;
13717
13718 ret = __i915_wait_request(intel_plane_state->wait_req,
13719 true, NULL, NULL);
13720 /* EIO should be eaten, and we can't get interrupted in the
13721 * worker, and blocking commits have waited already. */
13722 WARN_ON(ret);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013723 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013724
Daniel Vetterea0000f2016-06-13 16:13:46 +020013725 drm_atomic_helper_wait_for_dependencies(state);
13726
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013727 if (intel_state->modeset) {
13728 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13729 sizeof(intel_state->min_pixclk));
13730 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013731 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013732
13733 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013734 }
13735
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013736 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013737 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13738
Daniel Vetter5a21b662016-05-24 17:13:53 +020013739 if (needs_modeset(crtc->state) ||
13740 to_intel_crtc_state(crtc->state)->update_pipe) {
13741 hw_check = true;
13742
13743 put_domains[to_intel_crtc(crtc)->pipe] =
13744 modeset_get_crtc_power_domains(crtc,
13745 to_intel_crtc_state(crtc->state));
13746 }
13747
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013748 if (!needs_modeset(crtc->state))
13749 continue;
13750
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013751 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013752
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013753 if (old_crtc_state->active) {
13754 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013755 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013756 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013757 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013758 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013759
13760 /*
13761 * Underruns don't always raise
13762 * interrupts, so check manually.
13763 */
13764 intel_check_cpu_fifo_underruns(dev_priv);
13765 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013766
13767 if (!crtc->state->active)
13768 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013769 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013770 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013771
Daniel Vetterea9d7582012-07-10 10:42:52 +020013772 /* Only after disabling all output pipelines that will be changed can we
13773 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013774 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013775
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013776 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013777 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013778
13779 if (dev_priv->display.modeset_commit_cdclk &&
Clint Taylorc89e39f2016-05-13 23:41:21 +030013780 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013781 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013782 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013783
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013784 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013785 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013786
Daniel Vettera6778b32012-07-02 09:56:42 +020013787 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013788 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013789 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13790 bool modeset = needs_modeset(crtc->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013791 struct intel_crtc_state *pipe_config =
13792 to_intel_crtc_state(crtc->state);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013793
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013794 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013795 update_scanline_offset(to_intel_crtc(crtc));
13796 dev_priv->display.crtc_enable(crtc);
13797 }
13798
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013799 /* Complete events for now disable pipes here. */
13800 if (modeset && !crtc->state->active && crtc->state->event) {
13801 spin_lock_irq(&dev->event_lock);
13802 drm_crtc_send_vblank_event(crtc, crtc->state->event);
13803 spin_unlock_irq(&dev->event_lock);
13804
13805 crtc->state->event = NULL;
13806 }
13807
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013808 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013809 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013810
Daniel Vetter5a21b662016-05-24 17:13:53 +020013811 if (crtc->state->active &&
13812 drm_atomic_get_existing_plane_state(state, crtc->primary))
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020013813 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013814
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013815 if (crtc->state->active)
Daniel Vetter5a21b662016-05-24 17:13:53 +020013816 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013817
Daniel Vetter5a21b662016-05-24 17:13:53 +020013818 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13819 crtc_vblank_mask |= 1 << i;
Matt Ropered4a6a72016-02-23 17:20:13 -080013820 }
13821
Daniel Vetter94f05022016-06-14 18:01:00 +020013822 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13823 * already, but still need the state for the delayed optimization. To
13824 * fix this:
13825 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13826 * - schedule that vblank worker _before_ calling hw_done
13827 * - at the start of commit_tail, cancel it _synchrously
13828 * - switch over to the vblank wait helper in the core after that since
13829 * we don't need out special handling any more.
13830 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020013831 if (!state->legacy_cursor_update)
13832 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13833
13834 /*
13835 * Now that the vblank has passed, we can go ahead and program the
13836 * optimal watermarks on platforms that need two-step watermark
13837 * programming.
13838 *
13839 * TODO: Move this (and other cleanup) to an async worker eventually.
13840 */
13841 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13842 intel_cstate = to_intel_crtc_state(crtc->state);
13843
13844 if (dev_priv->display.optimize_watermarks)
13845 dev_priv->display.optimize_watermarks(intel_cstate);
13846 }
13847
13848 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13849 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13850
13851 if (put_domains[i])
13852 modeset_put_power_domains(dev_priv, put_domains[i]);
13853
13854 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13855 }
13856
Daniel Vetter94f05022016-06-14 18:01:00 +020013857 drm_atomic_helper_commit_hw_done(state);
13858
Daniel Vetter5a21b662016-05-24 17:13:53 +020013859 if (intel_state->modeset)
13860 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13861
13862 mutex_lock(&dev->struct_mutex);
13863 drm_atomic_helper_cleanup_planes(dev, state);
13864 mutex_unlock(&dev->struct_mutex);
13865
Daniel Vetterea0000f2016-06-13 16:13:46 +020013866 drm_atomic_helper_commit_cleanup_done(state);
13867
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013868 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013869
Mika Kuoppala75714942015-12-16 09:26:48 +020013870 /* As one of the primary mmio accessors, KMS has a high likelihood
13871 * of triggering bugs in unclaimed access. After we finish
13872 * modesetting, see if an error has been flagged, and if so
13873 * enable debugging for the next modeset - and hope we catch
13874 * the culprit.
13875 *
13876 * XXX note that we assume display power is on at this point.
13877 * This might hold true now but we need to add pm helper to check
13878 * unclaimed only when the hardware is on, as atomic commits
13879 * can happen also when the device is completely off.
13880 */
13881 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter94f05022016-06-14 18:01:00 +020013882}
13883
13884static void intel_atomic_commit_work(struct work_struct *work)
13885{
13886 struct drm_atomic_state *state = container_of(work,
13887 struct drm_atomic_state,
13888 commit_work);
13889 intel_atomic_commit_tail(state);
13890}
13891
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013892static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13893{
13894 struct drm_plane_state *old_plane_state;
13895 struct drm_plane *plane;
13896 struct drm_i915_gem_object *obj, *old_obj;
13897 struct intel_plane *intel_plane;
13898 int i;
13899
13900 mutex_lock(&state->dev->struct_mutex);
13901 for_each_plane_in_state(state, plane, old_plane_state, i) {
13902 obj = intel_fb_obj(plane->state->fb);
13903 old_obj = intel_fb_obj(old_plane_state->fb);
13904 intel_plane = to_intel_plane(plane);
13905
13906 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
13907 }
13908 mutex_unlock(&state->dev->struct_mutex);
13909}
13910
Daniel Vetter94f05022016-06-14 18:01:00 +020013911/**
13912 * intel_atomic_commit - commit validated state object
13913 * @dev: DRM device
13914 * @state: the top-level driver state object
13915 * @nonblock: nonblocking commit
13916 *
13917 * This function commits a top-level state object that has been validated
13918 * with drm_atomic_helper_check().
13919 *
13920 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13921 * nonblocking commits are only safe for pure plane updates. Everything else
13922 * should work though.
13923 *
13924 * RETURNS
13925 * Zero for success or -errno.
13926 */
13927static int intel_atomic_commit(struct drm_device *dev,
13928 struct drm_atomic_state *state,
13929 bool nonblock)
13930{
13931 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
13932 struct drm_i915_private *dev_priv = dev->dev_private;
13933 int ret = 0;
13934
13935 if (intel_state->modeset && nonblock) {
13936 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
13937 return -EINVAL;
13938 }
13939
13940 ret = drm_atomic_helper_setup_commit(state, nonblock);
13941 if (ret)
13942 return ret;
13943
13944 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
13945
13946 ret = intel_atomic_prepare_commit(dev, state, nonblock);
13947 if (ret) {
13948 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13949 return ret;
13950 }
13951
13952 drm_atomic_helper_swap_state(state, true);
13953 dev_priv->wm.distrust_bios_wm = false;
13954 dev_priv->wm.skl_results = intel_state->wm_results;
13955 intel_shared_dpll_commit(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013956 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020013957
13958 if (nonblock)
13959 queue_work(system_unbound_wq, &state->commit_work);
13960 else
13961 intel_atomic_commit_tail(state);
Mika Kuoppala75714942015-12-16 09:26:48 +020013962
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013963 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013964}
13965
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013966void intel_crtc_restore_mode(struct drm_crtc *crtc)
13967{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013968 struct drm_device *dev = crtc->dev;
13969 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013970 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013971 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013972
13973 state = drm_atomic_state_alloc(dev);
13974 if (!state) {
Ville Syrjälä78108b72016-05-27 20:59:19 +030013975 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13976 crtc->base.id, crtc->name);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013977 return;
13978 }
13979
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013980 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013981
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013982retry:
13983 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13984 ret = PTR_ERR_OR_ZERO(crtc_state);
13985 if (!ret) {
13986 if (!crtc_state->active)
13987 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013988
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013989 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013990 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013991 }
13992
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013993 if (ret == -EDEADLK) {
13994 drm_atomic_state_clear(state);
13995 drm_modeset_backoff(state->acquire_ctx);
13996 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013997 }
13998
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013999 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020014000out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030014001 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000014002}
14003
Daniel Vetter25c5b262012-07-08 22:08:04 +020014004#undef for_each_intel_crtc_masked
14005
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014006static const struct drm_crtc_funcs intel_crtc_funcs = {
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000014007 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020014008 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000014009 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014010 .destroy = intel_crtc_destroy,
Daniel Vetteree042aa2016-06-13 16:13:49 +020014011 .page_flip = drm_atomic_helper_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080014012 .atomic_duplicate_state = intel_crtc_duplicate_state,
14013 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010014014};
14015
Matt Roper6beb8c232014-12-01 15:40:14 -080014016/**
14017 * intel_prepare_plane_fb - Prepare fb for usage on plane
14018 * @plane: drm plane to prepare for
14019 * @fb: framebuffer to prepare for presentation
14020 *
14021 * Prepares a framebuffer for usage on a display plane. Generally this
14022 * involves pinning the underlying object and updating the frontbuffer tracking
14023 * bits. Some older platforms need special physical address handling for
14024 * cursor planes.
14025 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014026 * Must be called with struct_mutex held.
14027 *
Matt Roper6beb8c232014-12-01 15:40:14 -080014028 * Returns 0 on success, negative error code on failure.
14029 */
14030int
14031intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000014032 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070014033{
14034 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020014035 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080014036 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014037 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc37efb92016-06-17 08:28:47 +010014038 struct reservation_object *resv;
Matt Roper6beb8c232014-12-01 15:40:14 -080014039 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070014040
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014041 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070014042 return 0;
14043
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014044 if (old_obj) {
14045 struct drm_crtc_state *crtc_state =
14046 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14047
14048 /* Big Hammer, we also need to ensure that any pending
14049 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14050 * current scanout is retired before unpinning the old
14051 * framebuffer. Note that we rely on userspace rendering
14052 * into the buffer attached to the pipe they are waiting
14053 * on. If not, userspace generates a GPU hang with IPEHR
14054 * point to the MI_WAIT_FOR_EVENT.
14055 *
14056 * This should only fail upon a hung GPU, in which case we
14057 * can safely continue.
14058 */
14059 if (needs_modeset(crtc_state))
14060 ret = i915_gem_object_wait_rendering(old_obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014061 if (ret) {
14062 /* GPU hangs should have been swallowed by the wait */
14063 WARN_ON(ret == -EIO);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014064 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014065 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014066 }
14067
Chris Wilsonc37efb92016-06-17 08:28:47 +010014068 if (!obj)
14069 return 0;
14070
Daniel Vetter5a21b662016-05-24 17:13:53 +020014071 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010014072 resv = i915_gem_object_get_dmabuf_resv(obj);
14073 if (resv) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020014074 long lret;
14075
Chris Wilsonc37efb92016-06-17 08:28:47 +010014076 lret = reservation_object_wait_timeout_rcu(resv, false, true,
Daniel Vetter5a21b662016-05-24 17:13:53 +020014077 MAX_SCHEDULE_TIMEOUT);
14078 if (lret == -ERESTARTSYS)
14079 return lret;
14080
14081 WARN(lret < 0, "waiting returns %li\n", lret);
14082 }
14083
Chris Wilsonc37efb92016-06-17 08:28:47 +010014084 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080014085 INTEL_INFO(dev)->cursor_needs_physical) {
14086 int align = IS_I830(dev) ? 16 * 1024 : 256;
14087 ret = i915_gem_object_attach_phys(obj, align);
14088 if (ret)
14089 DRM_DEBUG_KMS("failed to attach phys object\n");
14090 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020014091 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080014092 }
14093
Chris Wilsonc37efb92016-06-17 08:28:47 +010014094 if (ret == 0) {
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014095 struct intel_plane_state *plane_state =
14096 to_intel_plane_state(new_state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014097
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014098 i915_gem_request_assign(&plane_state->wait_req,
14099 obj->last_write_req);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014100 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014101
Matt Roper6beb8c232014-12-01 15:40:14 -080014102 return ret;
14103}
14104
Matt Roper38f3ce32014-12-02 07:45:25 -080014105/**
14106 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14107 * @plane: drm plane to clean up for
14108 * @fb: old framebuffer that was on plane
14109 *
14110 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014111 *
14112 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080014113 */
14114void
14115intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000014116 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080014117{
14118 struct drm_device *dev = plane->dev;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014119 struct intel_plane_state *old_intel_state;
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014120 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14121 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080014122
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014123 old_intel_state = to_intel_plane_state(old_state);
14124
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014125 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080014126 return;
14127
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014128 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14129 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020014130 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014131
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014132 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070014133}
14134
Chandra Konduru6156a452015-04-27 13:48:39 -070014135int
14136skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14137{
14138 int max_scale;
14139 struct drm_device *dev;
14140 struct drm_i915_private *dev_priv;
14141 int crtc_clock, cdclk;
14142
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010014143 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070014144 return DRM_PLANE_HELPER_NO_SCALING;
14145
14146 dev = intel_crtc->base.dev;
14147 dev_priv = dev->dev_private;
14148 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014149 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070014150
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010014151 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070014152 return DRM_PLANE_HELPER_NO_SCALING;
14153
14154 /*
14155 * skl max scale is lower of:
14156 * close to 3 but not 3, -1 is for that purpose
14157 * or
14158 * cdclk/crtc_clock
14159 */
14160 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14161
14162 return max_scale;
14163}
14164
Matt Roper465c1202014-05-29 08:06:54 -070014165static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014166intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014167 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014168 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014169{
Matt Roper2b875c22014-12-01 15:40:13 -080014170 struct drm_crtc *crtc = state->base.crtc;
14171 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070014172 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014173 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14174 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014175
Ville Syrjälä693bdc22016-01-15 20:46:53 +020014176 if (INTEL_INFO(plane->dev)->gen >= 9) {
14177 /* use scaler when colorkey is not required */
14178 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14179 min_scale = 1;
14180 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14181 }
Sonika Jindald8106362015-04-10 14:37:28 +053014182 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014183 }
Sonika Jindald8106362015-04-10 14:37:28 +053014184
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014185 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14186 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014187 state->base.rotation,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014188 min_scale, max_scale,
14189 can_position, true,
14190 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070014191}
14192
Daniel Vetter5a21b662016-05-24 17:13:53 +020014193static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14194 struct drm_crtc_state *old_crtc_state)
14195{
14196 struct drm_device *dev = crtc->dev;
14197 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14198 struct intel_crtc_state *old_intel_state =
14199 to_intel_crtc_state(old_crtc_state);
14200 bool modeset = needs_modeset(crtc->state);
14201
14202 /* Perform vblank evasion around commit operation */
14203 intel_pipe_update_start(intel_crtc);
14204
14205 if (modeset)
14206 return;
14207
14208 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14209 intel_color_set_csc(crtc->state);
14210 intel_color_load_luts(crtc->state);
14211 }
14212
14213 if (to_intel_crtc_state(crtc->state)->update_pipe)
14214 intel_update_pipe_config(intel_crtc, old_intel_state);
14215 else if (INTEL_INFO(dev)->gen >= 9)
14216 skl_detach_scalers(intel_crtc);
14217}
14218
14219static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14220 struct drm_crtc_state *old_crtc_state)
14221{
14222 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14223
14224 intel_pipe_update_end(intel_crtc, NULL);
14225}
14226
Matt Ropercf4c7c12014-12-04 10:27:42 -080014227/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014228 * intel_plane_destroy - destroy a plane
14229 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014230 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014231 * Common destruction function for all types of planes (primary, cursor,
14232 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014233 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014234void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014235{
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014236 if (!plane)
14237 return;
14238
Matt Roper465c1202014-05-29 08:06:54 -070014239 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014240 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070014241}
14242
Matt Roper65a3fea2015-01-21 16:35:42 -080014243const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014244 .update_plane = drm_atomic_helper_update_plane,
14245 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014246 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014247 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014248 .atomic_get_property = intel_plane_atomic_get_property,
14249 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014250 .atomic_duplicate_state = intel_plane_duplicate_state,
14251 .atomic_destroy_state = intel_plane_destroy_state,
14252
Matt Roper465c1202014-05-29 08:06:54 -070014253};
14254
14255static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14256 int pipe)
14257{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014258 struct intel_plane *primary = NULL;
14259 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014260 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020014261 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014262 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014263
14264 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014265 if (!primary)
14266 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070014267
Matt Roper8e7d6882015-01-21 16:35:41 -080014268 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014269 if (!state)
14270 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014271 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014272
Matt Roper465c1202014-05-29 08:06:54 -070014273 primary->can_scale = false;
14274 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014275 if (INTEL_INFO(dev)->gen >= 9) {
14276 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014277 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014278 }
Matt Roper465c1202014-05-29 08:06:54 -070014279 primary->pipe = pipe;
14280 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014281 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014282 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014283 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14284 primary->plane = !pipe;
14285
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014286 if (INTEL_INFO(dev)->gen >= 9) {
14287 intel_primary_formats = skl_primary_formats;
14288 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014289
14290 primary->update_plane = skylake_update_primary_plane;
14291 primary->disable_plane = skylake_disable_primary_plane;
14292 } else if (HAS_PCH_SPLIT(dev)) {
14293 intel_primary_formats = i965_primary_formats;
14294 num_formats = ARRAY_SIZE(i965_primary_formats);
14295
14296 primary->update_plane = ironlake_update_primary_plane;
14297 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014298 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014299 intel_primary_formats = i965_primary_formats;
14300 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014301
14302 primary->update_plane = i9xx_update_primary_plane;
14303 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014304 } else {
14305 intel_primary_formats = i8xx_primary_formats;
14306 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014307
14308 primary->update_plane = i9xx_update_primary_plane;
14309 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014310 }
14311
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014312 if (INTEL_INFO(dev)->gen >= 9)
14313 ret = drm_universal_plane_init(dev, &primary->base, 0,
14314 &intel_plane_funcs,
14315 intel_primary_formats, num_formats,
14316 DRM_PLANE_TYPE_PRIMARY,
14317 "plane 1%c", pipe_name(pipe));
14318 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14319 ret = drm_universal_plane_init(dev, &primary->base, 0,
14320 &intel_plane_funcs,
14321 intel_primary_formats, num_formats,
14322 DRM_PLANE_TYPE_PRIMARY,
14323 "primary %c", pipe_name(pipe));
14324 else
14325 ret = drm_universal_plane_init(dev, &primary->base, 0,
14326 &intel_plane_funcs,
14327 intel_primary_formats, num_formats,
14328 DRM_PLANE_TYPE_PRIMARY,
14329 "plane %c", plane_name(primary->plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014330 if (ret)
14331 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053014332
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014333 if (INTEL_INFO(dev)->gen >= 4)
14334 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014335
Matt Roperea2c67b2014-12-23 10:41:52 -080014336 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14337
Matt Roper465c1202014-05-29 08:06:54 -070014338 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014339
14340fail:
14341 kfree(state);
14342 kfree(primary);
14343
14344 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014345}
14346
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014347void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14348{
14349 if (!dev->mode_config.rotation_property) {
14350 unsigned long flags = BIT(DRM_ROTATE_0) |
14351 BIT(DRM_ROTATE_180);
14352
14353 if (INTEL_INFO(dev)->gen >= 9)
14354 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14355
14356 dev->mode_config.rotation_property =
14357 drm_mode_create_rotation_property(dev, flags);
14358 }
14359 if (dev->mode_config.rotation_property)
14360 drm_object_attach_property(&plane->base.base,
14361 dev->mode_config.rotation_property,
14362 plane->base.state->rotation);
14363}
14364
Matt Roper3d7d6512014-06-10 08:28:13 -070014365static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014366intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014367 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014368 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014369{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014370 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014371 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014372 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014373 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014374 unsigned stride;
14375 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014376
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014377 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14378 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014379 state->base.rotation,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014380 DRM_PLANE_HELPER_NO_SCALING,
14381 DRM_PLANE_HELPER_NO_SCALING,
14382 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014383 if (ret)
14384 return ret;
14385
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014386 /* if we want to turn off the cursor ignore width and height */
14387 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014388 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014389
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014390 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014391 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014392 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14393 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014394 return -EINVAL;
14395 }
14396
Matt Roperea2c67b2014-12-23 10:41:52 -080014397 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14398 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014399 DRM_DEBUG_KMS("buffer is too small\n");
14400 return -ENOMEM;
14401 }
14402
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014403 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014404 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014405 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014406 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014407
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014408 /*
14409 * There's something wrong with the cursor on CHV pipe C.
14410 * If it straddles the left edge of the screen then
14411 * moving it away from the edge or disabling it often
14412 * results in a pipe underrun, and often that can lead to
14413 * dead pipe (constant underrun reported, and it scans
14414 * out just a solid color). To recover from that, the
14415 * display power well must be turned off and on again.
14416 * Refuse the put the cursor into that compromised position.
14417 */
14418 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14419 state->visible && state->base.crtc_x < 0) {
14420 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14421 return -EINVAL;
14422 }
14423
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014424 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014425}
14426
Matt Roperf4a2cf22014-12-01 15:40:12 -080014427static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014428intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014429 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014430{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014431 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14432
14433 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014434 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014435}
14436
14437static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014438intel_update_cursor_plane(struct drm_plane *plane,
14439 const struct intel_crtc_state *crtc_state,
14440 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014441{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014442 struct drm_crtc *crtc = crtc_state->base.crtc;
14443 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014444 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014445 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014446 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014447
Matt Roperf4a2cf22014-12-01 15:40:12 -080014448 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014449 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014450 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014451 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014452 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014453 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014454
Gustavo Padovana912f122014-12-01 15:40:10 -080014455 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014456 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014457}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014458
Matt Roper3d7d6512014-06-10 08:28:13 -070014459static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14460 int pipe)
14461{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014462 struct intel_plane *cursor = NULL;
14463 struct intel_plane_state *state = NULL;
14464 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070014465
14466 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014467 if (!cursor)
14468 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070014469
Matt Roper8e7d6882015-01-21 16:35:41 -080014470 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014471 if (!state)
14472 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014473 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014474
Matt Roper3d7d6512014-06-10 08:28:13 -070014475 cursor->can_scale = false;
14476 cursor->max_downscale = 1;
14477 cursor->pipe = pipe;
14478 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014479 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014480 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014481 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014482 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014483
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014484 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14485 &intel_plane_funcs,
14486 intel_cursor_formats,
14487 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014488 DRM_PLANE_TYPE_CURSOR,
14489 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014490 if (ret)
14491 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014492
14493 if (INTEL_INFO(dev)->gen >= 4) {
14494 if (!dev->mode_config.rotation_property)
14495 dev->mode_config.rotation_property =
14496 drm_mode_create_rotation_property(dev,
14497 BIT(DRM_ROTATE_0) |
14498 BIT(DRM_ROTATE_180));
14499 if (dev->mode_config.rotation_property)
14500 drm_object_attach_property(&cursor->base.base,
14501 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014502 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014503 }
14504
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014505 if (INTEL_INFO(dev)->gen >=9)
14506 state->scaler_id = -1;
14507
Matt Roperea2c67b2014-12-23 10:41:52 -080014508 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14509
Matt Roper3d7d6512014-06-10 08:28:13 -070014510 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014511
14512fail:
14513 kfree(state);
14514 kfree(cursor);
14515
14516 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014517}
14518
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014519static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14520 struct intel_crtc_state *crtc_state)
14521{
14522 int i;
14523 struct intel_scaler *intel_scaler;
14524 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14525
14526 for (i = 0; i < intel_crtc->num_scalers; i++) {
14527 intel_scaler = &scaler_state->scalers[i];
14528 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014529 intel_scaler->mode = PS_SCALER_MODE_DYN;
14530 }
14531
14532 scaler_state->scaler_id = -1;
14533}
14534
Hannes Ederb358d0a2008-12-18 21:18:47 +010014535static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014536{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014537 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014538 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014539 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014540 struct drm_plane *primary = NULL;
14541 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014542 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014543
Daniel Vetter955382f2013-09-19 14:05:45 +020014544 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014545 if (intel_crtc == NULL)
14546 return;
14547
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014548 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14549 if (!crtc_state)
14550 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014551 intel_crtc->config = crtc_state;
14552 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014553 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014554
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014555 /* initialize shared scalers */
14556 if (INTEL_INFO(dev)->gen >= 9) {
14557 if (pipe == PIPE_C)
14558 intel_crtc->num_scalers = 1;
14559 else
14560 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14561
14562 skl_init_scalers(dev, intel_crtc, crtc_state);
14563 }
14564
Matt Roper465c1202014-05-29 08:06:54 -070014565 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014566 if (!primary)
14567 goto fail;
14568
14569 cursor = intel_cursor_plane_create(dev, pipe);
14570 if (!cursor)
14571 goto fail;
14572
Matt Roper465c1202014-05-29 08:06:54 -070014573 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030014574 cursor, &intel_crtc_funcs,
14575 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070014576 if (ret)
14577 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014578
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014579 /*
14580 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014581 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014582 */
Jesse Barnes80824002009-09-10 15:28:06 -070014583 intel_crtc->pipe = pipe;
14584 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014585 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014586 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014587 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014588 }
14589
Chris Wilson4b0e3332014-05-30 16:35:26 +030014590 intel_crtc->cursor_base = ~0;
14591 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014592 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014593
Ville Syrjälä852eb002015-06-24 22:00:07 +030014594 intel_crtc->wm.cxsr_allowed = true;
14595
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014596 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14597 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14598 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14599 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14600
Jesse Barnes79e53942008-11-07 14:24:08 -080014601 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014602
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014603 intel_color_init(&intel_crtc->base);
14604
Daniel Vetter87b6b102014-05-15 15:33:46 +020014605 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014606 return;
14607
14608fail:
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014609 intel_plane_destroy(primary);
14610 intel_plane_destroy(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014611 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014612 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014613}
14614
Jesse Barnes752aa882013-10-31 18:55:49 +020014615enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14616{
14617 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014618 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014619
Rob Clark51fd3712013-11-19 12:10:12 -050014620 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014621
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014622 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014623 return INVALID_PIPE;
14624
14625 return to_intel_crtc(encoder->crtc)->pipe;
14626}
14627
Carl Worth08d7b3d2009-04-29 14:43:54 -070014628int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014629 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014630{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014631 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014632 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014633 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014634
Rob Clark7707e652014-07-17 23:30:04 -040014635 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010014636 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014637 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014638
Rob Clark7707e652014-07-17 23:30:04 -040014639 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014640 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014641
Daniel Vetterc05422d2009-08-11 16:05:30 +020014642 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014643}
14644
Daniel Vetter66a92782012-07-12 20:08:18 +020014645static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014646{
Daniel Vetter66a92782012-07-12 20:08:18 +020014647 struct drm_device *dev = encoder->base.dev;
14648 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014649 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014650 int entry = 0;
14651
Damien Lespiaub2784e12014-08-05 11:29:37 +010014652 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014653 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014654 index_mask |= (1 << entry);
14655
Jesse Barnes79e53942008-11-07 14:24:08 -080014656 entry++;
14657 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014658
Jesse Barnes79e53942008-11-07 14:24:08 -080014659 return index_mask;
14660}
14661
Chris Wilson4d302442010-12-14 19:21:29 +000014662static bool has_edp_a(struct drm_device *dev)
14663{
14664 struct drm_i915_private *dev_priv = dev->dev_private;
14665
14666 if (!IS_MOBILE(dev))
14667 return false;
14668
14669 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14670 return false;
14671
Damien Lespiaue3589902014-02-07 19:12:50 +000014672 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014673 return false;
14674
14675 return true;
14676}
14677
Jesse Barnes84b4e042014-06-25 08:24:29 -070014678static bool intel_crt_present(struct drm_device *dev)
14679{
14680 struct drm_i915_private *dev_priv = dev->dev_private;
14681
Damien Lespiau884497e2013-12-03 13:56:23 +000014682 if (INTEL_INFO(dev)->gen >= 9)
14683 return false;
14684
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014685 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014686 return false;
14687
14688 if (IS_CHERRYVIEW(dev))
14689 return false;
14690
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014691 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14692 return false;
14693
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014694 /* DDI E can't be used if DDI A requires 4 lanes */
14695 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14696 return false;
14697
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014698 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014699 return false;
14700
14701 return true;
14702}
14703
Jesse Barnes79e53942008-11-07 14:24:08 -080014704static void intel_setup_outputs(struct drm_device *dev)
14705{
Eric Anholt725e30a2009-01-22 13:01:02 -080014706 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014707 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014708 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014709
Imre Deak97a824e12016-06-21 11:51:47 +030014710 /*
14711 * intel_edp_init_connector() depends on this completing first, to
14712 * prevent the registeration of both eDP and LVDS and the incorrect
14713 * sharing of the PPS.
14714 */
Daniel Vetterc9093352013-06-06 22:22:47 +020014715 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014716
Jesse Barnes84b4e042014-06-25 08:24:29 -070014717 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014718 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014719
Vandana Kannanc776eb22014-08-19 12:05:01 +053014720 if (IS_BROXTON(dev)) {
14721 /*
14722 * FIXME: Broxton doesn't support port detection via the
14723 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14724 * detect the ports.
14725 */
14726 intel_ddi_init(dev, PORT_A);
14727 intel_ddi_init(dev, PORT_B);
14728 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014729
14730 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014731 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014732 int found;
14733
Jesse Barnesde31fac2015-03-06 15:53:32 -080014734 /*
14735 * Haswell uses DDI functions to detect digital outputs.
14736 * On SKL pre-D0 the strap isn't connected, so we assume
14737 * it's there.
14738 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014739 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014740 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014741 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014742 intel_ddi_init(dev, PORT_A);
14743
14744 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14745 * register */
14746 found = I915_READ(SFUSE_STRAP);
14747
14748 if (found & SFUSE_STRAP_DDIB_DETECTED)
14749 intel_ddi_init(dev, PORT_B);
14750 if (found & SFUSE_STRAP_DDIC_DETECTED)
14751 intel_ddi_init(dev, PORT_C);
14752 if (found & SFUSE_STRAP_DDID_DETECTED)
14753 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014754 /*
14755 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14756 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014757 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014758 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14759 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14760 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14761 intel_ddi_init(dev, PORT_E);
14762
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014763 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014764 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014765 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014766
14767 if (has_edp_a(dev))
14768 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014769
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014770 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014771 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014772 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014773 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014774 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014775 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014776 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014777 }
14778
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014779 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014780 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014781
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014782 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014783 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014784
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014785 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014786 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014787
Daniel Vetter270b3042012-10-27 15:52:05 +020014788 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014789 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014790 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014791 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010014792
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014793 /*
14794 * The DP_DETECTED bit is the latched state of the DDC
14795 * SDA pin at boot. However since eDP doesn't require DDC
14796 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14797 * eDP ports may have been muxed to an alternate function.
14798 * Thus we can't rely on the DP_DETECTED bit alone to detect
14799 * eDP ports. Consult the VBT as well as DP_DETECTED to
14800 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030014801 *
14802 * Sadly the straps seem to be missing sometimes even for HDMI
14803 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14804 * and VBT for the presence of the port. Additionally we can't
14805 * trust the port type the VBT declares as we've seen at least
14806 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014807 */
Chris Wilson457c52d2016-06-01 08:27:50 +010014808 has_edp = intel_dp_is_edp(dev, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014809 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14810 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010014811 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014812 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014813 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014814
Chris Wilson457c52d2016-06-01 08:27:50 +010014815 has_edp = intel_dp_is_edp(dev, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014816 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14817 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010014818 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014819 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014820 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014821
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014822 if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014823 /*
14824 * eDP not supported on port D,
14825 * so no need to worry about it
14826 */
14827 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14828 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014829 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014830 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
14831 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014832 }
14833
Jani Nikula3cfca972013-08-27 15:12:26 +030014834 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014835 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014836 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014837
Paulo Zanonie2debe92013-02-18 19:00:27 -030014838 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014839 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014840 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014841 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014842 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014843 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014844 }
Ma Ling27185ae2009-08-24 13:50:23 +080014845
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014846 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014847 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014848 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014849
14850 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014851
Paulo Zanonie2debe92013-02-18 19:00:27 -030014852 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014853 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014854 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014855 }
Ma Ling27185ae2009-08-24 13:50:23 +080014856
Paulo Zanonie2debe92013-02-18 19:00:27 -030014857 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014858
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014859 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014860 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014861 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014862 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014863 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014864 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014865 }
Ma Ling27185ae2009-08-24 13:50:23 +080014866
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014867 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014868 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014869 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014870 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014871 intel_dvo_init(dev);
14872
Zhenyu Wang103a1962009-11-27 11:44:36 +080014873 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014874 intel_tv_init(dev);
14875
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014876 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014877
Damien Lespiaub2784e12014-08-05 11:29:37 +010014878 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014879 encoder->base.possible_crtcs = encoder->crtc_mask;
14880 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014881 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014882 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014883
Paulo Zanonidde86e22012-12-01 12:04:25 -020014884 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014885
14886 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014887}
14888
14889static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14890{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014891 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014892 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014893
Daniel Vetteref2d6332014-02-10 18:00:38 +010014894 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014895 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014896 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014897 drm_gem_object_unreference(&intel_fb->obj->base);
14898 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014899 kfree(intel_fb);
14900}
14901
14902static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014903 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014904 unsigned int *handle)
14905{
14906 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014907 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014908
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014909 if (obj->userptr.mm) {
14910 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14911 return -EINVAL;
14912 }
14913
Chris Wilson05394f32010-11-08 19:18:58 +000014914 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014915}
14916
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014917static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14918 struct drm_file *file,
14919 unsigned flags, unsigned color,
14920 struct drm_clip_rect *clips,
14921 unsigned num_clips)
14922{
14923 struct drm_device *dev = fb->dev;
14924 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14925 struct drm_i915_gem_object *obj = intel_fb->obj;
14926
14927 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014928 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014929 mutex_unlock(&dev->struct_mutex);
14930
14931 return 0;
14932}
14933
Jesse Barnes79e53942008-11-07 14:24:08 -080014934static const struct drm_framebuffer_funcs intel_fb_funcs = {
14935 .destroy = intel_user_framebuffer_destroy,
14936 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014937 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014938};
14939
Damien Lespiaub3218032015-02-27 11:15:18 +000014940static
14941u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14942 uint32_t pixel_format)
14943{
14944 u32 gen = INTEL_INFO(dev)->gen;
14945
14946 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014947 int cpp = drm_format_plane_cpp(pixel_format, 0);
14948
Damien Lespiaub3218032015-02-27 11:15:18 +000014949 /* "The stride in bytes must not exceed the of the size of 8K
14950 * pixels and 32K bytes."
14951 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014952 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014953 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014954 return 32*1024;
14955 } else if (gen >= 4) {
14956 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14957 return 16*1024;
14958 else
14959 return 32*1024;
14960 } else if (gen >= 3) {
14961 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14962 return 8*1024;
14963 else
14964 return 16*1024;
14965 } else {
14966 /* XXX DSPC is limited to 4k tiled */
14967 return 8*1024;
14968 }
14969}
14970
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014971static int intel_framebuffer_init(struct drm_device *dev,
14972 struct intel_framebuffer *intel_fb,
14973 struct drm_mode_fb_cmd2 *mode_cmd,
14974 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014975{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014976 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014977 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014978 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014979 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014980
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014981 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14982
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014983 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14984 /* Enforce that fb modifier and tiling mode match, but only for
14985 * X-tiled. This is needed for FBC. */
14986 if (!!(obj->tiling_mode == I915_TILING_X) !=
14987 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14988 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14989 return -EINVAL;
14990 }
14991 } else {
14992 if (obj->tiling_mode == I915_TILING_X)
14993 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14994 else if (obj->tiling_mode == I915_TILING_Y) {
14995 DRM_DEBUG("No Y tiling for legacy addfb\n");
14996 return -EINVAL;
14997 }
14998 }
14999
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000015000 /* Passed in modifier sanity checking. */
15001 switch (mode_cmd->modifier[0]) {
15002 case I915_FORMAT_MOD_Y_TILED:
15003 case I915_FORMAT_MOD_Yf_TILED:
15004 if (INTEL_INFO(dev)->gen < 9) {
15005 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15006 mode_cmd->modifier[0]);
15007 return -EINVAL;
15008 }
15009 case DRM_FORMAT_MOD_NONE:
15010 case I915_FORMAT_MOD_X_TILED:
15011 break;
15012 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070015013 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15014 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010015015 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015016 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015017
Ville Syrjälä7b49f942016-01-12 21:08:32 +020015018 stride_alignment = intel_fb_stride_alignment(dev_priv,
15019 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000015020 mode_cmd->pixel_format);
15021 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15022 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15023 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010015024 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015025 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015026
Damien Lespiaub3218032015-02-27 11:15:18 +000015027 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
15028 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015029 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000015030 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15031 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015032 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010015033 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015034 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015035 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015036
Daniel Vetter2a80ead2015-02-10 17:16:06 +000015037 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015038 mode_cmd->pitches[0] != obj->stride) {
15039 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
15040 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015041 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015042 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020015043
Ville Syrjälä57779d02012-10-31 17:50:14 +020015044 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080015045 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020015046 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015047 case DRM_FORMAT_RGB565:
15048 case DRM_FORMAT_XRGB8888:
15049 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015050 break;
15051 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015052 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015053 DRM_DEBUG("unsupported pixel format: %s\n",
15054 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015055 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015056 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020015057 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020015058 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080015059 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15060 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015061 DRM_DEBUG("unsupported pixel format: %s\n",
15062 drm_get_format_name(mode_cmd->pixel_format));
15063 return -EINVAL;
15064 }
15065 break;
15066 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015067 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015068 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015069 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015070 DRM_DEBUG("unsupported pixel format: %s\n",
15071 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015072 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015073 }
Jesse Barnesb5626742011-06-24 12:19:27 -070015074 break;
Damien Lespiau75312082015-05-15 19:06:01 +010015075 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080015076 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010015077 DRM_DEBUG("unsupported pixel format: %s\n",
15078 drm_get_format_name(mode_cmd->pixel_format));
15079 return -EINVAL;
15080 }
15081 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020015082 case DRM_FORMAT_YUYV:
15083 case DRM_FORMAT_UYVY:
15084 case DRM_FORMAT_YVYU:
15085 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015086 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015087 DRM_DEBUG("unsupported pixel format: %s\n",
15088 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015089 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015090 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015091 break;
15092 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015093 DRM_DEBUG("unsupported pixel format: %s\n",
15094 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010015095 return -EINVAL;
15096 }
15097
Ville Syrjälä90f9a332012-10-31 17:50:19 +020015098 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15099 if (mode_cmd->offsets[0] != 0)
15100 return -EINVAL;
15101
Damien Lespiauec2c9812015-01-20 12:51:45 +000015102 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000015103 mode_cmd->pixel_format,
15104 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020015105 /* FIXME drm helper for size checks (especially planar formats)? */
15106 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
15107 return -EINVAL;
15108
Daniel Vetterc7d73f62012-12-13 23:38:38 +010015109 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15110 intel_fb->obj = obj;
15111
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020015112 intel_fill_fb_info(dev_priv, &intel_fb->base);
15113
Jesse Barnes79e53942008-11-07 14:24:08 -080015114 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15115 if (ret) {
15116 DRM_ERROR("framebuffer init failed %d\n", ret);
15117 return ret;
15118 }
15119
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020015120 intel_fb->obj->framebuffer_references++;
15121
Jesse Barnes79e53942008-11-07 14:24:08 -080015122 return 0;
15123}
15124
Jesse Barnes79e53942008-11-07 14:24:08 -080015125static struct drm_framebuffer *
15126intel_user_framebuffer_create(struct drm_device *dev,
15127 struct drm_file *filp,
Ville Syrjälä1eb834512015-11-11 19:11:29 +020015128 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080015129{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015130 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000015131 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020015132 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080015133
Chris Wilsona8ad0bd2016-05-09 11:04:54 +010015134 obj = to_intel_bo(drm_gem_object_lookup(filp, mode_cmd.handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000015135 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010015136 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080015137
Daniel Vetter92907cb2015-11-23 09:04:05 +010015138 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015139 if (IS_ERR(fb))
15140 drm_gem_object_unreference_unlocked(&obj->base);
15141
15142 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080015143}
15144
Daniel Vetter06957262015-08-10 13:34:08 +020015145#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020015146static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020015147{
15148}
15149#endif
15150
Jesse Barnes79e53942008-11-07 14:24:08 -080015151static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080015152 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020015153 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080015154 .atomic_check = intel_atomic_check,
15155 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020015156 .atomic_state_alloc = intel_atomic_state_alloc,
15157 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080015158};
15159
Imre Deak88212942016-03-16 13:38:53 +020015160/**
15161 * intel_init_display_hooks - initialize the display modesetting hooks
15162 * @dev_priv: device private
15163 */
15164void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070015165{
Imre Deak88212942016-03-16 13:38:53 +020015166 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015167 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015168 dev_priv->display.get_initial_plane_config =
15169 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015170 dev_priv->display.crtc_compute_clock =
15171 haswell_crtc_compute_clock;
15172 dev_priv->display.crtc_enable = haswell_crtc_enable;
15173 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015174 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015175 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015176 dev_priv->display.get_initial_plane_config =
15177 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020015178 dev_priv->display.crtc_compute_clock =
15179 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020015180 dev_priv->display.crtc_enable = haswell_crtc_enable;
15181 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015182 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015183 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015184 dev_priv->display.get_initial_plane_config =
15185 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020015186 dev_priv->display.crtc_compute_clock =
15187 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015188 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15189 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015190 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070015191 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015192 dev_priv->display.get_initial_plane_config =
15193 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015194 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15195 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15196 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15197 } else if (IS_VALLEYVIEW(dev_priv)) {
15198 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15199 dev_priv->display.get_initial_plane_config =
15200 i9xx_get_initial_plane_config;
15201 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070015202 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15203 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020015204 } else if (IS_G4X(dev_priv)) {
15205 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15206 dev_priv->display.get_initial_plane_config =
15207 i9xx_get_initial_plane_config;
15208 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15209 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15210 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020015211 } else if (IS_PINEVIEW(dev_priv)) {
15212 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15213 dev_priv->display.get_initial_plane_config =
15214 i9xx_get_initial_plane_config;
15215 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15216 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15217 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015218 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015219 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015220 dev_priv->display.get_initial_plane_config =
15221 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020015222 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015223 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15224 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015225 } else {
15226 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15227 dev_priv->display.get_initial_plane_config =
15228 i9xx_get_initial_plane_config;
15229 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15230 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15231 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070015232 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015233
Jesse Barnese70236a2009-09-21 10:42:27 -070015234 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020015235 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015236 dev_priv->display.get_display_clock_speed =
15237 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015238 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070015239 dev_priv->display.get_display_clock_speed =
15240 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015241 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015242 dev_priv->display.get_display_clock_speed =
15243 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015244 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015245 dev_priv->display.get_display_clock_speed =
15246 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015247 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070015248 dev_priv->display.get_display_clock_speed =
15249 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015250 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030015251 dev_priv->display.get_display_clock_speed =
15252 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015253 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15254 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015255 dev_priv->display.get_display_clock_speed =
15256 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015257 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015258 dev_priv->display.get_display_clock_speed =
15259 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015260 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015261 dev_priv->display.get_display_clock_speed =
15262 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015263 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015264 dev_priv->display.get_display_clock_speed =
15265 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015266 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015267 dev_priv->display.get_display_clock_speed =
15268 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015269 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015270 dev_priv->display.get_display_clock_speed =
15271 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015272 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015273 dev_priv->display.get_display_clock_speed =
15274 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015275 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015276 dev_priv->display.get_display_clock_speed =
15277 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015278 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015279 dev_priv->display.get_display_clock_speed =
15280 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015281 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015282 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030015283 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015284 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020015285 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070015286 dev_priv->display.get_display_clock_speed =
15287 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015288 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015289
Imre Deak88212942016-03-16 13:38:53 +020015290 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015291 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015292 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015293 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015294 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015295 /* FIXME: detect B0+ stepping and use auto training */
15296 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015297 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015298 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030015299 }
15300
15301 if (IS_BROADWELL(dev_priv)) {
15302 dev_priv->display.modeset_commit_cdclk =
15303 broadwell_modeset_commit_cdclk;
15304 dev_priv->display.modeset_calc_cdclk =
15305 broadwell_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015306 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015307 dev_priv->display.modeset_commit_cdclk =
15308 valleyview_modeset_commit_cdclk;
15309 dev_priv->display.modeset_calc_cdclk =
15310 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015311 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015312 dev_priv->display.modeset_commit_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015313 bxt_modeset_commit_cdclk;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015314 dev_priv->display.modeset_calc_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015315 bxt_modeset_calc_cdclk;
Clint Taylorc89e39f2016-05-13 23:41:21 +030015316 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15317 dev_priv->display.modeset_commit_cdclk =
15318 skl_modeset_commit_cdclk;
15319 dev_priv->display.modeset_calc_cdclk =
15320 skl_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015321 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020015322
15323 switch (INTEL_INFO(dev_priv)->gen) {
15324 case 2:
15325 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15326 break;
15327
15328 case 3:
15329 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15330 break;
15331
15332 case 4:
15333 case 5:
15334 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15335 break;
15336
15337 case 6:
15338 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15339 break;
15340 case 7:
15341 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15342 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15343 break;
15344 case 9:
15345 /* Drop through - unsupported since execlist only. */
15346 default:
15347 /* Default just returns -ENODEV to indicate unsupported */
15348 dev_priv->display.queue_flip = intel_default_queue_flip;
15349 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015350}
15351
Jesse Barnesb690e962010-07-19 13:53:12 -070015352/*
15353 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15354 * resume, or other times. This quirk makes sure that's the case for
15355 * affected systems.
15356 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015357static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015358{
15359 struct drm_i915_private *dev_priv = dev->dev_private;
15360
15361 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015362 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015363}
15364
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015365static void quirk_pipeb_force(struct drm_device *dev)
15366{
15367 struct drm_i915_private *dev_priv = dev->dev_private;
15368
15369 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15370 DRM_INFO("applying pipe b force quirk\n");
15371}
15372
Keith Packard435793d2011-07-12 14:56:22 -070015373/*
15374 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15375 */
15376static void quirk_ssc_force_disable(struct drm_device *dev)
15377{
15378 struct drm_i915_private *dev_priv = dev->dev_private;
15379 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015380 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015381}
15382
Carsten Emde4dca20e2012-03-15 15:56:26 +010015383/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015384 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15385 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015386 */
15387static void quirk_invert_brightness(struct drm_device *dev)
15388{
15389 struct drm_i915_private *dev_priv = dev->dev_private;
15390 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015391 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015392}
15393
Scot Doyle9c72cc62014-07-03 23:27:50 +000015394/* Some VBT's incorrectly indicate no backlight is present */
15395static void quirk_backlight_present(struct drm_device *dev)
15396{
15397 struct drm_i915_private *dev_priv = dev->dev_private;
15398 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15399 DRM_INFO("applying backlight present quirk\n");
15400}
15401
Jesse Barnesb690e962010-07-19 13:53:12 -070015402struct intel_quirk {
15403 int device;
15404 int subsystem_vendor;
15405 int subsystem_device;
15406 void (*hook)(struct drm_device *dev);
15407};
15408
Egbert Eich5f85f172012-10-14 15:46:38 +020015409/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15410struct intel_dmi_quirk {
15411 void (*hook)(struct drm_device *dev);
15412 const struct dmi_system_id (*dmi_id_list)[];
15413};
15414
15415static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15416{
15417 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15418 return 1;
15419}
15420
15421static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15422 {
15423 .dmi_id_list = &(const struct dmi_system_id[]) {
15424 {
15425 .callback = intel_dmi_reverse_brightness,
15426 .ident = "NCR Corporation",
15427 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15428 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15429 },
15430 },
15431 { } /* terminating entry */
15432 },
15433 .hook = quirk_invert_brightness,
15434 },
15435};
15436
Ben Widawskyc43b5632012-04-16 14:07:40 -070015437static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015438 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15439 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15440
Jesse Barnesb690e962010-07-19 13:53:12 -070015441 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15442 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15443
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015444 /* 830 needs to leave pipe A & dpll A up */
15445 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15446
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015447 /* 830 needs to leave pipe B & dpll B up */
15448 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15449
Keith Packard435793d2011-07-12 14:56:22 -070015450 /* Lenovo U160 cannot use SSC on LVDS */
15451 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015452
15453 /* Sony Vaio Y cannot use SSC on LVDS */
15454 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015455
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015456 /* Acer Aspire 5734Z must invert backlight brightness */
15457 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15458
15459 /* Acer/eMachines G725 */
15460 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15461
15462 /* Acer/eMachines e725 */
15463 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15464
15465 /* Acer/Packard Bell NCL20 */
15466 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15467
15468 /* Acer Aspire 4736Z */
15469 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015470
15471 /* Acer Aspire 5336 */
15472 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015473
15474 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15475 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015476
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015477 /* Acer C720 Chromebook (Core i3 4005U) */
15478 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15479
jens steinb2a96012014-10-28 20:25:53 +010015480 /* Apple Macbook 2,1 (Core 2 T7400) */
15481 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15482
Jani Nikula1b9448b02015-11-05 11:49:59 +020015483 /* Apple Macbook 4,1 */
15484 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15485
Scot Doyled4967d82014-07-03 23:27:52 +000015486 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15487 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015488
15489 /* HP Chromebook 14 (Celeron 2955U) */
15490 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015491
15492 /* Dell Chromebook 11 */
15493 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015494
15495 /* Dell Chromebook 11 (2015 version) */
15496 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015497};
15498
15499static void intel_init_quirks(struct drm_device *dev)
15500{
15501 struct pci_dev *d = dev->pdev;
15502 int i;
15503
15504 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15505 struct intel_quirk *q = &intel_quirks[i];
15506
15507 if (d->device == q->device &&
15508 (d->subsystem_vendor == q->subsystem_vendor ||
15509 q->subsystem_vendor == PCI_ANY_ID) &&
15510 (d->subsystem_device == q->subsystem_device ||
15511 q->subsystem_device == PCI_ANY_ID))
15512 q->hook(dev);
15513 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015514 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15515 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15516 intel_dmi_quirks[i].hook(dev);
15517 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015518}
15519
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015520/* Disable the VGA plane that we never use */
15521static void i915_disable_vga(struct drm_device *dev)
15522{
15523 struct drm_i915_private *dev_priv = dev->dev_private;
15524 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015525 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015526
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015527 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015528 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015529 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015530 sr1 = inb(VGA_SR_DATA);
15531 outb(sr1 | 1<<5, VGA_SR_DATA);
15532 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15533 udelay(300);
15534
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015535 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015536 POSTING_READ(vga_reg);
15537}
15538
Daniel Vetterf8175862012-04-10 15:50:11 +020015539void intel_modeset_init_hw(struct drm_device *dev)
15540{
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015541 struct drm_i915_private *dev_priv = dev->dev_private;
15542
Ville Syrjäläb6283052015-06-03 15:45:07 +030015543 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015544
15545 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15546
Daniel Vetterf8175862012-04-10 15:50:11 +020015547 intel_init_clock_gating(dev);
Chris Wilsondc979972016-05-10 14:10:04 +010015548 intel_enable_gt_powersave(dev_priv);
Daniel Vetterf8175862012-04-10 15:50:11 +020015549}
15550
Matt Roperd93c0372015-12-03 11:37:41 -080015551/*
15552 * Calculate what we think the watermarks should be for the state we've read
15553 * out of the hardware and then immediately program those watermarks so that
15554 * we ensure the hardware settings match our internal state.
15555 *
15556 * We can calculate what we think WM's should be by creating a duplicate of the
15557 * current state (which was constructed during hardware readout) and running it
15558 * through the atomic check code to calculate new watermark values in the
15559 * state object.
15560 */
15561static void sanitize_watermarks(struct drm_device *dev)
15562{
15563 struct drm_i915_private *dev_priv = to_i915(dev);
15564 struct drm_atomic_state *state;
15565 struct drm_crtc *crtc;
15566 struct drm_crtc_state *cstate;
15567 struct drm_modeset_acquire_ctx ctx;
15568 int ret;
15569 int i;
15570
15571 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015572 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015573 return;
15574
15575 /*
15576 * We need to hold connection_mutex before calling duplicate_state so
15577 * that the connector loop is protected.
15578 */
15579 drm_modeset_acquire_init(&ctx, 0);
15580retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015581 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015582 if (ret == -EDEADLK) {
15583 drm_modeset_backoff(&ctx);
15584 goto retry;
15585 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015586 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015587 }
15588
15589 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15590 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015591 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015592
Matt Ropered4a6a72016-02-23 17:20:13 -080015593 /*
15594 * Hardware readout is the only time we don't want to calculate
15595 * intermediate watermarks (since we don't trust the current
15596 * watermarks).
15597 */
15598 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15599
Matt Roperd93c0372015-12-03 11:37:41 -080015600 ret = intel_atomic_check(dev, state);
15601 if (ret) {
15602 /*
15603 * If we fail here, it means that the hardware appears to be
15604 * programmed in a way that shouldn't be possible, given our
15605 * understanding of watermark requirements. This might mean a
15606 * mistake in the hardware readout code or a mistake in the
15607 * watermark calculations for a given platform. Raise a WARN
15608 * so that this is noticeable.
15609 *
15610 * If this actually happens, we'll have to just leave the
15611 * BIOS-programmed watermarks untouched and hope for the best.
15612 */
15613 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015614 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015615 }
15616
15617 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080015618 for_each_crtc_in_state(state, crtc, cstate, i) {
15619 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15620
Matt Ropered4a6a72016-02-23 17:20:13 -080015621 cs->wm.need_postvbl_update = true;
15622 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015623 }
15624
15625 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015626fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015627 drm_modeset_drop_locks(&ctx);
15628 drm_modeset_acquire_fini(&ctx);
15629}
15630
Jesse Barnes79e53942008-11-07 14:24:08 -080015631void intel_modeset_init(struct drm_device *dev)
15632{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015633 struct drm_i915_private *dev_priv = to_i915(dev);
15634 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015635 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015636 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015637 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015638
15639 drm_mode_config_init(dev);
15640
15641 dev->mode_config.min_width = 0;
15642 dev->mode_config.min_height = 0;
15643
Dave Airlie019d96c2011-09-29 16:20:42 +010015644 dev->mode_config.preferred_depth = 24;
15645 dev->mode_config.prefer_shadow = 1;
15646
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015647 dev->mode_config.allow_fb_modifiers = true;
15648
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015649 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015650
Jesse Barnesb690e962010-07-19 13:53:12 -070015651 intel_init_quirks(dev);
15652
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015653 intel_init_pm(dev);
15654
Ben Widawskye3c74752013-04-05 13:12:39 -070015655 if (INTEL_INFO(dev)->num_pipes == 0)
15656 return;
15657
Lukas Wunner69f92f62015-07-15 13:57:35 +020015658 /*
15659 * There may be no VBT; and if the BIOS enabled SSC we can
15660 * just keep using it to avoid unnecessary flicker. Whereas if the
15661 * BIOS isn't using it, don't assume it will work even if the VBT
15662 * indicates as much.
15663 */
15664 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15665 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15666 DREF_SSC1_ENABLE);
15667
15668 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15669 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15670 bios_lvds_use_ssc ? "en" : "dis",
15671 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15672 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15673 }
15674 }
15675
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015676 if (IS_GEN2(dev)) {
15677 dev->mode_config.max_width = 2048;
15678 dev->mode_config.max_height = 2048;
15679 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015680 dev->mode_config.max_width = 4096;
15681 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015682 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015683 dev->mode_config.max_width = 8192;
15684 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015685 }
Damien Lespiau068be562014-03-28 14:17:49 +000015686
Ville Syrjälädc41c152014-08-13 11:57:05 +030015687 if (IS_845G(dev) || IS_I865G(dev)) {
15688 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15689 dev->mode_config.cursor_height = 1023;
15690 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015691 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15692 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15693 } else {
15694 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15695 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15696 }
15697
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015698 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015699
Zhao Yakui28c97732009-10-09 11:39:41 +080015700 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015701 INTEL_INFO(dev)->num_pipes,
15702 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015703
Damien Lespiau055e3932014-08-18 13:49:10 +010015704 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015705 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015706 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015707 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015708 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015709 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015710 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015711 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015712 }
15713
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015714 intel_update_czclk(dev_priv);
15715 intel_update_cdclk(dev);
15716
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015717 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015718
Ville Syrjäläb2045352016-05-13 23:41:27 +030015719 if (dev_priv->max_cdclk_freq == 0)
15720 intel_update_max_cdclk(dev);
15721
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015722 /* Just disable it once at startup */
15723 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015724 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015725
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015726 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015727 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015728 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015729
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015730 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015731 struct intel_initial_plane_config plane_config = {};
15732
Jesse Barnes46f297f2014-03-07 08:57:48 -080015733 if (!crtc->active)
15734 continue;
15735
Jesse Barnes46f297f2014-03-07 08:57:48 -080015736 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015737 * Note that reserving the BIOS fb up front prevents us
15738 * from stuffing other stolen allocations like the ring
15739 * on top. This prevents some ugliness at boot time, and
15740 * can even allow for smooth boot transitions if the BIOS
15741 * fb is large enough for the active pipe configuration.
15742 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015743 dev_priv->display.get_initial_plane_config(crtc,
15744 &plane_config);
15745
15746 /*
15747 * If the fb is shared between multiple heads, we'll
15748 * just get the first one.
15749 */
15750 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015751 }
Matt Roperd93c0372015-12-03 11:37:41 -080015752
15753 /*
15754 * Make sure hardware watermarks really match the state we read out.
15755 * Note that we need to do this after reconstructing the BIOS fb's
15756 * since the watermark calculation done here will use pstate->fb.
15757 */
15758 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015759}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015760
Daniel Vetter7fad7982012-07-04 17:51:47 +020015761static void intel_enable_pipe_a(struct drm_device *dev)
15762{
15763 struct intel_connector *connector;
15764 struct drm_connector *crt = NULL;
15765 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015766 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015767
15768 /* We can't just switch on the pipe A, we need to set things up with a
15769 * proper mode and output configuration. As a gross hack, enable pipe A
15770 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015771 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015772 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15773 crt = &connector->base;
15774 break;
15775 }
15776 }
15777
15778 if (!crt)
15779 return;
15780
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015781 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015782 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015783}
15784
Daniel Vetterfa555832012-10-10 23:14:00 +020015785static bool
15786intel_check_plane_mapping(struct intel_crtc *crtc)
15787{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015788 struct drm_device *dev = crtc->base.dev;
15789 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä649636e2015-09-22 19:50:01 +030015790 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015791
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015792 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015793 return true;
15794
Ville Syrjälä649636e2015-09-22 19:50:01 +030015795 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015796
15797 if ((val & DISPLAY_PLANE_ENABLE) &&
15798 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15799 return false;
15800
15801 return true;
15802}
15803
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015804static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15805{
15806 struct drm_device *dev = crtc->base.dev;
15807 struct intel_encoder *encoder;
15808
15809 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15810 return true;
15811
15812 return false;
15813}
15814
Ville Syrjälädd756192016-02-17 21:28:45 +020015815static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15816{
15817 struct drm_device *dev = encoder->base.dev;
15818 struct intel_connector *connector;
15819
15820 for_each_connector_on_encoder(dev, &encoder->base, connector)
15821 return true;
15822
15823 return false;
15824}
15825
Daniel Vetter24929352012-07-02 20:28:59 +020015826static void intel_sanitize_crtc(struct intel_crtc *crtc)
15827{
15828 struct drm_device *dev = crtc->base.dev;
15829 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula4d1de972016-03-18 17:05:42 +020015830 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015831
Daniel Vetter24929352012-07-02 20:28:59 +020015832 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015833 if (!transcoder_is_dsi(cpu_transcoder)) {
15834 i915_reg_t reg = PIPECONF(cpu_transcoder);
15835
15836 I915_WRITE(reg,
15837 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15838 }
Daniel Vetter24929352012-07-02 20:28:59 +020015839
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015840 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015841 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015842 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015843 struct intel_plane *plane;
15844
Daniel Vetter96256042015-02-13 21:03:42 +010015845 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015846
15847 /* Disable everything but the primary plane */
15848 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15849 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15850 continue;
15851
15852 plane->disable_plane(&plane->base, &crtc->base);
15853 }
Daniel Vetter96256042015-02-13 21:03:42 +010015854 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015855
Daniel Vetter24929352012-07-02 20:28:59 +020015856 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015857 * disable the crtc (and hence change the state) if it is wrong. Note
15858 * that gen4+ has a fixed plane -> pipe mapping. */
15859 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015860 bool plane;
15861
Ville Syrjälä78108b72016-05-27 20:59:19 +030015862 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15863 crtc->base.base.id, crtc->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015864
15865 /* Pipe has the wrong plane attached and the plane is active.
15866 * Temporarily change the plane mapping and disable everything
15867 * ... */
15868 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015869 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015870 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015871 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015872 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015873 }
Daniel Vetter24929352012-07-02 20:28:59 +020015874
Daniel Vetter7fad7982012-07-04 17:51:47 +020015875 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15876 crtc->pipe == PIPE_A && !crtc->active) {
15877 /* BIOS forgot to enable pipe A, this mostly happens after
15878 * resume. Force-enable the pipe to fix this, the update_dpms
15879 * call below we restore the pipe to the right state, but leave
15880 * the required bits on. */
15881 intel_enable_pipe_a(dev);
15882 }
15883
Daniel Vetter24929352012-07-02 20:28:59 +020015884 /* Adjust the state of the output pipe according to whether we
15885 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015886 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015887 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015888
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015889 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015890 /*
15891 * We start out with underrun reporting disabled to avoid races.
15892 * For correct bookkeeping mark this on active crtcs.
15893 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015894 * Also on gmch platforms we dont have any hardware bits to
15895 * disable the underrun reporting. Which means we need to start
15896 * out with underrun reporting disabled also on inactive pipes,
15897 * since otherwise we'll complain about the garbage we read when
15898 * e.g. coming up after runtime pm.
15899 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015900 * No protection against concurrent access is required - at
15901 * worst a fifo underrun happens which also sets this to false.
15902 */
15903 crtc->cpu_fifo_underrun_disabled = true;
15904 crtc->pch_fifo_underrun_disabled = true;
15905 }
Daniel Vetter24929352012-07-02 20:28:59 +020015906}
15907
15908static void intel_sanitize_encoder(struct intel_encoder *encoder)
15909{
15910 struct intel_connector *connector;
15911 struct drm_device *dev = encoder->base.dev;
15912
15913 /* We need to check both for a crtc link (meaning that the
15914 * encoder is active and trying to read from a pipe) and the
15915 * pipe itself being active. */
15916 bool has_active_crtc = encoder->base.crtc &&
15917 to_intel_crtc(encoder->base.crtc)->active;
15918
Ville Syrjälädd756192016-02-17 21:28:45 +020015919 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015920 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15921 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015922 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015923
15924 /* Connector is active, but has no active pipe. This is
15925 * fallout from our resume register restoring. Disable
15926 * the encoder manually again. */
15927 if (encoder->base.crtc) {
15928 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15929 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015930 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015931 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015932 if (encoder->post_disable)
15933 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015934 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015935 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015936
15937 /* Inconsistent output/port/pipe state happens presumably due to
15938 * a bug in one of the get_hw_state functions. Or someplace else
15939 * in our code, like the register restore mess on resume. Clamp
15940 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015941 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015942 if (connector->encoder != encoder)
15943 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015944 connector->base.dpms = DRM_MODE_DPMS_OFF;
15945 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015946 }
15947 }
15948 /* Enabled encoders without active connectors will be fixed in
15949 * the crtc fixup. */
15950}
15951
Imre Deak04098752014-02-18 00:02:16 +020015952void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015953{
15954 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015955 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015956
Imre Deak04098752014-02-18 00:02:16 +020015957 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15958 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15959 i915_disable_vga(dev);
15960 }
15961}
15962
15963void i915_redisable_vga(struct drm_device *dev)
15964{
15965 struct drm_i915_private *dev_priv = dev->dev_private;
15966
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015967 /* This function can be called both from intel_modeset_setup_hw_state or
15968 * at a very early point in our resume sequence, where the power well
15969 * structures are not yet restored. Since this function is at a very
15970 * paranoid "someone might have enabled VGA while we were not looking"
15971 * level, just check if the power well is enabled instead of trying to
15972 * follow the "don't touch the power well if we don't need it" policy
15973 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015974 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015975 return;
15976
Imre Deak04098752014-02-18 00:02:16 +020015977 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015978
15979 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015980}
15981
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015982static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015983{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015984 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015985
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015986 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015987}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015988
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015989/* FIXME read out full plane state for all planes */
15990static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015991{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015992 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015993 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015994 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015995
Matt Roper19b8d382015-09-24 15:53:17 -070015996 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015997 primary_get_hw_state(to_intel_plane(primary));
15998
15999 if (plane_state->visible)
16000 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030016001}
16002
Daniel Vetter30e984d2013-06-05 13:34:17 +020016003static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020016004{
16005 struct drm_i915_private *dev_priv = dev->dev_private;
16006 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020016007 struct intel_crtc *crtc;
16008 struct intel_encoder *encoder;
16009 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020016010 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020016011
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016012 dev_priv->active_crtcs = 0;
16013
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016014 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016015 struct intel_crtc_state *crtc_state = crtc->config;
16016 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020016017
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020016018 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016019 memset(crtc_state, 0, sizeof(*crtc_state));
16020 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020016021
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016022 crtc_state->base.active = crtc_state->base.enable =
16023 dev_priv->display.get_pipe_config(crtc, crtc_state);
16024
16025 crtc->base.enabled = crtc_state->base.enable;
16026 crtc->active = crtc_state->base.active;
16027
16028 if (crtc_state->base.active) {
16029 dev_priv->active_crtcs |= 1 << crtc->pipe;
16030
Clint Taylorc89e39f2016-05-13 23:41:21 +030016031 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016032 pixclk = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016033 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016034 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16035 else
16036 WARN_ON(dev_priv->display.modeset_calc_cdclk);
Ville Syrjälä9558d152016-05-13 23:41:20 +030016037
16038 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16039 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16040 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010016041 }
16042
16043 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030016044
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030016045 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020016046
Ville Syrjälä78108b72016-05-27 20:59:19 +030016047 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16048 crtc->base.base.id, crtc->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016049 crtc->active ? "enabled" : "disabled");
16050 }
16051
Daniel Vetter53589012013-06-05 13:34:16 +020016052 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16053 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16054
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016055 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16056 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016057 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016058 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016059 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016060 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020016061 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016062 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020016063
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020016064 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016065 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020016066 }
16067
Damien Lespiaub2784e12014-08-05 11:29:37 +010016068 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016069 pipe = 0;
16070
16071 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070016072 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16073 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016074 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020016075 } else {
16076 encoder->base.crtc = NULL;
16077 }
16078
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016079 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020016080 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016081 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016082 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016083 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020016084 }
16085
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016086 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020016087 if (connector->get_hw_state(connector)) {
16088 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016089
16090 encoder = connector->encoder;
16091 connector->base.encoder = &encoder->base;
16092
16093 if (encoder->base.crtc &&
16094 encoder->base.crtc->state->active) {
16095 /*
16096 * This has to be done during hardware readout
16097 * because anything calling .crtc_disable may
16098 * rely on the connector_mask being accurate.
16099 */
16100 encoder->base.crtc->state->connector_mask |=
16101 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010016102 encoder->base.crtc->state->encoder_mask |=
16103 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016104 }
16105
Daniel Vetter24929352012-07-02 20:28:59 +020016106 } else {
16107 connector->base.dpms = DRM_MODE_DPMS_OFF;
16108 connector->base.encoder = NULL;
16109 }
16110 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16111 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030016112 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016113 connector->base.encoder ? "enabled" : "disabled");
16114 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016115
16116 for_each_intel_crtc(dev, crtc) {
16117 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16118
16119 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16120 if (crtc->base.state->active) {
16121 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16122 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16123 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16124
16125 /*
16126 * The initial mode needs to be set in order to keep
16127 * the atomic core happy. It wants a valid mode if the
16128 * crtc's enabled, so we do the above call.
16129 *
16130 * At this point some state updated by the connectors
16131 * in their ->detect() callback has not run yet, so
16132 * no recalculation can be done yet.
16133 *
16134 * Even if we could do a recalculation and modeset
16135 * right now it would cause a double modeset if
16136 * fbdev or userspace chooses a different initial mode.
16137 *
16138 * If that happens, someone indicated they wanted a
16139 * mode change, which means it's safe to do a full
16140 * recalculation.
16141 */
16142 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030016143
16144 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16145 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016146 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020016147
16148 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016149 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020016150}
16151
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016152/* Scan out the current hw modeset state,
16153 * and sanitizes it to the current state
16154 */
16155static void
16156intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020016157{
16158 struct drm_i915_private *dev_priv = dev->dev_private;
16159 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016160 struct intel_crtc *crtc;
16161 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020016162 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016163
16164 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016165
16166 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010016167 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016168 intel_sanitize_encoder(encoder);
16169 }
16170
Damien Lespiau055e3932014-08-18 13:49:10 +010016171 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020016172 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16173 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016174 intel_dump_pipe_config(crtc, crtc->config,
16175 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020016176 }
Daniel Vetter9a935852012-07-05 22:34:27 +020016177
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020016178 intel_modeset_update_connector_atomic_state(dev);
16179
Daniel Vetter35c95372013-07-17 06:55:04 +020016180 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16181 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16182
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016183 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020016184 continue;
16185
16186 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16187
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016188 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020016189 pll->on = false;
16190 }
16191
Wayne Boyer666a4532015-12-09 12:29:35 -080016192 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030016193 vlv_wm_get_hw_state(dev);
16194 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000016195 skl_wm_get_hw_state(dev);
16196 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030016197 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016198
16199 for_each_intel_crtc(dev, crtc) {
16200 unsigned long put_domains;
16201
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010016202 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016203 if (WARN_ON(put_domains))
16204 modeset_put_power_domains(dev_priv, put_domains);
16205 }
16206 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020016207
16208 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016209}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030016210
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016211void intel_display_resume(struct drm_device *dev)
16212{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016213 struct drm_i915_private *dev_priv = to_i915(dev);
16214 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16215 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016216 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016217 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020016218
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016219 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016220
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016221 /*
16222 * This is a cludge because with real atomic modeset mode_config.mutex
16223 * won't be taken. Unfortunately some probed state like
16224 * audio_codec_enable is still protected by mode_config.mutex, so lock
16225 * it here for now.
16226 */
16227 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016228 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016229
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016230retry:
16231 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016232
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016233 if (ret == 0 && !setup) {
16234 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016235
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016236 intel_modeset_setup_hw_state(dev);
16237 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010016238 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020016239
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016240 if (ret == 0 && state) {
16241 struct drm_crtc_state *crtc_state;
16242 struct drm_crtc *crtc;
16243 int i;
16244
16245 state->acquire_ctx = &ctx;
16246
Ville Syrjäläe3d54572016-05-13 10:10:42 -070016247 /* ignore any reset values/BIOS leftovers in the WM registers */
16248 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16249
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016250 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16251 /*
16252 * Force recalculation even if we restore
16253 * current state. With fast modeset this may not result
16254 * in a modeset when the state is compatible.
16255 */
16256 crtc_state->mode_changed = true;
16257 }
16258
16259 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016260 }
16261
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016262 if (ret == -EDEADLK) {
16263 drm_modeset_backoff(&ctx);
16264 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016265 }
16266
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016267 drm_modeset_drop_locks(&ctx);
16268 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016269 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016270
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016271 if (ret) {
16272 DRM_ERROR("Restoring old state failed with %i\n", ret);
16273 drm_atomic_state_free(state);
16274 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010016275}
16276
16277void intel_modeset_gem_init(struct drm_device *dev)
16278{
Chris Wilsondc979972016-05-10 14:10:04 +010016279 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016280 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070016281 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016282 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080016283
Chris Wilsondc979972016-05-10 14:10:04 +010016284 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016285
Chris Wilson1833b132012-05-09 11:56:28 +010016286 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020016287
Chris Wilson1ee8da62016-05-12 12:43:23 +010016288 intel_setup_overlay(dev_priv);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016289
16290 /*
16291 * Make sure any fbs we allocated at startup are properly
16292 * pinned & fenced. When we do the allocation it's too early
16293 * for this.
16294 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010016295 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070016296 obj = intel_fb_obj(c->primary->fb);
16297 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080016298 continue;
16299
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016300 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020016301 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16302 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016303 mutex_unlock(&dev->struct_mutex);
16304 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016305 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16306 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016307 drm_framebuffer_unreference(c->primary->fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020016308 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016309 c->primary->crtc = c->primary->state->crtc = NULL;
Daniel Vetter5a21b662016-05-24 17:13:53 +020016310 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016311 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016312 }
16313 }
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016314}
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016315
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016316int intel_connector_register(struct drm_connector *connector)
16317{
16318 struct intel_connector *intel_connector = to_intel_connector(connector);
16319 int ret;
16320
16321 ret = intel_backlight_device_register(intel_connector);
16322 if (ret)
16323 goto err;
16324
16325 return 0;
16326
16327err:
16328 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080016329}
16330
Chris Wilsonc191eca2016-06-17 11:40:33 +010016331void intel_connector_unregister(struct drm_connector *connector)
Imre Deak4932e2c2014-02-11 17:12:48 +020016332{
Chris Wilsone63d87c2016-06-17 11:40:34 +010016333 struct intel_connector *intel_connector = to_intel_connector(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016334
Chris Wilsone63d87c2016-06-17 11:40:34 +010016335 intel_backlight_device_unregister(intel_connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016336 intel_panel_destroy_backlight(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016337}
16338
Jesse Barnes79e53942008-11-07 14:24:08 -080016339void intel_modeset_cleanup(struct drm_device *dev)
16340{
Jesse Barnes652c3932009-08-17 13:31:43 -070016341 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070016342
Chris Wilsondc979972016-05-10 14:10:04 +010016343 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020016344
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016345 /*
16346 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016347 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016348 * experience fancy races otherwise.
16349 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016350 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016351
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016352 /*
16353 * Due to the hpd irq storm handling the hotplug work can re-arm the
16354 * poll handlers. Hence disable polling after hpd handling is shut down.
16355 */
Keith Packardf87ea762010-10-03 19:36:26 -070016356 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016357
Jesse Barnes723bfd72010-10-07 16:01:13 -070016358 intel_unregister_dsm_handler();
16359
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016360 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016361
Chris Wilson1630fe72011-07-08 12:22:42 +010016362 /* flush any delayed tasks or pending work */
16363 flush_scheduled_work();
16364
Jesse Barnes79e53942008-11-07 14:24:08 -080016365 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016366
Chris Wilson1ee8da62016-05-12 12:43:23 +010016367 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016368
Chris Wilsondc979972016-05-10 14:10:04 +010016369 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010016370
16371 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016372}
16373
Chris Wilsondf0e9242010-09-09 16:20:55 +010016374void intel_connector_attach_encoder(struct intel_connector *connector,
16375 struct intel_encoder *encoder)
16376{
16377 connector->encoder = encoder;
16378 drm_mode_connector_attach_encoder(&connector->base,
16379 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016380}
Dave Airlie28d52042009-09-21 14:33:58 +100016381
16382/*
16383 * set vga decode state - true == enable VGA decode
16384 */
16385int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16386{
16387 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000016388 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016389 u16 gmch_ctrl;
16390
Chris Wilson75fa0412014-02-07 18:37:02 -020016391 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16392 DRM_ERROR("failed to read control word\n");
16393 return -EIO;
16394 }
16395
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016396 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16397 return 0;
16398
Dave Airlie28d52042009-09-21 14:33:58 +100016399 if (state)
16400 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16401 else
16402 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016403
16404 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16405 DRM_ERROR("failed to write control word\n");
16406 return -EIO;
16407 }
16408
Dave Airlie28d52042009-09-21 14:33:58 +100016409 return 0;
16410}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016411
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016412struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016413
16414 u32 power_well_driver;
16415
Chris Wilson63b66e52013-08-08 15:12:06 +020016416 int num_transcoders;
16417
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016418 struct intel_cursor_error_state {
16419 u32 control;
16420 u32 position;
16421 u32 base;
16422 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016423 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016424
16425 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016426 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016427 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030016428 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016429 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016430
16431 struct intel_plane_error_state {
16432 u32 control;
16433 u32 stride;
16434 u32 size;
16435 u32 pos;
16436 u32 addr;
16437 u32 surface;
16438 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016439 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016440
16441 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016442 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016443 enum transcoder cpu_transcoder;
16444
16445 u32 conf;
16446
16447 u32 htotal;
16448 u32 hblank;
16449 u32 hsync;
16450 u32 vtotal;
16451 u32 vblank;
16452 u32 vsync;
16453 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016454};
16455
16456struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010016457intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016458{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016459 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016460 int transcoders[] = {
16461 TRANSCODER_A,
16462 TRANSCODER_B,
16463 TRANSCODER_C,
16464 TRANSCODER_EDP,
16465 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016466 int i;
16467
Chris Wilsonc0336662016-05-06 15:40:21 +010016468 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020016469 return NULL;
16470
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016471 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016472 if (error == NULL)
16473 return NULL;
16474
Chris Wilsonc0336662016-05-06 15:40:21 +010016475 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016476 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16477
Damien Lespiau055e3932014-08-18 13:49:10 +010016478 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016479 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016480 __intel_display_power_is_enabled(dev_priv,
16481 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016482 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016483 continue;
16484
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016485 error->cursor[i].control = I915_READ(CURCNTR(i));
16486 error->cursor[i].position = I915_READ(CURPOS(i));
16487 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016488
16489 error->plane[i].control = I915_READ(DSPCNTR(i));
16490 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016491 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016492 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016493 error->plane[i].pos = I915_READ(DSPPOS(i));
16494 }
Chris Wilsonc0336662016-05-06 15:40:21 +010016495 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030016496 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016497 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016498 error->plane[i].surface = I915_READ(DSPSURF(i));
16499 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16500 }
16501
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016502 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030016503
Chris Wilsonc0336662016-05-06 15:40:21 +010016504 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e12014-04-18 15:55:04 +030016505 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016506 }
16507
Jani Nikula4d1de972016-03-18 17:05:42 +020016508 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010016509 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030016510 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020016511 error->num_transcoders++; /* Account for eDP. */
16512
16513 for (i = 0; i < error->num_transcoders; i++) {
16514 enum transcoder cpu_transcoder = transcoders[i];
16515
Imre Deakddf9c532013-11-27 22:02:02 +020016516 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016517 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016518 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016519 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016520 continue;
16521
Chris Wilson63b66e52013-08-08 15:12:06 +020016522 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16523
16524 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16525 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16526 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16527 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16528 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16529 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16530 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016531 }
16532
16533 return error;
16534}
16535
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016536#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16537
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016538void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016539intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016540 struct drm_device *dev,
16541 struct intel_display_error_state *error)
16542{
Damien Lespiau055e3932014-08-18 13:49:10 +010016543 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016544 int i;
16545
Chris Wilson63b66e52013-08-08 15:12:06 +020016546 if (!error)
16547 return;
16548
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016549 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016550 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016551 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016552 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016553 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016554 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016555 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016556 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016557 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030016558 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016559
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016560 err_printf(m, "Plane [%d]:\n", i);
16561 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16562 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016563 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016564 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16565 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016566 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016567 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016568 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016569 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016570 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16571 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016572 }
16573
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016574 err_printf(m, "Cursor [%d]:\n", i);
16575 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16576 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16577 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016578 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016579
16580 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016581 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016582 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016583 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016584 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016585 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16586 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16587 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16588 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16589 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16590 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16591 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16592 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016593}