blob: 1d326535d07b2199ec6c23a56379ceb720901f93 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Chris Wilsonc37efb92016-06-17 08:28:47 +010039#include "i915_gem_dmabuf.h"
Imre Deakdb18b6a2016-03-24 12:41:40 +020040#include "intel_dsi.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070041#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080042#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080043#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010044#include <drm/drm_dp_helper.h>
45#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070046#include <drm/drm_plane_helper.h>
47#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080048#include <linux/dma_remapping.h>
Alex Goinsfd8e0582015-11-25 18:43:38 -080049#include <linux/reservation.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080050
Daniel Vetter5a21b662016-05-24 17:13:53 +020051static bool is_mmio_work(struct intel_flip_work *work)
52{
53 return work->mmio_work.func;
54}
55
Matt Roper465c1202014-05-29 08:06:54 -070056/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010057static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010058 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070060 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010061 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070062};
63
64/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010065static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010066 DRM_FORMAT_C8,
67 DRM_FORMAT_RGB565,
68 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070069 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010070 DRM_FORMAT_XRGB2101010,
71 DRM_FORMAT_XBGR2101010,
72};
73
74static const uint32_t skl_primary_formats[] = {
75 DRM_FORMAT_C8,
76 DRM_FORMAT_RGB565,
77 DRM_FORMAT_XRGB8888,
78 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010079 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070080 DRM_FORMAT_ABGR8888,
81 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070082 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053083 DRM_FORMAT_YUYV,
84 DRM_FORMAT_YVYU,
85 DRM_FORMAT_UYVY,
86 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070087};
88
Matt Roper3d7d6512014-06-10 08:28:13 -070089/* Cursor formats */
90static const uint32_t intel_cursor_formats[] = {
91 DRM_FORMAT_ARGB8888,
92};
93
Jesse Barnesf1f644d2013-06-27 00:39:25 +030094static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020095 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030096static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020097 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030098
Jesse Barneseb1bfe82014-02-12 12:26:25 -080099static int intel_framebuffer_init(struct drm_device *dev,
100 struct intel_framebuffer *ifb,
101 struct drm_mode_fb_cmd2 *mode_cmd,
102 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +0200103static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
104static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +0200105static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200106static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700107 struct intel_link_m_n *m_n,
108 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200109static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200110static void haswell_set_pipeconf(struct drm_crtc *crtc);
Jani Nikula391bf042016-03-18 17:05:40 +0200111static void haswell_set_pipemisc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200112static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200113 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200114static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200115 const struct intel_crtc_state *pipe_config);
Daniel Vetter5a21b662016-05-24 17:13:53 +0200116static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
117static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700118static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
119 struct intel_crtc_state *crtc_state);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +0200120static void skylake_pfit_enable(struct intel_crtc *crtc);
121static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
122static void ironlake_pfit_enable(struct intel_crtc *crtc);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200123static void intel_modeset_setup_hw_state(struct drm_device *dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +0200124static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
Ville Syrjälä4e5ca602016-05-11 22:44:44 +0300125static int ilk_max_pixel_rate(struct drm_atomic_state *state);
Imre Deak324513c2016-06-13 16:44:36 +0300126static int bxt_calc_cdclk(int max_pixclk);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100127
Ma Lingd4906092009-03-18 20:13:27 +0800128struct intel_limit {
Ander Conselvan de Oliveira4c5def92016-05-04 12:11:58 +0300129 struct {
130 int min, max;
131 } dot, vco, n, m, m1, m2, p, p1;
132
133 struct {
134 int dot_limit;
135 int p2_slow, p2_fast;
136 } p2;
Ma Lingd4906092009-03-18 20:13:27 +0800137};
Jesse Barnes79e53942008-11-07 14:24:08 -0800138
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300139/* returns HPLL frequency in kHz */
140static int valleyview_get_vco(struct drm_i915_private *dev_priv)
141{
142 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
143
144 /* Obtain SKU information */
145 mutex_lock(&dev_priv->sb_lock);
146 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
147 CCK_FUSE_HPLL_FREQ_MASK;
148 mutex_unlock(&dev_priv->sb_lock);
149
150 return vco_freq[hpll_freq] * 1000;
151}
152
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200153int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
154 const char *name, u32 reg, int ref_freq)
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300155{
156 u32 val;
157 int divider;
158
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300159 mutex_lock(&dev_priv->sb_lock);
160 val = vlv_cck_read(dev_priv, reg);
161 mutex_unlock(&dev_priv->sb_lock);
162
163 divider = val & CCK_FREQUENCY_VALUES;
164
165 WARN((val & CCK_FREQUENCY_STATUS) !=
166 (divider << CCK_FREQUENCY_STATUS_SHIFT),
167 "%s change in progress\n", name);
168
Ville Syrjäläc30fec62016-03-04 21:43:02 +0200169 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
170}
171
172static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
173 const char *name, u32 reg)
174{
175 if (dev_priv->hpll_freq == 0)
176 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
177
178 return vlv_get_cck_clock(dev_priv, name, reg,
179 dev_priv->hpll_freq);
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300180}
181
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200182static int
183intel_pch_rawclk(struct drm_i915_private *dev_priv)
Daniel Vetterd2acd212012-10-20 20:57:43 +0200184{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200185 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
Daniel Vetterd2acd212012-10-20 20:57:43 +0200186}
187
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200188static int
189intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
Jani Nikula79e50a42015-08-26 10:58:20 +0300190{
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300191 /* RAWCLK_FREQ_VLV register updated from power well code */
Ville Syrjälä35d38d12016-03-02 17:22:16 +0200192 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
193 CCK_DISPLAY_REF_CLOCK_CONTROL);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200194}
195
196static int
197intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
198{
Jani Nikula79e50a42015-08-26 10:58:20 +0300199 uint32_t clkcfg;
200
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200201 /* hrawclock is 1/4 the FSB frequency */
Jani Nikula79e50a42015-08-26 10:58:20 +0300202 clkcfg = I915_READ(CLKCFG);
203 switch (clkcfg & CLKCFG_FSB_MASK) {
204 case CLKCFG_FSB_400:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200205 return 100000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300206 case CLKCFG_FSB_533:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200207 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300208 case CLKCFG_FSB_667:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200209 return 166667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300210 case CLKCFG_FSB_800:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200211 return 200000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300212 case CLKCFG_FSB_1067:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200213 return 266667;
Jani Nikula79e50a42015-08-26 10:58:20 +0300214 case CLKCFG_FSB_1333:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200215 return 333333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300216 /* these two are just a guess; one of them might be right */
217 case CLKCFG_FSB_1600:
218 case CLKCFG_FSB_1600_ALT:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200219 return 400000;
Jani Nikula79e50a42015-08-26 10:58:20 +0300220 default:
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200221 return 133333;
Jani Nikula79e50a42015-08-26 10:58:20 +0300222 }
223}
224
Ville Syrjälä19ab4ed2016-04-27 17:43:22 +0300225void intel_update_rawclk(struct drm_i915_private *dev_priv)
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +0200226{
227 if (HAS_PCH_SPLIT(dev_priv))
228 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
229 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
230 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
231 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
232 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
233 else
234 return; /* no rawclk on other platforms, or no need to know it */
235
236 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
237}
238
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300239static void intel_update_czclk(struct drm_i915_private *dev_priv)
240{
Wayne Boyer666a4532015-12-09 12:29:35 -0800241 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
Ville Syrjäläbfa7df02015-09-24 23:29:18 +0300242 return;
243
244 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
245 CCK_CZ_CLOCK_CONTROL);
246
247 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
248}
249
Chris Wilson021357a2010-09-07 20:54:59 +0100250static inline u32 /* units of 100MHz */
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200251intel_fdi_link_freq(struct drm_i915_private *dev_priv,
252 const struct intel_crtc_state *pipe_config)
Chris Wilson021357a2010-09-07 20:54:59 +0100253{
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200254 if (HAS_DDI(dev_priv))
255 return pipe_config->port_clock; /* SPLL */
256 else if (IS_GEN5(dev_priv))
257 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +0200258 else
Ville Syrjälä21a727b2016-02-17 21:41:10 +0200259 return 270000;
Chris Wilson021357a2010-09-07 20:54:59 +0100260}
261
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300262static const struct intel_limit intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400263 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200264 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200265 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400266 .m = { .min = 96, .max = 140 },
267 .m1 = { .min = 18, .max = 26 },
268 .m2 = { .min = 6, .max = 16 },
269 .p = { .min = 4, .max = 128 },
270 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700271 .p2 = { .dot_limit = 165000,
272 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700273};
274
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300275static const struct intel_limit intel_limits_i8xx_dvo = {
Daniel Vetter5d536e22013-07-06 12:52:06 +0200276 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200277 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200278 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200279 .m = { .min = 96, .max = 140 },
280 .m1 = { .min = 18, .max = 26 },
281 .m2 = { .min = 6, .max = 16 },
282 .p = { .min = 4, .max = 128 },
283 .p1 = { .min = 2, .max = 33 },
284 .p2 = { .dot_limit = 165000,
285 .p2_slow = 4, .p2_fast = 4 },
286};
287
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300288static const struct intel_limit intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400289 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200290 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200291 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400292 .m = { .min = 96, .max = 140 },
293 .m1 = { .min = 18, .max = 26 },
294 .m2 = { .min = 6, .max = 16 },
295 .p = { .min = 4, .max = 128 },
296 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700297 .p2 = { .dot_limit = 165000,
298 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700299};
Eric Anholt273e27c2011-03-30 13:01:10 -0700300
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300301static const struct intel_limit intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400302 .dot = { .min = 20000, .max = 400000 },
303 .vco = { .min = 1400000, .max = 2800000 },
304 .n = { .min = 1, .max = 6 },
305 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100306 .m1 = { .min = 8, .max = 18 },
307 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400308 .p = { .min = 5, .max = 80 },
309 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700310 .p2 = { .dot_limit = 200000,
311 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700312};
313
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300314static const struct intel_limit intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400315 .dot = { .min = 20000, .max = 400000 },
316 .vco = { .min = 1400000, .max = 2800000 },
317 .n = { .min = 1, .max = 6 },
318 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100319 .m1 = { .min = 8, .max = 18 },
320 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400321 .p = { .min = 7, .max = 98 },
322 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700323 .p2 = { .dot_limit = 112000,
324 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700325};
326
Eric Anholt273e27c2011-03-30 13:01:10 -0700327
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300328static const struct intel_limit intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700329 .dot = { .min = 25000, .max = 270000 },
330 .vco = { .min = 1750000, .max = 3500000},
331 .n = { .min = 1, .max = 4 },
332 .m = { .min = 104, .max = 138 },
333 .m1 = { .min = 17, .max = 23 },
334 .m2 = { .min = 5, .max = 11 },
335 .p = { .min = 10, .max = 30 },
336 .p1 = { .min = 1, .max = 3},
337 .p2 = { .dot_limit = 270000,
338 .p2_slow = 10,
339 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800340 },
Keith Packarde4b36692009-06-05 19:22:17 -0700341};
342
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300343static const struct intel_limit intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700344 .dot = { .min = 22000, .max = 400000 },
345 .vco = { .min = 1750000, .max = 3500000},
346 .n = { .min = 1, .max = 4 },
347 .m = { .min = 104, .max = 138 },
348 .m1 = { .min = 16, .max = 23 },
349 .m2 = { .min = 5, .max = 11 },
350 .p = { .min = 5, .max = 80 },
351 .p1 = { .min = 1, .max = 8},
352 .p2 = { .dot_limit = 165000,
353 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700354};
355
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300356static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700357 .dot = { .min = 20000, .max = 115000 },
358 .vco = { .min = 1750000, .max = 3500000 },
359 .n = { .min = 1, .max = 3 },
360 .m = { .min = 104, .max = 138 },
361 .m1 = { .min = 17, .max = 23 },
362 .m2 = { .min = 5, .max = 11 },
363 .p = { .min = 28, .max = 112 },
364 .p1 = { .min = 2, .max = 8 },
365 .p2 = { .dot_limit = 0,
366 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800367 },
Keith Packarde4b36692009-06-05 19:22:17 -0700368};
369
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300370static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700371 .dot = { .min = 80000, .max = 224000 },
372 .vco = { .min = 1750000, .max = 3500000 },
373 .n = { .min = 1, .max = 3 },
374 .m = { .min = 104, .max = 138 },
375 .m1 = { .min = 17, .max = 23 },
376 .m2 = { .min = 5, .max = 11 },
377 .p = { .min = 14, .max = 42 },
378 .p1 = { .min = 2, .max = 6 },
379 .p2 = { .dot_limit = 0,
380 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800381 },
Keith Packarde4b36692009-06-05 19:22:17 -0700382};
383
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300384static const struct intel_limit intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400385 .dot = { .min = 20000, .max = 400000},
386 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700387 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400388 .n = { .min = 3, .max = 6 },
389 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700390 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400391 .m1 = { .min = 0, .max = 0 },
392 .m2 = { .min = 0, .max = 254 },
393 .p = { .min = 5, .max = 80 },
394 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700395 .p2 = { .dot_limit = 200000,
396 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700397};
398
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300399static const struct intel_limit intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400400 .dot = { .min = 20000, .max = 400000 },
401 .vco = { .min = 1700000, .max = 3500000 },
402 .n = { .min = 3, .max = 6 },
403 .m = { .min = 2, .max = 256 },
404 .m1 = { .min = 0, .max = 0 },
405 .m2 = { .min = 0, .max = 254 },
406 .p = { .min = 7, .max = 112 },
407 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700408 .p2 = { .dot_limit = 112000,
409 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700410};
411
Eric Anholt273e27c2011-03-30 13:01:10 -0700412/* Ironlake / Sandybridge
413 *
414 * We calculate clock using (register_value + 2) for N/M1/M2, so here
415 * the range value for them is (actual_value - 2).
416 */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300417static const struct intel_limit intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700418 .dot = { .min = 25000, .max = 350000 },
419 .vco = { .min = 1760000, .max = 3510000 },
420 .n = { .min = 1, .max = 5 },
421 .m = { .min = 79, .max = 127 },
422 .m1 = { .min = 12, .max = 22 },
423 .m2 = { .min = 5, .max = 9 },
424 .p = { .min = 5, .max = 80 },
425 .p1 = { .min = 1, .max = 8 },
426 .p2 = { .dot_limit = 225000,
427 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700428};
429
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300430static const struct intel_limit intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700431 .dot = { .min = 25000, .max = 350000 },
432 .vco = { .min = 1760000, .max = 3510000 },
433 .n = { .min = 1, .max = 3 },
434 .m = { .min = 79, .max = 118 },
435 .m1 = { .min = 12, .max = 22 },
436 .m2 = { .min = 5, .max = 9 },
437 .p = { .min = 28, .max = 112 },
438 .p1 = { .min = 2, .max = 8 },
439 .p2 = { .dot_limit = 225000,
440 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800441};
442
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300443static const struct intel_limit intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700444 .dot = { .min = 25000, .max = 350000 },
445 .vco = { .min = 1760000, .max = 3510000 },
446 .n = { .min = 1, .max = 3 },
447 .m = { .min = 79, .max = 127 },
448 .m1 = { .min = 12, .max = 22 },
449 .m2 = { .min = 5, .max = 9 },
450 .p = { .min = 14, .max = 56 },
451 .p1 = { .min = 2, .max = 8 },
452 .p2 = { .dot_limit = 225000,
453 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800454};
455
Eric Anholt273e27c2011-03-30 13:01:10 -0700456/* LVDS 100mhz refclk limits. */
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300457static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700458 .dot = { .min = 25000, .max = 350000 },
459 .vco = { .min = 1760000, .max = 3510000 },
460 .n = { .min = 1, .max = 2 },
461 .m = { .min = 79, .max = 126 },
462 .m1 = { .min = 12, .max = 22 },
463 .m2 = { .min = 5, .max = 9 },
464 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400465 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700466 .p2 = { .dot_limit = 225000,
467 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800468};
469
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300470static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700471 .dot = { .min = 25000, .max = 350000 },
472 .vco = { .min = 1760000, .max = 3510000 },
473 .n = { .min = 1, .max = 3 },
474 .m = { .min = 79, .max = 126 },
475 .m1 = { .min = 12, .max = 22 },
476 .m2 = { .min = 5, .max = 9 },
477 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400478 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700479 .p2 = { .dot_limit = 225000,
480 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800481};
482
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300483static const struct intel_limit intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300484 /*
485 * These are the data rate limits (measured in fast clocks)
486 * since those are the strictest limits we have. The fast
487 * clock and actual rate limits are more relaxed, so checking
488 * them would make no difference.
489 */
490 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200491 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700492 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700493 .m1 = { .min = 2, .max = 3 },
494 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300495 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300496 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700497};
498
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300499static const struct intel_limit intel_limits_chv = {
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300500 /*
501 * These are the data rate limits (measured in fast clocks)
502 * since those are the strictest limits we have. The fast
503 * clock and actual rate limits are more relaxed, so checking
504 * them would make no difference.
505 */
506 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200507 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300508 .n = { .min = 1, .max = 1 },
509 .m1 = { .min = 2, .max = 2 },
510 .m2 = { .min = 24 << 22, .max = 175 << 22 },
511 .p1 = { .min = 2, .max = 4 },
512 .p2 = { .p2_slow = 1, .p2_fast = 14 },
513};
514
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300515static const struct intel_limit intel_limits_bxt = {
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200516 /* FIXME: find real dot limits */
517 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530518 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200519 .n = { .min = 1, .max = 1 },
520 .m1 = { .min = 2, .max = 2 },
521 /* FIXME: find real m2 limits */
522 .m2 = { .min = 2 << 22, .max = 255 << 22 },
523 .p1 = { .min = 2, .max = 4 },
524 .p2 = { .p2_slow = 1, .p2_fast = 20 },
525};
526
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200527static bool
528needs_modeset(struct drm_crtc_state *state)
529{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200530 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200531}
532
Imre Deakdccbea32015-06-22 23:35:51 +0300533/*
534 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
535 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
536 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
537 * The helpers' return value is the rate of the clock that is fed to the
538 * display engine's pipe which can be the above fast dot clock rate or a
539 * divided-down version of it.
540 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500541/* m1 is reserved as 0 in Pineview, n is a ring counter */
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300542static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800543{
Shaohua Li21778322009-02-23 15:19:16 +0800544 clock->m = clock->m2 + 2;
545 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200546 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300547 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300548 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
549 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300550
551 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800552}
553
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200554static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
555{
556 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
557}
558
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300559static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800560{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200561 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800562 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200563 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300564 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300565 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
566 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300567
568 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800569}
570
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300571static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300572{
573 clock->m = clock->m1 * clock->m2;
574 clock->p = clock->p1 * clock->p2;
575 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300576 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300577 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
578 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300579
580 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300581}
582
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300583int chv_calc_dpll_params(int refclk, struct dpll *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300584{
585 clock->m = clock->m1 * clock->m2;
586 clock->p = clock->p1 * clock->p2;
587 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300588 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300589 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
590 clock->n << 22);
591 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300592
593 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300594}
595
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800596#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800597/**
598 * Returns whether the given set of divisors are valid for a given refclk with
599 * the given connectors.
600 */
601
Chris Wilson1b894b52010-12-14 20:04:54 +0000602static bool intel_PLL_is_valid(struct drm_device *dev,
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300603 const struct intel_limit *limit,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300604 const struct dpll *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800605{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300606 if (clock->n < limit->n.min || limit->n.max < clock->n)
607 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800608 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400609 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800610 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400611 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400613 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300614
Wayne Boyer666a4532015-12-09 12:29:35 -0800615 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
616 !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300617 if (clock->m1 <= clock->m2)
618 INTELPllInvalid("m1 <= m2\n");
619
Wayne Boyer666a4532015-12-09 12:29:35 -0800620 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300621 if (clock->p < limit->p.min || limit->p.max < clock->p)
622 INTELPllInvalid("p out of range\n");
623 if (clock->m < limit->m.min || limit->m.max < clock->m)
624 INTELPllInvalid("m out of range\n");
625 }
626
Jesse Barnes79e53942008-11-07 14:24:08 -0800627 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400628 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800629 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
630 * connector, etc., rather than just a single range.
631 */
632 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400633 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800634
635 return true;
636}
637
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300638static int
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300639i9xx_select_p2_div(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300640 const struct intel_crtc_state *crtc_state,
641 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800642{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300643 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800644
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +0300645 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800646 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100647 * For LVDS just rely on its current settings for dual-channel.
648 * We haven't figured out how to reliably set up different
649 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800650 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100651 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300652 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800653 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300654 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800655 } else {
656 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300657 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800658 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300659 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800660 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300661}
662
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200663/*
664 * Returns a set of divisors for the desired target clock with the given
665 * refclk, or FALSE. The returned values represent the clock equation:
666 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
667 *
668 * Target and reference clocks are specified in kHz.
669 *
670 * If match_clock is provided, then best_clock P divider must match the P
671 * divider from @match_clock used for LVDS downclocking.
672 */
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300673static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300674i9xx_find_best_dpll(const struct intel_limit *limit,
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300675 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300676 int target, int refclk, struct dpll *match_clock,
677 struct dpll *best_clock)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300678{
679 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300680 struct dpll clock;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300681 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800682
Akshay Joshi0206e352011-08-16 15:34:10 -0400683 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800684
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300685 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
686
Zhao Yakui42158662009-11-20 11:24:18 +0800687 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
688 clock.m1++) {
689 for (clock.m2 = limit->m2.min;
690 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200691 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800692 break;
693 for (clock.n = limit->n.min;
694 clock.n <= limit->n.max; clock.n++) {
695 for (clock.p1 = limit->p1.min;
696 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800697 int this_err;
698
Imre Deakdccbea32015-06-22 23:35:51 +0300699 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000700 if (!intel_PLL_is_valid(dev, limit,
701 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800702 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800703 if (match_clock &&
704 clock.p != match_clock->p)
705 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800706
707 this_err = abs(clock.dot - target);
708 if (this_err < err) {
709 *best_clock = clock;
710 err = this_err;
711 }
712 }
713 }
714 }
715 }
716
717 return (err != target);
718}
719
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200720/*
721 * Returns a set of divisors for the desired target clock with the given
722 * refclk, or FALSE. The returned values represent the clock equation:
723 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
724 *
725 * Target and reference clocks are specified in kHz.
726 *
727 * If match_clock is provided, then best_clock P divider must match the P
728 * divider from @match_clock used for LVDS downclocking.
729 */
Ma Lingd4906092009-03-18 20:13:27 +0800730static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300731pnv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200732 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300733 int target, int refclk, struct dpll *match_clock,
734 struct dpll *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200735{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300736 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300737 struct dpll clock;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200738 int err = target;
739
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200740 memset(best_clock, 0, sizeof(*best_clock));
741
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300742 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
743
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200744 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
745 clock.m1++) {
746 for (clock.m2 = limit->m2.min;
747 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200748 for (clock.n = limit->n.min;
749 clock.n <= limit->n.max; clock.n++) {
750 for (clock.p1 = limit->p1.min;
751 clock.p1 <= limit->p1.max; clock.p1++) {
752 int this_err;
753
Imre Deakdccbea32015-06-22 23:35:51 +0300754 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800755 if (!intel_PLL_is_valid(dev, limit,
756 &clock))
757 continue;
758 if (match_clock &&
759 clock.p != match_clock->p)
760 continue;
761
762 this_err = abs(clock.dot - target);
763 if (this_err < err) {
764 *best_clock = clock;
765 err = this_err;
766 }
767 }
768 }
769 }
770 }
771
772 return (err != target);
773}
774
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200775/*
776 * Returns a set of divisors for the desired target clock with the given
777 * refclk, or FALSE. The returned values represent the clock equation:
778 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +0200779 *
780 * Target and reference clocks are specified in kHz.
781 *
782 * If match_clock is provided, then best_clock P divider must match the P
783 * divider from @match_clock used for LVDS downclocking.
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +0200784 */
Ma Lingd4906092009-03-18 20:13:27 +0800785static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300786g4x_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200787 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300788 int target, int refclk, struct dpll *match_clock,
789 struct dpll *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800790{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300791 struct drm_device *dev = crtc_state->base.crtc->dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300792 struct dpll clock;
Ma Lingd4906092009-03-18 20:13:27 +0800793 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300794 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400795 /* approximately equals target * 0.00585 */
796 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800797
798 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300799
800 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
801
Ma Lingd4906092009-03-18 20:13:27 +0800802 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200803 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800804 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200805 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800806 for (clock.m1 = limit->m1.max;
807 clock.m1 >= limit->m1.min; clock.m1--) {
808 for (clock.m2 = limit->m2.max;
809 clock.m2 >= limit->m2.min; clock.m2--) {
810 for (clock.p1 = limit->p1.max;
811 clock.p1 >= limit->p1.min; clock.p1--) {
812 int this_err;
813
Imre Deakdccbea32015-06-22 23:35:51 +0300814 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000815 if (!intel_PLL_is_valid(dev, limit,
816 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800817 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000818
819 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800820 if (this_err < err_most) {
821 *best_clock = clock;
822 err_most = this_err;
823 max_n = clock.n;
824 found = true;
825 }
826 }
827 }
828 }
829 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800830 return found;
831}
Ma Lingd4906092009-03-18 20:13:27 +0800832
Imre Deakd5dd62b2015-03-17 11:40:03 +0200833/*
834 * Check if the calculated PLL configuration is more optimal compared to the
835 * best configuration and error found so far. Return the calculated error.
836 */
837static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300838 const struct dpll *calculated_clock,
839 const struct dpll *best_clock,
Imre Deakd5dd62b2015-03-17 11:40:03 +0200840 unsigned int best_error_ppm,
841 unsigned int *error_ppm)
842{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200843 /*
844 * For CHV ignore the error and consider only the P value.
845 * Prefer a bigger P value based on HW requirements.
846 */
847 if (IS_CHERRYVIEW(dev)) {
848 *error_ppm = 0;
849
850 return calculated_clock->p > best_clock->p;
851 }
852
Imre Deak24be4e42015-03-17 11:40:04 +0200853 if (WARN_ON_ONCE(!target_freq))
854 return false;
855
Imre Deakd5dd62b2015-03-17 11:40:03 +0200856 *error_ppm = div_u64(1000000ULL *
857 abs(target_freq - calculated_clock->dot),
858 target_freq);
859 /*
860 * Prefer a better P value over a better (smaller) error if the error
861 * is small. Ensure this preference for future configurations too by
862 * setting the error to 0.
863 */
864 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
865 *error_ppm = 0;
866
867 return true;
868 }
869
870 return *error_ppm + 10 < best_error_ppm;
871}
872
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200873/*
874 * Returns a set of divisors for the desired target clock with the given
875 * refclk, or FALSE. The returned values represent the clock equation:
876 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
877 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800878static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300879vlv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200880 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300881 int target, int refclk, struct dpll *match_clock,
882 struct dpll *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700883{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200884 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300885 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300886 struct dpll clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300887 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300888 /* min update 19.2 MHz */
889 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300890 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700891
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300892 target *= 5; /* fast clock */
893
894 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700895
896 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300897 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300898 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300899 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300900 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300901 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700902 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300903 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200904 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300905
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300906 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
907 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300908
Imre Deakdccbea32015-06-22 23:35:51 +0300909 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300910
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300911 if (!intel_PLL_is_valid(dev, limit,
912 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300913 continue;
914
Imre Deakd5dd62b2015-03-17 11:40:03 +0200915 if (!vlv_PLL_is_optimal(dev, target,
916 &clock,
917 best_clock,
918 bestppm, &ppm))
919 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300920
Imre Deakd5dd62b2015-03-17 11:40:03 +0200921 *best_clock = clock;
922 bestppm = ppm;
923 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700924 }
925 }
926 }
927 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700928
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300929 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700930}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700931
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200932/*
933 * Returns a set of divisors for the desired target clock with the given
934 * refclk, or FALSE. The returned values represent the clock equation:
935 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
936 */
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300937static bool
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300938chv_find_best_dpll(const struct intel_limit *limit,
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200939 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300940 int target, int refclk, struct dpll *match_clock,
941 struct dpll *best_clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300942{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200943 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300944 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200945 unsigned int best_error_ppm;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300946 struct dpll clock;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300947 uint64_t m2;
948 int found = false;
949
950 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200951 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300952
953 /*
954 * Based on hardware doc, the n always set to 1, and m1 always
955 * set to 2. If requires to support 200Mhz refclk, we need to
956 * revisit this because n may not 1 anymore.
957 */
958 clock.n = 1, clock.m1 = 2;
959 target *= 5; /* fast clock */
960
961 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
962 for (clock.p2 = limit->p2.p2_fast;
963 clock.p2 >= limit->p2.p2_slow;
964 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200965 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300966
967 clock.p = clock.p1 * clock.p2;
968
969 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
970 clock.n) << 22, refclk * clock.m1);
971
972 if (m2 > INT_MAX/clock.m1)
973 continue;
974
975 clock.m2 = m2;
976
Imre Deakdccbea32015-06-22 23:35:51 +0300977 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300978
979 if (!intel_PLL_is_valid(dev, limit, &clock))
980 continue;
981
Imre Deak9ca3ba02015-03-17 11:40:05 +0200982 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
983 best_error_ppm, &error_ppm))
984 continue;
985
986 *best_clock = clock;
987 best_error_ppm = error_ppm;
988 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300989 }
990 }
991
992 return found;
993}
994
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200995bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +0300996 struct dpll *best_clock)
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200997{
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +0200998 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +0300999 const struct intel_limit *limit = &intel_limits_bxt;
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001000
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02001001 return chv_find_best_dpll(limit, crtc_state,
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001002 target_clock, refclk, NULL, best_clock);
1003}
1004
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001005bool intel_crtc_active(struct drm_crtc *crtc)
1006{
1007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1008
1009 /* Be paranoid as we can arrive here with only partial
1010 * state retrieved from the hardware during setup.
1011 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001012 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001013 * as Haswell has gained clock readout/fastboot support.
1014 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001015 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001016 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001017 *
1018 * FIXME: The intel_crtc->active here should be switched to
1019 * crtc->state->active once we have proper CRTC states wired up
1020 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001021 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001022 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001023 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001024}
1025
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001026enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1027 enum pipe pipe)
1028{
1029 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1031
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001032 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001033}
1034
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001035static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1036{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001037 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001038 i915_reg_t reg = PIPEDSL(pipe);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001039 u32 line1, line2;
1040 u32 line_mask;
1041
1042 if (IS_GEN2(dev))
1043 line_mask = DSL_LINEMASK_GEN2;
1044 else
1045 line_mask = DSL_LINEMASK_GEN3;
1046
1047 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001048 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001049 line2 = I915_READ(reg) & line_mask;
1050
1051 return line1 == line2;
1052}
1053
Keith Packardab7ad7f2010-10-03 00:33:06 -07001054/*
1055 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001056 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001057 *
1058 * After disabling a pipe, we can't wait for vblank in the usual way,
1059 * spinning on the vblank interrupt status bit, since we won't actually
1060 * see an interrupt when the pipe is disabled.
1061 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001062 * On Gen4 and above:
1063 * wait for the pipe register state bit to turn off
1064 *
1065 * Otherwise:
1066 * wait for the display line value to settle (it usually
1067 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001068 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001069 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001070static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001071{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001072 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001073 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001074 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001075 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001076
Keith Packardab7ad7f2010-10-03 00:33:06 -07001077 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001078 i915_reg_t reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001079
Keith Packardab7ad7f2010-10-03 00:33:06 -07001080 /* Wait for the Pipe State to go off */
Chris Wilsonb8511f52016-06-30 15:32:53 +01001081 if (intel_wait_for_register(dev_priv,
1082 reg, I965_PIPECONF_ACTIVE, 0,
1083 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001084 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001085 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001086 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001087 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001088 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001089 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001090}
1091
Jesse Barnesb24e7172011-01-04 15:09:30 -08001092/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001093void assert_pll(struct drm_i915_private *dev_priv,
1094 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001095{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001096 u32 val;
1097 bool cur_state;
1098
Ville Syrjälä649636e2015-09-22 19:50:01 +03001099 val = I915_READ(DPLL(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001100 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001101 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001102 "PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001103 onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001104}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001105
Jani Nikula23538ef2013-08-27 15:12:22 +03001106/* XXX: the dsi pll is shared between MIPI DSI ports */
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +00001107void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
Jani Nikula23538ef2013-08-27 15:12:22 +03001108{
1109 u32 val;
1110 bool cur_state;
1111
Ville Syrjäläa5805162015-05-26 20:42:30 +03001112 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001113 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001114 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001115
1116 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001117 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001118 "DSI PLL state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001119 onoff(state), onoff(cur_state));
Jani Nikula23538ef2013-08-27 15:12:22 +03001120}
Jani Nikula23538ef2013-08-27 15:12:22 +03001121
Jesse Barnes040484a2011-01-03 12:14:26 -08001122static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1123 enum pipe pipe, bool state)
1124{
Jesse Barnes040484a2011-01-03 12:14:26 -08001125 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001126 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1127 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001128
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001129 if (HAS_DDI(dev_priv)) {
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001130 /* DDI does not have a specific FDI_TX register */
Ville Syrjälä649636e2015-09-22 19:50:01 +03001131 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Paulo Zanoniad80a812012-10-24 16:06:19 -02001132 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001133 } else {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001134 u32 val = I915_READ(FDI_TX_CTL(pipe));
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001135 cur_state = !!(val & FDI_TX_ENABLE);
1136 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001137 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001138 "FDI TX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001139 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001140}
1141#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1142#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1143
1144static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1145 enum pipe pipe, bool state)
1146{
Jesse Barnes040484a2011-01-03 12:14:26 -08001147 u32 val;
1148 bool cur_state;
1149
Ville Syrjälä649636e2015-09-22 19:50:01 +03001150 val = I915_READ(FDI_RX_CTL(pipe));
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001151 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001152 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001153 "FDI RX state assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001154 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001155}
1156#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1157#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1158
1159static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1160 enum pipe pipe)
1161{
Jesse Barnes040484a2011-01-03 12:14:26 -08001162 u32 val;
1163
1164 /* ILK FDI PLL is always enabled */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01001165 if (IS_GEN5(dev_priv))
Jesse Barnes040484a2011-01-03 12:14:26 -08001166 return;
1167
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001168 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001169 if (HAS_DDI(dev_priv))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001170 return;
1171
Ville Syrjälä649636e2015-09-22 19:50:01 +03001172 val = I915_READ(FDI_TX_CTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001173 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
Jesse Barnes040484a2011-01-03 12:14:26 -08001174}
1175
Daniel Vetter55607e82013-06-16 21:42:39 +02001176void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1177 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001178{
Jesse Barnes040484a2011-01-03 12:14:26 -08001179 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001180 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001181
Ville Syrjälä649636e2015-09-22 19:50:01 +03001182 val = I915_READ(FDI_RX_CTL(pipe));
Daniel Vetter55607e82013-06-16 21:42:39 +02001183 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001184 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001185 "FDI RX PLL assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001186 onoff(state), onoff(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001187}
1188
Daniel Vetterb680c372014-09-19 18:27:27 +02001189void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1190 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001191{
Chris Wilson91c8a322016-07-05 10:40:23 +01001192 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001193 i915_reg_t pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001194 u32 val;
1195 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001196 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001197
Jani Nikulabedd4db2014-08-22 15:04:13 +03001198 if (WARN_ON(HAS_DDI(dev)))
1199 return;
1200
1201 if (HAS_PCH_SPLIT(dev)) {
1202 u32 port_sel;
1203
Jesse Barnesea0760c2011-01-04 15:09:32 -08001204 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001205 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1206
1207 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1208 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1209 panel_pipe = PIPE_B;
1210 /* XXX: else fix for eDP */
Wayne Boyer666a4532015-12-09 12:29:35 -08001211 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Jani Nikulabedd4db2014-08-22 15:04:13 +03001212 /* presumably write lock depends on pipe, not port select */
1213 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1214 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001215 } else {
1216 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001217 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1218 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001219 }
1220
1221 val = I915_READ(pp_reg);
1222 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001223 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001224 locked = false;
1225
Rob Clarke2c719b2014-12-15 13:56:32 -05001226 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001227 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001228 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001229}
1230
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001231static void assert_cursor(struct drm_i915_private *dev_priv,
1232 enum pipe pipe, bool state)
1233{
Chris Wilson91c8a322016-07-05 10:40:23 +01001234 struct drm_device *dev = &dev_priv->drm;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001235 bool cur_state;
1236
Paulo Zanonid9d82082014-02-27 16:30:56 -03001237 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä0b87c242015-09-22 19:47:51 +03001238 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001239 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001240 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001241
Rob Clarke2c719b2014-12-15 13:56:32 -05001242 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001243 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001244 pipe_name(pipe), onoff(state), onoff(cur_state));
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001245}
1246#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1247#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1248
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001249void assert_pipe(struct drm_i915_private *dev_priv,
1250 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001251{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001252 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001253 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1254 pipe);
Imre Deak4feed0e2016-02-12 18:55:14 +02001255 enum intel_display_power_domain power_domain;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001256
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001257 /* if we need the pipe quirk it must be always on */
1258 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1259 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001260 state = true;
1261
Imre Deak4feed0e2016-02-12 18:55:14 +02001262 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1263 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001264 u32 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni69310162013-01-29 16:35:19 -02001265 cur_state = !!(val & PIPECONF_ENABLE);
Imre Deak4feed0e2016-02-12 18:55:14 +02001266
1267 intel_display_power_put(dev_priv, power_domain);
1268 } else {
1269 cur_state = false;
Paulo Zanoni69310162013-01-29 16:35:19 -02001270 }
1271
Rob Clarke2c719b2014-12-15 13:56:32 -05001272 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001273 "pipe %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001274 pipe_name(pipe), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001275}
1276
Chris Wilson931872f2012-01-16 23:01:13 +00001277static void assert_plane(struct drm_i915_private *dev_priv,
1278 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001279{
Jesse Barnesb24e7172011-01-04 15:09:30 -08001280 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001281 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001282
Ville Syrjälä649636e2015-09-22 19:50:01 +03001283 val = I915_READ(DSPCNTR(plane));
Chris Wilson931872f2012-01-16 23:01:13 +00001284 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001285 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001286 "plane %c assertion failure (expected %s, current %s)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02001287 plane_name(plane), onoff(state), onoff(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001288}
1289
Chris Wilson931872f2012-01-16 23:01:13 +00001290#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1291#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1292
Jesse Barnesb24e7172011-01-04 15:09:30 -08001293static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1294 enum pipe pipe)
1295{
Chris Wilson91c8a322016-07-05 10:40:23 +01001296 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001297 int i;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001298
Ville Syrjälä653e1022013-06-04 13:49:05 +03001299 /* Primary planes are fixed to pipes on gen4+ */
1300 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001301 u32 val = I915_READ(DSPCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001302 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001303 "plane %c assertion failure, should be disabled but not\n",
1304 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001305 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001306 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001307
Jesse Barnesb24e7172011-01-04 15:09:30 -08001308 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001309 for_each_pipe(dev_priv, i) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001310 u32 val = I915_READ(DSPCNTR(i));
1311 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
Jesse Barnesb24e7172011-01-04 15:09:30 -08001312 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001313 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001314 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1315 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001316 }
1317}
1318
Jesse Barnes19332d72013-03-28 09:55:38 -07001319static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1320 enum pipe pipe)
1321{
Chris Wilson91c8a322016-07-05 10:40:23 +01001322 struct drm_device *dev = &dev_priv->drm;
Ville Syrjälä649636e2015-09-22 19:50:01 +03001323 int sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001324
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001325 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001326 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001327 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001328 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001329 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1330 sprite, pipe_name(pipe));
1331 }
Wayne Boyer666a4532015-12-09 12:29:35 -08001332 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001333 for_each_sprite(dev_priv, pipe, sprite) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001334 u32 val = I915_READ(SPCNTR(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001335 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001336 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001337 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001338 }
1339 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001340 u32 val = I915_READ(SPRCTL(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001341 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001342 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001343 plane_name(pipe), pipe_name(pipe));
1344 } else if (INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä649636e2015-09-22 19:50:01 +03001345 u32 val = I915_READ(DVSCNTR(pipe));
Rob Clarke2c719b2014-12-15 13:56:32 -05001346 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001347 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1348 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001349 }
1350}
1351
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001352static void assert_vblank_disabled(struct drm_crtc *crtc)
1353{
Rob Clarke2c719b2014-12-15 13:56:32 -05001354 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001355 drm_crtc_vblank_put(crtc);
1356}
1357
Ander Conselvan de Oliveira7abd4b32016-03-08 17:46:15 +02001358void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1359 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001360{
Jesse Barnes92f25842011-01-04 15:09:34 -08001361 u32 val;
1362 bool enabled;
1363
Ville Syrjälä649636e2015-09-22 19:50:01 +03001364 val = I915_READ(PCH_TRANSCONF(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001365 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001366 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001367 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1368 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001369}
1370
Keith Packard4e634382011-08-06 10:39:45 -07001371static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1372 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001373{
1374 if ((val & DP_PORT_EN) == 0)
1375 return false;
1376
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001377 if (HAS_PCH_CPT(dev_priv)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001378 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
Keith Packardf0575e92011-07-25 22:12:43 -07001379 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1380 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001381 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001382 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1383 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001384 } else {
1385 if ((val & DP_PIPE_MASK) != (pipe << 30))
1386 return false;
1387 }
1388 return true;
1389}
1390
Keith Packard1519b992011-08-06 10:35:34 -07001391static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1392 enum pipe pipe, u32 val)
1393{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001394 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001395 return false;
1396
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001397 if (HAS_PCH_CPT(dev_priv)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001398 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001399 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001400 } else if (IS_CHERRYVIEW(dev_priv)) {
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001401 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1402 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001403 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001404 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001405 return false;
1406 }
1407 return true;
1408}
1409
1410static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1411 enum pipe pipe, u32 val)
1412{
1413 if ((val & LVDS_PORT_EN) == 0)
1414 return false;
1415
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001416 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001417 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1418 return false;
1419 } else {
1420 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1421 return false;
1422 }
1423 return true;
1424}
1425
1426static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1427 enum pipe pipe, u32 val)
1428{
1429 if ((val & ADPA_DAC_ENABLE) == 0)
1430 return false;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001431 if (HAS_PCH_CPT(dev_priv)) {
Keith Packard1519b992011-08-06 10:35:34 -07001432 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1433 return false;
1434 } else {
1435 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1436 return false;
1437 }
1438 return true;
1439}
1440
Jesse Barnes291906f2011-02-02 12:28:03 -08001441static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001442 enum pipe pipe, i915_reg_t reg,
1443 u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001444{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001445 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001446 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001447 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001448 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001449
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001450 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001451 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001452 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001453}
1454
1455static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001456 enum pipe pipe, i915_reg_t reg)
Jesse Barnes291906f2011-02-02 12:28:03 -08001457{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001458 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001459 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001460 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001461 i915_mmio_reg_offset(reg), pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001462
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001463 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001464 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001465 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001466}
1467
1468static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1469 enum pipe pipe)
1470{
Jesse Barnes291906f2011-02-02 12:28:03 -08001471 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001472
Keith Packardf0575e92011-07-25 22:12:43 -07001473 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1474 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1475 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001476
Ville Syrjälä649636e2015-09-22 19:50:01 +03001477 val = I915_READ(PCH_ADPA);
Rob Clarke2c719b2014-12-15 13:56:32 -05001478 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001479 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001480 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001481
Ville Syrjälä649636e2015-09-22 19:50:01 +03001482 val = I915_READ(PCH_LVDS);
Rob Clarke2c719b2014-12-15 13:56:32 -05001483 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001484 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001485 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001486
Paulo Zanonie2debe92013-02-18 19:00:27 -03001487 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1488 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1489 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001490}
1491
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001492static void _vlv_enable_pll(struct intel_crtc *crtc,
1493 const struct intel_crtc_state *pipe_config)
1494{
1495 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1496 enum pipe pipe = crtc->pipe;
1497
1498 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1499 POSTING_READ(DPLL(pipe));
1500 udelay(150);
1501
Chris Wilson2c30b432016-06-30 15:32:54 +01001502 if (intel_wait_for_register(dev_priv,
1503 DPLL(pipe),
1504 DPLL_LOCK_VLV,
1505 DPLL_LOCK_VLV,
1506 1))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001507 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1508}
1509
Ville Syrjäläd288f652014-10-28 13:20:22 +02001510static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001511 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001512{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001513 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001514 enum pipe pipe = crtc->pipe;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001515
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001516 assert_pipe_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001517
Daniel Vetter87442f72013-06-06 00:52:17 +02001518 /* PLL is protected by panel, make sure we can write it */
Ville Syrjälä7d1a83c2016-03-15 16:39:58 +02001519 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001520
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001521 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1522 _vlv_enable_pll(crtc, pipe_config);
Daniel Vetter426115c2013-07-11 22:13:42 +02001523
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001524 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1525 POSTING_READ(DPLL_MD(pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001526}
1527
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001528
1529static void _chv_enable_pll(struct intel_crtc *crtc,
1530 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001531{
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001532 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä8bd3f302016-03-15 16:39:57 +02001533 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001534 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001535 u32 tmp;
1536
Ville Syrjäläa5805162015-05-26 20:42:30 +03001537 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001538
1539 /* Enable back the 10bit clock to display controller */
1540 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1541 tmp |= DPIO_DCLKP_EN;
1542 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1543
Ville Syrjälä54433e92015-05-26 20:42:31 +03001544 mutex_unlock(&dev_priv->sb_lock);
1545
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001546 /*
1547 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1548 */
1549 udelay(1);
1550
1551 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001552 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001553
1554 /* Check PLL is locked */
Chris Wilson6b188262016-06-30 15:32:55 +01001555 if (intel_wait_for_register(dev_priv,
1556 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1557 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001558 DRM_ERROR("PLL %d failed to lock\n", pipe);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03001559}
1560
1561static void chv_enable_pll(struct intel_crtc *crtc,
1562 const struct intel_crtc_state *pipe_config)
1563{
1564 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1565 enum pipe pipe = crtc->pipe;
1566
1567 assert_pipe_disabled(dev_priv, pipe);
1568
1569 /* PLL is protected by panel, make sure we can write it */
1570 assert_panel_unlocked(dev_priv, pipe);
1571
1572 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1573 _chv_enable_pll(crtc, pipe_config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001574
Ville Syrjäläc2317752016-03-15 16:39:56 +02001575 if (pipe != PIPE_A) {
1576 /*
1577 * WaPixelRepeatModeFixForC0:chv
1578 *
1579 * DPLLCMD is AWOL. Use chicken bits to propagate
1580 * the value from DPLLBMD to either pipe B or C.
1581 */
1582 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1583 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1584 I915_WRITE(CBR4_VLV, 0);
1585 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1586
1587 /*
1588 * DPLLB VGA mode also seems to cause problems.
1589 * We should always have it disabled.
1590 */
1591 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1592 } else {
1593 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1594 POSTING_READ(DPLL_MD(pipe));
1595 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001596}
1597
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001598static int intel_num_dvo_pipes(struct drm_device *dev)
1599{
1600 struct intel_crtc *crtc;
1601 int count = 0;
1602
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001603 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001604 count += crtc->base.state->active &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001605 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1606 }
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001607
1608 return count;
1609}
1610
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001611static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001612{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001613 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001614 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001615 i915_reg_t reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001616 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001617
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001618 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001619
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001620 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001621 if (IS_MOBILE(dev) && !IS_I830(dev))
1622 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001623
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001624 /* Enable DVO 2x clock on both PLLs if necessary */
1625 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1626 /*
1627 * It appears to be important that we don't enable this
1628 * for the current pipe before otherwise configuring the
1629 * PLL. No idea how this should be handled if multiple
1630 * DVO outputs are enabled simultaneosly.
1631 */
1632 dpll |= DPLL_DVO_2X_MODE;
1633 I915_WRITE(DPLL(!crtc->pipe),
1634 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1635 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001636
Ville Syrjäläc2b63372015-10-07 22:08:25 +03001637 /*
1638 * Apparently we need to have VGA mode enabled prior to changing
1639 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1640 * dividers, even though the register value does change.
1641 */
1642 I915_WRITE(reg, 0);
1643
Ville Syrjälä8e7a65a2015-10-07 22:08:24 +03001644 I915_WRITE(reg, dpll);
1645
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001646 /* Wait for the clocks to stabilize. */
1647 POSTING_READ(reg);
1648 udelay(150);
1649
1650 if (INTEL_INFO(dev)->gen >= 4) {
1651 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001652 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001653 } else {
1654 /* The pixel multiplier can only be updated once the
1655 * DPLL is enabled and the clocks are stable.
1656 *
1657 * So write it again.
1658 */
1659 I915_WRITE(reg, dpll);
1660 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001661
1662 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001663 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001664 POSTING_READ(reg);
1665 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001666 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001667 POSTING_READ(reg);
1668 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001669 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001670 POSTING_READ(reg);
1671 udelay(150); /* wait for warmup */
1672}
1673
1674/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001675 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001676 * @dev_priv: i915 private structure
1677 * @pipe: pipe PLL to disable
1678 *
1679 * Disable the PLL for @pipe, making sure the pipe is off first.
1680 *
1681 * Note! This is for pre-ILK only.
1682 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001683static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001684{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001685 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001686 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001687 enum pipe pipe = crtc->pipe;
1688
1689 /* Disable DVO 2x clock on both PLLs if necessary */
1690 if (IS_I830(dev) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001691 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001692 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001693 I915_WRITE(DPLL(PIPE_B),
1694 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1695 I915_WRITE(DPLL(PIPE_A),
1696 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1697 }
1698
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001699 /* Don't disable pipe or pipe PLLs if needed */
1700 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1701 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001702 return;
1703
1704 /* Make sure the pipe isn't still relying on us */
1705 assert_pipe_disabled(dev_priv, pipe);
1706
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001707 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001708 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001709}
1710
Jesse Barnesf6071162013-10-01 10:41:38 -07001711static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1712{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001713 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001714
1715 /* Make sure the pipe isn't still relying on us */
1716 assert_pipe_disabled(dev_priv, pipe);
1717
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001718 val = DPLL_INTEGRATED_REF_CLK_VLV |
1719 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1720 if (pipe != PIPE_A)
1721 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1722
Jesse Barnesf6071162013-10-01 10:41:38 -07001723 I915_WRITE(DPLL(pipe), val);
1724 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001725}
1726
1727static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1728{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001729 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001730 u32 val;
1731
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001732 /* Make sure the pipe isn't still relying on us */
1733 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001734
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001735 val = DPLL_SSC_REF_CLK_CHV |
1736 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001737 if (pipe != PIPE_A)
1738 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02001739
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001740 I915_WRITE(DPLL(pipe), val);
1741 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001742
Ville Syrjäläa5805162015-05-26 20:42:30 +03001743 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001744
1745 /* Disable 10bit clock to display controller */
1746 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1747 val &= ~DPIO_DCLKP_EN;
1748 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1749
Ville Syrjäläa5805162015-05-26 20:42:30 +03001750 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001751}
1752
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001753void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001754 struct intel_digital_port *dport,
1755 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001756{
1757 u32 port_mask;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001758 i915_reg_t dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001759
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001760 switch (dport->port) {
1761 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001762 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001763 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001764 break;
1765 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001766 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001767 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001768 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001769 break;
1770 case PORT_D:
1771 port_mask = DPLL_PORTD_READY_MASK;
1772 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001773 break;
1774 default:
1775 BUG();
1776 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001777
Chris Wilson370004d2016-06-30 15:32:56 +01001778 if (intel_wait_for_register(dev_priv,
1779 dpll_reg, port_mask, expected_mask,
1780 1000))
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001781 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1782 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001783}
1784
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001785static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1786 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001787{
Chris Wilson91c8a322016-07-05 10:40:23 +01001788 struct drm_device *dev = &dev_priv->drm;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001789 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001790 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001791 i915_reg_t reg;
1792 uint32_t val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001793
Jesse Barnes040484a2011-01-03 12:14:26 -08001794 /* Make sure PCH DPLL is enabled */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02001795 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
Jesse Barnes040484a2011-01-03 12:14:26 -08001796
1797 /* FDI must be feeding us bits for PCH ports */
1798 assert_fdi_tx_enabled(dev_priv, pipe);
1799 assert_fdi_rx_enabled(dev_priv, pipe);
1800
Daniel Vetter23670b322012-11-01 09:15:30 +01001801 if (HAS_PCH_CPT(dev)) {
1802 /* Workaround: Set the timing override bit before enabling the
1803 * pch transcoder. */
1804 reg = TRANS_CHICKEN2(pipe);
1805 val = I915_READ(reg);
1806 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1807 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001808 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001809
Daniel Vetterab9412b2013-05-03 11:49:46 +02001810 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001811 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001812 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001813
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001814 if (HAS_PCH_IBX(dev_priv)) {
Jesse Barnese9bcff52011-06-24 12:19:20 -07001815 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001816 * Make the BPC in transcoder be consistent with
1817 * that in pipeconf reg. For HDMI we must use 8bpc
1818 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001819 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001820 val &= ~PIPECONF_BPC_MASK;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001821 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001822 val |= PIPECONF_8BPC;
1823 else
1824 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001825 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001826
1827 val &= ~TRANS_INTERLACE_MASK;
1828 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001829 if (HAS_PCH_IBX(dev_priv) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03001830 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001831 val |= TRANS_LEGACY_INTERLACED_ILK;
1832 else
1833 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001834 else
1835 val |= TRANS_PROGRESSIVE;
1836
Jesse Barnes040484a2011-01-03 12:14:26 -08001837 I915_WRITE(reg, val | TRANS_ENABLE);
Chris Wilson650fbd82016-06-30 15:32:57 +01001838 if (intel_wait_for_register(dev_priv,
1839 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1840 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001841 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001842}
1843
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001844static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001845 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001846{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001847 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001848
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001849 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001850 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001851 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001852
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001853 /* Workaround: set timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001854 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001855 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001856 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001857
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001858 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001859 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001860
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001861 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1862 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001863 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001864 else
1865 val |= TRANS_PROGRESSIVE;
1866
Daniel Vetterab9412b2013-05-03 11:49:46 +02001867 I915_WRITE(LPT_TRANSCONF, val);
Chris Wilsond9f96242016-06-30 15:32:58 +01001868 if (intel_wait_for_register(dev_priv,
1869 LPT_TRANSCONF,
1870 TRANS_STATE_ENABLE,
1871 TRANS_STATE_ENABLE,
1872 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001873 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001874}
1875
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001876static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1877 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001878{
Chris Wilson91c8a322016-07-05 10:40:23 +01001879 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001880 i915_reg_t reg;
1881 uint32_t val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001882
1883 /* FDI relies on the transcoder */
1884 assert_fdi_tx_disabled(dev_priv, pipe);
1885 assert_fdi_rx_disabled(dev_priv, pipe);
1886
Jesse Barnes291906f2011-02-02 12:28:03 -08001887 /* Ports must be off as well */
1888 assert_pch_ports_disabled(dev_priv, pipe);
1889
Daniel Vetterab9412b2013-05-03 11:49:46 +02001890 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001891 val = I915_READ(reg);
1892 val &= ~TRANS_ENABLE;
1893 I915_WRITE(reg, val);
1894 /* wait for PCH transcoder off, transcoder state */
Chris Wilsona7d04662016-06-30 15:32:59 +01001895 if (intel_wait_for_register(dev_priv,
1896 reg, TRANS_STATE_ENABLE, 0,
1897 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001898 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001899
Ville Syrjäläc4656132015-10-29 21:25:56 +02001900 if (HAS_PCH_CPT(dev)) {
Daniel Vetter23670b322012-11-01 09:15:30 +01001901 /* Workaround: Clear the timing override chicken bit again. */
1902 reg = TRANS_CHICKEN2(pipe);
1903 val = I915_READ(reg);
1904 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1905 I915_WRITE(reg, val);
1906 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001907}
1908
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001909static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001910{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001911 u32 val;
1912
Daniel Vetterab9412b2013-05-03 11:49:46 +02001913 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001914 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001915 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001916 /* wait for PCH transcoder off, transcoder state */
Chris Wilsondfdb4742016-06-30 15:33:00 +01001917 if (intel_wait_for_register(dev_priv,
1918 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1919 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001920 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001921
1922 /* Workaround: clear timing override bit. */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001923 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
Daniel Vetter23670b322012-11-01 09:15:30 +01001924 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03001925 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001926}
1927
1928/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001929 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001930 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001931 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001932 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001933 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001934 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001935static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001936{
Paulo Zanoni03722642014-01-17 13:51:09 -02001937 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001938 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni03722642014-01-17 13:51:09 -02001939 enum pipe pipe = crtc->pipe;
Ville Syrjälä1a70a7282015-10-29 21:25:50 +02001940 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter1a240d42012-11-29 22:18:51 +01001941 enum pipe pch_transcoder;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001942 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001943 u32 val;
1944
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03001945 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1946
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001947 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001948 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001949 assert_sprites_disabled(dev_priv, pipe);
1950
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001951 if (HAS_PCH_LPT(dev_priv))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001952 pch_transcoder = TRANSCODER_A;
1953 else
1954 pch_transcoder = pipe;
1955
Jesse Barnesb24e7172011-01-04 15:09:30 -08001956 /*
1957 * A pipe without a PLL won't actually be able to drive bits from
1958 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1959 * need the check.
1960 */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03001961 if (HAS_GMCH_DISPLAY(dev_priv))
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03001962 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03001963 assert_dsi_pll_enabled(dev_priv);
1964 else
1965 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001966 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001967 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08001968 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001969 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001970 assert_fdi_tx_pll_enabled(dev_priv,
1971 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001972 }
1973 /* FIXME: assert CPU port conditions for SNB+ */
1974 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001975
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001976 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001977 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001978 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001979 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1980 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00001981 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02001982 }
Chris Wilson00d70b12011-03-17 07:18:29 +00001983
1984 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02001985 POSTING_READ(reg);
Ville Syrjäläb7792d82015-12-14 18:23:43 +02001986
1987 /*
1988 * Until the pipe starts DSL will read as 0, which would cause
1989 * an apparent vblank timestamp jump, which messes up also the
1990 * frame count when it's derived from the timestamps. So let's
1991 * wait for the pipe to start properly before we call
1992 * drm_crtc_vblank_on()
1993 */
1994 if (dev->max_vblank_count == 0 &&
1995 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1996 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001997}
1998
1999/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002000 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002001 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002002 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002003 * Disable the pipe of @crtc, making sure that various hardware
2004 * specific requirements are met, if applicable, e.g. plane
2005 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002006 *
2007 * Will wait until the pipe has shut down before returning.
2008 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002009static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002010{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002011 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002012 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002013 enum pipe pipe = crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002014 i915_reg_t reg;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002015 u32 val;
2016
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002017 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2018
Jesse Barnesb24e7172011-01-04 15:09:30 -08002019 /*
2020 * Make sure planes won't keep trying to pump pixels to us,
2021 * or we might hang the display.
2022 */
2023 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002024 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002025 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002026
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002027 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002028 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002029 if ((val & PIPECONF_ENABLE) == 0)
2030 return;
2031
Ville Syrjälä67adc642014-08-15 01:21:57 +03002032 /*
2033 * Double wide has implications for planes
2034 * so best keep it disabled when not needed.
2035 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002036 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002037 val &= ~PIPECONF_DOUBLE_WIDE;
2038
2039 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002040 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2041 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002042 val &= ~PIPECONF_ENABLE;
2043
2044 I915_WRITE(reg, val);
2045 if ((val & PIPECONF_ENABLE) == 0)
2046 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002047}
2048
Ville Syrjälä832be822016-01-12 21:08:33 +02002049static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2050{
2051 return IS_GEN2(dev_priv) ? 2048 : 4096;
2052}
2053
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002054static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2055 uint64_t fb_modifier, unsigned int cpp)
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002056{
2057 switch (fb_modifier) {
2058 case DRM_FORMAT_MOD_NONE:
2059 return cpp;
2060 case I915_FORMAT_MOD_X_TILED:
2061 if (IS_GEN2(dev_priv))
2062 return 128;
2063 else
2064 return 512;
2065 case I915_FORMAT_MOD_Y_TILED:
2066 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2067 return 128;
2068 else
2069 return 512;
2070 case I915_FORMAT_MOD_Yf_TILED:
2071 switch (cpp) {
2072 case 1:
2073 return 64;
2074 case 2:
2075 case 4:
2076 return 128;
2077 case 8:
2078 case 16:
2079 return 256;
2080 default:
2081 MISSING_CASE(cpp);
2082 return cpp;
2083 }
2084 break;
2085 default:
2086 MISSING_CASE(fb_modifier);
2087 return cpp;
2088 }
2089}
2090
Ville Syrjälä832be822016-01-12 21:08:33 +02002091unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2092 uint64_t fb_modifier, unsigned int cpp)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002093{
Ville Syrjälä832be822016-01-12 21:08:33 +02002094 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2095 return 1;
2096 else
2097 return intel_tile_size(dev_priv) /
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002098 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002099}
2100
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002101/* Return the tile dimensions in pixel units */
2102static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2103 unsigned int *tile_width,
2104 unsigned int *tile_height,
2105 uint64_t fb_modifier,
2106 unsigned int cpp)
2107{
2108 unsigned int tile_width_bytes =
2109 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2110
2111 *tile_width = tile_width_bytes / cpp;
2112 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2113}
2114
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002115unsigned int
2116intel_fb_align_height(struct drm_device *dev, unsigned int height,
Ville Syrjälä832be822016-01-12 21:08:33 +02002117 uint32_t pixel_format, uint64_t fb_modifier)
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002118{
Ville Syrjälä832be822016-01-12 21:08:33 +02002119 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2120 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2121
2122 return ALIGN(height, tile_height);
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002123}
2124
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002125unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2126{
2127 unsigned int size = 0;
2128 int i;
2129
2130 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2131 size += rot_info->plane[i].width * rot_info->plane[i].height;
2132
2133 return size;
2134}
2135
Daniel Vetter75c82a52015-10-14 16:51:04 +02002136static void
Ville Syrjälä3465c582016-02-15 22:54:43 +02002137intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2138 const struct drm_framebuffer *fb,
2139 unsigned int rotation)
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002140{
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002141 if (intel_rotation_90_or_270(rotation)) {
2142 *view = i915_ggtt_view_rotated;
2143 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2144 } else {
2145 *view = i915_ggtt_view_normal;
2146 }
2147}
2148
2149static void
2150intel_fill_fb_info(struct drm_i915_private *dev_priv,
2151 struct drm_framebuffer *fb)
2152{
2153 struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002154 unsigned int tile_size, tile_width, tile_height, cpp;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002155
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002156 tile_size = intel_tile_size(dev_priv);
2157
2158 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002159 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2160 fb->modifier[0], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002161
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002162 info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
2163 info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002164
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002165 if (info->pixel_format == DRM_FORMAT_NV12) {
Ville Syrjälä832be822016-01-12 21:08:33 +02002166 cpp = drm_format_plane_cpp(fb->pixel_format, 1);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002167 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2168 fb->modifier[1], cpp);
Ville Syrjäläd9b32882016-01-12 21:08:34 +02002169
Ville Syrjälä2d7a2152016-02-15 22:54:47 +02002170 info->uv_offset = fb->offsets[1];
Ville Syrjälä1663b9d2016-02-15 22:54:45 +02002171 info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
2172 info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01002173 }
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002174}
2175
Ville Syrjälä603525d2016-01-12 21:08:37 +02002176static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002177{
2178 if (INTEL_INFO(dev_priv)->gen >= 9)
2179 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002180 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
Wayne Boyer666a4532015-12-09 12:29:35 -08002181 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002182 return 128 * 1024;
2183 else if (INTEL_INFO(dev_priv)->gen >= 4)
2184 return 4 * 1024;
2185 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002186 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002187}
2188
Ville Syrjälä603525d2016-01-12 21:08:37 +02002189static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2190 uint64_t fb_modifier)
2191{
2192 switch (fb_modifier) {
2193 case DRM_FORMAT_MOD_NONE:
2194 return intel_linear_alignment(dev_priv);
2195 case I915_FORMAT_MOD_X_TILED:
2196 if (INTEL_INFO(dev_priv)->gen >= 9)
2197 return 256 * 1024;
2198 return 0;
2199 case I915_FORMAT_MOD_Y_TILED:
2200 case I915_FORMAT_MOD_Yf_TILED:
2201 return 1 * 1024 * 1024;
2202 default:
2203 MISSING_CASE(fb_modifier);
2204 return 0;
2205 }
2206}
2207
Chris Wilson127bd2a2010-07-23 23:32:05 +01002208int
Ville Syrjälä3465c582016-02-15 22:54:43 +02002209intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2210 unsigned int rotation)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002211{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002212 struct drm_device *dev = fb->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002213 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002214 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002215 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002216 u32 alignment;
2217 int ret;
2218
Matt Roperebcdd392014-07-09 16:22:11 -07002219 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2220
Ville Syrjälä603525d2016-01-12 21:08:37 +02002221 alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002222
Ville Syrjälä3465c582016-02-15 22:54:43 +02002223 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002224
Chris Wilson693db182013-03-05 14:52:39 +00002225 /* Note that the w/a also requires 64 PTE of padding following the
2226 * bo. We currently fill all unused PTE with the shadow page and so
2227 * we should always have valid PTE following the scanout preventing
2228 * the VT-d warning.
2229 */
Chris Wilson48f112f2016-06-24 14:07:14 +01002230 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
Chris Wilson693db182013-03-05 14:52:39 +00002231 alignment = 256 * 1024;
2232
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002233 /*
2234 * Global gtt pte registers are special registers which actually forward
2235 * writes to a chunk of system memory. Which means that there is no risk
2236 * that the register values disappear as soon as we call
2237 * intel_runtime_pm_put(), so it is correct to wrap only the
2238 * pin/unpin/fence and not more.
2239 */
2240 intel_runtime_pm_get(dev_priv);
2241
Maarten Lankhorst7580d772015-08-18 13:40:06 +02002242 ret = i915_gem_object_pin_to_display_plane(obj, alignment,
2243 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002244 if (ret)
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002245 goto err_pm;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002246
2247 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2248 * fence, whereas 965+ only requires a fence if using
2249 * framebuffer compression. For simplicity, we always install
2250 * a fence as the cost is not that onerous.
2251 */
Vivek Kasireddy98072162015-10-29 18:54:38 -07002252 if (view.type == I915_GGTT_VIEW_NORMAL) {
2253 ret = i915_gem_object_get_fence(obj);
2254 if (ret == -EDEADLK) {
2255 /*
2256 * -EDEADLK means there are no free fences
2257 * no pending flips.
2258 *
2259 * This is propagated to atomic, but it uses
2260 * -EDEADLK to force a locking recovery, so
2261 * change the returned error to -EBUSY.
2262 */
2263 ret = -EBUSY;
2264 goto err_unpin;
2265 } else if (ret)
2266 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002267
Vivek Kasireddy98072162015-10-29 18:54:38 -07002268 i915_gem_object_pin_fence(obj);
2269 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002270
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002271 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002272 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002273
2274err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002275 i915_gem_object_unpin_from_display_plane(obj, &view);
Maarten Lankhorstb26a6b32015-09-23 13:27:09 +02002276err_pm:
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002277 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002278 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002279}
2280
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +01002281void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002282{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002283 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002284 struct i915_ggtt_view view;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002285
Matt Roperebcdd392014-07-09 16:22:11 -07002286 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2287
Ville Syrjälä3465c582016-02-15 22:54:43 +02002288 intel_fill_fb_ggtt_view(&view, fb, rotation);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002289
Vivek Kasireddy98072162015-10-29 18:54:38 -07002290 if (view.type == I915_GGTT_VIEW_NORMAL)
2291 i915_gem_object_unpin_fence(obj);
2292
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002293 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002294}
2295
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002296/*
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002297 * Adjust the tile offset by moving the difference into
2298 * the x/y offsets.
2299 *
2300 * Input tile dimensions and pitch must already be
2301 * rotated to match x and y, and in pixel units.
2302 */
2303static u32 intel_adjust_tile_offset(int *x, int *y,
2304 unsigned int tile_width,
2305 unsigned int tile_height,
2306 unsigned int tile_size,
2307 unsigned int pitch_tiles,
2308 u32 old_offset,
2309 u32 new_offset)
2310{
2311 unsigned int tiles;
2312
2313 WARN_ON(old_offset & (tile_size - 1));
2314 WARN_ON(new_offset & (tile_size - 1));
2315 WARN_ON(new_offset > old_offset);
2316
2317 tiles = (old_offset - new_offset) / tile_size;
2318
2319 *y += tiles / pitch_tiles * tile_height;
2320 *x += tiles % pitch_tiles * tile_width;
2321
2322 return new_offset;
2323}
2324
2325/*
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002326 * Computes the linear offset to the base tile and adjusts
2327 * x, y. bytes per pixel is assumed to be a power-of-two.
2328 *
2329 * In the 90/270 rotated case, x and y are assumed
2330 * to be already rotated to match the rotated GTT view, and
2331 * pitch is the tile_height aligned framebuffer height.
2332 */
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002333u32 intel_compute_tile_offset(int *x, int *y,
2334 const struct drm_framebuffer *fb, int plane,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002335 unsigned int pitch,
2336 unsigned int rotation)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002337{
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002338 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
2339 uint64_t fb_modifier = fb->modifier[plane];
2340 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002341 u32 offset, offset_aligned, alignment;
2342
2343 alignment = intel_surf_alignment(dev_priv, fb_modifier);
2344 if (alignment)
2345 alignment--;
2346
Ville Syrjäläb5c65332016-01-12 21:08:31 +02002347 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002348 unsigned int tile_size, tile_width, tile_height;
2349 unsigned int tile_rows, tiles, pitch_tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002350
Ville Syrjäläd8433102016-01-12 21:08:35 +02002351 tile_size = intel_tile_size(dev_priv);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002352 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2353 fb_modifier, cpp);
2354
2355 if (intel_rotation_90_or_270(rotation)) {
2356 pitch_tiles = pitch / tile_height;
2357 swap(tile_width, tile_height);
2358 } else {
2359 pitch_tiles = pitch / (tile_width * cpp);
2360 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002361
Ville Syrjäläd8433102016-01-12 21:08:35 +02002362 tile_rows = *y / tile_height;
2363 *y %= tile_height;
Chris Wilsonbc752862013-02-21 20:04:31 +00002364
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002365 tiles = *x / tile_width;
2366 *x %= tile_width;
Ville Syrjäläd8433102016-01-12 21:08:35 +02002367
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002368 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2369 offset_aligned = offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002370
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002371 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2372 tile_size, pitch_tiles,
2373 offset, offset_aligned);
2374 } else {
Chris Wilsonbc752862013-02-21 20:04:31 +00002375 offset = *y * pitch + *x * cpp;
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002376 offset_aligned = offset & ~alignment;
2377
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002378 *y = (offset & alignment) / pitch;
2379 *x = ((offset & alignment) - *y * pitch) / cpp;
Chris Wilsonbc752862013-02-21 20:04:31 +00002380 }
Ville Syrjälä29cf9492016-02-15 22:54:42 +02002381
2382 return offset_aligned;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002383}
2384
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002385static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002386{
2387 switch (format) {
2388 case DISPPLANE_8BPP:
2389 return DRM_FORMAT_C8;
2390 case DISPPLANE_BGRX555:
2391 return DRM_FORMAT_XRGB1555;
2392 case DISPPLANE_BGRX565:
2393 return DRM_FORMAT_RGB565;
2394 default:
2395 case DISPPLANE_BGRX888:
2396 return DRM_FORMAT_XRGB8888;
2397 case DISPPLANE_RGBX888:
2398 return DRM_FORMAT_XBGR8888;
2399 case DISPPLANE_BGRX101010:
2400 return DRM_FORMAT_XRGB2101010;
2401 case DISPPLANE_RGBX101010:
2402 return DRM_FORMAT_XBGR2101010;
2403 }
2404}
2405
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002406static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2407{
2408 switch (format) {
2409 case PLANE_CTL_FORMAT_RGB_565:
2410 return DRM_FORMAT_RGB565;
2411 default:
2412 case PLANE_CTL_FORMAT_XRGB_8888:
2413 if (rgb_order) {
2414 if (alpha)
2415 return DRM_FORMAT_ABGR8888;
2416 else
2417 return DRM_FORMAT_XBGR8888;
2418 } else {
2419 if (alpha)
2420 return DRM_FORMAT_ARGB8888;
2421 else
2422 return DRM_FORMAT_XRGB8888;
2423 }
2424 case PLANE_CTL_FORMAT_XRGB_2101010:
2425 if (rgb_order)
2426 return DRM_FORMAT_XBGR2101010;
2427 else
2428 return DRM_FORMAT_XRGB2101010;
2429 }
2430}
2431
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002432static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002433intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2434 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002435{
2436 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -03002437 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002438 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002439 struct drm_i915_gem_object *obj = NULL;
2440 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002441 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002442 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2443 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2444 PAGE_SIZE);
2445
2446 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002447
Chris Wilsonff2652e2014-03-10 08:07:02 +00002448 if (plane_config->size == 0)
2449 return false;
2450
Paulo Zanoni3badb492015-09-23 12:52:23 -03002451 /* If the FB is too big, just don't use it since fbdev is not very
2452 * important and we should probably use that space with FBC or other
2453 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002454 if (size_aligned * 2 > ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -03002455 return false;
2456
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002457 mutex_lock(&dev->struct_mutex);
2458
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002459 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2460 base_aligned,
2461 base_aligned,
2462 size_aligned);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002463 if (!obj) {
2464 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002465 return false;
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002466 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002467
Damien Lespiau49af4492015-01-20 12:51:44 +00002468 obj->tiling_mode = plane_config->tiling;
2469 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002470 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002471
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002472 mode_cmd.pixel_format = fb->pixel_format;
2473 mode_cmd.width = fb->width;
2474 mode_cmd.height = fb->height;
2475 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002476 mode_cmd.modifier[0] = fb->modifier[0];
2477 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002478
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002479 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002480 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002481 DRM_DEBUG_KMS("intel fb init failed\n");
2482 goto out_unref_obj;
2483 }
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +00002484
Jesse Barnes46f297f2014-03-07 08:57:48 -08002485 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002486
Daniel Vetterf6936e22015-03-26 12:17:05 +01002487 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002488 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002489
2490out_unref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002491 i915_gem_object_put(obj);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002492 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002493 return false;
2494}
2495
Daniel Vetter5a21b662016-05-24 17:13:53 +02002496/* Update plane->state->fb to match plane->fb after driver-internal updates */
2497static void
2498update_state_fb(struct drm_plane *plane)
2499{
2500 if (plane->fb == plane->state->fb)
2501 return;
2502
2503 if (plane->state->fb)
2504 drm_framebuffer_unreference(plane->state->fb);
2505 plane->state->fb = plane->fb;
2506 if (plane->state->fb)
2507 drm_framebuffer_reference(plane->state->fb);
2508}
2509
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002510static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002511intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2512 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002513{
2514 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002515 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002516 struct drm_crtc *c;
2517 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002518 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002519 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002520 struct drm_plane_state *plane_state = primary->state;
Matt Roper200757f2015-12-03 11:37:36 -08002521 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2522 struct intel_plane *intel_plane = to_intel_plane(primary);
Matt Roper0a8d8a82015-12-03 11:37:38 -08002523 struct intel_plane_state *intel_state =
2524 to_intel_plane_state(plane_state);
Daniel Vetter88595ac2015-03-26 12:42:24 +01002525 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002526
Damien Lespiau2d140302015-02-05 17:22:18 +00002527 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002528 return;
2529
Daniel Vetterf6936e22015-03-26 12:17:05 +01002530 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002531 fb = &plane_config->fb->base;
2532 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002533 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002534
Damien Lespiau2d140302015-02-05 17:22:18 +00002535 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002536
2537 /*
2538 * Failed to alloc the obj, check to see if we should share
2539 * an fb with another CRTC instead
2540 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002541 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002542 i = to_intel_crtc(c);
2543
2544 if (c == &intel_crtc->base)
2545 continue;
2546
Matt Roper2ff8fde2014-07-08 07:50:07 -07002547 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002548 continue;
2549
Daniel Vetter88595ac2015-03-26 12:42:24 +01002550 fb = c->primary->fb;
2551 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002552 continue;
2553
Daniel Vetter88595ac2015-03-26 12:42:24 +01002554 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002555 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002556 drm_framebuffer_reference(fb);
2557 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002558 }
2559 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002560
Matt Roper200757f2015-12-03 11:37:36 -08002561 /*
2562 * We've failed to reconstruct the BIOS FB. Current display state
2563 * indicates that the primary plane is visible, but has a NULL FB,
2564 * which will lead to problems later if we don't fix it up. The
2565 * simplest solution is to just disable the primary plane now and
2566 * pretend the BIOS never had it enabled.
2567 */
2568 to_intel_plane_state(plane_state)->visible = false;
2569 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
Ville Syrjälä2622a082016-03-09 19:07:26 +02002570 intel_pre_disable_primary_noatomic(&intel_crtc->base);
Matt Roper200757f2015-12-03 11:37:36 -08002571 intel_plane->disable_plane(primary, &intel_crtc->base);
2572
Daniel Vetter88595ac2015-03-26 12:42:24 +01002573 return;
2574
2575valid_fb:
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002576 plane_state->src_x = 0;
2577 plane_state->src_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002578 plane_state->src_w = fb->width << 16;
2579 plane_state->src_h = fb->height << 16;
2580
Ville Syrjäläf44e2652015-11-13 19:16:13 +02002581 plane_state->crtc_x = 0;
2582 plane_state->crtc_y = 0;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002583 plane_state->crtc_w = fb->width;
2584 plane_state->crtc_h = fb->height;
2585
Matt Roper0a8d8a82015-12-03 11:37:38 -08002586 intel_state->src.x1 = plane_state->src_x;
2587 intel_state->src.y1 = plane_state->src_y;
2588 intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
2589 intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
2590 intel_state->dst.x1 = plane_state->crtc_x;
2591 intel_state->dst.y1 = plane_state->crtc_y;
2592 intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
2593 intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
2594
Daniel Vetter88595ac2015-03-26 12:42:24 +01002595 obj = intel_fb_obj(fb);
2596 if (obj->tiling_mode != I915_TILING_NONE)
2597 dev_priv->preserve_bios_swizzle = true;
2598
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002599 drm_framebuffer_reference(fb);
2600 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002601 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002602 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002603 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002604}
2605
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002606static void i9xx_update_primary_plane(struct drm_plane *primary,
2607 const struct intel_crtc_state *crtc_state,
2608 const struct intel_plane_state *plane_state)
Jesse Barnes81255562010-08-02 12:07:50 -07002609{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002610 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002611 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002612 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2613 struct drm_framebuffer *fb = plane_state->base.fb;
2614 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes81255562010-08-02 12:07:50 -07002615 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002616 u32 linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002617 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002618 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002619 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002620 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002621 int x = plane_state->src.x1 >> 16;
2622 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002623
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002624 dspcntr = DISPPLANE_GAMMA_ENABLE;
2625
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002626 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002627
2628 if (INTEL_INFO(dev)->gen < 4) {
2629 if (intel_crtc->pipe == PIPE_B)
2630 dspcntr |= DISPPLANE_SEL_PIPE_B;
2631
2632 /* pipesrc and dspsize control the size that is scaled from,
2633 * which should always be the user's requested size.
2634 */
2635 I915_WRITE(DSPSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002636 ((crtc_state->pipe_src_h - 1) << 16) |
2637 (crtc_state->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002638 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002639 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2640 I915_WRITE(PRIMSIZE(plane),
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002641 ((crtc_state->pipe_src_h - 1) << 16) |
2642 (crtc_state->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002643 I915_WRITE(PRIMPOS(plane), 0);
2644 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002645 }
2646
Ville Syrjälä57779d02012-10-31 17:50:14 +02002647 switch (fb->pixel_format) {
2648 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002649 dspcntr |= DISPPLANE_8BPP;
2650 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002651 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002652 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002653 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002654 case DRM_FORMAT_RGB565:
2655 dspcntr |= DISPPLANE_BGRX565;
2656 break;
2657 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002658 dspcntr |= DISPPLANE_BGRX888;
2659 break;
2660 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002661 dspcntr |= DISPPLANE_RGBX888;
2662 break;
2663 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002664 dspcntr |= DISPPLANE_BGRX101010;
2665 break;
2666 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002667 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002668 break;
2669 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002670 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002671 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002672
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002673 if (INTEL_INFO(dev)->gen >= 4 &&
2674 obj->tiling_mode != I915_TILING_NONE)
2675 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002676
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002677 if (IS_G4X(dev))
2678 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2679
Ville Syrjäläac484962016-01-20 21:05:26 +02002680 linear_offset = y * fb->pitches[0] + x * cpp;
Jesse Barnes81255562010-08-02 12:07:50 -07002681
Daniel Vetterc2c75132012-07-05 12:17:30 +02002682 if (INTEL_INFO(dev)->gen >= 4) {
2683 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002684 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002685 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002686 linear_offset -= intel_crtc->dspaddr_offset;
2687 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002688 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002689 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002690
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002691 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302692 dspcntr |= DISPPLANE_ROTATE_180;
2693
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002694 x += (crtc_state->pipe_src_w - 1);
2695 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302696
2697 /* Finding the last pixel of the last line of the display
2698 data and adding to linear_offset*/
2699 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002700 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002701 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302702 }
2703
Paulo Zanoni2db33662015-09-14 15:20:03 -03002704 intel_crtc->adjusted_x = x;
2705 intel_crtc->adjusted_y = y;
2706
Sonika Jindal48404c12014-08-22 14:06:04 +05302707 I915_WRITE(reg, dspcntr);
2708
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002709 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002710 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002711 I915_WRITE(DSPSURF(plane),
2712 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002713 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002714 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002715 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002716 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002717 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002718}
2719
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002720static void i9xx_disable_primary_plane(struct drm_plane *primary,
2721 struct drm_crtc *crtc)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002722{
2723 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002724 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002725 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002726 int plane = intel_crtc->plane;
2727
2728 I915_WRITE(DSPCNTR(plane), 0);
2729 if (INTEL_INFO(dev_priv)->gen >= 4)
2730 I915_WRITE(DSPSURF(plane), 0);
2731 else
2732 I915_WRITE(DSPADDR(plane), 0);
2733 POSTING_READ(DSPCNTR(plane));
2734}
2735
2736static void ironlake_update_primary_plane(struct drm_plane *primary,
2737 const struct intel_crtc_state *crtc_state,
2738 const struct intel_plane_state *plane_state)
2739{
2740 struct drm_device *dev = primary->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002741 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002742 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2743 struct drm_framebuffer *fb = plane_state->base.fb;
2744 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002745 int plane = intel_crtc->plane;
Ville Syrjälä54ea9da2016-01-20 21:05:25 +02002746 u32 linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002747 u32 dspcntr;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02002748 i915_reg_t reg = DSPCNTR(plane);
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002749 unsigned int rotation = plane_state->base.rotation;
Ville Syrjäläac484962016-01-20 21:05:26 +02002750 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002751 int x = plane_state->src.x1 >> 16;
2752 int y = plane_state->src.y1 >> 16;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002753
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002754 dspcntr = DISPPLANE_GAMMA_ENABLE;
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002755 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002756
2757 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2758 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2759
Ville Syrjälä57779d02012-10-31 17:50:14 +02002760 switch (fb->pixel_format) {
2761 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002762 dspcntr |= DISPPLANE_8BPP;
2763 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002764 case DRM_FORMAT_RGB565:
2765 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002766 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002767 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002768 dspcntr |= DISPPLANE_BGRX888;
2769 break;
2770 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002771 dspcntr |= DISPPLANE_RGBX888;
2772 break;
2773 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002774 dspcntr |= DISPPLANE_BGRX101010;
2775 break;
2776 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002777 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002778 break;
2779 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002780 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002781 }
2782
2783 if (obj->tiling_mode != I915_TILING_NONE)
2784 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002785
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002786 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002787 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002788
Ville Syrjäläac484962016-01-20 21:05:26 +02002789 linear_offset = y * fb->pitches[0] + x * cpp;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002790 intel_crtc->dspaddr_offset =
Ville Syrjälä4f2d9932016-02-15 22:54:44 +02002791 intel_compute_tile_offset(&x, &y, fb, 0,
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002792 fb->pitches[0], rotation);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002793 linear_offset -= intel_crtc->dspaddr_offset;
Ville Syrjälä8d0deca2016-02-15 22:54:41 +02002794 if (rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302795 dspcntr |= DISPPLANE_ROTATE_180;
2796
2797 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002798 x += (crtc_state->pipe_src_w - 1);
2799 y += (crtc_state->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302800
2801 /* Finding the last pixel of the last line of the display
2802 data and adding to linear_offset*/
2803 linear_offset +=
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002804 (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
Ville Syrjäläac484962016-01-20 21:05:26 +02002805 (crtc_state->pipe_src_w - 1) * cpp;
Sonika Jindal48404c12014-08-22 14:06:04 +05302806 }
2807 }
2808
Paulo Zanoni2db33662015-09-14 15:20:03 -03002809 intel_crtc->adjusted_x = x;
2810 intel_crtc->adjusted_y = y;
2811
Sonika Jindal48404c12014-08-22 14:06:04 +05302812 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002813
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002814 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002815 I915_WRITE(DSPSURF(plane),
2816 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002817 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002818 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2819 } else {
2820 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2821 I915_WRITE(DSPLINOFF(plane), linear_offset);
2822 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002823 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002824}
2825
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002826u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
2827 uint64_t fb_modifier, uint32_t pixel_format)
Damien Lespiaub3218032015-02-27 11:15:18 +00002828{
Ville Syrjälä7b49f942016-01-12 21:08:32 +02002829 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
2830 return 64;
2831 } else {
2832 int cpp = drm_format_plane_cpp(pixel_format, 0);
Damien Lespiaub3218032015-02-27 11:15:18 +00002833
Ville Syrjälä27ba3912016-02-15 22:54:40 +02002834 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
Damien Lespiaub3218032015-02-27 11:15:18 +00002835 }
2836}
2837
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002838u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
2839 struct drm_i915_gem_object *obj,
2840 unsigned int plane)
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002841{
Daniel Vetterce7f1722015-10-14 16:51:06 +02002842 struct i915_ggtt_view view;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002843 struct i915_vma *vma;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002844 u64 offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002845
Ville Syrjäläe7941292016-01-19 18:23:17 +02002846 intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
Ville Syrjälä3465c582016-02-15 22:54:43 +02002847 intel_plane->base.state->rotation);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002848
Daniel Vetterce7f1722015-10-14 16:51:06 +02002849 vma = i915_gem_obj_to_ggtt_view(obj, &view);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002850 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
Daniel Vetterce7f1722015-10-14 16:51:06 +02002851 view.type))
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002852 return -1;
2853
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002854 offset = vma->node.start;
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002855
2856 if (plane == 1) {
Ville Syrjälä7723f47d2016-01-20 21:05:22 +02002857 offset += vma->ggtt_view.params.rotated.uv_start_page *
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01002858 PAGE_SIZE;
2859 }
2860
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002861 WARN_ON(upper_32_bits(offset));
2862
2863 return lower_32_bits(offset);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002864}
2865
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002866static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2867{
2868 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002869 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002870
2871 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2872 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2873 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002874}
2875
Chandra Kondurua1b22782015-04-07 15:28:45 -07002876/*
2877 * This function detaches (aka. unbinds) unused scalers in hardware
2878 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002879static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002880{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002881 struct intel_crtc_scaler_state *scaler_state;
2882 int i;
2883
Chandra Kondurua1b22782015-04-07 15:28:45 -07002884 scaler_state = &intel_crtc->config->scaler_state;
2885
2886 /* loop through and disable scalers that aren't in use */
2887 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002888 if (!scaler_state->scalers[i].in_use)
2889 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002890 }
2891}
2892
Chandra Konduru6156a452015-04-27 13:48:39 -07002893u32 skl_plane_ctl_format(uint32_t pixel_format)
2894{
Chandra Konduru6156a452015-04-27 13:48:39 -07002895 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002896 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002897 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002898 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002899 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002900 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002901 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002902 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002903 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002904 /*
2905 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2906 * to be already pre-multiplied. We need to add a knob (or a different
2907 * DRM_FORMAT) for user-space to configure that.
2908 */
2909 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002910 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002911 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002912 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002913 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002914 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002915 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002916 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002917 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002918 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002919 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002920 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002921 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002922 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002923 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002924 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002925 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002926 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002927 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002928 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002929 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002930
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002931 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002932}
2933
2934u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2935{
Chandra Konduru6156a452015-04-27 13:48:39 -07002936 switch (fb_modifier) {
2937 case DRM_FORMAT_MOD_NONE:
2938 break;
2939 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002940 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002941 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002942 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002943 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002944 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002945 default:
2946 MISSING_CASE(fb_modifier);
2947 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002948
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002949 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950}
2951
2952u32 skl_plane_ctl_rotation(unsigned int rotation)
2953{
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 switch (rotation) {
2955 case BIT(DRM_ROTATE_0):
2956 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302957 /*
2958 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2959 * while i915 HW rotation is clockwise, thats why this swapping.
2960 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302962 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002963 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002964 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002965 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302966 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002967 default:
2968 MISSING_CASE(rotation);
2969 }
2970
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002971 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002972}
2973
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002974static void skylake_update_primary_plane(struct drm_plane *plane,
2975 const struct intel_crtc_state *crtc_state,
2976 const struct intel_plane_state *plane_state)
Damien Lespiau70d21f02013-07-03 21:06:04 +01002977{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002978 struct drm_device *dev = plane->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002979 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
2981 struct drm_framebuffer *fb = plane_state->base.fb;
2982 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002983 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302984 u32 plane_ctl, stride_div, stride;
2985 u32 tile_height, plane_offset, plane_size;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002986 unsigned int rotation = plane_state->base.rotation;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302987 int x_offset, y_offset;
Mika Kuoppala44eb0cb2015-10-30 13:26:15 +02002988 u32 surf_addr;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01002989 int scaler_id = plane_state->scaler_id;
2990 int src_x = plane_state->src.x1 >> 16;
2991 int src_y = plane_state->src.y1 >> 16;
2992 int src_w = drm_rect_width(&plane_state->src) >> 16;
2993 int src_h = drm_rect_height(&plane_state->src) >> 16;
2994 int dst_x = plane_state->dst.x1;
2995 int dst_y = plane_state->dst.y1;
2996 int dst_w = drm_rect_width(&plane_state->dst);
2997 int dst_h = drm_rect_height(&plane_state->dst);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002998
2999 plane_ctl = PLANE_CTL_ENABLE |
3000 PLANE_CTL_PIPE_GAMMA_ENABLE |
3001 PLANE_CTL_PIPE_CSC_ENABLE;
3002
Chandra Konduru6156a452015-04-27 13:48:39 -07003003 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3004 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003005 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Chandra Konduru6156a452015-04-27 13:48:39 -07003006 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003007
Ville Syrjälä7b49f942016-01-12 21:08:32 +02003008 stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +00003009 fb->pixel_format);
Tvrtko Ursulindedf2782015-09-21 10:45:35 +01003010 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303011
Paulo Zanonia42e5a22015-09-30 17:05:43 -03003012 WARN_ON(drm_rect_width(&plane_state->src) == 0);
Chandra Konduru6156a452015-04-27 13:48:39 -07003013
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303014 if (intel_rotation_90_or_270(rotation)) {
Ville Syrjälä832be822016-01-12 21:08:33 +02003015 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3016
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303017 /* stride = Surface height in tiles */
Ville Syrjälä832be822016-01-12 21:08:33 +02003018 tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303019 stride = DIV_ROUND_UP(fb->height, tile_height);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003020 x_offset = stride * tile_height - src_y - src_h;
3021 y_offset = src_x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003022 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303023 } else {
3024 stride = fb->pitches[0] / stride_div;
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003025 x_offset = src_x;
3026 y_offset = src_y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003027 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303028 }
3029 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003030
Paulo Zanoni2db33662015-09-14 15:20:03 -03003031 intel_crtc->adjusted_x = x_offset;
3032 intel_crtc->adjusted_y = y_offset;
3033
Damien Lespiau70d21f02013-07-03 21:06:04 +01003034 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303035 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3036 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3037 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003038
3039 if (scaler_id >= 0) {
3040 uint32_t ps_ctrl = 0;
3041
3042 WARN_ON(!dst_w || !dst_h);
3043 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3044 crtc_state->scaler_state.scalers[scaler_id].mode;
3045 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3046 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3047 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3048 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3049 I915_WRITE(PLANE_POS(pipe, 0), 0);
3050 } else {
3051 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3052 }
3053
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003054 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003055
3056 POSTING_READ(PLANE_SURF(pipe, 0));
3057}
3058
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003059static void skylake_disable_primary_plane(struct drm_plane *primary,
3060 struct drm_crtc *crtc)
3061{
3062 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003063 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003064 int pipe = to_intel_crtc(crtc)->pipe;
3065
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003066 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3067 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3068 POSTING_READ(PLANE_SURF(pipe, 0));
3069}
3070
Jesse Barnes17638cd2011-06-24 12:19:23 -07003071/* Assume fb object is pinned & idle & fenced and just update base pointers */
3072static int
3073intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3074 int x, int y, enum mode_set_atomic state)
3075{
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003076 /* Support for kgdboc is disabled, this needs a major rework. */
3077 DRM_ERROR("legacy panic handler not supported any more.\n");
Jesse Barnes17638cd2011-06-24 12:19:23 -07003078
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003079 return -ENODEV;
Jesse Barnes81255562010-08-02 12:07:50 -07003080}
3081
Daniel Vetter5a21b662016-05-24 17:13:53 +02003082static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3083{
3084 struct intel_crtc *crtc;
3085
Chris Wilson91c8a322016-07-05 10:40:23 +01003086 for_each_intel_crtc(&dev_priv->drm, crtc)
Daniel Vetter5a21b662016-05-24 17:13:53 +02003087 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3088}
3089
Ville Syrjälä75147472014-11-24 18:28:11 +02003090static void intel_update_primary_planes(struct drm_device *dev)
3091{
Ville Syrjälä75147472014-11-24 18:28:11 +02003092 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003093
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003094 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003095 struct intel_plane *plane = to_intel_plane(crtc->primary);
3096 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003097
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003098 drm_modeset_lock_crtc(crtc, &plane->base);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003099 plane_state = to_intel_plane_state(plane->base.state);
3100
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +01003101 if (plane_state->visible)
3102 plane->update_plane(&plane->base,
3103 to_intel_crtc_state(crtc->state),
3104 plane_state);
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003105
3106 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003107 }
3108}
3109
Chris Wilsonc0336662016-05-06 15:40:21 +01003110void intel_prepare_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003111{
3112 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003113 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003114 return;
3115
3116 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003117 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003118 return;
3119
Chris Wilson91c8a322016-07-05 10:40:23 +01003120 drm_modeset_lock_all(&dev_priv->drm);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003121 /*
3122 * Disabling the crtcs gracefully seems nicer. Also the
3123 * g33 docs say we should at least disable all the planes.
3124 */
Chris Wilson91c8a322016-07-05 10:40:23 +01003125 intel_display_suspend(&dev_priv->drm);
Ville Syrjälä75147472014-11-24 18:28:11 +02003126}
3127
Chris Wilsonc0336662016-05-06 15:40:21 +01003128void intel_finish_reset(struct drm_i915_private *dev_priv)
Ville Syrjälä75147472014-11-24 18:28:11 +02003129{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003130 /*
3131 * Flips in the rings will be nuked by the reset,
3132 * so complete all pending flips so that user space
3133 * will get its events and not get stuck.
3134 */
3135 intel_complete_page_flips(dev_priv);
3136
Ville Syrjälä75147472014-11-24 18:28:11 +02003137 /* no reset support for gen2 */
Chris Wilsonc0336662016-05-06 15:40:21 +01003138 if (IS_GEN2(dev_priv))
Ville Syrjälä75147472014-11-24 18:28:11 +02003139 return;
3140
3141 /* reset doesn't touch the display */
Chris Wilsonc0336662016-05-06 15:40:21 +01003142 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
Ville Syrjälä75147472014-11-24 18:28:11 +02003143 /*
3144 * Flips in the rings have been nuked by the reset,
3145 * so update the base address of all primary
3146 * planes to the the last fb to make sure we're
3147 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003148 *
3149 * FIXME: Atomic will make this obsolete since we won't schedule
3150 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003151 */
Chris Wilson91c8a322016-07-05 10:40:23 +01003152 intel_update_primary_planes(&dev_priv->drm);
Ville Syrjälä75147472014-11-24 18:28:11 +02003153 return;
3154 }
3155
3156 /*
3157 * The display has been reset as well,
3158 * so need a full re-initialization.
3159 */
3160 intel_runtime_pm_disable_interrupts(dev_priv);
3161 intel_runtime_pm_enable_interrupts(dev_priv);
3162
Chris Wilson91c8a322016-07-05 10:40:23 +01003163 intel_modeset_init_hw(&dev_priv->drm);
Ville Syrjälä75147472014-11-24 18:28:11 +02003164
3165 spin_lock_irq(&dev_priv->irq_lock);
3166 if (dev_priv->display.hpd_irq_setup)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01003167 dev_priv->display.hpd_irq_setup(dev_priv);
Ville Syrjälä75147472014-11-24 18:28:11 +02003168 spin_unlock_irq(&dev_priv->irq_lock);
3169
Chris Wilson91c8a322016-07-05 10:40:23 +01003170 intel_display_resume(&dev_priv->drm);
Ville Syrjälä75147472014-11-24 18:28:11 +02003171
3172 intel_hpd_init(dev_priv);
3173
Chris Wilson91c8a322016-07-05 10:40:23 +01003174 drm_modeset_unlock_all(&dev_priv->drm);
Ville Syrjälä75147472014-11-24 18:28:11 +02003175}
3176
Chris Wilson7d5e3792014-03-04 13:15:08 +00003177static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3178{
Daniel Vetter5a21b662016-05-24 17:13:53 +02003179 struct drm_device *dev = crtc->dev;
3180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3181 unsigned reset_counter;
3182 bool pending;
3183
3184 reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
3185 if (intel_crtc->reset_counter != reset_counter)
3186 return false;
3187
3188 spin_lock_irq(&dev->event_lock);
3189 pending = to_intel_crtc(crtc)->flip_work != NULL;
3190 spin_unlock_irq(&dev->event_lock);
3191
3192 return pending;
Chris Wilson7d5e3792014-03-04 13:15:08 +00003193}
3194
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003195static void intel_update_pipe_config(struct intel_crtc *crtc,
3196 struct intel_crtc_state *old_crtc_state)
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003197{
3198 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003199 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003200 struct intel_crtc_state *pipe_config =
3201 to_intel_crtc_state(crtc->base.state);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003202
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003203 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3204 crtc->base.mode = crtc->base.state->mode;
3205
3206 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3207 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3208 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003209
3210 /*
3211 * Update pipe size and adjust fitter if needed: the reason for this is
3212 * that in compute_mode_changes we check the native mode (not the pfit
3213 * mode) to see if we can flip rather than do a full mode set. In the
3214 * fastboot case, we'll flip, but if we don't update the pipesrc and
3215 * pfit state, we'll end up with a big fb scanned out into the wrong
3216 * sized surface.
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003217 */
3218
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003219 I915_WRITE(PIPESRC(crtc->pipe),
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02003220 ((pipe_config->pipe_src_w - 1) << 16) |
3221 (pipe_config->pipe_src_h - 1));
3222
3223 /* on skylake this is done by detaching scalers */
3224 if (INTEL_INFO(dev)->gen >= 9) {
3225 skl_detach_scalers(crtc);
3226
3227 if (pipe_config->pch_pfit.enabled)
3228 skylake_pfit_enable(crtc);
3229 } else if (HAS_PCH_SPLIT(dev)) {
3230 if (pipe_config->pch_pfit.enabled)
3231 ironlake_pfit_enable(crtc);
3232 else if (old_crtc_state->pch_pfit.enabled)
3233 ironlake_pfit_disable(crtc, true);
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003234 }
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003235}
3236
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003237static void intel_fdi_normal_train(struct drm_crtc *crtc)
3238{
3239 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003240 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3242 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003243 i915_reg_t reg;
3244 u32 temp;
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003245
3246 /* enable normal train */
3247 reg = FDI_TX_CTL(pipe);
3248 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003249 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003250 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3251 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003252 } else {
3253 temp &= ~FDI_LINK_TRAIN_NONE;
3254 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003255 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003256 I915_WRITE(reg, temp);
3257
3258 reg = FDI_RX_CTL(pipe);
3259 temp = I915_READ(reg);
3260 if (HAS_PCH_CPT(dev)) {
3261 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3262 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3263 } else {
3264 temp &= ~FDI_LINK_TRAIN_NONE;
3265 temp |= FDI_LINK_TRAIN_NONE;
3266 }
3267 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3268
3269 /* wait one idle pattern time */
3270 POSTING_READ(reg);
3271 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003272
3273 /* IVB wants error correction enabled */
3274 if (IS_IVYBRIDGE(dev))
3275 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3276 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003277}
3278
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003279/* The FDI link training functions for ILK/Ibexpeak. */
3280static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3281{
3282 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003283 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003284 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3285 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003286 i915_reg_t reg;
3287 u32 temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003288
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003289 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003290 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003291
Adam Jacksone1a44742010-06-25 15:32:14 -04003292 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3293 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003294 reg = FDI_RX_IMR(pipe);
3295 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003296 temp &= ~FDI_RX_SYMBOL_LOCK;
3297 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003298 I915_WRITE(reg, temp);
3299 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003300 udelay(150);
3301
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003302 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003303 reg = FDI_TX_CTL(pipe);
3304 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003305 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003306 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003307 temp &= ~FDI_LINK_TRAIN_NONE;
3308 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003309 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003310
Chris Wilson5eddb702010-09-11 13:48:45 +01003311 reg = FDI_RX_CTL(pipe);
3312 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003313 temp &= ~FDI_LINK_TRAIN_NONE;
3314 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003315 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3316
3317 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003318 udelay(150);
3319
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003320 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003321 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3322 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3323 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003324
Chris Wilson5eddb702010-09-11 13:48:45 +01003325 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003326 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003327 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003328 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3329
3330 if ((temp & FDI_RX_BIT_LOCK)) {
3331 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003332 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003333 break;
3334 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003335 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003336 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003337 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003338
3339 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003340 reg = FDI_TX_CTL(pipe);
3341 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003342 temp &= ~FDI_LINK_TRAIN_NONE;
3343 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003344 I915_WRITE(reg, temp);
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_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003350 I915_WRITE(reg, temp);
3351
3352 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003353 udelay(150);
3354
Chris Wilson5eddb702010-09-11 13:48:45 +01003355 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003356 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003357 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003358 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3359
3360 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003361 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003362 DRM_DEBUG_KMS("FDI train 2 done.\n");
3363 break;
3364 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003365 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003366 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003367 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003368
3369 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003370
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003371}
3372
Akshay Joshi0206e352011-08-16 15:34:10 -04003373static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003374 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3375 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3376 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3377 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3378};
3379
3380/* The FDI link training functions for SNB/Cougarpoint. */
3381static void gen6_fdi_link_train(struct drm_crtc *crtc)
3382{
3383 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003384 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3386 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003387 i915_reg_t reg;
3388 u32 temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003389
Adam Jacksone1a44742010-06-25 15:32:14 -04003390 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3391 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003392 reg = FDI_RX_IMR(pipe);
3393 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003394 temp &= ~FDI_RX_SYMBOL_LOCK;
3395 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003396 I915_WRITE(reg, temp);
3397
3398 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003399 udelay(150);
3400
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003401 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003402 reg = FDI_TX_CTL(pipe);
3403 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003404 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003405 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003406 temp &= ~FDI_LINK_TRAIN_NONE;
3407 temp |= FDI_LINK_TRAIN_PATTERN_1;
3408 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3409 /* SNB-B */
3410 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003411 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003412
Daniel Vetterd74cf322012-10-26 10:58:13 +02003413 I915_WRITE(FDI_RX_MISC(pipe),
3414 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3415
Chris Wilson5eddb702010-09-11 13:48:45 +01003416 reg = FDI_RX_CTL(pipe);
3417 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003418 if (HAS_PCH_CPT(dev)) {
3419 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3420 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3421 } else {
3422 temp &= ~FDI_LINK_TRAIN_NONE;
3423 temp |= FDI_LINK_TRAIN_PATTERN_1;
3424 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003425 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3426
3427 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003428 udelay(150);
3429
Akshay Joshi0206e352011-08-16 15:34:10 -04003430 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003431 reg = FDI_TX_CTL(pipe);
3432 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003433 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3434 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003435 I915_WRITE(reg, temp);
3436
3437 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003438 udelay(500);
3439
Sean Paulfa37d392012-03-02 12:53:39 -05003440 for (retry = 0; retry < 5; retry++) {
3441 reg = FDI_RX_IIR(pipe);
3442 temp = I915_READ(reg);
3443 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3444 if (temp & FDI_RX_BIT_LOCK) {
3445 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3446 DRM_DEBUG_KMS("FDI train 1 done.\n");
3447 break;
3448 }
3449 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003450 }
Sean Paulfa37d392012-03-02 12:53:39 -05003451 if (retry < 5)
3452 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003453 }
3454 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003455 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003456
3457 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003458 reg = FDI_TX_CTL(pipe);
3459 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003460 temp &= ~FDI_LINK_TRAIN_NONE;
3461 temp |= FDI_LINK_TRAIN_PATTERN_2;
3462 if (IS_GEN6(dev)) {
3463 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3464 /* SNB-B */
3465 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3466 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003467 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003468
Chris Wilson5eddb702010-09-11 13:48:45 +01003469 reg = FDI_RX_CTL(pipe);
3470 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003471 if (HAS_PCH_CPT(dev)) {
3472 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3473 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3474 } else {
3475 temp &= ~FDI_LINK_TRAIN_NONE;
3476 temp |= FDI_LINK_TRAIN_PATTERN_2;
3477 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003478 I915_WRITE(reg, temp);
3479
3480 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003481 udelay(150);
3482
Akshay Joshi0206e352011-08-16 15:34:10 -04003483 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003484 reg = FDI_TX_CTL(pipe);
3485 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003486 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3487 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003488 I915_WRITE(reg, temp);
3489
3490 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003491 udelay(500);
3492
Sean Paulfa37d392012-03-02 12:53:39 -05003493 for (retry = 0; retry < 5; retry++) {
3494 reg = FDI_RX_IIR(pipe);
3495 temp = I915_READ(reg);
3496 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3497 if (temp & FDI_RX_SYMBOL_LOCK) {
3498 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3499 DRM_DEBUG_KMS("FDI train 2 done.\n");
3500 break;
3501 }
3502 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003503 }
Sean Paulfa37d392012-03-02 12:53:39 -05003504 if (retry < 5)
3505 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003506 }
3507 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003508 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003509
3510 DRM_DEBUG_KMS("FDI train done.\n");
3511}
3512
Jesse Barnes357555c2011-04-28 15:09:55 -07003513/* Manual link training for Ivy Bridge A0 parts */
3514static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3515{
3516 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003517 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes357555c2011-04-28 15:09:55 -07003518 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3519 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003520 i915_reg_t reg;
3521 u32 temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003522
3523 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3524 for train result */
3525 reg = FDI_RX_IMR(pipe);
3526 temp = I915_READ(reg);
3527 temp &= ~FDI_RX_SYMBOL_LOCK;
3528 temp &= ~FDI_RX_BIT_LOCK;
3529 I915_WRITE(reg, temp);
3530
3531 POSTING_READ(reg);
3532 udelay(150);
3533
Daniel Vetter01a415f2012-10-27 15:58:40 +02003534 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3535 I915_READ(FDI_RX_IIR(pipe)));
3536
Jesse Barnes139ccd32013-08-19 11:04:55 -07003537 /* Try each vswing and preemphasis setting twice before moving on */
3538 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3539 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003540 reg = FDI_TX_CTL(pipe);
3541 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003542 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3543 temp &= ~FDI_TX_ENABLE;
3544 I915_WRITE(reg, temp);
3545
3546 reg = FDI_RX_CTL(pipe);
3547 temp = I915_READ(reg);
3548 temp &= ~FDI_LINK_TRAIN_AUTO;
3549 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3550 temp &= ~FDI_RX_ENABLE;
3551 I915_WRITE(reg, temp);
3552
3553 /* enable CPU FDI TX and PCH FDI RX */
3554 reg = FDI_TX_CTL(pipe);
3555 temp = I915_READ(reg);
3556 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003557 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003558 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003559 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003560 temp |= snb_b_fdi_train_param[j/2];
3561 temp |= FDI_COMPOSITE_SYNC;
3562 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3563
3564 I915_WRITE(FDI_RX_MISC(pipe),
3565 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3566
3567 reg = FDI_RX_CTL(pipe);
3568 temp = I915_READ(reg);
3569 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3570 temp |= FDI_COMPOSITE_SYNC;
3571 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3572
3573 POSTING_READ(reg);
3574 udelay(1); /* should be 0.5us */
3575
3576 for (i = 0; i < 4; i++) {
3577 reg = FDI_RX_IIR(pipe);
3578 temp = I915_READ(reg);
3579 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3580
3581 if (temp & FDI_RX_BIT_LOCK ||
3582 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3583 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3584 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3585 i);
3586 break;
3587 }
3588 udelay(1); /* should be 0.5us */
3589 }
3590 if (i == 4) {
3591 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3592 continue;
3593 }
3594
3595 /* Train 2 */
3596 reg = FDI_TX_CTL(pipe);
3597 temp = I915_READ(reg);
3598 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3599 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3600 I915_WRITE(reg, temp);
3601
3602 reg = FDI_RX_CTL(pipe);
3603 temp = I915_READ(reg);
3604 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3605 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003606 I915_WRITE(reg, temp);
3607
3608 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003609 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003610
Jesse Barnes139ccd32013-08-19 11:04:55 -07003611 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);
Jesse Barnes357555c2011-04-28 15:09:55 -07003615
Jesse Barnes139ccd32013-08-19 11:04:55 -07003616 if (temp & FDI_RX_SYMBOL_LOCK ||
3617 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3618 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3619 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3620 i);
3621 goto train_done;
3622 }
3623 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003624 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003625 if (i == 4)
3626 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003627 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003628
Jesse Barnes139ccd32013-08-19 11:04:55 -07003629train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003630 DRM_DEBUG_KMS("FDI train done.\n");
3631}
3632
Daniel Vetter88cefb62012-08-12 19:27:14 +02003633static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003634{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003635 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003636 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003637 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003638 i915_reg_t reg;
3639 u32 temp;
Jesse Barnesc64e3112010-09-10 11:27:03 -07003640
Jesse Barnes0e23b992010-09-10 11:10:00 -07003641 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003642 reg = FDI_RX_CTL(pipe);
3643 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003644 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003645 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003646 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003647 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3648
3649 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003650 udelay(200);
3651
3652 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003653 temp = I915_READ(reg);
3654 I915_WRITE(reg, temp | FDI_PCDCLK);
3655
3656 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003657 udelay(200);
3658
Paulo Zanoni20749732012-11-23 15:30:38 -02003659 /* Enable CPU FDI TX PLL, always on for Ironlake */
3660 reg = FDI_TX_CTL(pipe);
3661 temp = I915_READ(reg);
3662 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3663 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003664
Paulo Zanoni20749732012-11-23 15:30:38 -02003665 POSTING_READ(reg);
3666 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003667 }
3668}
3669
Daniel Vetter88cefb62012-08-12 19:27:14 +02003670static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3671{
3672 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003673 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter88cefb62012-08-12 19:27:14 +02003674 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003675 i915_reg_t reg;
3676 u32 temp;
Daniel Vetter88cefb62012-08-12 19:27:14 +02003677
3678 /* Switch from PCDclk to Rawclk */
3679 reg = FDI_RX_CTL(pipe);
3680 temp = I915_READ(reg);
3681 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3682
3683 /* Disable CPU FDI TX PLL */
3684 reg = FDI_TX_CTL(pipe);
3685 temp = I915_READ(reg);
3686 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3687
3688 POSTING_READ(reg);
3689 udelay(100);
3690
3691 reg = FDI_RX_CTL(pipe);
3692 temp = I915_READ(reg);
3693 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3694
3695 /* Wait for the clocks to turn off. */
3696 POSTING_READ(reg);
3697 udelay(100);
3698}
3699
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003700static void ironlake_fdi_disable(struct drm_crtc *crtc)
3701{
3702 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003703 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003704 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3705 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003706 i915_reg_t reg;
3707 u32 temp;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003708
3709 /* disable CPU FDI tx and PCH FDI rx */
3710 reg = FDI_TX_CTL(pipe);
3711 temp = I915_READ(reg);
3712 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3713 POSTING_READ(reg);
3714
3715 reg = FDI_RX_CTL(pipe);
3716 temp = I915_READ(reg);
3717 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003718 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003719 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3720
3721 POSTING_READ(reg);
3722 udelay(100);
3723
3724 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003725 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003726 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003727
3728 /* still set train pattern 1 */
3729 reg = FDI_TX_CTL(pipe);
3730 temp = I915_READ(reg);
3731 temp &= ~FDI_LINK_TRAIN_NONE;
3732 temp |= FDI_LINK_TRAIN_PATTERN_1;
3733 I915_WRITE(reg, temp);
3734
3735 reg = FDI_RX_CTL(pipe);
3736 temp = I915_READ(reg);
3737 if (HAS_PCH_CPT(dev)) {
3738 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3739 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3740 } else {
3741 temp &= ~FDI_LINK_TRAIN_NONE;
3742 temp |= FDI_LINK_TRAIN_PATTERN_1;
3743 }
3744 /* BPC in FDI rx is consistent with that in PIPECONF */
3745 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003746 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003747 I915_WRITE(reg, temp);
3748
3749 POSTING_READ(reg);
3750 udelay(100);
3751}
3752
Chris Wilson5dce5b932014-01-20 10:17:36 +00003753bool intel_has_pending_fb_unpin(struct drm_device *dev)
3754{
3755 struct intel_crtc *crtc;
3756
3757 /* Note that we don't need to be called with mode_config.lock here
3758 * as our list of CRTC objects is static for the lifetime of the
3759 * device and so cannot disappear as we iterate. Similarly, we can
3760 * happily treat the predicates as racy, atomic checks as userspace
3761 * cannot claim and pin a new fb without at least acquring the
3762 * struct_mutex and so serialising with us.
3763 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003764 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003765 if (atomic_read(&crtc->unpin_work_count) == 0)
3766 continue;
3767
Daniel Vetter5a21b662016-05-24 17:13:53 +02003768 if (crtc->flip_work)
Chris Wilson5dce5b932014-01-20 10:17:36 +00003769 intel_wait_for_vblank(dev, crtc->pipe);
3770
3771 return true;
3772 }
3773
3774 return false;
3775}
3776
Daniel Vetter5a21b662016-05-24 17:13:53 +02003777static void page_flip_completed(struct intel_crtc *intel_crtc)
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003778{
3779 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003780 struct intel_flip_work *work = intel_crtc->flip_work;
3781
3782 intel_crtc->flip_work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003783
3784 if (work->event)
Gustavo Padovan560ce1d2016-04-14 10:48:15 -07003785 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003786
3787 drm_crtc_vblank_put(&intel_crtc->base);
3788
Daniel Vetter5a21b662016-05-24 17:13:53 +02003789 wake_up_all(&dev_priv->pending_flip_queue);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +02003790 queue_work(dev_priv->wq, &work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +02003791
3792 trace_i915_flip_complete(intel_crtc->plane,
3793 work->pending_flip_obj);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003794}
3795
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003796static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003797{
Chris Wilson0f911282012-04-17 10:05:38 +01003798 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003799 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003800 long ret;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003801
Daniel Vetter2c10d572012-12-20 21:24:07 +01003802 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003803
3804 ret = wait_event_interruptible_timeout(
3805 dev_priv->pending_flip_queue,
3806 !intel_crtc_has_pending_flip(crtc),
3807 60*HZ);
3808
3809 if (ret < 0)
3810 return ret;
3811
Daniel Vetter5a21b662016-05-24 17:13:53 +02003812 if (ret == 0) {
3813 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3814 struct intel_flip_work *work;
3815
3816 spin_lock_irq(&dev->event_lock);
3817 work = intel_crtc->flip_work;
3818 if (work && !is_mmio_work(work)) {
3819 WARN_ONCE(1, "Removing stuck page flip\n");
3820 page_flip_completed(intel_crtc);
3821 }
3822 spin_unlock_irq(&dev->event_lock);
3823 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003824
Maarten Lankhorst5008e872015-08-18 13:40:05 +02003825 return 0;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003826}
3827
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003828static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
3829{
3830 u32 temp;
3831
3832 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3833
3834 mutex_lock(&dev_priv->sb_lock);
3835
3836 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3837 temp |= SBI_SSCCTL_DISABLE;
3838 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3839
3840 mutex_unlock(&dev_priv->sb_lock);
3841}
3842
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003843/* Program iCLKIP clock to the desired frequency */
3844static void lpt_program_iclkip(struct drm_crtc *crtc)
3845{
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003846 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003847 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003848 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3849 u32 temp;
3850
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003851 lpt_disable_iclkip(dev_priv);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003852
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003853 /* The iCLK virtual clock root frequency is in MHz,
3854 * but the adjusted_mode->crtc_clock in in KHz. To get the
3855 * divisors, it is necessary to divide one by another, so we
3856 * convert the virtual clock precision to KHz here for higher
3857 * precision.
3858 */
3859 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003860 u32 iclk_virtual_root_freq = 172800 * 1000;
3861 u32 iclk_pi_range = 64;
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003862 u32 desired_divisor;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003863
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003864 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3865 clock << auxdiv);
3866 divsel = (desired_divisor / iclk_pi_range) - 2;
3867 phaseinc = desired_divisor % iclk_pi_range;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003868
Ville Syrjälä64b46a02016-02-17 21:41:11 +02003869 /*
3870 * Near 20MHz is a corner case which is
3871 * out of range for the 7-bit divisor
3872 */
3873 if (divsel <= 0x7f)
3874 break;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003875 }
3876
3877 /* This should not happen with any sane values */
3878 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3879 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3880 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3881 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3882
3883 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 +03003884 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003885 auxdiv,
3886 divsel,
3887 phasedir,
3888 phaseinc);
3889
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003890 mutex_lock(&dev_priv->sb_lock);
3891
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003892 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003893 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003894 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3895 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3896 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3897 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3898 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3899 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003900 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003901
3902 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003903 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003904 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3905 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003906 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003907
3908 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003909 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003910 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003911 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003912
Ville Syrjälä060f02d2015-12-04 22:21:34 +02003913 mutex_unlock(&dev_priv->sb_lock);
3914
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003915 /* Wait for initialization time */
3916 udelay(24);
3917
3918 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3919}
3920
Ville Syrjälä8802e5b2016-02-17 21:41:12 +02003921int lpt_get_iclkip(struct drm_i915_private *dev_priv)
3922{
3923 u32 divsel, phaseinc, auxdiv;
3924 u32 iclk_virtual_root_freq = 172800 * 1000;
3925 u32 iclk_pi_range = 64;
3926 u32 desired_divisor;
3927 u32 temp;
3928
3929 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
3930 return 0;
3931
3932 mutex_lock(&dev_priv->sb_lock);
3933
3934 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3935 if (temp & SBI_SSCCTL_DISABLE) {
3936 mutex_unlock(&dev_priv->sb_lock);
3937 return 0;
3938 }
3939
3940 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3941 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
3942 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
3943 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
3944 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
3945
3946 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3947 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
3948 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
3949
3950 mutex_unlock(&dev_priv->sb_lock);
3951
3952 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
3953
3954 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
3955 desired_divisor << auxdiv);
3956}
3957
Daniel Vetter275f01b22013-05-03 11:49:47 +02003958static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3959 enum pipe pch_transcoder)
3960{
3961 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003962 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003963 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003964
3965 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3966 I915_READ(HTOTAL(cpu_transcoder)));
3967 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3968 I915_READ(HBLANK(cpu_transcoder)));
3969 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3970 I915_READ(HSYNC(cpu_transcoder)));
3971
3972 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3973 I915_READ(VTOTAL(cpu_transcoder)));
3974 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3975 I915_READ(VBLANK(cpu_transcoder)));
3976 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3977 I915_READ(VSYNC(cpu_transcoder)));
3978 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3979 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3980}
3981
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003982static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003983{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003984 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003985 uint32_t temp;
3986
3987 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003988 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003989 return;
3990
3991 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3992 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3993
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003994 temp &= ~FDI_BC_BIFURCATION_SELECT;
3995 if (enable)
3996 temp |= FDI_BC_BIFURCATION_SELECT;
3997
3998 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003999 I915_WRITE(SOUTH_CHICKEN1, temp);
4000 POSTING_READ(SOUTH_CHICKEN1);
4001}
4002
4003static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4004{
4005 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004006
4007 switch (intel_crtc->pipe) {
4008 case PIPE_A:
4009 break;
4010 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004011 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004012 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004013 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004014 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004015
4016 break;
4017 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004018 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004019
4020 break;
4021 default:
4022 BUG();
4023 }
4024}
4025
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004026/* Return which DP Port should be selected for Transcoder DP control */
4027static enum port
4028intel_trans_dp_port_sel(struct drm_crtc *crtc)
4029{
4030 struct drm_device *dev = crtc->dev;
4031 struct intel_encoder *encoder;
4032
4033 for_each_encoder_on_crtc(dev, crtc, encoder) {
Ville Syrjäläcca05022016-06-22 21:57:06 +03004034 if (encoder->type == INTEL_OUTPUT_DP ||
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004035 encoder->type == INTEL_OUTPUT_EDP)
4036 return enc_to_dig_port(&encoder->base)->port;
4037 }
4038
4039 return -1;
4040}
4041
Jesse Barnesf67a5592011-01-05 10:31:48 -08004042/*
4043 * Enable PCH resources required for PCH ports:
4044 * - PCH PLLs
4045 * - FDI training & RX/TX
4046 * - update transcoder timings
4047 * - DP transcoding bits
4048 * - transcoder
4049 */
4050static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004051{
4052 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004053 struct drm_i915_private *dev_priv = to_i915(dev);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004054 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4055 int pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004056 u32 temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004057
Daniel Vetterab9412b2013-05-03 11:49:46 +02004058 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004059
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004060 if (IS_IVYBRIDGE(dev))
4061 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4062
Daniel Vettercd986ab2012-10-26 10:58:12 +02004063 /* Write the TU size bits before fdi link training, so that error
4064 * detection works. */
4065 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4066 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4067
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004068 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004069 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004070
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004071 /* We need to program the right clock selection before writing the pixel
4072 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004073 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004074 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004075
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004076 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004077 temp |= TRANS_DPLL_ENABLE(pipe);
4078 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004079 if (intel_crtc->config->shared_dpll ==
4080 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004081 temp |= sel;
4082 else
4083 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004084 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004085 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004086
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004087 /* XXX: pch pll's can be enabled any time before we enable the PCH
4088 * transcoder, and we actually should do this to not upset any PCH
4089 * transcoder that already use the clock when we share it.
4090 *
4091 * Note that enable_shared_dpll tries to do the right thing, but
4092 * get_shared_dpll unconditionally resets the pll - we need that to have
4093 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004094 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004095
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004096 /* set transcoder timing, panel must allow it */
4097 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004098 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004099
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004100 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004101
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004102 /* For PCH DP, enable TRANS_DP_CTL */
Ville Syrjälä37a56502016-06-22 21:57:04 +03004103 if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004104 const struct drm_display_mode *adjusted_mode =
4105 &intel_crtc->config->base.adjusted_mode;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004106 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004107 i915_reg_t reg = TRANS_DP_CTL(pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01004108 temp = I915_READ(reg);
4109 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004110 TRANS_DP_SYNC_MASK |
4111 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004112 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004113 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004114
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004115 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004116 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Ville Syrjälä9c4edae2015-10-29 21:25:51 +02004117 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004118 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004119
4120 switch (intel_trans_dp_port_sel(crtc)) {
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004121 case PORT_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004122 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004123 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004124 case PORT_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004125 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004126 break;
Ville Syrjäläc48b5302015-11-04 23:19:56 +02004127 case PORT_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004128 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004129 break;
4130 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004131 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004132 }
4133
Chris Wilson5eddb702010-09-11 13:48:45 +01004134 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004135 }
4136
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004137 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004138}
4139
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004140static void lpt_pch_enable(struct drm_crtc *crtc)
4141{
4142 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004143 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004144 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004145 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004146
Daniel Vetterab9412b2013-05-03 11:49:46 +02004147 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004148
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004149 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004150
Paulo Zanoni0540e482012-10-31 18:12:40 -02004151 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004152 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004153
Paulo Zanoni937bb612012-10-31 18:12:47 -02004154 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004155}
4156
Daniel Vettera1520312013-05-03 11:49:50 +02004157static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004158{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004159 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004160 i915_reg_t dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004161 u32 temp;
4162
4163 temp = I915_READ(dslreg);
4164 udelay(500);
4165 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004166 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004167 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004168 }
4169}
4170
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004171static int
4172skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4173 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4174 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004175{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004176 struct intel_crtc_scaler_state *scaler_state =
4177 &crtc_state->scaler_state;
4178 struct intel_crtc *intel_crtc =
4179 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004180 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004181
4182 need_scaling = intel_rotation_90_or_270(rotation) ?
4183 (src_h != dst_w || src_w != dst_h):
4184 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004185
4186 /*
4187 * if plane is being disabled or scaler is no more required or force detach
4188 * - free scaler binded to this plane/crtc
4189 * - in order to do this, update crtc->scaler_usage
4190 *
4191 * Here scaler state in crtc_state is set free so that
4192 * scaler can be assigned to other user. Actual register
4193 * update to free the scaler is done in plane/panel-fit programming.
4194 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4195 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004196 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004197 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004198 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004199 scaler_state->scalers[*scaler_id].in_use = 0;
4200
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004201 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4202 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4203 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004204 scaler_state->scaler_users);
4205 *scaler_id = -1;
4206 }
4207 return 0;
4208 }
4209
4210 /* range checks */
4211 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4212 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4213
4214 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4215 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004216 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004217 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004218 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004219 return -EINVAL;
4220 }
4221
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004222 /* mark this plane as a scaler user in crtc_state */
4223 scaler_state->scaler_users |= (1 << scaler_user);
4224 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4225 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4226 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4227 scaler_state->scaler_users);
4228
4229 return 0;
4230}
4231
4232/**
4233 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4234 *
4235 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004236 *
4237 * Return
4238 * 0 - scaler_usage updated successfully
4239 * error - requested scaling cannot be supported or other error condition
4240 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004241int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004242{
4243 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03004244 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004245
Ville Syrjälä78108b72016-05-27 20:59:19 +03004246 DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
4247 intel_crtc->base.base.id, intel_crtc->base.name,
4248 intel_crtc->pipe, SKL_CRTC_INDEX);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004249
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004250 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Ville Syrjäläfa5a7972015-10-15 17:01:58 +03004251 &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004252 state->pipe_src_w, state->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +03004253 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004254}
4255
4256/**
4257 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4258 *
4259 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004260 * @plane_state: atomic plane state to update
4261 *
4262 * Return
4263 * 0 - scaler_usage updated successfully
4264 * error - requested scaling cannot be supported or other error condition
4265 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004266static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4267 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004268{
4269
4270 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004271 struct intel_plane *intel_plane =
4272 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004273 struct drm_framebuffer *fb = plane_state->base.fb;
4274 int ret;
4275
4276 bool force_detach = !fb || !plane_state->visible;
4277
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004278 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
4279 intel_plane->base.base.id, intel_plane->base.name,
4280 intel_crtc->pipe, drm_plane_index(&intel_plane->base));
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004281
4282 ret = skl_update_scaler(crtc_state, force_detach,
4283 drm_plane_index(&intel_plane->base),
4284 &plane_state->scaler_id,
4285 plane_state->base.rotation,
4286 drm_rect_width(&plane_state->src) >> 16,
4287 drm_rect_height(&plane_state->src) >> 16,
4288 drm_rect_width(&plane_state->dst),
4289 drm_rect_height(&plane_state->dst));
4290
4291 if (ret || plane_state->scaler_id < 0)
4292 return ret;
4293
Chandra Kondurua1b22782015-04-07 15:28:45 -07004294 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004295 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004296 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4297 intel_plane->base.base.id,
4298 intel_plane->base.name);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004299 return -EINVAL;
4300 }
4301
4302 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004303 switch (fb->pixel_format) {
4304 case DRM_FORMAT_RGB565:
4305 case DRM_FORMAT_XBGR8888:
4306 case DRM_FORMAT_XRGB8888:
4307 case DRM_FORMAT_ABGR8888:
4308 case DRM_FORMAT_ARGB8888:
4309 case DRM_FORMAT_XRGB2101010:
4310 case DRM_FORMAT_XBGR2101010:
4311 case DRM_FORMAT_YUYV:
4312 case DRM_FORMAT_YVYU:
4313 case DRM_FORMAT_UYVY:
4314 case DRM_FORMAT_VYUY:
4315 break;
4316 default:
Ville Syrjälä72660ce2016-05-27 20:59:20 +03004317 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4318 intel_plane->base.base.id, intel_plane->base.name,
4319 fb->base.id, fb->pixel_format);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004320 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004321 }
4322
Chandra Kondurua1b22782015-04-07 15:28:45 -07004323 return 0;
4324}
4325
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004326static void skylake_scaler_disable(struct intel_crtc *crtc)
4327{
4328 int i;
4329
4330 for (i = 0; i < crtc->num_scalers; i++)
4331 skl_detach_scaler(crtc, i);
4332}
4333
4334static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004335{
4336 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004337 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004338 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004339 struct intel_crtc_scaler_state *scaler_state =
4340 &crtc->config->scaler_state;
4341
4342 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4343
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004344 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004345 int id;
4346
4347 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4348 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4349 return;
4350 }
4351
4352 id = scaler_state->scaler_id;
4353 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4354 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4355 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4356 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4357
4358 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004359 }
4360}
4361
Jesse Barnesb074cec2013-04-25 12:55:02 -07004362static void ironlake_pfit_enable(struct intel_crtc *crtc)
4363{
4364 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004365 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb074cec2013-04-25 12:55:02 -07004366 int pipe = crtc->pipe;
4367
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004368 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004369 /* Force use of hard-coded filter coefficients
4370 * as some pre-programmed values are broken,
4371 * e.g. x201.
4372 */
4373 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4374 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4375 PF_PIPE_SEL_IVB(pipe));
4376 else
4377 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004378 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4379 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004380 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004381}
4382
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004383void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004384{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004385 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004386 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004387
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004388 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004389 return;
4390
Maarten Lankhorst307e4492016-03-23 14:33:28 +01004391 /*
4392 * We can only enable IPS after we enable a plane and wait for a vblank
4393 * This function is called from post_plane_update, which is run after
4394 * a vblank wait.
4395 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004396
Paulo Zanonid77e4532013-09-24 13:52:55 -03004397 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004398 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004399 mutex_lock(&dev_priv->rps.hw_lock);
4400 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4401 mutex_unlock(&dev_priv->rps.hw_lock);
4402 /* Quoting Art Runyan: "its not safe to expect any particular
4403 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004404 * mailbox." Moreover, the mailbox may return a bogus state,
4405 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004406 */
4407 } else {
4408 I915_WRITE(IPS_CTL, IPS_ENABLE);
4409 /* The bit only becomes 1 in the next vblank, so this wait here
4410 * is essentially intel_wait_for_vblank. If we don't have this
4411 * and don't wait for vblanks until the end of crtc_enable, then
4412 * the HW state readout code will complain that the expected
4413 * IPS_CTL value is not the one we read. */
Chris Wilson2ec9ba32016-06-30 15:33:01 +01004414 if (intel_wait_for_register(dev_priv,
4415 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4416 50))
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004417 DRM_ERROR("Timed out waiting for IPS enable\n");
4418 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004419}
4420
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004421void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004422{
4423 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004424 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonid77e4532013-09-24 13:52:55 -03004425
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004426 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004427 return;
4428
4429 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004430 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004431 mutex_lock(&dev_priv->rps.hw_lock);
4432 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4433 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004434 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
Chris Wilsonb85c1ec2016-06-30 15:33:02 +01004435 if (intel_wait_for_register(dev_priv,
4436 IPS_CTL, IPS_ENABLE, 0,
4437 42))
Ben Widawsky23d0b132014-04-10 14:32:41 -07004438 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004439 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004440 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004441 POSTING_READ(IPS_CTL);
4442 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004443
4444 /* We need to wait for a vblank before we can disable the plane. */
4445 intel_wait_for_vblank(dev, crtc->pipe);
4446}
4447
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004448static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004449{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004450 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004451 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004452 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004453
4454 mutex_lock(&dev->struct_mutex);
4455 dev_priv->mm.interruptible = false;
4456 (void) intel_overlay_switch_off(intel_crtc->overlay);
4457 dev_priv->mm.interruptible = true;
4458 mutex_unlock(&dev->struct_mutex);
4459 }
4460
4461 /* Let userspace switch the overlay on again. In most cases userspace
4462 * has to recompute where to put it anyway.
4463 */
4464}
4465
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004466/**
4467 * intel_post_enable_primary - Perform operations after enabling primary plane
4468 * @crtc: the CRTC whose primary plane was just enabled
4469 *
4470 * Performs potentially sleeping operations that must be done after the primary
4471 * plane is enabled, such as updating FBC and IPS. Note that this may be
4472 * called due to an explicit primary plane update, or due to an implicit
4473 * re-enable that is caused when a sprite plane is updated to no longer
4474 * completely hide the primary plane.
4475 */
4476static void
4477intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004478{
4479 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004480 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004481 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4482 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004483
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004484 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004485 * FIXME IPS should be fine as long as one plane is
4486 * enabled, but in practice it seems to have problems
4487 * when going from primary only to sprite only and vice
4488 * versa.
4489 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004490 hsw_enable_ips(intel_crtc);
4491
Daniel Vetterf99d7062014-06-19 16:01:59 +02004492 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004493 * Gen2 reports pipe underruns whenever all planes are disabled.
4494 * So don't enable underrun reporting before at least some planes
4495 * are enabled.
4496 * FIXME: Need to fix the logic to work when we turn off all planes
4497 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004498 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004499 if (IS_GEN2(dev))
4500 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4501
Ville Syrjäläaca7b682015-10-30 19:22:21 +02004502 /* Underruns don't always raise interrupts, so check manually. */
4503 intel_check_cpu_fifo_underruns(dev_priv);
4504 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004505}
4506
Ville Syrjälä2622a082016-03-09 19:07:26 +02004507/* FIXME move all this to pre_plane_update() with proper state tracking */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004508static void
4509intel_pre_disable_primary(struct drm_crtc *crtc)
4510{
4511 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004512 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4514 int pipe = intel_crtc->pipe;
4515
4516 /*
4517 * Gen2 reports pipe underruns whenever all planes are disabled.
4518 * So diasble underrun reporting before all the planes get disabled.
4519 * FIXME: Need to fix the logic to work when we turn off all planes
4520 * but leave the pipe running.
4521 */
4522 if (IS_GEN2(dev))
4523 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4524
4525 /*
Ville Syrjälä2622a082016-03-09 19:07:26 +02004526 * FIXME IPS should be fine as long as one plane is
4527 * enabled, but in practice it seems to have problems
4528 * when going from primary only to sprite only and vice
4529 * versa.
4530 */
4531 hsw_disable_ips(intel_crtc);
4532}
4533
4534/* FIXME get rid of this and use pre_plane_update */
4535static void
4536intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
4537{
4538 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004539 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004540 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4541 int pipe = intel_crtc->pipe;
4542
4543 intel_pre_disable_primary(crtc);
4544
4545 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004546 * Vblank time updates from the shadow to live plane control register
4547 * are blocked if the memory self-refresh mode is active at that
4548 * moment. So to make sure the plane gets truly disabled, disable
4549 * first the self-refresh mode. The self-refresh enable bit in turn
4550 * will be checked/applied by the HW only at the next frame start
4551 * event which is after the vblank start event, so we need to have a
4552 * wait-for-vblank between disabling the plane and the pipe.
4553 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004554 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004555 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004556 dev_priv->wm.vlv.cxsr = false;
4557 intel_wait_for_vblank(dev, pipe);
4558 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004559}
4560
Daniel Vetter5a21b662016-05-24 17:13:53 +02004561static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
4562{
4563 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
4564 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4565 struct intel_crtc_state *pipe_config =
4566 to_intel_crtc_state(crtc->base.state);
4567 struct drm_device *dev = crtc->base.dev;
4568 struct drm_plane *primary = crtc->base.primary;
4569 struct drm_plane_state *old_pri_state =
4570 drm_atomic_get_existing_plane_state(old_state, primary);
4571
4572 intel_frontbuffer_flip(dev, pipe_config->fb_bits);
4573
4574 crtc->wm.cxsr_allowed = true;
4575
4576 if (pipe_config->update_wm_post && pipe_config->base.active)
4577 intel_update_watermarks(&crtc->base);
4578
4579 if (old_pri_state) {
4580 struct intel_plane_state *primary_state =
4581 to_intel_plane_state(primary->state);
4582 struct intel_plane_state *old_primary_state =
4583 to_intel_plane_state(old_pri_state);
4584
4585 intel_fbc_post_update(crtc);
4586
4587 if (primary_state->visible &&
4588 (needs_modeset(&pipe_config->base) ||
4589 !old_primary_state->visible))
4590 intel_post_enable_primary(&crtc->base);
4591 }
4592}
4593
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004594static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004595{
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004596 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004597 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004598 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +01004599 struct intel_crtc_state *pipe_config =
4600 to_intel_crtc_state(crtc->base.state);
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004601 struct drm_atomic_state *old_state = old_crtc_state->base.state;
4602 struct drm_plane *primary = crtc->base.primary;
4603 struct drm_plane_state *old_pri_state =
4604 drm_atomic_get_existing_plane_state(old_state, primary);
4605 bool modeset = needs_modeset(&pipe_config->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004606
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004607 if (old_pri_state) {
4608 struct intel_plane_state *primary_state =
4609 to_intel_plane_state(primary->state);
4610 struct intel_plane_state *old_primary_state =
4611 to_intel_plane_state(old_pri_state);
4612
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +02004613 intel_fbc_pre_update(crtc, pipe_config, primary_state);
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +01004614
Maarten Lankhorst5c74cd72016-02-03 16:53:24 +01004615 if (old_primary_state->visible &&
4616 (modeset || !primary_state->visible))
4617 intel_pre_disable_primary(&crtc->base);
4618 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004619
David Weinehalla4015f92016-05-19 15:50:36 +03004620 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
Ville Syrjälä852eb002015-06-24 22:00:07 +03004621 crtc->wm.cxsr_allowed = false;
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004622
Ville Syrjälä2622a082016-03-09 19:07:26 +02004623 /*
4624 * Vblank time updates from the shadow to live plane control register
4625 * are blocked if the memory self-refresh mode is active at that
4626 * moment. So to make sure the plane gets truly disabled, disable
4627 * first the self-refresh mode. The self-refresh enable bit in turn
4628 * will be checked/applied by the HW only at the next frame start
4629 * event which is after the vblank start event, so we need to have a
4630 * wait-for-vblank between disabling the plane and the pipe.
4631 */
4632 if (old_crtc_state->base.active) {
Maarten Lankhorst2dfd1782016-02-03 16:53:25 +01004633 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä2622a082016-03-09 19:07:26 +02004634 dev_priv->wm.vlv.cxsr = false;
4635 intel_wait_for_vblank(dev, crtc->pipe);
4636 }
Ville Syrjälä852eb002015-06-24 22:00:07 +03004637 }
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004638
Matt Ropered4a6a72016-02-23 17:20:13 -08004639 /*
4640 * IVB workaround: must disable low power watermarks for at least
4641 * one frame before enabling scaling. LP watermarks can be re-enabled
4642 * when scaling is disabled.
4643 *
4644 * WaCxSRDisabledForSpriteScaling:ivb
4645 */
4646 if (pipe_config->disable_lp_wm) {
4647 ilk_disable_lp_wm(dev);
4648 intel_wait_for_vblank(dev, crtc->pipe);
4649 }
4650
4651 /*
4652 * If we're doing a modeset, we're done. No need to do any pre-vblank
4653 * watermark programming here.
4654 */
4655 if (needs_modeset(&pipe_config->base))
4656 return;
4657
4658 /*
4659 * For platforms that support atomic watermarks, program the
4660 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
4661 * will be the intermediate values that are safe for both pre- and
4662 * post- vblank; when vblank happens, the 'active' values will be set
4663 * to the final 'target' values and we'll do this again to get the
4664 * optimal watermarks. For gen9+ platforms, the values we program here
4665 * will be the final target values which will get automatically latched
4666 * at vblank time; no further programming will be necessary.
4667 *
4668 * If a platform hasn't been transitioned to atomic watermarks yet,
4669 * we'll continue to update watermarks the old way, if flags tell
4670 * us to.
4671 */
4672 if (dev_priv->display.initial_watermarks != NULL)
4673 dev_priv->display.initial_watermarks(pipe_config);
Ville Syrjäläcaed3612016-03-09 19:07:25 +02004674 else if (pipe_config->update_wm_pre)
Maarten Lankhorst92826fc2015-12-03 13:49:13 +01004675 intel_update_watermarks(&crtc->base);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004676}
4677
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004678static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004679{
4680 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004682 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004683 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004684
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004685 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004686
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004687 drm_for_each_plane_mask(p, dev, plane_mask)
4688 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004689
Daniel Vetterf99d7062014-06-19 16:01:59 +02004690 /*
4691 * FIXME: Once we grow proper nuclear flip support out of this we need
4692 * to compute the mask of flip planes precisely. For the time being
4693 * consider this a flip to a NULL plane.
4694 */
4695 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004696}
4697
Jesse Barnesf67a5592011-01-05 10:31:48 -08004698static void ironlake_crtc_enable(struct drm_crtc *crtc)
4699{
4700 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004701 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004702 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004703 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004704 int pipe = intel_crtc->pipe;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004705 struct intel_crtc_state *pipe_config =
4706 to_intel_crtc_state(crtc->state);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004707
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004708 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004709 return;
4710
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004711 /*
4712 * Sometimes spurious CPU pipe underruns happen during FDI
4713 * training, at least with VGA+HDMI cloning. Suppress them.
4714 *
4715 * On ILK we get an occasional spurious CPU pipe underruns
4716 * between eDP port A enable and vdd enable. Also PCH port
4717 * enable seems to result in the occasional CPU pipe underrun.
4718 *
4719 * Spurious PCH underruns also occur during PCH enabling.
4720 */
4721 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
4722 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004723 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004724 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4725
4726 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004727 intel_prepare_shared_dpll(intel_crtc);
4728
Ville Syrjälä37a56502016-06-22 21:57:04 +03004729 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304730 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004731
4732 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02004733 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004734
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004735 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004736 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004737 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004738 }
4739
4740 ironlake_set_pipeconf(crtc);
4741
Jesse Barnesf67a5592011-01-05 10:31:48 -08004742 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004743
Daniel Vetterf6736a12013-06-05 13:34:30 +02004744 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004745 if (encoder->pre_enable)
4746 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004747
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004748 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004749 /* Note: FDI PLL enabling _must_ be done before we enable the
4750 * cpu pipes, hence this is separate from all the other fdi/pch
4751 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004752 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004753 } else {
4754 assert_fdi_tx_disabled(dev_priv, pipe);
4755 assert_fdi_rx_disabled(dev_priv, pipe);
4756 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004757
Jesse Barnesb074cec2013-04-25 12:55:02 -07004758 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004759
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004760 /*
4761 * On ILK+ LUT must be loaded before the pipe is running but with
4762 * clocks enabled
4763 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004764 intel_color_load_luts(&pipe_config->base);
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004765
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004766 if (dev_priv->display.initial_watermarks != NULL)
4767 dev_priv->display.initial_watermarks(intel_crtc->config);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004768 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004769
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004770 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004771 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004772
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004773 assert_vblank_disabled(crtc);
4774 drm_crtc_vblank_on(crtc);
4775
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004776 for_each_encoder_on_crtc(dev, crtc, encoder)
4777 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004778
4779 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004780 cpt_verify_modeset(dev, intel_crtc->pipe);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004781
4782 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
4783 if (intel_crtc->config->has_pch_encoder)
4784 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004785 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004786 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004787}
4788
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004789/* IPS only exists on ULT machines and is tied to pipe A. */
4790static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4791{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004792 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004793}
4794
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004795static void haswell_crtc_enable(struct drm_crtc *crtc)
4796{
4797 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004798 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004799 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4800 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004801 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
Jani Nikula4d1de972016-03-18 17:05:42 +02004802 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004803 struct intel_crtc_state *pipe_config =
4804 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004805
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004806 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004807 return;
4808
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004809 if (intel_crtc->config->has_pch_encoder)
4810 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4811 false);
4812
Imre Deak95a7a2a2016-06-13 16:44:35 +03004813 for_each_encoder_on_crtc(dev, crtc, encoder)
4814 if (encoder->pre_pll_enable)
4815 encoder->pre_pll_enable(encoder);
4816
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02004817 if (intel_crtc->config->shared_dpll)
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004818 intel_enable_shared_dpll(intel_crtc);
4819
Ville Syrjälä37a56502016-06-22 21:57:04 +03004820 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304821 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004822
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004823 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02004824 intel_set_pipe_timings(intel_crtc);
4825
Jani Nikulabc58be62016-03-18 17:05:39 +02004826 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004827
Jani Nikula4d1de972016-03-18 17:05:42 +02004828 if (cpu_transcoder != TRANSCODER_EDP &&
4829 !transcoder_is_dsi(cpu_transcoder)) {
4830 I915_WRITE(PIPE_MULT(cpu_transcoder),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004831 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004832 }
4833
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004834 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004835 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004836 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004837 }
4838
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004839 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02004840 haswell_set_pipeconf(crtc);
4841
Jani Nikula391bf042016-03-18 17:05:40 +02004842 haswell_set_pipemisc(crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +02004843
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004844 intel_color_set_csc(&pipe_config->base);
Daniel Vetter229fca92014-04-24 23:55:09 +02004845
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004846 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004847
Daniel Vetter6b698512015-11-28 11:05:39 +01004848 if (intel_crtc->config->has_pch_encoder)
4849 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4850 else
4851 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4852
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304853 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004854 if (encoder->pre_enable)
4855 encoder->pre_enable(encoder);
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304856 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004857
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004858 if (intel_crtc->config->has_pch_encoder)
Imre Deak4fe94672014-06-25 22:01:49 +03004859 dev_priv->display.fdi_link_train(crtc);
Imre Deak4fe94672014-06-25 22:01:49 +03004860
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004861 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304862 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004863
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004864 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004865 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004866 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004867 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004868
4869 /*
4870 * On ILK+ LUT must be loaded before the pipe is running but with
4871 * clocks enabled
4872 */
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02004873 intel_color_load_luts(&pipe_config->base);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004874
Paulo Zanoni1f544382012-10-24 11:32:00 -02004875 intel_ddi_set_pipe_settings(crtc);
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004876 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05304877 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004878
Imre Deak1d5bf5d2016-02-29 22:10:33 +02004879 if (dev_priv->display.initial_watermarks != NULL)
4880 dev_priv->display.initial_watermarks(pipe_config);
4881 else
4882 intel_update_watermarks(crtc);
Jani Nikula4d1de972016-03-18 17:05:42 +02004883
4884 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03004885 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02004886 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004887
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004888 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004889 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004890
Jani Nikulaa65347b2015-11-27 12:21:46 +02004891 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004892 intel_ddi_set_vc_payload_alloc(crtc, true);
4893
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004894 assert_vblank_disabled(crtc);
4895 drm_crtc_vblank_on(crtc);
4896
Jani Nikula8807e552013-08-30 19:40:32 +03004897 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004898 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004899 intel_opregion_notify_encoder(encoder, true);
4900 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004901
Daniel Vetter6b698512015-11-28 11:05:39 +01004902 if (intel_crtc->config->has_pch_encoder) {
4903 intel_wait_for_vblank(dev, pipe);
4904 intel_wait_for_vblank(dev, pipe);
4905 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004906 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4907 true);
Daniel Vetter6b698512015-11-28 11:05:39 +01004908 }
Ville Syrjäläd2d65402015-10-29 21:25:53 +02004909
Paulo Zanonie4916942013-09-20 16:21:19 -03004910 /* If we change the relative order between pipe/planes enabling, we need
4911 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004912 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4913 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4914 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4915 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4916 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004917}
4918
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004919static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004920{
4921 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004922 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004923 int pipe = crtc->pipe;
4924
4925 /* To avoid upsetting the power well on haswell only disable the pfit if
4926 * it's in use. The hw state code will make sure we get this right. */
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004927 if (force || crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004928 I915_WRITE(PF_CTL(pipe), 0);
4929 I915_WRITE(PF_WIN_POS(pipe), 0);
4930 I915_WRITE(PF_WIN_SZ(pipe), 0);
4931 }
4932}
4933
Jesse Barnes6be4a602010-09-10 10:26:01 -07004934static void ironlake_crtc_disable(struct drm_crtc *crtc)
4935{
4936 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004937 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004938 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004939 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004940 int pipe = intel_crtc->pipe;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004941
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004942 /*
4943 * Sometimes spurious CPU pipe underruns happen when the
4944 * pipe is already disabled, but FDI RX/TX is still enabled.
4945 * Happens at least with VGA+HDMI cloning. Suppress them.
4946 */
4947 if (intel_crtc->config->has_pch_encoder) {
4948 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004949 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004950 }
Ville Syrjälä37ca8d42015-10-30 19:20:27 +02004951
Daniel Vetterea9d7582012-07-10 10:42:52 +02004952 for_each_encoder_on_crtc(dev, crtc, encoder)
4953 encoder->disable(encoder);
4954
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004955 drm_crtc_vblank_off(crtc);
4956 assert_vblank_disabled(crtc);
4957
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004958 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004959
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02004960 ironlake_pfit_disable(intel_crtc, false);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004961
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004962 if (intel_crtc->config->has_pch_encoder)
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004963 ironlake_fdi_disable(crtc);
4964
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004965 for_each_encoder_on_crtc(dev, crtc, encoder)
4966 if (encoder->post_disable)
4967 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004968
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004969 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004970 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004971
Daniel Vetterd925c592013-06-05 13:34:04 +02004972 if (HAS_PCH_CPT(dev)) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004973 i915_reg_t reg;
4974 u32 temp;
4975
Daniel Vetterd925c592013-06-05 13:34:04 +02004976 /* disable TRANS_DP_CTL */
4977 reg = TRANS_DP_CTL(pipe);
4978 temp = I915_READ(reg);
4979 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4980 TRANS_DP_PORT_SEL_MASK);
4981 temp |= TRANS_DP_PORT_SEL_NONE;
4982 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004983
Daniel Vetterd925c592013-06-05 13:34:04 +02004984 /* disable DPLL_SEL */
4985 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004986 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004987 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004988 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004989
Daniel Vetterd925c592013-06-05 13:34:04 +02004990 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004991 }
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004992
Ville Syrjäläb2c05932016-04-01 21:53:17 +03004993 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02004994 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004995}
4996
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004997static void haswell_crtc_disable(struct drm_crtc *crtc)
4998{
4999 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005000 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005001 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5002 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005003 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005004
Ville Syrjäläd2d65402015-10-29 21:25:53 +02005005 if (intel_crtc->config->has_pch_encoder)
5006 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5007 false);
5008
Jani Nikula8807e552013-08-30 19:40:32 +03005009 for_each_encoder_on_crtc(dev, crtc, encoder) {
5010 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005011 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005012 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005013
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005014 drm_crtc_vblank_off(crtc);
5015 assert_vblank_disabled(crtc);
5016
Jani Nikula4d1de972016-03-18 17:05:42 +02005017 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005018 if (!transcoder_is_dsi(cpu_transcoder))
Jani Nikula4d1de972016-03-18 17:05:42 +02005019 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005020
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005021 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005022 intel_ddi_set_vc_payload_alloc(crtc, false);
5023
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005024 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305025 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005026
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005027 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005028 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005029 else
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +02005030 ironlake_pfit_disable(intel_crtc, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005031
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03005032 if (!transcoder_is_dsi(cpu_transcoder))
Shashank Sharma7d4aefd2015-10-01 22:23:49 +05305033 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005034
Imre Deak97b040a2014-06-25 22:01:50 +03005035 for_each_encoder_on_crtc(dev, crtc, encoder)
5036 if (encoder->post_disable)
5037 encoder->post_disable(encoder);
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005038
Ville Syrjälä92966a32015-12-08 16:05:48 +02005039 if (intel_crtc->config->has_pch_encoder) {
5040 lpt_disable_pch_transcoder(dev_priv);
Ville Syrjälä503a74e2015-12-04 22:22:14 +02005041 lpt_disable_iclkip(dev_priv);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005042 intel_ddi_fdi_disable(crtc);
5043
Ville Syrjälä81b088c2015-10-30 19:21:31 +02005044 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5045 true);
Ville Syrjälä92966a32015-12-08 16:05:48 +02005046 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005047}
5048
Jesse Barnes2dd24552013-04-25 12:55:01 -07005049static void i9xx_pfit_enable(struct intel_crtc *crtc)
5050{
5051 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01005052 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005053 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005054
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005055 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005056 return;
5057
Daniel Vetterc0b03412013-05-28 12:05:54 +02005058 /*
5059 * The panel fitter should only be adjusted whilst the pipe is disabled,
5060 * according to register description and PRM.
5061 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005062 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5063 assert_pipe_disabled(dev_priv, crtc->pipe);
5064
Jesse Barnesb074cec2013-04-25 12:55:02 -07005065 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5066 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005067
5068 /* Border color in case we don't scale up to the full screen. Black by
5069 * default, change to something else for debugging. */
5070 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005071}
5072
Dave Airlied05410f2014-06-05 13:22:59 +10005073static enum intel_display_power_domain port_to_power_domain(enum port port)
5074{
5075 switch (port) {
5076 case PORT_A:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005077 return POWER_DOMAIN_PORT_DDI_A_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005078 case PORT_B:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005079 return POWER_DOMAIN_PORT_DDI_B_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005080 case PORT_C:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005081 return POWER_DOMAIN_PORT_DDI_C_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005082 case PORT_D:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005083 return POWER_DOMAIN_PORT_DDI_D_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005084 case PORT_E:
Patrik Jakobsson6331a702015-11-09 16:48:21 +01005085 return POWER_DOMAIN_PORT_DDI_E_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005086 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005087 MISSING_CASE(port);
Dave Airlied05410f2014-06-05 13:22:59 +10005088 return POWER_DOMAIN_PORT_OTHER;
5089 }
5090}
5091
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005092static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5093{
5094 switch (port) {
5095 case PORT_A:
5096 return POWER_DOMAIN_AUX_A;
5097 case PORT_B:
5098 return POWER_DOMAIN_AUX_B;
5099 case PORT_C:
5100 return POWER_DOMAIN_AUX_C;
5101 case PORT_D:
5102 return POWER_DOMAIN_AUX_D;
5103 case PORT_E:
5104 /* FIXME: Check VBT for actual wiring of PORT E */
5105 return POWER_DOMAIN_AUX_D;
5106 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005107 MISSING_CASE(port);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005108 return POWER_DOMAIN_AUX_A;
5109 }
5110}
5111
Imre Deak319be8a2014-03-04 19:22:57 +02005112enum intel_display_power_domain
5113intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005114{
Imre Deak319be8a2014-03-04 19:22:57 +02005115 struct drm_device *dev = intel_encoder->base.dev;
5116 struct intel_digital_port *intel_dig_port;
5117
5118 switch (intel_encoder->type) {
5119 case INTEL_OUTPUT_UNKNOWN:
5120 /* Only DDI platforms should ever use this output type */
5121 WARN_ON_ONCE(!HAS_DDI(dev));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005122 case INTEL_OUTPUT_DP:
Imre Deak319be8a2014-03-04 19:22:57 +02005123 case INTEL_OUTPUT_HDMI:
5124 case INTEL_OUTPUT_EDP:
5125 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005126 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005127 case INTEL_OUTPUT_DP_MST:
5128 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5129 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005130 case INTEL_OUTPUT_ANALOG:
5131 return POWER_DOMAIN_PORT_CRT;
5132 case INTEL_OUTPUT_DSI:
5133 return POWER_DOMAIN_PORT_DSI;
5134 default:
5135 return POWER_DOMAIN_PORT_OTHER;
5136 }
5137}
5138
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005139enum intel_display_power_domain
5140intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5141{
5142 struct drm_device *dev = intel_encoder->base.dev;
5143 struct intel_digital_port *intel_dig_port;
5144
5145 switch (intel_encoder->type) {
5146 case INTEL_OUTPUT_UNKNOWN:
Imre Deak651174a2015-11-18 15:57:24 +02005147 case INTEL_OUTPUT_HDMI:
5148 /*
5149 * Only DDI platforms should ever use these output types.
5150 * We can get here after the HDMI detect code has already set
5151 * the type of the shared encoder. Since we can't be sure
5152 * what's the status of the given connectors, play safe and
5153 * run the DP detection too.
5154 */
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005155 WARN_ON_ONCE(!HAS_DDI(dev));
Ville Syrjäläcca05022016-06-22 21:57:06 +03005156 case INTEL_OUTPUT_DP:
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005157 case INTEL_OUTPUT_EDP:
5158 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5159 return port_to_aux_power_domain(intel_dig_port->port);
5160 case INTEL_OUTPUT_DP_MST:
5161 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5162 return port_to_aux_power_domain(intel_dig_port->port);
5163 default:
Imre Deakb9fec162015-11-18 15:57:25 +02005164 MISSING_CASE(intel_encoder->type);
Ville Syrjälä25f78f52015-11-16 15:01:04 +01005165 return POWER_DOMAIN_AUX_A;
5166 }
5167}
5168
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005169static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5170 struct intel_crtc_state *crtc_state)
Imre Deak319be8a2014-03-04 19:22:57 +02005171{
5172 struct drm_device *dev = crtc->dev;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005173 struct drm_encoder *encoder;
Imre Deak319be8a2014-03-04 19:22:57 +02005174 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5175 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005176 unsigned long mask;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005177 enum transcoder transcoder = crtc_state->cpu_transcoder;
Imre Deak77d22dc2014-03-05 16:20:52 +02005178
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005179 if (!crtc_state->base.active)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005180 return 0;
5181
Imre Deak77d22dc2014-03-05 16:20:52 +02005182 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5183 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005184 if (crtc_state->pch_pfit.enabled ||
5185 crtc_state->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005186 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5187
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005188 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5189 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5190
Imre Deak319be8a2014-03-04 19:22:57 +02005191 mask |= BIT(intel_display_port_power_domain(intel_encoder));
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005192 }
Imre Deak319be8a2014-03-04 19:22:57 +02005193
Maarten Lankhorst15e7ec22016-03-14 09:27:54 +01005194 if (crtc_state->shared_dpll)
5195 mask |= BIT(POWER_DOMAIN_PLLS);
5196
Imre Deak77d22dc2014-03-05 16:20:52 +02005197 return mask;
5198}
5199
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005200static unsigned long
5201modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5202 struct intel_crtc_state *crtc_state)
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005203{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005204 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005205 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5206 enum intel_display_power_domain domain;
Daniel Vetter5a21b662016-05-24 17:13:53 +02005207 unsigned long domains, new_domains, old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005208
5209 old_domains = intel_crtc->enabled_power_domains;
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +01005210 intel_crtc->enabled_power_domains = new_domains =
5211 get_crtc_power_domains(crtc, crtc_state);
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005212
Daniel Vetter5a21b662016-05-24 17:13:53 +02005213 domains = new_domains & ~old_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005214
5215 for_each_power_domain(domain, domains)
5216 intel_display_power_get(dev_priv, domain);
5217
Daniel Vetter5a21b662016-05-24 17:13:53 +02005218 return old_domains & ~new_domains;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005219}
5220
5221static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5222 unsigned long domains)
5223{
5224 enum intel_display_power_domain domain;
5225
5226 for_each_power_domain(domain, domains)
5227 intel_display_power_put(dev_priv, domain);
5228}
5229
Mika Kaholaadafdc62015-08-18 14:36:59 +03005230static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5231{
5232 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5233
5234 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5235 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5236 return max_cdclk_freq;
5237 else if (IS_CHERRYVIEW(dev_priv))
5238 return max_cdclk_freq*95/100;
5239 else if (INTEL_INFO(dev_priv)->gen < 4)
5240 return 2*max_cdclk_freq*90/100;
5241 else
5242 return max_cdclk_freq*90/100;
5243}
5244
Ville Syrjäläb2045352016-05-13 23:41:27 +03005245static int skl_calc_cdclk(int max_pixclk, int vco);
5246
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005247static void intel_update_max_cdclk(struct drm_device *dev)
5248{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005249 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005250
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005251 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005252 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005253 int max_cdclk, vco;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005254
Ville Syrjäläb2045352016-05-13 23:41:27 +03005255 vco = dev_priv->skl_preferred_vco_freq;
Ville Syrjälä63911d72016-05-13 23:41:32 +03005256 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005257
5258 /*
5259 * Use the lower (vco 8640) cdclk values as a
5260 * first guess. skl_calc_cdclk() will correct it
5261 * if the preferred vco is 8100 instead.
5262 */
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005263 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005264 max_cdclk = 617143;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005265 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005266 max_cdclk = 540000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005267 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
Ville Syrjäläb2045352016-05-13 23:41:27 +03005268 max_cdclk = 432000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005269 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005270 max_cdclk = 308571;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005271
5272 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
Matt Roper281c1142016-04-05 14:37:19 -07005273 } else if (IS_BROXTON(dev)) {
5274 dev_priv->max_cdclk_freq = 624000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005275 } else if (IS_BROADWELL(dev)) {
5276 /*
5277 * FIXME with extra cooling we can allow
5278 * 540 MHz for ULX and 675 Mhz for ULT.
5279 * How can we know if extra cooling is
5280 * available? PCI ID, VTB, something else?
5281 */
5282 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5283 dev_priv->max_cdclk_freq = 450000;
5284 else if (IS_BDW_ULX(dev))
5285 dev_priv->max_cdclk_freq = 450000;
5286 else if (IS_BDW_ULT(dev))
5287 dev_priv->max_cdclk_freq = 540000;
5288 else
5289 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005290 } else if (IS_CHERRYVIEW(dev)) {
5291 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005292 } else if (IS_VALLEYVIEW(dev)) {
5293 dev_priv->max_cdclk_freq = 400000;
5294 } else {
5295 /* otherwise assume cdclk is fixed */
5296 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5297 }
5298
Mika Kaholaadafdc62015-08-18 14:36:59 +03005299 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5300
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005301 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5302 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005303
5304 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5305 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005306}
5307
5308static void intel_update_cdclk(struct drm_device *dev)
5309{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005310 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005311
5312 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005313
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005314 if (INTEL_GEN(dev_priv) >= 9)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005315 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5316 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5317 dev_priv->cdclk_pll.ref);
Ville Syrjälä2f2a1212016-05-13 23:41:25 +03005318 else
5319 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5320 dev_priv->cdclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005321
5322 /*
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005323 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5324 * Programmng [sic] note: bit[9:2] should be programmed to the number
5325 * of cdclk that generates 4MHz reference clock freq which is used to
5326 * generate GMBus clock. This will vary with the cdclk freq.
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005327 */
Ville Syrjäläb5d99ff2016-04-26 19:46:34 +03005328 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005329 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005330}
5331
Ville Syrjälä92891e42016-05-11 22:44:45 +03005332/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5333static int skl_cdclk_decimal(int cdclk)
5334{
5335 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5336}
5337
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005338static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5339{
5340 int ratio;
5341
5342 if (cdclk == dev_priv->cdclk_pll.ref)
5343 return 0;
5344
5345 switch (cdclk) {
5346 default:
5347 MISSING_CASE(cdclk);
5348 case 144000:
5349 case 288000:
5350 case 384000:
5351 case 576000:
5352 ratio = 60;
5353 break;
5354 case 624000:
5355 ratio = 65;
5356 break;
5357 }
5358
5359 return dev_priv->cdclk_pll.ref * ratio;
5360}
5361
Ville Syrjälä2b730012016-05-13 23:41:34 +03005362static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5363{
5364 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5365
5366 /* Timeout 200us */
Chris Wilson95cac282016-06-30 15:33:03 +01005367 if (intel_wait_for_register(dev_priv,
5368 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5369 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005370 DRM_ERROR("timeout waiting for DE PLL unlock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005371
5372 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005373}
5374
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005375static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005376{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005377 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005378 u32 val;
5379
5380 val = I915_READ(BXT_DE_PLL_CTL);
5381 val &= ~BXT_DE_PLL_RATIO_MASK;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005382 val |= BXT_DE_PLL_RATIO(ratio);
Ville Syrjälä2b730012016-05-13 23:41:34 +03005383 I915_WRITE(BXT_DE_PLL_CTL, val);
5384
5385 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5386
5387 /* Timeout 200us */
Chris Wilsone084e1b2016-06-30 15:33:04 +01005388 if (intel_wait_for_register(dev_priv,
5389 BXT_DE_PLL_ENABLE,
5390 BXT_DE_PLL_LOCK,
5391 BXT_DE_PLL_LOCK,
5392 1))
Ville Syrjälä2b730012016-05-13 23:41:34 +03005393 DRM_ERROR("timeout waiting for DE PLL lock\n");
Ville Syrjälä83d7c812016-05-13 23:41:35 +03005394
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005395 dev_priv->cdclk_pll.vco = vco;
Ville Syrjälä2b730012016-05-13 23:41:34 +03005396}
5397
Imre Deak324513c2016-06-13 16:44:36 +03005398static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305399{
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005400 u32 val, divider;
5401 int vco, ret;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305402
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005403 vco = bxt_de_pll_vco(dev_priv, cdclk);
5404
5405 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5406
5407 /* cdclk = vco / 2 / div{1,1.5,2,4} */
5408 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
5409 case 8:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305410 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305411 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005412 case 4:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305413 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305414 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005415 case 3:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305416 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305417 break;
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005418 case 2:
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305419 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305420 break;
5421 default:
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005422 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
5423 WARN_ON(vco != 0);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305424
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005425 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5426 break;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305427 }
5428
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305429 /* Inform power controller of upcoming frequency change */
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005430 mutex_lock(&dev_priv->rps.hw_lock);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305431 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5432 0x80000000);
5433 mutex_unlock(&dev_priv->rps.hw_lock);
5434
5435 if (ret) {
5436 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005437 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305438 return;
5439 }
5440
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005441 if (dev_priv->cdclk_pll.vco != 0 &&
5442 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä2b730012016-05-13 23:41:34 +03005443 bxt_de_pll_disable(dev_priv);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305444
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005445 if (dev_priv->cdclk_pll.vco != vco)
5446 bxt_de_pll_enable(dev_priv, vco);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305447
Ville Syrjälä5f199df2016-05-13 23:41:38 +03005448 val = divider | skl_cdclk_decimal(cdclk);
5449 /*
5450 * FIXME if only the cd2x divider needs changing, it could be done
5451 * without shutting off the pipe (if only one pipe is active).
5452 */
5453 val |= BXT_CDCLK_CD2X_PIPE_NONE;
5454 /*
5455 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5456 * enable otherwise.
5457 */
5458 if (cdclk >= 500000)
5459 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5460 I915_WRITE(CDCLK_CTL, val);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305461
5462 mutex_lock(&dev_priv->rps.hw_lock);
5463 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005464 DIV_ROUND_UP(cdclk, 25000));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305465 mutex_unlock(&dev_priv->rps.hw_lock);
5466
5467 if (ret) {
5468 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005469 ret, cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305470 return;
5471 }
5472
Chris Wilson91c8a322016-07-05 10:40:23 +01005473 intel_update_cdclk(&dev_priv->drm);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305474}
5475
Imre Deakd66a2192016-05-24 15:38:33 +03005476static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305477{
Imre Deakd66a2192016-05-24 15:38:33 +03005478 u32 cdctl, expected;
5479
Chris Wilson91c8a322016-07-05 10:40:23 +01005480 intel_update_cdclk(&dev_priv->drm);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305481
Imre Deakd66a2192016-05-24 15:38:33 +03005482 if (dev_priv->cdclk_pll.vco == 0 ||
5483 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5484 goto sanitize;
5485
5486 /* DPLL okay; verify the cdclock
5487 *
5488 * Some BIOS versions leave an incorrect decimal frequency value and
5489 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
5490 * so sanitize this register.
5491 */
5492 cdctl = I915_READ(CDCLK_CTL);
5493 /*
5494 * Let's ignore the pipe field, since BIOS could have configured the
5495 * dividers both synching to an active pipe, or asynchronously
5496 * (PIPE_NONE).
5497 */
5498 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
5499
5500 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
5501 skl_cdclk_decimal(dev_priv->cdclk_freq);
5502 /*
5503 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5504 * enable otherwise.
5505 */
5506 if (dev_priv->cdclk_freq >= 500000)
5507 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5508
5509 if (cdctl == expected)
5510 /* All well; nothing to sanitize */
5511 return;
5512
5513sanitize:
5514 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
5515
5516 /* force cdclk programming */
5517 dev_priv->cdclk_freq = 0;
5518
5519 /* force full PLL disable + enable */
5520 dev_priv->cdclk_pll.vco = -1;
5521}
5522
Imre Deak324513c2016-06-13 16:44:36 +03005523void bxt_init_cdclk(struct drm_i915_private *dev_priv)
Imre Deakd66a2192016-05-24 15:38:33 +03005524{
5525 bxt_sanitize_cdclk(dev_priv);
5526
5527 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
Ville Syrjälä089c6fd2016-05-13 23:41:36 +03005528 return;
Imre Deakc2e001e2016-04-01 16:02:43 +03005529
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305530 /*
5531 * FIXME:
5532 * - The initial CDCLK needs to be read from VBT.
5533 * Need to make this change after VBT has changes for BXT.
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305534 */
Imre Deak324513c2016-06-13 16:44:36 +03005535 bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305536}
5537
Imre Deak324513c2016-06-13 16:44:36 +03005538void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305539{
Imre Deak324513c2016-06-13 16:44:36 +03005540 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305541}
5542
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005543static int skl_calc_cdclk(int max_pixclk, int vco)
5544{
Ville Syrjälä63911d72016-05-13 23:41:32 +03005545 if (vco == 8640000) {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005546 if (max_pixclk > 540000)
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005547 return 617143;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005548 else if (max_pixclk > 432000)
5549 return 540000;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005550 else if (max_pixclk > 308571)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005551 return 432000;
5552 else
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005553 return 308571;
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005554 } else {
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005555 if (max_pixclk > 540000)
5556 return 675000;
5557 else if (max_pixclk > 450000)
5558 return 540000;
5559 else if (max_pixclk > 337500)
5560 return 450000;
5561 else
5562 return 337500;
5563 }
5564}
5565
Ville Syrjäläea617912016-05-13 23:41:24 +03005566static void
5567skl_dpll0_update(struct drm_i915_private *dev_priv)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005568{
Ville Syrjäläea617912016-05-13 23:41:24 +03005569 u32 val;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005570
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005571 dev_priv->cdclk_pll.ref = 24000;
Imre Deak1c3f7702016-05-24 15:38:32 +03005572 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005573
Ville Syrjäläea617912016-05-13 23:41:24 +03005574 val = I915_READ(LCPLL1_CTL);
Imre Deak1c3f7702016-05-24 15:38:32 +03005575 if ((val & LCPLL_PLL_ENABLE) == 0)
Ville Syrjäläea617912016-05-13 23:41:24 +03005576 return;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005577
Imre Deak1c3f7702016-05-24 15:38:32 +03005578 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
5579 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005580
Ville Syrjäläea617912016-05-13 23:41:24 +03005581 val = I915_READ(DPLL_CTRL1);
5582
Imre Deak1c3f7702016-05-24 15:38:32 +03005583 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
5584 DPLL_CTRL1_SSC(SKL_DPLL0) |
5585 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
5586 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
5587 return;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005588
Ville Syrjäläea617912016-05-13 23:41:24 +03005589 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
5590 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
5591 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
5592 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
5593 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005594 dev_priv->cdclk_pll.vco = 8100000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005595 break;
5596 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
5597 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
Ville Syrjälä63911d72016-05-13 23:41:32 +03005598 dev_priv->cdclk_pll.vco = 8640000;
Ville Syrjäläea617912016-05-13 23:41:24 +03005599 break;
5600 default:
5601 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
Ville Syrjäläea617912016-05-13 23:41:24 +03005602 break;
5603 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005604}
5605
Ville Syrjäläb2045352016-05-13 23:41:27 +03005606void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
5607{
5608 bool changed = dev_priv->skl_preferred_vco_freq != vco;
5609
5610 dev_priv->skl_preferred_vco_freq = vco;
5611
5612 if (changed)
Chris Wilson91c8a322016-07-05 10:40:23 +01005613 intel_update_max_cdclk(&dev_priv->drm);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005614}
5615
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005616static void
Ville Syrjälä3861fc62016-05-11 22:44:50 +03005617skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005618{
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005619 int min_cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005620 u32 val;
5621
Ville Syrjälä63911d72016-05-13 23:41:32 +03005622 WARN_ON(vco != 8100000 && vco != 8640000);
Ville Syrjäläb2045352016-05-13 23:41:27 +03005623
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005624 /* select the minimum CDCLK before enabling DPLL 0 */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005625 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005626 I915_WRITE(CDCLK_CTL, val);
5627 POSTING_READ(CDCLK_CTL);
5628
5629 /*
5630 * We always enable DPLL0 with the lowest link rate possible, but still
5631 * taking into account the VCO required to operate the eDP panel at the
5632 * desired frequency. The usual DP link rates operate with a VCO of
5633 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5634 * The modeset code is responsible for the selection of the exact link
5635 * rate later on, with the constraint of choosing a frequency that
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03005636 * works with vco.
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005637 */
5638 val = I915_READ(DPLL_CTRL1);
5639
5640 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5641 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5642 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
Ville Syrjälä63911d72016-05-13 23:41:32 +03005643 if (vco == 8640000)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005644 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5645 SKL_DPLL0);
5646 else
5647 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5648 SKL_DPLL0);
5649
5650 I915_WRITE(DPLL_CTRL1, val);
5651 POSTING_READ(DPLL_CTRL1);
5652
5653 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5654
Chris Wilsone24ca052016-06-30 15:33:05 +01005655 if (intel_wait_for_register(dev_priv,
5656 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
5657 5))
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005658 DRM_ERROR("DPLL0 not locked\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005659
Ville Syrjälä63911d72016-05-13 23:41:32 +03005660 dev_priv->cdclk_pll.vco = vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +03005661
5662 /* We'll want to keep using the current vco from now on. */
5663 skl_set_preferred_cdclk_vco(dev_priv, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005664}
5665
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005666static void
5667skl_dpll0_disable(struct drm_i915_private *dev_priv)
5668{
5669 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
Chris Wilson8ad32a052016-06-30 15:33:06 +01005670 if (intel_wait_for_register(dev_priv,
5671 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
5672 1))
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005673 DRM_ERROR("Couldn't disable DPLL0\n");
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005674
Ville Syrjälä63911d72016-05-13 23:41:32 +03005675 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä430e05d2016-05-11 22:44:47 +03005676}
5677
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005678static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5679{
5680 int ret;
5681 u32 val;
5682
5683 /* inform PCU we want to change CDCLK */
5684 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5685 mutex_lock(&dev_priv->rps.hw_lock);
5686 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5687 mutex_unlock(&dev_priv->rps.hw_lock);
5688
5689 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5690}
5691
5692static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5693{
Ville Syrjälä848496e2016-07-13 16:32:03 +03005694 return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005695}
5696
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005697static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005698{
Chris Wilson91c8a322016-07-05 10:40:23 +01005699 struct drm_device *dev = &dev_priv->drm;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005700 u32 freq_select, pcu_ack;
5701
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005702 WARN_ON((cdclk == 24000) != (vco == 0));
5703
Ville Syrjälä63911d72016-05-13 23:41:32 +03005704 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005705
5706 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5707 DRM_ERROR("failed to inform PCU about cdclk change\n");
5708 return;
5709 }
5710
5711 /* set CDCLK_CTL */
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005712 switch (cdclk) {
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005713 case 450000:
5714 case 432000:
5715 freq_select = CDCLK_FREQ_450_432;
5716 pcu_ack = 1;
5717 break;
5718 case 540000:
5719 freq_select = CDCLK_FREQ_540;
5720 pcu_ack = 2;
5721 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005722 case 308571:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005723 case 337500:
5724 default:
5725 freq_select = CDCLK_FREQ_337_308;
5726 pcu_ack = 0;
5727 break;
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03005728 case 617143:
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005729 case 675000:
5730 freq_select = CDCLK_FREQ_675_617;
5731 pcu_ack = 3;
5732 break;
5733 }
5734
Ville Syrjälä63911d72016-05-13 23:41:32 +03005735 if (dev_priv->cdclk_pll.vco != 0 &&
5736 dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005737 skl_dpll0_disable(dev_priv);
5738
Ville Syrjälä63911d72016-05-13 23:41:32 +03005739 if (dev_priv->cdclk_pll.vco != vco)
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005740 skl_dpll0_enable(dev_priv, vco);
5741
Ville Syrjälä9ef56152016-05-11 22:44:49 +03005742 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005743 POSTING_READ(CDCLK_CTL);
5744
5745 /* inform PCU of the change */
5746 mutex_lock(&dev_priv->rps.hw_lock);
5747 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5748 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005749
5750 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005751}
5752
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005753static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
5754
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005755void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5756{
Ville Syrjälä709e05c2016-05-13 23:41:33 +03005757 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005758}
5759
5760void skl_init_cdclk(struct drm_i915_private *dev_priv)
5761{
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005762 int cdclk, vco;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005763
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005764 skl_sanitize_cdclk(dev_priv);
5765
Ville Syrjälä63911d72016-05-13 23:41:32 +03005766 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005767 /*
5768 * Use the current vco as our initial
5769 * guess as to what the preferred vco is.
5770 */
5771 if (dev_priv->skl_preferred_vco_freq == 0)
5772 skl_set_preferred_cdclk_vco(dev_priv,
Ville Syrjälä63911d72016-05-13 23:41:32 +03005773 dev_priv->cdclk_pll.vco);
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005774 return;
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03005775 }
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005776
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005777 vco = dev_priv->skl_preferred_vco_freq;
5778 if (vco == 0)
Ville Syrjälä63911d72016-05-13 23:41:32 +03005779 vco = 8100000;
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005780 cdclk = skl_calc_cdclk(0, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005781
Ville Syrjälä70c2c182016-05-13 23:41:30 +03005782 skl_set_cdclk(dev_priv, cdclk, vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005783}
5784
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005785static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305786{
Ville Syrjälä09492492016-05-13 23:41:28 +03005787 uint32_t cdctl, expected;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305788
Shobhit Kumarf1b391a2015-11-05 18:05:32 +05305789 /*
5790 * check if the pre-os intialized the display
5791 * There is SWF18 scratchpad register defined which is set by the
5792 * pre-os which can be used by the OS drivers to check the status
5793 */
5794 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
5795 goto sanitize;
5796
Chris Wilson91c8a322016-07-05 10:40:23 +01005797 intel_update_cdclk(&dev_priv->drm);
Imre Deak1c3f7702016-05-24 15:38:32 +03005798 /* Is PLL enabled and locked ? */
5799 if (dev_priv->cdclk_pll.vco == 0 ||
5800 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
5801 goto sanitize;
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005802
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305803 /* DPLL okay; verify the cdclock
5804 *
5805 * Noticed in some instances that the freq selection is correct but
5806 * decimal part is programmed wrong from BIOS where pre-os does not
5807 * enable display. Verify the same as well.
5808 */
Ville Syrjälä09492492016-05-13 23:41:28 +03005809 cdctl = I915_READ(CDCLK_CTL);
5810 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
5811 skl_cdclk_decimal(dev_priv->cdclk_freq);
5812 if (cdctl == expected)
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305813 /* All well; nothing to sanitize */
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005814 return;
5815
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305816sanitize:
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005817 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
Clint Taylorc89e39f2016-05-13 23:41:21 +03005818
Ville Syrjälä9f7eb312016-05-13 23:41:29 +03005819 /* force cdclk programming */
5820 dev_priv->cdclk_freq = 0;
5821 /* force full PLL disable + enable */
Ville Syrjälä63911d72016-05-13 23:41:32 +03005822 dev_priv->cdclk_pll.vco = -1;
Shobhit Kumarc73666f2015-10-20 18:13:12 +05305823}
5824
Jesse Barnes30a970c2013-11-04 13:48:12 -08005825/* Adjust CDclk dividers to allow high res or save power if possible */
5826static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5827{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005828 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005829 u32 val, cmd;
5830
Vandana Kannan164dfd22014-11-24 13:37:41 +05305831 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5832 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005833
Ville Syrjälädfcab172014-06-13 13:37:47 +03005834 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005835 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005836 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005837 cmd = 1;
5838 else
5839 cmd = 0;
5840
5841 mutex_lock(&dev_priv->rps.hw_lock);
5842 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5843 val &= ~DSPFREQGUAR_MASK;
5844 val |= (cmd << DSPFREQGUAR_SHIFT);
5845 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5846 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5847 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5848 50)) {
5849 DRM_ERROR("timed out waiting for CDclk change\n");
5850 }
5851 mutex_unlock(&dev_priv->rps.hw_lock);
5852
Ville Syrjälä54433e92015-05-26 20:42:31 +03005853 mutex_lock(&dev_priv->sb_lock);
5854
Ville Syrjälädfcab172014-06-13 13:37:47 +03005855 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005856 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005857
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005858 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005859
Jesse Barnes30a970c2013-11-04 13:48:12 -08005860 /* adjust cdclk divider */
5861 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Vandana Kannan87d5d252015-09-24 23:29:17 +03005862 val &= ~CCK_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005863 val |= divider;
5864 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005865
5866 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
Vandana Kannan87d5d252015-09-24 23:29:17 +03005867 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
Ville Syrjäläa877e802014-06-13 13:37:52 +03005868 50))
5869 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005870 }
5871
Jesse Barnes30a970c2013-11-04 13:48:12 -08005872 /* adjust self-refresh exit latency value */
5873 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5874 val &= ~0x7f;
5875
5876 /*
5877 * For high bandwidth configs, we set a higher latency in the bunit
5878 * so that the core display fetch happens in time to avoid underruns.
5879 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005880 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005881 val |= 4500 / 250; /* 4.5 usec */
5882 else
5883 val |= 3000 / 250; /* 3.0 usec */
5884 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005885
Ville Syrjäläa5805162015-05-26 20:42:30 +03005886 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005887
Ville Syrjäläb6283052015-06-03 15:45:07 +03005888 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005889}
5890
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005891static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5892{
Chris Wilsonfac5e232016-07-04 11:34:36 +01005893 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005894 u32 val, cmd;
5895
Vandana Kannan164dfd22014-11-24 13:37:41 +05305896 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5897 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005898
5899 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005900 case 333333:
5901 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005902 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005903 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005904 break;
5905 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005906 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005907 return;
5908 }
5909
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005910 /*
5911 * Specs are full of misinformation, but testing on actual
5912 * hardware has shown that we just need to write the desired
5913 * CCK divider into the Punit register.
5914 */
5915 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5916
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005917 mutex_lock(&dev_priv->rps.hw_lock);
5918 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5919 val &= ~DSPFREQGUAR_MASK_CHV;
5920 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5921 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5922 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5923 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5924 50)) {
5925 DRM_ERROR("timed out waiting for CDclk change\n");
5926 }
5927 mutex_unlock(&dev_priv->rps.hw_lock);
5928
Ville Syrjäläb6283052015-06-03 15:45:07 +03005929 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005930}
5931
Jesse Barnes30a970c2013-11-04 13:48:12 -08005932static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5933 int max_pixclk)
5934{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005935 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005936 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005937
Jesse Barnes30a970c2013-11-04 13:48:12 -08005938 /*
5939 * Really only a few cases to deal with, as only 4 CDclks are supported:
5940 * 200MHz
5941 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005942 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005943 * 400MHz (VLV only)
5944 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5945 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005946 *
5947 * We seem to get an unstable or solid color picture at 200MHz.
5948 * Not sure what's wrong. For now use 200MHz only when all pipes
5949 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005950 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005951 if (!IS_CHERRYVIEW(dev_priv) &&
5952 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005953 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005954 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005955 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005956 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005957 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005958 else
5959 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005960}
5961
Imre Deak324513c2016-06-13 16:44:36 +03005962static int bxt_calc_cdclk(int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005963{
Ville Syrjälä760e1472016-05-11 22:44:46 +03005964 if (max_pixclk > 576000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305965 return 624000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005966 else if (max_pixclk > 384000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305967 return 576000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005968 else if (max_pixclk > 288000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305969 return 384000;
Ville Syrjälä760e1472016-05-11 22:44:46 +03005970 else if (max_pixclk > 144000)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305971 return 288000;
5972 else
5973 return 144000;
5974}
5975
Maarten Lankhorste8788cb2016-02-16 10:25:11 +01005976/* Compute the max pixel clock for new configuration. */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005977static int intel_mode_max_pixclk(struct drm_device *dev,
5978 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005979{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005980 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +01005981 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005982 struct drm_crtc *crtc;
5983 struct drm_crtc_state *crtc_state;
5984 unsigned max_pixclk = 0, i;
5985 enum pipe pipe;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005986
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005987 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
5988 sizeof(intel_state->min_pixclk));
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005989
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005990 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5991 int pixclk = 0;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005992
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005993 if (crtc_state->enable)
5994 pixclk = crtc_state->adjusted_mode.crtc_clock;
5995
5996 intel_state->min_pixclk[i] = pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005997 }
5998
Maarten Lankhorst565602d2015-12-10 12:33:57 +01005999 for_each_pipe(dev_priv, pipe)
6000 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6001
Jesse Barnes30a970c2013-11-04 13:48:12 -08006002 return max_pixclk;
6003}
6004
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006005static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006006{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006007 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006008 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006009 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006010 struct intel_atomic_state *intel_state =
6011 to_intel_atomic_state(state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006012
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006013 intel_state->cdclk = intel_state->dev_cdclk =
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006014 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306015
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006016 if (!intel_state->active_crtcs)
6017 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6018
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006019 return 0;
6020}
Jesse Barnes30a970c2013-11-04 13:48:12 -08006021
Imre Deak324513c2016-06-13 16:44:36 +03006022static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006023{
Ville Syrjälä4e5ca602016-05-11 22:44:44 +03006024 int max_pixclk = ilk_max_pixel_rate(state);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006025 struct intel_atomic_state *intel_state =
6026 to_intel_atomic_state(state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006027
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006028 intel_state->cdclk = intel_state->dev_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +03006029 bxt_calc_cdclk(max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02006030
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006031 if (!intel_state->active_crtcs)
Imre Deak324513c2016-06-13 16:44:36 +03006032 intel_state->dev_cdclk = bxt_calc_cdclk(0);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006033
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006034 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006035}
6036
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006037static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6038{
6039 unsigned int credits, default_credits;
6040
6041 if (IS_CHERRYVIEW(dev_priv))
6042 default_credits = PFI_CREDIT(12);
6043 else
6044 default_credits = PFI_CREDIT(8);
6045
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006046 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006047 /* CHV suggested value is 31 or 63 */
6048 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03006049 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006050 else
6051 credits = PFI_CREDIT(15);
6052 } else {
6053 credits = default_credits;
6054 }
6055
6056 /*
6057 * WA - write default credits before re-programming
6058 * FIXME: should we also set the resend bit here?
6059 */
6060 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6061 default_credits);
6062
6063 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6064 credits | PFI_CREDIT_RESEND);
6065
6066 /*
6067 * FIXME is this guaranteed to clear
6068 * immediately or should we poll for it?
6069 */
6070 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6071}
6072
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006073static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006074{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006075 struct drm_device *dev = old_state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006076 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01006077 struct intel_atomic_state *old_intel_state =
6078 to_intel_atomic_state(old_state);
6079 unsigned req_cdclk = old_intel_state->dev_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006080
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006081 /*
6082 * FIXME: We can end up here with all power domains off, yet
6083 * with a CDCLK frequency other than the minimum. To account
6084 * for this take the PIPE-A power domain, which covers the HW
6085 * blocks needed for the following programming. This can be
6086 * removed once it's guaranteed that we get here either with
6087 * the minimum CDCLK set, or the required power domains
6088 * enabled.
6089 */
6090 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006091
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006092 if (IS_CHERRYVIEW(dev))
6093 cherryview_set_cdclk(dev, req_cdclk);
6094 else
6095 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006096
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006097 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006098
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006099 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006100}
6101
Jesse Barnes89b667f2013-04-18 14:51:36 -07006102static void valleyview_crtc_enable(struct drm_crtc *crtc)
6103{
6104 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006105 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006106 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6107 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006108 struct intel_crtc_state *pipe_config =
6109 to_intel_crtc_state(crtc->state);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006110 int pipe = intel_crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006111
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006112 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006113 return;
6114
Ville Syrjälä37a56502016-06-22 21:57:04 +03006115 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306116 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006117
6118 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006119 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006120
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006121 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
Chris Wilsonfac5e232016-07-04 11:34:36 +01006122 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006123
6124 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6125 I915_WRITE(CHV_CANVAS(pipe), 0);
6126 }
6127
Daniel Vetter5b18e572014-04-24 23:55:06 +02006128 i9xx_set_pipeconf(intel_crtc);
6129
Jesse Barnes89b667f2013-04-18 14:51:36 -07006130 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006131
Daniel Vettera72e4c92014-09-30 10:56:47 +02006132 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006133
Jesse Barnes89b667f2013-04-18 14:51:36 -07006134 for_each_encoder_on_crtc(dev, crtc, encoder)
6135 if (encoder->pre_pll_enable)
6136 encoder->pre_pll_enable(encoder);
6137
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006138 if (IS_CHERRYVIEW(dev)) {
6139 chv_prepare_pll(intel_crtc, intel_crtc->config);
6140 chv_enable_pll(intel_crtc, intel_crtc->config);
6141 } else {
6142 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6143 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006144 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006145
6146 for_each_encoder_on_crtc(dev, crtc, encoder)
6147 if (encoder->pre_enable)
6148 encoder->pre_enable(encoder);
6149
Jesse Barnes2dd24552013-04-25 12:55:01 -07006150 i9xx_pfit_enable(intel_crtc);
6151
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006152 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006153
Ville Syrjäläcaed3612016-03-09 19:07:25 +02006154 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006155 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006156
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006157 assert_vblank_disabled(crtc);
6158 drm_crtc_vblank_on(crtc);
6159
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006160 for_each_encoder_on_crtc(dev, crtc, encoder)
6161 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006162}
6163
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006164static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6165{
6166 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006167 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006168
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006169 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6170 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006171}
6172
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006173static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006174{
6175 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006176 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006177 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006178 struct intel_encoder *encoder;
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006179 struct intel_crtc_state *pipe_config =
6180 to_intel_crtc_state(crtc->state);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03006181 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006182
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006183 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006184 return;
6185
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006186 i9xx_set_pll_dividers(intel_crtc);
6187
Ville Syrjälä37a56502016-06-22 21:57:04 +03006188 if (intel_crtc_has_dp_encoder(intel_crtc->config))
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306189 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006190
6191 intel_set_pipe_timings(intel_crtc);
Jani Nikulabc58be62016-03-18 17:05:39 +02006192 intel_set_pipe_src_size(intel_crtc);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006193
Daniel Vetter5b18e572014-04-24 23:55:06 +02006194 i9xx_set_pipeconf(intel_crtc);
6195
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006196 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006197
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006198 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006199 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006200
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006201 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006202 if (encoder->pre_enable)
6203 encoder->pre_enable(encoder);
6204
Daniel Vetterf6736a12013-06-05 13:34:30 +02006205 i9xx_enable_pll(intel_crtc);
6206
Jesse Barnes2dd24552013-04-25 12:55:01 -07006207 i9xx_pfit_enable(intel_crtc);
6208
Maarten Lankhorstb95c5322016-03-30 17:16:34 +02006209 intel_color_load_luts(&pipe_config->base);
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006210
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006211 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006212 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006213
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006214 assert_vblank_disabled(crtc);
6215 drm_crtc_vblank_on(crtc);
6216
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006217 for_each_encoder_on_crtc(dev, crtc, encoder)
6218 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006219}
6220
Daniel Vetter87476d62013-04-11 16:29:06 +02006221static void i9xx_pfit_disable(struct intel_crtc *crtc)
6222{
6223 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006224 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter328d8e82013-05-08 10:36:31 +02006225
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006226 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006227 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006228
6229 assert_pipe_disabled(dev_priv, crtc->pipe);
6230
Daniel Vetter328d8e82013-05-08 10:36:31 +02006231 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6232 I915_READ(PFIT_CONTROL));
6233 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006234}
6235
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006236static void i9xx_crtc_disable(struct drm_crtc *crtc)
6237{
6238 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006239 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006240 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006241 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006242 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006243
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006244 /*
6245 * On gen2 planes are double buffered but the pipe isn't, so we must
6246 * wait for planes to fully turn off before disabling the pipe.
6247 */
Ander Conselvan de Oliveira90e83e52016-03-22 10:11:24 +02006248 if (IS_GEN2(dev))
6249 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006250
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006251 for_each_encoder_on_crtc(dev, crtc, encoder)
6252 encoder->disable(encoder);
6253
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006254 drm_crtc_vblank_off(crtc);
6255 assert_vblank_disabled(crtc);
6256
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006257 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006258
Daniel Vetter87476d62013-04-11 16:29:06 +02006259 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006260
Jesse Barnes89b667f2013-04-18 14:51:36 -07006261 for_each_encoder_on_crtc(dev, crtc, encoder)
6262 if (encoder->post_disable)
6263 encoder->post_disable(encoder);
6264
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03006265 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006266 if (IS_CHERRYVIEW(dev))
6267 chv_disable_pll(dev_priv, pipe);
6268 else if (IS_VALLEYVIEW(dev))
6269 vlv_disable_pll(dev_priv, pipe);
6270 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006271 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006272 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006273
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006274 for_each_encoder_on_crtc(dev, crtc, encoder)
6275 if (encoder->post_pll_disable)
6276 encoder->post_pll_disable(encoder);
6277
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006278 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006279 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006280}
6281
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006282static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006283{
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006284 struct intel_encoder *encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006285 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006286 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006287 enum intel_display_power_domain domain;
6288 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006289
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006290 if (!intel_crtc->active)
6291 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006292
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006293 if (to_intel_plane_state(crtc->primary->state)->visible) {
Daniel Vetter5a21b662016-05-24 17:13:53 +02006294 WARN_ON(intel_crtc->flip_work);
Maarten Lankhorstfc32b1f2015-10-19 17:09:23 +02006295
Ville Syrjälä2622a082016-03-09 19:07:26 +02006296 intel_pre_disable_primary_noatomic(crtc);
Maarten Lankhorst54a419612015-11-23 10:25:28 +01006297
6298 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6299 to_intel_plane_state(crtc->primary->state)->visible = false;
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006300 }
6301
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006302 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006303
Ville Syrjälä78108b72016-05-27 20:59:19 +03006304 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6305 crtc->base.id, crtc->name);
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006306
6307 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6308 crtc->state->active = false;
Matt Roper37d90782015-09-24 15:53:06 -07006309 intel_crtc->active = false;
Maarten Lankhorst842e0302016-03-02 15:48:01 +01006310 crtc->enabled = false;
6311 crtc->state->connector_mask = 0;
6312 crtc->state->encoder_mask = 0;
6313
6314 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6315 encoder->base.crtc = NULL;
6316
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -02006317 intel_fbc_disable(intel_crtc);
Matt Roper37d90782015-09-24 15:53:06 -07006318 intel_update_watermarks(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006319 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006320
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006321 domains = intel_crtc->enabled_power_domains;
6322 for_each_power_domain(domain, domains)
6323 intel_display_power_put(dev_priv, domain);
6324 intel_crtc->enabled_power_domains = 0;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01006325
6326 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6327 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006328}
6329
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006330/*
6331 * turn all crtc's off, but do not adjust state
6332 * This has to be paired with a call to intel_modeset_setup_hw_state.
6333 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006334int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006335{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006336 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006337 struct drm_atomic_state *state;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006338 int ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006339
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006340 state = drm_atomic_helper_suspend(dev);
6341 ret = PTR_ERR_OR_ZERO(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006342 if (ret)
6343 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +01006344 else
6345 dev_priv->modeset_restore_state = state;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006346 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006347}
6348
Chris Wilsonea5b2132010-08-04 13:50:23 +01006349void intel_encoder_destroy(struct drm_encoder *encoder)
6350{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006351 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006352
Chris Wilsonea5b2132010-08-04 13:50:23 +01006353 drm_encoder_cleanup(encoder);
6354 kfree(intel_encoder);
6355}
6356
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006357/* Cross check the actual hw state with our own modeset state tracking (and it's
6358 * internal consistency). */
Daniel Vetter5a21b662016-05-24 17:13:53 +02006359static void intel_connector_verify_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006360{
Daniel Vetter5a21b662016-05-24 17:13:53 +02006361 struct drm_crtc *crtc = connector->base.state->crtc;
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006362
6363 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6364 connector->base.base.id,
6365 connector->base.name);
6366
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006367 if (connector->get_hw_state(connector)) {
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006368 struct intel_encoder *encoder = connector->encoder;
Daniel Vetter5a21b662016-05-24 17:13:53 +02006369 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006370
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006371 I915_STATE_WARN(!crtc,
6372 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006373
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006374 if (!crtc)
6375 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006376
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006377 I915_STATE_WARN(!crtc->state->active,
6378 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006379
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006380 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006381 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006382
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006383 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006384 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006385
Maarten Lankhorste85376c2015-08-27 13:13:31 +02006386 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006387 "attached encoder crtc differs from connector crtc\n");
6388 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006389 I915_STATE_WARN(crtc && crtc->state->active,
6390 "attached crtc is active, but connector isn't\n");
Daniel Vetter5a21b662016-05-24 17:13:53 +02006391 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006392 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006393 }
6394}
6395
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006396int intel_connector_init(struct intel_connector *connector)
6397{
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006398 drm_atomic_helper_connector_reset(&connector->base);
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006399
Maarten Lankhorst5350a032016-01-04 12:53:15 +01006400 if (!connector->base.state)
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006401 return -ENOMEM;
6402
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006403 return 0;
6404}
6405
6406struct intel_connector *intel_connector_alloc(void)
6407{
6408 struct intel_connector *connector;
6409
6410 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6411 if (!connector)
6412 return NULL;
6413
6414 if (intel_connector_init(connector) < 0) {
6415 kfree(connector);
6416 return NULL;
6417 }
6418
6419 return connector;
6420}
6421
Daniel Vetterf0947c32012-07-02 13:10:34 +02006422/* Simple connector->get_hw_state implementation for encoders that support only
6423 * one connector and no cloning and hence the encoder state determines the state
6424 * of the connector. */
6425bool intel_connector_get_hw_state(struct intel_connector *connector)
6426{
Daniel Vetter24929352012-07-02 20:28:59 +02006427 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006428 struct intel_encoder *encoder = connector->encoder;
6429
6430 return encoder->get_hw_state(encoder, &pipe);
6431}
6432
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006433static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006434{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006435 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6436 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006437
6438 return 0;
6439}
6440
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006441static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006442 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006443{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006444 struct drm_atomic_state *state = pipe_config->base.state;
6445 struct intel_crtc *other_crtc;
6446 struct intel_crtc_state *other_crtc_state;
6447
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006448 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6449 pipe_name(pipe), pipe_config->fdi_lanes);
6450 if (pipe_config->fdi_lanes > 4) {
6451 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6452 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006453 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006454 }
6455
Paulo Zanonibafb6552013-11-02 21:07:44 -07006456 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006457 if (pipe_config->fdi_lanes > 2) {
6458 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6459 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006460 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006461 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006462 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006463 }
6464 }
6465
6466 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006467 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006468
6469 /* Ivybridge 3 pipe is really complicated */
6470 switch (pipe) {
6471 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006472 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006473 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006474 if (pipe_config->fdi_lanes <= 2)
6475 return 0;
6476
6477 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6478 other_crtc_state =
6479 intel_atomic_get_crtc_state(state, other_crtc);
6480 if (IS_ERR(other_crtc_state))
6481 return PTR_ERR(other_crtc_state);
6482
6483 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006484 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6485 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006486 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006487 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006488 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006489 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006490 if (pipe_config->fdi_lanes > 2) {
6491 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6492 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006493 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006494 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006495
6496 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6497 other_crtc_state =
6498 intel_atomic_get_crtc_state(state, other_crtc);
6499 if (IS_ERR(other_crtc_state))
6500 return PTR_ERR(other_crtc_state);
6501
6502 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006503 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006504 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006505 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006506 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006507 default:
6508 BUG();
6509 }
6510}
6511
Daniel Vettere29c22c2013-02-21 00:00:16 +01006512#define RETRY 1
6513static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006514 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006515{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006516 struct drm_device *dev = intel_crtc->base.dev;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006517 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006518 int lane, link_bw, fdi_dotclock, ret;
6519 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006520
Daniel Vettere29c22c2013-02-21 00:00:16 +01006521retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006522 /* FDI is a binary signal running at ~2.7GHz, encoding
6523 * each output octet as 10 bits. The actual frequency
6524 * is stored as a divider into a 100MHz clock, and the
6525 * mode pixel clock is stored in units of 1KHz.
6526 * Hence the bw of each lane in terms of the mode signal
6527 * is:
6528 */
Ville Syrjälä21a727b2016-02-17 21:41:10 +02006529 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006530
Damien Lespiau241bfc32013-09-25 16:45:37 +01006531 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006532
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006533 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006534 pipe_config->pipe_bpp);
6535
6536 pipe_config->fdi_lanes = lane;
6537
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006538 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006539 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006540
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02006541 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006542 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006543 pipe_config->pipe_bpp -= 2*3;
6544 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6545 pipe_config->pipe_bpp);
6546 needs_recompute = true;
6547 pipe_config->bw_constrained = true;
6548
6549 goto retry;
6550 }
6551
6552 if (needs_recompute)
6553 return RETRY;
6554
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006555 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006556}
6557
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006558static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6559 struct intel_crtc_state *pipe_config)
6560{
6561 if (pipe_config->pipe_bpp > 24)
6562 return false;
6563
6564 /* HSW can handle pixel rate up to cdclk? */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03006565 if (IS_HASWELL(dev_priv))
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006566 return true;
6567
6568 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006569 * We compare against max which means we must take
6570 * the increased cdclk requirement into account when
6571 * calculating the new cdclk.
6572 *
6573 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006574 */
6575 return ilk_pipe_pixel_rate(pipe_config) <=
6576 dev_priv->max_cdclk_freq * 95 / 100;
6577}
6578
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006579static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006580 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006581{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006582 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006583 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006584
Jani Nikulad330a952014-01-21 11:24:25 +02006585 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006586 hsw_crtc_supports_ips(crtc) &&
6587 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006588}
6589
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006590static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
6591{
6592 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6593
6594 /* GDG double wide on either pipe, otherwise pipe A only */
6595 return INTEL_INFO(dev_priv)->gen < 4 &&
6596 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
6597}
6598
Daniel Vettera43f6e02013-06-07 23:10:32 +02006599static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006600 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006601{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006602 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01006603 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03006604 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläf3261152016-05-24 21:34:18 +03006605 int clock_limit = dev_priv->max_dotclk_freq;
Chris Wilson89749352010-09-12 18:25:19 +01006606
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006607 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006608 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006609
6610 /*
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006611 * Enable double wide mode when the dot clock
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006612 * is > 90% of the (display) core speed.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006613 */
Ville Syrjälä39acb4a2015-10-30 23:39:38 +02006614 if (intel_crtc_supports_double_wide(crtc) &&
6615 adjusted_mode->crtc_clock > clock_limit) {
Ville Syrjäläf3261152016-05-24 21:34:18 +03006616 clock_limit = dev_priv->max_dotclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006617 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006618 }
Ville Syrjäläf3261152016-05-24 21:34:18 +03006619 }
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006620
Ville Syrjäläf3261152016-05-24 21:34:18 +03006621 if (adjusted_mode->crtc_clock > clock_limit) {
6622 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
6623 adjusted_mode->crtc_clock, clock_limit,
6624 yesno(pipe_config->double_wide));
6625 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006626 }
Chris Wilson89749352010-09-12 18:25:19 +01006627
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006628 /*
6629 * Pipe horizontal size must be even in:
6630 * - DVO ganged mode
6631 * - LVDS dual channel mode
6632 * - Double wide pipe
6633 */
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03006634 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006635 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6636 pipe_config->pipe_src_w &= ~1;
6637
Damien Lespiau8693a822013-05-03 18:48:11 +01006638 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6639 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006640 */
6641 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
Ville Syrjäläaad941d2015-09-25 16:38:56 +03006642 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006643 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006644
Damien Lespiauf5adf942013-06-24 18:29:34 +01006645 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006646 hsw_compute_ips_config(crtc, pipe_config);
6647
Daniel Vetter877d48d2013-04-19 11:24:43 +02006648 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006649 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006650
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006651 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006652}
6653
Ville Syrjälä1652d192015-03-31 14:12:01 +03006654static int skylake_get_display_clock_speed(struct drm_device *dev)
6655{
6656 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläea617912016-05-13 23:41:24 +03006657 uint32_t cdctl;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006658
Ville Syrjäläea617912016-05-13 23:41:24 +03006659 skl_dpll0_update(dev_priv);
6660
Ville Syrjälä63911d72016-05-13 23:41:32 +03006661 if (dev_priv->cdclk_pll.vco == 0)
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006662 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006663
Ville Syrjäläea617912016-05-13 23:41:24 +03006664 cdctl = I915_READ(CDCLK_CTL);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006665
Ville Syrjälä63911d72016-05-13 23:41:32 +03006666 if (dev_priv->cdclk_pll.vco == 8640000) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006667 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6668 case CDCLK_FREQ_450_432:
6669 return 432000;
6670 case CDCLK_FREQ_337_308:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006671 return 308571;
Ville Syrjäläea617912016-05-13 23:41:24 +03006672 case CDCLK_FREQ_540:
6673 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006674 case CDCLK_FREQ_675_617:
Ville Syrjälä487ed2e2016-05-13 23:41:31 +03006675 return 617143;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006676 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006677 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006678 }
6679 } else {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006680 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6681 case CDCLK_FREQ_450_432:
6682 return 450000;
6683 case CDCLK_FREQ_337_308:
6684 return 337500;
Ville Syrjäläea617912016-05-13 23:41:24 +03006685 case CDCLK_FREQ_540:
6686 return 540000;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006687 case CDCLK_FREQ_675_617:
6688 return 675000;
6689 default:
Ville Syrjäläea617912016-05-13 23:41:24 +03006690 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006691 }
6692 }
6693
Ville Syrjälä709e05c2016-05-13 23:41:33 +03006694 return dev_priv->cdclk_pll.ref;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006695}
6696
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006697static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
6698{
6699 u32 val;
6700
6701 dev_priv->cdclk_pll.ref = 19200;
Imre Deak1c3f7702016-05-24 15:38:32 +03006702 dev_priv->cdclk_pll.vco = 0;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006703
6704 val = I915_READ(BXT_DE_PLL_ENABLE);
Imre Deak1c3f7702016-05-24 15:38:32 +03006705 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006706 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006707
Imre Deak1c3f7702016-05-24 15:38:32 +03006708 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
6709 return;
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006710
6711 val = I915_READ(BXT_DE_PLL_CTL);
6712 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
6713 dev_priv->cdclk_pll.ref;
6714}
6715
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006716static int broxton_get_display_clock_speed(struct drm_device *dev)
6717{
6718 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf5986242016-05-13 23:41:37 +03006719 u32 divider;
6720 int div, vco;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006721
Ville Syrjälä83d7c812016-05-13 23:41:35 +03006722 bxt_de_pll_update(dev_priv);
6723
Ville Syrjäläf5986242016-05-13 23:41:37 +03006724 vco = dev_priv->cdclk_pll.vco;
6725 if (vco == 0)
6726 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006727
Ville Syrjäläf5986242016-05-13 23:41:37 +03006728 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006729
Ville Syrjäläf5986242016-05-13 23:41:37 +03006730 switch (divider) {
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006731 case BXT_CDCLK_CD2X_DIV_SEL_1:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006732 div = 2;
6733 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006734 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006735 div = 3;
6736 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006737 case BXT_CDCLK_CD2X_DIV_SEL_2:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006738 div = 4;
6739 break;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006740 case BXT_CDCLK_CD2X_DIV_SEL_4:
Ville Syrjäläf5986242016-05-13 23:41:37 +03006741 div = 8;
6742 break;
6743 default:
6744 MISSING_CASE(divider);
6745 return dev_priv->cdclk_pll.ref;
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006746 }
6747
Ville Syrjäläf5986242016-05-13 23:41:37 +03006748 return DIV_ROUND_CLOSEST(vco, div);
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006749}
6750
Ville Syrjälä1652d192015-03-31 14:12:01 +03006751static int broadwell_get_display_clock_speed(struct drm_device *dev)
6752{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006753 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006754 uint32_t lcpll = I915_READ(LCPLL_CTL);
6755 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6756
6757 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6758 return 800000;
6759 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6760 return 450000;
6761 else if (freq == LCPLL_CLK_FREQ_450)
6762 return 450000;
6763 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6764 return 540000;
6765 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6766 return 337500;
6767 else
6768 return 675000;
6769}
6770
6771static int haswell_get_display_clock_speed(struct drm_device *dev)
6772{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006773 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä1652d192015-03-31 14:12:01 +03006774 uint32_t lcpll = I915_READ(LCPLL_CTL);
6775 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6776
6777 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6778 return 800000;
6779 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6780 return 450000;
6781 else if (freq == LCPLL_CLK_FREQ_450)
6782 return 450000;
6783 else if (IS_HSW_ULT(dev))
6784 return 337500;
6785 else
6786 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006787}
6788
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006789static int valleyview_get_display_clock_speed(struct drm_device *dev)
6790{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006791 return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6792 CCK_DISPLAY_CLOCK_CONTROL);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006793}
6794
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006795static int ilk_get_display_clock_speed(struct drm_device *dev)
6796{
6797 return 450000;
6798}
6799
Jesse Barnese70236a2009-09-21 10:42:27 -07006800static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006801{
Jesse Barnese70236a2009-09-21 10:42:27 -07006802 return 400000;
6803}
Jesse Barnes79e53942008-11-07 14:24:08 -08006804
Jesse Barnese70236a2009-09-21 10:42:27 -07006805static int i915_get_display_clock_speed(struct drm_device *dev)
6806{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006807 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006808}
Jesse Barnes79e53942008-11-07 14:24:08 -08006809
Jesse Barnese70236a2009-09-21 10:42:27 -07006810static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6811{
6812 return 200000;
6813}
Jesse Barnes79e53942008-11-07 14:24:08 -08006814
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006815static int pnv_get_display_clock_speed(struct drm_device *dev)
6816{
6817 u16 gcfgc = 0;
6818
6819 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6820
6821 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6822 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006823 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006824 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006825 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006826 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006827 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006828 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6829 return 200000;
6830 default:
6831 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6832 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006833 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006834 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006835 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006836 }
6837}
6838
Jesse Barnese70236a2009-09-21 10:42:27 -07006839static int i915gm_get_display_clock_speed(struct drm_device *dev)
6840{
6841 u16 gcfgc = 0;
6842
6843 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6844
6845 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006846 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006847 else {
6848 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6849 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006850 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006851 default:
6852 case GC_DISPLAY_CLOCK_190_200_MHZ:
6853 return 190000;
6854 }
6855 }
6856}
Jesse Barnes79e53942008-11-07 14:24:08 -08006857
Jesse Barnese70236a2009-09-21 10:42:27 -07006858static int i865_get_display_clock_speed(struct drm_device *dev)
6859{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006860 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006861}
6862
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006863static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006864{
6865 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006866
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006867 /*
6868 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6869 * encoding is different :(
6870 * FIXME is this the right way to detect 852GM/852GMV?
6871 */
6872 if (dev->pdev->revision == 0x1)
6873 return 133333;
6874
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006875 pci_bus_read_config_word(dev->pdev->bus,
6876 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6877
Jesse Barnese70236a2009-09-21 10:42:27 -07006878 /* Assume that the hardware is in the high speed state. This
6879 * should be the default.
6880 */
6881 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6882 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006883 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006884 case GC_CLOCK_100_200:
6885 return 200000;
6886 case GC_CLOCK_166_250:
6887 return 250000;
6888 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006889 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006890 case GC_CLOCK_133_266:
6891 case GC_CLOCK_133_266_2:
6892 case GC_CLOCK_166_266:
6893 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006894 }
6895
6896 /* Shouldn't happen */
6897 return 0;
6898}
6899
6900static int i830_get_display_clock_speed(struct drm_device *dev)
6901{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006902 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006903}
6904
Ville Syrjälä34edce22015-05-22 11:22:33 +03006905static unsigned int intel_hpll_vco(struct drm_device *dev)
6906{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006907 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä34edce22015-05-22 11:22:33 +03006908 static const unsigned int blb_vco[8] = {
6909 [0] = 3200000,
6910 [1] = 4000000,
6911 [2] = 5333333,
6912 [3] = 4800000,
6913 [4] = 6400000,
6914 };
6915 static const unsigned int pnv_vco[8] = {
6916 [0] = 3200000,
6917 [1] = 4000000,
6918 [2] = 5333333,
6919 [3] = 4800000,
6920 [4] = 2666667,
6921 };
6922 static const unsigned int cl_vco[8] = {
6923 [0] = 3200000,
6924 [1] = 4000000,
6925 [2] = 5333333,
6926 [3] = 6400000,
6927 [4] = 3333333,
6928 [5] = 3566667,
6929 [6] = 4266667,
6930 };
6931 static const unsigned int elk_vco[8] = {
6932 [0] = 3200000,
6933 [1] = 4000000,
6934 [2] = 5333333,
6935 [3] = 4800000,
6936 };
6937 static const unsigned int ctg_vco[8] = {
6938 [0] = 3200000,
6939 [1] = 4000000,
6940 [2] = 5333333,
6941 [3] = 6400000,
6942 [4] = 2666667,
6943 [5] = 4266667,
6944 };
6945 const unsigned int *vco_table;
6946 unsigned int vco;
6947 uint8_t tmp = 0;
6948
6949 /* FIXME other chipsets? */
6950 if (IS_GM45(dev))
6951 vco_table = ctg_vco;
6952 else if (IS_G4X(dev))
6953 vco_table = elk_vco;
6954 else if (IS_CRESTLINE(dev))
6955 vco_table = cl_vco;
6956 else if (IS_PINEVIEW(dev))
6957 vco_table = pnv_vco;
6958 else if (IS_G33(dev))
6959 vco_table = blb_vco;
6960 else
6961 return 0;
6962
6963 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6964
6965 vco = vco_table[tmp & 0x7];
6966 if (vco == 0)
6967 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6968 else
6969 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6970
6971 return vco;
6972}
6973
6974static int gm45_get_display_clock_speed(struct drm_device *dev)
6975{
6976 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6977 uint16_t tmp = 0;
6978
6979 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6980
6981 cdclk_sel = (tmp >> 12) & 0x1;
6982
6983 switch (vco) {
6984 case 2666667:
6985 case 4000000:
6986 case 5333333:
6987 return cdclk_sel ? 333333 : 222222;
6988 case 3200000:
6989 return cdclk_sel ? 320000 : 228571;
6990 default:
6991 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6992 return 222222;
6993 }
6994}
6995
6996static int i965gm_get_display_clock_speed(struct drm_device *dev)
6997{
6998 static const uint8_t div_3200[] = { 16, 10, 8 };
6999 static const uint8_t div_4000[] = { 20, 12, 10 };
7000 static const uint8_t div_5333[] = { 24, 16, 14 };
7001 const uint8_t *div_table;
7002 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7003 uint16_t tmp = 0;
7004
7005 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7006
7007 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7008
7009 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7010 goto fail;
7011
7012 switch (vco) {
7013 case 3200000:
7014 div_table = div_3200;
7015 break;
7016 case 4000000:
7017 div_table = div_4000;
7018 break;
7019 case 5333333:
7020 div_table = div_5333;
7021 break;
7022 default:
7023 goto fail;
7024 }
7025
7026 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7027
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007028fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007029 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7030 return 200000;
7031}
7032
7033static int g33_get_display_clock_speed(struct drm_device *dev)
7034{
7035 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7036 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7037 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7038 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7039 const uint8_t *div_table;
7040 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7041 uint16_t tmp = 0;
7042
7043 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7044
7045 cdclk_sel = (tmp >> 4) & 0x7;
7046
7047 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7048 goto fail;
7049
7050 switch (vco) {
7051 case 3200000:
7052 div_table = div_3200;
7053 break;
7054 case 4000000:
7055 div_table = div_4000;
7056 break;
7057 case 4800000:
7058 div_table = div_4800;
7059 break;
7060 case 5333333:
7061 div_table = div_5333;
7062 break;
7063 default:
7064 goto fail;
7065 }
7066
7067 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7068
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007069fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007070 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7071 return 190476;
7072}
7073
Zhenyu Wang2c072452009-06-05 15:38:42 +08007074static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007075intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007076{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007077 while (*num > DATA_LINK_M_N_MASK ||
7078 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007079 *num >>= 1;
7080 *den >>= 1;
7081 }
7082}
7083
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007084static void compute_m_n(unsigned int m, unsigned int n,
7085 uint32_t *ret_m, uint32_t *ret_n)
7086{
7087 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7088 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7089 intel_reduce_m_n_ratio(ret_m, ret_n);
7090}
7091
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007092void
7093intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7094 int pixel_clock, int link_clock,
7095 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007096{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007097 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007098
7099 compute_m_n(bits_per_pixel * pixel_clock,
7100 link_clock * nlanes * 8,
7101 &m_n->gmch_m, &m_n->gmch_n);
7102
7103 compute_m_n(pixel_clock, link_clock,
7104 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007105}
7106
Chris Wilsona7615032011-01-12 17:04:08 +00007107static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7108{
Jani Nikulad330a952014-01-21 11:24:25 +02007109 if (i915.panel_use_ssc >= 0)
7110 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007111 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007112 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007113}
7114
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007115static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007116{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007117 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007118}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007119
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007120static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7121{
7122 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007123}
7124
Daniel Vetterf47709a2013-03-28 10:42:02 +01007125static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007126 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007127 struct dpll *reduced_clock)
Jesse Barnesa7516a02011-12-15 12:30:37 -08007128{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007129 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007130 u32 fp, fp2 = 0;
7131
7132 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007133 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007134 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007135 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007136 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007137 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007138 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007139 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007140 }
7141
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007142 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007143
Daniel Vetterf47709a2013-03-28 10:42:02 +01007144 crtc->lowfreq_avail = false;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007145 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007146 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007147 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007148 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007149 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007150 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007151 }
7152}
7153
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007154static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7155 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007156{
7157 u32 reg_val;
7158
7159 /*
7160 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7161 * and set it to a reasonable value instead.
7162 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007163 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007164 reg_val &= 0xffffff00;
7165 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007166 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007167
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007168 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007169 reg_val &= 0x8cffffff;
7170 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007171 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007172
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007173 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007174 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007175 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007176
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007177 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007178 reg_val &= 0x00ffffff;
7179 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007180 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007181}
7182
Daniel Vetterb5518422013-05-03 11:49:48 +02007183static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7184 struct intel_link_m_n *m_n)
7185{
7186 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007187 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007188 int pipe = crtc->pipe;
7189
Daniel Vettere3b95f12013-05-03 11:49:49 +02007190 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7191 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7192 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7193 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007194}
7195
7196static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007197 struct intel_link_m_n *m_n,
7198 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007199{
7200 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007201 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterb5518422013-05-03 11:49:48 +02007202 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007203 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007204
7205 if (INTEL_INFO(dev)->gen >= 5) {
7206 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7207 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7208 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7209 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007210 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7211 * for gen < 8) and if DRRS is supported (to make sure the
7212 * registers are not unnecessarily accessed).
7213 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307214 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007215 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007216 I915_WRITE(PIPE_DATA_M2(transcoder),
7217 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7218 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7219 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7220 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7221 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007222 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007223 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7224 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7225 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7226 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007227 }
7228}
7229
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307230void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007231{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307232 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7233
7234 if (m_n == M1_N1) {
7235 dp_m_n = &crtc->config->dp_m_n;
7236 dp_m2_n2 = &crtc->config->dp_m2_n2;
7237 } else if (m_n == M2_N2) {
7238
7239 /*
7240 * M2_N2 registers are not supported. Hence m2_n2 divider value
7241 * needs to be programmed into M1_N1.
7242 */
7243 dp_m_n = &crtc->config->dp_m2_n2;
7244 } else {
7245 DRM_ERROR("Unsupported divider value\n");
7246 return;
7247 }
7248
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007249 if (crtc->config->has_pch_encoder)
7250 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007251 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307252 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007253}
7254
Daniel Vetter251ac862015-06-18 10:30:24 +02007255static void vlv_compute_dpll(struct intel_crtc *crtc,
7256 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007257{
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007258 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007259 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007260 if (crtc->pipe != PIPE_A)
7261 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007262
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007263 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007264 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007265 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7266 DPLL_EXT_BUFFER_ENABLE_VLV;
7267
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007268 pipe_config->dpll_hw_state.dpll_md =
7269 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7270}
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007271
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007272static void chv_compute_dpll(struct intel_crtc *crtc,
7273 struct intel_crtc_state *pipe_config)
7274{
7275 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007276 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007277 if (crtc->pipe != PIPE_A)
7278 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7279
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007280 /* DPLL not used with DSI, but still need the rest set up */
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03007281 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007282 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7283
Ville Syrjälä03ed5cbf2016-03-15 16:39:55 +02007284 pipe_config->dpll_hw_state.dpll_md =
7285 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007286}
7287
Ville Syrjäläd288f652014-10-28 13:20:22 +02007288static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007289 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007290{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007291 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007292 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007293 enum pipe pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007294 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007295 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007296 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007297
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007298 /* Enable Refclk */
7299 I915_WRITE(DPLL(pipe),
7300 pipe_config->dpll_hw_state.dpll &
7301 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7302
7303 /* No need to actually set up the DPLL with DSI */
7304 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7305 return;
7306
Ville Syrjäläa5805162015-05-26 20:42:30 +03007307 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007308
Ville Syrjäläd288f652014-10-28 13:20:22 +02007309 bestn = pipe_config->dpll.n;
7310 bestm1 = pipe_config->dpll.m1;
7311 bestm2 = pipe_config->dpll.m2;
7312 bestp1 = pipe_config->dpll.p1;
7313 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007314
Jesse Barnes89b667f2013-04-18 14:51:36 -07007315 /* See eDP HDMI DPIO driver vbios notes doc */
7316
7317 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007318 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007319 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007320
7321 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007322 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007323
7324 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007325 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007326 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007327 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007328
7329 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007330 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007331
7332 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007333 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7334 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7335 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007336 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007337
7338 /*
7339 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7340 * but we don't support that).
7341 * Note: don't use the DAC post divider as it seems unstable.
7342 */
7343 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007344 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007345
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007346 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007347 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007348
Jesse Barnes89b667f2013-04-18 14:51:36 -07007349 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007350 if (pipe_config->port_clock == 162000 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007351 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7352 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007353 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007354 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007355 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007356 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007357 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007358
Ville Syrjälä37a56502016-06-22 21:57:04 +03007359 if (intel_crtc_has_dp_encoder(pipe_config)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007360 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007361 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007362 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007363 0x0df40000);
7364 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007365 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007366 0x0df70000);
7367 } else { /* HDMI or VGA */
7368 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007369 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007370 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007371 0x0df70000);
7372 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007373 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007374 0x0df40000);
7375 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007376
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007377 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007378 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ville Syrjälä2210ce72016-06-22 21:57:05 +03007379 if (intel_crtc_has_dp_encoder(crtc->config))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007380 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007381 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007382
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007383 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007384 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007385}
7386
Ville Syrjäläd288f652014-10-28 13:20:22 +02007387static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007388 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007389{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007390 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007391 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007392 enum pipe pipe = crtc->pipe;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007393 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307394 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007395 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307396 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307397 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007398
Ville Syrjäläcd2d34d2016-04-12 22:14:34 +03007399 /* Enable Refclk and SSC */
7400 I915_WRITE(DPLL(pipe),
7401 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7402
7403 /* No need to actually set up the DPLL with DSI */
7404 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7405 return;
7406
Ville Syrjäläd288f652014-10-28 13:20:22 +02007407 bestn = pipe_config->dpll.n;
7408 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7409 bestm1 = pipe_config->dpll.m1;
7410 bestm2 = pipe_config->dpll.m2 >> 22;
7411 bestp1 = pipe_config->dpll.p1;
7412 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307413 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307414 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307415 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007416
Ville Syrjäläa5805162015-05-26 20:42:30 +03007417 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007418
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007419 /* p1 and p2 divider */
7420 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7421 5 << DPIO_CHV_S1_DIV_SHIFT |
7422 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7423 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7424 1 << DPIO_CHV_K_DIV_SHIFT);
7425
7426 /* Feedback post-divider - m2 */
7427 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7428
7429 /* Feedback refclk divider - n and m1 */
7430 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7431 DPIO_CHV_M1_DIV_BY_2 |
7432 1 << DPIO_CHV_N_DIV_SHIFT);
7433
7434 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007435 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007436
7437 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307438 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7439 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7440 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7441 if (bestm2_frac)
7442 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7443 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007444
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307445 /* Program digital lock detect threshold */
7446 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7447 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7448 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7449 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7450 if (!bestm2_frac)
7451 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7452 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7453
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007454 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307455 if (vco == 5400000) {
7456 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7457 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7458 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7459 tribuf_calcntr = 0x9;
7460 } else if (vco <= 6200000) {
7461 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7462 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7463 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7464 tribuf_calcntr = 0x9;
7465 } else if (vco <= 6480000) {
7466 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7467 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7468 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7469 tribuf_calcntr = 0x8;
7470 } else {
7471 /* Not supported. Apply the same limits as in the max case */
7472 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7473 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7474 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7475 tribuf_calcntr = 0;
7476 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007477 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7478
Ville Syrjälä968040b2015-03-11 22:52:08 +02007479 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307480 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7481 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7482 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7483
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007484 /* AFC Recal */
7485 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7486 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7487 DPIO_AFC_RECAL);
7488
Ville Syrjäläa5805162015-05-26 20:42:30 +03007489 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007490}
7491
Ville Syrjäläd288f652014-10-28 13:20:22 +02007492/**
7493 * vlv_force_pll_on - forcibly enable just the PLL
7494 * @dev_priv: i915 private structure
7495 * @pipe: pipe PLL to enable
7496 * @dpll: PLL configuration
7497 *
7498 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7499 * in cases where we need the PLL enabled even when @pipe is not going to
7500 * be enabled.
7501 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007502int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7503 const struct dpll *dpll)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007504{
7505 struct intel_crtc *crtc =
7506 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007507 struct intel_crtc_state *pipe_config;
7508
7509 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7510 if (!pipe_config)
7511 return -ENOMEM;
7512
7513 pipe_config->base.crtc = &crtc->base;
7514 pipe_config->pixel_multiplier = 1;
7515 pipe_config->dpll = *dpll;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007516
7517 if (IS_CHERRYVIEW(dev)) {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007518 chv_compute_dpll(crtc, pipe_config);
7519 chv_prepare_pll(crtc, pipe_config);
7520 chv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007521 } else {
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007522 vlv_compute_dpll(crtc, pipe_config);
7523 vlv_prepare_pll(crtc, pipe_config);
7524 vlv_enable_pll(crtc, pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007525 }
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +00007526
7527 kfree(pipe_config);
7528
7529 return 0;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007530}
7531
7532/**
7533 * vlv_force_pll_off - forcibly disable just the PLL
7534 * @dev_priv: i915 private structure
7535 * @pipe: pipe PLL to disable
7536 *
7537 * Disable the PLL for @pipe. To be used in cases where we need
7538 * the PLL enabled even when @pipe is not going to be enabled.
7539 */
7540void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7541{
7542 if (IS_CHERRYVIEW(dev))
7543 chv_disable_pll(to_i915(dev), pipe);
7544 else
7545 vlv_disable_pll(to_i915(dev), pipe);
7546}
7547
Daniel Vetter251ac862015-06-18 10:30:24 +02007548static void i9xx_compute_dpll(struct intel_crtc *crtc,
7549 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007550 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007551{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007552 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007553 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007554 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007555 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007556
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007557 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307558
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007559 dpll = DPLL_VGA_MODE_DIS;
7560
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007561 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007562 dpll |= DPLLB_MODE_LVDS;
7563 else
7564 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007565
Daniel Vetteref1b4602013-06-01 17:17:04 +02007566 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007567 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007568 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007569 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007570
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03007571 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7572 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007573 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007574
Ville Syrjälä37a56502016-06-22 21:57:04 +03007575 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007576 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007577
7578 /* compute bitmask from p1 value */
7579 if (IS_PINEVIEW(dev))
7580 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7581 else {
7582 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7583 if (IS_G4X(dev) && reduced_clock)
7584 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7585 }
7586 switch (clock->p2) {
7587 case 5:
7588 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7589 break;
7590 case 7:
7591 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7592 break;
7593 case 10:
7594 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7595 break;
7596 case 14:
7597 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7598 break;
7599 }
7600 if (INTEL_INFO(dev)->gen >= 4)
7601 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7602
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007603 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007604 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007605 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007606 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007607 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7608 else
7609 dpll |= PLL_REF_INPUT_DREFCLK;
7610
7611 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007612 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007613
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007614 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007615 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007616 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007617 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007618 }
7619}
7620
Daniel Vetter251ac862015-06-18 10:30:24 +02007621static void i8xx_compute_dpll(struct intel_crtc *crtc,
7622 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03007623 struct dpll *reduced_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007624{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007625 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007626 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007627 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007628 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007629
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007630 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307631
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007632 dpll = DPLL_VGA_MODE_DIS;
7633
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007634 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007635 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7636 } else {
7637 if (clock->p1 == 2)
7638 dpll |= PLL_P1_DIVIDE_BY_TWO;
7639 else
7640 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7641 if (clock->p2 == 4)
7642 dpll |= PLL_P2_DIVIDE_BY_4;
7643 }
7644
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007645 if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007646 dpll |= DPLL_DVO_2X_MODE;
7647
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007648 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraceb41002016-03-21 18:00:02 +02007649 intel_panel_use_ssc(dev_priv))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007650 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7651 else
7652 dpll |= PLL_REF_INPUT_DREFCLK;
7653
7654 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007655 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007656}
7657
Daniel Vetter8a654f32013-06-01 17:16:22 +02007658static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007659{
7660 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007661 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007662 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007663 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +03007664 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007665 uint32_t crtc_vtotal, crtc_vblank_end;
7666 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007667
7668 /* We need to be careful not to changed the adjusted mode, for otherwise
7669 * the hw state checker will get angry at the mismatch. */
7670 crtc_vtotal = adjusted_mode->crtc_vtotal;
7671 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007672
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007673 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007674 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007675 crtc_vtotal -= 1;
7676 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007677
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007678 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007679 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7680 else
7681 vsyncshift = adjusted_mode->crtc_hsync_start -
7682 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007683 if (vsyncshift < 0)
7684 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007685 }
7686
7687 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007688 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007689
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007690 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007691 (adjusted_mode->crtc_hdisplay - 1) |
7692 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007693 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007694 (adjusted_mode->crtc_hblank_start - 1) |
7695 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007696 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007697 (adjusted_mode->crtc_hsync_start - 1) |
7698 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7699
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007700 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007701 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007702 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007703 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007704 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007705 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007706 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007707 (adjusted_mode->crtc_vsync_start - 1) |
7708 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7709
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007710 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7711 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7712 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7713 * bits. */
7714 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7715 (pipe == PIPE_B || pipe == PIPE_C))
7716 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7717
Jani Nikulabc58be62016-03-18 17:05:39 +02007718}
7719
7720static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
7721{
7722 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007723 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007724 enum pipe pipe = intel_crtc->pipe;
7725
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007726 /* pipesrc controls the size that is scaled from, which should
7727 * always be the user's requested size.
7728 */
7729 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007730 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7731 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007732}
7733
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007734static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007735 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007736{
7737 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007738 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007739 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7740 uint32_t tmp;
7741
7742 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007743 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7744 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007745 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007746 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7747 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007748 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007749 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7750 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007751
7752 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007753 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7754 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007755 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007756 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7757 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007758 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007759 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7760 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007761
7762 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007763 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7764 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7765 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007766 }
Jani Nikulabc58be62016-03-18 17:05:39 +02007767}
7768
7769static void intel_get_pipe_src_size(struct intel_crtc *crtc,
7770 struct intel_crtc_state *pipe_config)
7771{
7772 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007773 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulabc58be62016-03-18 17:05:39 +02007774 u32 tmp;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007775
7776 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007777 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7778 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7779
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007780 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7781 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007782}
7783
Daniel Vetterf6a83282014-02-11 15:28:57 -08007784void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007785 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007786{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007787 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7788 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7789 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7790 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007791
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007792 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7793 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7794 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7795 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007796
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007797 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007798 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007799
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007800 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7801 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007802
7803 mode->hsync = drm_mode_hsync(mode);
7804 mode->vrefresh = drm_mode_vrefresh(mode);
7805 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007806}
7807
Daniel Vetter84b046f2013-02-19 18:48:54 +01007808static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7809{
7810 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007811 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter84b046f2013-02-19 18:48:54 +01007812 uint32_t pipeconf;
7813
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007814 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007815
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007816 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7817 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7818 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007819
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007820 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007821 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007822
Daniel Vetterff9ce462013-04-24 14:57:17 +02007823 /* only g4x and later have fancy bpc/dither controls */
Wayne Boyer666a4532015-12-09 12:29:35 -08007824 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007825 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007826 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007827 pipeconf |= PIPECONF_DITHER_EN |
7828 PIPECONF_DITHER_TYPE_SP;
7829
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007830 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007831 case 18:
7832 pipeconf |= PIPECONF_6BPC;
7833 break;
7834 case 24:
7835 pipeconf |= PIPECONF_8BPC;
7836 break;
7837 case 30:
7838 pipeconf |= PIPECONF_10BPC;
7839 break;
7840 default:
7841 /* Case prevented by intel_choose_pipe_bpp_dither. */
7842 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007843 }
7844 }
7845
7846 if (HAS_PIPE_CXSR(dev)) {
7847 if (intel_crtc->lowfreq_avail) {
7848 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7849 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7850 } else {
7851 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007852 }
7853 }
7854
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007855 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007856 if (INTEL_INFO(dev)->gen < 4 ||
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007857 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007858 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7859 else
7860 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7861 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007862 pipeconf |= PIPECONF_PROGRESSIVE;
7863
Wayne Boyer666a4532015-12-09 12:29:35 -08007864 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
7865 intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007866 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007867
Daniel Vetter84b046f2013-02-19 18:48:54 +01007868 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7869 POSTING_READ(PIPECONF(intel_crtc->pipe));
7870}
7871
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007872static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
7873 struct intel_crtc_state *crtc_state)
7874{
7875 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007876 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007877 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007878 int refclk = 48000;
7879
7880 memset(&crtc_state->dpll_hw_state, 0,
7881 sizeof(crtc_state->dpll_hw_state));
7882
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007883 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007884 if (intel_panel_use_ssc(dev_priv)) {
7885 refclk = dev_priv->vbt.lvds_ssc_freq;
7886 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7887 }
7888
7889 limit = &intel_limits_i8xx_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007890 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007891 limit = &intel_limits_i8xx_dvo;
7892 } else {
7893 limit = &intel_limits_i8xx_dac;
7894 }
7895
7896 if (!crtc_state->clock_set &&
7897 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7898 refclk, NULL, &crtc_state->dpll)) {
7899 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7900 return -EINVAL;
7901 }
7902
7903 i8xx_compute_dpll(crtc, crtc_state, NULL);
7904
7905 return 0;
7906}
7907
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007908static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
7909 struct intel_crtc_state *crtc_state)
7910{
7911 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007912 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007913 const struct intel_limit *limit;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007914 int refclk = 96000;
7915
7916 memset(&crtc_state->dpll_hw_state, 0,
7917 sizeof(crtc_state->dpll_hw_state));
7918
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007919 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007920 if (intel_panel_use_ssc(dev_priv)) {
7921 refclk = dev_priv->vbt.lvds_ssc_freq;
7922 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7923 }
7924
7925 if (intel_is_dual_link_lvds(dev))
7926 limit = &intel_limits_g4x_dual_channel_lvds;
7927 else
7928 limit = &intel_limits_g4x_single_channel_lvds;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007929 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
7930 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007931 limit = &intel_limits_g4x_hdmi;
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007932 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +02007933 limit = &intel_limits_g4x_sdvo;
7934 } else {
7935 /* The option is for other outputs */
7936 limit = &intel_limits_i9xx_sdvo;
7937 }
7938
7939 if (!crtc_state->clock_set &&
7940 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7941 refclk, NULL, &crtc_state->dpll)) {
7942 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7943 return -EINVAL;
7944 }
7945
7946 i9xx_compute_dpll(crtc, crtc_state, NULL);
7947
7948 return 0;
7949}
7950
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007951static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
7952 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007953{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007954 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007955 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007956 const struct intel_limit *limit;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007957 int refclk = 96000;
Jesse Barnes79e53942008-11-07 14:24:08 -08007958
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007959 memset(&crtc_state->dpll_hw_state, 0,
7960 sizeof(crtc_state->dpll_hw_state));
7961
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007962 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007963 if (intel_panel_use_ssc(dev_priv)) {
7964 refclk = dev_priv->vbt.lvds_ssc_freq;
7965 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7966 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007967
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007968 limit = &intel_limits_pineview_lvds;
7969 } else {
7970 limit = &intel_limits_pineview_sdvo;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02007971 }
Jani Nikulaf2335332013-09-13 11:03:09 +03007972
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007973 if (!crtc_state->clock_set &&
7974 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
7975 refclk, NULL, &crtc_state->dpll)) {
7976 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7977 return -EINVAL;
7978 }
7979
7980 i9xx_compute_dpll(crtc, crtc_state, NULL);
7981
7982 return 0;
7983}
7984
7985static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7986 struct intel_crtc_state *crtc_state)
7987{
7988 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01007989 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03007990 const struct intel_limit *limit;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007991 int refclk = 96000;
7992
7993 memset(&crtc_state->dpll_hw_state, 0,
7994 sizeof(crtc_state->dpll_hw_state));
7995
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03007996 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02007997 if (intel_panel_use_ssc(dev_priv)) {
7998 refclk = dev_priv->vbt.lvds_ssc_freq;
7999 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008000 }
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +02008001
8002 limit = &intel_limits_i9xx_lvds;
8003 } else {
8004 limit = &intel_limits_i9xx_sdvo;
8005 }
8006
8007 if (!crtc_state->clock_set &&
8008 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8009 refclk, NULL, &crtc_state->dpll)) {
8010 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8011 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008012 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008013
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +02008014 i9xx_compute_dpll(crtc, crtc_state, NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07008015
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008016 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008017}
8018
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008019static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8020 struct intel_crtc_state *crtc_state)
8021{
8022 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008023 const struct intel_limit *limit = &intel_limits_chv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008024
8025 memset(&crtc_state->dpll_hw_state, 0,
8026 sizeof(crtc_state->dpll_hw_state));
8027
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008028 if (!crtc_state->clock_set &&
8029 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8030 refclk, NULL, &crtc_state->dpll)) {
8031 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8032 return -EINVAL;
8033 }
8034
8035 chv_compute_dpll(crtc, crtc_state);
8036
8037 return 0;
8038}
8039
8040static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8041 struct intel_crtc_state *crtc_state)
8042{
8043 int refclk = 100000;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008044 const struct intel_limit *limit = &intel_limits_vlv;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008045
8046 memset(&crtc_state->dpll_hw_state, 0,
8047 sizeof(crtc_state->dpll_hw_state));
8048
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +02008049 if (!crtc_state->clock_set &&
8050 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8051 refclk, NULL, &crtc_state->dpll)) {
8052 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8053 return -EINVAL;
8054 }
8055
8056 vlv_compute_dpll(crtc, crtc_state);
8057
8058 return 0;
8059}
8060
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008061static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008062 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008063{
8064 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008065 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008066 uint32_t tmp;
8067
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008068 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8069 return;
8070
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008071 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008072 if (!(tmp & PFIT_ENABLE))
8073 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008074
Daniel Vetter06922822013-07-11 13:35:40 +02008075 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008076 if (INTEL_INFO(dev)->gen < 4) {
8077 if (crtc->pipe != PIPE_B)
8078 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008079 } else {
8080 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8081 return;
8082 }
8083
Daniel Vetter06922822013-07-11 13:35:40 +02008084 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008085 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008086}
8087
Jesse Barnesacbec812013-09-20 11:29:32 -07008088static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008089 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008090{
8091 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008092 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesacbec812013-09-20 11:29:32 -07008093 int pipe = pipe_config->cpu_transcoder;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008094 struct dpll clock;
Jesse Barnesacbec812013-09-20 11:29:32 -07008095 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008096 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008097
Ville Syrjäläb5219732016-03-15 16:40:01 +02008098 /* In case of DSI, DPLL will not be used */
8099 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
Shobhit Kumarf573de52014-07-30 20:32:37 +05308100 return;
8101
Ville Syrjäläa5805162015-05-26 20:42:30 +03008102 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008103 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008104 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008105
8106 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8107 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8108 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8109 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8110 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8111
Imre Deakdccbea32015-06-22 23:35:51 +03008112 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008113}
8114
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008115static void
8116i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8117 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008118{
8119 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008120 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008121 u32 val, base, offset;
8122 int pipe = crtc->pipe, plane = crtc->plane;
8123 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008124 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008125 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008126 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008127
Damien Lespiau42a7b082015-02-05 19:35:13 +00008128 val = I915_READ(DSPCNTR(plane));
8129 if (!(val & DISPLAY_PLANE_ENABLE))
8130 return;
8131
Damien Lespiaud9806c92015-01-21 14:07:19 +00008132 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008133 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008134 DRM_DEBUG_KMS("failed to alloc fb\n");
8135 return;
8136 }
8137
Damien Lespiau1b842c82015-01-21 13:50:54 +00008138 fb = &intel_fb->base;
8139
Daniel Vetter18c52472015-02-10 17:16:09 +00008140 if (INTEL_INFO(dev)->gen >= 4) {
8141 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008142 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008143 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8144 }
8145 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008146
8147 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008148 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008149 fb->pixel_format = fourcc;
8150 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008151
8152 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008153 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008154 offset = I915_READ(DSPTILEOFF(plane));
8155 else
8156 offset = I915_READ(DSPLINOFF(plane));
8157 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8158 } else {
8159 base = I915_READ(DSPADDR(plane));
8160 }
8161 plane_config->base = base;
8162
8163 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008164 fb->width = ((val >> 16) & 0xfff) + 1;
8165 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008166
8167 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008168 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008169
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008170 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008171 fb->pixel_format,
8172 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008173
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008174 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008175
Damien Lespiau2844a922015-01-20 12:51:48 +00008176 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8177 pipe_name(pipe), plane, fb->width, fb->height,
8178 fb->bits_per_pixel, base, fb->pitches[0],
8179 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008180
Damien Lespiau2d140302015-02-05 17:22:18 +00008181 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008182}
8183
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008184static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008185 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008186{
8187 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008188 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008189 int pipe = pipe_config->cpu_transcoder;
8190 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008191 struct dpll clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008192 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008193 int refclk = 100000;
8194
Ville Syrjäläb5219732016-03-15 16:40:01 +02008195 /* In case of DSI, DPLL will not be used */
8196 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8197 return;
8198
Ville Syrjäläa5805162015-05-26 20:42:30 +03008199 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008200 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8201 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8202 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8203 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03008204 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008205 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008206
8207 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03008208 clock.m2 = (pll_dw0 & 0xff) << 22;
8209 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8210 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008211 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8212 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8213 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8214
Imre Deakdccbea32015-06-22 23:35:51 +03008215 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008216}
8217
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008218static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008219 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008220{
8221 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008222 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02008223 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008224 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02008225 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008226
Imre Deak17290502016-02-12 18:55:11 +02008227 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8228 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +02008229 return false;
8230
Daniel Vettere143a212013-07-04 12:01:15 +02008231 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02008232 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02008233
Imre Deak17290502016-02-12 18:55:11 +02008234 ret = false;
8235
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008236 tmp = I915_READ(PIPECONF(crtc->pipe));
8237 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02008238 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008239
Wayne Boyer666a4532015-12-09 12:29:35 -08008240 if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008241 switch (tmp & PIPECONF_BPC_MASK) {
8242 case PIPECONF_6BPC:
8243 pipe_config->pipe_bpp = 18;
8244 break;
8245 case PIPECONF_8BPC:
8246 pipe_config->pipe_bpp = 24;
8247 break;
8248 case PIPECONF_10BPC:
8249 pipe_config->pipe_bpp = 30;
8250 break;
8251 default:
8252 break;
8253 }
8254 }
8255
Wayne Boyer666a4532015-12-09 12:29:35 -08008256 if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
8257 (tmp & PIPECONF_COLOR_RANGE_SELECT))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008258 pipe_config->limited_color_range = true;
8259
Ville Syrjälä282740f2013-09-04 18:30:03 +03008260 if (INTEL_INFO(dev)->gen < 4)
8261 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8262
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008263 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02008264 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008265
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008266 i9xx_get_pfit_config(crtc, pipe_config);
8267
Daniel Vetter6c49f242013-06-06 12:45:25 +02008268 if (INTEL_INFO(dev)->gen >= 4) {
Ville Syrjäläc2317752016-03-15 16:39:56 +02008269 /* No way to read it out on pipes B and C */
8270 if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
8271 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8272 else
8273 tmp = I915_READ(DPLL_MD(crtc->pipe));
Daniel Vetter6c49f242013-06-06 12:45:25 +02008274 pipe_config->pixel_multiplier =
8275 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8276 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008277 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008278 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8279 tmp = I915_READ(DPLL(crtc->pipe));
8280 pipe_config->pixel_multiplier =
8281 ((tmp & SDVO_MULTIPLIER_MASK)
8282 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8283 } else {
8284 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8285 * port and will be fixed up in the encoder->get_config
8286 * function. */
8287 pipe_config->pixel_multiplier = 1;
8288 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008289 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
Wayne Boyer666a4532015-12-09 12:29:35 -08008290 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008291 /*
8292 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8293 * on 830. Filter it out here so that we don't
8294 * report errors due to that.
8295 */
8296 if (IS_I830(dev))
8297 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8298
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008299 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8300 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008301 } else {
8302 /* Mask out read-only status bits. */
8303 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8304 DPLL_PORTC_READY_MASK |
8305 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008306 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008307
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008308 if (IS_CHERRYVIEW(dev))
8309 chv_crtc_clock_get(crtc, pipe_config);
8310 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008311 vlv_crtc_clock_get(crtc, pipe_config);
8312 else
8313 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008314
Ville Syrjälä0f646142015-08-26 19:39:18 +03008315 /*
8316 * Normally the dotclock is filled in by the encoder .get_config()
8317 * but in case the pipe is enabled w/o any ports we need a sane
8318 * default.
8319 */
8320 pipe_config->base.adjusted_mode.crtc_clock =
8321 pipe_config->port_clock / pipe_config->pixel_multiplier;
8322
Imre Deak17290502016-02-12 18:55:11 +02008323 ret = true;
8324
8325out:
8326 intel_display_power_put(dev_priv, power_domain);
8327
8328 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008329}
8330
Paulo Zanonidde86e22012-12-01 12:04:25 -02008331static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008332{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008333 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008334 struct intel_encoder *encoder;
Lyude1c1a24d2016-06-14 11:04:09 -04008335 int i;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008336 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008337 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008338 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008339 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008340 bool has_ck505 = false;
8341 bool can_ssc = false;
Lyude1c1a24d2016-06-14 11:04:09 -04008342 bool using_ssc_source = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008343
8344 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008345 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008346 switch (encoder->type) {
8347 case INTEL_OUTPUT_LVDS:
8348 has_panel = true;
8349 has_lvds = true;
8350 break;
8351 case INTEL_OUTPUT_EDP:
8352 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008353 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008354 has_cpu_edp = true;
8355 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008356 default:
8357 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008358 }
8359 }
8360
Keith Packard99eb6a02011-09-26 14:29:12 -07008361 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008362 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008363 can_ssc = has_ck505;
8364 } else {
8365 has_ck505 = false;
8366 can_ssc = true;
8367 }
8368
Lyude1c1a24d2016-06-14 11:04:09 -04008369 /* Check if any DPLLs are using the SSC source */
8370 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8371 u32 temp = I915_READ(PCH_DPLL(i));
8372
8373 if (!(temp & DPLL_VCO_ENABLE))
8374 continue;
8375
8376 if ((temp & PLL_REF_INPUT_MASK) ==
8377 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8378 using_ssc_source = true;
8379 break;
8380 }
8381 }
8382
8383 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8384 has_panel, has_lvds, has_ck505, using_ssc_source);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008385
8386 /* Ironlake: try to setup display ref clock before DPLL
8387 * enabling. This is only under driver's control after
8388 * PCH B stepping, previous chipset stepping should be
8389 * ignoring this setting.
8390 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008391 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008392
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008393 /* As we must carefully and slowly disable/enable each source in turn,
8394 * compute the final state we want first and check if we need to
8395 * make any changes at all.
8396 */
8397 final = val;
8398 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008399 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008400 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008401 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008402 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8403
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008404 final &= ~DREF_SSC_SOURCE_MASK;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008405 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Daniel Vetter8c07eb62016-06-09 18:39:07 +02008406 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008407
Keith Packard199e5d72011-09-22 12:01:57 -07008408 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008409 final |= DREF_SSC_SOURCE_ENABLE;
8410
8411 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8412 final |= DREF_SSC1_ENABLE;
8413
8414 if (has_cpu_edp) {
8415 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8416 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8417 else
8418 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8419 } else
8420 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Lyude1c1a24d2016-06-14 11:04:09 -04008421 } else if (using_ssc_source) {
8422 final |= DREF_SSC_SOURCE_ENABLE;
8423 final |= DREF_SSC1_ENABLE;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008424 }
8425
8426 if (final == val)
8427 return;
8428
8429 /* Always enable nonspread source */
8430 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8431
8432 if (has_ck505)
8433 val |= DREF_NONSPREAD_CK505_ENABLE;
8434 else
8435 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8436
8437 if (has_panel) {
8438 val &= ~DREF_SSC_SOURCE_MASK;
8439 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008440
Keith Packard199e5d72011-09-22 12:01:57 -07008441 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008442 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008443 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008444 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008445 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008446 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008447
8448 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008449 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008450 POSTING_READ(PCH_DREF_CONTROL);
8451 udelay(200);
8452
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008453 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008454
8455 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008456 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008457 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008458 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008459 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008460 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008461 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008462 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008463 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008464
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008465 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008466 POSTING_READ(PCH_DREF_CONTROL);
8467 udelay(200);
8468 } else {
Lyude1c1a24d2016-06-14 11:04:09 -04008469 DRM_DEBUG_KMS("Disabling CPU source output\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008470
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008471 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008472
8473 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008474 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008475
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008476 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008477 POSTING_READ(PCH_DREF_CONTROL);
8478 udelay(200);
8479
Lyude1c1a24d2016-06-14 11:04:09 -04008480 if (!using_ssc_source) {
8481 DRM_DEBUG_KMS("Disabling SSC source\n");
Keith Packard199e5d72011-09-22 12:01:57 -07008482
Lyude1c1a24d2016-06-14 11:04:09 -04008483 /* Turn off the SSC source */
8484 val &= ~DREF_SSC_SOURCE_MASK;
8485 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008486
Lyude1c1a24d2016-06-14 11:04:09 -04008487 /* Turn off SSC1 */
8488 val &= ~DREF_SSC1_ENABLE;
8489
8490 I915_WRITE(PCH_DREF_CONTROL, val);
8491 POSTING_READ(PCH_DREF_CONTROL);
8492 udelay(200);
8493 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07008494 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008495
8496 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008497}
8498
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008499static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008500{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008501 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008502
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008503 tmp = I915_READ(SOUTH_CHICKEN2);
8504 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8505 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008506
Imre Deakcf3598c2016-06-28 13:37:31 +03008507 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
8508 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008509 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008510
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008511 tmp = I915_READ(SOUTH_CHICKEN2);
8512 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8513 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008514
Imre Deakcf3598c2016-06-28 13:37:31 +03008515 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
8516 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008517 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008518}
8519
8520/* WaMPhyProgramming:hsw */
8521static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8522{
8523 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008524
8525 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8526 tmp &= ~(0xFF << 24);
8527 tmp |= (0x12 << 24);
8528 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8529
Paulo Zanonidde86e22012-12-01 12:04:25 -02008530 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8531 tmp |= (1 << 11);
8532 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8533
8534 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8535 tmp |= (1 << 11);
8536 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8537
Paulo Zanonidde86e22012-12-01 12:04:25 -02008538 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8539 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8540 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8541
8542 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8543 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8544 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8545
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008546 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8547 tmp &= ~(7 << 13);
8548 tmp |= (5 << 13);
8549 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008550
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008551 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8552 tmp &= ~(7 << 13);
8553 tmp |= (5 << 13);
8554 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008555
8556 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8557 tmp &= ~0xFF;
8558 tmp |= 0x1C;
8559 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8560
8561 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8562 tmp &= ~0xFF;
8563 tmp |= 0x1C;
8564 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8565
8566 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8567 tmp &= ~(0xFF << 16);
8568 tmp |= (0x1C << 16);
8569 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8570
8571 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8572 tmp &= ~(0xFF << 16);
8573 tmp |= (0x1C << 16);
8574 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8575
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008576 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8577 tmp |= (1 << 27);
8578 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008579
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008580 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8581 tmp |= (1 << 27);
8582 intel_sbi_write(dev_priv, 0x21C4, 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, 0x20EC, SBI_MPHY);
8585 tmp &= ~(0xF << 28);
8586 tmp |= (4 << 28);
8587 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008588
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008589 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8590 tmp &= ~(0xF << 28);
8591 tmp |= (4 << 28);
8592 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008593}
8594
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008595/* Implements 3 different sequences from BSpec chapter "Display iCLK
8596 * Programming" based on the parameters passed:
8597 * - Sequence to enable CLKOUT_DP
8598 * - Sequence to enable CLKOUT_DP without spread
8599 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8600 */
8601static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8602 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008603{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008604 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008605 uint32_t reg, tmp;
8606
8607 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8608 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008609 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008610 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008611
Ville Syrjäläa5805162015-05-26 20:42:30 +03008612 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008613
8614 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8615 tmp &= ~SBI_SSCCTL_DISABLE;
8616 tmp |= SBI_SSCCTL_PATHALT;
8617 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8618
8619 udelay(24);
8620
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008621 if (with_spread) {
8622 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8623 tmp &= ~SBI_SSCCTL_PATHALT;
8624 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008625
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008626 if (with_fdi) {
8627 lpt_reset_fdi_mphy(dev_priv);
8628 lpt_program_fdi_mphy(dev_priv);
8629 }
8630 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008631
Ville Syrjäläc2699522015-08-27 23:55:59 +03008632 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008633 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8634 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8635 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008636
Ville Syrjäläa5805162015-05-26 20:42:30 +03008637 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008638}
8639
Paulo Zanoni47701c32013-07-23 11:19:25 -03008640/* Sequence to disable CLKOUT_DP */
8641static void lpt_disable_clkout_dp(struct drm_device *dev)
8642{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008643 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008644 uint32_t reg, tmp;
8645
Ville Syrjäläa5805162015-05-26 20:42:30 +03008646 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008647
Ville Syrjäläc2699522015-08-27 23:55:59 +03008648 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008649 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8650 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8651 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8652
8653 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8654 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8655 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8656 tmp |= SBI_SSCCTL_PATHALT;
8657 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8658 udelay(32);
8659 }
8660 tmp |= SBI_SSCCTL_DISABLE;
8661 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8662 }
8663
Ville Syrjäläa5805162015-05-26 20:42:30 +03008664 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008665}
8666
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008667#define BEND_IDX(steps) ((50 + (steps)) / 5)
8668
8669static const uint16_t sscdivintphase[] = {
8670 [BEND_IDX( 50)] = 0x3B23,
8671 [BEND_IDX( 45)] = 0x3B23,
8672 [BEND_IDX( 40)] = 0x3C23,
8673 [BEND_IDX( 35)] = 0x3C23,
8674 [BEND_IDX( 30)] = 0x3D23,
8675 [BEND_IDX( 25)] = 0x3D23,
8676 [BEND_IDX( 20)] = 0x3E23,
8677 [BEND_IDX( 15)] = 0x3E23,
8678 [BEND_IDX( 10)] = 0x3F23,
8679 [BEND_IDX( 5)] = 0x3F23,
8680 [BEND_IDX( 0)] = 0x0025,
8681 [BEND_IDX( -5)] = 0x0025,
8682 [BEND_IDX(-10)] = 0x0125,
8683 [BEND_IDX(-15)] = 0x0125,
8684 [BEND_IDX(-20)] = 0x0225,
8685 [BEND_IDX(-25)] = 0x0225,
8686 [BEND_IDX(-30)] = 0x0325,
8687 [BEND_IDX(-35)] = 0x0325,
8688 [BEND_IDX(-40)] = 0x0425,
8689 [BEND_IDX(-45)] = 0x0425,
8690 [BEND_IDX(-50)] = 0x0525,
8691};
8692
8693/*
8694 * Bend CLKOUT_DP
8695 * steps -50 to 50 inclusive, in steps of 5
8696 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
8697 * change in clock period = -(steps / 10) * 5.787 ps
8698 */
8699static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
8700{
8701 uint32_t tmp;
8702 int idx = BEND_IDX(steps);
8703
8704 if (WARN_ON(steps % 5 != 0))
8705 return;
8706
8707 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
8708 return;
8709
8710 mutex_lock(&dev_priv->sb_lock);
8711
8712 if (steps % 10 != 0)
8713 tmp = 0xAAAAAAAB;
8714 else
8715 tmp = 0x00000000;
8716 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
8717
8718 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
8719 tmp &= 0xffff0000;
8720 tmp |= sscdivintphase[idx];
8721 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
8722
8723 mutex_unlock(&dev_priv->sb_lock);
8724}
8725
8726#undef BEND_IDX
8727
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008728static void lpt_init_pch_refclk(struct drm_device *dev)
8729{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008730 struct intel_encoder *encoder;
8731 bool has_vga = false;
8732
Damien Lespiaub2784e12014-08-05 11:29:37 +01008733 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008734 switch (encoder->type) {
8735 case INTEL_OUTPUT_ANALOG:
8736 has_vga = true;
8737 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008738 default:
8739 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008740 }
8741 }
8742
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008743 if (has_vga) {
8744 lpt_bend_clkout_dp(to_i915(dev), 0);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008745 lpt_enable_clkout_dp(dev, true, true);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008746 } else {
Paulo Zanoni47701c32013-07-23 11:19:25 -03008747 lpt_disable_clkout_dp(dev);
Ville Syrjäläf7be2c22015-12-04 22:19:39 +02008748 }
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008749}
8750
Paulo Zanonidde86e22012-12-01 12:04:25 -02008751/*
8752 * Initialize reference clocks when the driver loads
8753 */
8754void intel_init_pch_refclk(struct drm_device *dev)
8755{
8756 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8757 ironlake_init_pch_refclk(dev);
8758 else if (HAS_PCH_LPT(dev))
8759 lpt_init_pch_refclk(dev);
8760}
8761
Daniel Vetter6ff93602013-04-19 11:24:36 +02008762static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008763{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008764 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanonic8203562012-09-12 10:06:29 -03008765 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8766 int pipe = intel_crtc->pipe;
8767 uint32_t val;
8768
Daniel Vetter78114072013-06-13 00:54:57 +02008769 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008770
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008771 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008772 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008773 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008774 break;
8775 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008776 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008777 break;
8778 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008779 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008780 break;
8781 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008782 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008783 break;
8784 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008785 /* Case prevented by intel_choose_pipe_bpp_dither. */
8786 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008787 }
8788
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008789 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008790 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8791
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008792 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008793 val |= PIPECONF_INTERLACED_ILK;
8794 else
8795 val |= PIPECONF_PROGRESSIVE;
8796
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008797 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008798 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008799
Paulo Zanonic8203562012-09-12 10:06:29 -03008800 I915_WRITE(PIPECONF(pipe), val);
8801 POSTING_READ(PIPECONF(pipe));
8802}
8803
Daniel Vetter6ff93602013-04-19 11:24:36 +02008804static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008805{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008806 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008807 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008808 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jani Nikula391bf042016-03-18 17:05:40 +02008809 u32 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008810
Jani Nikula391bf042016-03-18 17:05:40 +02008811 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008812 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8813
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008814 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008815 val |= PIPECONF_INTERLACED_ILK;
8816 else
8817 val |= PIPECONF_PROGRESSIVE;
8818
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008819 I915_WRITE(PIPECONF(cpu_transcoder), val);
8820 POSTING_READ(PIPECONF(cpu_transcoder));
Jani Nikula391bf042016-03-18 17:05:40 +02008821}
8822
Jani Nikula391bf042016-03-18 17:05:40 +02008823static void haswell_set_pipemisc(struct drm_crtc *crtc)
8824{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008825 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Jani Nikula391bf042016-03-18 17:05:40 +02008826 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8827
8828 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
8829 u32 val = 0;
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008830
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008831 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008832 case 18:
8833 val |= PIPEMISC_DITHER_6_BPC;
8834 break;
8835 case 24:
8836 val |= PIPEMISC_DITHER_8_BPC;
8837 break;
8838 case 30:
8839 val |= PIPEMISC_DITHER_10_BPC;
8840 break;
8841 case 36:
8842 val |= PIPEMISC_DITHER_12_BPC;
8843 break;
8844 default:
8845 /* Case prevented by pipe_config_set_bpp. */
8846 BUG();
8847 }
8848
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008849 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008850 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8851
Jani Nikula391bf042016-03-18 17:05:40 +02008852 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008853 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008854}
8855
Paulo Zanonid4b19312012-11-29 11:29:32 -02008856int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8857{
8858 /*
8859 * Account for spread spectrum to avoid
8860 * oversubscribing the link. Max center spread
8861 * is 2.5%; use 5% for safety's sake.
8862 */
8863 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008864 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008865}
8866
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008867static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008868{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008869 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008870}
8871
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008872static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8873 struct intel_crtc_state *crtc_state,
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008874 struct dpll *reduced_clock)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008875{
8876 struct drm_crtc *crtc = &intel_crtc->base;
8877 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008878 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008879 u32 dpll, fp, fp2;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008880 int factor;
Jesse Barnes79e53942008-11-07 14:24:08 -08008881
Chris Wilsonc1858122010-12-03 21:35:48 +00008882 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008883 factor = 21;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008884 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Eric Anholt8febb292011-03-30 13:01:07 -07008885 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008886 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008887 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008888 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008889 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008890 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008891
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008892 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Chris Wilsonc1858122010-12-03 21:35:48 +00008893
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008894 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8895 fp |= FP_CB_TUNE;
8896
8897 if (reduced_clock) {
8898 fp2 = i9xx_dpll_compute_fp(reduced_clock);
8899
8900 if (reduced_clock->m < factor * reduced_clock->n)
8901 fp2 |= FP_CB_TUNE;
8902 } else {
8903 fp2 = fp;
8904 }
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008905
Chris Wilson5eddb702010-09-11 13:48:45 +01008906 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008907
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008908 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
Eric Anholta07d6782011-03-30 13:01:08 -07008909 dpll |= DPLLB_MODE_LVDS;
8910 else
8911 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008912
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008913 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008914 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008915
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008916 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8917 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008918 dpll |= DPLL_SDVO_HIGH_SPEED;
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008919
Ville Syrjälä37a56502016-06-22 21:57:04 +03008920 if (intel_crtc_has_dp_encoder(crtc_state))
Daniel Vetter4a33e482013-07-06 12:52:05 +02008921 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008922
Eric Anholta07d6782011-03-30 13:01:08 -07008923 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008924 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008925 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008926 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008927
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008928 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008929 case 5:
8930 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8931 break;
8932 case 7:
8933 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8934 break;
8935 case 10:
8936 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8937 break;
8938 case 14:
8939 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8940 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008941 }
8942
Ville Syrjälä3d6e9ee2016-06-22 21:57:03 +03008943 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8944 intel_panel_use_ssc(dev_priv))
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008945 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008946 else
8947 dpll |= PLL_REF_INPUT_DREFCLK;
8948
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02008949 dpll |= DPLL_VCO_ENABLE;
8950
8951 crtc_state->dpll_hw_state.dpll = dpll;
8952 crtc_state->dpll_hw_state.fp0 = fp;
8953 crtc_state->dpll_hw_state.fp1 = fp2;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008954}
8955
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008956static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8957 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008958{
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008959 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01008960 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +03008961 struct dpll reduced_clock;
Ander Conselvan de Oliveira7ed9f892016-03-21 18:00:07 +02008962 bool has_reduced_clock = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008963 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira1b6f4952016-05-04 12:11:59 +03008964 const struct intel_limit *limit;
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008965 int refclk = 120000;
Jesse Barnes79e53942008-11-07 14:24:08 -08008966
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008967 memset(&crtc_state->dpll_hw_state, 0,
8968 sizeof(crtc_state->dpll_hw_state));
8969
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02008970 crtc->lowfreq_avail = false;
8971
8972 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8973 if (!crtc_state->has_pch_encoder)
8974 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008975
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03008976 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008977 if (intel_panel_use_ssc(dev_priv)) {
8978 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8979 dev_priv->vbt.lvds_ssc_freq);
8980 refclk = dev_priv->vbt.lvds_ssc_freq;
8981 }
8982
8983 if (intel_is_dual_link_lvds(dev)) {
8984 if (refclk == 100000)
8985 limit = &intel_limits_ironlake_dual_lvds_100m;
8986 else
8987 limit = &intel_limits_ironlake_dual_lvds;
8988 } else {
8989 if (refclk == 100000)
8990 limit = &intel_limits_ironlake_single_lvds_100m;
8991 else
8992 limit = &intel_limits_ironlake_single_lvds;
8993 }
8994 } else {
8995 limit = &intel_limits_ironlake_dac;
8996 }
8997
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02008998 if (!crtc_state->clock_set &&
Ander Conselvan de Oliveira997c0302016-03-21 18:00:12 +02008999 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9000 refclk, NULL, &crtc_state->dpll)) {
Ander Conselvan de Oliveira364ee292016-03-21 18:00:10 +02009001 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9002 return -EINVAL;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009003 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009004
Ander Conselvan de Oliveirab75ca6f2016-03-21 18:00:11 +02009005 ironlake_compute_dpll(crtc, crtc_state,
9006 has_reduced_clock ? &reduced_clock : NULL);
Daniel Vetter66e985c2013-06-05 13:34:20 +02009007
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009008 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9009 if (pll == NULL) {
9010 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9011 pipe_name(crtc->pipe));
9012 return -EINVAL;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009013 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009014
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +03009015 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Ander Conselvan de Oliveiraded220e2016-03-21 18:00:09 +02009016 has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009017 crtc->lowfreq_avail = true;
Daniel Vettere2b78262013-06-07 23:10:03 +02009018
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009019 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009020}
9021
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009022static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9023 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009024{
9025 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009026 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009027 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009028
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009029 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9030 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9031 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9032 & ~TU_SIZE_MASK;
9033 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9034 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9035 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9036}
9037
9038static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9039 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009040 struct intel_link_m_n *m_n,
9041 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009042{
9043 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009044 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009045 enum pipe pipe = crtc->pipe;
9046
9047 if (INTEL_INFO(dev)->gen >= 5) {
9048 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9049 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9050 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9051 & ~TU_SIZE_MASK;
9052 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9053 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9054 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009055 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9056 * gen < 8) and if DRRS is supported (to make sure the
9057 * registers are not unnecessarily read).
9058 */
9059 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009060 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009061 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9062 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9063 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9064 & ~TU_SIZE_MASK;
9065 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9066 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9067 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9068 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009069 } else {
9070 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9071 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9072 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9073 & ~TU_SIZE_MASK;
9074 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9075 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9076 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9077 }
9078}
9079
9080void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009081 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009082{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009083 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009084 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9085 else
9086 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009087 &pipe_config->dp_m_n,
9088 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009089}
9090
Daniel Vetter72419202013-04-04 13:28:53 +02009091static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009092 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009093{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009094 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009095 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009096}
9097
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009098static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009099 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009100{
9101 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009102 struct drm_i915_private *dev_priv = to_i915(dev);
Chandra Kondurua1b22782015-04-07 15:28:45 -07009103 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9104 uint32_t ps_ctrl = 0;
9105 int id = -1;
9106 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009107
Chandra Kondurua1b22782015-04-07 15:28:45 -07009108 /* find scaler attached to this pipe */
9109 for (i = 0; i < crtc->num_scalers; i++) {
9110 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9111 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9112 id = i;
9113 pipe_config->pch_pfit.enabled = true;
9114 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9115 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9116 break;
9117 }
9118 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009119
Chandra Kondurua1b22782015-04-07 15:28:45 -07009120 scaler_state->scaler_id = id;
9121 if (id >= 0) {
9122 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9123 } else {
9124 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009125 }
9126}
9127
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009128static void
9129skylake_get_initial_plane_config(struct intel_crtc *crtc,
9130 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009131{
9132 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009133 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau40f46282015-02-27 11:15:21 +00009134 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009135 int pipe = crtc->pipe;
9136 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009137 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009138 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009139 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009140
Damien Lespiaud9806c92015-01-21 14:07:19 +00009141 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009142 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009143 DRM_DEBUG_KMS("failed to alloc fb\n");
9144 return;
9145 }
9146
Damien Lespiau1b842c82015-01-21 13:50:54 +00009147 fb = &intel_fb->base;
9148
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009149 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009150 if (!(val & PLANE_CTL_ENABLE))
9151 goto error;
9152
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009153 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9154 fourcc = skl_format_to_fourcc(pixel_format,
9155 val & PLANE_CTL_ORDER_RGBX,
9156 val & PLANE_CTL_ALPHA_MASK);
9157 fb->pixel_format = fourcc;
9158 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9159
Damien Lespiau40f46282015-02-27 11:15:21 +00009160 tiling = val & PLANE_CTL_TILED_MASK;
9161 switch (tiling) {
9162 case PLANE_CTL_TILED_LINEAR:
9163 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9164 break;
9165 case PLANE_CTL_TILED_X:
9166 plane_config->tiling = I915_TILING_X;
9167 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9168 break;
9169 case PLANE_CTL_TILED_Y:
9170 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9171 break;
9172 case PLANE_CTL_TILED_YF:
9173 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9174 break;
9175 default:
9176 MISSING_CASE(tiling);
9177 goto error;
9178 }
9179
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009180 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9181 plane_config->base = base;
9182
9183 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9184
9185 val = I915_READ(PLANE_SIZE(pipe, 0));
9186 fb->height = ((val >> 16) & 0xfff) + 1;
9187 fb->width = ((val >> 0) & 0x1fff) + 1;
9188
9189 val = I915_READ(PLANE_STRIDE(pipe, 0));
Ville Syrjälä7b49f942016-01-12 21:08:32 +02009190 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
Damien Lespiau40f46282015-02-27 11:15:21 +00009191 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009192 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9193
9194 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009195 fb->pixel_format,
9196 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009197
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009198 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009199
9200 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9201 pipe_name(pipe), fb->width, fb->height,
9202 fb->bits_per_pixel, base, fb->pitches[0],
9203 plane_config->size);
9204
Damien Lespiau2d140302015-02-05 17:22:18 +00009205 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009206 return;
9207
9208error:
9209 kfree(fb);
9210}
9211
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009212static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009213 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009214{
9215 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009216 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009217 uint32_t tmp;
9218
9219 tmp = I915_READ(PF_CTL(crtc->pipe));
9220
9221 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009222 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009223 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9224 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009225
9226 /* We currently do not free assignements of panel fitters on
9227 * ivb/hsw (since we don't use the higher upscaling modes which
9228 * differentiates them) so just WARN about this case for now. */
9229 if (IS_GEN7(dev)) {
9230 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9231 PF_PIPE_SEL_IVB(crtc->pipe));
9232 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009233 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009234}
9235
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009236static void
9237ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9238 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009239{
9240 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009241 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009242 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009243 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009244 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009245 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009246 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009247 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009248
Damien Lespiau42a7b082015-02-05 19:35:13 +00009249 val = I915_READ(DSPCNTR(pipe));
9250 if (!(val & DISPLAY_PLANE_ENABLE))
9251 return;
9252
Damien Lespiaud9806c92015-01-21 14:07:19 +00009253 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009254 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009255 DRM_DEBUG_KMS("failed to alloc fb\n");
9256 return;
9257 }
9258
Damien Lespiau1b842c82015-01-21 13:50:54 +00009259 fb = &intel_fb->base;
9260
Daniel Vetter18c52472015-02-10 17:16:09 +00009261 if (INTEL_INFO(dev)->gen >= 4) {
9262 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009263 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009264 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9265 }
9266 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009267
9268 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009269 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009270 fb->pixel_format = fourcc;
9271 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009272
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009273 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009274 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009275 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009276 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009277 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009278 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009279 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009280 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009281 }
9282 plane_config->base = base;
9283
9284 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009285 fb->width = ((val >> 16) & 0xfff) + 1;
9286 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009287
9288 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009289 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009290
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009291 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009292 fb->pixel_format,
9293 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009294
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009295 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009296
Damien Lespiau2844a922015-01-20 12:51:48 +00009297 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9298 pipe_name(pipe), fb->width, fb->height,
9299 fb->bits_per_pixel, base, fb->pitches[0],
9300 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009301
Damien Lespiau2d140302015-02-05 17:22:18 +00009302 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009303}
9304
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009305static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009306 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009307{
9308 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009309 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +02009310 enum intel_display_power_domain power_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009311 uint32_t tmp;
Imre Deak17290502016-02-12 18:55:11 +02009312 bool ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009313
Imre Deak17290502016-02-12 18:55:11 +02009314 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9315 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009316 return false;
9317
Daniel Vettere143a212013-07-04 12:01:15 +02009318 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009319 pipe_config->shared_dpll = NULL;
Daniel Vettereccb1402013-05-22 00:50:22 +02009320
Imre Deak17290502016-02-12 18:55:11 +02009321 ret = false;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009322 tmp = I915_READ(PIPECONF(crtc->pipe));
9323 if (!(tmp & PIPECONF_ENABLE))
Imre Deak17290502016-02-12 18:55:11 +02009324 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009325
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009326 switch (tmp & PIPECONF_BPC_MASK) {
9327 case PIPECONF_6BPC:
9328 pipe_config->pipe_bpp = 18;
9329 break;
9330 case PIPECONF_8BPC:
9331 pipe_config->pipe_bpp = 24;
9332 break;
9333 case PIPECONF_10BPC:
9334 pipe_config->pipe_bpp = 30;
9335 break;
9336 case PIPECONF_12BPC:
9337 pipe_config->pipe_bpp = 36;
9338 break;
9339 default:
9340 break;
9341 }
9342
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009343 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9344 pipe_config->limited_color_range = true;
9345
Daniel Vetterab9412b2013-05-03 11:49:46 +02009346 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009347 struct intel_shared_dpll *pll;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009348 enum intel_dpll_id pll_id;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009349
Daniel Vetter88adfff2013-03-28 10:42:01 +01009350 pipe_config->has_pch_encoder = true;
9351
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009352 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9353 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9354 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009355
9356 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009357
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03009358 if (HAS_PCH_IBX(dev_priv)) {
Imre Deakd9a7bc62016-05-12 16:18:50 +03009359 /*
9360 * The pipe->pch transcoder and pch transcoder->pll
9361 * mapping is fixed.
9362 */
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009363 pll_id = (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009364 } else {
9365 tmp = I915_READ(PCH_DPLL_SEL);
9366 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009367 pll_id = DPLL_ID_PCH_PLL_B;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009368 else
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009369 pll_id= DPLL_ID_PCH_PLL_A;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009370 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009371
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009372 pipe_config->shared_dpll =
9373 intel_get_shared_dpll_by_id(dev_priv, pll_id);
9374 pll = pipe_config->shared_dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009375
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +02009376 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
9377 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009378
9379 tmp = pipe_config->dpll_hw_state.dpll;
9380 pipe_config->pixel_multiplier =
9381 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9382 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009383
9384 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009385 } else {
9386 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009387 }
9388
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009389 intel_get_pipe_timings(crtc, pipe_config);
Jani Nikulabc58be62016-03-18 17:05:39 +02009390 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009391
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009392 ironlake_get_pfit_config(crtc, pipe_config);
9393
Imre Deak17290502016-02-12 18:55:11 +02009394 ret = true;
9395
9396out:
9397 intel_display_power_put(dev_priv, power_domain);
9398
9399 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009400}
9401
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009402static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9403{
Chris Wilson91c8a322016-07-05 10:40:23 +01009404 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009405 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009406
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009407 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009408 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009409 pipe_name(crtc->pipe));
9410
Rob Clarke2c719b2014-12-15 13:56:32 -05009411 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9412 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
Ville Syrjälä01403de2015-09-18 20:03:33 +03009413 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9414 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009415 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9416 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009417 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009418 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009419 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009420 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009421 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009422 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009423 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009424 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009425 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009426
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009427 /*
9428 * In theory we can still leave IRQs enabled, as long as only the HPD
9429 * interrupts remain enabled. We used to check for that, but since it's
9430 * gen-specific and since we only disable LCPLL after we fully disable
9431 * the interrupts, the check below should be enough.
9432 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009433 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009434}
9435
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009436static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9437{
Chris Wilson91c8a322016-07-05 10:40:23 +01009438 struct drm_device *dev = &dev_priv->drm;
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009439
9440 if (IS_HASWELL(dev))
9441 return I915_READ(D_COMP_HSW);
9442 else
9443 return I915_READ(D_COMP_BDW);
9444}
9445
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009446static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9447{
Chris Wilson91c8a322016-07-05 10:40:23 +01009448 struct drm_device *dev = &dev_priv->drm;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009449
9450 if (IS_HASWELL(dev)) {
9451 mutex_lock(&dev_priv->rps.hw_lock);
9452 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9453 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009454 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009455 mutex_unlock(&dev_priv->rps.hw_lock);
9456 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009457 I915_WRITE(D_COMP_BDW, val);
9458 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009459 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009460}
9461
9462/*
9463 * This function implements pieces of two sequences from BSpec:
9464 * - Sequence for display software to disable LCPLL
9465 * - Sequence for display software to allow package C8+
9466 * The steps implemented here are just the steps that actually touch the LCPLL
9467 * register. Callers should take care of disabling all the display engine
9468 * functions, doing the mode unset, fixing interrupts, etc.
9469 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009470static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9471 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009472{
9473 uint32_t val;
9474
9475 assert_can_disable_lcpll(dev_priv);
9476
9477 val = I915_READ(LCPLL_CTL);
9478
9479 if (switch_to_fclk) {
9480 val |= LCPLL_CD_SOURCE_FCLK;
9481 I915_WRITE(LCPLL_CTL, val);
9482
Imre Deakf53dd632016-06-28 13:37:32 +03009483 if (wait_for_us(I915_READ(LCPLL_CTL) &
9484 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009485 DRM_ERROR("Switching to FCLK failed\n");
9486
9487 val = I915_READ(LCPLL_CTL);
9488 }
9489
9490 val |= LCPLL_PLL_DISABLE;
9491 I915_WRITE(LCPLL_CTL, val);
9492 POSTING_READ(LCPLL_CTL);
9493
Chris Wilson24d84412016-06-30 15:33:07 +01009494 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009495 DRM_ERROR("LCPLL still locked\n");
9496
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009497 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009498 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009499 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009500 ndelay(100);
9501
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009502 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9503 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009504 DRM_ERROR("D_COMP RCOMP still in progress\n");
9505
9506 if (allow_power_down) {
9507 val = I915_READ(LCPLL_CTL);
9508 val |= LCPLL_POWER_DOWN_ALLOW;
9509 I915_WRITE(LCPLL_CTL, val);
9510 POSTING_READ(LCPLL_CTL);
9511 }
9512}
9513
9514/*
9515 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9516 * source.
9517 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009518static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009519{
9520 uint32_t val;
9521
9522 val = I915_READ(LCPLL_CTL);
9523
9524 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9525 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9526 return;
9527
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009528 /*
9529 * Make sure we're not on PC8 state before disabling PC8, otherwise
9530 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009531 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009532 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009533
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009534 if (val & LCPLL_POWER_DOWN_ALLOW) {
9535 val &= ~LCPLL_POWER_DOWN_ALLOW;
9536 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009537 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009538 }
9539
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009540 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009541 val |= D_COMP_COMP_FORCE;
9542 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009543 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009544
9545 val = I915_READ(LCPLL_CTL);
9546 val &= ~LCPLL_PLL_DISABLE;
9547 I915_WRITE(LCPLL_CTL, val);
9548
Chris Wilson93220c02016-06-30 15:33:08 +01009549 if (intel_wait_for_register(dev_priv,
9550 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
9551 5))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009552 DRM_ERROR("LCPLL not locked yet\n");
9553
9554 if (val & LCPLL_CD_SOURCE_FCLK) {
9555 val = I915_READ(LCPLL_CTL);
9556 val &= ~LCPLL_CD_SOURCE_FCLK;
9557 I915_WRITE(LCPLL_CTL, val);
9558
Imre Deakf53dd632016-06-28 13:37:32 +03009559 if (wait_for_us((I915_READ(LCPLL_CTL) &
9560 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009561 DRM_ERROR("Switching back to LCPLL failed\n");
9562 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009563
Mika Kuoppala59bad942015-01-16 11:34:40 +02009564 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Chris Wilson91c8a322016-07-05 10:40:23 +01009565 intel_update_cdclk(&dev_priv->drm);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009566}
9567
Paulo Zanoni765dab672014-03-07 20:08:18 -03009568/*
9569 * Package states C8 and deeper are really deep PC states that can only be
9570 * reached when all the devices on the system allow it, so even if the graphics
9571 * device allows PC8+, it doesn't mean the system will actually get to these
9572 * states. Our driver only allows PC8+ when going into runtime PM.
9573 *
9574 * The requirements for PC8+ are that all the outputs are disabled, the power
9575 * well is disabled and most interrupts are disabled, and these are also
9576 * requirements for runtime PM. When these conditions are met, we manually do
9577 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9578 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9579 * hang the machine.
9580 *
9581 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9582 * the state of some registers, so when we come back from PC8+ we need to
9583 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9584 * need to take care of the registers kept by RC6. Notice that this happens even
9585 * if we don't put the device in PCI D3 state (which is what currently happens
9586 * because of the runtime PM support).
9587 *
9588 * For more, read "Display Sequences for Package C8" on the hardware
9589 * documentation.
9590 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009591void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009592{
Chris Wilson91c8a322016-07-05 10:40:23 +01009593 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonic67a4702013-08-19 13:18:09 -03009594 uint32_t val;
9595
Paulo Zanonic67a4702013-08-19 13:18:09 -03009596 DRM_DEBUG_KMS("Enabling package C8+\n");
9597
Ville Syrjäläc2699522015-08-27 23:55:59 +03009598 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009599 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9600 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9601 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9602 }
9603
9604 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009605 hsw_disable_lcpll(dev_priv, true, true);
9606}
9607
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009608void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009609{
Chris Wilson91c8a322016-07-05 10:40:23 +01009610 struct drm_device *dev = &dev_priv->drm;
Paulo Zanonic67a4702013-08-19 13:18:09 -03009611 uint32_t val;
9612
Paulo Zanonic67a4702013-08-19 13:18:09 -03009613 DRM_DEBUG_KMS("Disabling package C8+\n");
9614
9615 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009616 lpt_init_pch_refclk(dev);
9617
Ville Syrjäläc2699522015-08-27 23:55:59 +03009618 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009619 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9620 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9621 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9622 }
Paulo Zanonic67a4702013-08-19 13:18:09 -03009623}
9624
Imre Deak324513c2016-06-13 16:44:36 +03009625static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309626{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009627 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009628 struct intel_atomic_state *old_intel_state =
9629 to_intel_atomic_state(old_state);
9630 unsigned int req_cdclk = old_intel_state->dev_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309631
Imre Deak324513c2016-06-13 16:44:36 +03009632 bxt_set_cdclk(to_i915(dev), req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309633}
9634
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009635/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009636static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009637{
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009638 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +01009639 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009640 struct drm_crtc *crtc;
9641 struct drm_crtc_state *cstate;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009642 struct intel_crtc_state *crtc_state;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009643 unsigned max_pixel_rate = 0, i;
9644 enum pipe pipe;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009645
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009646 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
9647 sizeof(intel_state->min_pixclk));
9648
9649 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009650 int pixel_rate;
9651
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009652 crtc_state = to_intel_crtc_state(cstate);
9653 if (!crtc_state->base.enable) {
9654 intel_state->min_pixclk[i] = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009655 continue;
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009656 }
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009657
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009658 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009659
9660 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009661 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009662 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9663
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009664 intel_state->min_pixclk[i] = pixel_rate;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009665 }
9666
Maarten Lankhorst565602d2015-12-10 12:33:57 +01009667 for_each_pipe(dev_priv, pipe)
9668 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
9669
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009670 return max_pixel_rate;
9671}
9672
9673static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9674{
Chris Wilsonfac5e232016-07-04 11:34:36 +01009675 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009676 uint32_t val, data;
9677 int ret;
9678
9679 if (WARN((I915_READ(LCPLL_CTL) &
9680 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9681 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9682 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9683 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9684 "trying to change cdclk frequency with cdclk not enabled\n"))
9685 return;
9686
9687 mutex_lock(&dev_priv->rps.hw_lock);
9688 ret = sandybridge_pcode_write(dev_priv,
9689 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9690 mutex_unlock(&dev_priv->rps.hw_lock);
9691 if (ret) {
9692 DRM_ERROR("failed to inform pcode about cdclk change\n");
9693 return;
9694 }
9695
9696 val = I915_READ(LCPLL_CTL);
9697 val |= LCPLL_CD_SOURCE_FCLK;
9698 I915_WRITE(LCPLL_CTL, val);
9699
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009700 if (wait_for_us(I915_READ(LCPLL_CTL) &
9701 LCPLL_CD_SOURCE_FCLK_DONE, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009702 DRM_ERROR("Switching to FCLK failed\n");
9703
9704 val = I915_READ(LCPLL_CTL);
9705 val &= ~LCPLL_CLK_FREQ_MASK;
9706
9707 switch (cdclk) {
9708 case 450000:
9709 val |= LCPLL_CLK_FREQ_450;
9710 data = 0;
9711 break;
9712 case 540000:
9713 val |= LCPLL_CLK_FREQ_54O_BDW;
9714 data = 1;
9715 break;
9716 case 337500:
9717 val |= LCPLL_CLK_FREQ_337_5_BDW;
9718 data = 2;
9719 break;
9720 case 675000:
9721 val |= LCPLL_CLK_FREQ_675_BDW;
9722 data = 3;
9723 break;
9724 default:
9725 WARN(1, "invalid cdclk frequency\n");
9726 return;
9727 }
9728
9729 I915_WRITE(LCPLL_CTL, val);
9730
9731 val = I915_READ(LCPLL_CTL);
9732 val &= ~LCPLL_CD_SOURCE_FCLK;
9733 I915_WRITE(LCPLL_CTL, val);
9734
Tvrtko Ursulin5ba00172016-03-03 14:36:45 +00009735 if (wait_for_us((I915_READ(LCPLL_CTL) &
9736 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009737 DRM_ERROR("Switching back to LCPLL failed\n");
9738
9739 mutex_lock(&dev_priv->rps.hw_lock);
9740 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9741 mutex_unlock(&dev_priv->rps.hw_lock);
9742
Ville Syrjälä7f1052a2016-04-26 19:46:32 +03009743 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
9744
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009745 intel_update_cdclk(dev);
9746
9747 WARN(cdclk != dev_priv->cdclk_freq,
9748 "cdclk requested %d kHz but got %d kHz\n",
9749 cdclk, dev_priv->cdclk_freq);
9750}
9751
Ville Syrjälä587c7912016-05-11 22:44:41 +03009752static int broadwell_calc_cdclk(int max_pixclk)
9753{
9754 if (max_pixclk > 540000)
9755 return 675000;
9756 else if (max_pixclk > 450000)
9757 return 540000;
9758 else if (max_pixclk > 337500)
9759 return 450000;
9760 else
9761 return 337500;
9762}
9763
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009764static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009765{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009766 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009767 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009768 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009769 int cdclk;
9770
9771 /*
9772 * FIXME should also account for plane ratio
9773 * once 64bpp pixel formats are supported.
9774 */
Ville Syrjälä587c7912016-05-11 22:44:41 +03009775 cdclk = broadwell_calc_cdclk(max_pixclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009776
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009777 if (cdclk > dev_priv->max_cdclk_freq) {
Maarten Lankhorst63ba5342015-11-24 11:29:03 +01009778 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9779 cdclk, dev_priv->max_cdclk_freq);
9780 return -EINVAL;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009781 }
9782
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009783 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9784 if (!intel_state->active_crtcs)
Ville Syrjälä587c7912016-05-11 22:44:41 +03009785 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009786
9787 return 0;
9788}
9789
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009790static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009791{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009792 struct drm_device *dev = old_state->dev;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +01009793 struct intel_atomic_state *old_intel_state =
9794 to_intel_atomic_state(old_state);
9795 unsigned req_cdclk = old_intel_state->dev_cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009796
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009797 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009798}
9799
Clint Taylorc89e39f2016-05-13 23:41:21 +03009800static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
9801{
9802 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
9803 struct drm_i915_private *dev_priv = to_i915(state->dev);
9804 const int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009805 int vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009806 int cdclk;
9807
9808 /*
9809 * FIXME should also account for plane ratio
9810 * once 64bpp pixel formats are supported.
9811 */
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009812 cdclk = skl_calc_cdclk(max_pixclk, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009813
9814 /*
9815 * FIXME move the cdclk caclulation to
9816 * compute_config() so we can fail gracegully.
9817 */
9818 if (cdclk > dev_priv->max_cdclk_freq) {
9819 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9820 cdclk, dev_priv->max_cdclk_freq);
9821 cdclk = dev_priv->max_cdclk_freq;
9822 }
9823
9824 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
9825 if (!intel_state->active_crtcs)
Ville Syrjäläa8ca4932016-05-13 23:41:23 +03009826 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009827
9828 return 0;
9829}
9830
9831static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9832{
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009833 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
9834 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
9835 unsigned int req_cdclk = intel_state->dev_cdclk;
9836 unsigned int req_vco = intel_state->cdclk_pll_vco;
Clint Taylorc89e39f2016-05-13 23:41:21 +03009837
Ville Syrjälä1cd593e2016-05-13 23:41:26 +03009838 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
Clint Taylorc89e39f2016-05-13 23:41:21 +03009839}
9840
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009841static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9842 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009843{
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +03009844 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
Mika Kaholaaf3997b2016-02-05 13:29:28 +02009845 if (!intel_ddi_pll_select(crtc, crtc_state))
9846 return -EINVAL;
9847 }
Daniel Vetter716c2e52014-06-25 22:02:02 +03009848
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009849 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009850
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009851 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009852}
9853
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309854static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9855 enum port port,
9856 struct intel_crtc_state *pipe_config)
9857{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009858 enum intel_dpll_id id;
9859
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309860 switch (port) {
9861 case PORT_A:
9862 pipe_config->ddi_pll_sel = SKL_DPLL0;
Imre Deak08250c42016-03-14 19:55:34 +02009863 id = DPLL_ID_SKL_DPLL0;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309864 break;
9865 case PORT_B:
9866 pipe_config->ddi_pll_sel = SKL_DPLL1;
Imre Deak08250c42016-03-14 19:55:34 +02009867 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309868 break;
9869 case PORT_C:
9870 pipe_config->ddi_pll_sel = SKL_DPLL2;
Imre Deak08250c42016-03-14 19:55:34 +02009871 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309872 break;
9873 default:
9874 DRM_ERROR("Incorrect port type\n");
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009875 return;
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309876 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009877
9878 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309879}
9880
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009881static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9882 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009883 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009884{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009885 enum intel_dpll_id id;
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009886 u32 temp;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009887
9888 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9889 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9890
9891 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009892 case SKL_DPLL0:
Ander Conselvan de Oliveiraa3c988e2016-03-08 17:46:27 +02009893 id = DPLL_ID_SKL_DPLL0;
9894 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009895 case SKL_DPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009896 id = DPLL_ID_SKL_DPLL1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009897 break;
9898 case SKL_DPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009899 id = DPLL_ID_SKL_DPLL2;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009900 break;
9901 case SKL_DPLL3:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009902 id = DPLL_ID_SKL_DPLL3;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009903 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009904 default:
9905 MISSING_CASE(pipe_config->ddi_pll_sel);
9906 return;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009907 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009908
9909 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009910}
9911
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009912static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9913 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009914 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009915{
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009916 enum intel_dpll_id id;
9917
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009918 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9919
9920 switch (pipe_config->ddi_pll_sel) {
9921 case PORT_CLK_SEL_WRPLL1:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009922 id = DPLL_ID_WRPLL1;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009923 break;
9924 case PORT_CLK_SEL_WRPLL2:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009925 id = DPLL_ID_WRPLL2;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009926 break;
Maarten Lankhorst00490c22015-11-16 14:42:12 +01009927 case PORT_CLK_SEL_SPLL:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009928 id = DPLL_ID_SPLL;
Ville Syrjälä79bd23d2015-12-01 23:32:07 +02009929 break;
Ander Conselvan de Oliveira9d16da62016-03-08 17:46:26 +02009930 case PORT_CLK_SEL_LCPLL_810:
9931 id = DPLL_ID_LCPLL_810;
9932 break;
9933 case PORT_CLK_SEL_LCPLL_1350:
9934 id = DPLL_ID_LCPLL_1350;
9935 break;
9936 case PORT_CLK_SEL_LCPLL_2700:
9937 id = DPLL_ID_LCPLL_2700;
9938 break;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009939 default:
9940 MISSING_CASE(pipe_config->ddi_pll_sel);
9941 /* fall through */
9942 case PORT_CLK_SEL_NONE:
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009943 return;
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009944 }
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +02009945
9946 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009947}
9948
Jani Nikulacf304292016-03-18 17:05:41 +02009949static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
9950 struct intel_crtc_state *pipe_config,
9951 unsigned long *power_domain_mask)
9952{
9953 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01009954 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikulacf304292016-03-18 17:05:41 +02009955 enum intel_display_power_domain power_domain;
9956 u32 tmp;
9957
Imre Deakd9a7bc62016-05-12 16:18:50 +03009958 /*
9959 * The pipe->transcoder mapping is fixed with the exception of the eDP
9960 * transcoder handled below.
9961 */
Jani Nikulacf304292016-03-18 17:05:41 +02009962 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9963
9964 /*
9965 * XXX: Do intel_display_power_get_if_enabled before reading this (for
9966 * consistency and less surprising code; it's in always on power).
9967 */
9968 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9969 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9970 enum pipe trans_edp_pipe;
9971 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9972 default:
9973 WARN(1, "unknown pipe linked to edp transcoder\n");
9974 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9975 case TRANS_DDI_EDP_INPUT_A_ON:
9976 trans_edp_pipe = PIPE_A;
9977 break;
9978 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9979 trans_edp_pipe = PIPE_B;
9980 break;
9981 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9982 trans_edp_pipe = PIPE_C;
9983 break;
9984 }
9985
9986 if (trans_edp_pipe == crtc->pipe)
9987 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9988 }
9989
9990 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
9991 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
9992 return false;
9993 *power_domain_mask |= BIT(power_domain);
9994
9995 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9996
9997 return tmp & PIPECONF_ENABLE;
9998}
9999
Jani Nikula4d1de972016-03-18 17:05:42 +020010000static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10001 struct intel_crtc_state *pipe_config,
10002 unsigned long *power_domain_mask)
10003{
10004 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010005 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020010006 enum intel_display_power_domain power_domain;
10007 enum port port;
10008 enum transcoder cpu_transcoder;
10009 u32 tmp;
10010
Jani Nikula4d1de972016-03-18 17:05:42 +020010011 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10012 if (port == PORT_A)
10013 cpu_transcoder = TRANSCODER_DSI_A;
10014 else
10015 cpu_transcoder = TRANSCODER_DSI_C;
10016
10017 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10018 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10019 continue;
10020 *power_domain_mask |= BIT(power_domain);
10021
Imre Deakdb18b6a2016-03-24 12:41:40 +020010022 /*
10023 * The PLL needs to be enabled with a valid divider
10024 * configuration, otherwise accessing DSI registers will hang
10025 * the machine. See BSpec North Display Engine
10026 * registers/MIPI[BXT]. We can break out here early, since we
10027 * need the same DSI PLL to be enabled for both DSI ports.
10028 */
10029 if (!intel_dsi_pll_is_enabled(dev_priv))
10030 break;
10031
Jani Nikula4d1de972016-03-18 17:05:42 +020010032 /* XXX: this works for video mode only */
10033 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10034 if (!(tmp & DPI_ENABLE))
10035 continue;
10036
10037 tmp = I915_READ(MIPI_CTRL(port));
10038 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10039 continue;
10040
10041 pipe_config->cpu_transcoder = cpu_transcoder;
Jani Nikula4d1de972016-03-18 17:05:42 +020010042 break;
10043 }
10044
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010045 return transcoder_is_dsi(pipe_config->cpu_transcoder);
Jani Nikula4d1de972016-03-18 17:05:42 +020010046}
10047
Daniel Vetter26804af2014-06-25 22:01:55 +030010048static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010049 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +030010050{
10051 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010052 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010053 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +030010054 enum port port;
10055 uint32_t tmp;
10056
10057 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10058
10059 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10060
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070010061 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010062 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +053010063 else if (IS_BROXTON(dev))
10064 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +000010065 else
10066 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +030010067
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010068 pll = pipe_config->shared_dpll;
10069 if (pll) {
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020010070 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10071 &pipe_config->dpll_hw_state));
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010072 }
10073
Daniel Vetter26804af2014-06-25 22:01:55 +030010074 /*
10075 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10076 * DDI E. So just check whether this pipe is wired to DDI E and whether
10077 * the PCH transcoder is on.
10078 */
Damien Lespiauca370452013-12-03 13:56:24 +000010079 if (INTEL_INFO(dev)->gen < 9 &&
10080 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +030010081 pipe_config->has_pch_encoder = true;
10082
10083 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10084 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10085 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10086
10087 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10088 }
10089}
10090
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010091static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010092 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010093{
10094 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010095 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak17290502016-02-12 18:55:11 +020010096 enum intel_display_power_domain power_domain;
10097 unsigned long power_domain_mask;
Jani Nikulacf304292016-03-18 17:05:41 +020010098 bool active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010099
Imre Deak17290502016-02-12 18:55:11 +020010100 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10101 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Imre Deakb5482bd2014-03-05 16:20:55 +020010102 return false;
Imre Deak17290502016-02-12 18:55:11 +020010103 power_domain_mask = BIT(power_domain);
10104
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020010105 pipe_config->shared_dpll = NULL;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010106
Jani Nikulacf304292016-03-18 17:05:41 +020010107 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
Daniel Vettereccb1402013-05-22 00:50:22 +020010108
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010109 if (IS_BROXTON(dev_priv) &&
10110 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10111 WARN_ON(active);
10112 active = true;
Jani Nikula4d1de972016-03-18 17:05:42 +020010113 }
10114
Jani Nikulacf304292016-03-18 17:05:41 +020010115 if (!active)
Imre Deak17290502016-02-12 18:55:11 +020010116 goto out;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010117
Ville Syrjäläd7edc4e2016-06-22 21:57:07 +030010118 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Jani Nikula4d1de972016-03-18 17:05:42 +020010119 haswell_get_ddi_port_state(crtc, pipe_config);
10120 intel_get_pipe_timings(crtc, pipe_config);
10121 }
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010122
Jani Nikulabc58be62016-03-18 17:05:39 +020010123 intel_get_pipe_src_size(crtc, pipe_config);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010124
Lionel Landwerlin05dc6982016-03-16 10:57:15 +000010125 pipe_config->gamma_mode =
10126 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10127
Chandra Kondurua1b22782015-04-07 15:28:45 -070010128 if (INTEL_INFO(dev)->gen >= 9) {
10129 skl_init_scalers(dev, crtc, pipe_config);
10130 }
10131
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070010132 if (INTEL_INFO(dev)->gen >= 9) {
10133 pipe_config->scaler_state.scaler_id = -1;
10134 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10135 }
10136
Imre Deak17290502016-02-12 18:55:11 +020010137 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10138 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10139 power_domain_mask |= BIT(power_domain);
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010140 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010141 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010142 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -070010143 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010144 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010145
Jesse Barnese59150d2014-01-07 13:30:45 -080010146 if (IS_HASWELL(dev))
10147 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10148 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010149
Jani Nikula4d1de972016-03-18 17:05:42 +020010150 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10151 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
Clint Taylorebb69c92014-09-30 10:30:22 -070010152 pipe_config->pixel_multiplier =
10153 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10154 } else {
10155 pipe_config->pixel_multiplier = 1;
10156 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010157
Imre Deak17290502016-02-12 18:55:11 +020010158out:
10159 for_each_power_domain(power_domain, power_domain_mask)
10160 intel_display_power_put(dev_priv, power_domain);
10161
Jani Nikulacf304292016-03-18 17:05:41 +020010162 return active;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010163}
10164
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010165static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10166 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010167{
10168 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010169 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010170 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010171 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010172
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010173 if (plane_state && plane_state->visible) {
10174 unsigned int width = plane_state->base.crtc_w;
10175 unsigned int height = plane_state->base.crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010176 unsigned int stride = roundup_pow_of_two(width) * 4;
10177
10178 switch (stride) {
10179 default:
10180 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10181 width, stride);
10182 stride = 256;
10183 /* fallthrough */
10184 case 256:
10185 case 512:
10186 case 1024:
10187 case 2048:
10188 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010189 }
10190
Ville Syrjälädc41c152014-08-13 11:57:05 +030010191 cntl |= CURSOR_ENABLE |
10192 CURSOR_GAMMA_ENABLE |
10193 CURSOR_FORMAT_ARGB |
10194 CURSOR_STRIDE(stride);
10195
10196 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010197 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010198
Ville Syrjälädc41c152014-08-13 11:57:05 +030010199 if (intel_crtc->cursor_cntl != 0 &&
10200 (intel_crtc->cursor_base != base ||
10201 intel_crtc->cursor_size != size ||
10202 intel_crtc->cursor_cntl != cntl)) {
10203 /* On these chipsets we can only modify the base/size/stride
10204 * whilst the cursor is disabled.
10205 */
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010206 I915_WRITE(CURCNTR(PIPE_A), 0);
10207 POSTING_READ(CURCNTR(PIPE_A));
Ville Syrjälädc41c152014-08-13 11:57:05 +030010208 intel_crtc->cursor_cntl = 0;
10209 }
10210
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010211 if (intel_crtc->cursor_base != base) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010212 I915_WRITE(CURBASE(PIPE_A), base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010213 intel_crtc->cursor_base = base;
10214 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010215
10216 if (intel_crtc->cursor_size != size) {
10217 I915_WRITE(CURSIZE, size);
10218 intel_crtc->cursor_size = size;
10219 }
10220
Chris Wilson4b0e3332014-05-30 16:35:26 +030010221 if (intel_crtc->cursor_cntl != cntl) {
Ville Syrjälä0b87c242015-09-22 19:47:51 +030010222 I915_WRITE(CURCNTR(PIPE_A), cntl);
10223 POSTING_READ(CURCNTR(PIPE_A));
Chris Wilson4b0e3332014-05-30 16:35:26 +030010224 intel_crtc->cursor_cntl = cntl;
10225 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010226}
10227
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010228static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10229 const struct intel_plane_state *plane_state)
Chris Wilson560b85b2010-08-07 11:01:38 +010010230{
10231 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010232 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson560b85b2010-08-07 11:01:38 +010010233 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10234 int pipe = intel_crtc->pipe;
Ville Syrjälä663f3122015-12-14 13:16:48 +020010235 uint32_t cntl = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010236
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010237 if (plane_state && plane_state->visible) {
Chris Wilson4b0e3332014-05-30 16:35:26 +030010238 cntl = MCURSOR_GAMMA_ENABLE;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010239 switch (plane_state->base.crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010240 case 64:
10241 cntl |= CURSOR_MODE_64_ARGB_AX;
10242 break;
10243 case 128:
10244 cntl |= CURSOR_MODE_128_ARGB_AX;
10245 break;
10246 case 256:
10247 cntl |= CURSOR_MODE_256_ARGB_AX;
10248 break;
10249 default:
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010250 MISSING_CASE(plane_state->base.crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010251 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010252 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010253 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010254
Bob Paauwefc6f93b2015-08-31 14:03:30 -070010255 if (HAS_DDI(dev))
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010256 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010257
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010258 if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
10259 cntl |= CURSOR_ROTATE_180;
10260 }
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010261
Chris Wilson4b0e3332014-05-30 16:35:26 +030010262 if (intel_crtc->cursor_cntl != cntl) {
10263 I915_WRITE(CURCNTR(pipe), cntl);
10264 POSTING_READ(CURCNTR(pipe));
10265 intel_crtc->cursor_cntl = cntl;
10266 }
10267
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010268 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010269 I915_WRITE(CURBASE(pipe), base);
10270 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010271
10272 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010273}
10274
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010275/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010276static void intel_crtc_update_cursor(struct drm_crtc *crtc,
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010277 const struct intel_plane_state *plane_state)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010278{
10279 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010280 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010281 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10282 int pipe = intel_crtc->pipe;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010283 u32 base = intel_crtc->cursor_addr;
10284 u32 pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010285
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010286 if (plane_state) {
10287 int x = plane_state->base.crtc_x;
10288 int y = plane_state->base.crtc_y;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010289
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010290 if (x < 0) {
10291 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10292 x = -x;
10293 }
10294 pos |= x << CURSOR_X_SHIFT;
10295
10296 if (y < 0) {
10297 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10298 y = -y;
10299 }
10300 pos |= y << CURSOR_Y_SHIFT;
10301
10302 /* ILK+ do this automagically */
10303 if (HAS_GMCH_DISPLAY(dev) &&
10304 plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
10305 base += (plane_state->base.crtc_h *
10306 plane_state->base.crtc_w - 1) * 4;
10307 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010308 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010309
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010310 I915_WRITE(CURPOS(pipe), pos);
10311
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010312 if (IS_845G(dev) || IS_I865G(dev))
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010313 i845_update_cursor(crtc, base, plane_state);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010314 else
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010010315 i9xx_update_cursor(crtc, base, plane_state);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010316}
10317
Ville Syrjälädc41c152014-08-13 11:57:05 +030010318static bool cursor_size_ok(struct drm_device *dev,
10319 uint32_t width, uint32_t height)
10320{
10321 if (width == 0 || height == 0)
10322 return false;
10323
10324 /*
10325 * 845g/865g are special in that they are only limited by
10326 * the width of their cursors, the height is arbitrary up to
10327 * the precision of the register. Everything else requires
10328 * square cursors, limited to a few power-of-two sizes.
10329 */
10330 if (IS_845G(dev) || IS_I865G(dev)) {
10331 if ((width & 63) != 0)
10332 return false;
10333
10334 if (width > (IS_845G(dev) ? 64 : 512))
10335 return false;
10336
10337 if (height > 1023)
10338 return false;
10339 } else {
10340 switch (width | height) {
10341 case 256:
10342 case 128:
10343 if (IS_GEN2(dev))
10344 return false;
10345 case 64:
10346 break;
10347 default:
10348 return false;
10349 }
10350 }
10351
10352 return true;
10353}
10354
Jesse Barnes79e53942008-11-07 14:24:08 -080010355/* VESA 640x480x72Hz mode to set on the pipe */
10356static struct drm_display_mode load_detect_mode = {
10357 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10358 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10359};
10360
Daniel Vettera8bb6812014-02-10 18:00:39 +010010361struct drm_framebuffer *
10362__intel_framebuffer_create(struct drm_device *dev,
10363 struct drm_mode_fb_cmd2 *mode_cmd,
10364 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010365{
10366 struct intel_framebuffer *intel_fb;
10367 int ret;
10368
10369 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010370 if (!intel_fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010371 return ERR_PTR(-ENOMEM);
Chris Wilsond2dff872011-04-19 08:36:26 +010010372
10373 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010374 if (ret)
10375 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010376
10377 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010378
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010379err:
10380 kfree(intel_fb);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010381 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010382}
10383
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010384static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010385intel_framebuffer_create(struct drm_device *dev,
10386 struct drm_mode_fb_cmd2 *mode_cmd,
10387 struct drm_i915_gem_object *obj)
10388{
10389 struct drm_framebuffer *fb;
10390 int ret;
10391
10392 ret = i915_mutex_lock_interruptible(dev);
10393 if (ret)
10394 return ERR_PTR(ret);
10395 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10396 mutex_unlock(&dev->struct_mutex);
10397
10398 return fb;
10399}
10400
Chris Wilsond2dff872011-04-19 08:36:26 +010010401static u32
10402intel_framebuffer_pitch_for_width(int width, int bpp)
10403{
10404 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10405 return ALIGN(pitch, 64);
10406}
10407
10408static u32
10409intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10410{
10411 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010412 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010413}
10414
10415static struct drm_framebuffer *
10416intel_framebuffer_create_for_mode(struct drm_device *dev,
10417 struct drm_display_mode *mode,
10418 int depth, int bpp)
10419{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010420 struct drm_framebuffer *fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010421 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010422 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010423
Dave Gordond37cd8a2016-04-22 19:14:32 +010010424 obj = i915_gem_object_create(dev,
Chris Wilsond2dff872011-04-19 08:36:26 +010010425 intel_framebuffer_size_for_mode(mode, bpp));
Chris Wilsonfe3db792016-04-25 13:32:13 +010010426 if (IS_ERR(obj))
10427 return ERR_CAST(obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010428
10429 mode_cmd.width = mode->hdisplay;
10430 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010431 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10432 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010433 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010434
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010435 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
10436 if (IS_ERR(fb))
Chris Wilson34911fd2016-07-20 13:31:54 +010010437 i915_gem_object_put_unlocked(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020010438
10439 return fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010440}
10441
10442static struct drm_framebuffer *
10443mode_fits_in_fbdev(struct drm_device *dev,
10444 struct drm_display_mode *mode)
10445{
Daniel Vetter06957262015-08-10 13:34:08 +020010446#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsonfac5e232016-07-04 11:34:36 +010010447 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsond2dff872011-04-19 08:36:26 +010010448 struct drm_i915_gem_object *obj;
10449 struct drm_framebuffer *fb;
10450
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010451 if (!dev_priv->fbdev)
10452 return NULL;
10453
10454 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010455 return NULL;
10456
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010457 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010458 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010459
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010460 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010461 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10462 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010463 return NULL;
10464
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010465 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010466 return NULL;
10467
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010468 drm_framebuffer_reference(fb);
Chris Wilsond2dff872011-04-19 08:36:26 +010010469 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010470#else
10471 return NULL;
10472#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010473}
10474
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010475static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10476 struct drm_crtc *crtc,
10477 struct drm_display_mode *mode,
10478 struct drm_framebuffer *fb,
10479 int x, int y)
10480{
10481 struct drm_plane_state *plane_state;
10482 int hdisplay, vdisplay;
10483 int ret;
10484
10485 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10486 if (IS_ERR(plane_state))
10487 return PTR_ERR(plane_state);
10488
10489 if (mode)
10490 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10491 else
10492 hdisplay = vdisplay = 0;
10493
10494 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10495 if (ret)
10496 return ret;
10497 drm_atomic_set_fb_for_plane(plane_state, fb);
10498 plane_state->crtc_x = 0;
10499 plane_state->crtc_y = 0;
10500 plane_state->crtc_w = hdisplay;
10501 plane_state->crtc_h = vdisplay;
10502 plane_state->src_x = x << 16;
10503 plane_state->src_y = y << 16;
10504 plane_state->src_w = hdisplay << 16;
10505 plane_state->src_h = vdisplay << 16;
10506
10507 return 0;
10508}
10509
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010510bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010511 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010512 struct intel_load_detect_pipe *old,
10513 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010514{
10515 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010516 struct intel_encoder *intel_encoder =
10517 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010518 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010519 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010520 struct drm_crtc *crtc = NULL;
10521 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010522 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010523 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010524 struct drm_atomic_state *state = NULL, *restore_state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010525 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010526 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010527 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010528
Chris Wilsond2dff872011-04-19 08:36:26 +010010529 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010530 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010531 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010532
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010533 old->restore_state = NULL;
10534
Rob Clark51fd3712013-11-19 12:10:12 -050010535retry:
10536 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10537 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010538 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010539
Jesse Barnes79e53942008-11-07 14:24:08 -080010540 /*
10541 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010542 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010543 * - if the connector already has an assigned crtc, use it (but make
10544 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010545 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010546 * - try to find the first unused crtc that can drive this connector,
10547 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010548 */
10549
10550 /* See if we already have a CRTC for this connector */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010551 if (connector->state->crtc) {
10552 crtc = connector->state->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010553
Rob Clark51fd3712013-11-19 12:10:12 -050010554 ret = drm_modeset_lock(&crtc->mutex, ctx);
10555 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010556 goto fail;
Chris Wilson8261b192011-04-19 23:18:09 +010010557
10558 /* Make sure the crtc and connector are running */
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010559 goto found;
Jesse Barnes79e53942008-11-07 14:24:08 -080010560 }
10561
10562 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010563 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010564 i++;
10565 if (!(encoder->possible_crtcs & (1 << i)))
10566 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010567
10568 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
10569 if (ret)
10570 goto fail;
10571
10572 if (possible_crtc->state->enable) {
10573 drm_modeset_unlock(&possible_crtc->mutex);
Ville Syrjäläa4592492014-08-11 13:15:36 +030010574 continue;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010575 }
Ville Syrjäläa4592492014-08-11 13:15:36 +030010576
10577 crtc = possible_crtc;
10578 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010579 }
10580
10581 /*
10582 * If we didn't find an unused CRTC, don't use any.
10583 */
10584 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010585 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010586 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010587 }
10588
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010589found:
10590 intel_crtc = to_intel_crtc(crtc);
10591
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010592 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10593 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010594 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010595
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010596 state = drm_atomic_state_alloc(dev);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010597 restore_state = drm_atomic_state_alloc(dev);
10598 if (!state || !restore_state) {
10599 ret = -ENOMEM;
10600 goto fail;
10601 }
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010602
10603 state->acquire_ctx = ctx;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010604 restore_state->acquire_ctx = ctx;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010605
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010606 connector_state = drm_atomic_get_connector_state(state, connector);
10607 if (IS_ERR(connector_state)) {
10608 ret = PTR_ERR(connector_state);
10609 goto fail;
10610 }
10611
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010612 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
10613 if (ret)
10614 goto fail;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010615
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010616 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10617 if (IS_ERR(crtc_state)) {
10618 ret = PTR_ERR(crtc_state);
10619 goto fail;
10620 }
10621
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010622 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010623
Chris Wilson64927112011-04-20 07:25:26 +010010624 if (!mode)
10625 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010626
Chris Wilsond2dff872011-04-19 08:36:26 +010010627 /* We need a framebuffer large enough to accommodate all accesses
10628 * that the plane may generate whilst we perform load detection.
10629 * We can not rely on the fbcon either being present (we get called
10630 * during its initialisation to detect all boot displays, or it may
10631 * not even exist) or that it is large enough to satisfy the
10632 * requested mode.
10633 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010634 fb = mode_fits_in_fbdev(dev, mode);
10635 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010636 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010637 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
Chris Wilsond2dff872011-04-19 08:36:26 +010010638 } else
10639 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010640 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010641 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010642 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010643 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010644
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010645 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10646 if (ret)
10647 goto fail;
10648
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010649 drm_framebuffer_unreference(fb);
10650
10651 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
10652 if (ret)
10653 goto fail;
10654
10655 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
10656 if (!ret)
10657 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
10658 if (!ret)
10659 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
10660 if (ret) {
10661 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
10662 goto fail;
10663 }
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010664
Maarten Lankhorst3ba86072016-02-29 09:18:57 +010010665 ret = drm_atomic_commit(state);
10666 if (ret) {
Chris Wilson64927112011-04-20 07:25:26 +010010667 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010668 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010669 }
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010670
10671 old->restore_state = restore_state;
Chris Wilson71731882011-04-19 23:10:58 +010010672
Jesse Barnes79e53942008-11-07 14:24:08 -080010673 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010674 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010675 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010676
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010677fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010678 drm_atomic_state_free(state);
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010679 drm_atomic_state_free(restore_state);
10680 restore_state = state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010681
Rob Clark51fd3712013-11-19 12:10:12 -050010682 if (ret == -EDEADLK) {
10683 drm_modeset_backoff(ctx);
10684 goto retry;
10685 }
10686
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010687 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010688}
10689
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010690void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010691 struct intel_load_detect_pipe *old,
10692 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010693{
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010694 struct intel_encoder *intel_encoder =
10695 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010696 struct drm_encoder *encoder = &intel_encoder->base;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010697 struct drm_atomic_state *state = old->restore_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010698 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010699
Chris Wilsond2dff872011-04-19 08:36:26 +010010700 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010701 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010702 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010703
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010704 if (!state)
Chris Wilson0622a532011-04-21 09:32:11 +010010705 return;
Maarten Lankhorstedde3612016-02-17 09:18:35 +010010706
10707 ret = drm_atomic_commit(state);
10708 if (ret) {
10709 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
10710 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010711 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010712}
10713
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010714static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010715 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010716{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010717 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010718 u32 dpll = pipe_config->dpll_hw_state.dpll;
10719
10720 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010721 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010722 else if (HAS_PCH_SPLIT(dev))
10723 return 120000;
10724 else if (!IS_GEN2(dev))
10725 return 96000;
10726 else
10727 return 48000;
10728}
10729
Jesse Barnes79e53942008-11-07 14:24:08 -080010730/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010731static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010732 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010733{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010734 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010735 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010736 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010737 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010738 u32 fp;
Ander Conselvan de Oliveira9e2c8472016-05-04 12:11:57 +030010739 struct dpll clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010740 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010741 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010742
10743 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010744 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010745 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010746 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010747
10748 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010749 if (IS_PINEVIEW(dev)) {
10750 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10751 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010752 } else {
10753 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10754 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10755 }
10756
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010757 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010758 if (IS_PINEVIEW(dev))
10759 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10760 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010761 else
10762 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010763 DPLL_FPA01_P1_POST_DIV_SHIFT);
10764
10765 switch (dpll & DPLL_MODE_MASK) {
10766 case DPLLB_MODE_DAC_SERIAL:
10767 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10768 5 : 10;
10769 break;
10770 case DPLLB_MODE_LVDS:
10771 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10772 7 : 14;
10773 break;
10774 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010775 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010776 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010777 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010778 }
10779
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010780 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010781 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010782 else
Imre Deakdccbea32015-06-22 23:35:51 +030010783 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010784 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010785 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010786 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010787
10788 if (is_lvds) {
10789 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10790 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010791
10792 if (lvds & LVDS_CLKB_POWER_UP)
10793 clock.p2 = 7;
10794 else
10795 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010796 } else {
10797 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10798 clock.p1 = 2;
10799 else {
10800 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10801 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10802 }
10803 if (dpll & PLL_P2_DIVIDE_BY_4)
10804 clock.p2 = 4;
10805 else
10806 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010807 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010808
Imre Deakdccbea32015-06-22 23:35:51 +030010809 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010810 }
10811
Ville Syrjälä18442d02013-09-13 16:00:08 +030010812 /*
10813 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010814 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010815 * encoder's get_config() function.
10816 */
Imre Deakdccbea32015-06-22 23:35:51 +030010817 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010818}
10819
Ville Syrjälä6878da02013-09-13 15:59:11 +030010820int intel_dotclock_calculate(int link_freq,
10821 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010822{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010823 /*
10824 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010825 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010826 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010827 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010828 *
10829 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010830 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010831 */
10832
Ville Syrjälä6878da02013-09-13 15:59:11 +030010833 if (!m_n->link_n)
10834 return 0;
10835
10836 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10837}
10838
Ville Syrjälä18442d02013-09-13 16:00:08 +030010839static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010840 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010841{
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010842 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010843
10844 /* read out port_clock from the DPLL */
10845 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010846
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010847 /*
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020010848 * In case there is an active pipe without active ports,
10849 * we may need some idea for the dotclock anyway.
10850 * Calculate one based on the FDI configuration.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010851 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010852 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä21a727b2016-02-17 21:41:10 +020010853 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010854 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010855}
10856
10857/** Returns the currently programmed mode of the given pipe. */
10858struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10859 struct drm_crtc *crtc)
10860{
Chris Wilsonfac5e232016-07-04 11:34:36 +010010861 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080010862 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010863 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010864 struct drm_display_mode *mode;
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010865 struct intel_crtc_state *pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010866 int htot = I915_READ(HTOTAL(cpu_transcoder));
10867 int hsync = I915_READ(HSYNC(cpu_transcoder));
10868 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10869 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010870 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010871
10872 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10873 if (!mode)
10874 return NULL;
10875
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010876 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10877 if (!pipe_config) {
10878 kfree(mode);
10879 return NULL;
10880 }
10881
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010882 /*
10883 * Construct a pipe_config sufficient for getting the clock info
10884 * back out of crtc_clock_get.
10885 *
10886 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10887 * to use a real value here instead.
10888 */
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010889 pipe_config->cpu_transcoder = (enum transcoder) pipe;
10890 pipe_config->pixel_multiplier = 1;
10891 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10892 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10893 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10894 i9xx_crtc_clock_get(intel_crtc, pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010895
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010896 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010897 mode->hdisplay = (htot & 0xffff) + 1;
10898 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10899 mode->hsync_start = (hsync & 0xffff) + 1;
10900 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10901 mode->vdisplay = (vtot & 0xffff) + 1;
10902 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10903 mode->vsync_start = (vsync & 0xffff) + 1;
10904 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10905
10906 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010907
Tvrtko Ursulin3f36b932016-01-19 15:25:17 +000010908 kfree(pipe_config);
10909
Jesse Barnes79e53942008-11-07 14:24:08 -080010910 return mode;
10911}
10912
10913static void intel_crtc_destroy(struct drm_crtc *crtc)
10914{
10915 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010916 struct drm_device *dev = crtc->dev;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010917 struct intel_flip_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010918
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010919 spin_lock_irq(&dev->event_lock);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010920 work = intel_crtc->flip_work;
10921 intel_crtc->flip_work = NULL;
10922 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010923
Daniel Vetter5a21b662016-05-24 17:13:53 +020010924 if (work) {
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010925 cancel_work_sync(&work->mmio_work);
10926 cancel_work_sync(&work->unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010927 kfree(work);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010928 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010929
10930 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010931
Jesse Barnes79e53942008-11-07 14:24:08 -080010932 kfree(intel_crtc);
10933}
10934
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010935static void intel_unpin_work_fn(struct work_struct *__work)
10936{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010937 struct intel_flip_work *work =
10938 container_of(__work, struct intel_flip_work, unpin_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010939 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10940 struct drm_device *dev = crtc->base.dev;
10941 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010942
Daniel Vetter5a21b662016-05-24 17:13:53 +020010943 if (is_mmio_work(work))
10944 flush_work(&work->mmio_work);
10945
10946 mutex_lock(&dev->struct_mutex);
10947 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
Chris Wilsonf8c417c2016-07-20 13:31:53 +010010948 i915_gem_object_put(work->pending_flip_obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010949 mutex_unlock(&dev->struct_mutex);
10950
Chris Wilsone8a261e2016-07-20 13:31:49 +010010951 i915_gem_request_put(work->flip_queued_req);
10952
Daniel Vetter5a21b662016-05-24 17:13:53 +020010953 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
10954 intel_fbc_post_update(crtc);
10955 drm_framebuffer_unreference(work->old_fb);
10956
10957 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10958 atomic_dec(&crtc->unpin_work_count);
10959
10960 kfree(work);
10961}
10962
10963/* Is 'a' after or equal to 'b'? */
10964static bool g4x_flip_count_after_eq(u32 a, u32 b)
10965{
10966 return !((a - b) & 0x80000000);
10967}
10968
10969static bool __pageflip_finished_cs(struct intel_crtc *crtc,
10970 struct intel_flip_work *work)
10971{
10972 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010010973 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020010974 unsigned reset_counter;
10975
10976 reset_counter = i915_reset_counter(&dev_priv->gpu_error);
10977 if (crtc->reset_counter != reset_counter)
10978 return true;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020010979
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010980 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020010981 * The relevant registers doen't exist on pre-ctg.
10982 * As the flip done interrupt doesn't trigger for mmio
10983 * flips on gmch platforms, a flip count check isn't
10984 * really needed there. But since ctg has the registers,
10985 * include it in the check anyway.
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010986 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020010987 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10988 return true;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020010989
Daniel Vetter5a21b662016-05-24 17:13:53 +020010990 /*
10991 * BDW signals flip done immediately if the plane
10992 * is disabled, even if the plane enable is already
10993 * armed to occur at the next vblank :(
10994 */
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020010995
Daniel Vetter5a21b662016-05-24 17:13:53 +020010996 /*
10997 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10998 * used the same base address. In that case the mmio flip might
10999 * have completed, but the CS hasn't even executed the flip yet.
11000 *
11001 * A flip count check isn't enough as the CS might have updated
11002 * the base address just after start of vblank, but before we
11003 * managed to process the interrupt. This means we'd complete the
11004 * CS flip too soon.
11005 *
11006 * Combining both checks should get us a good enough result. It may
11007 * still happen that the CS flip has been executed, but has not
11008 * yet actually completed. But in case the base address is the same
11009 * anyway, we don't really care.
11010 */
11011 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11012 crtc->flip_work->gtt_offset &&
11013 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11014 crtc->flip_work->flip_count);
11015}
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011016
Daniel Vetter5a21b662016-05-24 17:13:53 +020011017static bool
11018__pageflip_finished_mmio(struct intel_crtc *crtc,
11019 struct intel_flip_work *work)
11020{
11021 /*
11022 * MMIO work completes when vblank is different from
11023 * flip_queued_vblank.
11024 *
11025 * Reset counter value doesn't matter, this is handled by
11026 * i915_wait_request finishing early, so no need to handle
11027 * reset here.
11028 */
11029 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011030}
11031
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011032
11033static bool pageflip_finished(struct intel_crtc *crtc,
11034 struct intel_flip_work *work)
11035{
11036 if (!atomic_read(&work->pending))
11037 return false;
11038
11039 smp_rmb();
11040
Daniel Vetter5a21b662016-05-24 17:13:53 +020011041 if (is_mmio_work(work))
11042 return __pageflip_finished_mmio(crtc, work);
11043 else
11044 return __pageflip_finished_cs(crtc, work);
11045}
11046
11047void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11048{
Chris Wilson91c8a322016-07-05 10:40:23 +010011049 struct drm_device *dev = &dev_priv->drm;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011050 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11051 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11052 struct intel_flip_work *work;
11053 unsigned long flags;
11054
11055 /* Ignore early vblank irqs */
11056 if (!crtc)
11057 return;
11058
Daniel Vetterf3260382014-09-15 14:55:23 +020011059 /*
Daniel Vetter5a21b662016-05-24 17:13:53 +020011060 * This is called both by irq handlers and the reset code (to complete
11061 * lost pageflips) so needs the full irqsave spinlocks.
Chris Wilsone7d841c2012-12-03 11:36:30 +000011062 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011063 spin_lock_irqsave(&dev->event_lock, flags);
11064 work = intel_crtc->flip_work;
11065
11066 if (work != NULL &&
11067 !is_mmio_work(work) &&
11068 pageflip_finished(intel_crtc, work))
11069 page_flip_completed(intel_crtc);
11070
11071 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011072}
11073
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011074void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011075{
Chris Wilson91c8a322016-07-05 10:40:23 +010011076 struct drm_device *dev = &dev_priv->drm;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011077 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11078 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11079 struct intel_flip_work *work;
11080 unsigned long flags;
11081
11082 /* Ignore early vblank irqs */
11083 if (!crtc)
11084 return;
11085
11086 /*
11087 * This is called both by irq handlers and the reset code (to complete
11088 * lost pageflips) so needs the full irqsave spinlocks.
11089 */
11090 spin_lock_irqsave(&dev->event_lock, flags);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011091 work = intel_crtc->flip_work;
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011092
Daniel Vetter5a21b662016-05-24 17:13:53 +020011093 if (work != NULL &&
11094 is_mmio_work(work) &&
11095 pageflip_finished(intel_crtc, work))
11096 page_flip_completed(intel_crtc);
Maarten Lankhorst68858432016-05-17 15:07:52 +020011097
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011098 spin_unlock_irqrestore(&dev->event_lock, flags);
11099}
11100
Daniel Vetter5a21b662016-05-24 17:13:53 +020011101static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11102 struct intel_flip_work *work)
11103{
11104 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
11105
11106 /* Ensure that the work item is consistent when activating it ... */
11107 smp_mb__before_atomic();
11108 atomic_set(&work->pending, 1);
11109}
11110
11111static int intel_gen2_queue_flip(struct drm_device *dev,
11112 struct drm_crtc *crtc,
11113 struct drm_framebuffer *fb,
11114 struct drm_i915_gem_object *obj,
11115 struct drm_i915_gem_request *req,
11116 uint32_t flags)
11117{
Chris Wilsonb5321f32016-08-02 22:50:18 +010011118 struct intel_ringbuffer *ring = req->ringbuf;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011119 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11120 u32 flip_mask;
11121 int ret;
11122
11123 ret = intel_ring_begin(req, 6);
11124 if (ret)
11125 return ret;
11126
11127 /* Can't queue multiple flips, so wait for the previous
11128 * one to finish before executing the next.
11129 */
11130 if (intel_crtc->plane)
11131 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11132 else
11133 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011134 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11135 intel_ring_emit(ring, MI_NOOP);
11136 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011137 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011138 intel_ring_emit(ring, fb->pitches[0]);
11139 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11140 intel_ring_emit(ring, 0); /* aux display base address, unused */
Daniel Vetter5a21b662016-05-24 17:13:53 +020011141
11142 return 0;
11143}
11144
11145static int intel_gen3_queue_flip(struct drm_device *dev,
11146 struct drm_crtc *crtc,
11147 struct drm_framebuffer *fb,
11148 struct drm_i915_gem_object *obj,
11149 struct drm_i915_gem_request *req,
11150 uint32_t flags)
11151{
Chris Wilsonb5321f32016-08-02 22:50:18 +010011152 struct intel_ringbuffer *ring = req->ringbuf;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11154 u32 flip_mask;
11155 int ret;
11156
11157 ret = intel_ring_begin(req, 6);
11158 if (ret)
11159 return ret;
11160
11161 if (intel_crtc->plane)
11162 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11163 else
11164 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011165 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11166 intel_ring_emit(ring, MI_NOOP);
11167 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011168 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011169 intel_ring_emit(ring, fb->pitches[0]);
11170 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11171 intel_ring_emit(ring, MI_NOOP);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011172
11173 return 0;
11174}
11175
11176static int intel_gen4_queue_flip(struct drm_device *dev,
11177 struct drm_crtc *crtc,
11178 struct drm_framebuffer *fb,
11179 struct drm_i915_gem_object *obj,
11180 struct drm_i915_gem_request *req,
11181 uint32_t flags)
11182{
Chris Wilsonb5321f32016-08-02 22:50:18 +010011183 struct intel_ringbuffer *ring = req->ringbuf;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011184 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011185 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11186 uint32_t pf, pipesrc;
11187 int ret;
11188
11189 ret = intel_ring_begin(req, 4);
11190 if (ret)
11191 return ret;
11192
11193 /* i965+ uses the linear or tiled offsets from the
11194 * Display Registers (which do not change across a page-flip)
11195 * so we need only reprogram the base address.
11196 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010011197 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011198 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011199 intel_ring_emit(ring, fb->pitches[0]);
11200 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011201 obj->tiling_mode);
11202
11203 /* XXX Enabling the panel-fitter across page-flip is so far
11204 * untested on non-native modes, so ignore it for now.
11205 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11206 */
11207 pf = 0;
11208 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011209 intel_ring_emit(ring, pf | pipesrc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011210
11211 return 0;
11212}
11213
11214static int intel_gen6_queue_flip(struct drm_device *dev,
11215 struct drm_crtc *crtc,
11216 struct drm_framebuffer *fb,
11217 struct drm_i915_gem_object *obj,
11218 struct drm_i915_gem_request *req,
11219 uint32_t flags)
11220{
Chris Wilsonb5321f32016-08-02 22:50:18 +010011221 struct intel_ringbuffer *ring = req->ringbuf;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011222 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011223 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11224 uint32_t pf, pipesrc;
11225 int ret;
11226
11227 ret = intel_ring_begin(req, 4);
11228 if (ret)
11229 return ret;
11230
Chris Wilsonb5321f32016-08-02 22:50:18 +010011231 intel_ring_emit(ring, MI_DISPLAY_FLIP |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011232 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Chris Wilsonb5321f32016-08-02 22:50:18 +010011233 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
11234 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011235
11236 /* Contrary to the suggestions in the documentation,
11237 * "Enable Panel Fitter" does not seem to be required when page
11238 * flipping with a non-native mode, and worse causes a normal
11239 * modeset to fail.
11240 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11241 */
11242 pf = 0;
11243 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011244 intel_ring_emit(ring, pf | pipesrc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011245
11246 return 0;
11247}
11248
11249static int intel_gen7_queue_flip(struct drm_device *dev,
11250 struct drm_crtc *crtc,
11251 struct drm_framebuffer *fb,
11252 struct drm_i915_gem_object *obj,
11253 struct drm_i915_gem_request *req,
11254 uint32_t flags)
11255{
Chris Wilsonb5321f32016-08-02 22:50:18 +010011256 struct intel_ringbuffer *ring = req->ringbuf;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011257 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11258 uint32_t plane_bit = 0;
11259 int len, ret;
11260
11261 switch (intel_crtc->plane) {
11262 case PLANE_A:
11263 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11264 break;
11265 case PLANE_B:
11266 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11267 break;
11268 case PLANE_C:
11269 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11270 break;
11271 default:
11272 WARN_ONCE(1, "unknown plane in flip command\n");
11273 return -ENODEV;
11274 }
11275
11276 len = 4;
Chris Wilsonb5321f32016-08-02 22:50:18 +010011277 if (req->engine->id == RCS) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020011278 len += 6;
11279 /*
11280 * On Gen 8, SRM is now taking an extra dword to accommodate
11281 * 48bits addresses, and we need a NOOP for the batch size to
11282 * stay even.
11283 */
11284 if (IS_GEN8(dev))
11285 len += 2;
11286 }
11287
11288 /*
11289 * BSpec MI_DISPLAY_FLIP for IVB:
11290 * "The full packet must be contained within the same cache line."
11291 *
11292 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11293 * cacheline, if we ever start emitting more commands before
11294 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11295 * then do the cacheline alignment, and finally emit the
11296 * MI_DISPLAY_FLIP.
11297 */
11298 ret = intel_ring_cacheline_align(req);
11299 if (ret)
11300 return ret;
11301
11302 ret = intel_ring_begin(req, len);
11303 if (ret)
11304 return ret;
11305
11306 /* Unmask the flip-done completion message. Note that the bspec says that
11307 * we should do this for both the BCS and RCS, and that we must not unmask
11308 * more than one flip event at any time (or ensure that one flip message
11309 * can be sent by waiting for flip-done prior to queueing new flips).
11310 * Experimentation says that BCS works despite DERRMR masking all
11311 * flip-done completion events and that unmasking all planes at once
11312 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11313 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11314 */
Chris Wilsonb5321f32016-08-02 22:50:18 +010011315 if (req->engine->id == RCS) {
11316 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11317 intel_ring_emit_reg(ring, DERRMR);
11318 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011319 DERRMR_PIPEB_PRI_FLIP_DONE |
11320 DERRMR_PIPEC_PRI_FLIP_DONE));
11321 if (IS_GEN8(dev))
Chris Wilsonb5321f32016-08-02 22:50:18 +010011322 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011323 MI_SRM_LRM_GLOBAL_GTT);
11324 else
Chris Wilsonb5321f32016-08-02 22:50:18 +010011325 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
Daniel Vetter5a21b662016-05-24 17:13:53 +020011326 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonb5321f32016-08-02 22:50:18 +010011327 intel_ring_emit_reg(ring, DERRMR);
11328 intel_ring_emit(ring, req->engine->scratch.gtt_offset + 256);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011329 if (IS_GEN8(dev)) {
Chris Wilsonb5321f32016-08-02 22:50:18 +010011330 intel_ring_emit(ring, 0);
11331 intel_ring_emit(ring, MI_NOOP);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011332 }
11333 }
11334
Chris Wilsonb5321f32016-08-02 22:50:18 +010011335 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
11336 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
11337 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11338 intel_ring_emit(ring, (MI_NOOP));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011339
11340 return 0;
11341}
11342
11343static bool use_mmio_flip(struct intel_engine_cs *engine,
11344 struct drm_i915_gem_object *obj)
11345{
Chris Wilsonc37efb92016-06-17 08:28:47 +010011346 struct reservation_object *resv;
11347
Daniel Vetter5a21b662016-05-24 17:13:53 +020011348 /*
11349 * This is not being used for older platforms, because
11350 * non-availability of flip done interrupt forces us to use
11351 * CS flips. Older platforms derive flip done using some clever
11352 * tricks involving the flip_pending status bits and vblank irqs.
11353 * So using MMIO flips there would disrupt this mechanism.
11354 */
11355
11356 if (engine == NULL)
11357 return true;
11358
11359 if (INTEL_GEN(engine->i915) < 5)
11360 return false;
11361
11362 if (i915.use_mmio_flip < 0)
11363 return false;
11364 else if (i915.use_mmio_flip > 0)
11365 return true;
11366 else if (i915.enable_execlists)
11367 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011368
11369 resv = i915_gem_object_get_dmabuf_resv(obj);
11370 if (resv && !reservation_object_test_signaled_rcu(resv, false))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011371 return true;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011372
11373 return engine != i915_gem_request_get_engine(obj->last_write_req);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011374}
11375
11376static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11377 unsigned int rotation,
11378 struct intel_flip_work *work)
11379{
11380 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011381 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011382 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11383 const enum pipe pipe = intel_crtc->pipe;
11384 u32 ctl, stride, tile_height;
11385
11386 ctl = I915_READ(PLANE_CTL(pipe, 0));
11387 ctl &= ~PLANE_CTL_TILED_MASK;
11388 switch (fb->modifier[0]) {
11389 case DRM_FORMAT_MOD_NONE:
11390 break;
11391 case I915_FORMAT_MOD_X_TILED:
11392 ctl |= PLANE_CTL_TILED_X;
11393 break;
11394 case I915_FORMAT_MOD_Y_TILED:
11395 ctl |= PLANE_CTL_TILED_Y;
11396 break;
11397 case I915_FORMAT_MOD_Yf_TILED:
11398 ctl |= PLANE_CTL_TILED_YF;
11399 break;
11400 default:
11401 MISSING_CASE(fb->modifier[0]);
11402 }
11403
11404 /*
11405 * The stride is either expressed as a multiple of 64 bytes chunks for
11406 * linear buffers or in number of tiles for tiled buffers.
11407 */
11408 if (intel_rotation_90_or_270(rotation)) {
11409 /* stride = Surface height in tiles */
11410 tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
11411 stride = DIV_ROUND_UP(fb->height, tile_height);
11412 } else {
11413 stride = fb->pitches[0] /
11414 intel_fb_stride_alignment(dev_priv, fb->modifier[0],
11415 fb->pixel_format);
11416 }
11417
11418 /*
11419 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11420 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11421 */
11422 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11423 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11424
11425 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11426 POSTING_READ(PLANE_SURF(pipe, 0));
11427}
11428
11429static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11430 struct intel_flip_work *work)
11431{
11432 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011433 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011434 struct intel_framebuffer *intel_fb =
11435 to_intel_framebuffer(intel_crtc->base.primary->fb);
11436 struct drm_i915_gem_object *obj = intel_fb->obj;
11437 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
11438 u32 dspcntr;
11439
11440 dspcntr = I915_READ(reg);
11441
11442 if (obj->tiling_mode != I915_TILING_NONE)
11443 dspcntr |= DISPPLANE_TILED;
11444 else
11445 dspcntr &= ~DISPPLANE_TILED;
11446
11447 I915_WRITE(reg, dspcntr);
11448
11449 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11450 POSTING_READ(DSPSURF(intel_crtc->plane));
11451}
11452
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011453static void intel_mmio_flip_work_func(struct work_struct *w)
Damien Lespiauff944562014-11-20 14:58:16 +000011454{
Maarten Lankhorst51cbaf02016-05-17 15:07:49 +020011455 struct intel_flip_work *work =
11456 container_of(w, struct intel_flip_work, mmio_work);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011457 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11458 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11459 struct intel_framebuffer *intel_fb =
11460 to_intel_framebuffer(crtc->base.primary->fb);
11461 struct drm_i915_gem_object *obj = intel_fb->obj;
Chris Wilsonc37efb92016-06-17 08:28:47 +010011462 struct reservation_object *resv;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011463
11464 if (work->flip_queued_req)
11465 WARN_ON(__i915_wait_request(work->flip_queued_req,
11466 false, NULL,
Chris Wilson197be2a2016-07-20 09:21:13 +010011467 NO_WAITBOOST));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011468
11469 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010011470 resv = i915_gem_object_get_dmabuf_resv(obj);
11471 if (resv)
11472 WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
Daniel Vetter5a21b662016-05-24 17:13:53 +020011473 MAX_SCHEDULE_TIMEOUT) < 0);
11474
11475 intel_pipe_update_start(crtc);
11476
11477 if (INTEL_GEN(dev_priv) >= 9)
11478 skl_do_mmio_flip(crtc, work->rotation, work);
11479 else
11480 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11481 ilk_do_mmio_flip(crtc, work);
11482
11483 intel_pipe_update_end(crtc, work);
11484}
11485
11486static int intel_default_queue_flip(struct drm_device *dev,
11487 struct drm_crtc *crtc,
11488 struct drm_framebuffer *fb,
11489 struct drm_i915_gem_object *obj,
11490 struct drm_i915_gem_request *req,
11491 uint32_t flags)
11492{
11493 return -ENODEV;
11494}
11495
11496static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
11497 struct intel_crtc *intel_crtc,
11498 struct intel_flip_work *work)
11499{
11500 u32 addr, vblank;
11501
11502 if (!atomic_read(&work->pending))
11503 return false;
11504
11505 smp_rmb();
11506
11507 vblank = intel_crtc_get_vblank_counter(intel_crtc);
11508 if (work->flip_ready_vblank == 0) {
11509 if (work->flip_queued_req &&
Chris Wilsonf69a02c2016-07-01 17:23:16 +010011510 !i915_gem_request_completed(work->flip_queued_req))
Daniel Vetter5a21b662016-05-24 17:13:53 +020011511 return false;
11512
11513 work->flip_ready_vblank = vblank;
11514 }
11515
11516 if (vblank - work->flip_ready_vblank < 3)
11517 return false;
11518
11519 /* Potential stall - if we see that the flip has happened,
11520 * assume a missed interrupt. */
11521 if (INTEL_GEN(dev_priv) >= 4)
11522 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11523 else
11524 addr = I915_READ(DSPADDR(intel_crtc->plane));
11525
11526 /* There is a potential issue here with a false positive after a flip
11527 * to the same address. We could address this by checking for a
11528 * non-incrementing frame counter.
11529 */
11530 return addr == work->gtt_offset;
11531}
11532
11533void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
11534{
Chris Wilson91c8a322016-07-05 10:40:23 +010011535 struct drm_device *dev = &dev_priv->drm;
Daniel Vetter5a21b662016-05-24 17:13:53 +020011536 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011537 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011538 struct intel_flip_work *work;
11539
11540 WARN_ON(!in_interrupt());
11541
11542 if (crtc == NULL)
11543 return;
11544
11545 spin_lock(&dev->event_lock);
11546 work = intel_crtc->flip_work;
11547
11548 if (work != NULL && !is_mmio_work(work) &&
11549 __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
11550 WARN_ONCE(1,
11551 "Kicking stuck page flip: queued at %d, now %d\n",
11552 work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
11553 page_flip_completed(intel_crtc);
11554 work = NULL;
11555 }
11556
11557 if (work != NULL && !is_mmio_work(work) &&
11558 intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
11559 intel_queue_rps_boost_for_request(work->flip_queued_req);
11560 spin_unlock(&dev->event_lock);
11561}
11562
11563static int intel_crtc_page_flip(struct drm_crtc *crtc,
11564 struct drm_framebuffer *fb,
11565 struct drm_pending_vblank_event *event,
11566 uint32_t page_flip_flags)
11567{
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011568 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011569 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011570 struct drm_framebuffer *old_fb = crtc->primary->fb;
11571 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11572 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11573 struct drm_plane *primary = crtc->primary;
11574 enum pipe pipe = intel_crtc->pipe;
11575 struct intel_flip_work *work;
11576 struct intel_engine_cs *engine;
11577 bool mmio_flip;
11578 struct drm_i915_gem_request *request = NULL;
11579 int ret;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011580
Daniel Vetter5a21b662016-05-24 17:13:53 +020011581 /*
11582 * drm_mode_page_flip_ioctl() should already catch this, but double
11583 * check to be safe. In the future we may enable pageflipping from
11584 * a disabled primary plane.
11585 */
11586 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11587 return -EBUSY;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020011588
Daniel Vetter5a21b662016-05-24 17:13:53 +020011589 /* Can't change pixel format via MI display flips. */
11590 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11591 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011592
Daniel Vetter5a21b662016-05-24 17:13:53 +020011593 /*
11594 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11595 * Note that pitch changes could also affect these register.
11596 */
11597 if (INTEL_INFO(dev)->gen > 3 &&
11598 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11599 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11600 return -EINVAL;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011601
Daniel Vetter5a21b662016-05-24 17:13:53 +020011602 if (i915_terminally_wedged(&dev_priv->gpu_error))
11603 goto out_hang;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011604
Daniel Vetter5a21b662016-05-24 17:13:53 +020011605 work = kzalloc(sizeof(*work), GFP_KERNEL);
11606 if (work == NULL)
11607 return -ENOMEM;
11608
11609 work->event = event;
11610 work->crtc = crtc;
11611 work->old_fb = old_fb;
11612 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011613
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011614 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011615 if (ret)
11616 goto free_work;
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011617
Daniel Vetter5a21b662016-05-24 17:13:53 +020011618 /* We borrow the event spin lock for protecting flip_work */
11619 spin_lock_irq(&dev->event_lock);
11620 if (intel_crtc->flip_work) {
11621 /* Before declaring the flip queue wedged, check if
11622 * the hardware completed the operation behind our backs.
11623 */
11624 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
11625 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11626 page_flip_completed(intel_crtc);
11627 } else {
11628 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11629 spin_unlock_irq(&dev->event_lock);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020011630
Daniel Vetter5a21b662016-05-24 17:13:53 +020011631 drm_crtc_vblank_put(crtc);
11632 kfree(work);
11633 return -EBUSY;
11634 }
11635 }
11636 intel_crtc->flip_work = work;
11637 spin_unlock_irq(&dev->event_lock);
Alex Goinsfd8e0582015-11-25 18:43:38 -080011638
Daniel Vetter5a21b662016-05-24 17:13:53 +020011639 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11640 flush_workqueue(dev_priv->wq);
11641
11642 /* Reference the objects for the scheduled work. */
11643 drm_framebuffer_reference(work->old_fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011644
11645 crtc->primary->fb = fb;
11646 update_state_fb(crtc->primary);
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020011647
11648 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
11649 to_intel_plane_state(primary->state));
Daniel Vetter5a21b662016-05-24 17:13:53 +020011650
Chris Wilson25dc5562016-07-20 13:31:52 +010011651 work->pending_flip_obj = i915_gem_object_get(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011652
11653 ret = i915_mutex_lock_interruptible(dev);
11654 if (ret)
11655 goto cleanup;
11656
11657 intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
11658 if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
11659 ret = -EIO;
11660 goto cleanup;
11661 }
11662
11663 atomic_inc(&intel_crtc->unpin_work_count);
11664
11665 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11666 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11667
11668 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
11669 engine = &dev_priv->engine[BCS];
11670 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
11671 /* vlv: DISPLAY_FLIP fails to change tiling */
11672 engine = NULL;
11673 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11674 engine = &dev_priv->engine[BCS];
11675 } else if (INTEL_INFO(dev)->gen >= 7) {
11676 engine = i915_gem_request_get_engine(obj->last_write_req);
11677 if (engine == NULL || engine->id != RCS)
11678 engine = &dev_priv->engine[BCS];
11679 } else {
11680 engine = &dev_priv->engine[RCS];
11681 }
11682
11683 mmio_flip = use_mmio_flip(engine, obj);
11684
11685 /* When using CS flips, we want to emit semaphores between rings.
11686 * However, when using mmio flips we will create a task to do the
11687 * synchronisation, so all we want here is to pin the framebuffer
11688 * into the display plane and skip any waits.
11689 */
11690 if (!mmio_flip) {
11691 ret = i915_gem_object_sync(obj, engine, &request);
11692 if (!ret && !request) {
11693 request = i915_gem_request_alloc(engine, NULL);
11694 ret = PTR_ERR_OR_ZERO(request);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011695 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053011696
Daniel Vetter5a21b662016-05-24 17:13:53 +020011697 if (ret)
11698 goto cleanup_pending;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011699 }
11700
Daniel Vetter5a21b662016-05-24 17:13:53 +020011701 ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
11702 if (ret)
11703 goto cleanup_pending;
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011704
Daniel Vetter5a21b662016-05-24 17:13:53 +020011705 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11706 obj, 0);
11707 work->gtt_offset += intel_crtc->dspaddr_offset;
11708 work->rotation = crtc->primary->state->rotation;
11709
11710 if (mmio_flip) {
11711 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
11712
11713 i915_gem_request_assign(&work->flip_queued_req,
11714 obj->last_write_req);
11715
11716 schedule_work(&work->mmio_work);
11717 } else {
11718 i915_gem_request_assign(&work->flip_queued_req, request);
11719 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11720 page_flip_flags);
11721 if (ret)
11722 goto cleanup_unpin;
11723
11724 intel_mark_page_flip_active(intel_crtc, work);
11725
11726 i915_add_request_no_flush(request);
Maarten Lankhorst143f73b32016-05-17 15:07:54 +020011727 }
11728
Daniel Vetter5a21b662016-05-24 17:13:53 +020011729 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
11730 to_intel_plane(primary)->frontbuffer_bit);
11731 mutex_unlock(&dev->struct_mutex);
11732
11733 intel_frontbuffer_flip_prepare(dev,
11734 to_intel_plane(primary)->frontbuffer_bit);
11735
11736 trace_i915_flip_request(intel_crtc->plane, obj);
11737
11738 return 0;
11739
11740cleanup_unpin:
11741 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
11742cleanup_pending:
11743 if (!IS_ERR_OR_NULL(request))
11744 i915_add_request_no_flush(request);
11745 atomic_dec(&intel_crtc->unpin_work_count);
11746 mutex_unlock(&dev->struct_mutex);
11747cleanup:
11748 crtc->primary->fb = old_fb;
11749 update_state_fb(crtc->primary);
11750
Chris Wilson34911fd2016-07-20 13:31:54 +010011751 i915_gem_object_put_unlocked(obj);
Daniel Vetter5a21b662016-05-24 17:13:53 +020011752 drm_framebuffer_unreference(work->old_fb);
11753
11754 spin_lock_irq(&dev->event_lock);
11755 intel_crtc->flip_work = NULL;
11756 spin_unlock_irq(&dev->event_lock);
11757
11758 drm_crtc_vblank_put(crtc);
11759free_work:
11760 kfree(work);
11761
11762 if (ret == -EIO) {
11763 struct drm_atomic_state *state;
11764 struct drm_plane_state *plane_state;
11765
11766out_hang:
11767 state = drm_atomic_state_alloc(dev);
11768 if (!state)
11769 return -ENOMEM;
11770 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11771
11772retry:
11773 plane_state = drm_atomic_get_plane_state(state, primary);
11774 ret = PTR_ERR_OR_ZERO(plane_state);
11775 if (!ret) {
11776 drm_atomic_set_fb_for_plane(plane_state, fb);
11777
11778 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11779 if (!ret)
11780 ret = drm_atomic_commit(state);
11781 }
11782
11783 if (ret == -EDEADLK) {
11784 drm_modeset_backoff(state->acquire_ctx);
11785 drm_atomic_state_clear(state);
11786 goto retry;
11787 }
11788
11789 if (ret)
11790 drm_atomic_state_free(state);
11791
11792 if (ret == 0 && event) {
11793 spin_lock_irq(&dev->event_lock);
11794 drm_crtc_send_vblank_event(crtc, event);
11795 spin_unlock_irq(&dev->event_lock);
11796 }
11797 }
11798 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011799}
11800
Daniel Vetter5a21b662016-05-24 17:13:53 +020011801
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011802/**
11803 * intel_wm_need_update - Check whether watermarks need updating
11804 * @plane: drm plane
11805 * @state: new plane state
11806 *
11807 * Check current plane state versus the new one to determine whether
11808 * watermarks need to be recalculated.
11809 *
11810 * Returns true or false.
11811 */
11812static bool intel_wm_need_update(struct drm_plane *plane,
11813 struct drm_plane_state *state)
11814{
Matt Roperd21fbe82015-09-24 15:53:12 -070011815 struct intel_plane_state *new = to_intel_plane_state(state);
11816 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
11817
11818 /* Update watermarks on tiling or size changes. */
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011819 if (new->visible != cur->visible)
11820 return true;
11821
11822 if (!cur->base.fb || !new->base.fb)
11823 return false;
11824
11825 if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
11826 cur->base.rotation != new->base.rotation ||
Matt Roperd21fbe82015-09-24 15:53:12 -070011827 drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
11828 drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
11829 drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
11830 drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011831 return true;
11832
11833 return false;
11834}
11835
Matt Roperd21fbe82015-09-24 15:53:12 -070011836static bool needs_scaling(struct intel_plane_state *state)
11837{
11838 int src_w = drm_rect_width(&state->src) >> 16;
11839 int src_h = drm_rect_height(&state->src) >> 16;
11840 int dst_w = drm_rect_width(&state->dst);
11841 int dst_h = drm_rect_height(&state->dst);
11842
11843 return (src_w != dst_w || src_h != dst_h);
11844}
11845
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011846int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11847 struct drm_plane_state *plane_state)
11848{
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011849 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011850 struct drm_crtc *crtc = crtc_state->crtc;
11851 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11852 struct drm_plane *plane = plane_state->plane;
11853 struct drm_device *dev = crtc->dev;
Matt Ropered4a6a72016-02-23 17:20:13 -080011854 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011855 struct intel_plane_state *old_plane_state =
11856 to_intel_plane_state(plane->state);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011857 bool mode_changed = needs_modeset(crtc_state);
11858 bool was_crtc_enabled = crtc->state->active;
11859 bool is_crtc_enabled = crtc_state->active;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011860 bool turn_off, turn_on, visible, was_visible;
11861 struct drm_framebuffer *fb = plane_state->fb;
Ville Syrjälä78108b72016-05-27 20:59:19 +030011862 int ret;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011863
Chris Wilson84114992016-07-02 15:36:06 +010011864 if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011865 ret = skl_update_scaler_plane(
11866 to_intel_crtc_state(crtc_state),
11867 to_intel_plane_state(plane_state));
11868 if (ret)
11869 return ret;
11870 }
11871
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011872 was_visible = old_plane_state->visible;
11873 visible = to_intel_plane_state(plane_state)->visible;
11874
11875 if (!was_crtc_enabled && WARN_ON(was_visible))
11876 was_visible = false;
11877
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011878 /*
11879 * Visibility is calculated as if the crtc was on, but
11880 * after scaler setup everything depends on it being off
11881 * when the crtc isn't active.
Ville Syrjäläf818ffe2016-04-29 17:31:18 +030011882 *
11883 * FIXME this is wrong for watermarks. Watermarks should also
11884 * be computed as if the pipe would be active. Perhaps move
11885 * per-plane wm computation to the .check_plane() hook, and
11886 * only combine the results from all planes in the current place?
Maarten Lankhorst35c08f42015-12-03 14:31:07 +010011887 */
11888 if (!is_crtc_enabled)
11889 to_intel_plane_state(plane_state)->visible = visible = false;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011890
11891 if (!was_visible && !visible)
11892 return 0;
11893
Maarten Lankhorste8861672016-02-24 11:24:26 +010011894 if (fb != old_plane_state->base.fb)
11895 pipe_config->fb_changed = true;
11896
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011897 turn_off = was_visible && (!visible || mode_changed);
11898 turn_on = visible && (!was_visible || mode_changed);
11899
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011900 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
Ville Syrjälä78108b72016-05-27 20:59:19 +030011901 intel_crtc->base.base.id,
11902 intel_crtc->base.name,
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011903 plane->base.id, plane->name,
11904 fb ? fb->base.id : -1);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011905
Ville Syrjälä72660ce2016-05-27 20:59:20 +030011906 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
11907 plane->base.id, plane->name,
11908 was_visible, visible,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011909 turn_off, turn_on, mode_changed);
11910
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011911 if (turn_on) {
11912 pipe_config->update_wm_pre = true;
11913
11914 /* must disable cxsr around plane enable/disable */
11915 if (plane->type != DRM_PLANE_TYPE_CURSOR)
11916 pipe_config->disable_cxsr = true;
11917 } else if (turn_off) {
11918 pipe_config->update_wm_post = true;
Maarten Lankhorst92826fc2015-12-03 13:49:13 +010011919
Ville Syrjälä852eb002015-06-24 22:00:07 +030011920 /* must disable cxsr around plane enable/disable */
Maarten Lankhorste8861672016-02-24 11:24:26 +010011921 if (plane->type != DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorstab1d3a02015-11-19 16:07:14 +010011922 pipe_config->disable_cxsr = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011923 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011924 /* FIXME bollocks */
11925 pipe_config->update_wm_pre = true;
11926 pipe_config->update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011927 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011928
Matt Ropered4a6a72016-02-23 17:20:13 -080011929 /* Pre-gen9 platforms need two-step watermark updates */
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011930 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
11931 INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
Matt Ropered4a6a72016-02-23 17:20:13 -080011932 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
11933
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011934 if (visible || was_visible)
Maarten Lankhorstcd202f62016-03-09 10:35:44 +010011935 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011936
Maarten Lankhorst31ae71f2016-03-09 10:35:45 +010011937 /*
11938 * WaCxSRDisabledForSpriteScaling:ivb
11939 *
11940 * cstate->update_wm was already set above, so this flag will
11941 * take effect when we commit and program watermarks.
11942 */
11943 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
11944 needs_scaling(to_intel_plane_state(plane_state)) &&
11945 !needs_scaling(old_plane_state))
11946 pipe_config->disable_lp_wm = true;
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011947
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011948 return 0;
11949}
11950
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011951static bool encoders_cloneable(const struct intel_encoder *a,
11952 const struct intel_encoder *b)
11953{
11954 /* masks could be asymmetric, so check both ways */
11955 return a == b || (a->cloneable & (1 << b->type) &&
11956 b->cloneable & (1 << a->type));
11957}
11958
11959static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11960 struct intel_crtc *crtc,
11961 struct intel_encoder *encoder)
11962{
11963 struct intel_encoder *source_encoder;
11964 struct drm_connector *connector;
11965 struct drm_connector_state *connector_state;
11966 int i;
11967
11968 for_each_connector_in_state(state, connector, connector_state, i) {
11969 if (connector_state->crtc != &crtc->base)
11970 continue;
11971
11972 source_encoder =
11973 to_intel_encoder(connector_state->best_encoder);
11974 if (!encoders_cloneable(encoder, source_encoder))
11975 return false;
11976 }
11977
11978 return true;
11979}
11980
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011981static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11982 struct drm_crtc_state *crtc_state)
11983{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011984 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010011985 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011986 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011987 struct intel_crtc_state *pipe_config =
11988 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011989 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011990 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011991 bool mode_changed = needs_modeset(crtc_state);
11992
Ville Syrjälä852eb002015-06-24 22:00:07 +030011993 if (mode_changed && !crtc_state->active)
Ville Syrjäläcaed3612016-03-09 19:07:25 +020011994 pipe_config->update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011995
Maarten Lankhorstad421372015-06-15 12:33:42 +020011996 if (mode_changed && crtc_state->enable &&
11997 dev_priv->display.crtc_compute_clock &&
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020011998 !WARN_ON(pipe_config->shared_dpll)) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020011999 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12000 pipe_config);
12001 if (ret)
12002 return ret;
12003 }
12004
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012005 if (crtc_state->color_mgmt_changed) {
12006 ret = intel_color_check(crtc, crtc_state);
12007 if (ret)
12008 return ret;
Lionel Landwerline7852a42016-05-25 14:30:41 +010012009
12010 /*
12011 * Changing color management on Intel hardware is
12012 * handled as part of planes update.
12013 */
12014 crtc_state->planes_changed = true;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000012015 }
12016
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012017 ret = 0;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012018 if (dev_priv->display.compute_pipe_wm) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +010012019 ret = dev_priv->display.compute_pipe_wm(pipe_config);
Matt Ropered4a6a72016-02-23 17:20:13 -080012020 if (ret) {
12021 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
Matt Roper86c8bbb2015-09-24 15:53:16 -070012022 return ret;
Matt Ropered4a6a72016-02-23 17:20:13 -080012023 }
12024 }
12025
12026 if (dev_priv->display.compute_intermediate_wm &&
12027 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12028 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12029 return 0;
12030
12031 /*
12032 * Calculate 'intermediate' watermarks that satisfy both the
12033 * old state and the new state. We can program these
12034 * immediately.
12035 */
12036 ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
12037 intel_crtc,
12038 pipe_config);
12039 if (ret) {
12040 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
12041 return ret;
12042 }
Ville Syrjäläe3d54572016-05-13 10:10:42 -070012043 } else if (dev_priv->display.compute_intermediate_wm) {
12044 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12045 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -070012046 }
12047
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020012048 if (INTEL_INFO(dev)->gen >= 9) {
12049 if (mode_changed)
12050 ret = skl_update_scaler_crtc(pipe_config);
12051
12052 if (!ret)
12053 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12054 pipe_config);
12055 }
12056
12057 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012058}
12059
Jani Nikula65b38e02015-04-13 11:26:56 +030012060static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012061 .mode_set_base_atomic = intel_pipe_set_base_atomic,
Daniel Vetter5a21b662016-05-24 17:13:53 +020012062 .atomic_begin = intel_begin_crtc_commit,
12063 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020012064 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012065};
12066
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012067static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12068{
12069 struct intel_connector *connector;
12070
12071 for_each_intel_connector(dev, connector) {
Daniel Vetter8863dc72016-05-06 15:39:03 +020012072 if (connector->base.state->crtc)
12073 drm_connector_unreference(&connector->base);
12074
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012075 if (connector->base.encoder) {
12076 connector->base.state->best_encoder =
12077 connector->base.encoder;
12078 connector->base.state->crtc =
12079 connector->base.encoder->crtc;
Daniel Vetter8863dc72016-05-06 15:39:03 +020012080
12081 drm_connector_reference(&connector->base);
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020012082 } else {
12083 connector->base.state->best_encoder = NULL;
12084 connector->base.state->crtc = NULL;
12085 }
12086 }
12087}
12088
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012089static void
Robin Schroereba905b2014-05-18 02:24:50 +020012090connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012091 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012092{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012093 int bpp = pipe_config->pipe_bpp;
12094
12095 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
12096 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012097 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012098
12099 /* Don't use an invalid EDID bpc value */
12100 if (connector->base.display_info.bpc &&
12101 connector->base.display_info.bpc * 3 < bpp) {
12102 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
12103 bpp, connector->base.display_info.bpc*3);
12104 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
12105 }
12106
Jani Nikula013dd9e2016-01-13 16:35:20 +020012107 /* Clamp bpp to default limit on screens without EDID 1.4 */
12108 if (connector->base.display_info.bpc == 0) {
12109 int type = connector->base.connector_type;
12110 int clamp_bpp = 24;
12111
12112 /* Fall back to 18 bpp when DP sink capability is unknown. */
12113 if (type == DRM_MODE_CONNECTOR_DisplayPort ||
12114 type == DRM_MODE_CONNECTOR_eDP)
12115 clamp_bpp = 18;
12116
12117 if (bpp > clamp_bpp) {
12118 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
12119 bpp, clamp_bpp);
12120 pipe_config->pipe_bpp = clamp_bpp;
12121 }
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012122 }
12123}
12124
12125static int
12126compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012127 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012128{
12129 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012130 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012131 struct drm_connector *connector;
12132 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012133 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012134
Wayne Boyer666a4532015-12-09 12:29:35 -080012135 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012136 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012137 else if (INTEL_INFO(dev)->gen >= 5)
12138 bpp = 12*3;
12139 else
12140 bpp = 8*3;
12141
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012142
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012143 pipe_config->pipe_bpp = bpp;
12144
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012145 state = pipe_config->base.state;
12146
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012147 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012148 for_each_connector_in_state(state, connector, connector_state, i) {
12149 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012150 continue;
12151
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012152 connected_sink_compute_bpp(to_intel_connector(connector),
12153 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012154 }
12155
12156 return bpp;
12157}
12158
Daniel Vetter644db712013-09-19 14:53:58 +020012159static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12160{
12161 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12162 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012163 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012164 mode->crtc_hdisplay, mode->crtc_hsync_start,
12165 mode->crtc_hsync_end, mode->crtc_htotal,
12166 mode->crtc_vdisplay, mode->crtc_vsync_start,
12167 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12168}
12169
Daniel Vetterc0b03412013-05-28 12:05:54 +020012170static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012171 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012172 const char *context)
12173{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012174 struct drm_device *dev = crtc->base.dev;
12175 struct drm_plane *plane;
12176 struct intel_plane *intel_plane;
12177 struct intel_plane_state *state;
12178 struct drm_framebuffer *fb;
12179
Ville Syrjälä78108b72016-05-27 20:59:19 +030012180 DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
12181 crtc->base.base.id, crtc->base.name,
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012182 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012183
Jani Nikulada205632016-03-15 21:51:10 +020012184 DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012185 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12186 pipe_config->pipe_bpp, pipe_config->dither);
12187 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12188 pipe_config->has_pch_encoder,
12189 pipe_config->fdi_lanes,
12190 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12191 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12192 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012193 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
Ville Syrjälä37a56502016-06-22 21:57:04 +030012194 intel_crtc_has_dp_encoder(pipe_config),
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012195 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012196 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12197 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12198 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012199
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012200 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
Ville Syrjälä37a56502016-06-22 21:57:04 +030012201 intel_crtc_has_dp_encoder(pipe_config),
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012202 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012203 pipe_config->dp_m2_n2.gmch_m,
12204 pipe_config->dp_m2_n2.gmch_n,
12205 pipe_config->dp_m2_n2.link_m,
12206 pipe_config->dp_m2_n2.link_n,
12207 pipe_config->dp_m2_n2.tu);
12208
Daniel Vetter55072d12014-11-20 16:10:28 +010012209 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12210 pipe_config->has_audio,
12211 pipe_config->has_infoframe);
12212
Daniel Vetterc0b03412013-05-28 12:05:54 +020012213 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012214 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012215 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012216 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12217 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012218 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012219 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12220 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012221 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12222 crtc->num_scalers,
12223 pipe_config->scaler_state.scaler_users,
12224 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012225 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12226 pipe_config->gmch_pfit.control,
12227 pipe_config->gmch_pfit.pgm_ratios,
12228 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012229 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012230 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012231 pipe_config->pch_pfit.size,
12232 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012233 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012234 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012235
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012236 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012237 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012238 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012239 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012240 pipe_config->ddi_pll_sel,
12241 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012242 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012243 pipe_config->dpll_hw_state.pll0,
12244 pipe_config->dpll_hw_state.pll1,
12245 pipe_config->dpll_hw_state.pll2,
12246 pipe_config->dpll_hw_state.pll3,
12247 pipe_config->dpll_hw_state.pll6,
12248 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012249 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012250 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012251 pipe_config->dpll_hw_state.pcsdw12);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070012252 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012253 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12254 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12255 pipe_config->ddi_pll_sel,
12256 pipe_config->dpll_hw_state.ctrl1,
12257 pipe_config->dpll_hw_state.cfgcr1,
12258 pipe_config->dpll_hw_state.cfgcr2);
12259 } else if (HAS_DDI(dev)) {
Ville Syrjälä1260f072016-02-17 21:41:08 +020012260 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 +010012261 pipe_config->ddi_pll_sel,
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012262 pipe_config->dpll_hw_state.wrpll,
12263 pipe_config->dpll_hw_state.spll);
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012264 } else {
12265 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12266 "fp0: 0x%x, fp1: 0x%x\n",
12267 pipe_config->dpll_hw_state.dpll,
12268 pipe_config->dpll_hw_state.dpll_md,
12269 pipe_config->dpll_hw_state.fp0,
12270 pipe_config->dpll_hw_state.fp1);
12271 }
12272
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012273 DRM_DEBUG_KMS("planes on this crtc\n");
12274 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12275 intel_plane = to_intel_plane(plane);
12276 if (intel_plane->pipe != crtc->pipe)
12277 continue;
12278
12279 state = to_intel_plane_state(plane->state);
12280 fb = state->base.fb;
12281 if (!fb) {
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012282 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12283 plane->base.id, plane->name, state->scaler_id);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012284 continue;
12285 }
12286
Ville Syrjälä1d577e02016-05-27 20:59:25 +030012287 DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
12288 plane->base.id, plane->name);
12289 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
12290 fb->base.id, fb->width, fb->height,
12291 drm_get_format_name(fb->pixel_format));
12292 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12293 state->scaler_id,
12294 state->src.x1 >> 16, state->src.y1 >> 16,
12295 drm_rect_width(&state->src) >> 16,
12296 drm_rect_height(&state->src) >> 16,
12297 state->dst.x1, state->dst.y1,
12298 drm_rect_width(&state->dst),
12299 drm_rect_height(&state->dst));
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012300 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012301}
12302
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012303static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012304{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012305 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012306 struct drm_connector *connector;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012307 unsigned int used_ports = 0;
12308
12309 /*
12310 * Walk the connector list instead of the encoder
12311 * list to detect the problem on ddi platforms
12312 * where there's just one encoder per digital port.
12313 */
Ville Syrjälä0bff4852015-12-10 18:22:31 +020012314 drm_for_each_connector(connector, dev) {
12315 struct drm_connector_state *connector_state;
12316 struct intel_encoder *encoder;
12317
12318 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12319 if (!connector_state)
12320 connector_state = connector->state;
12321
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012322 if (!connector_state->best_encoder)
12323 continue;
12324
12325 encoder = to_intel_encoder(connector_state->best_encoder);
12326
12327 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012328
12329 switch (encoder->type) {
12330 unsigned int port_mask;
12331 case INTEL_OUTPUT_UNKNOWN:
12332 if (WARN_ON(!HAS_DDI(dev)))
12333 break;
Ville Syrjäläcca05022016-06-22 21:57:06 +030012334 case INTEL_OUTPUT_DP:
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012335 case INTEL_OUTPUT_HDMI:
12336 case INTEL_OUTPUT_EDP:
12337 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12338
12339 /* the same port mustn't appear more than once */
12340 if (used_ports & port_mask)
12341 return false;
12342
12343 used_ports |= port_mask;
12344 default:
12345 break;
12346 }
12347 }
12348
12349 return true;
12350}
12351
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012352static void
12353clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12354{
12355 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012356 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012357 struct intel_dpll_hw_state dpll_hw_state;
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012358 struct intel_shared_dpll *shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012359 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012360 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012361
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012362 /* FIXME: before the switch to atomic started, a new pipe_config was
12363 * kzalloc'd. Code that depends on any field being zero should be
12364 * fixed, so that the crtc_state can be safely duplicated. For now,
12365 * only fields that are know to not cause problems are preserved. */
12366
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012367 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012368 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012369 shared_dpll = crtc_state->shared_dpll;
12370 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012371 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012372 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012373
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012374 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012375
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012376 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012377 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012378 crtc_state->shared_dpll = shared_dpll;
12379 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012380 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012381 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012382}
12383
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012384static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012385intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012386 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012387{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012388 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012389 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012390 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012391 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012392 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012393 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012394 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012395
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012396 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012397
Daniel Vettere143a212013-07-04 12:01:15 +020012398 pipe_config->cpu_transcoder =
12399 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012400
Imre Deak2960bc92013-07-30 13:36:32 +030012401 /*
12402 * Sanitize sync polarity flags based on requested ones. If neither
12403 * positive or negative polarity is requested, treat this as meaning
12404 * negative polarity.
12405 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012406 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012407 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012408 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012409
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012410 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012411 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012412 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012413
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012414 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12415 pipe_config);
12416 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012417 goto fail;
12418
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012419 /*
12420 * Determine the real pipe dimensions. Note that stereo modes can
12421 * increase the actual pipe size due to the frame doubling and
12422 * insertion of additional space for blanks between the frame. This
12423 * is stored in the crtc timings. We use the requested mode to do this
12424 * computation to clearly distinguish it from the adjusted mode, which
12425 * can be changed by the connectors in the below retry loop.
12426 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012427 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012428 &pipe_config->pipe_src_w,
12429 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012430
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012431 for_each_connector_in_state(state, connector, connector_state, i) {
12432 if (connector_state->crtc != crtc)
12433 continue;
12434
12435 encoder = to_intel_encoder(connector_state->best_encoder);
12436
Ville Syrjäläe25148d2016-06-22 21:57:09 +030012437 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
12438 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12439 goto fail;
12440 }
12441
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012442 /*
12443 * Determine output_types before calling the .compute_config()
12444 * hooks so that the hooks can use this information safely.
12445 */
12446 pipe_config->output_types |= 1 << encoder->type;
12447 }
12448
Daniel Vettere29c22c2013-02-21 00:00:16 +010012449encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012450 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012451 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012452 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012453
Daniel Vetter135c81b2013-07-21 21:37:09 +020012454 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012455 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12456 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012457
Daniel Vetter7758a112012-07-08 19:40:39 +020012458 /* Pass our mode to the connectors and the CRTC to give them a chance to
12459 * adjust it according to limitations or connector properties, and also
12460 * a chance to reject the mode entirely.
12461 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012462 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012463 if (connector_state->crtc != crtc)
12464 continue;
12465
12466 encoder = to_intel_encoder(connector_state->best_encoder);
12467
Daniel Vetterefea6e82013-07-21 21:36:59 +020012468 if (!(encoder->compute_config(encoder, pipe_config))) {
12469 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012470 goto fail;
12471 }
12472 }
12473
Daniel Vetterff9a6752013-06-01 17:16:21 +020012474 /* Set default port clock if not overwritten by the encoder. Needs to be
12475 * done afterwards in case the encoder adjusts the mode. */
12476 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012477 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012478 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012479
Daniel Vettera43f6e02013-06-07 23:10:32 +020012480 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012481 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012482 DRM_DEBUG_KMS("CRTC fixup failed\n");
12483 goto fail;
12484 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012485
12486 if (ret == RETRY) {
12487 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12488 ret = -EINVAL;
12489 goto fail;
12490 }
12491
12492 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12493 retry = false;
12494 goto encoder_retry;
12495 }
12496
Daniel Vettere8fa4272015-08-12 11:43:34 +020012497 /* Dithering seems to not pass-through bits correctly when it should, so
12498 * only enable it on 6bpc panels. */
12499 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012500 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012501 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012502
Daniel Vetter7758a112012-07-08 19:40:39 +020012503fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012504 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012505}
12506
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012507static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012508intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012509{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012510 struct drm_crtc *crtc;
12511 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012512 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012513
Ville Syrjälä76688512014-01-10 11:28:06 +020012514 /* Double check state. */
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012515 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012516 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012517
12518 /* Update hwmode for vblank functions */
12519 if (crtc->state->active)
12520 crtc->hwmode = crtc->state->adjusted_mode;
12521 else
12522 crtc->hwmode.crtc_clock = 0;
Maarten Lankhorst61067a52015-09-23 16:29:36 +020012523
12524 /*
12525 * Update legacy state to satisfy fbc code. This can
12526 * be removed when fbc uses the atomic state.
12527 */
12528 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
12529 struct drm_plane_state *plane_state = crtc->primary->state;
12530
12531 crtc->primary->fb = plane_state->fb;
12532 crtc->x = plane_state->src_x >> 16;
12533 crtc->y = plane_state->src_y >> 16;
12534 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012535 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012536}
12537
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012538static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012539{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012540 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012541
12542 if (clock1 == clock2)
12543 return true;
12544
12545 if (!clock1 || !clock2)
12546 return false;
12547
12548 diff = abs(clock1 - clock2);
12549
12550 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12551 return true;
12552
12553 return false;
12554}
12555
Daniel Vetter25c5b262012-07-08 22:08:04 +020012556#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12557 list_for_each_entry((intel_crtc), \
12558 &(dev)->mode_config.crtc_list, \
12559 base.head) \
Jani Nikula95150bd2015-11-24 21:21:56 +020012560 for_each_if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012561
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012562static bool
12563intel_compare_m_n(unsigned int m, unsigned int n,
12564 unsigned int m2, unsigned int n2,
12565 bool exact)
12566{
12567 if (m == m2 && n == n2)
12568 return true;
12569
12570 if (exact || !m || !n || !m2 || !n2)
12571 return false;
12572
12573 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12574
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012575 if (n > n2) {
12576 while (n > n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012577 m2 <<= 1;
12578 n2 <<= 1;
12579 }
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012580 } else if (n < n2) {
12581 while (n < n2) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012582 m <<= 1;
12583 n <<= 1;
12584 }
12585 }
12586
Maarten Lankhorst31d10b52016-01-06 13:54:43 +010012587 if (n != n2)
12588 return false;
12589
12590 return intel_fuzzy_clock_check(m, m2);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012591}
12592
12593static bool
12594intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12595 struct intel_link_m_n *m2_n2,
12596 bool adjust)
12597{
12598 if (m_n->tu == m2_n2->tu &&
12599 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12600 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12601 intel_compare_m_n(m_n->link_m, m_n->link_n,
12602 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12603 if (adjust)
12604 *m2_n2 = *m_n;
12605
12606 return true;
12607 }
12608
12609 return false;
12610}
12611
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012612static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012613intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012614 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012615 struct intel_crtc_state *pipe_config,
12616 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012617{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012618 bool ret = true;
12619
12620#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12621 do { \
12622 if (!adjust) \
12623 DRM_ERROR(fmt, ##__VA_ARGS__); \
12624 else \
12625 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12626 } while (0)
12627
Daniel Vetter66e985c2013-06-05 13:34:20 +020012628#define PIPE_CONF_CHECK_X(name) \
12629 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012630 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012631 "(expected 0x%08x, found 0x%08x)\n", \
12632 current_config->name, \
12633 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012634 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012635 }
12636
Daniel Vetter08a24032013-04-19 11:25:34 +020012637#define PIPE_CONF_CHECK_I(name) \
12638 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012639 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012640 "(expected %i, found %i)\n", \
12641 current_config->name, \
12642 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012643 ret = false; \
12644 }
12645
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012646#define PIPE_CONF_CHECK_P(name) \
12647 if (current_config->name != pipe_config->name) { \
12648 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12649 "(expected %p, found %p)\n", \
12650 current_config->name, \
12651 pipe_config->name); \
12652 ret = false; \
12653 }
12654
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012655#define PIPE_CONF_CHECK_M_N(name) \
12656 if (!intel_compare_link_m_n(&current_config->name, \
12657 &pipe_config->name,\
12658 adjust)) { \
12659 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12660 "(expected tu %i gmch %i/%i link %i/%i, " \
12661 "found tu %i, gmch %i/%i link %i/%i)\n", \
12662 current_config->name.tu, \
12663 current_config->name.gmch_m, \
12664 current_config->name.gmch_n, \
12665 current_config->name.link_m, \
12666 current_config->name.link_n, \
12667 pipe_config->name.tu, \
12668 pipe_config->name.gmch_m, \
12669 pipe_config->name.gmch_n, \
12670 pipe_config->name.link_m, \
12671 pipe_config->name.link_n); \
12672 ret = false; \
12673 }
12674
Daniel Vetter55c561a2016-03-30 11:34:36 +020012675/* This is required for BDW+ where there is only one set of registers for
12676 * switching between high and low RR.
12677 * This macro can be used whenever a comparison has to be made between one
12678 * hw state and multiple sw state variables.
12679 */
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012680#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12681 if (!intel_compare_link_m_n(&current_config->name, \
12682 &pipe_config->name, adjust) && \
12683 !intel_compare_link_m_n(&current_config->alt_name, \
12684 &pipe_config->name, adjust)) { \
12685 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12686 "(expected tu %i gmch %i/%i link %i/%i, " \
12687 "or tu %i gmch %i/%i link %i/%i, " \
12688 "found tu %i, gmch %i/%i link %i/%i)\n", \
12689 current_config->name.tu, \
12690 current_config->name.gmch_m, \
12691 current_config->name.gmch_n, \
12692 current_config->name.link_m, \
12693 current_config->name.link_n, \
12694 current_config->alt_name.tu, \
12695 current_config->alt_name.gmch_m, \
12696 current_config->alt_name.gmch_n, \
12697 current_config->alt_name.link_m, \
12698 current_config->alt_name.link_n, \
12699 pipe_config->name.tu, \
12700 pipe_config->name.gmch_m, \
12701 pipe_config->name.gmch_n, \
12702 pipe_config->name.link_m, \
12703 pipe_config->name.link_n); \
12704 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012705 }
12706
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012707#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12708 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012709 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012710 "(expected %i, found %i)\n", \
12711 current_config->name & (mask), \
12712 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012713 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012714 }
12715
Ville Syrjälä5e550652013-09-06 23:29:07 +030012716#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12717 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012718 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012719 "(expected %i, found %i)\n", \
12720 current_config->name, \
12721 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012722 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012723 }
12724
Daniel Vetterbb760062013-06-06 14:55:52 +020012725#define PIPE_CONF_QUIRK(quirk) \
12726 ((current_config->quirks | pipe_config->quirks) & (quirk))
12727
Daniel Vettereccb1402013-05-22 00:50:22 +020012728 PIPE_CONF_CHECK_I(cpu_transcoder);
12729
Daniel Vetter08a24032013-04-19 11:25:34 +020012730 PIPE_CONF_CHECK_I(has_pch_encoder);
12731 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012732 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012733
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012734 PIPE_CONF_CHECK_I(lane_count);
Imre Deak95a7a2a2016-06-13 16:44:35 +030012735 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012736
12737 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012738 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012739
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012740 if (current_config->has_drrs)
12741 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12742 } else
12743 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012744
Ville Syrjälä253c84c2016-06-22 21:57:01 +030012745 PIPE_CONF_CHECK_X(output_types);
Jani Nikulaa65347b2015-11-27 12:21:46 +020012746
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012747 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12748 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12749 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12750 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12751 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12752 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012753
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012754 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12755 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12756 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12757 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12758 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12759 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012760
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012761 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012762 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012763 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
Wayne Boyer666a4532015-12-09 12:29:35 -080012764 IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012765 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012766 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012767
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012768 PIPE_CONF_CHECK_I(has_audio);
12769
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012770 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012771 DRM_MODE_FLAG_INTERLACE);
12772
Daniel Vetterbb760062013-06-06 14:55:52 +020012773 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012774 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012775 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012776 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012777 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012778 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012779 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012780 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012781 DRM_MODE_FLAG_NVSYNC);
12782 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012783
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012784 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012785 /* pfit ratios are autocomputed by the hw on gen4+ */
12786 if (INTEL_INFO(dev)->gen < 4)
Ville Syrjälä7f7d8dd2016-03-15 16:40:07 +020012787 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012788 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012789
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020012790 if (!adjust) {
12791 PIPE_CONF_CHECK_I(pipe_src_w);
12792 PIPE_CONF_CHECK_I(pipe_src_h);
12793
12794 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12795 if (current_config->pch_pfit.enabled) {
12796 PIPE_CONF_CHECK_X(pch_pfit.pos);
12797 PIPE_CONF_CHECK_X(pch_pfit.size);
12798 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012799
Maarten Lankhorst7aefe2b2015-09-14 11:30:10 +020012800 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12801 }
Chandra Kondurua1b22782015-04-07 15:28:45 -070012802
Jesse Barnese59150d2014-01-07 13:30:45 -080012803 /* BDW+ don't expose a synchronous way to read the state */
12804 if (IS_HASWELL(dev))
12805 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012806
Ville Syrjälä282740f2013-09-04 18:30:03 +030012807 PIPE_CONF_CHECK_I(double_wide);
12808
Daniel Vetter26804af2014-06-25 22:01:55 +030012809 PIPE_CONF_CHECK_X(ddi_pll_sel);
12810
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012811 PIPE_CONF_CHECK_P(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012812 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012813 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012814 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12815 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012816 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Maarten Lankhorst00490c22015-11-16 14:42:12 +010012817 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012818 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12819 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12820 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012821
Ville Syrjälä47eacba2016-04-12 22:14:35 +030012822 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12823 PIPE_CONF_CHECK_X(dsi_pll.div);
12824
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012825 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12826 PIPE_CONF_CHECK_I(pipe_bpp);
12827
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012828 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012829 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012830
Daniel Vetter66e985c2013-06-05 13:34:20 +020012831#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012832#undef PIPE_CONF_CHECK_I
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020012833#undef PIPE_CONF_CHECK_P
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012834#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012835#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012836#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012837#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012838
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012839 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012840}
12841
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012842static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12843 const struct intel_crtc_state *pipe_config)
12844{
12845 if (pipe_config->has_pch_encoder) {
Ville Syrjälä21a727b2016-02-17 21:41:10 +020012846 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020012847 &pipe_config->fdi_m_n);
12848 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12849
12850 /*
12851 * FDI already provided one idea for the dotclock.
12852 * Yell if the encoder disagrees.
12853 */
12854 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12855 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12856 fdi_dotclock, dotclock);
12857 }
12858}
12859
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012860static void verify_wm_state(struct drm_crtc *crtc,
12861 struct drm_crtc_state *new_state)
Damien Lespiau08db6652014-11-04 17:06:52 +000012862{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012863 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012864 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau08db6652014-11-04 17:06:52 +000012865 struct skl_ddb_allocation hw_ddb, *sw_ddb;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012866 struct skl_ddb_entry *hw_entry, *sw_entry;
12867 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12868 const enum pipe pipe = intel_crtc->pipe;
Damien Lespiau08db6652014-11-04 17:06:52 +000012869 int plane;
12870
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012871 if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
Damien Lespiau08db6652014-11-04 17:06:52 +000012872 return;
12873
12874 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12875 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12876
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012877 /* planes */
12878 for_each_plane(dev_priv, pipe, plane) {
12879 hw_entry = &hw_ddb.plane[pipe][plane];
12880 sw_entry = &sw_ddb->plane[pipe][plane];
Damien Lespiau08db6652014-11-04 17:06:52 +000012881
12882 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12883 continue;
12884
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012885 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12886 "(expected (%u,%u), found (%u,%u))\n",
12887 pipe_name(pipe), plane + 1,
12888 sw_entry->start, sw_entry->end,
12889 hw_entry->start, hw_entry->end);
12890 }
12891
12892 /* cursor */
12893 hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12894 sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
12895
12896 if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012897 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12898 "(expected (%u,%u), found (%u,%u))\n",
12899 pipe_name(pipe),
12900 sw_entry->start, sw_entry->end,
12901 hw_entry->start, hw_entry->end);
12902 }
12903}
12904
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012905static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012906verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012907{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012908 struct drm_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012909
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012910 drm_for_each_connector(connector, dev) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012911 struct drm_encoder *encoder = connector->encoder;
12912 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012913
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012914 if (state->crtc != crtc)
12915 continue;
12916
Daniel Vetter5a21b662016-05-24 17:13:53 +020012917 intel_connector_verify_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012918
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012919 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012920 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012921 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012922}
12923
12924static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012925verify_encoder_state(struct drm_device *dev)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012926{
12927 struct intel_encoder *encoder;
12928 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012929
Damien Lespiaub2784e12014-08-05 11:29:37 +010012930 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012931 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012932 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012933
12934 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12935 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012936 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012937
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012938 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012939 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012940 continue;
12941 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012942
12943 I915_STATE_WARN(connector->base.state->crtc !=
12944 encoder->base.crtc,
12945 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012946 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012947
Rob Clarke2c719b2014-12-15 13:56:32 -050012948 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012949 "encoder's enabled state mismatch "
12950 "(expected %i, found %i)\n",
12951 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012952
12953 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012954 bool active;
12955
12956 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012957 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012958 "encoder detached but still enabled on pipe %c.\n",
12959 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012960 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012961 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012962}
12963
12964static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020012965verify_crtc_state(struct drm_crtc *crtc,
12966 struct drm_crtc_state *old_crtc_state,
12967 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012968{
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012969 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010012970 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012971 struct intel_encoder *encoder;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12973 struct intel_crtc_state *pipe_config, *sw_config;
12974 struct drm_atomic_state *old_state;
12975 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012976
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012977 old_state = old_crtc_state->state;
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020012978 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012979 pipe_config = to_intel_crtc_state(old_crtc_state);
12980 memset(pipe_config, 0, sizeof(*pipe_config));
12981 pipe_config->base.crtc = crtc;
12982 pipe_config->base.state = old_state;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012983
Ville Syrjälä78108b72016-05-27 20:59:19 +030012984 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012985
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012986 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012987
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012988 /* hw state is inconsistent with the pipe quirk */
12989 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12990 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12991 active = new_crtc_state->active;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012992
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012993 I915_STATE_WARN(new_crtc_state->active != active,
12994 "crtc active state doesn't match with hw state "
12995 "(expected %i, found %i)\n", new_crtc_state->active, active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012996
Maarten Lankhorste7c84542016-03-23 14:58:06 +010012997 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
12998 "transitional active state does not match atomic hw state "
12999 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013000
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013001 for_each_encoder_on_crtc(dev, crtc, encoder) {
13002 enum pipe pipe;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013003
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013004 active = encoder->get_hw_state(encoder, &pipe);
13005 I915_STATE_WARN(active != new_crtc_state->active,
13006 "[ENCODER:%i] active %i with crtc active %i\n",
13007 encoder->base.base.id, active, new_crtc_state->active);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013008
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013009 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13010 "Encoder connected to wrong pipe %c\n",
13011 pipe_name(pipe));
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013012
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013013 if (active) {
13014 pipe_config->output_types |= 1 << encoder->type;
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013015 encoder->get_config(encoder, pipe_config);
Ville Syrjälä253c84c2016-06-22 21:57:01 +030013016 }
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013017 }
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013018
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013019 if (!new_crtc_state->active)
13020 return;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013021
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013022 intel_pipe_config_sanity_check(dev_priv, pipe_config);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020013023
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013024 sw_config = to_intel_crtc_state(crtc->state);
13025 if (!intel_pipe_config_compare(dev, sw_config,
13026 pipe_config, false)) {
13027 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13028 intel_dump_pipe_config(intel_crtc, pipe_config,
13029 "[hw state]");
13030 intel_dump_pipe_config(intel_crtc, sw_config,
13031 "[sw state]");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013032 }
13033}
13034
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013035static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013036verify_single_dpll_state(struct drm_i915_private *dev_priv,
13037 struct intel_shared_dpll *pll,
13038 struct drm_crtc *crtc,
13039 struct drm_crtc_state *new_state)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013040{
13041 struct intel_dpll_hw_state dpll_hw_state;
13042 unsigned crtc_mask;
13043 bool active;
13044
13045 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13046
13047 DRM_DEBUG_KMS("%s\n", pll->name);
13048
13049 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
13050
13051 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13052 I915_STATE_WARN(!pll->on && pll->active_mask,
13053 "pll in active use but not on in sw tracking\n");
13054 I915_STATE_WARN(pll->on && !pll->active_mask,
13055 "pll is on but not used by any active crtc\n");
13056 I915_STATE_WARN(pll->on != active,
13057 "pll on state mismatch (expected %i, found %i)\n",
13058 pll->on, active);
13059 }
13060
13061 if (!crtc) {
13062 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
13063 "more active pll users than references: %x vs %x\n",
13064 pll->active_mask, pll->config.crtc_mask);
13065
13066 return;
13067 }
13068
13069 crtc_mask = 1 << drm_crtc_index(crtc);
13070
13071 if (new_state->active)
13072 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13073 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13074 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13075 else
13076 I915_STATE_WARN(pll->active_mask & crtc_mask,
13077 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13078 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13079
13080 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13081 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13082 crtc_mask, pll->config.crtc_mask);
13083
13084 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13085 &dpll_hw_state,
13086 sizeof(dpll_hw_state)),
13087 "pll hw state mismatch\n");
13088}
13089
13090static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013091verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13092 struct drm_crtc_state *old_crtc_state,
13093 struct drm_crtc_state *new_crtc_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013094{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013095 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013096 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13097 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13098
13099 if (new_state->shared_dpll)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013100 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013101
13102 if (old_state->shared_dpll &&
13103 old_state->shared_dpll != new_state->shared_dpll) {
13104 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13105 struct intel_shared_dpll *pll = old_state->shared_dpll;
13106
13107 I915_STATE_WARN(pll->active_mask & crtc_mask,
13108 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13109 pipe_name(drm_crtc_index(crtc)));
13110 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13111 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13112 pipe_name(drm_crtc_index(crtc)));
13113 }
13114}
13115
13116static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013117intel_modeset_verify_crtc(struct drm_crtc *crtc,
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013118 struct drm_crtc_state *old_state,
13119 struct drm_crtc_state *new_state)
13120{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013121 if (!needs_modeset(new_state) &&
13122 !to_intel_crtc_state(new_state)->update_pipe)
13123 return;
13124
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013125 verify_wm_state(crtc, new_state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013126 verify_connector_state(crtc->dev, crtc);
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013127 verify_crtc_state(crtc, old_state, new_state);
13128 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013129}
13130
13131static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013132verify_disabled_dpll_state(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013133{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013134 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020013135 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020013136
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013137 for (i = 0; i < dev_priv->num_shared_dpll; i++)
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013138 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013139}
Daniel Vetter53589012013-06-05 13:34:16 +020013140
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013141static void
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013142intel_modeset_verify_disabled(struct drm_device *dev)
Maarten Lankhorste7c84542016-03-23 14:58:06 +010013143{
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013144 verify_encoder_state(dev);
13145 verify_connector_state(dev, NULL);
13146 verify_disabled_dpll_state(dev);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020013147}
13148
Ville Syrjälä80715b22014-05-15 20:23:23 +030013149static void update_scanline_offset(struct intel_crtc *crtc)
13150{
13151 struct drm_device *dev = crtc->base.dev;
13152
13153 /*
13154 * The scanline counter increments at the leading edge of hsync.
13155 *
13156 * On most platforms it starts counting from vtotal-1 on the
13157 * first active line. That means the scanline counter value is
13158 * always one less than what we would expect. Ie. just after
13159 * start of vblank, which also occurs at start of hsync (on the
13160 * last active line), the scanline counter will read vblank_start-1.
13161 *
13162 * On gen2 the scanline counter starts counting from 1 instead
13163 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13164 * to keep the value positive), instead of adding one.
13165 *
13166 * On HSW+ the behaviour of the scanline counter depends on the output
13167 * type. For DP ports it behaves like most other platforms, but on HDMI
13168 * there's an extra 1 line difference. So we need to add two instead of
13169 * one to the value.
13170 */
13171 if (IS_GEN2(dev)) {
Ville Syrjälä124abe02015-09-08 13:40:45 +030013172 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013173 int vtotal;
13174
Ville Syrjälä124abe02015-09-08 13:40:45 +030013175 vtotal = adjusted_mode->crtc_vtotal;
13176 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä80715b22014-05-15 20:23:23 +030013177 vtotal /= 2;
13178
13179 crtc->scanline_offset = vtotal - 1;
13180 } else if (HAS_DDI(dev) &&
Ville Syrjälä2d84d2b2016-06-22 21:57:02 +030013181 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030013182 crtc->scanline_offset = 2;
13183 } else
13184 crtc->scanline_offset = 1;
13185}
13186
Maarten Lankhorstad421372015-06-15 12:33:42 +020013187static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013188{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013189 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013190 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013191 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013192 struct drm_crtc *crtc;
13193 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013194 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013195
13196 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013197 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013198
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013199 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013200 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013201 struct intel_shared_dpll *old_dpll =
13202 to_intel_crtc_state(crtc->state)->shared_dpll;
Maarten Lankhorstad421372015-06-15 12:33:42 +020013203
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013204 if (!needs_modeset(crtc_state))
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013205 continue;
13206
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013207 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013208
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013209 if (!old_dpll)
Maarten Lankhorstfb1a38a2016-02-09 13:02:17 +010013210 continue;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013211
Maarten Lankhorstad421372015-06-15 12:33:42 +020013212 if (!shared_dpll)
13213 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13214
Ander Conselvan de Oliveira8106ddb2016-03-08 17:46:18 +020013215 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013216 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013217}
13218
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013219/*
13220 * This implements the workaround described in the "notes" section of the mode
13221 * set sequence documentation. When going from no pipes or single pipe to
13222 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13223 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13224 */
13225static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13226{
13227 struct drm_crtc_state *crtc_state;
13228 struct intel_crtc *intel_crtc;
13229 struct drm_crtc *crtc;
13230 struct intel_crtc_state *first_crtc_state = NULL;
13231 struct intel_crtc_state *other_crtc_state = NULL;
13232 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13233 int i;
13234
13235 /* look at all crtc's that are going to be enabled in during modeset */
13236 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13237 intel_crtc = to_intel_crtc(crtc);
13238
13239 if (!crtc_state->active || !needs_modeset(crtc_state))
13240 continue;
13241
13242 if (first_crtc_state) {
13243 other_crtc_state = to_intel_crtc_state(crtc_state);
13244 break;
13245 } else {
13246 first_crtc_state = to_intel_crtc_state(crtc_state);
13247 first_pipe = intel_crtc->pipe;
13248 }
13249 }
13250
13251 /* No workaround needed? */
13252 if (!first_crtc_state)
13253 return 0;
13254
13255 /* w/a possibly needed, check how many crtc's are already enabled. */
13256 for_each_intel_crtc(state->dev, intel_crtc) {
13257 struct intel_crtc_state *pipe_config;
13258
13259 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13260 if (IS_ERR(pipe_config))
13261 return PTR_ERR(pipe_config);
13262
13263 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13264
13265 if (!pipe_config->base.active ||
13266 needs_modeset(&pipe_config->base))
13267 continue;
13268
13269 /* 2 or more enabled crtcs means no need for w/a */
13270 if (enabled_pipe != INVALID_PIPE)
13271 return 0;
13272
13273 enabled_pipe = intel_crtc->pipe;
13274 }
13275
13276 if (enabled_pipe != INVALID_PIPE)
13277 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13278 else if (other_crtc_state)
13279 other_crtc_state->hsw_workaround_pipe = first_pipe;
13280
13281 return 0;
13282}
13283
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013284static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13285{
13286 struct drm_crtc *crtc;
13287 struct drm_crtc_state *crtc_state;
13288 int ret = 0;
13289
13290 /* add all active pipes to the state */
13291 for_each_crtc(state->dev, crtc) {
13292 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13293 if (IS_ERR(crtc_state))
13294 return PTR_ERR(crtc_state);
13295
13296 if (!crtc_state->active || needs_modeset(crtc_state))
13297 continue;
13298
13299 crtc_state->mode_changed = true;
13300
13301 ret = drm_atomic_add_affected_connectors(state, crtc);
13302 if (ret)
13303 break;
13304
13305 ret = drm_atomic_add_affected_planes(state, crtc);
13306 if (ret)
13307 break;
13308 }
13309
13310 return ret;
13311}
13312
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013313static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013314{
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013315 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013316 struct drm_i915_private *dev_priv = to_i915(state->dev);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013317 struct drm_crtc *crtc;
13318 struct drm_crtc_state *crtc_state;
13319 int ret = 0, i;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013320
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013321 if (!check_digital_port_conflicts(state)) {
13322 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13323 return -EINVAL;
13324 }
13325
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013326 intel_state->modeset = true;
13327 intel_state->active_crtcs = dev_priv->active_crtcs;
13328
13329 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13330 if (crtc_state->active)
13331 intel_state->active_crtcs |= 1 << i;
13332 else
13333 intel_state->active_crtcs &= ~(1 << i);
Matt Roper8b4a7d02016-05-12 07:06:00 -070013334
13335 if (crtc_state->active != crtc->state->active)
13336 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013337 }
13338
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013339 /*
13340 * See if the config requires any additional preparation, e.g.
13341 * to adjust global state with pipes off. We need to do this
13342 * here so we can get the modeset_pipe updated config for the new
13343 * mode set on this crtc. For other crtcs we need to use the
13344 * adjusted_mode bits in the crtc directly.
13345 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013346 if (dev_priv->display.modeset_calc_cdclk) {
Clint Taylorc89e39f2016-05-13 23:41:21 +030013347 if (!intel_state->cdclk_pll_vco)
Ville Syrjälä63911d72016-05-13 23:41:32 +030013348 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
Ville Syrjäläb2045352016-05-13 23:41:27 +030013349 if (!intel_state->cdclk_pll_vco)
13350 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013351
Clint Taylorc89e39f2016-05-13 23:41:21 +030013352 ret = dev_priv->display.modeset_calc_cdclk(state);
13353 if (ret < 0)
13354 return ret;
13355
13356 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013357 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013358 ret = intel_modeset_all_pipes(state);
13359
13360 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013361 return ret;
Maarten Lankhorste8788cb2016-02-16 10:25:11 +010013362
13363 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
13364 intel_state->cdclk, intel_state->dev_cdclk);
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013365 } else
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013366 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013367
Maarten Lankhorstad421372015-06-15 12:33:42 +020013368 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013369
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013370 if (IS_HASWELL(dev_priv))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013371 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013372
Maarten Lankhorstad421372015-06-15 12:33:42 +020013373 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013374}
13375
Matt Roperaa363132015-09-24 15:53:18 -070013376/*
13377 * Handle calculation of various watermark data at the end of the atomic check
13378 * phase. The code here should be run after the per-crtc and per-plane 'check'
13379 * handlers to ensure that all derived state has been updated.
13380 */
Matt Roper55994c22016-05-12 07:06:08 -070013381static int calc_watermark_data(struct drm_atomic_state *state)
Matt Roperaa363132015-09-24 15:53:18 -070013382{
13383 struct drm_device *dev = state->dev;
Matt Roper98d39492016-05-12 07:06:03 -070013384 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper98d39492016-05-12 07:06:03 -070013385
13386 /* Is there platform-specific watermark information to calculate? */
13387 if (dev_priv->display.compute_global_watermarks)
Matt Roper55994c22016-05-12 07:06:08 -070013388 return dev_priv->display.compute_global_watermarks(state);
13389
13390 return 0;
Matt Roperaa363132015-09-24 15:53:18 -070013391}
13392
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013393/**
13394 * intel_atomic_check - validate state object
13395 * @dev: drm device
13396 * @state: state to validate
13397 */
13398static int intel_atomic_check(struct drm_device *dev,
13399 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013400{
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013401 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roperaa363132015-09-24 15:53:18 -070013402 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013403 struct drm_crtc *crtc;
13404 struct drm_crtc_state *crtc_state;
13405 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013406 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013407
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013408 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013409 if (ret)
13410 return ret;
13411
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013412 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013413 struct intel_crtc_state *pipe_config =
13414 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013415
13416 /* Catch I915_MODE_FLAG_INHERITED */
13417 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13418 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013419
Daniel Vetter26495482015-07-15 14:15:52 +020013420 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013421 continue;
13422
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013423 if (!crtc_state->enable) {
13424 any_ms = true;
13425 continue;
13426 }
13427
Daniel Vetter26495482015-07-15 14:15:52 +020013428 /* FIXME: For only active_changed we shouldn't need to do any
13429 * state recomputation at all. */
13430
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013431 ret = drm_atomic_add_affected_connectors(state, crtc);
13432 if (ret)
13433 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013434
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013435 ret = intel_modeset_pipe_config(crtc, pipe_config);
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013436 if (ret) {
13437 intel_dump_pipe_config(to_intel_crtc(crtc),
13438 pipe_config, "[failed]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013439 return ret;
Maarten Lankhorst25aa1c32016-05-03 10:30:38 +020013440 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013441
Jani Nikula73831232015-11-19 10:26:30 +020013442 if (i915.fastboot &&
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013443 intel_pipe_config_compare(dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013444 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013445 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013446 crtc_state->mode_changed = false;
Maarten Lankhorstbfd16b22015-08-27 15:44:05 +020013447 to_intel_crtc_state(crtc_state)->update_pipe = true;
Daniel Vetter26495482015-07-15 14:15:52 +020013448 }
13449
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013450 if (needs_modeset(crtc_state))
Daniel Vetter26495482015-07-15 14:15:52 +020013451 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013452
Daniel Vetteraf4a8792016-05-09 09:31:25 +020013453 ret = drm_atomic_add_affected_planes(state, crtc);
13454 if (ret)
13455 return ret;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013456
Daniel Vetter26495482015-07-15 14:15:52 +020013457 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13458 needs_modeset(crtc_state) ?
13459 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013460 }
13461
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013462 if (any_ms) {
13463 ret = intel_modeset_checks(state);
13464
13465 if (ret)
13466 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013467 } else
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013468 intel_state->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013469
Paulo Zanonidd8b3bd2016-01-19 11:35:49 -020013470 ret = drm_atomic_helper_check_planes(dev, state);
Matt Roperaa363132015-09-24 15:53:18 -070013471 if (ret)
13472 return ret;
13473
Paulo Zanonif51be2e2016-01-19 11:35:50 -020013474 intel_fbc_choose_crtc(dev_priv, state);
Matt Roper55994c22016-05-12 07:06:08 -070013475 return calc_watermark_data(state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013476}
13477
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013478static int intel_atomic_prepare_commit(struct drm_device *dev,
13479 struct drm_atomic_state *state,
Maarten Lankhorst81072bf2016-04-26 16:11:45 +020013480 bool nonblock)
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013481{
Chris Wilsonfac5e232016-07-04 11:34:36 +010013482 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013483 struct drm_plane_state *plane_state;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013484 struct drm_crtc_state *crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013485 struct drm_plane *plane;
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013486 struct drm_crtc *crtc;
13487 int i, ret;
13488
Daniel Vetter5a21b662016-05-24 17:13:53 +020013489 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13490 if (state->legacy_cursor_update)
13491 continue;
13492
13493 ret = intel_crtc_wait_for_pending_flips(crtc);
13494 if (ret)
13495 return ret;
13496
13497 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
13498 flush_workqueue(dev_priv->wq);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013499 }
13500
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013501 ret = mutex_lock_interruptible(&dev->struct_mutex);
13502 if (ret)
13503 return ret;
13504
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013505 ret = drm_atomic_helper_prepare_planes(dev, state);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013506 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013507
Dave Airlie21daaee2016-05-05 09:56:30 +100013508 if (!ret && !nonblock) {
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013509 for_each_plane_in_state(state, plane, plane_state, i) {
13510 struct intel_plane_state *intel_plane_state =
13511 to_intel_plane_state(plane_state);
13512
13513 if (!intel_plane_state->wait_req)
13514 continue;
13515
13516 ret = __i915_wait_request(intel_plane_state->wait_req,
Chris Wilson299259a2016-04-13 17:35:06 +010013517 true, NULL, NULL);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013518 if (ret) {
Chris Wilsonf4457ae2016-04-13 17:35:08 +010013519 /* Any hang should be swallowed by the wait */
13520 WARN_ON(ret == -EIO);
Chris Wilsonf7e58382016-04-13 17:35:07 +010013521 mutex_lock(&dev->struct_mutex);
13522 drm_atomic_helper_cleanup_planes(dev, state);
13523 mutex_unlock(&dev->struct_mutex);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013524 break;
Chris Wilsonf7e58382016-04-13 17:35:07 +010013525 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013526 }
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013527 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013528
13529 return ret;
13530}
13531
Maarten Lankhorsta2991412016-05-17 15:07:48 +020013532u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13533{
13534 struct drm_device *dev = crtc->base.dev;
13535
13536 if (!dev->max_vblank_count)
13537 return drm_accurate_vblank_count(&crtc->base);
13538
13539 return dev->driver->get_vblank_counter(dev, crtc->pipe);
13540}
13541
Daniel Vetter5a21b662016-05-24 17:13:53 +020013542static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
13543 struct drm_i915_private *dev_priv,
13544 unsigned crtc_mask)
Maarten Lankhorste8861672016-02-24 11:24:26 +010013545{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013546 unsigned last_vblank_count[I915_MAX_PIPES];
13547 enum pipe pipe;
13548 int ret;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013549
Daniel Vetter5a21b662016-05-24 17:13:53 +020013550 if (!crtc_mask)
13551 return;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013552
Daniel Vetter5a21b662016-05-24 17:13:53 +020013553 for_each_pipe(dev_priv, pipe) {
13554 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Maarten Lankhorste8861672016-02-24 11:24:26 +010013555
Daniel Vetter5a21b662016-05-24 17:13:53 +020013556 if (!((1 << pipe) & crtc_mask))
Maarten Lankhorste8861672016-02-24 11:24:26 +010013557 continue;
13558
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013559 ret = drm_crtc_vblank_get(crtc);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013560 if (WARN_ON(ret != 0)) {
13561 crtc_mask &= ~(1 << pipe);
13562 continue;
13563 }
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013564
Daniel Vetter5a21b662016-05-24 17:13:53 +020013565 last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
13566 }
13567
13568 for_each_pipe(dev_priv, pipe) {
13569 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
13570 long lret;
13571
13572 if (!((1 << pipe) & crtc_mask))
13573 continue;
13574
13575 lret = wait_event_timeout(dev->vblank[pipe].queue,
13576 last_vblank_count[pipe] !=
13577 drm_crtc_vblank_count(crtc),
13578 msecs_to_jiffies(50));
13579
13580 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
13581
13582 drm_crtc_vblank_put(crtc);
Maarten Lankhorstd55dbd02016-05-17 15:08:04 +020013583 }
13584}
13585
Daniel Vetter5a21b662016-05-24 17:13:53 +020013586static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013587{
Daniel Vetter5a21b662016-05-24 17:13:53 +020013588 /* fb updated, need to unpin old fb */
13589 if (crtc_state->fb_changed)
13590 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013591
Daniel Vetter5a21b662016-05-24 17:13:53 +020013592 /* wm changes, need vblank before final wm's */
13593 if (crtc_state->update_wm_post)
13594 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013595
Daniel Vetter5a21b662016-05-24 17:13:53 +020013596 /*
13597 * cxsr is re-enabled after vblank.
13598 * This is already handled by crtc_state->update_wm_post,
13599 * but added for clarity.
13600 */
13601 if (crtc_state->disable_cxsr)
13602 return true;
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013603
Daniel Vetter5a21b662016-05-24 17:13:53 +020013604 return false;
Maarten Lankhorste8861672016-02-24 11:24:26 +010013605}
13606
Daniel Vetter94f05022016-06-14 18:01:00 +020013607static void intel_atomic_commit_tail(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013608{
Daniel Vetter94f05022016-06-14 18:01:00 +020013609 struct drm_device *dev = state->dev;
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013610 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013611 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013612 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013613 struct drm_crtc *crtc;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013614 struct intel_crtc_state *intel_cstate;
Daniel Vetter94f05022016-06-14 18:01:00 +020013615 struct drm_plane *plane;
13616 struct drm_plane_state *plane_state;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013617 bool hw_check = intel_state->modeset;
13618 unsigned long put_domains[I915_MAX_PIPES] = {};
13619 unsigned crtc_vblank_mask = 0;
Daniel Vetter94f05022016-06-14 18:01:00 +020013620 int i, ret;
Daniel Vettera6778b32012-07-02 09:56:42 +020013621
Daniel Vetter94f05022016-06-14 18:01:00 +020013622 for_each_plane_in_state(state, plane, plane_state, i) {
13623 struct intel_plane_state *intel_plane_state =
13624 to_intel_plane_state(plane_state);
Daniel Vetterea0000f2016-06-13 16:13:46 +020013625
Daniel Vetter94f05022016-06-14 18:01:00 +020013626 if (!intel_plane_state->wait_req)
13627 continue;
13628
13629 ret = __i915_wait_request(intel_plane_state->wait_req,
13630 true, NULL, NULL);
13631 /* EIO should be eaten, and we can't get interrupted in the
13632 * worker, and blocking commits have waited already. */
13633 WARN_ON(ret);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020013634 }
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013635
Daniel Vetterea0000f2016-06-13 16:13:46 +020013636 drm_atomic_helper_wait_for_dependencies(state);
13637
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013638 if (intel_state->modeset) {
13639 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
13640 sizeof(intel_state->min_pixclk));
13641 dev_priv->active_crtcs = intel_state->active_crtcs;
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010013642 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
Daniel Vetter5a21b662016-05-24 17:13:53 +020013643
13644 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013645 }
13646
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013647 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013648 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13649
Daniel Vetter5a21b662016-05-24 17:13:53 +020013650 if (needs_modeset(crtc->state) ||
13651 to_intel_crtc_state(crtc->state)->update_pipe) {
13652 hw_check = true;
13653
13654 put_domains[to_intel_crtc(crtc)->pipe] =
13655 modeset_get_crtc_power_domains(crtc,
13656 to_intel_crtc_state(crtc->state));
13657 }
13658
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013659 if (!needs_modeset(crtc->state))
13660 continue;
13661
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013662 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Daniel Vetter460da9162013-03-27 00:44:51 +010013663
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013664 if (old_crtc_state->active) {
13665 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013666 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013667 intel_crtc->active = false;
Paulo Zanoni58f9c0b2016-01-19 11:35:51 -020013668 intel_fbc_disable(intel_crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013669 intel_disable_shared_dpll(intel_crtc);
Ville Syrjälä9bbc8258a2015-11-20 22:09:20 +020013670
13671 /*
13672 * Underruns don't always raise
13673 * interrupts, so check manually.
13674 */
13675 intel_check_cpu_fifo_underruns(dev_priv);
13676 intel_check_pch_fifo_underruns(dev_priv);
Maarten Lankhorstb9001112015-11-19 16:07:16 +010013677
13678 if (!crtc->state->active)
13679 intel_update_watermarks(crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013680 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013681 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013682
Daniel Vetterea9d7582012-07-10 10:42:52 +020013683 /* Only after disabling all output pipelines that will be changed can we
13684 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013685 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013686
Maarten Lankhorst565602d2015-12-10 12:33:57 +010013687 if (intel_state->modeset) {
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013688 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013689
13690 if (dev_priv->display.modeset_commit_cdclk &&
Clint Taylorc89e39f2016-05-13 23:41:21 +030013691 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
Ville Syrjälä63911d72016-05-13 23:41:32 +030013692 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
Maarten Lankhorst33c8df892016-02-10 13:49:37 +010013693 dev_priv->display.modeset_commit_cdclk(state);
Maarten Lankhorstf6d19732016-03-23 14:58:07 +010013694
Maarten Lankhorstc0ead702016-03-30 10:00:05 +020013695 intel_modeset_verify_disabled(dev);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013696 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013697
Daniel Vettera6778b32012-07-02 09:56:42 +020013698 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013699 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013700 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13701 bool modeset = needs_modeset(crtc->state);
Daniel Vetter5a21b662016-05-24 17:13:53 +020013702 struct intel_crtc_state *pipe_config =
13703 to_intel_crtc_state(crtc->state);
Patrik Jakobsson9f836f92015-11-16 16:20:01 +010013704
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013705 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013706 update_scanline_offset(to_intel_crtc(crtc));
13707 dev_priv->display.crtc_enable(crtc);
13708 }
13709
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013710 /* Complete events for now disable pipes here. */
13711 if (modeset && !crtc->state->active && crtc->state->event) {
13712 spin_lock_irq(&dev->event_lock);
13713 drm_crtc_send_vblank_event(crtc, crtc->state->event);
13714 spin_unlock_irq(&dev->event_lock);
13715
13716 crtc->state->event = NULL;
13717 }
13718
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013719 if (!modeset)
Ville Syrjälä29ceb0e2016-03-09 19:07:27 +020013720 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013721
Daniel Vetter5a21b662016-05-24 17:13:53 +020013722 if (crtc->state->active &&
13723 drm_atomic_get_existing_plane_state(state, crtc->primary))
Maarten Lankhorstfaf68d92016-06-14 14:24:20 +020013724 intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013725
Daniel Vetter1f7528c2016-06-13 16:13:45 +020013726 if (crtc->state->active)
Daniel Vetter5a21b662016-05-24 17:13:53 +020013727 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
Maarten Lankhorsta6747b72016-05-17 15:08:01 +020013728
Daniel Vetter5a21b662016-05-24 17:13:53 +020013729 if (pipe_config->base.active && needs_vblank_wait(pipe_config))
13730 crtc_vblank_mask |= 1 << i;
Matt Ropered4a6a72016-02-23 17:20:13 -080013731 }
13732
Daniel Vetter94f05022016-06-14 18:01:00 +020013733 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
13734 * already, but still need the state for the delayed optimization. To
13735 * fix this:
13736 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
13737 * - schedule that vblank worker _before_ calling hw_done
13738 * - at the start of commit_tail, cancel it _synchrously
13739 * - switch over to the vblank wait helper in the core after that since
13740 * we don't need out special handling any more.
13741 */
Daniel Vetter5a21b662016-05-24 17:13:53 +020013742 if (!state->legacy_cursor_update)
13743 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
13744
13745 /*
13746 * Now that the vblank has passed, we can go ahead and program the
13747 * optimal watermarks on platforms that need two-step watermark
13748 * programming.
13749 *
13750 * TODO: Move this (and other cleanup) to an async worker eventually.
13751 */
13752 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13753 intel_cstate = to_intel_crtc_state(crtc->state);
13754
13755 if (dev_priv->display.optimize_watermarks)
13756 dev_priv->display.optimize_watermarks(intel_cstate);
13757 }
13758
13759 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
13760 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
13761
13762 if (put_domains[i])
13763 modeset_put_power_domains(dev_priv, put_domains[i]);
13764
13765 intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
13766 }
13767
Daniel Vetter94f05022016-06-14 18:01:00 +020013768 drm_atomic_helper_commit_hw_done(state);
13769
Daniel Vetter5a21b662016-05-24 17:13:53 +020013770 if (intel_state->modeset)
13771 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
13772
13773 mutex_lock(&dev->struct_mutex);
13774 drm_atomic_helper_cleanup_planes(dev, state);
13775 mutex_unlock(&dev->struct_mutex);
13776
Daniel Vetterea0000f2016-06-13 16:13:46 +020013777 drm_atomic_helper_commit_cleanup_done(state);
13778
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013779 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013780
Mika Kuoppala75714942015-12-16 09:26:48 +020013781 /* As one of the primary mmio accessors, KMS has a high likelihood
13782 * of triggering bugs in unclaimed access. After we finish
13783 * modesetting, see if an error has been flagged, and if so
13784 * enable debugging for the next modeset - and hope we catch
13785 * the culprit.
13786 *
13787 * XXX note that we assume display power is on at this point.
13788 * This might hold true now but we need to add pm helper to check
13789 * unclaimed only when the hardware is on, as atomic commits
13790 * can happen also when the device is completely off.
13791 */
13792 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
Daniel Vetter94f05022016-06-14 18:01:00 +020013793}
13794
13795static void intel_atomic_commit_work(struct work_struct *work)
13796{
13797 struct drm_atomic_state *state = container_of(work,
13798 struct drm_atomic_state,
13799 commit_work);
13800 intel_atomic_commit_tail(state);
13801}
13802
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013803static void intel_atomic_track_fbs(struct drm_atomic_state *state)
13804{
13805 struct drm_plane_state *old_plane_state;
13806 struct drm_plane *plane;
13807 struct drm_i915_gem_object *obj, *old_obj;
13808 struct intel_plane *intel_plane;
13809 int i;
13810
13811 mutex_lock(&state->dev->struct_mutex);
13812 for_each_plane_in_state(state, plane, old_plane_state, i) {
13813 obj = intel_fb_obj(plane->state->fb);
13814 old_obj = intel_fb_obj(old_plane_state->fb);
13815 intel_plane = to_intel_plane(plane);
13816
13817 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
13818 }
13819 mutex_unlock(&state->dev->struct_mutex);
13820}
13821
Daniel Vetter94f05022016-06-14 18:01:00 +020013822/**
13823 * intel_atomic_commit - commit validated state object
13824 * @dev: DRM device
13825 * @state: the top-level driver state object
13826 * @nonblock: nonblocking commit
13827 *
13828 * This function commits a top-level state object that has been validated
13829 * with drm_atomic_helper_check().
13830 *
13831 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13832 * nonblocking commits are only safe for pure plane updates. Everything else
13833 * should work though.
13834 *
13835 * RETURNS
13836 * Zero for success or -errno.
13837 */
13838static int intel_atomic_commit(struct drm_device *dev,
13839 struct drm_atomic_state *state,
13840 bool nonblock)
13841{
13842 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Chris Wilsonfac5e232016-07-04 11:34:36 +010013843 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter94f05022016-06-14 18:01:00 +020013844 int ret = 0;
13845
13846 if (intel_state->modeset && nonblock) {
13847 DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
13848 return -EINVAL;
13849 }
13850
13851 ret = drm_atomic_helper_setup_commit(state, nonblock);
13852 if (ret)
13853 return ret;
13854
13855 INIT_WORK(&state->commit_work, intel_atomic_commit_work);
13856
13857 ret = intel_atomic_prepare_commit(dev, state, nonblock);
13858 if (ret) {
13859 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
13860 return ret;
13861 }
13862
13863 drm_atomic_helper_swap_state(state, true);
13864 dev_priv->wm.distrust_bios_wm = false;
13865 dev_priv->wm.skl_results = intel_state->wm_results;
13866 intel_shared_dpll_commit(state);
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020013867 intel_atomic_track_fbs(state);
Daniel Vetter94f05022016-06-14 18:01:00 +020013868
13869 if (nonblock)
13870 queue_work(system_unbound_wq, &state->commit_work);
13871 else
13872 intel_atomic_commit_tail(state);
Mika Kuoppala75714942015-12-16 09:26:48 +020013873
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013874 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013875}
13876
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013877void intel_crtc_restore_mode(struct drm_crtc *crtc)
13878{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013879 struct drm_device *dev = crtc->dev;
13880 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013881 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013882 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013883
13884 state = drm_atomic_state_alloc(dev);
13885 if (!state) {
Ville Syrjälä78108b72016-05-27 20:59:19 +030013886 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
13887 crtc->base.id, crtc->name);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013888 return;
13889 }
13890
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013891 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013892
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013893retry:
13894 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13895 ret = PTR_ERR_OR_ZERO(crtc_state);
13896 if (!ret) {
13897 if (!crtc_state->active)
13898 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013899
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013900 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013901 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013902 }
13903
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013904 if (ret == -EDEADLK) {
13905 drm_atomic_state_clear(state);
13906 drm_modeset_backoff(state->acquire_ctx);
13907 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013908 }
13909
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013910 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013911out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013912 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013913}
13914
Daniel Vetter25c5b262012-07-08 22:08:04 +020013915#undef for_each_intel_crtc_masked
13916
Bob Paauwea8784872016-07-15 14:59:02 +010013917/*
13918 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
13919 * drm_atomic_helper_legacy_gamma_set() directly.
13920 */
13921static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
13922 u16 *red, u16 *green, u16 *blue,
13923 uint32_t size)
13924{
13925 struct drm_device *dev = crtc->dev;
13926 struct drm_mode_config *config = &dev->mode_config;
13927 struct drm_crtc_state *state;
13928 int ret;
13929
13930 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
13931 if (ret)
13932 return ret;
13933
13934 /*
13935 * Make sure we update the legacy properties so this works when
13936 * atomic is not enabled.
13937 */
13938
13939 state = crtc->state;
13940
13941 drm_object_property_set_value(&crtc->base,
13942 config->degamma_lut_property,
13943 (state->degamma_lut) ?
13944 state->degamma_lut->base.id : 0);
13945
13946 drm_object_property_set_value(&crtc->base,
13947 config->ctm_property,
13948 (state->ctm) ?
13949 state->ctm->base.id : 0);
13950
13951 drm_object_property_set_value(&crtc->base,
13952 config->gamma_lut_property,
13953 (state->gamma_lut) ?
13954 state->gamma_lut->base.id : 0);
13955
13956 return 0;
13957}
13958
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013959static const struct drm_crtc_funcs intel_crtc_funcs = {
Bob Paauwea8784872016-07-15 14:59:02 +010013960 .gamma_set = intel_atomic_legacy_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013961 .set_config = drm_atomic_helper_set_config,
Lionel Landwerlin82cf4352016-03-16 10:57:16 +000013962 .set_property = drm_atomic_helper_crtc_set_property,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013963 .destroy = intel_crtc_destroy,
Chris Wilson527b6ab2016-06-24 13:44:03 +010013964 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013965 .atomic_duplicate_state = intel_crtc_duplicate_state,
13966 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013967};
13968
Matt Roper6beb8c232014-12-01 15:40:14 -080013969/**
13970 * intel_prepare_plane_fb - Prepare fb for usage on plane
13971 * @plane: drm plane to prepare for
13972 * @fb: framebuffer to prepare for presentation
13973 *
13974 * Prepares a framebuffer for usage on a display plane. Generally this
13975 * involves pinning the underlying object and updating the frontbuffer tracking
13976 * bits. Some older platforms need special physical address handling for
13977 * cursor planes.
13978 *
Maarten Lankhorstf9356752015-08-18 13:40:05 +020013979 * Must be called with struct_mutex held.
13980 *
Matt Roper6beb8c232014-12-01 15:40:14 -080013981 * Returns 0 on success, negative error code on failure.
13982 */
13983int
13984intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013985 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013986{
13987 struct drm_device *dev = plane->dev;
Maarten Lankhorst844f9112015-09-02 10:42:40 +020013988 struct drm_framebuffer *fb = new_state->fb;
Matt Roper6beb8c232014-12-01 15:40:14 -080013989 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013990 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
Chris Wilsonc37efb92016-06-17 08:28:47 +010013991 struct reservation_object *resv;
Matt Roper6beb8c232014-12-01 15:40:14 -080013992 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013993
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020013994 if (!obj && !old_obj)
Matt Roper465c1202014-05-29 08:06:54 -070013995 return 0;
13996
Maarten Lankhorst5008e872015-08-18 13:40:05 +020013997 if (old_obj) {
13998 struct drm_crtc_state *crtc_state =
13999 drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
14000
14001 /* Big Hammer, we also need to ensure that any pending
14002 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14003 * current scanout is retired before unpinning the old
14004 * framebuffer. Note that we rely on userspace rendering
14005 * into the buffer attached to the pipe they are waiting
14006 * on. If not, userspace generates a GPU hang with IPEHR
14007 * point to the MI_WAIT_FOR_EVENT.
14008 *
14009 * This should only fail upon a hung GPU, in which case we
14010 * can safely continue.
14011 */
14012 if (needs_modeset(crtc_state))
14013 ret = i915_gem_object_wait_rendering(old_obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014014 if (ret) {
14015 /* GPU hangs should have been swallowed by the wait */
14016 WARN_ON(ret == -EIO);
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014017 return ret;
Chris Wilsonf4457ae2016-04-13 17:35:08 +010014018 }
Maarten Lankhorst5008e872015-08-18 13:40:05 +020014019 }
14020
Chris Wilsonc37efb92016-06-17 08:28:47 +010014021 if (!obj)
14022 return 0;
14023
Daniel Vetter5a21b662016-05-24 17:13:53 +020014024 /* For framebuffer backed by dmabuf, wait for fence */
Chris Wilsonc37efb92016-06-17 08:28:47 +010014025 resv = i915_gem_object_get_dmabuf_resv(obj);
14026 if (resv) {
Daniel Vetter5a21b662016-05-24 17:13:53 +020014027 long lret;
14028
Chris Wilsonc37efb92016-06-17 08:28:47 +010014029 lret = reservation_object_wait_timeout_rcu(resv, false, true,
Daniel Vetter5a21b662016-05-24 17:13:53 +020014030 MAX_SCHEDULE_TIMEOUT);
14031 if (lret == -ERESTARTSYS)
14032 return lret;
14033
14034 WARN(lret < 0, "waiting returns %li\n", lret);
14035 }
14036
Chris Wilsonc37efb92016-06-17 08:28:47 +010014037 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
Matt Roper6beb8c232014-12-01 15:40:14 -080014038 INTEL_INFO(dev)->cursor_needs_physical) {
14039 int align = IS_I830(dev) ? 16 * 1024 : 256;
14040 ret = i915_gem_object_attach_phys(obj, align);
14041 if (ret)
14042 DRM_DEBUG_KMS("failed to attach phys object\n");
14043 } else {
Ville Syrjälä3465c582016-02-15 22:54:43 +020014044 ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
Matt Roper6beb8c232014-12-01 15:40:14 -080014045 }
14046
Chris Wilsonc37efb92016-06-17 08:28:47 +010014047 if (ret == 0) {
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014048 struct intel_plane_state *plane_state =
14049 to_intel_plane_state(new_state);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014050
Daniel Vetter6c9c1b32016-06-13 16:13:48 +020014051 i915_gem_request_assign(&plane_state->wait_req,
14052 obj->last_write_req);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014053 }
Matt Roper6beb8c232014-12-01 15:40:14 -080014054
Matt Roper6beb8c232014-12-01 15:40:14 -080014055 return ret;
14056}
14057
Matt Roper38f3ce32014-12-02 07:45:25 -080014058/**
14059 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14060 * @plane: drm plane to clean up for
14061 * @fb: old framebuffer that was on plane
14062 *
14063 * Cleans up a framebuffer that has just been removed from a plane.
Maarten Lankhorstf9356752015-08-18 13:40:05 +020014064 *
14065 * Must be called with struct_mutex held.
Matt Roper38f3ce32014-12-02 07:45:25 -080014066 */
14067void
14068intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000014069 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080014070{
14071 struct drm_device *dev = plane->dev;
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014072 struct intel_plane_state *old_intel_state;
Keith Packard84978252016-07-31 00:54:51 -070014073 struct intel_plane_state *intel_state = to_intel_plane_state(plane->state);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014074 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14075 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
Matt Roper38f3ce32014-12-02 07:45:25 -080014076
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014077 old_intel_state = to_intel_plane_state(old_state);
14078
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014079 if (!obj && !old_obj)
Matt Roper38f3ce32014-12-02 07:45:25 -080014080 return;
14081
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014082 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
14083 !INTEL_INFO(dev)->cursor_needs_physical))
Ville Syrjälä3465c582016-02-15 22:54:43 +020014084 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
Maarten Lankhorst1ee49392015-09-23 13:27:08 +020014085
Keith Packard84978252016-07-31 00:54:51 -070014086 i915_gem_request_assign(&intel_state->wait_req, NULL);
Maarten Lankhorst7580d772015-08-18 13:40:06 +020014087 i915_gem_request_assign(&old_intel_state->wait_req, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070014088}
14089
Chandra Konduru6156a452015-04-27 13:48:39 -070014090int
14091skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14092{
14093 int max_scale;
Chandra Konduru6156a452015-04-27 13:48:39 -070014094 int crtc_clock, cdclk;
14095
Maarten Lankhorstbf8a0af2015-11-24 11:29:02 +010014096 if (!intel_crtc || !crtc_state->base.enable)
Chandra Konduru6156a452015-04-27 13:48:39 -070014097 return DRM_PLANE_HELPER_NO_SCALING;
14098
Chandra Konduru6156a452015-04-27 13:48:39 -070014099 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014100 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070014101
Tvrtko Ursulin54bf1ce2015-10-20 17:17:07 +010014102 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
Chandra Konduru6156a452015-04-27 13:48:39 -070014103 return DRM_PLANE_HELPER_NO_SCALING;
14104
14105 /*
14106 * skl max scale is lower of:
14107 * close to 3 but not 3, -1 is for that purpose
14108 * or
14109 * cdclk/crtc_clock
14110 */
14111 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14112
14113 return max_scale;
14114}
14115
Matt Roper465c1202014-05-29 08:06:54 -070014116static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014117intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014118 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014119 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070014120{
Matt Roper2b875c22014-12-01 15:40:13 -080014121 struct drm_crtc *crtc = state->base.crtc;
14122 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070014123 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014124 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14125 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030014126
Ville Syrjälä693bdc22016-01-15 20:46:53 +020014127 if (INTEL_INFO(plane->dev)->gen >= 9) {
14128 /* use scaler when colorkey is not required */
14129 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14130 min_scale = 1;
14131 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14132 }
Sonika Jindald8106362015-04-10 14:37:28 +053014133 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070014134 }
Sonika Jindald8106362015-04-10 14:37:28 +053014135
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014136 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14137 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014138 state->base.rotation,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014139 min_scale, max_scale,
14140 can_position, true,
14141 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070014142}
14143
Daniel Vetter5a21b662016-05-24 17:13:53 +020014144static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14145 struct drm_crtc_state *old_crtc_state)
14146{
14147 struct drm_device *dev = crtc->dev;
14148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14149 struct intel_crtc_state *old_intel_state =
14150 to_intel_crtc_state(old_crtc_state);
14151 bool modeset = needs_modeset(crtc->state);
14152
14153 /* Perform vblank evasion around commit operation */
14154 intel_pipe_update_start(intel_crtc);
14155
14156 if (modeset)
14157 return;
14158
14159 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14160 intel_color_set_csc(crtc->state);
14161 intel_color_load_luts(crtc->state);
14162 }
14163
14164 if (to_intel_crtc_state(crtc->state)->update_pipe)
14165 intel_update_pipe_config(intel_crtc, old_intel_state);
14166 else if (INTEL_INFO(dev)->gen >= 9)
14167 skl_detach_scalers(intel_crtc);
14168}
14169
14170static void intel_finish_crtc_commit(struct drm_crtc *crtc,
14171 struct drm_crtc_state *old_crtc_state)
14172{
14173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14174
14175 intel_pipe_update_end(intel_crtc, NULL);
14176}
14177
Matt Ropercf4c7c12014-12-04 10:27:42 -080014178/**
Matt Roper4a3b8762014-12-23 10:41:51 -080014179 * intel_plane_destroy - destroy a plane
14180 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080014181 *
Matt Roper4a3b8762014-12-23 10:41:51 -080014182 * Common destruction function for all types of planes (primary, cursor,
14183 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080014184 */
Matt Roper4a3b8762014-12-23 10:41:51 -080014185void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070014186{
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014187 if (!plane)
14188 return;
14189
Matt Roper465c1202014-05-29 08:06:54 -070014190 drm_plane_cleanup(plane);
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014191 kfree(to_intel_plane(plane));
Matt Roper465c1202014-05-29 08:06:54 -070014192}
14193
Matt Roper65a3fea2015-01-21 16:35:42 -080014194const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070014195 .update_plane = drm_atomic_helper_update_plane,
14196 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070014197 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080014198 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080014199 .atomic_get_property = intel_plane_atomic_get_property,
14200 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080014201 .atomic_duplicate_state = intel_plane_duplicate_state,
14202 .atomic_destroy_state = intel_plane_destroy_state,
14203
Matt Roper465c1202014-05-29 08:06:54 -070014204};
14205
14206static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
14207 int pipe)
14208{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014209 struct intel_plane *primary = NULL;
14210 struct intel_plane_state *state = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014211 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020014212 unsigned int num_formats;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014213 int ret;
Matt Roper465c1202014-05-29 08:06:54 -070014214
14215 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014216 if (!primary)
14217 goto fail;
Matt Roper465c1202014-05-29 08:06:54 -070014218
Matt Roper8e7d6882015-01-21 16:35:41 -080014219 state = intel_create_plane_state(&primary->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014220 if (!state)
14221 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014222 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014223
Matt Roper465c1202014-05-29 08:06:54 -070014224 primary->can_scale = false;
14225 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014226 if (INTEL_INFO(dev)->gen >= 9) {
14227 primary->can_scale = true;
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070014228 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070014229 }
Matt Roper465c1202014-05-29 08:06:54 -070014230 primary->pipe = pipe;
14231 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014232 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014233 primary->check_plane = intel_check_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014234 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
14235 primary->plane = !pipe;
14236
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014237 if (INTEL_INFO(dev)->gen >= 9) {
14238 intel_primary_formats = skl_primary_formats;
14239 num_formats = ARRAY_SIZE(skl_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014240
14241 primary->update_plane = skylake_update_primary_plane;
14242 primary->disable_plane = skylake_disable_primary_plane;
14243 } else if (HAS_PCH_SPLIT(dev)) {
14244 intel_primary_formats = i965_primary_formats;
14245 num_formats = ARRAY_SIZE(i965_primary_formats);
14246
14247 primary->update_plane = ironlake_update_primary_plane;
14248 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014249 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010014250 intel_primary_formats = i965_primary_formats;
14251 num_formats = ARRAY_SIZE(i965_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014252
14253 primary->update_plane = i9xx_update_primary_plane;
14254 primary->disable_plane = i9xx_disable_primary_plane;
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014255 } else {
14256 intel_primary_formats = i8xx_primary_formats;
14257 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Maarten Lankhorsta8d201a2016-01-07 11:54:11 +010014258
14259 primary->update_plane = i9xx_update_primary_plane;
14260 primary->disable_plane = i9xx_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070014261 }
14262
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014263 if (INTEL_INFO(dev)->gen >= 9)
14264 ret = drm_universal_plane_init(dev, &primary->base, 0,
14265 &intel_plane_funcs,
14266 intel_primary_formats, num_formats,
14267 DRM_PLANE_TYPE_PRIMARY,
14268 "plane 1%c", pipe_name(pipe));
14269 else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14270 ret = drm_universal_plane_init(dev, &primary->base, 0,
14271 &intel_plane_funcs,
14272 intel_primary_formats, num_formats,
14273 DRM_PLANE_TYPE_PRIMARY,
14274 "primary %c", pipe_name(pipe));
14275 else
14276 ret = drm_universal_plane_init(dev, &primary->base, 0,
14277 &intel_plane_funcs,
14278 intel_primary_formats, num_formats,
14279 DRM_PLANE_TYPE_PRIMARY,
14280 "plane %c", plane_name(primary->plane));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014281 if (ret)
14282 goto fail;
Sonika Jindal48404c12014-08-22 14:06:04 +053014283
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014284 if (INTEL_INFO(dev)->gen >= 4)
14285 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053014286
Matt Roperea2c67b2014-12-23 10:41:52 -080014287 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
14288
Matt Roper465c1202014-05-29 08:06:54 -070014289 return &primary->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014290
14291fail:
14292 kfree(state);
14293 kfree(primary);
14294
14295 return NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014296}
14297
Sonika Jindal3b7a5112015-04-10 14:37:29 +053014298void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
14299{
14300 if (!dev->mode_config.rotation_property) {
14301 unsigned long flags = BIT(DRM_ROTATE_0) |
14302 BIT(DRM_ROTATE_180);
14303
14304 if (INTEL_INFO(dev)->gen >= 9)
14305 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
14306
14307 dev->mode_config.rotation_property =
14308 drm_mode_create_rotation_property(dev, flags);
14309 }
14310 if (dev->mode_config.rotation_property)
14311 drm_object_attach_property(&plane->base.base,
14312 dev->mode_config.rotation_property,
14313 plane->base.state->rotation);
14314}
14315
Matt Roper3d7d6512014-06-10 08:28:13 -070014316static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014317intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014318 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014319 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014320{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014321 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014322 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014323 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014324 enum pipe pipe = to_intel_plane(plane)->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014325 unsigned stride;
14326 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014327
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014328 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
14329 &state->dst, &state->clip,
Ville Syrjälä9b8b0132016-06-17 17:13:10 +030014330 state->base.rotation,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014331 DRM_PLANE_HELPER_NO_SCALING,
14332 DRM_PLANE_HELPER_NO_SCALING,
14333 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014334 if (ret)
14335 return ret;
14336
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014337 /* if we want to turn off the cursor ignore width and height */
14338 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014339 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014340
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014341 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020014342 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014343 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14344 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014345 return -EINVAL;
14346 }
14347
Matt Roperea2c67b2014-12-23 10:41:52 -080014348 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14349 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014350 DRM_DEBUG_KMS("buffer is too small\n");
14351 return -ENOMEM;
14352 }
14353
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014354 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014355 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014356 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014357 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014358
Ville Syrjäläb29ec922015-12-18 19:24:39 +020014359 /*
14360 * There's something wrong with the cursor on CHV pipe C.
14361 * If it straddles the left edge of the screen then
14362 * moving it away from the edge or disabling it often
14363 * results in a pipe underrun, and often that can lead to
14364 * dead pipe (constant underrun reported, and it scans
14365 * out just a solid color). To recover from that, the
14366 * display power well must be turned off and on again.
14367 * Refuse the put the cursor into that compromised position.
14368 */
14369 if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
14370 state->visible && state->base.crtc_x < 0) {
14371 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
14372 return -EINVAL;
14373 }
14374
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014375 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014376}
14377
Matt Roperf4a2cf22014-12-01 15:40:12 -080014378static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014379intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014380 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014381{
Maarten Lankhorstf2858022016-01-07 11:54:09 +010014382 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14383
14384 intel_crtc->cursor_addr = 0;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014385 intel_crtc_update_cursor(crtc, NULL);
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014386}
14387
14388static void
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014389intel_update_cursor_plane(struct drm_plane *plane,
14390 const struct intel_crtc_state *crtc_state,
14391 const struct intel_plane_state *state)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014392{
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014393 struct drm_crtc *crtc = crtc_state->base.crtc;
14394 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080014395 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014396 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014397 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014398
Matt Roperf4a2cf22014-12-01 15:40:12 -080014399 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014400 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014401 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014402 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014403 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014404 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014405
Gustavo Padovana912f122014-12-01 15:40:10 -080014406 intel_crtc->cursor_addr = addr;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014407 intel_crtc_update_cursor(crtc, state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014408}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014409
Matt Roper3d7d6512014-06-10 08:28:13 -070014410static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14411 int pipe)
14412{
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014413 struct intel_plane *cursor = NULL;
14414 struct intel_plane_state *state = NULL;
14415 int ret;
Matt Roper3d7d6512014-06-10 08:28:13 -070014416
14417 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014418 if (!cursor)
14419 goto fail;
Matt Roper3d7d6512014-06-10 08:28:13 -070014420
Matt Roper8e7d6882015-01-21 16:35:41 -080014421 state = intel_create_plane_state(&cursor->base);
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014422 if (!state)
14423 goto fail;
Matt Roper8e7d6882015-01-21 16:35:41 -080014424 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014425
Matt Roper3d7d6512014-06-10 08:28:13 -070014426 cursor->can_scale = false;
14427 cursor->max_downscale = 1;
14428 cursor->pipe = pipe;
14429 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014430 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014431 cursor->check_plane = intel_check_cursor_plane;
Maarten Lankhorst55a08b3f2016-01-07 11:54:10 +010014432 cursor->update_plane = intel_update_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014433 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014434
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014435 ret = drm_universal_plane_init(dev, &cursor->base, 0,
14436 &intel_plane_funcs,
14437 intel_cursor_formats,
14438 ARRAY_SIZE(intel_cursor_formats),
Ville Syrjälä38573dc2016-05-27 20:59:23 +030014439 DRM_PLANE_TYPE_CURSOR,
14440 "cursor %c", pipe_name(pipe));
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014441 if (ret)
14442 goto fail;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014443
14444 if (INTEL_INFO(dev)->gen >= 4) {
14445 if (!dev->mode_config.rotation_property)
14446 dev->mode_config.rotation_property =
14447 drm_mode_create_rotation_property(dev,
14448 BIT(DRM_ROTATE_0) |
14449 BIT(DRM_ROTATE_180));
14450 if (dev->mode_config.rotation_property)
14451 drm_object_attach_property(&cursor->base.base,
14452 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014453 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014454 }
14455
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070014456 if (INTEL_INFO(dev)->gen >=9)
14457 state->scaler_id = -1;
14458
Matt Roperea2c67b2014-12-23 10:41:52 -080014459 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14460
Matt Roper3d7d6512014-06-10 08:28:13 -070014461 return &cursor->base;
Ville Syrjäläfca0ce22016-03-21 14:43:22 +000014462
14463fail:
14464 kfree(state);
14465 kfree(cursor);
14466
14467 return NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014468}
14469
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014470static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14471 struct intel_crtc_state *crtc_state)
14472{
14473 int i;
14474 struct intel_scaler *intel_scaler;
14475 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14476
14477 for (i = 0; i < intel_crtc->num_scalers; i++) {
14478 intel_scaler = &scaler_state->scalers[i];
14479 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014480 intel_scaler->mode = PS_SCALER_MODE_DYN;
14481 }
14482
14483 scaler_state->scaler_id = -1;
14484}
14485
Hannes Ederb358d0a2008-12-18 21:18:47 +010014486static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014487{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014488 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014489 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014490 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014491 struct drm_plane *primary = NULL;
14492 struct drm_plane *cursor = NULL;
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014493 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014494
Daniel Vetter955382f2013-09-19 14:05:45 +020014495 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014496 if (intel_crtc == NULL)
14497 return;
14498
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014499 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14500 if (!crtc_state)
14501 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014502 intel_crtc->config = crtc_state;
14503 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014504 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014505
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014506 /* initialize shared scalers */
14507 if (INTEL_INFO(dev)->gen >= 9) {
14508 if (pipe == PIPE_C)
14509 intel_crtc->num_scalers = 1;
14510 else
14511 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14512
14513 skl_init_scalers(dev, intel_crtc, crtc_state);
14514 }
14515
Matt Roper465c1202014-05-29 08:06:54 -070014516 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014517 if (!primary)
14518 goto fail;
14519
14520 cursor = intel_cursor_plane_create(dev, pipe);
14521 if (!cursor)
14522 goto fail;
14523
Matt Roper465c1202014-05-29 08:06:54 -070014524 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Ville Syrjälä4d5d72b72016-05-27 20:59:21 +030014525 cursor, &intel_crtc_funcs,
14526 "pipe %c", pipe_name(pipe));
Matt Roper3d7d6512014-06-10 08:28:13 -070014527 if (ret)
14528 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014529
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014530 /*
14531 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014532 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014533 */
Jesse Barnes80824002009-09-10 15:28:06 -070014534 intel_crtc->pipe = pipe;
14535 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014536 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014537 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014538 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014539 }
14540
Chris Wilson4b0e3332014-05-30 16:35:26 +030014541 intel_crtc->cursor_base = ~0;
14542 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014543 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014544
Ville Syrjälä852eb002015-06-24 22:00:07 +030014545 intel_crtc->wm.cxsr_allowed = true;
14546
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014547 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14548 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14549 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14550 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14551
Jesse Barnes79e53942008-11-07 14:24:08 -080014552 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014553
Lionel Landwerlin8563b1e2016-03-16 10:57:14 +000014554 intel_color_init(&intel_crtc->base);
14555
Daniel Vetter87b6b102014-05-15 15:33:46 +020014556 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014557 return;
14558
14559fail:
Ville Syrjälä69ae5612016-05-27 20:59:22 +030014560 intel_plane_destroy(primary);
14561 intel_plane_destroy(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014562 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014563 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014564}
14565
Jesse Barnes752aa882013-10-31 18:55:49 +020014566enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14567{
14568 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014569 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014570
Rob Clark51fd3712013-11-19 12:10:12 -050014571 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014572
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014573 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014574 return INVALID_PIPE;
14575
14576 return to_intel_crtc(encoder->crtc)->pipe;
14577}
14578
Carl Worth08d7b3d2009-04-29 14:43:54 -070014579int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014580 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014581{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014582 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014583 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014584 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014585
Rob Clark7707e652014-07-17 23:30:04 -040014586 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Chris Wilson71240ed2016-06-24 14:00:24 +010014587 if (!drmmode_crtc)
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014588 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014589
Rob Clark7707e652014-07-17 23:30:04 -040014590 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014591 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014592
Daniel Vetterc05422d2009-08-11 16:05:30 +020014593 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014594}
14595
Daniel Vetter66a92782012-07-12 20:08:18 +020014596static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014597{
Daniel Vetter66a92782012-07-12 20:08:18 +020014598 struct drm_device *dev = encoder->base.dev;
14599 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014600 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014601 int entry = 0;
14602
Damien Lespiaub2784e12014-08-05 11:29:37 +010014603 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014604 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014605 index_mask |= (1 << entry);
14606
Jesse Barnes79e53942008-11-07 14:24:08 -080014607 entry++;
14608 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014609
Jesse Barnes79e53942008-11-07 14:24:08 -080014610 return index_mask;
14611}
14612
Chris Wilson4d302442010-12-14 19:21:29 +000014613static bool has_edp_a(struct drm_device *dev)
14614{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014615 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson4d302442010-12-14 19:21:29 +000014616
14617 if (!IS_MOBILE(dev))
14618 return false;
14619
14620 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14621 return false;
14622
Damien Lespiaue3589902014-02-07 19:12:50 +000014623 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014624 return false;
14625
14626 return true;
14627}
14628
Jesse Barnes84b4e042014-06-25 08:24:29 -070014629static bool intel_crt_present(struct drm_device *dev)
14630{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014631 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes84b4e042014-06-25 08:24:29 -070014632
Damien Lespiau884497e2013-12-03 13:56:23 +000014633 if (INTEL_INFO(dev)->gen >= 9)
14634 return false;
14635
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014636 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014637 return false;
14638
14639 if (IS_CHERRYVIEW(dev))
14640 return false;
14641
Ville Syrjälä65e472e2015-12-01 23:28:55 +020014642 if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
14643 return false;
14644
Ville Syrjälä70ac54d2015-12-01 23:29:56 +020014645 /* DDI E can't be used if DDI A requires 4 lanes */
14646 if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
14647 return false;
14648
Ville Syrjäläe4abb732015-12-01 23:31:33 +020014649 if (!dev_priv->vbt.int_crt_support)
Jesse Barnes84b4e042014-06-25 08:24:29 -070014650 return false;
14651
14652 return true;
14653}
14654
Jesse Barnes79e53942008-11-07 14:24:08 -080014655static void intel_setup_outputs(struct drm_device *dev)
14656{
Chris Wilsonfac5e232016-07-04 11:34:36 +010014657 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson4ef69c72010-09-09 15:14:28 +010014658 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014659 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014660
Imre Deak97a824e12016-06-21 11:51:47 +030014661 /*
14662 * intel_edp_init_connector() depends on this completing first, to
14663 * prevent the registeration of both eDP and LVDS and the incorrect
14664 * sharing of the PPS.
14665 */
Daniel Vetterc9093352013-06-06 22:22:47 +020014666 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014667
Jesse Barnes84b4e042014-06-25 08:24:29 -070014668 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014669 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014670
Vandana Kannanc776eb22014-08-19 12:05:01 +053014671 if (IS_BROXTON(dev)) {
14672 /*
14673 * FIXME: Broxton doesn't support port detection via the
14674 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14675 * detect the ports.
14676 */
14677 intel_ddi_init(dev, PORT_A);
14678 intel_ddi_init(dev, PORT_B);
14679 intel_ddi_init(dev, PORT_C);
Shashank Sharmac6c794a2016-03-22 12:01:50 +020014680
14681 intel_dsi_init(dev);
Vandana Kannanc776eb22014-08-19 12:05:01 +053014682 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014683 int found;
14684
Jesse Barnesde31fac2015-03-06 15:53:32 -080014685 /*
14686 * Haswell uses DDI functions to detect digital outputs.
14687 * On SKL pre-D0 the strap isn't connected, so we assume
14688 * it's there.
14689 */
Ville Syrjälä77179402015-09-18 20:03:35 +030014690 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014691 /* WaIgnoreDDIAStrap: skl */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014692 if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014693 intel_ddi_init(dev, PORT_A);
14694
14695 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14696 * register */
14697 found = I915_READ(SFUSE_STRAP);
14698
14699 if (found & SFUSE_STRAP_DDIB_DETECTED)
14700 intel_ddi_init(dev, PORT_B);
14701 if (found & SFUSE_STRAP_DDIC_DETECTED)
14702 intel_ddi_init(dev, PORT_C);
14703 if (found & SFUSE_STRAP_DDID_DETECTED)
14704 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014705 /*
14706 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14707 */
Rodrigo Vivief11bdb2015-10-28 04:16:45 -070014708 if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070014709 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14710 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14711 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14712 intel_ddi_init(dev, PORT_E);
14713
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014714 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014715 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014716 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014717
14718 if (has_edp_a(dev))
14719 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014720
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014721 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014722 /* PCH SDVOB multiplex with HDMIB */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014723 found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014724 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014725 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014726 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014727 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014728 }
14729
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014730 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014731 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014732
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014733 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014734 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014735
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014736 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014737 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014738
Daniel Vetter270b3042012-10-27 15:52:05 +020014739 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014740 intel_dp_init(dev, PCH_DP_D, PORT_D);
Wayne Boyer666a4532015-12-09 12:29:35 -080014741 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014742 bool has_edp, has_port;
Chris Wilson457c52d2016-06-01 08:27:50 +010014743
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014744 /*
14745 * The DP_DETECTED bit is the latched state of the DDC
14746 * SDA pin at boot. However since eDP doesn't require DDC
14747 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14748 * eDP ports may have been muxed to an alternate function.
14749 * Thus we can't rely on the DP_DETECTED bit alone to detect
14750 * eDP ports. Consult the VBT as well as DP_DETECTED to
14751 * detect eDP ports.
Ville Syrjälä22f350422016-06-03 12:17:43 +030014752 *
14753 * Sadly the straps seem to be missing sometimes even for HDMI
14754 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
14755 * and VBT for the presence of the port. Additionally we can't
14756 * trust the port type the VBT declares as we've seen at least
14757 * HDMI ports that the VBT claim are DP or eDP.
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014758 */
Chris Wilson457c52d2016-06-01 08:27:50 +010014759 has_edp = intel_dp_is_edp(dev, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014760 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
14761 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010014762 has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014763 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014764 intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014765
Chris Wilson457c52d2016-06-01 08:27:50 +010014766 has_edp = intel_dp_is_edp(dev, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014767 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
14768 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
Chris Wilson457c52d2016-06-01 08:27:50 +010014769 has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014770 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014771 intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014772
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014773 if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä22f350422016-06-03 12:17:43 +030014774 /*
14775 * eDP not supported on port D,
14776 * so no need to worry about it
14777 */
14778 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
14779 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
Ville Syrjäläe66eb812015-09-18 20:03:34 +030014780 intel_dp_init(dev, CHV_DP_D, PORT_D);
Ville Syrjälä22f350422016-06-03 12:17:43 +030014781 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
14782 intel_hdmi_init(dev, CHV_HDMID, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014783 }
14784
Jani Nikula3cfca972013-08-27 15:12:26 +030014785 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014786 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014787 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014788
Paulo Zanonie2debe92013-02-18 19:00:27 -030014789 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014790 DRM_DEBUG_KMS("probing SDVOB\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014791 found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014792 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014793 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014794 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014795 }
Ma Ling27185ae2009-08-24 13:50:23 +080014796
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014797 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014798 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014799 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014800
14801 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014802
Paulo Zanonie2debe92013-02-18 19:00:27 -030014803 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014804 DRM_DEBUG_KMS("probing SDVOC\n");
Ville Syrjälä2a5c0832015-11-06 21:29:59 +020014805 found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014806 }
Ma Ling27185ae2009-08-24 13:50:23 +080014807
Paulo Zanonie2debe92013-02-18 19:00:27 -030014808 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014809
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014810 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014811 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014812 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014813 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014814 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014815 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014816 }
Ma Ling27185ae2009-08-24 13:50:23 +080014817
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014818 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014819 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014820 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014821 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014822 intel_dvo_init(dev);
14823
Zhenyu Wang103a1962009-11-27 11:44:36 +080014824 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014825 intel_tv_init(dev);
14826
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014827 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014828
Damien Lespiaub2784e12014-08-05 11:29:37 +010014829 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014830 encoder->base.possible_crtcs = encoder->crtc_mask;
14831 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014832 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014833 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014834
Paulo Zanonidde86e22012-12-01 12:04:25 -020014835 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014836
14837 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014838}
14839
14840static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14841{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014842 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014843 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014844
Daniel Vetteref2d6332014-02-10 18:00:38 +010014845 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014846 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014847 WARN_ON(!intel_fb->obj->framebuffer_references--);
Chris Wilsonf8c417c2016-07-20 13:31:53 +010014848 i915_gem_object_put(intel_fb->obj);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014849 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014850 kfree(intel_fb);
14851}
14852
14853static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014854 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014855 unsigned int *handle)
14856{
14857 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014858 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014859
Chris Wilsoncc917ab2015-10-13 14:22:26 +010014860 if (obj->userptr.mm) {
14861 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14862 return -EINVAL;
14863 }
14864
Chris Wilson05394f32010-11-08 19:18:58 +000014865 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014866}
14867
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014868static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14869 struct drm_file *file,
14870 unsigned flags, unsigned color,
14871 struct drm_clip_rect *clips,
14872 unsigned num_clips)
14873{
14874 struct drm_device *dev = fb->dev;
14875 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14876 struct drm_i915_gem_object *obj = intel_fb->obj;
14877
14878 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014879 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014880 mutex_unlock(&dev->struct_mutex);
14881
14882 return 0;
14883}
14884
Jesse Barnes79e53942008-11-07 14:24:08 -080014885static const struct drm_framebuffer_funcs intel_fb_funcs = {
14886 .destroy = intel_user_framebuffer_destroy,
14887 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014888 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014889};
14890
Damien Lespiaub3218032015-02-27 11:15:18 +000014891static
14892u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14893 uint32_t pixel_format)
14894{
14895 u32 gen = INTEL_INFO(dev)->gen;
14896
14897 if (gen >= 9) {
Ville Syrjäläac484962016-01-20 21:05:26 +020014898 int cpp = drm_format_plane_cpp(pixel_format, 0);
14899
Damien Lespiaub3218032015-02-27 11:15:18 +000014900 /* "The stride in bytes must not exceed the of the size of 8K
14901 * pixels and 32K bytes."
14902 */
Ville Syrjäläac484962016-01-20 21:05:26 +020014903 return min(8192 * cpp, 32768);
Wayne Boyer666a4532015-12-09 12:29:35 -080014904 } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014905 return 32*1024;
14906 } else if (gen >= 4) {
14907 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14908 return 16*1024;
14909 else
14910 return 32*1024;
14911 } else if (gen >= 3) {
14912 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14913 return 8*1024;
14914 else
14915 return 16*1024;
14916 } else {
14917 /* XXX DSPC is limited to 4k tiled */
14918 return 8*1024;
14919 }
14920}
14921
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014922static int intel_framebuffer_init(struct drm_device *dev,
14923 struct intel_framebuffer *intel_fb,
14924 struct drm_mode_fb_cmd2 *mode_cmd,
14925 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014926{
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014927 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014928 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014929 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014930 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014931
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014932 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14933
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014934 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14935 /* Enforce that fb modifier and tiling mode match, but only for
14936 * X-tiled. This is needed for FBC. */
14937 if (!!(obj->tiling_mode == I915_TILING_X) !=
14938 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14939 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14940 return -EINVAL;
14941 }
14942 } else {
14943 if (obj->tiling_mode == I915_TILING_X)
14944 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14945 else if (obj->tiling_mode == I915_TILING_Y) {
14946 DRM_DEBUG("No Y tiling for legacy addfb\n");
14947 return -EINVAL;
14948 }
14949 }
14950
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014951 /* Passed in modifier sanity checking. */
14952 switch (mode_cmd->modifier[0]) {
14953 case I915_FORMAT_MOD_Y_TILED:
14954 case I915_FORMAT_MOD_Yf_TILED:
14955 if (INTEL_INFO(dev)->gen < 9) {
14956 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14957 mode_cmd->modifier[0]);
14958 return -EINVAL;
14959 }
14960 case DRM_FORMAT_MOD_NONE:
14961 case I915_FORMAT_MOD_X_TILED:
14962 break;
14963 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014964 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14965 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014966 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014967 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014968
Ville Syrjälä7b49f942016-01-12 21:08:32 +020014969 stride_alignment = intel_fb_stride_alignment(dev_priv,
14970 mode_cmd->modifier[0],
Damien Lespiaub3218032015-02-27 11:15:18 +000014971 mode_cmd->pixel_format);
14972 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14973 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14974 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014975 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014976 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014977
Damien Lespiaub3218032015-02-27 11:15:18 +000014978 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14979 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014980 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014981 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14982 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014983 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014984 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014985 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014986 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014987
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014988 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014989 mode_cmd->pitches[0] != obj->stride) {
14990 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14991 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014992 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014993 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014994
Ville Syrjälä57779d02012-10-31 17:50:14 +020014995 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014996 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014997 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014998 case DRM_FORMAT_RGB565:
14999 case DRM_FORMAT_XRGB8888:
15000 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015001 break;
15002 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015003 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015004 DRM_DEBUG("unsupported pixel format: %s\n",
15005 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015006 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015007 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020015008 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020015009 case DRM_FORMAT_ABGR8888:
Wayne Boyer666a4532015-12-09 12:29:35 -080015010 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
15011 INTEL_INFO(dev)->gen < 9) {
Damien Lespiau6c0fd452015-05-19 12:29:16 +010015012 DRM_DEBUG("unsupported pixel format: %s\n",
15013 drm_get_format_name(mode_cmd->pixel_format));
15014 return -EINVAL;
15015 }
15016 break;
15017 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020015018 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020015019 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015020 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015021 DRM_DEBUG("unsupported pixel format: %s\n",
15022 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015023 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015024 }
Jesse Barnesb5626742011-06-24 12:19:27 -070015025 break;
Damien Lespiau75312082015-05-15 19:06:01 +010015026 case DRM_FORMAT_ABGR2101010:
Wayne Boyer666a4532015-12-09 12:29:35 -080015027 if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
Damien Lespiau75312082015-05-15 19:06:01 +010015028 DRM_DEBUG("unsupported pixel format: %s\n",
15029 drm_get_format_name(mode_cmd->pixel_format));
15030 return -EINVAL;
15031 }
15032 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020015033 case DRM_FORMAT_YUYV:
15034 case DRM_FORMAT_UYVY:
15035 case DRM_FORMAT_YVYU:
15036 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015037 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015038 DRM_DEBUG("unsupported pixel format: %s\n",
15039 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020015040 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000015041 }
Chris Wilson57cd6502010-08-08 12:34:44 +010015042 break;
15043 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000015044 DRM_DEBUG("unsupported pixel format: %s\n",
15045 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010015046 return -EINVAL;
15047 }
15048
Ville Syrjälä90f9a332012-10-31 17:50:19 +020015049 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15050 if (mode_cmd->offsets[0] != 0)
15051 return -EINVAL;
15052
Damien Lespiauec2c9812015-01-20 12:51:45 +000015053 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000015054 mode_cmd->pixel_format,
15055 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020015056 /* FIXME drm helper for size checks (especially planar formats)? */
15057 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
15058 return -EINVAL;
15059
Daniel Vetterc7d73f62012-12-13 23:38:38 +010015060 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15061 intel_fb->obj = obj;
15062
Ville Syrjälä2d7a2152016-02-15 22:54:47 +020015063 intel_fill_fb_info(dev_priv, &intel_fb->base);
15064
Jesse Barnes79e53942008-11-07 14:24:08 -080015065 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15066 if (ret) {
15067 DRM_ERROR("framebuffer init failed %d\n", ret);
15068 return ret;
15069 }
15070
Ville Syrjälä0b05e1e2016-01-14 15:22:09 +020015071 intel_fb->obj->framebuffer_references++;
15072
Jesse Barnes79e53942008-11-07 14:24:08 -080015073 return 0;
15074}
15075
Jesse Barnes79e53942008-11-07 14:24:08 -080015076static struct drm_framebuffer *
15077intel_user_framebuffer_create(struct drm_device *dev,
15078 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020015079 const struct drm_mode_fb_cmd2 *user_mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080015080{
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015081 struct drm_framebuffer *fb;
Chris Wilson05394f32010-11-08 19:18:58 +000015082 struct drm_i915_gem_object *obj;
Ville Syrjälä76dc3762015-11-11 19:11:28 +020015083 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
Jesse Barnes79e53942008-11-07 14:24:08 -080015084
Chris Wilson03ac0642016-07-20 13:31:51 +010015085 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15086 if (!obj)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010015087 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080015088
Daniel Vetter92907cb2015-11-23 09:04:05 +010015089 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015090 if (IS_ERR(fb))
Chris Wilson34911fd2016-07-20 13:31:54 +010015091 i915_gem_object_put_unlocked(obj);
Lukas Wunnerdcb13942015-07-04 11:50:58 +020015092
15093 return fb;
Jesse Barnes79e53942008-11-07 14:24:08 -080015094}
15095
Daniel Vetter06957262015-08-10 13:34:08 +020015096#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020015097static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020015098{
15099}
15100#endif
15101
Jesse Barnes79e53942008-11-07 14:24:08 -080015102static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080015103 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020015104 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080015105 .atomic_check = intel_atomic_check,
15106 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020015107 .atomic_state_alloc = intel_atomic_state_alloc,
15108 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080015109};
15110
Imre Deak88212942016-03-16 13:38:53 +020015111/**
15112 * intel_init_display_hooks - initialize the display modesetting hooks
15113 * @dev_priv: device private
15114 */
15115void intel_init_display_hooks(struct drm_i915_private *dev_priv)
Jesse Barnese70236a2009-09-21 10:42:27 -070015116{
Imre Deak88212942016-03-16 13:38:53 +020015117 if (INTEL_INFO(dev_priv)->gen >= 9) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015118 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015119 dev_priv->display.get_initial_plane_config =
15120 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000015121 dev_priv->display.crtc_compute_clock =
15122 haswell_crtc_compute_clock;
15123 dev_priv->display.crtc_enable = haswell_crtc_enable;
15124 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015125 } else if (HAS_DDI(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015126 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015127 dev_priv->display.get_initial_plane_config =
15128 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020015129 dev_priv->display.crtc_compute_clock =
15130 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020015131 dev_priv->display.crtc_enable = haswell_crtc_enable;
15132 dev_priv->display.crtc_disable = haswell_crtc_disable;
Imre Deak88212942016-03-16 13:38:53 +020015133 } else if (HAS_PCH_SPLIT(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015134 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015135 dev_priv->display.get_initial_plane_config =
15136 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020015137 dev_priv->display.crtc_compute_clock =
15138 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015139 dev_priv->display.crtc_enable = ironlake_crtc_enable;
15140 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015141 } else if (IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -070015142 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015143 dev_priv->display.get_initial_plane_config =
15144 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveira65b3d6a2016-03-21 18:00:13 +020015145 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15146 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15147 dev_priv->display.crtc_disable = i9xx_crtc_disable;
15148 } else if (IS_VALLEYVIEW(dev_priv)) {
15149 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15150 dev_priv->display.get_initial_plane_config =
15151 i9xx_get_initial_plane_config;
15152 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070015153 dev_priv->display.crtc_enable = valleyview_crtc_enable;
15154 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira19ec6692016-03-21 18:00:15 +020015155 } else if (IS_G4X(dev_priv)) {
15156 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15157 dev_priv->display.get_initial_plane_config =
15158 i9xx_get_initial_plane_config;
15159 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15160 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15161 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira70e8aa22016-03-21 18:00:16 +020015162 } else if (IS_PINEVIEW(dev_priv)) {
15163 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15164 dev_priv->display.get_initial_plane_config =
15165 i9xx_get_initial_plane_config;
15166 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15167 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15168 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015169 } else if (!IS_GEN2(dev_priv)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015170 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015171 dev_priv->display.get_initial_plane_config =
15172 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020015173 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020015174 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15175 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Ander Conselvan de Oliveira81c97f52016-03-22 15:35:23 +020015176 } else {
15177 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15178 dev_priv->display.get_initial_plane_config =
15179 i9xx_get_initial_plane_config;
15180 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15181 dev_priv->display.crtc_enable = i9xx_crtc_enable;
15182 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Eric Anholtf564048e2011-03-30 13:01:02 -070015183 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015184
Jesse Barnese70236a2009-09-21 10:42:27 -070015185 /* Returns the core display clock speed */
Imre Deak88212942016-03-16 13:38:53 +020015186 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015187 dev_priv->display.get_display_clock_speed =
15188 skylake_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015189 else if (IS_BROXTON(dev_priv))
Bob Paauweacd3f3d2015-06-23 14:14:26 -070015190 dev_priv->display.get_display_clock_speed =
15191 broxton_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015192 else if (IS_BROADWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015193 dev_priv->display.get_display_clock_speed =
15194 broadwell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015195 else if (IS_HASWELL(dev_priv))
Ville Syrjälä1652d192015-03-31 14:12:01 +030015196 dev_priv->display.get_display_clock_speed =
15197 haswell_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015198 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070015199 dev_priv->display.get_display_clock_speed =
15200 valleyview_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015201 else if (IS_GEN5(dev_priv))
Ville Syrjäläb37a6432015-03-31 14:11:54 +030015202 dev_priv->display.get_display_clock_speed =
15203 ilk_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015204 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
15205 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015206 dev_priv->display.get_display_clock_speed =
15207 i945_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015208 else if (IS_GM45(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015209 dev_priv->display.get_display_clock_speed =
15210 gm45_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015211 else if (IS_CRESTLINE(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015212 dev_priv->display.get_display_clock_speed =
15213 i965gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015214 else if (IS_PINEVIEW(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015215 dev_priv->display.get_display_clock_speed =
15216 pnv_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015217 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
Ville Syrjälä34edce22015-05-22 11:22:33 +030015218 dev_priv->display.get_display_clock_speed =
15219 g33_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015220 else if (IS_I915G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015221 dev_priv->display.get_display_clock_speed =
15222 i915_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015223 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015224 dev_priv->display.get_display_clock_speed =
15225 i9xx_misc_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015226 else if (IS_I915GM(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015227 dev_priv->display.get_display_clock_speed =
15228 i915gm_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015229 else if (IS_I865G(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015230 dev_priv->display.get_display_clock_speed =
15231 i865_get_display_clock_speed;
Imre Deak88212942016-03-16 13:38:53 +020015232 else if (IS_I85X(dev_priv))
Jesse Barnese70236a2009-09-21 10:42:27 -070015233 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030015234 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015235 else { /* 830 */
Imre Deak88212942016-03-16 13:38:53 +020015236 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070015237 dev_priv->display.get_display_clock_speed =
15238 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030015239 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015240
Imre Deak88212942016-03-16 13:38:53 +020015241 if (IS_GEN5(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015242 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015243 } else if (IS_GEN6(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015244 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015245 } else if (IS_IVYBRIDGE(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015246 /* FIXME: detect B0+ stepping and use auto training */
15247 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Imre Deak88212942016-03-16 13:38:53 +020015248 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053015249 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjälä445e7802016-05-11 22:44:42 +030015250 }
15251
15252 if (IS_BROADWELL(dev_priv)) {
15253 dev_priv->display.modeset_commit_cdclk =
15254 broadwell_modeset_commit_cdclk;
15255 dev_priv->display.modeset_calc_cdclk =
15256 broadwell_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015257 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015258 dev_priv->display.modeset_commit_cdclk =
15259 valleyview_modeset_commit_cdclk;
15260 dev_priv->display.modeset_calc_cdclk =
15261 valleyview_modeset_calc_cdclk;
Imre Deak88212942016-03-16 13:38:53 +020015262 } else if (IS_BROXTON(dev_priv)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015263 dev_priv->display.modeset_commit_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015264 bxt_modeset_commit_cdclk;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020015265 dev_priv->display.modeset_calc_cdclk =
Imre Deak324513c2016-06-13 16:44:36 +030015266 bxt_modeset_calc_cdclk;
Clint Taylorc89e39f2016-05-13 23:41:21 +030015267 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
15268 dev_priv->display.modeset_commit_cdclk =
15269 skl_modeset_commit_cdclk;
15270 dev_priv->display.modeset_calc_cdclk =
15271 skl_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070015272 }
Daniel Vetter5a21b662016-05-24 17:13:53 +020015273
15274 switch (INTEL_INFO(dev_priv)->gen) {
15275 case 2:
15276 dev_priv->display.queue_flip = intel_gen2_queue_flip;
15277 break;
15278
15279 case 3:
15280 dev_priv->display.queue_flip = intel_gen3_queue_flip;
15281 break;
15282
15283 case 4:
15284 case 5:
15285 dev_priv->display.queue_flip = intel_gen4_queue_flip;
15286 break;
15287
15288 case 6:
15289 dev_priv->display.queue_flip = intel_gen6_queue_flip;
15290 break;
15291 case 7:
15292 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
15293 dev_priv->display.queue_flip = intel_gen7_queue_flip;
15294 break;
15295 case 9:
15296 /* Drop through - unsupported since execlist only. */
15297 default:
15298 /* Default just returns -ENODEV to indicate unsupported */
15299 dev_priv->display.queue_flip = intel_default_queue_flip;
15300 }
Jesse Barnese70236a2009-09-21 10:42:27 -070015301}
15302
Jesse Barnesb690e962010-07-19 13:53:12 -070015303/*
15304 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
15305 * resume, or other times. This quirk makes sure that's the case for
15306 * affected systems.
15307 */
Akshay Joshi0206e352011-08-16 15:34:10 -040015308static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070015309{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015310 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnesb690e962010-07-19 13:53:12 -070015311
15312 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015313 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015314}
15315
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015316static void quirk_pipeb_force(struct drm_device *dev)
15317{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015318 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015319
15320 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
15321 DRM_INFO("applying pipe b force quirk\n");
15322}
15323
Keith Packard435793d2011-07-12 14:56:22 -070015324/*
15325 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
15326 */
15327static void quirk_ssc_force_disable(struct drm_device *dev)
15328{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015329 struct drm_i915_private *dev_priv = to_i915(dev);
Keith Packard435793d2011-07-12 14:56:22 -070015330 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015331 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070015332}
15333
Carsten Emde4dca20e2012-03-15 15:56:26 +010015334/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010015335 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
15336 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010015337 */
15338static void quirk_invert_brightness(struct drm_device *dev)
15339{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015340 struct drm_i915_private *dev_priv = to_i915(dev);
Carsten Emde4dca20e2012-03-15 15:56:26 +010015341 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020015342 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070015343}
15344
Scot Doyle9c72cc62014-07-03 23:27:50 +000015345/* Some VBT's incorrectly indicate no backlight is present */
15346static void quirk_backlight_present(struct drm_device *dev)
15347{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015348 struct drm_i915_private *dev_priv = to_i915(dev);
Scot Doyle9c72cc62014-07-03 23:27:50 +000015349 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
15350 DRM_INFO("applying backlight present quirk\n");
15351}
15352
Jesse Barnesb690e962010-07-19 13:53:12 -070015353struct intel_quirk {
15354 int device;
15355 int subsystem_vendor;
15356 int subsystem_device;
15357 void (*hook)(struct drm_device *dev);
15358};
15359
Egbert Eich5f85f172012-10-14 15:46:38 +020015360/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
15361struct intel_dmi_quirk {
15362 void (*hook)(struct drm_device *dev);
15363 const struct dmi_system_id (*dmi_id_list)[];
15364};
15365
15366static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
15367{
15368 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
15369 return 1;
15370}
15371
15372static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15373 {
15374 .dmi_id_list = &(const struct dmi_system_id[]) {
15375 {
15376 .callback = intel_dmi_reverse_brightness,
15377 .ident = "NCR Corporation",
15378 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15379 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15380 },
15381 },
15382 { } /* terminating entry */
15383 },
15384 .hook = quirk_invert_brightness,
15385 },
15386};
15387
Ben Widawskyc43b5632012-04-16 14:07:40 -070015388static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015389 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15390 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15391
Jesse Barnesb690e962010-07-19 13:53:12 -070015392 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15393 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15394
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015395 /* 830 needs to leave pipe A & dpll A up */
15396 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15397
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015398 /* 830 needs to leave pipe B & dpll B up */
15399 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15400
Keith Packard435793d2011-07-12 14:56:22 -070015401 /* Lenovo U160 cannot use SSC on LVDS */
15402 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015403
15404 /* Sony Vaio Y cannot use SSC on LVDS */
15405 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015406
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015407 /* Acer Aspire 5734Z must invert backlight brightness */
15408 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15409
15410 /* Acer/eMachines G725 */
15411 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15412
15413 /* Acer/eMachines e725 */
15414 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15415
15416 /* Acer/Packard Bell NCL20 */
15417 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15418
15419 /* Acer Aspire 4736Z */
15420 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015421
15422 /* Acer Aspire 5336 */
15423 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015424
15425 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15426 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015427
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015428 /* Acer C720 Chromebook (Core i3 4005U) */
15429 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15430
jens steinb2a96012014-10-28 20:25:53 +010015431 /* Apple Macbook 2,1 (Core 2 T7400) */
15432 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15433
Jani Nikula1b9448b2015-11-05 11:49:59 +020015434 /* Apple Macbook 4,1 */
15435 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
15436
Scot Doyled4967d82014-07-03 23:27:52 +000015437 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15438 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015439
15440 /* HP Chromebook 14 (Celeron 2955U) */
15441 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015442
15443 /* Dell Chromebook 11 */
15444 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jani Nikula9be64ee2015-10-30 14:50:24 +020015445
15446 /* Dell Chromebook 11 (2015 version) */
15447 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015448};
15449
15450static void intel_init_quirks(struct drm_device *dev)
15451{
15452 struct pci_dev *d = dev->pdev;
15453 int i;
15454
15455 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15456 struct intel_quirk *q = &intel_quirks[i];
15457
15458 if (d->device == q->device &&
15459 (d->subsystem_vendor == q->subsystem_vendor ||
15460 q->subsystem_vendor == PCI_ANY_ID) &&
15461 (d->subsystem_device == q->subsystem_device ||
15462 q->subsystem_device == PCI_ANY_ID))
15463 q->hook(dev);
15464 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015465 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15466 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15467 intel_dmi_quirks[i].hook(dev);
15468 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015469}
15470
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015471/* Disable the VGA plane that we never use */
15472static void i915_disable_vga(struct drm_device *dev)
15473{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015474 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015475 u8 sr1;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015476 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015477
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015478 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015479 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015480 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015481 sr1 = inb(VGA_SR_DATA);
15482 outb(sr1 | 1<<5, VGA_SR_DATA);
15483 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15484 udelay(300);
15485
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015486 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015487 POSTING_READ(vga_reg);
15488}
15489
Daniel Vetterf8175862012-04-10 15:50:11 +020015490void intel_modeset_init_hw(struct drm_device *dev)
15491{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015492 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015493
Ville Syrjäläb6283052015-06-03 15:45:07 +030015494 intel_update_cdclk(dev);
Maarten Lankhorst1a617b72015-12-03 14:31:06 +010015495
15496 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
15497
Daniel Vetterf8175862012-04-10 15:50:11 +020015498 intel_init_clock_gating(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015499}
15500
Matt Roperd93c0372015-12-03 11:37:41 -080015501/*
15502 * Calculate what we think the watermarks should be for the state we've read
15503 * out of the hardware and then immediately program those watermarks so that
15504 * we ensure the hardware settings match our internal state.
15505 *
15506 * We can calculate what we think WM's should be by creating a duplicate of the
15507 * current state (which was constructed during hardware readout) and running it
15508 * through the atomic check code to calculate new watermark values in the
15509 * state object.
15510 */
15511static void sanitize_watermarks(struct drm_device *dev)
15512{
15513 struct drm_i915_private *dev_priv = to_i915(dev);
15514 struct drm_atomic_state *state;
15515 struct drm_crtc *crtc;
15516 struct drm_crtc_state *cstate;
15517 struct drm_modeset_acquire_ctx ctx;
15518 int ret;
15519 int i;
15520
15521 /* Only supported on platforms that use atomic watermark design */
Matt Ropered4a6a72016-02-23 17:20:13 -080015522 if (!dev_priv->display.optimize_watermarks)
Matt Roperd93c0372015-12-03 11:37:41 -080015523 return;
15524
15525 /*
15526 * We need to hold connection_mutex before calling duplicate_state so
15527 * that the connector loop is protected.
15528 */
15529 drm_modeset_acquire_init(&ctx, 0);
15530retry:
Matt Roper0cd12622016-01-12 07:13:37 -080015531 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Matt Roperd93c0372015-12-03 11:37:41 -080015532 if (ret == -EDEADLK) {
15533 drm_modeset_backoff(&ctx);
15534 goto retry;
15535 } else if (WARN_ON(ret)) {
Matt Roper0cd12622016-01-12 07:13:37 -080015536 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015537 }
15538
15539 state = drm_atomic_helper_duplicate_state(dev, &ctx);
15540 if (WARN_ON(IS_ERR(state)))
Matt Roper0cd12622016-01-12 07:13:37 -080015541 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015542
Matt Ropered4a6a72016-02-23 17:20:13 -080015543 /*
15544 * Hardware readout is the only time we don't want to calculate
15545 * intermediate watermarks (since we don't trust the current
15546 * watermarks).
15547 */
15548 to_intel_atomic_state(state)->skip_intermediate_wm = true;
15549
Matt Roperd93c0372015-12-03 11:37:41 -080015550 ret = intel_atomic_check(dev, state);
15551 if (ret) {
15552 /*
15553 * If we fail here, it means that the hardware appears to be
15554 * programmed in a way that shouldn't be possible, given our
15555 * understanding of watermark requirements. This might mean a
15556 * mistake in the hardware readout code or a mistake in the
15557 * watermark calculations for a given platform. Raise a WARN
15558 * so that this is noticeable.
15559 *
15560 * If this actually happens, we'll have to just leave the
15561 * BIOS-programmed watermarks untouched and hope for the best.
15562 */
15563 WARN(true, "Could not determine valid watermarks for inherited state\n");
Matt Roper0cd12622016-01-12 07:13:37 -080015564 goto fail;
Matt Roperd93c0372015-12-03 11:37:41 -080015565 }
15566
15567 /* Write calculated watermark values back */
Matt Roperd93c0372015-12-03 11:37:41 -080015568 for_each_crtc_in_state(state, crtc, cstate, i) {
15569 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
15570
Matt Ropered4a6a72016-02-23 17:20:13 -080015571 cs->wm.need_postvbl_update = true;
15572 dev_priv->display.optimize_watermarks(cs);
Matt Roperd93c0372015-12-03 11:37:41 -080015573 }
15574
15575 drm_atomic_state_free(state);
Matt Roper0cd12622016-01-12 07:13:37 -080015576fail:
Matt Roperd93c0372015-12-03 11:37:41 -080015577 drm_modeset_drop_locks(&ctx);
15578 drm_modeset_acquire_fini(&ctx);
15579}
15580
Jesse Barnes79e53942008-11-07 14:24:08 -080015581void intel_modeset_init(struct drm_device *dev)
15582{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015583 struct drm_i915_private *dev_priv = to_i915(dev);
15584 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015585 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015586 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015587 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015588
15589 drm_mode_config_init(dev);
15590
15591 dev->mode_config.min_width = 0;
15592 dev->mode_config.min_height = 0;
15593
Dave Airlie019d96c2011-09-29 16:20:42 +010015594 dev->mode_config.preferred_depth = 24;
15595 dev->mode_config.prefer_shadow = 1;
15596
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015597 dev->mode_config.allow_fb_modifiers = true;
15598
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015599 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015600
Jesse Barnesb690e962010-07-19 13:53:12 -070015601 intel_init_quirks(dev);
15602
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015603 intel_init_pm(dev);
15604
Ben Widawskye3c74752013-04-05 13:12:39 -070015605 if (INTEL_INFO(dev)->num_pipes == 0)
15606 return;
15607
Lukas Wunner69f92f62015-07-15 13:57:35 +020015608 /*
15609 * There may be no VBT; and if the BIOS enabled SSC we can
15610 * just keep using it to avoid unnecessary flicker. Whereas if the
15611 * BIOS isn't using it, don't assume it will work even if the VBT
15612 * indicates as much.
15613 */
15614 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
15615 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15616 DREF_SSC1_ENABLE);
15617
15618 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
15619 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
15620 bios_lvds_use_ssc ? "en" : "dis",
15621 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
15622 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
15623 }
15624 }
15625
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015626 if (IS_GEN2(dev)) {
15627 dev->mode_config.max_width = 2048;
15628 dev->mode_config.max_height = 2048;
15629 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015630 dev->mode_config.max_width = 4096;
15631 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015632 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015633 dev->mode_config.max_width = 8192;
15634 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015635 }
Damien Lespiau068be562014-03-28 14:17:49 +000015636
Ville Syrjälädc41c152014-08-13 11:57:05 +030015637 if (IS_845G(dev) || IS_I865G(dev)) {
15638 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15639 dev->mode_config.cursor_height = 1023;
15640 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015641 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15642 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15643 } else {
15644 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15645 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15646 }
15647
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030015648 dev->mode_config.fb_base = ggtt->mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015649
Zhao Yakui28c97732009-10-09 11:39:41 +080015650 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015651 INTEL_INFO(dev)->num_pipes,
15652 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015653
Damien Lespiau055e3932014-08-18 13:49:10 +010015654 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015655 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015656 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015657 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015658 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015659 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015660 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015661 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015662 }
15663
Ville Syrjäläbfa7df02015-09-24 23:29:18 +030015664 intel_update_czclk(dev_priv);
15665 intel_update_cdclk(dev);
15666
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015667 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015668
Ville Syrjäläb2045352016-05-13 23:41:27 +030015669 if (dev_priv->max_cdclk_freq == 0)
15670 intel_update_max_cdclk(dev);
15671
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015672 /* Just disable it once at startup */
15673 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015674 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015675
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015676 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015677 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015678 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015679
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015680 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015681 struct intel_initial_plane_config plane_config = {};
15682
Jesse Barnes46f297f2014-03-07 08:57:48 -080015683 if (!crtc->active)
15684 continue;
15685
Jesse Barnes46f297f2014-03-07 08:57:48 -080015686 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015687 * Note that reserving the BIOS fb up front prevents us
15688 * from stuffing other stolen allocations like the ring
15689 * on top. This prevents some ugliness at boot time, and
15690 * can even allow for smooth boot transitions if the BIOS
15691 * fb is large enough for the active pipe configuration.
15692 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015693 dev_priv->display.get_initial_plane_config(crtc,
15694 &plane_config);
15695
15696 /*
15697 * If the fb is shared between multiple heads, we'll
15698 * just get the first one.
15699 */
15700 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015701 }
Matt Roperd93c0372015-12-03 11:37:41 -080015702
15703 /*
15704 * Make sure hardware watermarks really match the state we read out.
15705 * Note that we need to do this after reconstructing the BIOS fb's
15706 * since the watermark calculation done here will use pstate->fb.
15707 */
15708 sanitize_watermarks(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015709}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015710
Daniel Vetter7fad7982012-07-04 17:51:47 +020015711static void intel_enable_pipe_a(struct drm_device *dev)
15712{
15713 struct intel_connector *connector;
15714 struct drm_connector *crt = NULL;
15715 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015716 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015717
15718 /* We can't just switch on the pipe A, we need to set things up with a
15719 * proper mode and output configuration. As a gross hack, enable pipe A
15720 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015721 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015722 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15723 crt = &connector->base;
15724 break;
15725 }
15726 }
15727
15728 if (!crt)
15729 return;
15730
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015731 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015732 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015733}
15734
Daniel Vetterfa555832012-10-10 23:14:00 +020015735static bool
15736intel_check_plane_mapping(struct intel_crtc *crtc)
15737{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015738 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010015739 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä649636e2015-09-22 19:50:01 +030015740 u32 val;
Daniel Vetterfa555832012-10-10 23:14:00 +020015741
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015742 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015743 return true;
15744
Ville Syrjälä649636e2015-09-22 19:50:01 +030015745 val = I915_READ(DSPCNTR(!crtc->plane));
Daniel Vetterfa555832012-10-10 23:14:00 +020015746
15747 if ((val & DISPLAY_PLANE_ENABLE) &&
15748 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15749 return false;
15750
15751 return true;
15752}
15753
Ville Syrjälä02e93c32015-08-26 19:39:19 +030015754static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15755{
15756 struct drm_device *dev = crtc->base.dev;
15757 struct intel_encoder *encoder;
15758
15759 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15760 return true;
15761
15762 return false;
15763}
15764
Ville Syrjälädd756192016-02-17 21:28:45 +020015765static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
15766{
15767 struct drm_device *dev = encoder->base.dev;
15768 struct intel_connector *connector;
15769
15770 for_each_connector_on_encoder(dev, &encoder->base, connector)
15771 return true;
15772
15773 return false;
15774}
15775
Daniel Vetter24929352012-07-02 20:28:59 +020015776static void intel_sanitize_crtc(struct intel_crtc *crtc)
15777{
15778 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +010015779 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula4d1de972016-03-18 17:05:42 +020015780 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter24929352012-07-02 20:28:59 +020015781
Daniel Vetter24929352012-07-02 20:28:59 +020015782 /* Clear any frame start delays used for debugging left by the BIOS */
Jani Nikula4d1de972016-03-18 17:05:42 +020015783 if (!transcoder_is_dsi(cpu_transcoder)) {
15784 i915_reg_t reg = PIPECONF(cpu_transcoder);
15785
15786 I915_WRITE(reg,
15787 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15788 }
Daniel Vetter24929352012-07-02 20:28:59 +020015789
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015790 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015791 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015792 if (crtc->active) {
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015793 struct intel_plane *plane;
15794
Daniel Vetter96256042015-02-13 21:03:42 +010015795 drm_crtc_vblank_on(&crtc->base);
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015796
15797 /* Disable everything but the primary plane */
15798 for_each_intel_plane_on_crtc(dev, crtc, plane) {
15799 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15800 continue;
15801
15802 plane->disable_plane(&plane->base, &crtc->base);
15803 }
Daniel Vetter96256042015-02-13 21:03:42 +010015804 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015805
Daniel Vetter24929352012-07-02 20:28:59 +020015806 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015807 * disable the crtc (and hence change the state) if it is wrong. Note
15808 * that gen4+ has a fixed plane -> pipe mapping. */
15809 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015810 bool plane;
15811
Ville Syrjälä78108b72016-05-27 20:59:19 +030015812 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
15813 crtc->base.base.id, crtc->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015814
15815 /* Pipe has the wrong plane attached and the plane is active.
15816 * Temporarily change the plane mapping and disable everything
15817 * ... */
15818 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015819 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015820 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015821 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015822 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015823 }
Daniel Vetter24929352012-07-02 20:28:59 +020015824
Daniel Vetter7fad7982012-07-04 17:51:47 +020015825 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15826 crtc->pipe == PIPE_A && !crtc->active) {
15827 /* BIOS forgot to enable pipe A, this mostly happens after
15828 * resume. Force-enable the pipe to fix this, the update_dpms
15829 * call below we restore the pipe to the right state, but leave
15830 * the required bits on. */
15831 intel_enable_pipe_a(dev);
15832 }
15833
Daniel Vetter24929352012-07-02 20:28:59 +020015834 /* Adjust the state of the output pipe according to whether we
15835 * have active connectors/encoders. */
Maarten Lankhorst842e0302016-03-02 15:48:01 +010015836 if (crtc->active && !intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015837 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015838
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015839 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015840 /*
15841 * We start out with underrun reporting disabled to avoid races.
15842 * For correct bookkeeping mark this on active crtcs.
15843 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015844 * Also on gmch platforms we dont have any hardware bits to
15845 * disable the underrun reporting. Which means we need to start
15846 * out with underrun reporting disabled also on inactive pipes,
15847 * since otherwise we'll complain about the garbage we read when
15848 * e.g. coming up after runtime pm.
15849 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015850 * No protection against concurrent access is required - at
15851 * worst a fifo underrun happens which also sets this to false.
15852 */
15853 crtc->cpu_fifo_underrun_disabled = true;
15854 crtc->pch_fifo_underrun_disabled = true;
15855 }
Daniel Vetter24929352012-07-02 20:28:59 +020015856}
15857
15858static void intel_sanitize_encoder(struct intel_encoder *encoder)
15859{
15860 struct intel_connector *connector;
15861 struct drm_device *dev = encoder->base.dev;
15862
15863 /* We need to check both for a crtc link (meaning that the
15864 * encoder is active and trying to read from a pipe) and the
15865 * pipe itself being active. */
15866 bool has_active_crtc = encoder->base.crtc &&
15867 to_intel_crtc(encoder->base.crtc)->active;
15868
Ville Syrjälädd756192016-02-17 21:28:45 +020015869 if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020015870 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15871 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015872 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015873
15874 /* Connector is active, but has no active pipe. This is
15875 * fallout from our resume register restoring. Disable
15876 * the encoder manually again. */
15877 if (encoder->base.crtc) {
15878 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15879 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015880 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015881 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015882 if (encoder->post_disable)
15883 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015884 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015885 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015886
15887 /* Inconsistent output/port/pipe state happens presumably due to
15888 * a bug in one of the get_hw_state functions. Or someplace else
15889 * in our code, like the register restore mess on resume. Clamp
15890 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015891 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015892 if (connector->encoder != encoder)
15893 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015894 connector->base.dpms = DRM_MODE_DPMS_OFF;
15895 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015896 }
15897 }
15898 /* Enabled encoders without active connectors will be fixed in
15899 * the crtc fixup. */
15900}
15901
Imre Deak04098752014-02-18 00:02:16 +020015902void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015903{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015904 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020015905 i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015906
Imre Deak04098752014-02-18 00:02:16 +020015907 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15908 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15909 i915_disable_vga(dev);
15910 }
15911}
15912
15913void i915_redisable_vga(struct drm_device *dev)
15914{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015915 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak04098752014-02-18 00:02:16 +020015916
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015917 /* This function can be called both from intel_modeset_setup_hw_state or
15918 * at a very early point in our resume sequence, where the power well
15919 * structures are not yet restored. Since this function is at a very
15920 * paranoid "someone might have enabled VGA while we were not looking"
15921 * level, just check if the power well is enabled instead of trying to
15922 * follow the "don't touch the power well if we don't need it" policy
15923 * the rest of the driver uses. */
Imre Deak6392f842016-02-12 18:55:13 +020015924 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015925 return;
15926
Imre Deak04098752014-02-18 00:02:16 +020015927 i915_redisable_vga_power_on(dev);
Imre Deak6392f842016-02-12 18:55:13 +020015928
15929 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015930}
15931
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015932static bool primary_get_hw_state(struct intel_plane *plane)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015933{
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015934 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015935
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015936 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015937}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015938
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015939/* FIXME read out full plane state for all planes */
15940static void readout_plane_state(struct intel_crtc *crtc)
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015941{
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015942 struct drm_plane *primary = crtc->base.primary;
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015943 struct intel_plane_state *plane_state =
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015944 to_intel_plane_state(primary->state);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015945
Matt Roper19b8d382015-09-24 15:53:17 -070015946 plane_state->visible = crtc->active &&
Maarten Lankhorstb26d3ea2015-09-23 16:11:41 +020015947 primary_get_hw_state(to_intel_plane(primary));
15948
15949 if (plane_state->visible)
15950 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015951}
15952
Daniel Vetter30e984d2013-06-05 13:34:17 +020015953static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015954{
Chris Wilsonfac5e232016-07-04 11:34:36 +010015955 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015956 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015957 struct intel_crtc *crtc;
15958 struct intel_encoder *encoder;
15959 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015960 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015961
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015962 dev_priv->active_crtcs = 0;
15963
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015964 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015965 struct intel_crtc_state *crtc_state = crtc->config;
15966 int pixclk = 0;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015967
Daniel Vetterec2dc6a2016-05-09 16:34:09 +020015968 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015969 memset(crtc_state, 0, sizeof(*crtc_state));
15970 crtc_state->base.crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015971
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015972 crtc_state->base.active = crtc_state->base.enable =
15973 dev_priv->display.get_pipe_config(crtc, crtc_state);
15974
15975 crtc->base.enabled = crtc_state->base.enable;
15976 crtc->active = crtc_state->base.active;
15977
15978 if (crtc_state->base.active) {
15979 dev_priv->active_crtcs |= 1 << crtc->pipe;
15980
Clint Taylorc89e39f2016-05-13 23:41:21 +030015981 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015982 pixclk = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjälä9558d152016-05-13 23:41:20 +030015983 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015984 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
15985 else
15986 WARN_ON(dev_priv->display.modeset_calc_cdclk);
Ville Syrjälä9558d152016-05-13 23:41:20 +030015987
15988 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
15989 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
15990 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
Maarten Lankhorst565602d2015-12-10 12:33:57 +010015991 }
15992
15993 dev_priv->min_pixclk[crtc->pipe] = pixclk;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015994
Ville Syrjäläf9cd7b82015-09-10 18:59:08 +030015995 readout_plane_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015996
Ville Syrjälä78108b72016-05-27 20:59:19 +030015997 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
15998 crtc->base.base.id, crtc->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015999 crtc->active ? "enabled" : "disabled");
16000 }
16001
Daniel Vetter53589012013-06-05 13:34:16 +020016002 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16003 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16004
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016005 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16006 &pll->config.hw_state);
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016007 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010016008 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016009 if (crtc->active && crtc->config->shared_dpll == pll)
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016010 pll->config.crtc_mask |= 1 << crtc->pipe;
Daniel Vetter53589012013-06-05 13:34:16 +020016011 }
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016012 pll->active_mask = pll->config.crtc_mask;
Daniel Vetter53589012013-06-05 13:34:16 +020016013
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020016014 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020016015 pll->name, pll->config.crtc_mask, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020016016 }
16017
Damien Lespiaub2784e12014-08-05 11:29:37 +010016018 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016019 pipe = 0;
16020
16021 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070016022 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16023 encoder->base.crtc = &crtc->base;
Ville Syrjälä253c84c2016-06-22 21:57:01 +030016024 crtc->config->output_types |= 1 << encoder->type;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016025 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020016026 } else {
16027 encoder->base.crtc = NULL;
16028 }
16029
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016030 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020016031 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030016032 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016033 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010016034 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020016035 }
16036
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020016037 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020016038 if (connector->get_hw_state(connector)) {
16039 connector->base.dpms = DRM_MODE_DPMS_ON;
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016040
16041 encoder = connector->encoder;
16042 connector->base.encoder = &encoder->base;
16043
16044 if (encoder->base.crtc &&
16045 encoder->base.crtc->state->active) {
16046 /*
16047 * This has to be done during hardware readout
16048 * because anything calling .crtc_disable may
16049 * rely on the connector_mask being accurate.
16050 */
16051 encoder->base.crtc->state->connector_mask |=
16052 1 << drm_connector_index(&connector->base);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +010016053 encoder->base.crtc->state->encoder_mask |=
16054 1 << drm_encoder_index(&encoder->base);
Maarten Lankhorst2aa974c2016-01-06 14:53:25 +010016055 }
16056
Daniel Vetter24929352012-07-02 20:28:59 +020016057 } else {
16058 connector->base.dpms = DRM_MODE_DPMS_OFF;
16059 connector->base.encoder = NULL;
16060 }
16061 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16062 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030016063 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020016064 connector->base.encoder ? "enabled" : "disabled");
16065 }
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016066
16067 for_each_intel_crtc(dev, crtc) {
16068 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16069
16070 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16071 if (crtc->base.state->active) {
16072 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16073 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
16074 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16075
16076 /*
16077 * The initial mode needs to be set in order to keep
16078 * the atomic core happy. It wants a valid mode if the
16079 * crtc's enabled, so we do the above call.
16080 *
16081 * At this point some state updated by the connectors
16082 * in their ->detect() callback has not run yet, so
16083 * no recalculation can be done yet.
16084 *
16085 * Even if we could do a recalculation and modeset
16086 * right now it would cause a double modeset if
16087 * fbdev or userspace chooses a different initial mode.
16088 *
16089 * If that happens, someone indicated they wanted a
16090 * mode change, which means it's safe to do a full
16091 * recalculation.
16092 */
16093 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Ville Syrjälä9eca68322015-09-10 18:59:10 +030016094
16095 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
16096 update_scanline_offset(crtc);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016097 }
Ville Syrjäläe3b247d2016-02-17 21:41:09 +020016098
16099 intel_pipe_config_sanity_check(dev_priv, crtc->config);
Ville Syrjälä7f4c6282015-09-10 18:59:07 +030016100 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020016101}
16102
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016103/* Scan out the current hw modeset state,
16104 * and sanitizes it to the current state
16105 */
16106static void
16107intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020016108{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016109 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter30e984d2013-06-05 13:34:17 +020016110 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016111 struct intel_crtc *crtc;
16112 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020016113 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020016114
16115 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020016116
16117 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010016118 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020016119 intel_sanitize_encoder(encoder);
16120 }
16121
Damien Lespiau055e3932014-08-18 13:49:10 +010016122 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020016123 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
16124 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020016125 intel_dump_pipe_config(crtc, crtc->config,
16126 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020016127 }
Daniel Vetter9a935852012-07-05 22:34:27 +020016128
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020016129 intel_modeset_update_connector_atomic_state(dev);
16130
Daniel Vetter35c95372013-07-17 06:55:04 +020016131 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16132 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16133
Maarten Lankhorst2dd66ebd2016-03-14 09:27:52 +010016134 if (!pll->on || pll->active_mask)
Daniel Vetter35c95372013-07-17 06:55:04 +020016135 continue;
16136
16137 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
16138
Ander Conselvan de Oliveira2edd6442016-03-08 17:46:21 +020016139 pll->funcs.disable(dev_priv, pll);
Daniel Vetter35c95372013-07-17 06:55:04 +020016140 pll->on = false;
16141 }
16142
Wayne Boyer666a4532015-12-09 12:29:35 -080016143 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030016144 vlv_wm_get_hw_state(dev);
16145 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000016146 skl_wm_get_hw_state(dev);
16147 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030016148 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016149
16150 for_each_intel_crtc(dev, crtc) {
16151 unsigned long put_domains;
16152
Maarten Lankhorst74bff5f2016-02-10 13:49:36 +010016153 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020016154 if (WARN_ON(put_domains))
16155 modeset_put_power_domains(dev_priv, put_domains);
16156 }
16157 intel_display_set_init_power(dev_priv, false);
Paulo Zanoni010cf732016-01-19 11:35:48 -020016158
16159 intel_fbc_init_pipe_state(dev_priv);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016160}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030016161
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016162void intel_display_resume(struct drm_device *dev)
16163{
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016164 struct drm_i915_private *dev_priv = to_i915(dev);
16165 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
16166 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016167 int ret;
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016168 bool setup = false;
Daniel Vetterf30da182013-04-11 20:22:50 +020016169
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016170 dev_priv->modeset_restore_state = NULL;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016171
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016172 /*
16173 * This is a cludge because with real atomic modeset mode_config.mutex
16174 * won't be taken. Unfortunately some probed state like
16175 * audio_codec_enable is still protected by mode_config.mutex, so lock
16176 * it here for now.
16177 */
16178 mutex_lock(&dev->mode_config.mutex);
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016179 drm_modeset_acquire_init(&ctx, 0);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016180
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016181retry:
16182 ret = drm_modeset_lock_all_ctx(dev, &ctx);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016183
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016184 if (ret == 0 && !setup) {
16185 setup = true;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016186
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016187 intel_modeset_setup_hw_state(dev);
16188 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010016189 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020016190
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016191 if (ret == 0 && state) {
16192 struct drm_crtc_state *crtc_state;
16193 struct drm_crtc *crtc;
16194 int i;
16195
16196 state->acquire_ctx = &ctx;
16197
Ville Syrjäläe3d54572016-05-13 10:10:42 -070016198 /* ignore any reset values/BIOS leftovers in the WM registers */
16199 to_intel_atomic_state(state)->skip_intermediate_wm = true;
16200
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016201 for_each_crtc_in_state(state, crtc, crtc_state, i) {
16202 /*
16203 * Force recalculation even if we restore
16204 * current state. With fast modeset this may not result
16205 * in a modeset when the state is compatible.
16206 */
16207 crtc_state->mode_changed = true;
16208 }
16209
16210 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016211 }
16212
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016213 if (ret == -EDEADLK) {
16214 drm_modeset_backoff(&ctx);
16215 goto retry;
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016216 }
16217
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016218 drm_modeset_drop_locks(&ctx);
16219 drm_modeset_acquire_fini(&ctx);
Maarten Lankhorstea49c9a2016-02-16 15:27:42 +010016220 mutex_unlock(&dev->mode_config.mutex);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020016221
Maarten Lankhorste2c8b872016-02-16 10:06:14 +010016222 if (ret) {
16223 DRM_ERROR("Restoring old state failed with %i\n", ret);
16224 drm_atomic_state_free(state);
16225 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010016226}
16227
16228void intel_modeset_gem_init(struct drm_device *dev)
16229{
Chris Wilsondc979972016-05-10 14:10:04 +010016230 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016231 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070016232 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016233 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080016234
Chris Wilsondc979972016-05-10 14:10:04 +010016235 intel_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016236
Chris Wilson1833b132012-05-09 11:56:28 +010016237 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020016238
Chris Wilson1ee8da62016-05-12 12:43:23 +010016239 intel_setup_overlay(dev_priv);
Jesse Barnes484b41d2014-03-07 08:57:55 -080016240
16241 /*
16242 * Make sure any fbs we allocated at startup are properly
16243 * pinned & fenced. When we do the allocation it's too early
16244 * for this.
16245 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010016246 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070016247 obj = intel_fb_obj(c->primary->fb);
16248 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080016249 continue;
16250
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016251 mutex_lock(&dev->struct_mutex);
Ville Syrjälä3465c582016-02-15 22:54:43 +020016252 ret = intel_pin_and_fence_fb_obj(c->primary->fb,
16253 c->primary->state->rotation);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010016254 mutex_unlock(&dev->struct_mutex);
16255 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080016256 DRM_ERROR("failed to pin boot fb on pipe %d\n",
16257 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100016258 drm_framebuffer_unreference(c->primary->fb);
Daniel Vetter5a21b662016-05-24 17:13:53 +020016259 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016260 c->primary->crtc = c->primary->state->crtc = NULL;
Daniel Vetter5a21b662016-05-24 17:13:53 +020016261 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020016262 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080016263 }
16264 }
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016265}
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020016266
Chris Wilson1ebaa0b2016-06-24 14:00:15 +010016267int intel_connector_register(struct drm_connector *connector)
16268{
16269 struct intel_connector *intel_connector = to_intel_connector(connector);
16270 int ret;
16271
16272 ret = intel_backlight_device_register(intel_connector);
16273 if (ret)
16274 goto err;
16275
16276 return 0;
16277
16278err:
16279 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080016280}
16281
Chris Wilsonc191eca2016-06-17 11:40:33 +010016282void intel_connector_unregister(struct drm_connector *connector)
Imre Deak4932e2c2014-02-11 17:12:48 +020016283{
Chris Wilsone63d87c2016-06-17 11:40:34 +010016284 struct intel_connector *intel_connector = to_intel_connector(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016285
Chris Wilsone63d87c2016-06-17 11:40:34 +010016286 intel_backlight_device_unregister(intel_connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016287 intel_panel_destroy_backlight(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020016288}
16289
Jesse Barnes79e53942008-11-07 14:24:08 -080016290void intel_modeset_cleanup(struct drm_device *dev)
16291{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016292 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -070016293
Chris Wilsondc979972016-05-10 14:10:04 +010016294 intel_disable_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +020016295
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016296 /*
16297 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020016298 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016299 * experience fancy races otherwise.
16300 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020016301 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070016302
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016303 /*
16304 * Due to the hpd irq storm handling the hotplug work can re-arm the
16305 * poll handlers. Hence disable polling after hpd handling is shut down.
16306 */
Keith Packardf87ea762010-10-03 19:36:26 -070016307 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020016308
Jesse Barnes723bfd72010-10-07 16:01:13 -070016309 intel_unregister_dsm_handler();
16310
Paulo Zanonic937ab3e52016-01-19 11:35:46 -020016311 intel_fbc_global_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050016312
Chris Wilson1630fe72011-07-08 12:22:42 +010016313 /* flush any delayed tasks or pending work */
16314 flush_scheduled_work();
16315
Jesse Barnes79e53942008-11-07 14:24:08 -080016316 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010016317
Chris Wilson1ee8da62016-05-12 12:43:23 +010016318 intel_cleanup_overlay(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +030016319
Chris Wilsondc979972016-05-10 14:10:04 +010016320 intel_cleanup_gt_powersave(dev_priv);
Daniel Vetterf5949142016-01-13 11:55:28 +010016321
16322 intel_teardown_gmbus(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080016323}
16324
Chris Wilsondf0e9242010-09-09 16:20:55 +010016325void intel_connector_attach_encoder(struct intel_connector *connector,
16326 struct intel_encoder *encoder)
16327{
16328 connector->encoder = encoder;
16329 drm_mode_connector_attach_encoder(&connector->base,
16330 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080016331}
Dave Airlie28d52042009-09-21 14:33:58 +100016332
16333/*
16334 * set vga decode state - true == enable VGA decode
16335 */
16336int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
16337{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016338 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsona885b3c2013-12-17 14:34:50 +000016339 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100016340 u16 gmch_ctrl;
16341
Chris Wilson75fa0412014-02-07 18:37:02 -020016342 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
16343 DRM_ERROR("failed to read control word\n");
16344 return -EIO;
16345 }
16346
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020016347 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
16348 return 0;
16349
Dave Airlie28d52042009-09-21 14:33:58 +100016350 if (state)
16351 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
16352 else
16353 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020016354
16355 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
16356 DRM_ERROR("failed to write control word\n");
16357 return -EIO;
16358 }
16359
Dave Airlie28d52042009-09-21 14:33:58 +100016360 return 0;
16361}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016362
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016363struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016364
16365 u32 power_well_driver;
16366
Chris Wilson63b66e52013-08-08 15:12:06 +020016367 int num_transcoders;
16368
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016369 struct intel_cursor_error_state {
16370 u32 control;
16371 u32 position;
16372 u32 base;
16373 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010016374 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016375
16376 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016377 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016378 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030016379 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010016380 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016381
16382 struct intel_plane_error_state {
16383 u32 control;
16384 u32 stride;
16385 u32 size;
16386 u32 pos;
16387 u32 addr;
16388 u32 surface;
16389 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010016390 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020016391
16392 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020016393 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020016394 enum transcoder cpu_transcoder;
16395
16396 u32 conf;
16397
16398 u32 htotal;
16399 u32 hblank;
16400 u32 hsync;
16401 u32 vtotal;
16402 u32 vblank;
16403 u32 vsync;
16404 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016405};
16406
16407struct intel_display_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +010016408intel_display_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016409{
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016410 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020016411 int transcoders[] = {
16412 TRANSCODER_A,
16413 TRANSCODER_B,
16414 TRANSCODER_C,
16415 TRANSCODER_EDP,
16416 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016417 int i;
16418
Chris Wilsonc0336662016-05-06 15:40:21 +010016419 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson63b66e52013-08-08 15:12:06 +020016420 return NULL;
16421
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016422 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016423 if (error == NULL)
16424 return NULL;
16425
Chris Wilsonc0336662016-05-06 15:40:21 +010016426 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016427 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
16428
Damien Lespiau055e3932014-08-18 13:49:10 +010016429 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020016430 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016431 __intel_display_power_is_enabled(dev_priv,
16432 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020016433 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016434 continue;
16435
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030016436 error->cursor[i].control = I915_READ(CURCNTR(i));
16437 error->cursor[i].position = I915_READ(CURPOS(i));
16438 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016439
16440 error->plane[i].control = I915_READ(DSPCNTR(i));
16441 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016442 if (INTEL_GEN(dev_priv) <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030016443 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016444 error->plane[i].pos = I915_READ(DSPPOS(i));
16445 }
Chris Wilsonc0336662016-05-06 15:40:21 +010016446 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
Paulo Zanonica291362013-03-06 20:03:14 -030016447 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc0336662016-05-06 15:40:21 +010016448 if (INTEL_GEN(dev_priv) >= 4) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016449 error->plane[i].surface = I915_READ(DSPSURF(i));
16450 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
16451 }
16452
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016453 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030016454
Chris Wilsonc0336662016-05-06 15:40:21 +010016455 if (HAS_GMCH_DISPLAY(dev_priv))
Imre Deakf301b1e2014-04-18 15:55:04 +030016456 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020016457 }
16458
Jani Nikula4d1de972016-03-18 17:05:42 +020016459 /* Note: this does not include DSI transcoders. */
Chris Wilsonc0336662016-05-06 15:40:21 +010016460 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +030016461 if (HAS_DDI(dev_priv))
Chris Wilson63b66e52013-08-08 15:12:06 +020016462 error->num_transcoders++; /* Account for eDP. */
16463
16464 for (i = 0; i < error->num_transcoders; i++) {
16465 enum transcoder cpu_transcoder = transcoders[i];
16466
Imre Deakddf9c532013-11-27 22:02:02 +020016467 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020016468 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020016469 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016470 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020016471 continue;
16472
Chris Wilson63b66e52013-08-08 15:12:06 +020016473 error->transcoder[i].cpu_transcoder = cpu_transcoder;
16474
16475 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
16476 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
16477 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
16478 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
16479 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
16480 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
16481 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016482 }
16483
16484 return error;
16485}
16486
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016487#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
16488
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016489void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016490intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016491 struct drm_device *dev,
16492 struct intel_display_error_state *error)
16493{
Chris Wilsonfac5e232016-07-04 11:34:36 +010016494 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016495 int i;
16496
Chris Wilson63b66e52013-08-08 15:12:06 +020016497 if (!error)
16498 return;
16499
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016500 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020016501 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016502 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030016503 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010016504 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016505 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020016506 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016507 onoff(error->pipe[i].power_domain_on));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016508 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030016509 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016510
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016511 err_printf(m, "Plane [%d]:\n", i);
16512 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
16513 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016514 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016515 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
16516 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030016517 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030016518 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016519 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016520 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016521 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
16522 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016523 }
16524
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030016525 err_printf(m, "Cursor [%d]:\n", i);
16526 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
16527 err_printf(m, " POS: %08x\n", error->cursor[i].position);
16528 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016529 }
Chris Wilson63b66e52013-08-08 15:12:06 +020016530
16531 for (i = 0; i < error->num_transcoders; i++) {
Jani Nikulada205632016-03-15 21:51:10 +020016532 err_printf(m, "CPU transcoder: %s\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020016533 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020016534 err_printf(m, " Power: %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +020016535 onoff(error->transcoder[i].power_domain_on));
Chris Wilson63b66e52013-08-08 15:12:06 +020016536 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
16537 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
16538 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
16539 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
16540 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
16541 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16542 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16543 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016544}